PageRenderTime 69ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 1ms

/shop/csv/import_mainte.php

https://github.com/shoitokazu/GNS-idemitsu-Job
PHP | 167 lines | 148 code | 13 blank | 6 comment | 24 complexity | 2154e5dc073ebd64626e22a20dbf4aea MD5 | raw file
  1. <?php
  2. include 'header.php';
  3. include '../../include/config_fields.php';
  4. $id = $_REQUEST['id'];
  5. $ssid = $uid;
  6. $mode = $_REQUEST['mode'];
  7. $add = $_REQUEST['add'];
  8. $fname = $_FILES['csvfile']['name'];
  9. $err = $_FILES['csvfile']['error'];
  10. $tmp = $_FILES['csvfile']['tmp_name'];
  11. $table = "mainte_d";
  12. if ( $mode == 'ins' ) {
  13. switch ($err) {
  14. case UPLOAD_ERR_OK:
  15. if ($id=='') {
  16. echo "<p>IDが不正です。</p>";
  17. } else {
  18. //既存データを削除
  19. $where = "hid=$id";
  20. $where .= " and ssid=$ssid";
  21. if ($add=='') {
  22. $sql = "DELETE FROM $table WHERE $where";
  23. $rs = db_exec($conn, $sql);
  24. }
  25. $no = DLookUp("max(sort)", $table, $where);
  26. if ($no==false) $no=0;
  27. // CSVファイルを開く
  28. // $fp = fopen($tmp, "r") or die();
  29. //文字コードの変換
  30. $buf = mb_convert_encoding(file_get_contents($tmp), $client_charset, $csv_charset);
  31. if ($debug) echo $buf;
  32. $fp = tmpfile();
  33. fwrite($fp, $buf);
  34. rewind($fp);
  35. // 一行目を読んでヘッダにする
  36. $header = fgetcsv_reg($fp, 1024);
  37. $sql = "select * from $table limit 1";
  38. $rs = db_exec($conn, $sql);
  39. $fields_type = db_fields_type($rs);
  40. db_free($rs);
  41. $fields[0] = "hid";
  42. $types[0] = "int";
  43. $fields[1] = "sort";
  44. $types[1] = "int";
  45. $fields[2] = "ssid";
  46. $types[2] = "int";
  47. $i=3;
  48. foreach ($header as $f_jp) {
  49. $f = jp2db($f_jp);
  50. if ($f=='ID') continue;
  51. if ($f=='hid') continue;
  52. if ($f=='sort') continue;
  53. if ($f=='ssid') continue;
  54. if ($f=='company') continue;
  55. if ($fields_type[$f]=='') continue;
  56. $fields[$i] = $f;
  57. $types[$i] = $fields_type[$f];
  58. $i++;
  59. }
  60. $values[0] = $id;
  61. $values[2] = $ssid;
  62. while(($data = fgetcsv_reg($fp, 1024))) {
  63. $no++;
  64. $values[1] = $no;
  65. $i=3;
  66. foreach ($data as $k => $v) {
  67. $f_jp = $header[$k];
  68. $f = jp2db($f_jp);
  69. if ($f=='ID') continue;
  70. if ($f=='hid') continue;
  71. if ($f=='sort') continue;
  72. if ($f=='ssid') continue;
  73. if ($f=='company') continue;
  74. if ($fields_type[$f]=='') continue;
  75. $values[$i] = $v;
  76. $i++;
  77. }
  78. db_insert($conn, $table, $fields, $types, $values);
  79. }
  80. // ファイルを閉じる
  81. fclose($fp);
  82. }
  83. $errorMessage = '整備明細を上書き登録しました。';
  84. if ($add<>'') $errorMessage = '整備明細を追加登録しました。';
  85. break;
  86. case UPLOAD_ERR_INI_SIZE:
  87. $errorMessage = '値: 1; アップロードされたファイルは、php.ini の upload_max_filesize ディレクティブの値を超えています。';
  88. break;
  89. case UPLOAD_ERR_FORM_SIZE:
  90. $errorMessage = '値: 2; アップロードされたファイルは、HTMLフォームで指定された MAX_FILE_SIZE を超えています。';
  91. break;
  92. case UPLOAD_ERR_PARTIAL:
  93. $errorMessage = '値: 3; アップロードされたファイルは一部のみしかアップロードされていません。';
  94. break;
  95. case UPLOAD_ERR_NO_FILE:
  96. $errorMessage = '値: 4; ファイルはアップロードされませんでした。';
  97. break;
  98. case UPLOAD_ERR_NO_TMP_DIR:
  99. $errorMessage = '値: 6; テンポラリフォルダがありません。PHP 4.3.10 と PHP 5.0.3 で導入されました。';
  100. break;
  101. }
  102. }
  103. ?>
  104. <html>
  105. <head>
  106. <meta http-equiv="Content-Type" content="text/html; charset=<?=$html_charset?>">
  107. <title>整備明細CSV登録フォーム</title>
  108. <script type="text/javascript">
  109. function reloadWindows() {
  110. window.opener.location.reload();
  111. }
  112. </script>
  113. </head>
  114. <?php
  115. if ( $mode == 'ins' ) {
  116. echo "<body onLoad='reloadWindows()'>";
  117. } else {
  118. echo "<body>";
  119. }
  120. ?>
  121. <center>
  122. <table border="0" cellspacing="0" cellpadding="1" width="400">
  123. <tr><td><b>整備明細CSV登録</b></td></tr>
  124. <tr><td bgcolor="aaaaaa"></td></tr>
  125. </table>
  126. <br>
  127. <table border="0" cellspacing="1" cellpadding="2" bgcolor="#999999" width="360">
  128. <form enctype="multipart/form-data" action="?" method="POST">
  129. <tr><td colspan="2" bgcolor="#aaaaff">
  130. <table border="0" cellspacing="0" cellpadding="0" width="100%">
  131. <tr><td><b><font size="2">CSVファイルの選択</font></b></td><td align="right"><img src="" width="1" height="22"></td></tr>
  132. </table>
  133. </td></tr>
  134. <tr><td bgcolor="#eeeeff"><input name="csvfile" type="file" size="56" /></td></tr>
  135. <tr><td align="right" bgcolor="#ffffff">
  136. <input type="submit" value="上書き登録">
  137. <input type="submit" name="add" value="追加登録"></td></tr>
  138. <input type="hidden" name="mode" value="ins" />
  139. <input type="hidden" name="id" value="<?php echo $_REQUEST['id'] ?>">
  140. <input type="hidden" name="ssid" value="<?php echo $_REQUEST['ssid'] ?>">
  141. <input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
  142. </form>
  143. </table>
  144.  <b><font size="2" color="#cc0000"><?= $errorMessage ?></font></b> <br><br>
  145. <a href="javascript:window.close()"><b><font size="2">閉じる</font></b></a>
  146. <br><br><br>
  147. <table border="0" cellspacing="0" cellpadding="2" bgcolor="#aaaaaa" width="400">
  148. <tr><td bgcolor="aaaaaa"></td></tr>
  149. <tr><td align="center" bgcolor="#ffffff"><font size="2">Copyright &reg; YAMAHA BOATING SYSTEM</font></td></tr>
  150. </table>
  151. </center>
  152. </body>
  153. </html>
  154. <?php include 'footer.php';?>