SignupManagementController.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Http\Controllers\Backend\DataManagement;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Http\Services\CheckParamService;
  6. use App\Http\Controllers\Controller;
  7. use App\Http\Services\Backend\DataManagement\SignupManagementService;
  8. use Log;
  9. use Illuminate\Support\Facades\Session;
  10. use Spatie\SimpleExcel\SimpleExcelWriter;
  11. class SignupManagementController extends Controller
  12. {
  13. // 相關私有服務層調用器宣告
  14. private $checkParamSv;
  15. private $signupManagementSv;
  16. public function __construct()
  17. {
  18. // 建構服務層調用器
  19. $this->checkParamSv=new CheckParamService();
  20. $this->signupManagementSv = new SignupManagementService();
  21. }
  22. public function index()
  23. {
  24. // 取得參數
  25. $param = $_GET;
  26. // 渲染
  27. return view('admin.DataManagement.SignupManagement');
  28. }
  29. public function grid()
  30. {
  31. // 取得參數
  32. $param = $_GET;
  33. // if ($param == null) exit();
  34. $draw = $param["draw"]; //客戶端傳來的查詢次數,無條件回傳用以核對
  35. $orderColumn = $param["order"][0]["column"] + 1; //前端從 0 開始送,但 mysql 從 1 開始算
  36. $orderDir = $param["order"][0]["dir"];
  37. $start = $param["start"]; // 頁碼
  38. $length = $param["length"]; // 一頁多大
  39. $searchValue = $param["search"]["value"];
  40. //客製化搜尋欄位
  41. $keyword = $param["columns"][1]["search"]["value"];
  42. $trackNo = $param["columns"][2]["search"]["value"];
  43. $createDateStart = $param["columns"][3]["search"]["value"];
  44. $createDateFinal = $param["columns"][4]["search"]["value"];
  45. // \Log::info('createDateStart: '.$createDateStart);
  46. // \Log::info('createDateFinal: '.$createDateFinal);
  47. // 驗證
  48. if ($keyword != filter_var($keyword, FILTER_SANITIZE_SPECIAL_CHARS)) $keyword = "___CANNOT_FIND_STRING___";
  49. if (!$this->checkParamSv->LenMToN($keyword, 0, 50)) $keyword = "___CANNOT_FIND_STRING___";
  50. if ($trackNo != filter_var($trackNo, FILTER_SANITIZE_SPECIAL_CHARS)) $trackNo = "___CANNOT_FIND_STRING___";
  51. if (!$this->checkParamSv->LenMToN($trackNo, 0, 50)) $trackNo = "___CANNOT_FIND_STRING___";
  52. if ($createDateStart != filter_var($createDateStart, FILTER_SANITIZE_SPECIAL_CHARS)) $createDateStart = "___CANNOT_FIND_STRING___";
  53. if (!$this->checkParamSv->LenMToN($createDateStart, 0, 50)) $createDateStart = "___CANNOT_FIND_STRING___";
  54. if ($createDateFinal != filter_var($createDateFinal, FILTER_SANITIZE_SPECIAL_CHARS)) $createDateFinal = "___CANNOT_FIND_STRING___";
  55. if (!$this->checkParamSv->LenMToN($createDateFinal, 0, 50)) $createDateFinal = "___CANNOT_FIND_STRING___";
  56. if ($createDateStart&&$createDateFinal&&$createDateStart>$createDateFinal) {
  57. Session::flash('msg', '請填入正確起訖日!');
  58. return redirect()->back();
  59. }
  60. if (!$createDateStart) {
  61. $createDateStart = "2022-09-10 00:00:00";
  62. } else {
  63. $createDateStart = $createDateStart." 00:00:00";
  64. }
  65. if (!$createDateFinal) {
  66. $createDateFinal = date('Y-m-d H:i:s');
  67. } else {
  68. $createDateFinal = $createDateFinal." 23:59:59";
  69. }
  70. //資料庫
  71. $recordsTotal = 0;
  72. if ($keyword) {
  73. $result=$this->signupManagementSv->getList($recordsTotal, $orderColumn, $orderDir, $start, $length, $searchValue, $this->safeEncrypt(($keyword), 'arm'), $trackNo, $createDateStart, $createDateFinal);
  74. } else {
  75. $result=$this->signupManagementSv->getList($recordsTotal, $orderColumn, $orderDir, $start, $length, $searchValue, '', $trackNo, $createDateStart, $createDateFinal);
  76. }
  77. // 外部短網址系統串接
  78. $orlCodeIds = array();
  79. // 整理返回資料
  80. $data = array();
  81. $registeredSession = '';
  82. $lunchOptions = '';
  83. for ($i = 0; $i < count($result); $i++) {
  84. $data[] = array(
  85. //一般資料
  86. $result[$i]["id"],
  87. htmlspecialchars($this->safeDecrypt($result[$i]["firstName"], 'arm')),
  88. htmlspecialchars($this->safeDecrypt($result[$i]["lastName"], 'arm')),
  89. htmlspecialchars($this->safeDecrypt($result[$i]["companyName"], 'arm')),
  90. htmlspecialchars($result[$i]["country"]),
  91. htmlspecialchars($result[$i]["trackNo"]),
  92. htmlspecialchars($result[$i]["registeredSession"]),
  93. htmlspecialchars($result[$i]["lunchOptions"]),
  94. $result[$i]["createDate"],
  95. );
  96. }
  97. $json = array(
  98. "draw" => $draw,
  99. "recordsTotal" => $recordsTotal,
  100. "recordsFiltered" => $recordsTotal, //其實還是填入所有筆數,本次筆數可從陣列取得
  101. "data" => $data,
  102. );
  103. // 返回
  104. return json_decode(json_encode($json), true);
  105. }
  106. public function export(Request $request)
  107. {
  108. // 取得參數
  109. $param = $_POST;
  110. /*
  111. if (!$request->keyword) {
  112. Session::flash('msg', '請填入關鍵字!');
  113. return redirect()->back();
  114. }
  115. */
  116. $keyword = $request->keyword;
  117. $trackNo = $request->trackNo;
  118. $createDateStart = $request->createDateStart;
  119. $createDateFinal = $request->createDateFinal;
  120. if ($keyword) {
  121. $datas = $this->signupManagementSv->getExportList($this->safeEncrypt(($keyword), 'arm'), $trackNo, $createDateStart, $createDateFinal);
  122. } else {
  123. $datas = $this->signupManagementSv->getExportList('', $trackNo, $createDateStart, $createDateFinal);
  124. }
  125. $rows = [];
  126. foreach ($datas as $data) {
  127. if ($data['backupEmail']) {
  128. $backupEmail = $this->safeDecrypt($data['backupEmail'], 'arm');
  129. } else {
  130. $backupEmail = '';
  131. }
  132. $rows[] = [
  133. '序號' => $data['id'],
  134. 'firstName' => $this->safeDecrypt($data['firstName'], 'arm'),
  135. 'lastName' => $this->safeDecrypt($data['lastName'], 'arm'),
  136. 'companyName' => $this->safeDecrypt($data['companyName'], 'arm'),
  137. 'companyEmail' => $this->safeDecrypt($data['companyEmail'], 'arm'),
  138. 'backupEmail' => $backupEmail,
  139. 'phoneNumber' => $this->safeDecrypt($data['phoneNumber'], 'arm'),
  140. 'country' => $data['country'],
  141. 'trackNo' => $data['trackNo'],
  142. 'registeredSession(TW only)' => $data['registeredSession'],
  143. 'lunchOptions(TW only)' => $data['lunchOptions'],
  144. 'typeOfIndustry' => $data['typeOfIndustry'],
  145. 'typeOfJob' => $data['typeOfJob'],
  146. 'jobTitle' => $data['jobTitle'],
  147. 'trackOfInterest' => $data['trackOfInterest'],
  148. 'areaOfInterest' => $data['areaOfInterest'],
  149. 'howToKnowAboutTheEvent' => $data['howToKnowAboutTheEvent'],
  150. 'consentAcceptEmail' => $data['consentAcceptEmail'],
  151. 'consentPrivacyPolicy' => $data['consentPrivacyPolicy'],
  152. '報名時間' => $data['createDate'],
  153. ];
  154. }
  155. SimpleExcelWriter::streamDownload('報名資料'.date('YmdHis').'.xlsx')
  156. ->addRows($rows)
  157. ->toBrowser();
  158. }
  159. /**
  160. * 參數加解密模組: 加密部分,建議使用環境變數中的 secret key 作加解密種子
  161. */
  162. public function safeEncrypt(string $message, string $skey): string
  163. {
  164. $strArr = str_split(base64_encode($message));
  165. $strCount = count($strArr);
  166. foreach (str_split($skey) as $key => $value)
  167. $key < $strCount && $strArr[$key].=$value;
  168. return str_replace(array('=', ' ', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
  169. }
  170. /**
  171. * 參數加解密模組: 解密部分,建議使用環境變數中的 secret key 作加解密種子
  172. */
  173. public function safeDecrypt(string $encrypted, string $skey): string
  174. {
  175. $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', ' ', '/'), $encrypted), 2);
  176. $strCount = count($strArr);
  177. foreach (str_split($skey) as $key => $value) {
  178. $key <= $strCount && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
  179. }
  180. return base64_decode(join('', $strArr));
  181. }
  182. }