SubmitManagementController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. namespace App\Http\Controllers\Backend\DataManagement;
  3. use Illuminate\Http\Request;
  4. use App\Http\Services\Backend\DataManagement\SubmitManagementService;
  5. use App\Http\Controllers\Controller;
  6. use App\Http\Services\ConstDef\GeneralConst;
  7. use App\Http\Services\CheckParamService;
  8. use Redirect;
  9. use ZipArchive;
  10. class SubmitManagementController
  11. {
  12. // 相關私有服務層調用器宣告
  13. private $submitManagementSv;
  14. private $checkParamSv;
  15. public function __construct()
  16. {
  17. // 建構服務層調用器
  18. $this->submitManagementSv = new SubmitManagementService();
  19. $this->checkParamSv = new CheckParamService();
  20. // 時區調整
  21. date_default_timezone_set("Asia/Taipei");
  22. }
  23. public function index()
  24. {
  25. // 渲染
  26. return view('admin.DataManagement.SubmitManagement', [
  27. 'duplicate' => GeneralConst::$duplicateMap,
  28. 'events' => $this->submitManagementSv->getEvents(),
  29. ]);
  30. }
  31. public function grid()
  32. {
  33. // 取得參數
  34. $param = $_GET;
  35. if ($param == null) exit();
  36. $draw = $param["draw"]; //客戶端傳來的查詢次數,無條件回傳用以核對
  37. $orderColumn = $param["order"][0]["column"] + 1; //前端從 0 開始送,但 mysql 從 1 開始算
  38. $orderDir = $param["order"][0]["dir"];
  39. $start = $param["start"]; // 頁碼
  40. $length = $param["length"]; // 一頁多大
  41. $searchValue = $param["search"]["value"];
  42. //客製化搜尋欄位
  43. $submitDate = explode("\n", $param["columns"][1]["search"]["value"]);
  44. $submitDateStart = $submitDate[0] ?? null;
  45. $submitDateFinal = $submitDate[1] ?? null;
  46. $iid = $param["columns"][2]["search"]["value"];
  47. $searchcol = $param["columns"][3]["search"]["value"];
  48. $duplicate = $param["columns"][4]["search"]["value"];
  49. // 驗證
  50. if ($submitDateStart == "") $submitDateStart = "1900-01-01";
  51. if ($submitDateFinal == "") $submitDateFinal = "2050-12-31";
  52. if ($iid == "") $iid = 0;
  53. if (!$this->checkParamSv->validateDate($submitDateStart, 'Y-m-d')) $submitDateStart = "1900-01-01";
  54. if (!$this->checkParamSv->validateDate($submitDateFinal, 'Y-m-d')) $submitDateFinal = "2050-12-31";
  55. if ($searchcol != filter_var($searchcol, FILTER_SANITIZE_SPECIAL_CHARS)) $searchcol = "___CANNOT_FIND_STRING___";
  56. if (!$this->checkParamSv->LenMToN($searchcol, 0, 128)) $searchcol = "___CANNOT_FIND_STRING___";
  57. //資料庫
  58. $recordsTotal = 0;
  59. $result = $this->submitManagementSv->getSubmits(
  60. $recordsTotal,
  61. $orderColumn,
  62. $orderDir,
  63. $start,
  64. $length,
  65. $searchValue,
  66. $submitDateStart,
  67. $submitDateFinal,
  68. $iid,
  69. $searchcol,
  70. $duplicate
  71. );
  72. // 整理返回資料
  73. $data = array();
  74. for ($i = 0; $i < count($result); $i++) {
  75. $data[] = array(
  76. //一般資料
  77. $result[ $i ]["id"],
  78. $result[ $i ]["event_name"],
  79. $result[ $i ]["item_name"],
  80. $result[ $i ]["name"],
  81. $result[ $i ]["identity"],
  82. $result[ $i ]["add_host"],
  83. $result[ $i ]["add_contact"],
  84. $result[ $i ]["tel"],
  85. $result[ $i ][ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['sql'] ],
  86. $result[ $i ][ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['sql'] ],
  87. $result[ $i ][ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['sql'] ],
  88. $result[ $i ][ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['sql'] ],
  89. $result[ $i ]["cdate"],
  90. );
  91. }
  92. $json = array(
  93. "draw" => $draw,
  94. "recordsTotal" => $recordsTotal,
  95. "recordsFiltered" => $recordsTotal, //其實還是填入所有筆數,本次筆數可從陣列取得
  96. "data" => $data,
  97. );
  98. // 返回
  99. return json_decode(json_encode($json, JSON_NUMERIC_CHECK), true);
  100. }
  101. public function getItems($eid)
  102. {
  103. $json = $this->submitManagementSv->getItems($eid);
  104. // 整理返回值並返回
  105. return json_decode(json_encode($json, JSON_NUMERIC_CHECK), true);
  106. }
  107. public function getZip($param)
  108. {
  109. // 取得參數
  110. if ($param == null) exit();
  111. $param = explode('|', $param);
  112. //客製化搜尋欄位
  113. $submitDateStart = $param[0];
  114. $submitDateFinal = $param[1];
  115. $iid = $param[2];
  116. $searchcol = $param[3];
  117. $duplicate = $param[4];
  118. // 驗證
  119. if ($submitDateStart == "") $submitDateStart = "1900-01-01";
  120. if ($submitDateFinal == "") $submitDateFinal = "2050-12-31";
  121. if (!$this->checkParamSv->validateDate($submitDateStart, 'Y-m-d')) $submitDateStart = "1900-01-01";
  122. if (!$this->checkParamSv->validateDate($submitDateFinal, 'Y-m-d')) $submitDateFinal = "2050-12-31";
  123. if ($iid == "") $iid = 0;
  124. if ($searchcol != filter_var($searchcol, FILTER_SANITIZE_SPECIAL_CHARS)) $searchcol = "___CANNOT_FIND_STRING___";
  125. if (!$this->checkParamSv->LenMToN($searchcol, 0, 128)) $searchcol = "___CANNOT_FIND_STRING___";
  126. // 路徑命名
  127. $dirname = 'working';
  128. $date = date("YmdHis");
  129. if (!file_exists($dirname)) mkdir($dirname, 0777, true);
  130. if (!file_exists($dirname . '/' . $date)) mkdir($dirname . '/' . $date, 0777, true);
  131. $basepath = $dirname . '/' . $date . '/';
  132. $excel_name = 'REPORT.xlsx';
  133. $zip_name = 'REPORT' . $date . '.zip';
  134. $report_files = [];
  135. // 製作 EXCEL
  136. $title = [
  137. 'ID',
  138. '活動名稱',
  139. '獎別',
  140. '申請人姓名',
  141. '身分證號',
  142. '戶籍地址',
  143. '通訊地址',
  144. '電話',
  145. '建立時間',
  146. ];
  147. $submits = $this->submitManagementSv->getExportSubmits(
  148. $submitDateStart,
  149. $submitDateFinal,
  150. $iid,
  151. $searchcol,
  152. $duplicate
  153. );
  154. $excels = [];
  155. foreach ($submits as $s) {
  156. $excels[] = [
  157. 'id' => $s['id'],
  158. 'event_name' => $s['event_name'],
  159. 'item_name' => $s['item_name'],
  160. 'name' => $s['name'],
  161. 'identity' => $s['identity'],
  162. 'add_host' => $s['add_host'],
  163. 'add_contact' => $s['add_contact'],
  164. 'tel' => $s['tel'],
  165. 'cdate' => $s['cdate'],
  166. ];
  167. }
  168. $this->submitManagementSv->getExcel($title, $excels, $basepath . $excel_name);
  169. $report_files[] = $basepath . $excel_name;
  170. // 取得相關素材
  171. foreach ($submits as $s) {
  172. // 依照該身分證字號創建資料夾
  173. $mypath = $basepath . $s['identity'] . '/';
  174. if (!file_exists($mypath)) mkdir($mypath, 0777, true);
  175. // 逐一拷貝
  176. if (file_exists($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['sql'] ])) {
  177. copy($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['sql'] ], $mypath . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['zip_file'] . substr($date, 2, 6) . '-' . $s['id'] . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['ext']);
  178. $report_files[] = $mypath . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['zip_file'] . substr($date, 2, 6) . '-' . $s['id'] . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_FRONT ]['ext'];
  179. }
  180. if (file_exists($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['sql'] ])) {
  181. copy($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['sql'] ], $mypath . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['zip_file'] . substr($date, 2, 6) . '-' . $s['id'] . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['ext']);
  182. $report_files[] = $mypath . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['zip_file'] . substr($date, 2, 6) . '-' . $s['id'] . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_IDENTITY_BACK ]['ext'];
  183. }
  184. if (file_exists($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['sql'] ])) {
  185. copy($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['sql'] ], $mypath . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['zip_file'] . substr($date, 2, 6) . '-' . $s['id'] . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['ext']);
  186. $report_files[] = $mypath . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['zip_file'] . substr($date, 2, 6) . '-' . $s['id'] . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_PASSBOOK ]['ext'];
  187. }
  188. if (file_exists($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['sql'] ])) {
  189. copy($s[ GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['sql'] ], $mypath . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['zip_file'] . substr($date, 2, 6) . '-' . $s['id'] . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['ext']);
  190. $report_files[] = $mypath . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['zip_file'] . substr($date, 2, 6) . '-' . $s['id'] . GeneralConst::$appendixMap[ GeneralConst::APPENDIX_DECLARE_PDF ]['ext'];
  191. }
  192. }
  193. // 製作壓縮檔
  194. foreach (glob($dirname . '/*.zip') as $f) if (is_file($f)) unlink($f); // 殺掉前幾次的舊匯出檔案
  195. $zip = new ZipArchive;
  196. if ($zip->open($dirname . '/' . $zip_name, ZipArchive::CREATE) === true) {
  197. foreach ($report_files as $f) $zip->addFile($f, str_replace($dirname . '/', '', $f));
  198. $zip->close();
  199. }
  200. $headers = array(
  201. 'Content-Type' => 'application/octet-stream',
  202. );
  203. // 刪除文件夾
  204. $this->rrmdir($basepath);
  205. return response()->download($dirname . '/' . $zip_name, $zip_name, $headers);
  206. }
  207. public function rrmdir($dir)
  208. {
  209. if (is_dir($dir)) {
  210. $objects = scandir($dir);
  211. foreach ($objects as $object) {
  212. if ($object != "." && $object != "..") {
  213. if (is_dir($dir . DIRECTORY_SEPARATOR . $object) && !is_link($dir . "/" . $object))
  214. $this->rrmdir($dir . DIRECTORY_SEPARATOR . $object);
  215. else
  216. unlink($dir . DIRECTORY_SEPARATOR . $object);
  217. }
  218. }
  219. rmdir($dir);
  220. }
  221. }
  222. }