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