| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 | <?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Api\ApiController;
use App\Http\Services\Api\SeminarSignUpService;
use App\Http\Requests\Api\SeminarSignUp\StoreRequest;
use Log;
class SeminarSignUpController extends ApiController
{
    
    private $seminarSignUpSv;
    
    public function __construct()
    {
        $this->seminarSignUpSv = new SeminarSignUpService();
    }
    // save data to db
    public function insertData(StoreRequest $request)
    {
        $trackNo = $request->input('trackNo', '');
        $lang = substr($trackNo, 0, 2);
        $firstName_orig = $request->input('firstName', '');
        $firstName = $this->safeEncrypt($firstName_orig, 'arm');
        $lastName_orig = $request->input('lastName', '');
        $lastName = $this->safeEncrypt($lastName_orig, 'arm');
        
        $companyName_orig = $request->input('companyName', '');
        $companyName = $this->safeEncrypt($companyName_orig, 'arm');
        
        $companyEmail_orig = $request->input('companyEmail', '');
        $companyEmail = $this->safeEncrypt($companyEmail_orig, 'arm');
        
        $backupEmail = $request->input('backupEmail', '');
        if (!is_null($backupEmail)) {
        
            $backupEmail_orig = $request->input('backupEmail', '');
            $backupEmail = $this->safeEncrypt($backupEmail_orig, 'arm');
        } else {
            $backupEmail_orig = '';
        }
        
        $phoneNumber_orig = $request->input('phoneNumber', '');
        $phoneNumber = $this->safeEncrypt($phoneNumber_orig, 'arm');
        
        $country = $request->input('country', '');
        $registeredSession = $request->input('registeredSession', '');
        $lunchOptions = $request->input('lunchOptions', '');
        $typeOfIndustry = $request->input('typeOfIndustry', '');
        $typeOfJob = $request->input('typeOfJob', '');
        $jobTitle = $request->input('jobTitle', '');
        $trackOfInterest = $request->input('trackOfInterest', '');
        $areaOfInterest = $request->input('areaOfInterest', '');
        $howToKnowAboutTheEvent = $request->input('howToKnowAboutTheEvent', '');
        $consentAcceptEmail = $request->input('consentAcceptEmail', '');
        $consentPrivacyPolicy = $request->input('consentPrivacyPolicy', '');
        $media = $request->input('media', '');
        $utm_source = $request->input('utm_source', '');
        $utm_medium = $request->input('utm_medium', '');
        $utm_content = $request->input('utm_content', '');
        $utm_term = $request->input('utm_term', '');
        $utm_campaign = $request->input('utm_campaign', '');
        
        $overOrNot = $this->seminarSignUpSv->overLimitOrNot($trackNo);          // true: 可報名 / false: 已額滿
        $duplicatedOrNot = $this->seminarSignUpSv->duplicatedOrNot($trackNo, $companyEmail, $phoneNumber);   // true: 可報名 / false: 已重複
        if ($overOrNot&&$duplicatedOrNot) {
            $this->seminarSignUpSv->insertData(
                $firstName, 
                $lastName, 
                $companyName, 
                $companyEmail, 
                $backupEmail, 
                $phoneNumber, 
                $country, 
                $trackNo, 
                $registeredSession,
                $lunchOptions,
                $typeOfIndustry,
                $typeOfJob,
                $jobTitle,
                $trackOfInterest,
                $areaOfInterest,
                $howToKnowAboutTheEvent,
                $consentAcceptEmail,
                $consentPrivacyPolicy,
                $media,
                $utm_source,
                $utm_medium,
                $utm_content,
                $utm_term,
                $utm_campaign,
            );
            $res = '報名成功';
            if ($lang=='TP'||$lang=='HS') {
                $this->mailToUser_TW($firstName_orig, $lastName_orig, $companyName_orig, $companyEmail_orig, $backupEmail_orig, 
                    $phoneNumber_orig, $country, $registeredSession, $lunchOptions, $typeOfIndustry, $typeOfJob, $jobTitle, $trackOfInterest);
            } elseif ($lang=='JP') {
                $this->mailToUser_JP($firstName_orig, $lastName_orig, $companyName_orig, $companyEmail_orig, $backupEmail_orig, 
                $phoneNumber_orig, $country, /*$registeredSession, $lunchOptions, */$typeOfIndustry, $typeOfJob, $jobTitle, $trackOfInterest);
            } elseif ($lang=='KR') {
                $this->mailToUser_KR($firstName_orig, $lastName_orig, $companyName_orig, $companyEmail_orig, $backupEmail_orig, 
                $phoneNumber_orig, $country, /*$registeredSession, $lunchOptions, */$typeOfIndustry, $typeOfJob, $jobTitle, $trackOfInterest);
            } else {
                $this->mailToUser_EN($firstName_orig, $companyEmail_orig, $backupEmail_orig);
            }
            
        } elseif (!$overOrNot) {
            $res = '已達報名上限';
        } elseif (!$duplicatedOrNot) {
            $res = '已重複報名';
        }
        $data = [
            'res' => $res,
        ];
        
        return $this->apiResponse($data);
    }
    // list 
    public function getReMailData()
    {
        
        $list = $this->seminarSignUpSv->getReMailData();
        
        $returnData = array();
        for ($i = 0; $i < count($list); $i++) {
            try{
                // \Log::info($this->safeDecrypt($list[$i]["firstName"], 'arm'));
                
                // $data[] = array(
                    //一般資料
                    $id = $list[$i]["id"];
                    
                    $firstName_orig = $this->safeDecrypt($list[$i]["firstName"], 'arm');
                    $lastName_orig = $this->safeDecrypt($list[$i]["lastName"], 'arm');
                    $companyName_orig = $this->safeDecrypt($list[$i]["companyName"], 'arm');
                    $companyEmail_orig = $this->safeDecrypt($list[$i]["companyEmail"], 'arm');
                    
                    if (!is_null($list[$i]["backupEmail"])) {
        
                        $backupEmail_orig = $this->safeDecrypt($list[$i]["backupEmail"], 'arm');
                    } else {
                        $backupEmail_orig = '';
                    }
                    $phoneNumber_orig = $this->safeDecrypt($list[$i]["phoneNumber"], 'arm');
                    $country = $list[$i]["country"];
                    
                    $trackNo = $list[$i]["trackNo"];
                    $lang = substr($trackNo, 0, 2);
                    $registeredSession = $list[$i]["registeredSession"];
                    $lunchOptions = $list[$i]["lunchOptions"];
                    $typeOfIndustry = $list[$i]["typeOfIndustry"];
                    $typeOfJob = $list[$i]["typeOfJob"];
                    $jobTitle = $list[$i]["jobTitle"];
                    $trackOfInterest = $list[$i]["trackOfInterest"];
                    // if ($lang=='TP'||$lang=='HS') {
                    //     $this->mailToUser_TW($firstName_orig, $lastName_orig, $companyName_orig, $companyEmail_orig, $backupEmail_orig, 
                    //         $phoneNumber_orig, $country, $registeredSession, $lunchOptions, $typeOfIndustry, $typeOfJob, $jobTitle, $trackOfInterest);
                    // } elseif ($lang=='JP') {
                    //     $this->mailToUser_JP($firstName_orig, $lastName_orig, $companyName_orig, $companyEmail_orig, $backupEmail_orig, 
                    //     $phoneNumber_orig, $country, /*$registeredSession, $lunchOptions, */$typeOfIndustry, $typeOfJob, $jobTitle, $trackOfInterest);
                    // } elseif ($lang=='KR') {
                    //     $this->mailToUser_KR($firstName_orig, $lastName_orig, $companyName_orig, $companyEmail_orig, $backupEmail_orig, 
                    //     $phoneNumber_orig, $country, /*$registeredSession, $lunchOptions, */$typeOfIndustry, $typeOfJob, $jobTitle, $trackOfInterest);
                    // } else {
                    //     $this->mailToUser_EN($firstName_orig, $companyEmail_orig, $backupEmail_orig);
                    // }
                // );
                $res = [
                    'id'      => $id,
                    'value'    => $id.' / '.$firstName_orig.' / '.$lastName_orig.' / '.$companyName_orig.' / '.$companyEmail_orig.' / '.
                                $backupEmail_orig.' / '.$phoneNumber_orig.' / '.$country.' / '.$trackNo.' / '.$lang.' / '.$registeredSession.' / '.
                                $lunchOptions.' / '.$typeOfIndustry.' / '.$typeOfJob.' / '.$jobTitle.' / '.$trackOfInterest,
                    'message' => 'succ',
                ];
                \Log::info($id.': '.print_r($res, true));
                $returnData[] = $res;
            } catch (\Throwable $exception) {
                $message = [
                    'msg'  => $exception->getMessage(),
                    'line' => $exception->getLine(),
                    'file' => $exception->getFile()
                ];
                // 返回
                $res = [
                    'id'      => $id,
                    'value'    => $id.' / '.$firstName_orig.' / '.$lastName_orig.' / '.$companyName_orig.' / '.$companyEmail_orig.' / '.
                                $backupEmail_orig.' / '.$phoneNumber_orig.' / '.$country.' / '.$trackNo.' / '.$lang.' / '.$registeredSession.' / '.
                                $lunchOptions.' / '.$typeOfIndustry.' / '.$typeOfJob.' / '.$jobTitle.' / '.$trackOfInterest,
                    'message' => $message,
                ];
                $returnData[] = $res;
                $log = json_encode($res, JSON_UNESCAPED_UNICODE);
                \Log::info(print_r($log, true));
                
            }
        }
        
        return $returnData;
    }
    // list 
    public function restoreUTMData()
    {
        
        $list = $this->seminarSignUpSv->getUTMData();
        // $list = $this->seminarSignUpSv->getUTMData2();
        \Log::info(print_r($list, true));
        $returnData = array();
        for ($i = 0; $i<count($list); $i++) {
            //一般資料
            $importArr = array();
            $id = $list[$i]["id"];
            $media = $list[$i]["media"];
            if ($media) {
                \Log::info(print_r($id, true));
                \Log::info(print_r($media, true));
                
                $tmp = substr($media, 1);
                $tmpArr = explode('&', $tmp);
                // if (count($tmpArr)==4) {
                    for ($j=0; $j<5; $j++) {
                        $cont = explode('=', $tmpArr[$j]);
                        // $importArr[] = $cont[1];
                        $importArr = array(
                            $cont[0] => $cont[1],
                        );
                    }
                    \Log::info(print_r($importArr, true));
                    // $this->seminarSignUpSv->updateUTMData($id, $importArr);
                // }
                    
            }
                
        }
        
        return $importArr;
    }
    public function getData()
    {
        $list = $this->seminarSignUpSv->getData();
        
        $data = [
            'list' => $list
        ];
        
        return $this->apiResponse($data);
    }
    /**
     * 參數加解密模組: 加密部分,建議使用環境變數中的 secret key 作加解密種子
     */
    public function safeEncrypt(string $message, string $skey): string
    {
        $strArr = str_split(base64_encode($message));
        $strCount = count($strArr);
        foreach (str_split($skey) as $key => $value)
        $key < $strCount && $strArr[$key].=$value;
        return str_replace(array('=', ' ', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
    }
    
    /**
     * 參數加解密模組: 解密部分,建議使用環境變數中的 secret key 作加解密種子
     */
    public function safeDecrypt(string $encrypted, string $skey): string
    {
        $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', ' ', '/'), $encrypted), 2);
        $strCount = count($strArr);
        foreach (str_split($skey) as $key => $value)
        $key <= $strCount && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
        return base64_decode(join('', $strArr));
    }
    
    public function mailToUser_TW(
        string $firstName='', 
        string $lastName='', 
        string $companyName='',
        string $companyEmail='',
        string $backupEmail='',
        string $phoneNumber='',
        string $country='',
        string $registeredSession='',
        string $lunchOptions='',
        string $typeOfIndustry='',
        string $typeOfJob='',
        string $jobTitle='',
        string $trackOfInterest=''
        )
    {
        $data = array(
            'firstName' => $firstName,
            'lastName' => $lastName,
            'companyName' => $companyName,
            'companyEmail' => $companyEmail,
            'backupEmail' => $backupEmail,
            'phoneNumber' => $phoneNumber,
            'country' => $country,
            'registeredSession' => $registeredSession,
            'lunchOptions' => $lunchOptions,
            'typeOfIndustry' => $typeOfIndustry,
            'typeOfJob' => $typeOfJob,
            'jobTitle' => $jobTitle,
            'trackOfInterest' => $trackOfInterest,
        );
        if (strlen($backupEmail)>0) {
            \Mail::send(['text'=>'mailTW'], $data, function($message) use ($firstName, $companyEmail, $backupEmail) {
                $message->to($companyEmail, $firstName)->subject('報名成功-Arm Tech Symposia 2022');
                $message->cc($backupEmail, $firstName)->subject('報名成功-Arm Tech Symposia 2022');
                $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
            });
        } else {
            \Mail::send(['text'=>'mailTW'], $data, function($message) use ($companyEmail, $firstName) {
                $message->to($companyEmail, $firstName)->subject('報名成功-Arm Tech Symposia 2022');
                $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
            });
        }
    }
    public function mailToUser_JP(
        string $firstName='', 
        string $lastName='', 
        string $companyName='',
        string $companyEmail='',
        string $backupEmail='',
        string $phoneNumber='',
        string $country='',
        // string $registeredSession='',
        // string $lunchOptions='',
        string $typeOfIndustry='',
        string $typeOfJob='',
        string $jobTitle='',
        string $trackOfInterest=''
        )
    {
        $data = array(
            'firstName' => $firstName,
            'lastName' => $lastName,
            'companyName' => $companyName,
            'companyEmail' => $companyEmail,
            'backupEmail' => $backupEmail,
            'phoneNumber' => $phoneNumber,
            'country' => $country,
            // 'registeredSession' => $registeredSession,
            // 'lunchOptions' => $lunchOptions,
            'typeOfIndustry' => $typeOfIndustry,
            'typeOfJob' => $typeOfJob,
            'jobTitle' => $jobTitle,
            'trackOfInterest' => $trackOfInterest,
        );
        if ($backupEmail) {
            \Mail::send(['text'=>'mailJP'], $data, function($message) use ($companyEmail, $firstName, $backupEmail) {
                $message->to($companyEmail, $firstName)->subject('Arm Tech Symposia 仮登録完了のお知らせ');
                $message->cc($backupEmail, $firstName)->subject('Arm Tech Symposia 仮登録完了のお知らせ');
                $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
            });
        } else {
            \Mail::send(['text'=>'mailJP'], $data, function($message) use ($companyEmail, $firstName) {
                $message->to($companyEmail, $firstName)->subject('Arm Tech Symposia 仮登録完了のお知らせ');
                $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
            });
        }
    }
    public function mailToUser_KR(
        string $firstName='', 
        string $lastName='', 
        string $companyName='',
        string $companyEmail='',
        string $backupEmail='',
        string $phoneNumber='',
        string $country='',
        // string $registeredSession='',
        // string $lunchOptions='',
        string $typeOfIndustry='',
        string $typeOfJob='',
        string $jobTitle='',
        string $trackOfInterest=''
        )
    {
        $data = array(
            'firstName' => $firstName,
            'lastName' => $lastName,
            'companyName' => $companyName,
            'companyEmail' => $companyEmail,
            'backupEmail' => $backupEmail,
            'phoneNumber' => $phoneNumber,
            'country' => $country,
            // 'registeredSession' => $registeredSession,
            // 'lunchOptions' => $lunchOptions,
            'typeOfIndustry' => $typeOfIndustry,
            'typeOfJob' => $typeOfJob,
            'jobTitle' => $jobTitle,
            'trackOfInterest' => $trackOfInterest,
        );
        if ($backupEmail) {
            \Mail::send(['text'=>'mailKR'], $data, function($message) use ($companyEmail, $firstName, $backupEmail) {
                $message->to($companyEmail, $firstName)->subject('등록 확인 메일');
                $message->cc($backupEmail, $firstName)->subject('등록 확인 메일');
                $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
            });
        } else {
            \Mail::send(['text'=>'mailKR'], $data, function($message) use ($companyEmail, $firstName) {
                $message->to($companyEmail, $firstName)->subject('등록 확인 메일');
                $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
            });
        }
    }
    
    public function mailToUser_EN(string $firstName='', string $companyEmail='', string $backupEmail='')
    {
        $data = array('name'=>$firstName);
        if ($backupEmail) {
            \Mail::send(['text'=>'mailEN'], $data, function($message) use ($companyEmail, $firstName, $backupEmail) {
                $message->to($companyEmail, $firstName)->subject('Registration is complete');
                $message->cc($backupEmail, $firstName)->subject('Registration is complete');
                $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
            });
        } else {
            \Mail::send(['text'=>'mailEN'], $data, function($message) use ($companyEmail, $firstName) {
                $message->to($companyEmail, $firstName)->subject('Registration is complete');
                $message->from(env('MAIL_USERNAME'),env('MAIL_FROM_NAME'));
            });
        }
    }
}
 |