1: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2:
3: class Appointments_Model extends CI_Model {
4: 5: 6:
7: public function __construct() {
8: parent::__construct();
9: }
10:
11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22: public function add($appointment) {
23:
24: if (!$this->validate($appointment)) {
25: throw new Exception('Appointment data are invalid.');
26: }
27:
28:
29: if (!isset($appointment['id'])) {
30: $appointment['id'] = $this->insert($appointment);
31: } else {
32: $this->update($appointment);
33: }
34:
35: return $appointment['id'];
36: }
37:
38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49:
50: public function exists($appointment) {
51: if (!isset($appointment['start_datetime'])
52: || !isset($appointment['end_datetime'])
53: || !isset($appointment['id_users_provider'])
54: || !isset($appointment['id_users_customer'])
55: || !isset($appointment['id_services'])) {
56: throw new Exception('Not all appointment field values '
57: . 'are provided : ' . print_r($appointment, TRUE));
58: }
59:
60: $num_rows = $this->db->get_where('ea_appointments', array(
61: 'start_datetime' => $appointment['start_datetime'],
62: 'end_datetime' => $appointment['end_datetime'],
63: 'id_users_provider' => $appointment['id_users_provider'],
64: 'id_users_customer' => $appointment['id_users_customer'],
65: 'id_services' => $appointment['id_services'],))
66: ->num_rows();
67:
68: return ($num_rows > 0) ? TRUE : FALSE;
69: }
70:
71: 72: 73: 74: 75: 76: 77:
78: private function insert($appointment) {
79: $appointment['book_datetime'] = date('Y-m-d H:i:s');
80: $appointment['hash'] = $this->generate_hash();
81:
82: if (!$this->db->insert('ea_appointments', $appointment)) {
83: throw new Exception('Could not insert appointment record.');
84: }
85:
86: return intval($this->db->insert_id());
87: }
88:
89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100:
101: private function update($appointment) {
102: $this->db->where('id', $appointment['id']);
103: if (!$this->db->update('ea_appointments', $appointment)) {
104: throw new Exception('Could not update appointment record.');
105: }
106: }
107:
108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122:
123: public function find_record_id($appointment) {
124: $this->db->where(array(
125: 'start_datetime' => $appointment['start_datetime'],
126: 'end_datetime' => $appointment['end_datetime'],
127: 'id_users_provider' => $appointment['id_users_provider'],
128: 'id_users_customer' => $appointment['id_users_customer'],
129: 'id_services' => $appointment['id_services']
130: ));
131:
132: $result = $this->db->get('ea_appointments');
133:
134: if ($result->num_rows() == 0) {
135: throw new Exception('Could not find appointment record id.');
136: }
137:
138: return $result->row()->id;
139: }
140:
141: 142: 143: 144: 145: 146: 147:
148: public function validate($appointment) {
149: $this->load->helper('data_validation');
150:
151: try {
152:
153:
154: if (isset($appointment['id'])) {
155: $num_rows = $this->db->get_where('ea_appointments',
156: array('id' => $appointment['id']))->num_rows();
157: if ($num_rows == 0) {
158: throw new Exception('Provided appointment id does not '
159: . 'exist in the database.');
160: }
161: }
162:
163:
164: if (!validate_mysql_datetime($appointment['start_datetime'])) {
165: throw new Exception('Appointment start datetime is invalid.');
166: }
167:
168: if (!validate_mysql_datetime($appointment['end_datetime'])) {
169: throw new Exception('Appointment end datetime is invalid.');
170: }
171:
172:
173: $num_rows = $this->db
174: ->select('*')
175: ->from('ea_users')
176: ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
177: ->where('ea_users.id', $appointment['id_users_provider'])
178: ->where('ea_roles.slug', DB_SLUG_PROVIDER)
179: ->get()->num_rows();
180: if ($num_rows == 0) {
181: throw new Exception('Appointment provider id is invalid.');
182: }
183:
184: if ($appointment['is_unavailable'] == FALSE) {
185:
186: $num_rows = $this->db
187: ->select('*')
188: ->from('ea_users')
189: ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
190: ->where('ea_users.id', $appointment['id_users_customer'])
191: ->where('ea_roles.slug', DB_SLUG_CUSTOMER)
192: ->get()->num_rows();
193: if ($num_rows == 0) {
194: throw new Exception('Appointment customer id is invalid.');
195: }
196:
197:
198: $num_rows = $this->db->get_where('ea_services',
199: array('id' => $appointment['id_services']))->num_rows();
200: if ($num_rows == 0) {
201: throw new Exception('Appointment customer id is invalid.');
202: }
203: }
204:
205: return TRUE;
206: } catch (Exception $exc) {
207: return FALSE;
208: }
209: }
210:
211: 212: 213: 214: 215: 216: 217: 218: 219:
220: public function delete($appointment_id) {
221: if (!is_numeric($appointment_id)) {
222: throw new Exception('Invalid argument type $appointment_id (value:"' . $appointment_id . '")');
223: }
224:
225: $num_rows = $this->db->get_where('ea_appointments', array('id' => $appointment_id))->num_rows();
226:
227: if ($num_rows == 0) {
228: return FALSE;
229: }
230:
231: $this->db->where('id', $appointment_id);
232: return $this->db->delete('ea_appointments');
233: }
234:
235: 236: 237: 238: 239: 240: 241: 242:
243: public function get_row($appointment_id) {
244: if (!is_numeric($appointment_id)) {
245: throw new Exception('Invalid argument given. Expected '
246: . 'integer for the $appointment_id : ' . $appointment_id);
247: }
248: return $this->db->get_where('ea_appointments',
249: array('id' => $appointment_id))->row_array();
250: }
251:
252: 253: 254: 255: 256: 257: 258:
259: public function get_value($field_name, $appointment_id) {
260: if (!is_numeric($appointment_id)) {
261: throw new Exception('Invalid argument given, expected '
262: . 'integer for the $appointment_id : ' . $appointment_id);
263: }
264:
265: if (!is_string($field_name)) {
266: throw new Exception('Invalid argument given, expected '
267: . 'string for the $field_name : ' . $field_name);
268: }
269:
270: if ($this->db->get_where('ea_appointments',
271: array('id' => $appointment_id))->num_rows() == 0) {
272: throw new Exception('The record with the provided id '
273: . 'does not exist in the database : ' . $appointment_id);
274: }
275:
276: $row_data = $this->db->get_where('ea_appointments',
277: array('id' => $appointment_id))->row_array();
278:
279: if (!isset($row_data[$field_name])) {
280: throw new Exception('The given field name does not '
281: . 'exist in the database : ' . $field_name);
282: }
283:
284: return $row_data[$field_name];
285: }
286:
287: 288: 289: 290: 291: 292: 293: 294: 295:
296: public function get_batch($where_clause = '') {
297: if ($where_clause != '') {
298: $this->db->where($where_clause);
299: }
300:
301: return $this->db->get('ea_appointments')->result_array();
302: }
303:
304: 305: 306: 307: 308: 309: 310: 311: 312: 313:
314: public function generate_hash() {
315: $current_date = new DateTime();
316: return md5($current_date->getTimestamp());
317: }
318:
319: 320: 321: 322: 323: 324:
325: public function add_unavailable($unavailable) {
326:
327: $start = strtotime($unavailable['start_datetime']);
328: $end = strtotime($unavailable['end_datetime']);
329: if ($start > $end) {
330: throw new Exception('Unavailable period start must be prior to end.');
331: }
332:
333:
334: $where_clause = array(
335: 'id' => $unavailable['id_users_provider'],
336: 'id_roles' => $this->db->get_where('ea_roles', array('slug' => DB_SLUG_PROVIDER))->row()->id
337: );
338:
339: if ($this->db->get_where('ea_users', $where_clause)->num_rows() == 0) {
340: throw new Exception('Provider id was not found in database.');
341: }
342:
343:
344: if (!isset($unavailable['id'])) {
345: $unavailable['book_datetime'] = date('Y-m-d H:i:s');
346: $unavailable['is_unavailable'] = true;
347:
348: $this->db->insert('ea_appointments', $unavailable);
349: $unavailable['id'] = $this->db->insert_id();
350: } else {
351: $this->db->where(array('id' => $unavailable['id']));
352: $this->db->update('ea_appointments', $unavailable);
353: }
354:
355: return $unavailable['id'];
356: }
357:
358: 359: 360: 361: 362:
363: public function delete_unavailable($unavailable_id) {
364: if (!is_numeric($unavailable_id)) {
365: throw new Exception('Invalid argument type $unavailable_id (value:"' .
366: $unavailable_id . '")');
367: }
368:
369: $num_rows = $this->db->get_where('ea_appointments', array('id' => $unavailable_id))
370: ->num_rows();
371: if ($num_rows == 0) {
372: return FALSE;
373: }
374:
375: $this->db->where('id', $unavailable_id);
376: return $this->db->delete('ea_appointments');
377: }
378: }
379:
380:
381: