SignupManagementController.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. $createDateStart = $param["columns"][2]["search"]["value"];
  43. $createDateFinal = $param["columns"][3]["search"]["value"];
  44. // \Log::info('createDateStart: '.$createDateStart);
  45. // \Log::info('createDateFinal: '.$createDateFinal);
  46. // 驗證
  47. if ($keyword != filter_var($keyword, FILTER_SANITIZE_SPECIAL_CHARS)) $keyword = "___CANNOT_FIND_STRING___";
  48. if (!$this->checkParamSv->LenMToN($keyword, 0, 50)) $keyword = "___CANNOT_FIND_STRING___";
  49. if ($createDateStart != filter_var($createDateStart, FILTER_SANITIZE_SPECIAL_CHARS)) $createDateStart = "___CANNOT_FIND_STRING___";
  50. if (!$this->checkParamSv->LenMToN($createDateStart, 0, 50)) $createDateStart = "___CANNOT_FIND_STRING___";
  51. if ($createDateFinal != filter_var($createDateFinal, FILTER_SANITIZE_SPECIAL_CHARS)) $createDateFinal = "___CANNOT_FIND_STRING___";
  52. if (!$this->checkParamSv->LenMToN($createDateFinal, 0, 50)) $createDateFinal = "___CANNOT_FIND_STRING___";
  53. if ($createDateStart&&$createDateFinal&&$createDateStart>$createDateFinal) {
  54. Session::flash('msg', '請填入正確起訖日!');
  55. return redirect()->back();
  56. }
  57. if (!$createDateStart) {
  58. $createDateStart = "2022-09-10 00:00:00";
  59. } else {
  60. $createDateStart = $createDateStart." 00:00:00";
  61. }
  62. if (!$createDateFinal) {
  63. $createDateFinal = date('Y-m-d H:i:s');
  64. } else {
  65. $createDateFinal = $createDateFinal." 23:59:59";
  66. }
  67. //資料庫
  68. $recordsTotal = 0;
  69. if ($keyword) {
  70. $result=$this->signupManagementSv->getList($recordsTotal, $orderColumn, $orderDir, $start, $length, $searchValue, $this->safeEncrypt(($keyword), 'arm'), $createDateStart, $createDateFinal);
  71. } else {
  72. $result=$this->signupManagementSv->getList($recordsTotal, $orderColumn, $orderDir, $start, $length, $searchValue, '', $createDateStart, $createDateFinal);
  73. }
  74. // 外部短網址系統串接
  75. $orlCodeIds = array();
  76. // 整理返回資料
  77. $data = array();
  78. $registeredSession = '';
  79. $lunchOptions = '';
  80. for ($i = 0; $i < count($result); $i++) {
  81. $data[] = array(
  82. //一般資料
  83. $result[$i]["id"],
  84. htmlspecialchars($this->safeDecrypt($result[$i]["firstName"], 'arm')),
  85. htmlspecialchars($result[$i]["lastName"]),
  86. // htmlspecialchars($this->safeDecrypt($result[$i]["lastName"], 'arm')),
  87. htmlspecialchars($this->safeDecrypt($result[$i]["companyName"], 'arm')),
  88. htmlspecialchars($result[$i]["country"]),
  89. htmlspecialchars($result[$i]["trackNo"]),
  90. htmlspecialchars($result[$i]["registeredSession"]),
  91. htmlspecialchars($result[$i]["lunchOptions"]),
  92. $result[$i]["createDate"],
  93. );
  94. }
  95. $json = array(
  96. "draw" => $draw,
  97. "recordsTotal" => $recordsTotal,
  98. "recordsFiltered" => $recordsTotal, //其實還是填入所有筆數,本次筆數可從陣列取得
  99. "data" => $data,
  100. );
  101. // 返回
  102. return json_decode(json_encode($json), true);
  103. }
  104. public function export(Request $request)
  105. {
  106. // 取得參數
  107. $param = $_POST;
  108. /*
  109. if (!$request->keyword) {
  110. Session::flash('msg', '請填入關鍵字!');
  111. return redirect()->back();
  112. }
  113. */
  114. if ($request->keyword) {
  115. $datas = $this->signupManagementSv->getExportList($this->safeEncrypt(($request->keyword), 'arm'));
  116. } else {
  117. $datas = $this->signupManagementSv->getExportList('');
  118. }
  119. $rows = [];
  120. foreach ($datas as $data) {
  121. if ($data['backupEmail']) {
  122. $backupEmail = $this->safeDecrypt($data['backupEmail'], 'arm');
  123. } else {
  124. $backupEmail = '';
  125. }
  126. $rows[] = [
  127. '序號' => $data['id'],
  128. 'firstName' => $this->safeDecrypt($data['firstName'], 'arm'),
  129. 'lastName' => $this->safeDecrypt($data['lastName'], 'arm'),
  130. 'companyName' => $this->safeDecrypt($data['companyName'], 'arm'),
  131. 'companyEmail' => $this->safeDecrypt($data['companyEmail'], 'arm'),
  132. 'backupEmail' => $backupEmail,
  133. 'phoneNumber' => $this->safeDecrypt($data['phoneNumber'], 'arm'),
  134. 'country' => $data['country'],
  135. 'trackNo' => $data['trackNo'],
  136. 'registeredSession(TW only)' => $data['registeredSession'],
  137. 'lunchOptions(TW only)' => $data['lunchOptions'],
  138. 'typeOfIndustry' => $data['typeOfIndustry'],
  139. 'typeOfJob' => $data['typeOfJob'],
  140. 'jobTitle' => $data['jobTitle'],
  141. 'trackOfInterest' => $data['trackOfInterest'],
  142. 'areaOfInterest' => $data['areaOfInterest'],
  143. 'howToKnowAboutTheEvent' => $data['howToKnowAboutTheEvent'],
  144. 'consentAcceptEmail' => $data['consentAcceptEmail'],
  145. 'consentPrivacyPolicy' => $data['consentPrivacyPolicy'],
  146. '報名時間' => $data['createDate'],
  147. ];
  148. }
  149. SimpleExcelWriter::streamDownload('報名資料'.date('YmdHis').'.xlsx')
  150. ->addRows($rows)
  151. ->toBrowser();
  152. }
  153. /**
  154. * 參數加解密模組: 加密部分,建議使用環境變數中的 secret key 作加解密種子
  155. */
  156. public function safeEncrypt(string $message, string $skey): string
  157. {
  158. $strArr = str_split(base64_encode($message));
  159. $strCount = count($strArr);
  160. foreach (str_split($skey) as $key => $value)
  161. $key < $strCount && $strArr[$key].=$value;
  162. return str_replace(array('=', ' ', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
  163. }
  164. /**
  165. * 參數加解密模組: 解密部分,建議使用環境變數中的 secret key 作加解密種子
  166. */
  167. public function safeDecrypt(string $encrypted, string $skey): string
  168. {
  169. $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', ' ', '/'), $encrypted), 2);
  170. $strCount = count($strArr);
  171. foreach (str_split($skey) as $key => $value) {
  172. $key <= $strCount && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
  173. }
  174. return base64_decode(join('', $strArr));
  175. }
  176. }