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: 23: 24:
25: public function add($appointment_data) {
26:
27: if (!$this->validate_data($appointment_data)) {
28: throw new ValidationException('Appointment data are not valid');
29: }
30:
31:
32: if (!$this->exists($appointment_data)) {
33: $appointment_data['id'] = $this->insert($appointment_data);
34: } else {
35: $appointment_data['id'] = $this->update($appointment_data);
36: }
37:
38: return $appointment_data['id'];
39: }
40:
41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55:
56: public function exists($appointment_data) {
57: if (!isset($appointment_data['start_datetime'])
58: || !isset($appointment_data['end_datetime'])
59: || !isset($appointment_data['id_users_provider'])
60: || !isset($appointment_data['id_users_customer'])
61: || !isset($appointment_data['id_services'])) {
62: throw new InvalidArgumentException('Not all appointment field values are provided : ' . print_r($appointment_data, TRUE));
63: }
64:
65: $num_rows = $this->db->get_where('ea_appointments', array(
66: 'start_datetime' => $appointment_data['start_datetime'],
67: 'end_datetime' => $appointment_data['end_datetime'],
68: 'id_users_provider' => $appointment_data['id_users_provider'],
69: 'id_users_customer' => $appointment_data['id_users_customer'],
70: 'id_services' => $appointment_data['id_services'],
71: ))->num_rows();
72:
73: return ($num_rows > 0) ? TRUE : FALSE;
74: }
75:
76: 77: 78: 79: 80: 81: 82: 83: 84:
85: private function insert($appointment_data) {
86: if (!$this->db->insert('ea_appointments', $appointment_data)) {
87: throw new DatabaseException('Could not insert appointment record.');
88: }
89: return intval($this->db->insert_id());
90: }
91:
92: 93: 94: 95: 96: 97: 98: 99: 100: 101:
102: private function update($appointment_data) {
103: if (!isset($appointment_data['id'])) {
104: $appointment_data['id'] = $this->find_record_id($appointment_data);
105: }
106:
107: $this->db->where('id', $appointment_data['id']);
108: if (!$this->db->update('ea_appointments', $appointment_data)) {
109: throw new DatabaseException('Could not update appointment record.');
110: }
111:
112: return intval($appointment_data['id']);
113: }
114:
115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130:
131: public function find_record_id($appointment_data) {
132: $this->db->where(array(
133: 'start_datetime' => $appointment_data['start_datetime'],
134: 'end_datetime' => $appointment_data['end_datetime'],
135: 'id_users_provider' => $appointment_data['id_users_provider'],
136: 'id_users_customer' => $appointment_data['id_users_customer'],
137: 'id_services' => $appointment_data['id_services']
138: ));
139:
140: $result = $this->db->get('ea_appointments');
141:
142: if ($result->num_rows() == 0) {
143: throw new DatabaseException('Could not find appointment record id.');
144: }
145:
146: return $result->row()->id;
147: }
148:
149: 150: 151: 152: 153: 154: 155:
156: public function validate_data($appointment_data) {
157: $this->load->helper('data_validation');
158:
159: try {
160:
161: if (!validate_mysql_datetime($appointment_data['start_datetime'])) {
162: throw new Exception('Appointment start datetime is invalid.');
163: }
164:
165: if (!validate_mysql_datetime($appointment_data['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_data['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:
182: $num_rows = $this->db
183: ->select('*')
184: ->from('ea_users')
185: ->join('ea_roles', 'ea_roles.id = ea_users.id_roles', 'inner')
186: ->where('ea_users.id', $appointment_data['id_users_customer'])
187: ->where('ea_roles.slug', DB_SLUG_CUSTOMER)
188: ->get()->num_rows();
189: if ($num_rows == 0) {
190: throw new Exception('Appointment customer id is invalid.');
191: }
192:
193:
194: $num_rows = $this->db->get_where('ea_services', array('id' => $appointment_data['id_services']))->num_rows();
195: if ($num_rows == 0) {
196: throw new Exception('Appointment customer id is invalid.');
197: }
198:
199: return TRUE;
200: } catch (Exception $exc) {
201: return FALSE;
202: }
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 InvalidArgumentException('Invalid argument type $appointment_id : ' . $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 InvalidArgumentException('Invalid argument given. Expected integer for the $appointment_id : ' . $appointment_id);
240: }
241: return $this->db->get_where('ea_appointments', array('id' => $appointment_id))->row_array();
242: }
243:
244: 245: 246: 247: 248: 249: 250: 251:
252: public function get_value($field_name, $appointment_id) {
253: if (!is_numeric($appointment_id)) {
254: throw new InvalidArgumentException('Invalid argument given, expected integer for the $appointment_id : ' . $appointment_id);
255: }
256:
257: if (!is_string($field_name)) {
258: throw new InvalidArgumentException('Invalid argument given, expected string for the $field_name : ' . $field_name);
259: }
260:
261: if ($this->db->get_where('ea_appointments', array('id' => $appointment_id))->num_rows() == 0) {
262: throw new InvalidArgumentException('The record with the provided id does not exist in the database : ' . $appointment_id);
263: }
264:
265: $row_data = $this->db->get_where('ea_appointments', array('id' => $appointment_id))->row_array();
266:
267: if (!isset($row_data[$field_name])) {
268: throw new InvalidArgumentException('The given field name does not exist in the database : ' . $field_name);
269: }
270:
271: return $row_data[$field_name];
272: }
273:
274: 275: 276: 277: 278: 279: 280: 281: 282:
283: public function get_batch($where_clause = '') {
284: if ($where_clause != '') {
285: $this->db->where($where_clause);
286: }
287:
288: return $this->db->get('ea_appointments')->result_array();
289: }
290: }
291:
292:
293: