SignupManagementController.php 6.7KB

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