1: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2:
3: 4: 5:
6: class Backend_api extends CI_Controller {
7: private $privileges;
8:
9: public function __construct() {
10: parent::__construct();
11:
12: $this->load->library('session');
13: $this->load->model('roles_model');
14: $this->privileges = $this->roles_model->get_privileges($this->session->userdata('role_slug'));
15: }
16:
17:
18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28:
29: public function ajax_get_calendar_appointments() {
30: try {
31: if ($this->privileges[PRIV_APPOINTMENTS]['view'] == FALSE) {
32: throw new Exception('You do not have the required privileges for this task.');
33: }
34:
35: $this->load->model('appointments_model');
36: $this->load->model('providers_model');
37: $this->load->model('services_model');
38: $this->load->model('customers_model');
39:
40: if ($_POST['filter_type'] == FILTER_TYPE_PROVIDER) {
41: $where_id = 'id_users_provider';
42: } else {
43: $where_id = 'id_services';
44: }
45:
46:
47: $where_clause = array(
48: $where_id => $_POST['record_id'],
49: 'start_datetime >=' => $_POST['start_date'],
50: 'end_datetime <=' => $_POST['end_date'],
51: 'is_unavailable' => FALSE
52: );
53:
54: $response['appointments'] = $this->appointments_model->get_batch($where_clause);
55:
56: foreach($response['appointments'] as &$appointment) {
57: $appointment['provider'] = $this->providers_model->get_row($appointment['id_users_provider']);
58: $appointment['service'] = $this->services_model->get_row($appointment['id_services']);
59: $appointment['customer'] = $this->customers_model->get_row($appointment['id_users_customer']);
60: }
61:
62:
63: if ($_POST['filter_type'] == FILTER_TYPE_PROVIDER) {
64: $where_clause = array(
65: $where_id => $_POST['record_id'],
66: 'start_datetime >=' => $_POST['start_date'],
67: 'end_datetime <=' => $_POST['end_date'],
68: 'is_unavailable' => TRUE
69: );
70:
71: $response['unavailables'] = $this->appointments_model->get_batch($where_clause);
72: }
73:
74: echo json_encode($response);
75:
76: } catch(Exception $exc) {
77: echo json_encode(array(
78: 'exceptions' => array(exceptionToJavaScript($exc))
79: ));
80: }
81: }
82:
83: 84: 85: 86: 87: 88: 89: 90: 91:
92: public function ajax_save_appointment() {
93: try {
94: $this->load->model('appointments_model');
95: $this->load->model('providers_model');
96: $this->load->model('services_model');
97: $this->load->model('customers_model');
98: $this->load->model('settings_model');
99:
100:
101: if (isset($_POST['customer_data'])) {
102: $customer = json_decode(stripcslashes($_POST['customer_data']), true);
103:
104: $REQUIRED_PRIV = (!isset($customer['id']))
105: ? $this->privileges[PRIV_CUSTOMERS]['add']
106: : $this->privileges[PRIV_CUSTOMERS]['edit'];
107: if ($REQUIRED_PRIV == FALSE) {
108: throw new Exception('You do not have the required privileges for this task.');
109: }
110:
111: $customer['id'] = $this->customers_model->add($customer);
112: }
113:
114:
115: if (isset($_POST['appointment_data'])) {
116: $appointment = json_decode(stripcslashes($_POST['appointment_data']), true);
117:
118: $REQUIRED_PRIV = (!isset($appointment['id']))
119: ? $this->privileges[PRIV_APPOINTMENTS]['add']
120: : $this->privileges[PRIV_APPOINTMENTS]['edit'];
121: if ($REQUIRED_PRIV == FALSE) {
122: throw new Exception('You do not have the required privileges for this task.');
123: }
124:
125: $manage_mode = isset($appointment['id']);
126:
127:
128: if (!isset($appointment['id_users_customer'])) {
129: $appointment['id_users_customer'] = $customer['id'];
130: }
131:
132: $appointment['id'] = $this->appointments_model->add($appointment);
133: }
134:
135: $appointment = $this->appointments_model->get_row($appointment['id']);
136: $provider = $this->providers_model->get_row($appointment['id_users_provider']);
137: $customer = $this->customers_model->get_row($appointment['id_users_customer']);
138: $service = $this->services_model->get_row($appointment['id_services']);
139:
140: $company_settings = array(
141: 'company_name' => $this->settings_model->get_setting('company_name'),
142: 'company_link' => $this->settings_model->get_setting('company_link'),
143: 'company_email' => $this->settings_model->get_setting('company_email')
144: );
145:
146:
147: try {
148: $google_sync = $this->providers_model->get_setting('google_sync',
149: $appointment['id_users_provider']);
150:
151: if ($google_sync == TRUE) {
152: $google_token = json_decode($this->providers_model->get_setting('google_token',
153: $appointment['id_users_provider']));
154:
155: $this->load->library('Google_Sync');
156: $this->google_sync->refresh_token($google_token->refresh_token);
157:
158: if ($appointment['id_google_calendar'] == NULL) {
159: $google_event = $this->google_sync->add_appointment($appointment, $provider,
160: $service, $customer, $company_settings);
161: $appointment['id_google_calendar'] = $google_event->id;
162: $this->appointments_model->add($appointment);
163: } else {
164: $this->google_sync->update_appointment($appointment, $provider,
165: $service, $customer, $company_settings);
166: }
167: }
168: } catch(Exception $exc) {
169: $warnings[] = exceptionToJavaScript($exc);
170: }
171:
172:
173: try {
174: $this->load->library('Notifications');
175:
176: $send_provider = $this->providers_model
177: ->get_setting('notifications', $provider['id']);
178:
179: if (!$manage_mode) {
180: $customer_title = 'Your appointment has been successfully booked!';
181: $customer_message = 'Thank you for arranging an appointment with us. '
182: . 'Below you can see the appointment details. Make changes '
183: . 'by clicking the appointment link.';
184: $customer_link = $this->config->item('base_url') . 'appointments/index/'
185: . $appointment['hash'];
186:
187: $provider_title = 'A new appointment has been added to your plan.';
188: $provider_message = 'You can make changes by clicking the appointment '
189: . 'link below';
190: $provider_link = $this->config->item('base_url') . 'backend/index/'
191: . $appointment['hash'];
192: } else {
193: $customer_title = 'Appointment changes have been successfully saved!';
194: $customer_message = '';
195: $customer_link = $this->config->item('base_url') . 'appointments/index/'
196: . $appointment['hash'];
197:
198: $provider_title = 'Appointment details have changed.';
199: $provider_message = '';
200: $provider_link = $this->config->item('base_url') . 'backend/'
201: . $appointment['hash'];
202: }
203:
204: $this->notifications->send_appointment_details($appointment, $provider,
205: $service, $customer, $company_settings, $customer_title,
206: $customer_message, $customer_link, $customer['email']);
207:
208: if ($send_provider == TRUE) {
209: $this->notifications->send_appointment_details($appointment, $provider,
210: $service, $customer, $company_settings, $provider_title,
211: $provider_message, $provider_link, $provider['email']);
212: }
213:
214: } catch(Exception $exc) {
215: $warnings[] = exceptionToJavaScript($exc);
216: }
217:
218: if (!isset($warnings)) {
219: echo json_encode(AJAX_SUCCESS);
220: } else {
221: echo json_encode(array(
222: 'warnings' => $warnings
223: ));
224: }
225: } catch(Exception $exc) {
226: echo json_encode(array(
227: 'exceptions' => array(exceptionToJavaScript($exc))
228: ));
229: }
230: }
231:
232: 233: 234: 235: 236: 237: 238: 239: 240: 241:
242: public function ajax_delete_appointment() {
243: try {
244: if ($this->privileges[PRIV_APPOINTMENTS]['delete'] == FALSE) {
245: throw new Exception('You do not have the required privileges for this task.');
246: }
247:
248: if (!isset($_POST['appointment_id'])) {
249: throw new Exception('No appointment id provided.');
250: }
251:
252:
253: $this->load->model('appointments_model');
254: $this->load->model('providers_model');
255: $this->load->model('customers_model');
256: $this->load->model('services_model');
257: $this->load->model('settings_model');
258:
259: $appointment = $this->appointments_model->get_row($_POST['appointment_id']);
260: $provider = $this->providers_model->get_row($appointment['id_users_provider']);
261: $customer = $this->customers_model->get_row($appointment['id_users_customer']);
262: $service = $this->services_model->get_row($appointment['id_services']);
263:
264: $company_settings = array(
265: 'company_name' => $this->settings_model->get_setting('company_name'),
266: 'company_email' => $this->settings_model->get_setting('company_email'),
267: 'company_link' => $this->settings_model->get_setting('company_link')
268: );
269:
270:
271: $this->appointments_model->delete($_POST['appointment_id']);
272:
273:
274: if ($appointment['id_google_calendar'] != NULL) {
275: try {
276: $google_sync = $this->providers_model->get_setting('google_sync', $provider['id']);
277:
278: if ($google_sync == TRUE) {
279: $google_token = json_decode($this->providers_model
280: ->get_setting('google_token', $provider['id']));
281: $this->load->library('Google_Sync');
282: $this->google_sync->refresh_token($google_token->refresh_token);
283: $this->google_sync->delete_appointment($appointment['id_google_calendar']);
284: }
285: } catch(Exception $exc) {
286: $warnings[] = exceptionToJavaScript($exc);
287: }
288: }
289:
290:
291: try {
292: $this->load->library('Notifications');
293:
294: $send_provider = $this->providers_model
295: ->get_setting('notifications', $provider['id']);
296:
297: if ($send_provider == TRUE) {
298: $this->notifications->send_delete_appointment($appointment, $provider,
299: $service, $customer, $company_settings, $provider['email'],
300: $_POST['delete_reason']);
301: }
302:
303: $this->notifications->send_delete_appointment($appointment, $provider,
304: $service, $customer, $company_settings, $customer['email'],
305: $_POST['delete_reason']);
306: } catch(Exception $exc) {
307: $warnings[] = exceptionToJavaScript($exc);
308: }
309:
310:
311: if (!isset($warnings)) {
312: echo json_encode(AJAX_SUCCESS);
313: } else {
314: echo json_encode(array(
315: 'warnings' => $warnings
316: ));
317: }
318: } catch(Exception $exc) {
319: echo json_encode(array(
320: 'exceptions' => array(exceptionToJavaScript($exc))
321: ));
322: }
323: }
324:
325: 326: 327: 328: 329: 330: 331: 332: 333:
334: public function ajax_disable_provider_sync() {
335: try {
336: if ($this->privileges[PRIV_USERS]['edit'] == FALSE) {
337: throw new Exception('You do not have the required privileges for this task.');
338: }
339:
340: if (!isset($_POST['provider_id'])) {
341: throw new Exception('Provider id not specified.');
342: }
343:
344: $this->load->model('providers_model');
345: $this->providers_model->set_setting('google_sync', FALSE, $_POST['provider_id']);
346: $this->providers_model->set_setting('google_token', NULL, $_POST['provider_id']);
347:
348: echo json_encode(AJAX_SUCCESS);
349:
350: } catch(Exception $exc) {
351: echo json_encode(array(
352: 'exceptions' => array(exceptionToJavaScript($exc))
353: ));
354: }
355: }
356:
357: 358: 359: 360: 361: 362:
363: public function ajax_filter_customers() {
364: try {
365: if ($this->privileges[PRIV_CUSTOMERS]['view'] == FALSE) {
366: throw new Exception('You do not have the required privileges for this task.');
367: }
368:
369: $this->load->model('appointments_model');
370: $this->load->model('services_model');
371: $this->load->model('providers_model');
372: $this->load->model('customers_model');
373:
374: $key = mysql_real_escape_string($_POST['key']);
375:
376: $where_clause =
377: '(first_name LIKE "%' . $key . '%" OR ' .
378: 'last_name LIKE "%' . $key . '%" OR ' .
379: 'email LIKE "%' . $key . '%" OR ' .
380: 'phone_number LIKE "%' . $key . '%" OR ' .
381: 'address LIKE "%' . $key . '%" OR ' .
382: 'city LIKE "%' . $key . '%" OR ' .
383: 'zip_code LIKE "%' . $key . '%")';
384:
385: $customers = $this->customers_model->get_batch($where_clause);
386:
387: foreach($customers as &$customer) {
388: $appointments = $this->appointments_model
389: ->get_batch(array('id_users_customer' => $customer['id']));
390:
391: foreach($appointments as &$appointment) {
392: $appointment['service'] = $this->services_model
393: ->get_row($appointment['id_services']);
394: $appointment['provider'] = $this->providers_model
395: ->get_row($appointment['id_users_provider']);
396: }
397:
398: $customer['appointments'] = $appointments;
399: }
400:
401: echo json_encode($customers);
402:
403: } catch(Exception $exc) {
404: echo json_encode(array(
405: 'exceptions' => array(exceptionToJavaScript($exc))
406: ));
407: }
408: }
409:
410: 411: 412: 413: 414: 415:
416: public function ajax_save_unavailable() {
417: try {
418:
419: $unavailable = json_decode($_POST['unavailable'], true);
420:
421: $REQUIRED_PRIV = (!isset($unavailable['id']))
422: ? $this->privileges[PRIV_APPOINTMENTS]['add']
423: : $this->privileges[PRIV_APPOINTMENTS]['edit'];
424: if ($REQUIRED_PRIV == FALSE) {
425: throw new Exception('You do not have the required privileges for this task.');
426: }
427:
428: $this->load->model('appointments_model');
429: $this->load->model('providers_model');
430:
431:
432: $unavailable['id'] = $this->appointments_model->add_unavailable($unavailable);
433: $unavailable = $this->appointments_model->get_row($unavailable['id']);
434:
435:
436: try {
437: $google_sync = $this->providers_model->get_setting('google_sync',
438: $unavailable['id_users_provider']);
439:
440: if ($google_sync) {
441: $google_token = json_decode($this->providers_model->get_setting('google_token',
442: $unavailable['id_users_provider']));
443:
444: $this->load->library('google_sync');
445: $this->google_sync->refresh_token($google_token->refresh_token);
446:
447: if ($unavailable['id_google_calendar'] == NULL) {
448: $google_event = $this->google_sync->add_unavailable($unavailable);
449: $unavailable['id_google_calendar'] = $google_event->id;
450: $this->appointments_model->add_unavailable($unavailable);
451: } else {
452: $google_event = $this->google_sync->update_unavailable($unavailable);
453: }
454: }
455: } catch(Exception $exc) {
456: $warnings[] = $exc;
457: }
458:
459: if (isset($warnings)) {
460: echo json_encode(array(
461: 'warnings' => $warnings
462: ));
463: } else {
464: echo json_encode(AJAX_SUCCESS);
465: }
466:
467: } catch(Exception $exc) {
468: echo json_encode(array(
469: 'exceptions' => array(exceptionToJavaScript($exc))
470: ));
471: }
472: }
473:
474: 475: 476: 477: 478:
479: public function ajax_delete_unavailable() {
480: try {
481: if ($this->privileges[PRIV_APPOINTMENTS]['delete'] == FALSE) {
482: throw new Exception('You do not have the required privileges for this task.');
483: }
484:
485: $this->load->model('appointments_model');
486: $this->load->model('providers_model');
487:
488: $unavailable = $this->appointments_model->get_row($_POST['unavailable_id']);
489: $provider = $this->providers_model->get_row($unavailable['id_users_provider']);
490:
491:
492: $this->appointments_model->delete_unavailable($unavailable['id']);
493:
494:
495: try {
496: $google_sync = $this->providers_model->get_setting('google_sync', $provider['id']);
497: if ($google_sync == TRUE) {
498: $google_token = json_decode($this->providers_model->get_setting('google_token', $provider['id']));
499: $this->load->library('google_sync');
500: $this->google_sync->refresh_token($google_token->refresh_token);
501: $this->google_sync->delete_unavailable($unavailable['id_google_calendar']);
502: }
503: } catch(Exception $exc) {
504: $warnings[] = $exc;
505: }
506:
507: if (isset($warnings)) {
508: echo json_encode(array(
509: 'warnings' => $warnings
510: ));
511: } else {
512: echo json_encode(AJAX_SUCCESS);
513: }
514:
515: } catch(Exception $exc) {
516: echo json_encode(array(
517: 'exceptions' => array(exceptionToJavaScript($exc))
518: ));
519: }
520: }
521:
522: 523: 524: 525: 526:
527: public function ajax_save_customer() {
528: try {
529: $this->load->model('customers_model');
530: $customer = json_decode($_POST['customer'], true);
531:
532: $REQUIRED_PRIV = (!isset($customer['id']))
533: ? $this->privileges[PRIV_CUSTOMERS]['add']
534: : $this->privileges[PRIV_CUSTOMERS]['edit'];
535: if ($REQUIRED_PRIV == FALSE) {
536: throw new Exception('You do not have the required privileges for this task.');
537: }
538:
539: $customer_id = $this->customers_model->add($customer);
540: echo json_encode(array(
541: 'status' => AJAX_SUCCESS,
542: 'id' => $customer_id
543: ));
544: } catch(Exception $exc) {
545: echo json_encode(array(
546: 'exceptions' => array(exceptionToJavaScript($exc))
547: ));
548: }
549: }
550:
551: 552: 553: 554: 555:
556: public function ajax_delete_customer() {
557: try {
558: if ($this->privileges[PRIV_CUSTOMERS]['delete'] == FALSE) {
559: throw new Exception('You do not have the required privileges for this task.');
560: }
561:
562: $this->load->model('customers_model');
563: $this->customers_model->delete($_POST['customer_id']);
564: echo json_encode(AJAX_SUCCESS);
565: } catch(Exception $exc) {
566: echo json_encode(array(
567: 'exceptions' => array(exceptionToJavaScript($exc))
568: ));
569: }
570: }
571:
572: 573: 574: 575: 576:
577: public function ajax_save_service() {
578: try {
579: $this->load->model('services_model');
580: $service = json_decode($_POST['service'], true);
581:
582: $REQUIRED_PRIV = (!isset($service['id']))
583: ? $this->privileges[PRIV_SERVICES]['add']
584: : $this->privileges[PRIV_SERVICES]['edit'];
585: if ($REQUIRED_PRIV == FALSE) {
586: throw new Exception('You do not have the required privileges for this task.');
587: }
588:
589: $service_id =$this->services_model->add($service);
590: echo json_encode(array(
591: 'status' => AJAX_SUCCESS,
592: 'id' => $service_id
593: ));
594: } catch(Exception $exc) {
595: echo json_encode(array(
596: 'exceptions' => array(exceptionToJavaScript($exc))
597: ));
598: }
599: }
600:
601: 602: 603: 604: 605:
606: public function ajax_delete_service() {
607: try {
608: if ($this->privileges[PRIV_SERVICES]['delete'] == FALSE) {
609: throw new Exception('You do not have the required privileges for this task.');
610: }
611:
612: $this->load->model('services_model');
613: $result = $this->services_model->delete($_POST['service_id']);
614: echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE);
615: } catch(Exception $exc) {
616: echo json_encode(array(
617: 'exceptions' => array(exceptionToJavaScript($exc))
618: ));
619: }
620: }
621:
622: 623: 624: 625: 626: 627:
628: public function ajax_filter_services() {
629: try {
630: if ($this->privileges[PRIV_SERVICES]['view'] == FALSE) {
631: throw new Exception('You do not have the required privileges for this task.');
632: }
633:
634: $this->load->model('services_model');
635: $key = mysql_real_escape_string($_POST['key']);
636: $where =
637: '(name LIKE "%' . $key . '%" OR duration LIKE "%' . $key . '%" OR ' .
638: 'price LIKE "%' . $key . '%" OR currency LIKE "%' . $key . '%" OR ' .
639: 'description LIKE "%' . $key . '%")';
640: $services = $this->services_model->get_batch($where);
641: echo json_encode($services);
642: } catch(Exception $exc) {
643: echo json_encode(array(
644: 'exceptions' => array(exceptionToJavaScript($exc))
645: ));
646: }
647: }
648:
649: 650: 651: 652: 653: 654:
655: public function ajax_save_service_category() {
656: try {
657: $this->load->model('services_model');
658: $category = json_decode($_POST['category'], true);
659:
660: $REQUIRED_PRIV = (!isset($category['id']))
661: ? $this->privileges[PRIV_SERVICES]['add']
662: : $this->privileges[PRIV_SERVICES]['edit'];
663: if ($REQUIRED_PRIV == FALSE) {
664: throw new Exception('You do not have the required privileges for this task.');
665: }
666:
667: $category_id = $this->services_model->add_category($category);
668: echo json_encode(array(
669: 'status' => AJAX_SUCCESS,
670: 'id' => $category_id
671: ));
672: } catch(Exception $exc) {
673: echo json_encode(array(
674: 'exceptions' => array(exceptionToJavaScript($exc))
675: ));
676: }
677: }
678:
679: 680: 681: 682: 683:
684: public function ajax_delete_service_category() {
685: try {
686: if ($this->privileges[PRIV_SERVICES]['delete'] == FALSE) {
687: throw new Exception('You do not have the required privileges for this task.');
688: }
689:
690: $this->load->model('services_model');
691: $result = $this->services_model->delete_category($_POST['category_id']);
692: echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE);
693: } catch(Exception $exc) {
694: echo json_encode(array(
695: 'exceptions' => array(exceptionToJavaScript($exc))
696: ));
697: }
698: }
699:
700: 701: 702: 703: 704: 705:
706: public function ajax_filter_service_categories() {
707: try {
708: if ($this->privileges[PRIV_SERVICES]['view'] == FALSE) {
709: throw new Exception('You do not have the required privileges for this task.');
710: }
711:
712: $this->load->model('services_model');
713: $key = mysql_real_escape_string($_POST['key']);
714: $where = '(name LIKE "%' . $key . '%" OR description LIKE "%' . $key . '%")';
715: $categories = $this->services_model->get_all_categories($where);
716: echo json_encode($categories);
717: } catch(Exception $exc) {
718: echo json_encode(array(
719: 'exceptions' => array(exceptionToJavaScript($exc))
720: ));
721: }
722: }
723:
724: 725: 726: 727: 728: 729:
730: public function ajax_filter_admins() {
731: try {
732: if ($this->privileges[PRIV_USERS]['view'] == FALSE) {
733: throw new Exception('You do not have the required privileges for this task.');
734: }
735:
736: $this->load->model('admins_model');
737: $key = mysql_real_escape_string($_POST['key']);
738: $where =
739: '(first_name LIKE "%' . $key . '%" OR last_name LIKE "%' . $key . '%" ' .
740: 'OR email LIKE "%' . $key . '%" OR mobile_number LIKE "%' . $key . '%" ' .
741: 'OR phone_number LIKE "%' . $key . '%" OR address LIKE "%' . $key . '%" ' .
742: 'OR city LIKE "%' . $key . '%" OR state LIKE "%' . $key . '%" ' .
743: 'OR zip_code LIKE "%' . $key . '%" OR notes LIKE "%' . $key . '%")';
744: $admins = $this->admins_model->get_batch($where);
745: echo json_encode($admins);
746: } catch(Exception $exc) {
747: echo json_encode(array(
748: 'exceptions' => array(exceptionToJavaScript($exc))
749: ));
750: }
751: }
752:
753: 754: 755: 756: 757: 758: 759: 760:
761: public function ajax_save_admin() {
762: try {
763: $this->load->model('admins_model');
764: $admin = json_decode($_POST['admin'], true);
765:
766: $REQUIRED_PRIV = (!isset($admin['id']))
767: ? $this->privileges[PRIV_USERS]['add']
768: : $this->privileges[PRIV_USERS]['edit'];
769: if ($REQUIRED_PRIV == FALSE) {
770: throw new Exception('You do not have the required privileges for this task.');
771: }
772:
773: $admin_id = $this->admins_model->add($admin);
774:
775: $response = array(
776: 'status' => AJAX_SUCCESS,
777: 'id' => $admin_id
778: );
779:
780: echo json_encode($response);
781: } catch(Exception $exc) {
782: echo json_encode(array(
783: 'exceptions' => array(exceptionToJavaScript($exc))
784: ));
785: }
786: }
787:
788: 789: 790: 791: 792: 793:
794: public function ajax_delete_admin() {
795: try {
796: if ($this->privileges[PRIV_USERS]['delete'] == FALSE) {
797: throw new Exception('You do not have the required privileges for this task.');
798: }
799:
800: $this->load->model('admins_model');
801: $result = $this->admins_model->delete($_POST['admin_id']);
802: echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE);
803: } catch(Exception $exc) {
804: echo json_encode(array(
805: 'exceptions' => array(exceptionToJavaScript($exc))
806: ));
807: }
808: }
809:
810: 811: 812: 813: 814: 815:
816: public function ajax_filter_providers() {
817: try {
818: if ($this->privileges[PRIV_USERS]['view'] == FALSE) {
819: throw new Exception('You do not have the required privileges for this task.');
820: }
821:
822: $this->load->model('providers_model');
823: $key = mysql_real_escape_string($_POST['key']);
824: $where =
825: '(first_name LIKE "%' . $key . '%" OR last_name LIKE "%' . $key . '%" ' .
826: 'OR email LIKE "%' . $key . '%" OR mobile_number LIKE "%' . $key . '%" ' .
827: 'OR phone_number LIKE "%' . $key . '%" OR address LIKE "%' . $key . '%" ' .
828: 'OR city LIKE "%' . $key . '%" OR state LIKE "%' . $key . '%" ' .
829: 'OR zip_code LIKE "%' . $key . '%" OR notes LIKE "%' . $key . '%")';
830: $providers = $this->providers_model->get_batch($where);
831: echo json_encode($providers);
832: } catch(Exception $exc) {
833: echo json_encode(array(
834: 'exceptions' => array(exceptionToJavaScript($exc))
835: ));
836: }
837: }
838:
839: 840: 841: 842: 843: 844: 845: 846:
847: public function ajax_save_provider() {
848: try {
849: $this->load->model('providers_model');
850: $provider = json_decode($_POST['provider'], true);
851:
852: $REQUIRED_PRIV = (!isset($provider['id']))
853: ? $this->privileges[PRIV_USERS]['add']
854: : $this->privileges[PRIV_USERS]['edit'];
855: if ($REQUIRED_PRIV == FALSE) {
856: throw new Exception('You do not have the required privileges for this task.');
857: }
858:
859: if (!isset($provider['settings']['working_plan'])) {
860: $this->load->model('settings_model');
861: $provider['settings']['working_plan'] = $this->settings_model
862: ->get_setting('company_working_plan');
863: }
864:
865: $provider_id = $this->providers_model->add($provider);
866:
867: echo json_encode(array(
868: 'status' => AJAX_SUCCESS,
869: 'id' => $provider_id
870: ));
871:
872: } catch(Exception $exc) {
873: echo json_encode(array(
874: 'exceptions' => array(exceptionToJavaScript($exc))
875: ));
876: }
877: }
878:
879: 880: 881: 882: 883: 884:
885: public function ajax_delete_provider() {
886: try {
887: if ($this->privileges[PRIV_USERS]['delete'] == FALSE) {
888: throw new Exception('You do not have the required privileges for this task.');
889: }
890:
891: $this->load->model('providers_model');
892: $result = $this->providers_model->delete($_POST['provider_id']);
893: echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE);
894: } catch(Exception $exc) {
895: echo json_encode(array(
896: 'exceptions' => array(exceptionToJavaScript($exc))
897: ));
898: }
899: }
900:
901: 902: 903: 904: 905: 906:
907: public function ajax_filter_secretaries() {
908: try {
909: if ($this->privileges[PRIV_USERS]['view'] == FALSE) {
910: throw new Exception('You do not have the required privileges for this task.');
911: }
912:
913: $this->load->model('secretaries_model');
914: $key = mysql_real_escape_string($_POST['key']);
915: $where =
916: '(first_name LIKE "%' . $key . '%" OR last_name LIKE "%' . $key . '%" ' .
917: 'OR email LIKE "%' . $key . '%" OR mobile_number LIKE "%' . $key . '%" ' .
918: 'OR phone_number LIKE "%' . $key . '%" OR address LIKE "%' . $key . '%" ' .
919: 'OR city LIKE "%' . $key . '%" OR state LIKE "%' . $key . '%" ' .
920: 'OR zip_code LIKE "%' . $key . '%" OR notes LIKE "%' . $key . '%")';
921: $secretaries = $this->secretaries_model->get_batch($where);
922: echo json_encode($secretaries);
923: } catch(Exception $exc) {
924: echo json_encode(array(
925: 'exceptions' => array(exceptionToJavaScript($exc))
926: ));
927: }
928: }
929:
930: 931: 932: 933: 934: 935: 936: 937:
938: public function ajax_save_secretary() {
939: try {
940: $this->load->model('secretaries_model');
941: $secretary = json_decode($_POST['secretary'], true);
942:
943: $REQUIRED_PRIV = (!isset($secretary['id']))
944: ? $this->privileges[PRIV_USERS]['add']
945: : $this->privileges[PRIV_USERS]['edit'];
946: if ($REQUIRED_PRIV == FALSE) {
947: throw new Exception('You do not have the required privileges for this task.');
948: }
949:
950: $secretary_id = $this->secretaries_model->add($secretary);
951:
952: echo json_encode(array(
953: 'status' => AJAX_SUCCESS,
954: 'id' => $secretary_id
955: ));
956: } catch(Exception $exc) {
957: echo json_encode(array(
958: 'exceptions' => array(exceptionToJavaScript($exc))
959: ));
960: }
961: }
962:
963: 964: 965: 966: 967: 968:
969: public function ajax_delete_secretary() {
970: try {
971: if ($this->privileges[PRIV_USERS]['delete'] == FALSE) {
972: throw new Exception('You do not have the required privileges for this task.');
973: }
974:
975: $this->load->model('secretaries_model');
976: $result = $this->secretaries_model->delete($_POST['secretary_id']);
977: echo ($result) ? json_encode(AJAX_SUCCESS) : json_encode(AJAX_FAILURE);
978: } catch(Exception $exc) {
979: echo json_encode(array(
980: 'exceptions' => array(exceptionToJavaScript($exc))
981: ));
982: }
983: }
984:
985: 986: 987: 988: 989: 990: 991: 992: 993: 994:
995: public function ajax_save_settings() {
996: try {
997: if ($_POST['type'] == SETTINGS_SYSTEM) {
998: if ($this->privileges[PRIV_SYSTEM_SETTINGS]['edit'] == FALSE) {
999: throw new Exception('You do not have the required privileges for this task.');
1000: }
1001: $this->load->model('settings_model');
1002: $settings = json_decode($_POST['settings'], true);
1003: $this->settings_model->save_settings($settings);
1004: } else if ($_POST['type'] == SETTINGS_USER) {
1005: if ($this->privileges[PRIV_USER_SETTINGS]['edit'] == FALSE) {
1006: throw new Exception('You do not have the required privileges for this task.');
1007: }
1008: $this->load->model('user_model');
1009: $this->user_model->save_settings(json_decode($_POST['settings'], true));
1010: }
1011:
1012: echo json_encode(AJAX_SUCCESS);
1013: } catch(Exception $exc) {
1014: echo json_encode(array(
1015: 'exceptions' => array(exceptionToJavaScript($exc))
1016: ));
1017: }
1018: }
1019:
1020: 1021: 1022: 1023: 1024: 1025:
1026: public function ajax_validate_username() {
1027: try {
1028:
1029:
1030:
1031: $_POST['record_exists'] = ($_POST['record_exists'] == 'true') ? TRUE : FALSE;
1032:
1033:
1034:
1035: $this->load->model('admins_model');
1036: $is_valid = $this->admins_model->validate_username($_POST['username'], $_POST['record_exists']);
1037: echo json_encode($is_valid);
1038: } catch(Exception $exc) {
1039: echo json_encode(array(
1040: 'exceptions' => array(exceptionToJavaScript($exc))
1041: ));
1042: }
1043: }
1044: }
1045:
1046:
1047: