/
application_form.php
4103 lines (3966 loc) · 234 KB
/
application_form.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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* Plugin Name: Application Form
Plugin URI: http://applicationform.com/
Description: custom application form for IAS
Depends: Ib Chase Payment
Version: 4.0
Author: Anand Pandey
Author URI: http://anand.pandey@infobeans.com/
License: GPLv2 or later
*/
/* 0846 - 08/06/15 - activation hook after activate create a table named application_form */
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
register_activation_hook(__FILE__, 'application_form_install');
global $application_db_version;
$application_db_version = '1.0';
function application_form_install() {
global $wpdb;
global $application_db_version;
/* create new tables */
$programs = $wpdb->prefix . 'programs';
$application_data = $wpdb->prefix . 'application_data';
$templates = $wpdb->prefix . 'templates';
$application_templates = $wpdb->prefix . 'application_templates';
$company = $wpdb->prefix . 'company';
$tab_slug = $wpdb->prefix . 'tab_slugs';
/* update users table */
$users = $wpdb->prefix . 'users';
$charset_collate = $wpdb->get_charset_collate();
$sql_users = "ALTER TABLE $users
ADD my_column varchar(255)";
$sql_app = "CREATE TABLE IF NOT EXISTS $programs (
id int(10) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
description varchar(255) NOT NULL,
created_by int(10) NOT NULL,
created_on datetime,
modified_by int(10) NOT NULL,
modified_on datetime NOT NULL,
deleted_by int(10) NOT NULL,
deleted_on datetime NOT NULL,
doc_id int(10) NOT NULL,
abbreviation varchar(10) NULL,
PRIMARY KEY id (id)
) $charset_collate";
$sql_app_data = "CREATE TABLE IF NOT EXISTS $application_data (
id int(10) NOT NULL AUTO_INCREMENT,
program_id int(10) NOT NULL,
user_id int(10) NOT NULL,
company_id int(10) NOT NULL,
application_data longtext NOT NULL,
created_by int(10) NOT NULL,
created_on datetime,
modified_by int(10) NOT NULL,
modified_on datetime,
deleted_by int(10) NOT NULL,
deleted_on datetime,
PRIMARY KEY id (id),
FOREIGN KEY user_id (user_id) REFERENCES $users(ID),
FOREIGN KEY program_id (program_id) REFERENCES $applications(id),
FOREIGN KEY company_id (company_id) REFERENCES $company(id)
) ENGINE=MyIsam $charset_collate";
$sql_temp = "CREATE TABLE IF NOT EXISTS $templates (
id int(10) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
description varchar(255) NOT NULL,
slug varchar(255) NOT NULL,
created_by int(10) NOT NULL,
created_on datetime,
modified_by int(10) NOT NULL,
modified_on datetime,
deleted_by int(10) NOT NULL,
deleted_on datetime,
PRIMARY KEY id (id)
) $charset_collate";
$sql_app_temp = "CREATE TABLE IF NOT EXISTS $application_templates (
id int(10) NOT NULL AUTO_INCREMENT,
program_id int(10) NOT NULL,
template_id int(10) NOT NULL,
template_render_order int(10) NOT NULL,
tab_slug varchar(255) NOT NULL,
created_by int(10) NOT NULL,
created_on datetime,
modified_by int(10) NOT NULL,
modified_on datetime,
deleted_by int(10) NOT NULL,
deleted_on datetime,
PRIMARY KEY id (id),
FOREIGN KEY template_id (template_id) REFERENCES $templates (id),
FOREIGN KEY program_id (program_id) REFERENCES $applications(id)
) $charset_collate";
$sql_company = "CREATE TABLE IF NOT EXISTS $company (
id int(10) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
created_by int(10) NOT NULL,
created_on datetime,
modified_by int(10) NOT NULL,
modified_on datetime,
deleted_by int(10) NOT NULL,
deleted_on datetime,
PRIMARY KEY id (id)
) $charset_collate";
$sql_tab = "CREATE TABLE IF NOT EXISTS $tab_slug (
id int(11) NOT NULL AUTO_INCREMENT,
tab_slug varchar(255) NOT NULL,
tab_name varchar(255) NOT NULL,
tab_order int(11) NOT NULL,
PRIMARY KEY (id)
) $charset_collate";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta($sql_app);
dbDelta($sql_app_data);
dbDelta($sql_temp);
dbDelta($sql_app_temp);
dbDelta($sql_company);
dbDelta($sql_users);
dbDelta($sql_tab);
add_option('application_db_version', $application_db_version);
$wpdb->query("ALTER TABLE $users ADD COLUMN first_name varchar(255) AFTER display_name");
$wpdb->query("ALTER TABLE $users ADD COLUMN middle_name varchar(255) AFTER display_name");
$wpdb->query("ALTER TABLE $users ADD COLUMN last_name varchar(255) AFTER display_name");
$wpdb->query("ALTER TABLE $users ADD COLUMN phone varchar(255) AFTER display_name");
$wpdb->query("ALTER TABLE $users ADD COLUMN fax varchar(255) AFTER display_name");
$wpdb->query("ALTER TABLE $users ADD COLUMN address varchar(255) AFTER display_name");
$wpdb->query("ALTER TABLE $users ADD COLUMN third_party varchar(255) AFTER display_name");
$wpdb->query("ALTER TABLE $users ADD COLUMN title varchar(255) AFTER display_name");
$wpdb->query("ALTER TABLE $users ADD COLUMN company_name varchar(255) AFTER display_name");
$wpdb->query("ALTER TABLE $users ADD COLUMN created_by int(10) AFTER display_name");
$wpdb->query("ALTER TABLE $users ADD COLUMN created_on DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL");
$wpdb->query("ALTER TABLE $users ADD COLUMN modified_by int(10) AFTER display_name");
$wpdb->query("ALTER TABLE $users ADD COLUMN modified_on DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL");
$wpdb->query("ALTER TABLE $users ADD COLUMN deleted_by int(10) AFTER display_name");
$wpdb->query("ALTER TABLE $users ADD COLUMN deleted_on DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL");
$wpdb->query("ALTER TABLE $users ADD COLUMN crm_id int(10) NOT NULL");
$wpdb->query("ALTER TABLE $users ADD COLUMN crm_lead_id int(10) NOT NULL");
}
/* 0846 - 08/06/15 - function to deactivate and uninstall the table from database */
register_deactivation_hook(__FILE__, 'application_form_uninstall');
function application_form_uninstall() {
global $wpdb;
$applications = $wpdb->prefix . 'applications';
$application_data = $wpdb->prefix . 'application_data';
$templates = $wpdb->prefix . 'templates';
$application_templates = $wpdb->prefix . 'application_templates';
$company = $wpdb->prefix . 'company';
$users = $wpdb->prefix . 'users';
$tab_slugs = $wpdb->prefix . 'tab_slugs';
//$wpdb->query("DROP TABLE IF EXISTS $application_data, $application_templates, $company");
//$wpdb->query("DROP TABLE $templates");
//$wpdb->query("DROP TABLE $applications");
/* $wpdb->query( "ALTER TABLE $users DROP COLUMN first_name");
$wpdb->query( "ALTER TABLE $users DROP COLUMN middle_name");
$wpdb->query( "ALTER TABLE $users DROP COLUMN last_name");
$wpdb->query( "ALTER TABLE $users DROP COLUMN phone");
$wpdb->query( "ALTER TABLE $users DROP COLUMN fax");
$wpdb->query( "ALTER TABLE $users DROP COLUMN address");
$wpdb->query( "ALTER TABLE $users DROP COLUMN third_party");
$wpdb->query( "ALTER TABLE $users DROP COLUMN title");
$wpdb->query( "ALTER TABLE $users DROP COLUMN company_name");
$wpdb->query( "ALTER TABLE $users DROP COLUMN created_by");
$wpdb->query( "ALTER TABLE $users DROP COLUMN created_on");
$wpdb->query( "ALTER TABLE $users DROP COLUMN modified_by");
$wpdb->query( "ALTER TABLE $users DROP COLUMN modified_on");
$wpdb->query( "ALTER TABLE $users DROP COLUMN deleted_by");
$wpdb->query( "ALTER TABLE $users DROP COLUMN deleted_on");
$wpdb->query( "ALTER TABLE $users DROP COLUMN crm_id");
$wpdb->query( "ALTER TABLE $users DROP COLUMN crm_lead_id"); */
}
/* 0846 - 08/06/15 - add application menu on admin */
add_action('admin_menu', 'application_form_setup_menu');
function application_form_setup_menu() {
global $current_user; //user global
get_currentuserinfo(); // Make sure global is set, if not set it.
add_menu_page('application-form-register', 'Programs', 'application-form', 'application-form-view');
add_menu_page( 'profile update', 'Profile', 'application-form', 'profile.php', '', '', 5 );
/* check current user is customer */
if (user_can($current_user, "subscriber") || user_can($current_user, "modified") || user_can($current_user, "administrator") || user_can($current_user, "customer") || user_can($current_user, "new_user" )) {
add_submenu_page('application-form-view', 'All Program Types ', 'All Program Types', 'application-form', 'application-form-register', 'view_application_form');
//add_menu_page('My Invoices', 'My Invoices', 'read', 'my_invoices_list', 'my_invoices_list');
// add_menu_page('My Companies', 'My Companies', 'application-form', 'my-companies', 'my_companies', '', 6);
//add_menu_page('My Contacts', 'My Contacts', 'application-form', 'my-contacts', 'my_contacts', '', 7);
//add_submenu_page('application-form-register', 'Create', 'Create', 'application-form', 'create-form-register', 'application_form_init');
}
/* check current user is staff */
if (user_can($current_user, "staff")) {
add_menu_page('All Companies', 'All Companies', 'application-form', 'all-companies', 'all_companies', '', 8);
add_submenu_page('application-form-view', 'All', 'All', 'application-form', 'application-form-register', 'view_application_form');
add_submenu_page('application-form-view', 'View Application', 'View Application', 'application-form', 'create-form-register', 'application_form_init');
}
/* check current user is administrator */
//if (user_can($current_user, "administrator")) {
add_submenu_page('application-form-view', 'Templates', 'Templates', 'application-form', 'templates', 'view_templates');
//add_submenu_page('app_tmp', 'Templates', 'Templates', 'application-form', 'application-form-register', 'view_templates');
//}
if (!user_can($current_user, "administrator")) {
if (user_can($current_user, "moderator")) {
remove_menu_page('Change Password', 'Change Password', 'read', 'change_password', 'change_password', '');
remove_menu_page('theme_my_login');
remove_menu_page('IB-Contact-Form');
remove_menu_page('options-general.php');
} else {
add_menu_page('Change Password', 'Change Password', 'read', 'change_password', 'change_password', '');
}
}
add_submenu_page('application-form-view', 'View', 'Program Documents', 'application-form', 'application-form-view', 'view_all_applications');
//add_submenu_page('application-form-register', 'Documents', 'Documents', 'application-form', 'documents', 'documents');
add_submenu_page('application-form-view', ' Application Form', ' Application Form', 'application-form', '-application-form', '_application_form');
add_submenu_page('application-form-view', "Assign Roles", "Permission", "application-form", "app_mang_role", "manage_templates_roles");
add_submenu_page('application-form-view', "Renewal Notification", "Renewal notification", "application-form", "renewal_notification", "renewal_notification");
//add_menu_page( 'Logut', 'Logout', 'read', 'logout', 'Logout','',1);
//add_submenu_page( 'application-form-register', 'Payments', 'Payments', 'application-form', 'payments', 'payments' );
}
// Add short code to call application_form_init
add_shortcode('application_form', 'application_form_init');
/* 0846 - 08/06/15 - function for show application form */
function application_form_init($params = '') {
global $current_user;
get_currentuserinfo();
echo "<script>var admin_url='" . admin_url() . "'
var ajaxurl = '" . admin_url("admin-ajax.php") . "';";
if (isset($_GET['view']) && $_GET['view'] == 'true') {
echo "var view = true;";
} else {
echo "var view = false;";
}
if (user_can($current_user, "staff")) {
echo "var staff=true;";
} else {
echo "var staff=false;";
}
echo "</script>";
//wp_enqueue_style( 'bootstrap-css', plugin_dir_url(__FILE__).'css/bootstrap.min.css' );
//wp_enqueue_style( 'switchery-css', plugin_dir_url(__FILE__).'css/switchery.min.css' );
//wp_enqueue_style( 'application-form-css', plugin_dir_url(__FILE__).'css/application-form.css' );
wp_enqueue_style('upload-file-css', plugin_dir_url(__FILE__) . 'css/uploadfile.css');
include('register-application-form.php');
}
/* 0787---8/5/2015----- function to save application form details into the database When click on next button on application form */
add_action('admin_post_application-form-on-nextbtn', 'application_form_on_nextbtn');
function application_form_on_nextbtn() {
global $current_user;
global $wpdb;
get_currentuserinfo();
$pageCode = 'application-form-register';
//print_r($_POST['data']);
$json = json_encode($_POST['data']);
//echo "<pre>";print_r($json);die();
$company_id = get_company_id();
if ($_POST['editid'] != "0" && is_numeric($_POST['editid'])) {
//Check on click of next button any new data is filled or not
$log_sql_crm = 'select id,content from ' . $wpdb->prefix . 'logger where LOWER(ref_type) = "application" and ref_id = "' . $_POST['editid'] . '" and UPPER(type) = "CRM" order by id desc limit 0,1';
$log_details_crm = $wpdb->get_results($log_sql_crm);
if ($log_details_crm) {
$pre_log_content = json_decode($log_details_crm[0]->content);
$new_log_content = json_decode($json);
$diff_result = get_json_diff($pre_log_content, $new_log_content, 'ARRAY');
}
/* checking crm id */
$sql_data = $wpdb->query('select crm_id from ' . $wpdb->prefix . 'application_data where crm_id is not NULL AND id=' . $_POST['editid']);
if ($wpdb->num_rows == 1) {
if(isset($diff_result) && $diff_result !=NULL){-
$wpdb->update($wpdb->prefix . 'application_data', array('application_data' => $json, 'status' => 'Modified'), array('id' => $_POST['editid']));
$log_data = array(); //Declare array to stored log data
$log_data['ref_type'] = "Application";
$log_data['ref_id'] = $_POST['editid'];
$log_data['title'] = "Existing Application Updated";
$log_data['description'] = "Application Updated From Portal";
$log_data['content'] = $json;
//Used to log activity
IB_Logging::ib_log_activity($log_data);
}
} else {
$sql_is_draft = $wpdb->query('select * from ' . $wpdb->prefix . 'application_data where status="New" AND id=' . $_POST['editid']);
if ($wpdb->num_rows == 0) {
$wpdb->update($wpdb->prefix . 'application_data', array('application_data' => $json, 'status' => 'Draft'), array('id' => $_POST['editid']));
} else {
$wpdb->update($wpdb->prefix . 'application_data', array('application_data' => $json, 'status' => 'New'), array('id' => $_POST['editid']));
}
$log_data = array(); //Declare array to stored log data
$log_data['ref_type'] = "Application";
$log_data['ref_id'] = $_POST['editid'];
$log_data['title'] = "Existing Application Updated";
$log_data['description'] = "Application Updated From Portal";
$log_data['content'] = $json;
//Used to log activity
IB_Logging::ib_log_activity($log_data);
}
/* assign roles change via application edit/submit */
if (isset($_POST['data']['new_application']['_linked']['contact']['new_technicalcontactid'])) {
$technical = $_POST['data']['new_application']['_linked']['contact']['new_technicalcontactid'];
} else {
$technical = '';
}
if (isset($_POST['data']['new_application']['_linked']['contact']['new_billingcontact'])) {
$billing = $_POST['data']['new_application']['_linked']['contact']['new_billingcontact'];
} else {
$billing = '';
}
if (isset($_POST['data']['new_application']['_linked']['contact']['new_legalcontactid'])) {
$legal = $_POST['data']['new_application']['_linked']['contact']['new_legalcontactid'];
} else {
$legal = '';
}
if (isset($_POST['data']['new_application']['_linked']['contact']['new_chiefadminofficerid'])) {
$chief = $_POST['data']['new_application']['_linked']['contact']['new_chiefadminofficerid'];
} else {
$chief = '';
}
$edit_id = $_POST['editid'];
change_user_roles_from_application($technical, $billing, $legal, $chief, $edit_id);
echo trim($_POST['editid']);
} else {
if (isset($_POST["quotation_id"])) {
$quotation_id = $_POST["quotation_id"];
} else {
$quotation_id = '';
}
$wpdb->insert($wpdb->prefix . 'application_data', array('program_id' => $_POST['program_id'], 'company_id' => $current_user->company_id, 'user_id' => get_current_user_id(), 'application_data' => $json, 'status' => 'Draft', 'created_by' => get_current_user_id(), 'created_on' => current_time('mysql', 1), 'quotation_id' => $quotation_id));
$lastid = trim($wpdb->insert_id);
/* assign roles change via application edit/submit */
if (isset($_POST['data']['new_application']['_linked']['contact']['new_technicalcontactid'])) {
$technical = $_POST['data']['new_application']['_linked']['contact']['new_technicalcontactid'];
} else {
$technical = '';
}
if (isset($_POST['data']['new_application']['_linked']['contact']['new_billingcontact'])) {
$billing = $_POST['data']['new_application']['_linked']['contact']['new_billingcontact'];
} else {
$billing = '';
}
if (isset($_POST['data']['new_application']['_linked']['contact']['new_legalcontactid'])) {
$legal = $_POST['data']['new_application']['_linked']['contact']['new_legalcontactid'];
} else {
$legal = '';
}
if (isset($_POST['data']['new_application']['_linked']['contact']['new_chiefadminofficerid'])) {
$chief = $_POST['data']['new_application']['_linked']['contact']['new_chiefadminofficerid'];
} else {
$chief = '';
}
change_user_roles_from_application($technical, $billing, $legal, $chief, $lastid);
$log_data = array(); //Declare array to stored log data
$log_data['ref_type'] = "Application";
$log_data['ref_id'] = $lastid;
$log_data['title'] = "New Application Register";
$log_data['description'] = "Application Registered into database";
$log_data['content'] = $json;
//Used to log activity
IB_Logging::ib_log_activity($log_data);
//check quotation id
if (isset($_POST["quotation_id"]) && $_POST["quotation_id"] != 0) {
$quotation_id = $_POST["quotation_id"];
//check if scope document uploaded or not.
$sql = "SELECT post_id FROM " . $wpdb->prefix . "quotation_scope_data qsa, wp_terms wt WHERE wt.term_id = qsa.category_term_id AND qsa.quotation_id = " . $_POST['quotation_id'] . " AND wt.slug = 'customer-quotation' ORDER By id DESC LIMIT 0,1";
$quotation_doc = $wpdb->get_row($sql);
//Case where quotation document and id added to DB if quotation id and doc exists
if ($quotation_doc) {
$sql = "select term_id FROM " . $wpdb->prefix . "terms WHERE slug='scope-of-accreditation-document'";
$media_category = $wpdb->get_row($sql);
//saving file in database
$wpdb->insert($wpdb->prefix . 'application_docs', array('application_id' => $lastid, 'doc_id' => $quotation_doc->post_id, 'doc_category' => $media_category->term_id));
//Adding Document data in logger on updation
$doc_path = array();
$sql = "SELECT wad.doc_id,wt.slug FROM " . $wpdb->prefix . "application_docs wad, " . $wpdb->prefix . "terms wt WHERE wad.doc_category = wt.term_id AND application_id = " . $lastid . " ORDER By id DESC";
$app_docs = $wpdb->get_results($sql);
if ($app_docs) {
foreach ($app_docs as $doc):
$sql = "SELECT meta_value FROM " . $wpdb->prefix . "postmeta WHERE post_id = " . $doc->doc_id;
$post = $wpdb->get_row($sql);
if (isset($doc_path[$doc->slug]))
$doc_path[$doc->slug] .= ";" . $post->meta_value;
else
$doc_path[$doc->slug] = $post->meta_value;
endforeach;
$json_data = json_encode($doc_path);
$log_data = array(); //Declare array to stored log data
$log_data['ref_type'] = "Application_docs";
$log_data['ref_id'] = $lastid;
$log_data['title'] = "Application Docs Updated";
$log_data['description'] = "Application Docs Updated From Portal";
$log_data['content'] = $json_data;
//Used to log activity
IB_Logging::ib_log_activity($log_data);
}
//Adding Document data in logger on updation
//Adding document data in cron table
$sql = "SELECT slug FROM " . $wpdb->prefix . "terms WHERE term_id = " . $media_category->term_id;
$category = $wpdb->get_row($sql);
$wpdb->query("INSERT INTO " . $wpdb->prefix . "cron_files (post_id, category_name,file_path,action_type,ref_type,ref_id,status,attempt) VALUES(" . $quotation_doc->post_id . ",'" . $category->slug . "','" . get_post_meta( $quotation_doc->post_id , '_wp_attached_file', true ) . "','add','application'," . $lastid . ",'open','0')");
//Adding document data in cron table
}
//Case where quotation document and id added to DB if quotation id and doc exists
}
if (isset($media_category->term_id))
echo $lastid . "_" . $media_category->term_id;
else
echo $lastid;
}
}
/* 0787---8/5/2015----- function to save application form details into the database */
add_action('wp_ajax_application-form', 'validate_payment');
function validate_payment()
{
postRequest($_POST);
}
add_action('admin_post_application-form', 'application_form_save_action'); // If the user is logged in
function application_form_save_action() {
$pageCode = 'application-form-register';
global $wpdb;
$json = json_encode($_POST['data']);
$company_id = get_company_id();
$renewal_options = '';
if ($_POST['editid'] != 0) {
$log_sql_crm = 'select id,content from ' . $wpdb->prefix . 'logger where LOWER(ref_type) = "application" and ref_id = "' . $_POST['editid'] . '" and UPPER(type) = "CRM" order by id desc limit 0,1';
$log_details_crm = $wpdb->get_results($log_sql_crm);
if ($log_details_crm) {
$pre_log_content = json_decode($log_details_crm[0]->content);
$new_log_content = json_decode($json);
$diff_result = get_json_diff($pre_log_content, $new_log_content, 'ARRAY');
}
$log_sql_portal_doc = 'select id,content from ' . $wpdb->prefix . 'logger where LOWER(ref_type) = "application_docs" and ref_id = "' . $_POST['editid'] . '" and UPPER(type) = "PORTAL" order by id desc limit 0,1;';
$log_sql_crm_doc = 'select id,content from ' . $wpdb->prefix . 'logger where LOWER(ref_type) = "application_docs" and ref_id = "' . $_POST['editid'] . '" and UPPER(type) = "CRM" order by id desc limit 0,1;';
$log_details_portal_doc = $wpdb->get_results($log_sql_portal_doc);
$log_details_crm_doc = $wpdb->get_results($log_sql_crm_doc);
if ($log_details_portal_doc && $log_details_crm_doc) {
$pre_log_content_crm = json_decode($log_details_crm_doc[0]->content);
$new_log_content_portal = json_decode($log_details_portal_doc[0]->content);
$diff_result_doc = get_json_diff($pre_log_content_crm, $new_log_content_portal, 'ARRAY');
}
// echo "portal <pre>";
// print_r($log_sql_portal_doc);
// echo "</pre>";
// echo "New <pre>";
// print_r($log_sql_crm_doc);
// echo "</pre>";
// die;
// echo "<pre>";print_r($diff_result);print_r($diff_result_doc);die;
$sql_data = $wpdb->query('select crm_id from ' . $wpdb->prefix . 'application_data where crm_id is not NULL AND id=' . $_POST['editid']);
if ($wpdb->num_rows == 1) {
if((isset($diff_result) && $diff_result !=NULL) || (isset($diff_result_doc) && $diff_result_doc !=NULL)){
$wpdb->update($wpdb->prefix . 'application_data', array('application_data' => $json, 'status' => 'Modified'), array('id' => $_POST['editid']));
//Send Modified Application notification to Staff
$get_current_user_role_new = get_current_user_role();
$bnfw = BNFW::factory();
if (strtolower($get_current_user_role_new) != 'staff') {
$sql = 'SELECT * FROM ' . $wpdb->prefix . 'users where ID=' . get_current_user_id();
$result_user = $wpdb->get_results($sql);
$link_to_users = get_permalink(get_page_by_path('listings')) . '?page=create-form-register&id=' . base64_encode($_POST['editid']) . '&view=true';
if ($bnfw->notifier->notification_exists('modified-application-staff')) {
$notifications = $bnfw->notifier->get_notifications('modified-application-staff');
foreach ($notifications as $notification) {
$setting = $bnfw->notifier->read_settings($notification->ID);
foreach ($setting['users'] as $users_role) {
$main_role = strtolower(str_replace('role-', '', $users_role));
$sql = "select `user_email`,`display_name` from " . $wpdb->prefix . "users where `user_type` = '" . $main_role . "'";
$staff_user = $wpdb->get_results($sql);
foreach ($staff_user as $staff_user) {
$emailstaff = $staff_user->user_email;
$display_name = (isset($staff_user->display_name))?$staff_user->display_name:'';
$usernamestaff = ucwords($display_name);
$subjectstaff = $setting['subject'];
$date = date('Y-m-d');
$messagestaff = $setting['message'];
$messagestaff = str_replace('[firstname]', $result_user[0]->first_name, $messagestaff);
$messagestaff = str_replace('[lastname]', $result_user[0]->last_name, $messagestaff);
$messagestaff = str_replace('[username]', $usernamestaff, $messagestaff);
$messagestaff = str_replace('[date]', $date, $messagestaff);
$messagestaff = str_replace('[linkToApplicationView]', $link_to_users, $messagestaff);
$subjectstaff = str_replace('[firstname]', $result_user[0]->first_name, $subjectstaff);
$subjectstaff = str_replace('[lastname]', $result_user[0]->last_name, $subjectstaff);
wp_mail($emailstaff, $subjectstaff, wpautop($messagestaff));
}
}
}
}
}
$log_data = array(); //Declare array to stored log data
$log_data['ref_type'] = "Application";
$log_data['ref_id'] = $_POST['editid'];
$log_data['title'] = "Existing Application Updated";
$log_data['description'] = "Application Updated From Portal";
$log_data['content'] = $json;
//Used to log activity
IB_Logging::ib_log_activity($log_data);
}
/* assign roles change via application edit/submit */
/* assign roles change via application edit/submit */
if (isset($_POST['data']['new_application']['_linked']['contact']['new_technicalcontactid'])) {
$technical = $_POST['data']['new_application']['_linked']['contact']['new_technicalcontactid'];
} else {
$technical = '';
}
if (isset($_POST['data']['new_application']['_linked']['contact']['new_billingcontact'])) {
$billing = $_POST['data']['new_application']['_linked']['contact']['new_billingcontact'];
} else {
$billing = '';
}
if (isset($_POST['data']['new_application']['_linked']['contact']['new_legalcontactid'])) {
$legal = $_POST['data']['new_application']['_linked']['contact']['new_legalcontactid'];
} else {
$legal = '';
}
if (isset($_POST['data']['new_application']['_linked']['contact']['new_chiefadminofficerid'])) {
$chief = $_POST['data']['new_application']['_linked']['contact']['new_chiefadminofficerid'];
} else {
$chief = '';
}
$edit_id = $_POST['editid'];
change_user_roles_from_application($technical, $billing, $legal, $edit_id);
set_site_message($pageCode, 'success', "Application form successfully updated");
//$_SESSION['wp_notices']['status'] = "Application form successfully updated";
} else {
$sql_data = $wpdb->query('select status from ' . $wpdb->prefix . 'application_data where status = "Draft" AND id=' . $_POST['editid']);
if ($wpdb->num_rows == 1) {
$bnfw = BNFW::factory();
//if ($bnfw->notifier->notification_exists('new-application')) {
//$notifications = $bnfw->notifier->get_notifications('new-application');
//foreach ($notifications as $notification) {
// $bnfw->engine->send_new_application_email($bnfw->notifier->read_settings($notification->ID), $user);
//}
//}
set_site_message($pageCode, 'success', "Application form successfully submitted");
//Send New Application notification to Customer
if ($bnfw->notifier->notification_exists('new-application')) {
$notifications_staff = $bnfw->notifier->get_notifications('new-application');
foreach ($notifications_staff as $notification_staff) {
$setting_staff = $bnfw->notifier->read_settings($notification_staff->ID);
$sql = "select `salutaions`,`display_name`,`user_email`,`first_name` from " . $wpdb->prefix . "users where `ID` = '" . get_current_user_id() . "'";
$staff_user = $wpdb->get_row($sql);
$emailstaff = $staff_user->user_email;
$firstname = ucfirst($staff_user->first_name);
$subjectcustomer = $setting_staff['subject'];
$date = date('Y-m-d');
$messagecustomer = $setting_staff['message'];
$messagecustomer = str_replace('[saluirstnametations]', $staff_user->salutaions, $messagecustomer);
$messagecustomer = str_replace('[username]', $staff_user->display_name, $messagecustomer);
$messagecustomer = str_replace('[firstname]', $firstname, $messagecustomer);
$messagecustomer = str_replace('[date]', $date, $messagecustomer);
wp_mail($emailstaff, $subjectcustomer, wpautop($messagecustomer));
}
}
//Send New Application notification to contact
if ($bnfw->notifier->notification_exists('new-application-assoc-contact')) {
$notifications_staff = $bnfw->notifier->get_notifications('new-application-assoc-contact');
foreach ($notifications_staff as $notification_staff) {
$setting_staff = $bnfw->notifier->read_settings($notification_staff->ID);
$assoc_user_sql = "select " . $wpdb->prefix . "users.`salutaions`," . $wpdb->prefix . "application_user_roles.`roles`," . $wpdb->prefix . "users.`display_name`," . $wpdb->prefix . "users.`first_name`," . $wpdb->prefix . "users.`user_email` from " . $wpdb->prefix . "users join " . $wpdb->prefix . "application_user_roles on " . $wpdb->prefix . "application_user_roles.user_id = " . $wpdb->prefix . "users.ID where " . $wpdb->prefix . "application_user_roles.`application_id` = '" . $_POST['editid'] . "'";
$assoc_user = $wpdb->get_row($assoc_user_sql);
$emailuser = $assoc_user->user_email;
$roles_arr = unserialize($assoc_user->roles);
$key_arr = array_keys($roles_arr[0]);
$verb = (count($key_arr)==1)?"is":"are";
$key_arr=array_map(function($word) { return ucfirst($word); }, $key_arr);
$roles_str = join(' and ', array_filter(array_merge(array(join(', ', array_slice($key_arr, 0, -1))), array_slice($key_arr, -1))));
//$roles_str = implode(", ",$key_arr);
$emailassocuser = $assoc_user->user_email;
$subjectcustomer = $setting_staff['subject'];
$date = date('Y-m-d');
$messagecustomer = $setting_staff['message'];
$messagecustomer = str_replace('[salutations]', $assoc_user->salutaions, $messagecustomer);
$messagecustomer = str_replace('[firstname]', ucfirst($assoc_user->first_name), $messagecustomer);
$messagecustomer = str_replace('[currentusername]', $assoc_user->display_name, $messagecustomer);
$messagecustomer = str_replace('[username]', $staff_user->display_name, $messagecustomer);
$messagecustomer = str_replace('[verb]', $verb, $messagecustomer);
$messagecustomer = str_replace('[roles]', $roles_str, $messagecustomer);
$messagecustomer = str_replace('[date]', $date, $messagecustomer);
wp_mail($emailassocuser, $subjectcustomer, wpautop($messagecustomer));
}
}
}
// else {
// set_site_message($pageCode, 'success', "Application form successfully updated");
// }
if( (isset($_POST['renewal_year'])) && (!empty($_POST['renewal_year'])) ) {
//Calculate and store application renewal fields
$renewal_period = intval($_POST['renewal_year']);
//Get initial application for getting renewal options set by staff
$initial_application_data = $wpdb->get_row("SELECT id, renewal_options, application_exp_date from " . $wpdb->prefix . "application_data WHERE new_application_id=" . $_POST['editid']);
if( (isset($initial_application_data)) && (!empty($initial_application_data->renewal_options)) ) {
$renewal_options = json_decode($initial_application_data->renewal_options, true);
$renewal_app_exp_date = '';
$renewal_app_start_date = null;
//Calculate renewed application's expiry date based customer selection for renewal period.
if(isset($initial_application_data->application_exp_date) && (!empty($initial_application_data->application_exp_date))) {
$app_exp_date = new DateTime(date("Y-m-d H:i:s" , strtotime($initial_application_data->application_exp_date)));
$old_app_exp_date = clone $app_exp_date;
$modifier= '+'.$renewal_period.' years';
$app_exp_date->modify($modifier);
while($app_exp_date->format('m')!=$old_app_exp_date->format('m')) {
$app_exp_date->modify('-1 day');
}
$renewal_app_exp_date = $app_exp_date->format('Y-m-d H:i:s');
$old_app_exp_date->modify('+1 day');
$renewal_app_start_date = $old_app_exp_date->format('Y-m-d H:i:s');
}
$renewal_options['new_application_start_date'] = $renewal_app_start_date;
$renewal_options['new_application_exp_date'] = $renewal_app_exp_date;
$renewal_options['renewal_year'] = $renewal_period;
//Update renewal_options in initial application to store renewed details in the renewal_options field.
$wpdb->update($wpdb->prefix . 'application_data', array('renewal_options' => json_encode($renewal_options)), array('id' => $initial_application_data->id));
//Update and set new application expiry date for renewed application based on renewal period.
$wpdb->update($wpdb->prefix . 'application_data', array('application_exp_date' => $renewal_app_exp_date), array('id' => $_POST['editid']));
//Update and set initial application is_renewed to true
$wpdb->query("UPDATE " . $wpdb->prefix . "application_renewal_notification SET `is_renewed` = '1' WHERE application_id = " . $initial_application_data->id);
}
}
$wpdb->update($wpdb->prefix . 'application_data', array('application_data' => $json, 'status' => 'New', 'renewal_options' => $renewal_options), array('id' => $_POST['editid']));
$log_data = array(); //Declare array to stored log data
$log_data['ref_type'] = "Application";
$log_data['ref_id'] = $_POST['editid'];
$log_data['title'] = "Existing Application Updated";
$log_data['description'] = "Application Updated From Portal";
$log_data['content'] = $json;
//Used to log activity
IB_Logging::ib_log_activity($log_data);
/* assign roles change via application edit/submit */
/* assign roles change via application edit/submit */
if (isset($_POST['data']['new_application']['_linked']['contact']['new_technicalcontactid'])) {
$technical = $_POST['data']['new_application']['_linked']['contact']['new_technicalcontactid'];
} else {
$technical = '';
}
if (isset($_POST['data']['new_application']['_linked']['contact']['new_billingcontact'])) {
$billing = $_POST['data']['new_application']['_linked']['contact']['new_billingcontact'];
} else {
$billing = '';
}
if (isset($_POST['data']['new_application']['_linked']['contact']['new_legalcontactid'])) {
$legal = $_POST['data']['new_application']['_linked']['contact']['new_legalcontactid'];
} else {
$legal = '';
}
if (isset($_POST['data']['new_application']['_linked']['contact']['new_chiefadminofficerid'])) {
$chief = $_POST['data']['new_application']['_linked']['contact']['new_chiefadminofficerid'];
} else {
$chief = '';
}
$edit_id = $_POST['editid'];
change_user_roles_from_application($technical, $billing, $legal, $chief, $edit_id);
//set_site_message($pageCode, 'success', "Application form successfully updated");
}
$user = isset($user)?$user:wp_get_current_user();
//Send New Application notification to Staff
$bnfw = BNFW::factory();
if ($bnfw->notifier->notification_exists('new-application')) {
$notifications = $bnfw->notifier->get_notifications('new-application');
foreach ($notifications as $notification) {
$bnfw->engine->send_new_application_email($bnfw->notifier->read_settings($notification->ID), $user);
}
}
$sql_check_status = 'select `status` from ' . $wpdb->prefix . 'application_data where id=' . $_POST['editid'];
$result_status_check = $wpdb->get_row($sql_check_status);
//Send New Application notification to Staff
if($result_status_check->status !='Modified'){
$user_id_staff_mail = get_current_user_id();
$user_data_staff_mail = get_userdata($user_id_staff_mail);
$firstname_staff = $user_data_staff_mail->data->first_name;
$lastname_staff = $user_data_staff_mail->data->last_name;
$url_staff = '' . site_url() . '/index.php/listings/?page=create-form-register&id=' . base64_encode($_POST['editid']) . '&view=true';
if ($bnfw->notifier->notification_exists('new-application-staff')) {
$notifications_staff = $bnfw->notifier->get_notifications('new-application-staff');
foreach ($notifications_staff as $notification_staff) {
$setting_staff = $bnfw->notifier->read_settings($notification_staff->ID);
$sql_data = 'select `program_id`,`id` from ' . $wpdb->prefix . 'application_data where id=' . $_POST['editid'];
$program_id = $wpdb->get_row($sql_data);
$sql = "select `user_id` from " . $wpdb->prefix . "program_user_association where `program_id` = " . $program_id->program_id . "";
$user_id = $wpdb->get_results($sql);
$applicationid = $program_id->id;
foreach ($user_id as $user_id) {
$sql = "select `user_email`,`display_name` from " . $wpdb->prefix . "users where `ID` = '" . $user_id->user_id . "'";
$staff_user = $wpdb->get_row($sql);
$emailstaff = $staff_user->user_email;
$username_staff = ucwords($staff_user->display_name);
$subjectstaff = $setting_staff['subject'];
$date = date('Y-m-d');
$messagestaff = $setting_staff['message'];
$messagestaff = str_replace('[firstname]', $firstname_staff, $messagestaff);
$messagestaff = str_replace('[applicationid]', $applicationid, $messagestaff);
$messagestaff = str_replace('[lastname]', $lastname_staff, $messagestaff);
$messagestaff = str_replace('[date]', $date, $messagestaff);
$messagestaff = str_replace('[linkToApplicationView]', $url_staff, $messagestaff);
$messagestaff = str_replace('[username]', $username_staff, $messagestaff);
$subjectstaff = str_replace('[firstname]', $firstname_staff, $subjectstaff);
$subjectstaff = str_replace('[lastname]', $lastname_staff, $subjectstaff);
wp_mail($emailstaff, $subjectstaff, wpautop($messagestaff));
}
}
}
}
}
// application / quotation payment
if( isset( $_POST['payment_mode'] ) ){
$boolPaymentStatus = paymentTransaction( $_POST['chasePaymentAmount'], 'application', $_POST['editid'] );
if( true == $boolPaymentStatus )
{
unset( $_SESSION['wp_success'][$pageCode]);
set_site_message($pageCode, 'success', "Application payment done successfully.");
wp_redirect(site_url() . '/index.php/listings/?page=application-form-register&view=approved&paged=1');
}
}
wp_redirect(site_url() . '/index.php/listings/?page=application-form-register&view=approved&paged=1');
//wp_redirect(add_query_arg(array('page' => 'application-form-register', 'action' => 'InvalidData'), admin_url()));
}
/* 0787---12/5/2015----- function to delete application */
add_action('admin_post_delete-application-form', 'delete_application_form');
function delete_application_form() {
$pageCode = 'application-form-register';
global $wpdb;
if (isset($_GET['id'])) {
// Send email to customer and staff if any application is deleted
$getApplicationData = $wpdb->get_row( 'SELECT * FROM '.$wpdb->prefix.'application_data WHERE id='.base64_decode( $_GET['id'] ) );
if( isset( $getApplicationData ) && $getApplicationData->status == 'New' ) {
$arrStaffIds = array();
$bnfw = BNFW::factory();
$notification_name = 'application-deletion-by-customer';
if($bnfw->notifier->notification_exists($notification_name)) {
//Prepare data which will be sent in the mail for users
$notifications = $bnfw->notifier->get_notifications($notification_name);
$user = get_userdata( get_current_user_id() );
//Send renewal email notification according to user_type
if(!empty($notifications)) {
$notification = $notifications[0];
$setting = $bnfw->notifier->read_settings($notification->ID);
// send mail to customer
$arrEmailStuff = deleteApplicationMailContent( $user->first_name . ' ' . $user->last_name, $setting, $getApplicationData->id, $getApplicationData->application_name );
$strEmailSubject = $arrEmailStuff[0];
$strEmailContent = $arrEmailStuff[1];
wp_mail($user->user_email, $strEmailSubject, wpautop( $strEmailContent ) );
// send mail to all staff members who are associated with the application program
$user_ids = $wpdb->get_results("SELECT `user_id` FROM " . $wpdb->prefix . "program_user_association WHERE `program_id` = ".$getApplicationData->program_id);
if(!empty($user_ids)) {
foreach ($user_ids as $user_id) {
$arrStaffIds[] = $user_id->user_id;
}
$email_to_user = $wpdb->get_results("SELECT user_email, first_name, last_name FROM " . $wpdb->prefix . "users WHERE ID IN(" . implode( ',', $arrStaffIds ) . " )" );
foreach ($email_to_user as $user_id) {
$arrEmailStuff = deleteApplicationMailContent( $user_id->first_name . ' ' . $user_id->last_name, $setting, $getApplicationData->id, $getApplicationData->application_name );
$strEmailSubject = $arrEmailStuff[0];
$strEmailContent = $arrEmailStuff[1];
wp_mail($user_id->user_email, $strEmailSubject, wpautop( $strEmailContent ) );
}
}
}
}
}
$wpdb->update($wpdb->prefix . 'application_data', array('status' => 'delete', 'deleted_by' => get_current_user_id(), 'deleted_on' => current_time('mysql', 1)), array('id' => base64_decode($_GET['id'])));
$sql = 'select * from '.$wpdb->prefix.'application_docs where application_id='.base64_decode($_GET['id']);
$result = $wpdb->get_results($sql);
foreach ($result as $key => $value) {
remove_application_documents($value->id,$value->application_id,$value->doc_id,$value->doc_category);
}
}
set_site_message($pageCode, 'success', "Application form successfully deleted");
//$_SESSION['wp_errors']['status'] = "Application form successfully deleted";
wp_redirect(site_url() . '/index.php/listings/?page=application-form-register&view=approved');
//wp_redirect(add_query_arg(array('page' => 'application-form-register', 'action' => 'InvalidData'), admin_url()));
}
function deleteApplicationMailContent( $strUserName, $arrEmailSetting, $intApplicationId, $strApplicationName ) {
$strEmailSubject = str_replace( '[firstname] [lastname]',$strUserName, $arrEmailSetting['subject'] );
$strEmailContent = $arrEmailSetting['message'];
$strEmailContent = str_replace('[username]', $strUserName, $strEmailContent);
$strEmailContent = str_replace('[application-id]', $intApplicationId, $strEmailContent);
$strEmailContent = str_replace('[application-name]', $strApplicationName, $strEmailContent);
return array( $strEmailSubject, $strEmailContent );
}
/* 0787 - 08/06/15 - function to delete contact users added by super user */
add_action('admin_post_delete-contact-user', 'delete_contact_user');
function delete_contact_user() {
//$_SESSION['wp_page'] = $_GET['page'];
// wp_delete_user($_GET['hash']);
global $wpdb;
if (isset($_GET['id'])) {
// $wpdb->query("update " . $wpdb->prefix . "users set deleted_by = '" . get_current_user_id() . "',deleted_on = '" . date("Y-m-d H:m:s") . "' where ID=" . $_GET['id'] . "");
$wpdb->query("delete from " . $wpdb->prefix . "users where ID=" . $_GET['id'] . "");
}
if (isset($_GET['page'])) {
if (isset($_GET['redirect_url']) && !empty($_GET['redirect_url'])) {
set_site_message('my-contacts', 'success', "Contact Deleted Successfully");
//$_SESSION['wp_notices']['user_deleted'] = __("Contact Deleted Successfully");
wp_redirect(base64_decode($_GET['redirect_url']) . '?page=' . $_GET['page']);
} else {
set_site_message('my-contacts', 'success', "Contact Deleted Successfully");
//$_SESSION['wp_notices']['user_deleted'] = __("Contact Deleted Successfully");
wp_redirect(admin_url() . 'admin.php?page=' . $_GET['page']);
}
} else {
user_form();
}
}
/* 0787---8/5/2015----- function to get Company id */
function get_company_id() {
global $wpdb;
$user_id = get_current_user_id();
$user = get_user_by('id', $user_id);
if (isset($user->company_id) && $user->company_id != '') {
return $user->company_id;
}
}
add_shortcode('view-application-form', 'view_application_form');
/* function for view application form */
function view_application_form() {
include('view-application-form.php');
}
/* function for view all applications */
function view_all_applications() {
wp_enqueue_style('theme-css', get_bloginfo('template_directory') . '/core/css/ib-custom.css');
include('view-all-applications.php');
}
function view_templates() {
include('templates.php');
}
/* 0846 - 16/06/15 - function to show manage roles template page */
function manage_templates_roles() {
include('manage_templates_role.php');
}
function application_documents() {
include('templates/upload-application-document.php');
}
/* 0846 - 08/06/15 - all companies */
function all_companies() {
include('all_companies.php');
}
/* 0787 - 11/08/15 - function to show Renewal Notification */
function renewal_notification() {
include('renewal_notification.php');
}
add_action('wp_enqueue_scripts', 'include_application_form_scripts');
add_action('admin_enqueue_scripts', 'include_application_form_scripts');
/* 0846 - 08/06/15 - function to add scripts and style sheets on the plugin pages */
function include_application_form_scripts() {
/* all srcipts */
wp_enqueue_script('jquery-js', plugin_dir_url(__FILE__) . 'js/jquery-1.11.1.min.js', array(), '1.0.0', false);
/* page where we want sorting,seraching,pagination add below condition */
/* datatables js and css */
wp_enqueue_script('jquery-steps-js', plugin_dir_url(__FILE__) . 'js/jquery.steps.js', array('jquery'), '1.0.0', false);
wp_enqueue_script('jquery-datatable-min', plugin_dir_url(__FILE__) . 'js/jquery.dataTables.min.js', array('jquery'), '1.0.0', false);
wp_enqueue_script('datatable-responsive', plugin_dir_url(__FILE__) . 'js/dataTables.responsive.min.js', array('jquery'), '1.0.0', false);
wp_enqueue_script('datatable-bootstrap', plugin_dir_url(__FILE__) . 'js/dataTables.bootstrap.js', array('jquery'), '1.0.0', false);
if ( isset($_GET['page']) ) {
wp_enqueue_style('bootstrap-css', plugin_dir_url(__FILE__) . 'css/bootstrap.min.css');
}
wp_enqueue_style('datatable-bootstrap-css', plugin_dir_url(__FILE__) . 'css/dataTables.bootstrap.css');
wp_enqueue_style('application-css', plugin_dir_url(__FILE__) . 'css/application-form.css');
wp_enqueue_style('jquery-steps-css', plugin_dir_url(__FILE__) . 'css/jquery.steps.css');
wp_enqueue_style('datatable-responsive-css', plugin_dir_url(__FILE__) . 'css/responsive.dataTables.min.css');
wp_enqueue_script('general', plugin_dir_url(__FILE__) . 'js/general.js', array('jquery', 'jquery-ui-js', 'switchery-js'), '1.0.0', false);
wp_enqueue_script('switchery-js', plugin_dir_url(__FILE__) . 'js/switchery.min.js', array(), '1.0.0', false);
wp_enqueue_script('upload-file-js', plugin_dir_url(__FILE__) . 'js/jquery.uploadfile.min.js', array('jquery'), '1.0.0', false);
wp_enqueue_script('jquery-ui-js', plugin_dir_url(__FILE__) . 'js/jquery-ui.js', array('jquery'), '1.0.0', false);
}
/* 0846 - 08/06/15 - function to save user list from application form */
add_action('admin_post_user-form', 'user_form_save_action'); // If the user is logged in
function user_form_save_action() {
//$_SESSION['wp_page'] = $_GET['page'];
$current_user = wp_get_current_user();
$user_name = isset($_POST['name'])?$_POST['name']:'';
$user_email = (isset($_POST['email']) && (!empty($_POST['email']))) ? $_POST['email'] : "" ;
global $wpdb;
$error = '';
$table_name = $wpdb->prefix . 'users';
$deactive_user_id = $wpdb->get_var('SELECT ID from '.$table_name.' where user_email="'.$_POST['email'].'" AND user_status=-1');
if($deactive_user_id!='')
{
if (isset($_GET['page']) && $_GET['page'] != "") {
if (isset($_POST['redirect_url']) && !empty($_POST['redirect_url'])) {
if ($error !== '') {
//$_SESSION['wp_page'] = $_GET['page'];
//$_SESSION['wp_errors']['my-contacts']['user_exists'] = $error;
}
set_site_message('my-contacts', 'success', "Contact Added Successfully");
if(active_user($user_email)){
wp_redirect(base64_decode($_POST['redirect_url']) . '?page=' . $_GET['page']);
}
} else {
wp_redirect(admin_url() . "admin.php?page=" . $_GET['page']);
}
} else {
$success = "Contact Added Successfully";
$error = '';
if(active_user($user_email)){
user_form($error, $success, $deactive_user_id, $_POST['label']);
}
}
die;
}
$is_update = false;
if ($_POST['id'] == 0) {
remove_filter('profile_update', 'numediaweb_custom_user_profile_fields', 20);
if (!email_exists($user_email)) {
$random_password = wp_generate_password($length = 12, $include_standard_special_chars = false);
/* 0846 - 08/06/15 - in place of user_name in below function we take user_email because we change the functionality to take user_name as user_email */
$user_id = @wp_create_user($user_email, $random_password, $user_email);
//$user_id = wp_update_user(array('first_name' => $_POST['fname'], 'last_name' => $_POST['lname'], 'ID' => $user_id));
$user = get_user_by('id', $user_id);
$wpdb->query("update " . $wpdb->prefix . "users set display_name = '" . $_POST['fname'] . " " . $_POST['lname'] . "', first_name = '" . $_POST['fname'] . "',last_name='" . $_POST['lname'] . "',salutaions='" . $_POST['salutaions'] . "', phone = '" . $_POST['phone'] . "', country=" . $_POST['country'] . ",state=" . $_POST['state'] . ",city='" . $_POST['city'] . "', title = '" . $_POST['title'] . "', fax = '" . $_POST['fax'] . "', address = '" . $_POST['address'] . "', created_by = " . get_current_user_id() . ", created_on = '" . date("Y-m-d H:m:s") . "', company_id = '" . $_POST['company_id'] . "' where ID=" . $user_id);
$get_current_user_role_new = get_current_user_role();
$bnfw = BNFW::factory();
if (strtolower($get_current_user_role_new) != 'staff') {
$sql = 'SELECT * FROM ' . $wpdb->prefix . 'users where ID=' . get_current_user_id();
$result_user = $wpdb->get_results($sql);
$link_to_users = get_permalink(get_page_by_path('listings')) . '?page=my-contacts';
if ($bnfw->notifier->notification_exists('new-contact')) {
$notifications = $bnfw->notifier->get_notifications('new-contact');
foreach ($notifications as $notification) {
$setting = $bnfw->notifier->read_settings($notification->ID);
foreach ($setting['users'] as $users_role) {
$main_role = strtolower(str_replace('role-', '', $users_role));
$sql = "select `user_email` from " . $wpdb->prefix . "users where `user_type` = '" . $main_role . "'";
$staff_user = $wpdb->get_results($sql);
foreach ($staff_user as $staff_user) {
$emailstaff = $staff_user->user_email;
$display_name = (isset($staff_user->display_name))?$staff_user->display_name:'';
$usernamestaff = ucwords($display_name);
$subjectstaff = $setting['subject'];
$date = date('Y-m-d');
$messagestaff = $setting['message'];
$messagestaff = str_replace('[firstuser]', $_POST['fname'], $messagestaff);
$messagestaff = str_replace('[lastuser]', $_POST['lname'], $messagestaff);
$messagestaff = str_replace('[firstname]', $result_user[0]->first_name, $messagestaff);
$messagestaff = str_replace('[lastname]', $result_user[0]->last_name, $messagestaff);
$messagestaff = str_replace('[username]', $usernamestaff, $messagestaff);
$messagestaff = str_replace('[date]', $date, $messagestaff);
$messagestaff = str_replace('[linkToUser]', $link_to_users, $messagestaff);
$subjectstaff = str_replace('[firstname]', $result_user[0]->first_name, $subjectstaff);
$subjectstaff = str_replace('[lastname]', $result_user[0]->last_name, $subjectstaff);
wp_mail($emailstaff, $subjectstaff, wpautop($messagestaff));
}
}
}
}
}
if ($bnfw->notifier->notification_exists('new-contact-to-customer')) {
$notifications = $bnfw->notifier->get_notifications('new-contact-to-customer');
foreach ($notifications as $notification) {
$setting = $bnfw->notifier->read_settings($notification->ID);
$emailcustomer = $current_user->user_email;
$subjectcustomer = $setting['subject'];
$date = date('Y-m-d');
$messagecustomer = $setting['message'];
$messagecustomer = str_replace('[firstuser]', $_POST['fname'], $messagecustomer);
$messagecustomer = str_replace('[lastuser]', $_POST['lname'], $messagecustomer);
$messagecustomer = str_replace('[firstname]', ucfirst($result_user[0]->first_name), $messagecustomer);
$messagecustomer = str_replace('[lastname]', $result_user[0]->last_name, $messagecustomer);
$messagecustomer = str_replace('[date]', $date, $messagecustomer);
$messagecustomer = str_replace('[linkToUser]', $link_to_users, $messagecustomer);
$subjectcustomer = str_replace('[firstname]', $result_user[0]->first_name, $subjectcustomer);
$subjectcustomer = str_replace('[lastname]', $result_user[0]->last_name, $subjectcustomer);
wp_mail($emailcustomer, $subjectcustomer, wpautop($messagecustomer));
}
}
$error = '';
$success = 'User added successfully';
if (isset($_GET['page']) && $_GET['page'] != "") {
set_site_message('my-contacts', 'success', "Contact Added Successfully");
}
//$_SESSION['wp_notices']['user_added'] = __('Contact added successfully.');
//$_SESSION['wp_page'] = $_GET['page'];
} else {
if(isset($_POST['label']) && (!empty($_POST['label'])) && ($_POST['label']=='applicantbillingid')) {
$user = get_user_by( 'email', $_POST['email']);
$is_billing_exist_for_application = $wpdb->get_row("select * from ".$wpdb->prefix."application_user_roles where user_id = ".$user->data->ID." and application_id = ".$_POST['application_id']);
if(empty($is_billing_exist_for_application)) {
if(!empty($user)) {
$wpdb->insert($wpdb->prefix.'application_user_roles',array('user_id'=>$user->data->ID,'application_id'=>$_POST['application_id'],'roles'=>serialize(array(array("billing"=>1)))));
}
$error = '';
$success = 'User added successfully';
user_form($error, $success, $user->data->ID, $_POST['label']);
$user_id=$user->data->ID;