/var/www/hkosl.com/ifapc/html_20210817/IFAPC2021/application/controllers/Application.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
<?php
require_once APPPATH 'core/MY_Controller_2.php';

class 
Application extends MY_Controller_2
{
    public function 
__construct()
    {
        
// set $this->table and $this->model
        
parent::__construct(__CLASS__);

        
// if (!check_member_login($_SESSION['member_id'])) {
        //     $_SESSION['back'] = 'application';
        //     redirect(front_url('login'));
        // }

//        if(!is_osl()){
//            if(empty($_SESSION["login_id"]) || $_SESSION["login_id"] != 'skyadmin'){
//                if (time() > strtotime('2020-08-22 12:00:00')) {
//                    $_SESSION['error_msg'] = __('Application Closed');
//                    $_SESSION['msg_type'] = 'error';
//                    redirect(front_url('message'));
//                    exit;
//                }
//            }
//        }


        
$this->updated_single_language_fields = array(
            
'category_id' => __('Category'),
            
'nominee_title' => __('Title'),
            
'nominee_birth_date' => __('Date of Birth'),
            
'nominee_birth_place' => __('Place of Birth'),
            
'nominee_tel' => __('Contact Tel./ Moile No.'),
            
'nominee_wechat' => __('Wechat Account'),
            
'nominee_assistant_title' => __('Assistant&#39;s Title'),
            
'nominee_assistant_email' => __('Assistant&#39;s Email'),
            
'nominee_assistant_tel' => __('Assistant&#39;s Contact No.'),

            
'company_name' => __('Name of Present Company Name'),
            
'company_address' => __('Address of Present Company Name'),
            
'company_position' => __('Present Position'),
            
'company_position_years' => __('Number of years in present position'),
            
'company_is_founder' => __('Is the nominee a founder of current company?'),
            
'company_is_family_business' => __('Is it a family business?'),
            
'company_founded_year' => __('What year was the company established?'),
            
'company_industry' => __('Industry'),
            
'company_website' => __('Company website'),
            
'company_is_listed' => __('Is the company publicly listed?'),
            
'company_stock_code' => __('Stock code'),

            
'narrative_enterprise' => __('Enterprise Management and Development') . ' (' __('by bullet points') . ')',
            
'narrative_industry' => __('Industry Influence and Achievement') . ' (' __('by bullet points') . ')',
            
'narrative_contribution' => __('Contribution for the Greater Bay Area Development') . ' (' __('by bullet points') . ')',
            
'narrative_corporate' => __('Corporate and Social Responsibility') . ' (' __('by bullet points') . ')',
            
'narrative_personal' => __('Personal Reputation and Recognition') . ' (' __('provide supporting information') . ')',

            
'terms_is_declaration' => __('Declaration'),
            
'terms_is_attend' => __('Attend'),

            
'nominator_title' => __('Title'),
            
'nominator_job_title' => __('Present Position'),
            
'nominator_company' => __('Name of Present Company'),
            
'nominator_tel' => __('Office Tel./ Mobile No.'),
            
'nominator_address' => __('Correspondence Address'),
            
'nominator_reason' => __('Reason for nomination'),
        );
        
$tmp = array(
            
'nominee_email' => __('Email'),
            
'nominator_email' => __('Email'),

            
'organization_name' => __('Name of Organization'),
            
'service_position' => __('Position Held'),
            
'participation_year' => __('Participation Year'),
            
'involvement' => __('Community Service Involvement'),
            
'achievement' => __('Achievement and Contributions'),
        );
        
$this->single_language_fields array_merge($this->updated_single_language_fields$tmp);

        
$this->updated_multiple_language_fields = array(
            
'nominee_name_' => __('Name'),
            
'nominee_assistant_name_' => __('Assistant&#39;s Name'),

            
'nominator_name_' => __('Name'),
        );
        
$this->multiple_language_fields $this->updated_multiple_language_fields;

        
$this->single_language_validate_fields = array(
            
'nominee_email' => 'required|valid_email',
            
'nominator_email' => 'required|valid_email|differs[nominee_email]',

            
'nominee_title' => 'required|in_list[ms,mrs,miss,dr]',
            
'category_id' => 'required',
            
'nominee_birth_date' => 'required|callback_validate_date_of_birth',
            
'nominee_birth_place' => 'required',
            
'nominee_tel' => 'required|callback_validate_phone',

            
'nominee_assistant_title' => 'required|in_list[mr,ms,miss]',
            
'nominee_assistant_email' => 'required|valid_email',
            
'nominee_assistant_tel' => 'required|callback_validate_phone',

            
'company_name' => 'required',
            
'company_address' => 'required',
            
'company_position' => 'required',
            
'company_position_years' => 'required|greater_than_equal_to[0]',
            
'company_is_founder' => 'required|in_list[1,0]',
            
'company_is_family_business' => 'required|in_list[1,0]',
            
'company_founded_year' => 'required|callback_validate_year',
            
'company_industry' => 'required',
            
'company_website' => 'required|valid_url',
            
'company_is_listed' => 'required|in_list[1,0]',
            
'company_stock_code' => 'callback_validate_company_stock_code',

            
'organization_name' => 'callback_validate_required_service[organization_name]',
            
'service_position' => 'callback_validate_required_service[service_position]',
            
'participation_year' => 'callback_validate_required_service[participation_year]',
            
'involvement' => 'callback_validate_required_service[involvement]',
            
'achievement' => 'callback_validate_required_service[achievement]',

            
'narrative_enterprise' => 'required',
            
'narrative_industry' => 'required',
            
'narrative_contribution' => 'required',
            
'narrative_corporate' => 'required',
            
'narrative_personal' => 'required',

            
'terms_is_declaration' => 'required|in_list[1]',
            
'terms_is_attend' => 'in_list[1,0]',

            
'nominator_title' => 'required|in_list[mr,mrs,ms,miss,dr]',
            
'nominator_job_title' => 'required',
            
'nominator_company' => 'required',
            
'nominator_tel' => 'required|callback_validate_phone',
            
'nominator_address' => 'required',
            
'nominator_reason' => 'required',

        );
        
$this->multiple_language_validate_fields = array(
            
'nominee_name_' => 'required',
            
'nominee_assistant_name_' => 'required',

            
'nominator_name_' => 'required',
        );

        
$this->validate_massages['validate_date_of_birth'] = __('Please enter validated ') . ' %s';
        
$this->validate_massages['validate_phone'] = __('Please enter validated ') . ' %s';
        
$this->validate_massages['validate_year'] = __('Please enter validated ') . ' %s';
        
$this->validate_massages['validate_company_stock_code'] = __('Please enter validated ') . ' %s';
        
$this->validate_massages['validate_required_service'] = __('Please enter validated ') . ' %s';

        
$this->single_language_encpytion_fields = array(
            
'nominee_birth_place''nominee_tel''nominee_wechat''nominee_assistant_tel''company_name''company_address''company_position''company_industry''company_website''narrative_enterprise''narrative_industry''narrative_contribution''narrative_corporate''narrative_personal''nominator_job_title''nominator_company''nominator_tel''nominator_address''nominator_reason',
        );
        
$this->multiple_language_encpytion_fields = array(
            
'nominee_name_''nominee_assistant_name_''company_position_years''nominator_name_',
        );

        
// $this->fields_permission[$field][0]: nomminee, $this->fields_permission[$field][1]: nommintor
        // 1: view, 2: update
        // premission = view | update
        
$this->fields_permission = array(
            
'category_id' => '33',
            
'nominee_title' => '33',
            
'nominee_name_' => '33',
            
'nominee_birth_date' => '30',
            
'nominee_birth_place' => '30',
            
'nominee_email' => '33',
            
'nominee_tel' => '33',
            
'nominee_wechat' => '30',
            
'nominee_assistant_title' => '30',
            
'nominee_assistant_name_' => '30',
            
'nominee_assistant_email' => '30',
            
'nominee_assistant_tel' => '30',

            
'company_name' => '33',
            
'company_address' => '30',
            
'company_position' => '33',
            
'company_position_years' => '30',
            
'company_is_founder' => '30',
            
'company_is_family_business' => '30',
            
'company_founded_year' => '30',
            
'company_industry' => '30',
            
'company_website' => '30',
            
'company_is_listed' => '30',
            
'company_stock_code' => '30',

            
'organization_name' => '30',
            
'service_position' => '30',
            
'participation_year' => '30',
            
'involvement' => '30',
            
'achievement' => '30',

            
'narrative_enterprise' => '30',
            
'narrative_industry' => '30',
            
'narrative_contribution' => '30',
            
'narrative_corporate' => '30',
            
'narrative_personal' => '30',

            
'terms_is_declaration' => '30',
            
'terms_is_attend' => '30',

            
'nominator_title' => '33',
            
'nominator_name_' => '33',
            
'nominator_job_title' => '03',
            
'nominator_company' => '03',
            
'nominator_email' => '33',
            
'nominator_tel' => '03',
            
'nominator_address' => '03',
            
'nominator_reason' => '03',
        );

        
$this->nominee_modify_fields_permission = array(
            
'category_id' => '30',
            
'nominee_title' => '10',
            
'nominee_name_' => '10',
            
'nominee_birth_date' => '30',
            
'nominee_birth_place' => '30',
            
'nominee_email' => '10',
            
'nominee_tel' => '10',
            
'nominee_wechat' => '30',
            
'nominee_assistant_title' => '30',
            
'nominee_assistant_name_' => '30',
            
'nominee_assistant_email' => '30',
            
'nominee_assistant_tel' => '30',

            
'company_name' => '10',
            
'company_address' => '30',
            
'company_position' => '10',
            
'company_position_years' => '30',
            
'company_is_founder' => '30',
            
'company_is_family_business' => '30',
            
'company_founded_year' => '30',
            
'company_industry' => '30',
            
'company_website' => '30',
            
'company_is_listed' => '30',
            
'company_stock_code' => '30',

            
'organization_name' => '30',
            
'service_position' => '30',
            
'participation_year' => '30',
            
'involvement' => '30',
            
'achievement' => '30',

            
'narrative_enterprise' => '30',
            
'narrative_industry' => '30',
            
'narrative_contribution' => '30',
            
'narrative_corporate' => '30',
            
'narrative_personal' => '30',

            
'terms_is_declaration' => '30',
            
'terms_is_attend' => '30',

            
'nominator_title' => '10',
            
'nominator_name_' => '10',
            
'nominator_job_title' => '00',
            
'nominator_company' => '00',
            
'nominator_email' => '10',
            
'nominator_tel' => '00',
            
'nominator_address' => '00',
            
'nominator_reason' => '00',
        );


        
$this->nominator_modify_fields_permission = array(
            
'category_id' => '03',
            
'nominee_title' => '01',
            
'nominee_name_' => '01',
            
'nominee_birth_date' => '00',
            
'nominee_birth_place' => '00',
            
'nominee_email' => '01',
            
'nominee_tel' => '01',
            
'nominee_wechat' => '00',
            
'nominee_assistant_title' => '00',
            
'nominee_assistant_name_' => '00',
            
'nominee_assistant_email' => '00',
            
'nominee_assistant_tel' => '00',

            
'company_name' => '01',
            
'company_address' => '00',
            
'company_position' => '01',
            
'company_position_years' => '00',
            
'company_is_founder' => '00',
            
'company_is_family_business' => '00',
            
'company_founded_year' => '00',
            
'company_industry' => '00',
            
'company_website' => '00',
            
'company_is_listed' => '00',
            
'company_stock_code' => '00',

            
'organization_name' => '00',
            
'service_position' => '00',
            
'participation_year' => '00',
            
'involvement' => '00',
            
'achievement' => '00',

            
'narrative_enterprise' => '00',
            
'narrative_industry' => '00',
            
'narrative_contribution' => '00',
            
'narrative_corporate' => '00',
            
'narrative_personal' => '00',

            
'terms_is_declaration' => '00',
            
'terms_is_attend' => '00',

            
'nominator_title' => '01',
            
'nominator_name_' => '01',
            
'nominator_job_title' => '03',
            
'nominator_company' => '03',
            
'nominator_email' => '01',
            
'nominator_tel' => '03',
            
'nominator_address' => '03',
            
'nominator_reason' => '03',
        );
    }

    public function 
index()
    {
        
redirect(front_url('home'));
    }

    public function 
guideline($membership) {
        if (!
in_array($membership, ['nominee''nominator'])) {
            
redirect(front_url('home'));
        }
        
$this->load->vars('membership'$membership);
        
$this->load->view('main/guideline');
    }

    public function 
form_create($membership)
    {
        if (!
in_array($membership, ['nominee''nominator'])) {
            
redirect(front_url('home'));
        }

        
$this->membership $membership;

        
$latest_no Application_no_model::create()->id;
        
$reference_no 'REF' date('ym') . str_pad($latest_no6"0"STR_PAD_LEFT);
        
$application_data = array(
            
'reference_no' => 'REF' date('ym') . str_pad($latest_no6"0"STR_PAD_LEFT),
            
'creator_membership' => $membership,
            
'creator_session_id' => session_id(),
        );
        
$application $this->model->create();
        
$application->update($application_data);

        
$this->modify($application->id);
    }

    public function 
form_modify($id$email_token)
    {
        if (!
count($certain $this->model->get_certain($id))) {
            
$_SESSION['error_msg'] = __('The application is not available.') . ' #2';
        } else if (
$certain['email_token'] != $email_token) {
            
$_SESSION['error_msg'] = __('The application is not available.') . ' #3';
        }
        if (!empty(
$_SESSION['error_msg'])) {
            
$_SESSION['msg_type'] = 'error';
            
// $_SESSION['msg_back_url'] = front_url();
            
redirect(front_url('message'));
            exit;
        }
        
$this->membership $certain['creator_membership'] == 'nominee' 'nominator' 'nominee';

        
$this->fields_permission $this->{$this->membership '_modify_fields_permission'};

        if (
$this->membership == 'nominee');
        
$this->allow_methods = array('modify');
        
$this->modify($certain->id);
    }

    public function 
modify($id)
    {
        if (!
count($certain $this->model->get_certain($id))) {
            
$_SESSION['error_msg'] = __('The application is not available.') . ' #1';
        } else if (
$certain[$this->membership '_submitted'] != 0) {
            
$_SESSION['error_msg'] = __('Have not permissions.') . ' #3';
        } else if (
$this->membership == $certain['creator_membership'] && $certain['creator_session_id'] != session_id()) {
            
// creator first and create form must match session_id
            
$_SESSION['error_msg'] = __('Have not permissions.') . ' #1';
        }
        if (!empty(
$_SESSION['error_msg'])) {
            
$_SESSION['msg_type'] = 'error';
            
// $_SESSION['msg_back_url'] = front_url();
            
redirect(front_url('message'));
            exit;
        }

        if (
$this->membership == 'nominee') {
            if (
$count_application_file count($application_file Application_file_model::whereDeleted(0)->where('application_id'$id)->get())) {
                
$this->load->vars('count_application_file'$count_application_file);
                
$this->load->vars('base64_path''assets/files/application/');
                
$this->load->vars('application_file'$application_file);
            }
        }

        
$_SESSION['application'][$id]['membership'] = $this->membership;

        
$this->load->vars('categories'Category_model::get_data());
        
$this->load->vars('fields_permission'$this->fields_permission);
        
$this->load->vars('services'Application_service_model::get_data_by_field_with_value(['application_id' => $id]));
        
$this->load->vars('membership'$this->membership);

        
$this->allow_methods = array('modify');
        
parent::modify($id);
    }

    private function 
concat_service()
    {
        
$service_fields = array('organization_name''service_position' ,'participation_year''involvement''achievement');
        
$count count($this->input->post($service_fields[0]));

        
$return = array();
        
$j 0;
        for (
$i 0$i $count$i++) {
            
$have_value false;
            foreach (
$service_fields as $field) {
                if (!empty(
$this->input->post($field)[$i])) {
                    
$have_value true;
                    break;
                }
            }
            if (
$have_value) {
                foreach (
$service_fields as $field) {
                    
$return[$j][$field] = $this->input->post($field)[$i];
                }
                
$j++;
            }
        }
        return 
$return;
    }

    protected function 
validation_error($id null)
    {
        
$certain $this->model->get_certain($id);

        if (!
count($certain)) {
            
$_SESSION['error_msg'] = __('The application is not available.') . ' #6';
        }

        if (!empty(
$_SESSION['error_msg'])) {
            
$_SESSION['msg_type'] = 'error';
            
// $_SESSION['msg_back_url'] = front_url();
            
redirect(front_url('message'));
            exit;
        }

        
$this->modify($id);
    }

    public function 
submit_form($id null)
    {
        
$this->allow_methods = array('submit_form');

        
ob_start();
        
var_dump($_POST);
        
$getOb ob_get_clean();
        
$fileName APPPATH 'logs' DIRECTORY_SEPARATOR 'post' DIRECTORY_SEPARATOR date('Y-m-d') . '.php';
        
file_put_contents($fileNamedate('H:i:s') . ' ' __FILE__ ' -'$_SERVER['HTTP_USER_AGENT'] . '- ' "\r\n" $getOb "\r\n"FILE_APPEND);


        
$certain $this->model->get_certain($id);

        if (
$certain[$certain['creator_membership'] . '_submitted'] == 0) {
            
$this->membership $certain['creator_membership'];

            
$this->additional_submit_data['nominee_lang'] = get_lang();
            
$this->additional_submit_data['nominator_lang'] = get_lang();
        } else {
            
$this->membership $certain['creator_membership'] == 'nominee' 'nominator' 'nominee';
            
$this->fields_permission $this->{$this->membership '_modify_fields_permission'};

            
$this->additional_submit_data[$this->membership '_lang'] = get_lang();
        }

        if (!
count($certain)) {
            
$_SESSION['error_msg'] = __('The application is not available.') . '#4';
            
redirect(front_url('message'));
            exit;
        } else if (
$certain[$this->membership '_submitted'] != 0) {
            
$_SESSION['error_msg'] = __('You have already submitted this application.');
        } else if (
$this->membership == $certain['creator_membership'] && $certain['creator_session_id'] != session_id()) {
            
// creator first and create form must match session_id
            
$_SESSION['error_msg'] = __('Have not permissions.') . ' #7';
        }
        if (!empty(
$_SESSION['error_msg'])) {
            
$_SESSION['msg_type'] = 'error';
        }

        
$_POST['terms_is_declaration'] = $this->input->post('terms_is_declaration') == 'on' 0;
        
$_POST['terms_is_attend'] = $this->input->post('terms_is_attend') == 'on' 0;

        if (
$this->membership == 'nominee') {
            
$fields_permission_index 0;
        } else if (
$this->membership == 'nominator') {
            
$fields_permission_index 1;
        }

        foreach (
$this->fields_permission as $field => $permissions) {
            if (!(
$permissions[$fields_permission_index] & 2)) {
                unset(
$this->updated_single_language_fields[$field]);
                unset(
$this->updated_multiple_language_fields[$field]);

                unset(
$this->single_language_validate_fields[$field]);
                unset(
$this->multiple_language_validate_fields[$field]);
            }
        }

        if (
$this->run_validation) {
            
$this->post_services $this->concat_service();
            
// client clicked [Submit] not [Save]
            
$this->additional_submit_data[$this->membership '_submitted'] = 1;
        }

        
$this->submit_bypass true;

        
parent::submit_form($id);
    }

    protected function 
update_other_tables($id$current_update_data$action)
    {
        
$certain $this->model->get_certain($id);

        
$r = array();

        
$delete_data = array(
            
"deleted" => 1,
            
"deleted_by" => $_SESSION['sys_user_id'],
            
"deleted_at" => date('Y-m-d H:i:s'),
            
"deleted_by" => $_SESSION['member_id'],
        );

        if (
$certain[$certain['creator_membership'] . '_submitted'] == 0) {
            
$fields = array('nominee_email''nominator_email');
            foreach (
$fields as $field) {
                
$post_email set_value($field);

                
$member_data = array('email' => $post_email);
                if (!
count($member Member_model::where('email'$post_email)->where('deleted'0)->get()[0])) {
                    
$new_password substr(md5(uniqid(mt_rand(), true)), 06);
                    
$new_password str_shuffle($new_password 'Gb18');

                    
$member_data['password'] = $this->encryption->encrypt($new_password);
                    
$member_data['verified'] = 1;

                    
$member Member_model::create();
                    
$member->update($member_data);
                }
            }

            
$fields = array('nominee_email''nominator_email');
            foreach (
$fields as $field) {
                
$post_email set_value($field);
                
$member Member_model::where('email'$post_email)->where('deleted'0)->get()[0];

                
$membership substr($field0, -strlen('_email'));

                
$r[$membership '_member_id'] = $member['id'];

                
$email_data = array(
                    
'id' => $id,
                    
'membership' => $membership,
                    
'email' => $post_email,
                    
'reference_no' => $certain['reference_no'],
                    
'member_name_en' => $this->encryption->decrypt($membership == 'nominator' $current_update_data['nominator_name_en'] : $current_update_data['nominee_name_en']),
                    
'member_name_tc' => $this->encryption->decrypt($membership == 'nominator' $current_update_data['nominator_name_tc'] : $current_update_data['nominee_name_tc']),
                    
'another_member_name_en' => $this->encryption->decrypt($membership == 'nominee' $current_update_data['nominator_name_en'] : $current_update_data['nominee_name_en']),
                    
'another_member_name_tc' => $this->encryption->decrypt($membership == 'nominee' $current_update_data['nominator_name_tc'] : $current_update_data['nominee_name_tc'])
                );

                if (
$this->membership == $membership) {
                    
$this->application_thank_email($email_data);
                } else {
                    
$email_data['name'] = $this->encryption->decrypt($current_update_data[$membership '_name_' . (get_lang() == 'en' 'en' 'tc')]);
                    
$email_data['creator_name'] = $this->encryption->decrypt($current_update_data[$certain['creator_membership'] . '_name_' . (get_lang() == 'en' 'en' 'tc')]);
                    
$email_data['email_token'] = $r['email_token'] = bin2hex(random_bytes(16));

                    
$this->application_token_email($email_data);
                }

                
$application_data[$member_data['field'] . '_member_id'] = Member_model::where('email'$post_email)->where('deleted'0)->get()[0]['id'];
            }
        }

        if (
$this->membership == 'nominee') {
            
Application_service_model::where('application_id'$id)->update($delete_data);
            
$services $this->concat_service();
            
$service_datat = array(
                
"application_id" => $id,
            );
            foreach (
$services as $i => $row) {
                
$row['application_id'] = $id;
                
$row['sort'] = ++$i;
                
Application_service_model::create()->update($row);
            }
        }

        return 
$r;
    }

    protected function 
submit_bypass($data$id null)
    {
        if (!
count($certain $this->model->get_certain($id))) {
            
$_SESSION['msg_type'] = 'error';
            
$_SESSION['error_msg'] = __('The application is not available.') . ' #5';
            
// $_SESSION['msg_back_url'] = front_url();
            
redirect(front_url('message'));
            exit;
        }

        
$other_tables_data $this->update_other_tables($id$data'update');

        if (!empty(
$other_tables_data)) {
            
$data array_merge($data$other_tables_data);
        }

        if (empty(
$_SESSION['error_msg'])) {
            foreach(
$data as $field => $value) {
                
$this->model->where('id'$id)->update(array($field => $value));
            }
            
$_SESSION['msg_type'] = 'success';
            
$_SESSION['success_msg'] = __('Submitted Nomination. Reference No.:') . ' ' $certain['reference_no'];
            
// $_SESSION['msg_back_url'] = front_url();
            
redirect(front_url('message'));
        } else {
            
$this->validation_error($id);
            return;
        }

        
redirect(front_url($this->table));
    }

    
/*protected function registration_email($data)
    {
    $body = '';
    $body .= '<tr>';
    $body .= '    <td align="center"><h1>' . __('Welcome') . '</h1>';
    $body .= '</tr>';
    $body .= '<tr>';
    $body .= '    <td>Login Password:' . $data['new_password'] . '</h3>';
    $body .= '</tr>';
    $body .= '<tr>';
    $body .= '    <td><h3>' . __('Please confirm your account by clicking the link below') . ':</h3>';
    $body .= '</tr>';
    $body .= '<tr>';
    $body .= '    <td><h5><a href="' . front_url('member/verify/') . $data['id'] . '/' . $data['token'] . '" target="_blank">' . __('Confirm Email') . '</a></h5></td>';
    $body .= '</tr>';

    $this->send_email($data['email'], __('Verification of registration'), $body);
    }*/

    // protected function application_email($data)
    // {
    //     $body = '';
    //     $body .= '<tr>';
    //     $body .= '    <td align="center"><h1>' . __($data['field']) . '</h1>';
    //     $body .= '</tr>';
    //     if ($data['registration']) {
    //         $body .= '<tr>';
    //         $body .= '    <td>Login Password:' . $data['new_password'] . '</h3>';
    //         $body .= '</tr>';
    //         $body .= '<tr>';
    //         $body .= '    <td><h3>' . __('You are ' . $data['field'] . ', please confirm your account by clicking the link below') . ':</h3>';
    //         $body .= '</tr>';
    //         $body .= '<tr>';
    //         $body .= '    <td><h5><a href="' . front_url('member/verify/') . $data['id'] . '/' . $data['token'] . '" target="_blank">' . __('Confirm Email') . '</a></h5></td>';
    //         $body .= '</tr>';
    //     } else {
    //         $body .= '<tr>';
    //         $body .= '    <td><h3>' . __('You are ' . $data['field'] . ', please check detail by clicking the link below') . ':</h3>';
    //         $body .= '</tr>';
    //         $body .= '<tr>';
    //         $body .= '    <td><h5><a href="' . front_url('application') . '" target="_blank">' . __('Check detail') . '</a></h5></td>';
    //         $body .= '</tr>';
    //     }

    //     $this->allow_methods = array('send_email');
    //     $this->send_email($data['email'], __($data['field'] . ' of GBA'), $body);
    // }

    
protected function application_thank_email($data)
    {
        
// if (PRODUCTION != 1) {
        //     return true;
        //     exit;
        // }

        
$site_info get_site_info();
        
//$lang = get_lang();
        
$lang get_lang() == 'en' 'en' 'tc';
        
$from $site_info['email'];
        
$from_name $site_info['name_' $lang];
        
$to $data['email'];
        
$subject $site_info['name_' $lang] . ' – ' __(ucfirst($data['membership'])) . ' ' __('Notification email');
        
$message '';
        
$bcc = array('kelvinchan@onesolution.com.hk''skycheng@onesolution.com.hk');
        
//$bcc = array();

        
$template_field = array(
            
'logo' => assets_url('main/img/2020/logo'. (get_lang() == 'sc' '' '' ) . '.png'),
            
'footer' => date('Y-m-d H:i:s'),
            
'email' => $site_info['email'],
        );
        if (
$data['membership'] == 'nominee') {
            
$template_field['content'] = '<p>' __('To : {%name%}', ['name' => $data['member_name_en'] . ' ' $data['member_name_tc']]) . '<br><br>' __('Your submission has been received via online system.  Thank you so much for your participation in GBA Outstanding Women Entrepreneur Awards 2020.') . '<br><br>' __('The nominator {%name%} will soon be reached by email to ask for confirmation reply before the deadline.', ['name' => $data['another_member_name_en'] . ' ' $data['another_member_name_tc']]) . '</p>';
        } else {
            
$template_field['content'] = '<p>' __('To : {%name%}', ['name' => $data['member_name_en'] . ' ' $data['member_name_tc']]) . '<br><br>' __('Your nomination has been received via online system.  Thank you so much for your support to GBA Outstanding Women Entrepreneur Awards 2020.') . '<br><br>' __('he nominee {%name%} will soon be reached by email to ask for providing entrant information and supporting documents before the deadline.', ['name' => $data['another_member_name_en'] . ' ' $data['another_member_name_tc']]) . '</p>';
        }
        
$template_field['content'] .= '<br><p>' __('Reference No.') . ': '$data['reference_no'] . '</p><br><br><p><b>' __('Enquiries') . '</b><br>' __('Tel') . ': (852) 3698 8024 / (852) 2320 3620<br>' __('Email') . ': info@gba-owea.com<br>' __('Website') . ': www.gba-owea.com<br>' .'</p>';

        
$attachment = array();

        
log_message('error'$template_field['content']);

        
$template_path APPPATH DIRECTORY_SEPARATOR 'resources' DIRECTORY_SEPARATOR 'email' DIRECTORY_SEPARATOR 'application_' get_lang() . '.html';
        
$template_content '';
        
$table_name 'application';
        
$table_id $data['id'];

        
_ci_send_email($from$from_name$to$subject$message$bcc$template_field$template_path$template_content$attachment$table_name$table_id);
    }

    protected function 
application_token_email($data)
    {
        
// if (PRODUCTION != 1) {
        //     return true;
        //     exit;
        // }

        
$site_info get_site_info();
        
//$lang = get_lang();
        
$lang get_lang() == 'en' 'en' 'tc';
        
$from $site_info['email'];
        
$from_name $site_info['name_' $lang];
        
$to $data['email'];
        
$subject $site_info['name_' $lang] . ' – ' __(ucfirst($data['membership'])) . ' ' __('Notification email');
        
$message '';
        
// $bcc = array('kelvinchan@onesolution.com.hk', 'skycheng@onesolution.com.hk');
        //$bcc = array();

        
$first_lang get_lang() == 'sc' 'sc' 'tc';

        
$template_field = array(
            
'logo' => assets_url('main/img/2020/logo'. (get_lang() == 'sc' '' '' ) . '.png'),
            
'footer' => date('Y-m-d H:i:s'),
            
'email' => $site_info['email'],
        );
        if (
$data['membership']  == 'nominee') {
            
$form __('Submission Form');
            
$template_field['content'] = '<p>' __('To : {%name%}', ['name' => $data['member_name_' . (get_lang() == 'sc' 'tc' get_lang()) ]]) . '<br><br>' __('We are glad to inform you that Mr / Ms {%name%} would like to nominate you for the GBA Outstanding Women Entrepreneur Awards 2020.', ['name' => $data['another_member_name_' . (get_lang() == 'sc' 'tc' get_lang()) ]]) . '<br><br>' __('Please click the link below to read the Awards details and fill out the submission form, then submit together with supporting files by no later than Aug 9, 2020.') . '</p>';
        } else {
            
$form __('Nomination Form');
            
$template_field['content'] = '<p>' __('To : {%name%}', ['name' => $data['member_name_' . (get_lang() == 'sc' 'tc' get_lang()) ]]) . '<br><br>' __('We are glad to inform you that Ms {%name%} would like to participate in GBA Outstanding Women Entrepreneur Awards 2020, and invite you to be the nominator.', ['name' => $data['another_member_name_' . (get_lang() == 'sc' 'tc' get_lang()) ]]) . '<br><br>' __('Please click the link below to read the Awards details, then fill out the nomination form and submit by no later than Aug 9, 2020.') . '</p>';
        }
        
$template_field['content'] .= '<p><a href="' front_url('application/form_modify/') . $data['id'] . '/' $data['email_token'] . '" target="_blank">' $form '</a></p><br><p>' __('Reference No.') . ': '$data['reference_no'] . '</p><br><br><p><b>' __('Enquiries') . '</b><br>' __('Tel') . ': (852) 3698 8024 / (852) 2320 3620<br>' __('Email') . ': info@gba-owea.com<br>' __('Website') . ': www.gba-owea.com<br>' .'<br></p>';

        
$attachment = array();

        
log_message('error'$template_field['content']);

        
$template_path APPPATH DIRECTORY_SEPARATOR 'resources' DIRECTORY_SEPARATOR 'email' DIRECTORY_SEPARATOR 'application_' get_lang() . '.html';
        
$template_content '';
        
$table_name 'application';
        
$table_id $data['id'];

        
_ci_send_email($from$from_name$to$subject$message$bcc$template_field$template_path$template_content$attachment$table_name$table_id);
    }

    public function 
validate_date_of_birth($str)
    {
        if (!empty(
$str)) {
            
$date = new DateTime();
            
$format 'Y-m-d';
            
$input_date DateTime::createFromFormat($format$str);
            if (!
$input_date) return false;
            
//120 years
            
return $input_date->format($format) == $str && $input_date <= $date && $date->format('U') - $input_date->format('U') < 3786824202;
        }
        return 
true;
    }

    public function 
validate_phone($str)
    {
        if (!empty(
$str)) {
            
$pattern '/^\(*\+*\d{0,3}\)*[- ]*\d{4}\s*\d{4,15}$/';
            return (bool) 
preg_match($pattern$str);
        }
        return 
true;
    }

    public function 
validate_year($str)
    {
        if (!empty(
$str)) {
            
$pattern '/^\d{4}$/';
            return (bool) 
preg_match($pattern$str) && $str <= date('Y');
        }
        return 
true;
    }

    public function 
validate_required_service($str$field)
    {
        if (!
count($this->post_services)) return false;
        foreach (
$this->post_services as $row) {
            if (empty(
$row[$field])) {
                return 
false;
            }
        }
        return 
true;
    }

    public function 
validate_company_stock_code($str)
    {
        
$company_is_listed $this->input->post('company_is_listed');
        if ((
$company_is_listed && empty($str)) || (!$company_is_listed && !empty($str))) {
            return 
false;
        }
        return 
true;
    }

}