1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3 class Backend extends CI_Controller {
4 public function __construct() {
5 parent::__construct();
6 $this->load->library('session');
7
8
9 if ($this->session->userdata('language')) {
10 $this->config->set_item('language', $this->session->userdata('language'));
11 $this->lang->load('translations', $this->session->userdata('language'));
12 } else {
13 $this->lang->load('translations', $this->config->item('language'));
14 }
15 }
16
17 18 19 20 21 22 23 24 25 26 27
28 public function index($appointment_hash = '') {
29 $this->session->set_userdata('dest_url', $this->config->item('base_url') . '/backend');
30 if (!$this->hasPrivileges(PRIV_APPOINTMENTS)) return;
31
32 $this->load->model('appointments_model');
33 $this->load->model('providers_model');
34 $this->load->model('services_model');
35 $this->load->model('customers_model');
36 $this->load->model('settings_model');
37 $this->load->model('roles_model');
38 $this->load->model('user_model');
39 $this->load->model('secretaries_model');
40
41 $view['base_url'] = $this->config->item('base_url');
42 $view['user_display_name'] = $this->user_model->get_user_display_name($this->session->userdata('user_id'));
43 $view['active_menu'] = PRIV_APPOINTMENTS;
44 $view['book_advance_timeout'] = $this->settings_model->get_setting('book_advance_timeout');
45 $view['company_name'] = $this->settings_model->get_setting('company_name');
46 $view['available_providers'] = $this->providers_model->get_available_providers();
47 $view['available_services'] = $this->services_model->get_available_services();
48 $view['customers'] = $this->customers_model->get_batch();
49 $this->setUserData($view);
50
51 if ($this->session->userdata('role_slug') == DB_SLUG_SECRETARY) {
52 $secretary = $this->secretaries_model->get_row($this->session->userdata('user_id'));
53 $view['secretary_providers'] = $secretary['providers'];
54 } else {
55 $view['secretary_providers'] = array();
56 }
57
58
59 $results = $this->appointments_model->get_batch(array('hash' => $appointment_hash));
60 if ($appointment_hash != '' && count($results) > 0) {
61 $appointment = $results[0];
62 $appointment['customer'] = $this->customers_model->get_row($appointment['id_users_customer']);
63 $view['edit_appointment'] = $appointment;
64 } else {
65 $view['edit_appointment'] = NULL;
66 }
67
68 $this->load->view('backend/header', $view);
69 $this->load->view('backend/calendar', $view);
70 $this->load->view('backend/footer', $view);
71 }
72
73 74 75 76 77
78 public function customers() {
79 $this->session->set_userdata('dest_url', $this->config->item('base_url') . '/backend/customers');
80 if (!$this->hasPrivileges(PRIV_CUSTOMERS)) return;
81
82 $this->load->model('providers_model');
83 $this->load->model('customers_model');
84 $this->load->model('services_model');
85 $this->load->model('settings_model');
86 $this->load->model('user_model');
87
88 $view['base_url'] = $this->config->item('base_url');
89 $view['user_display_name'] = $this->user_model->get_user_display_name($this->session->userdata('user_id'));
90 $view['active_menu'] = PRIV_CUSTOMERS;
91 $view['company_name'] = $this->settings_model->get_setting('company_name');
92 $view['customers'] = $this->customers_model->get_batch();
93 $view['available_providers'] = $this->providers_model->get_available_providers();
94 $view['available_services'] = $this->services_model->get_available_services();
95 $this->setUserData($view);
96
97 $this->load->view('backend/header', $view);
98 $this->load->view('backend/customers', $view);
99 $this->load->view('backend/footer', $view);
100 }
101
102 103 104 105 106 107 108 109 110
111 public function services() {
112 $this->session->set_userdata('dest_url', $this->config->item('base_url') . '/backend/services');
113 if (!$this->hasPrivileges(PRIV_SERVICES)) return;
114
115 $this->load->model('customers_model');
116 $this->load->model('services_model');
117 $this->load->model('settings_model');
118 $this->load->model('user_model');
119
120 $view['base_url'] = $this->config->item('base_url');
121 $view['user_display_name'] = $this->user_model->get_user_display_name($this->session->userdata('user_id'));
122 $view['active_menu'] = PRIV_SERVICES;
123 $view['company_name'] = $this->settings_model->get_setting('company_name');
124 $view['services'] = $this->services_model->get_batch();
125 $view['categories'] = $this->services_model->get_all_categories();
126 $this->setUserData($view);
127
128 $this->load->view('backend/header', $view);
129 $this->load->view('backend/services', $view);
130 $this->load->view('backend/footer', $view);
131 }
132
133 134 135 136 137 138 139
140 public function users() {
141 $this->session->set_userdata('dest_url', $this->config->item('base_url') . '/backend/users');
142 if (!$this->hasPrivileges(PRIV_USERS)) return;
143
144 $this->load->model('providers_model');
145 $this->load->model('secretaries_model');
146 $this->load->model('admins_model');
147 $this->load->model('services_model');
148 $this->load->model('settings_model');
149 $this->load->model('user_model');
150
151 $view['base_url'] = $this->config->item('base_url');
152 $view['user_display_name'] = $this->user_model->get_user_display_name($this->session->userdata('user_id'));
153 $view['active_menu'] = PRIV_USERS;
154 $view['company_name'] = $this->settings_model->get_setting('company_name');
155 $view['admins'] = $this->admins_model->get_batch();
156 $view['providers'] = $this->providers_model->get_batch();
157 $view['secretaries'] = $this->secretaries_model->get_batch();
158 $view['services'] = $this->services_model->get_batch();
159 $view['working_plan'] = $this->settings_model->get_setting('company_working_plan');
160 $this->setUserData($view);
161
162 $this->load->view('backend/header', $view);
163 $this->load->view('backend/users', $view);
164 $this->load->view('backend/footer', $view);
165 }
166
167 168 169 170 171 172 173
174 public function settings() {
175 $this->session->set_userdata('dest_url', $this->config->item('base_url') . '/backend/settings');
176 if (!$this->hasPrivileges(PRIV_SYSTEM_SETTINGS, FALSE)
177 && !$this->hasPrivileges(PRIV_USER_SETTINGS)) return;
178
179 $this->load->model('settings_model');
180 $this->load->model('user_model');
181
182 $this->load->library('session');
183 $user_id = $this->session->userdata('user_id');
184
185 $view['base_url'] = $this->config->item('base_url');
186 $view['user_display_name'] = $this->user_model->get_user_display_name($user_id);
187 $view['active_menu'] = PRIV_SYSTEM_SETTINGS;
188 $view['company_name'] = $this->settings_model->get_setting('company_name');
189 $view['role_slug'] = $this->session->userdata('role_slug');
190 $view['system_settings'] = $this->settings_model->get_settings();
191 $view['user_settings'] = $this->user_model->get_settings($user_id);
192 $this->setUserData($view);
193
194 $this->load->view('backend/header', $view);
195 $this->load->view('backend/settings', $view);
196 $this->load->view('backend/footer', $view);
197 }
198
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
218 private function hasPrivileges($page, $redirect = TRUE) {
219
220 $user_id = $this->session->userdata('user_id');
221 if ($user_id == FALSE) {
222 if ($redirect) {
223 header('Location: ' . $this->config->item('base_url') . '/user/login');
224 }
225 return FALSE;
226 }
227
228
229 $role_slug = $this->session->userdata('role_slug');
230 $role_priv = $this->db->get_where('ea_roles', array('slug' => $role_slug))->row_array();
231 if ($role_priv[$page] < PRIV_VIEW) {
232 if ($redirect) {
233 header('Location: ' . $this->config->item('base_url') . '/user/no_privileges');
234 }
235 return FALSE;
236 }
237
238 return TRUE;
239 }
240
241 242 243 244 245
246 public function setUserData(&$view) {
247 $this->load->model('roles_model');
248
249
250 $view['user_id'] = $this->session->userdata('user_id');
251 $view['user_email'] = $this->session->userdata('user_email');
252 $view['role_slug'] = $this->session->userdata('role_slug');
253 $view['privileges'] = $this->roles_model->get_privileges($this->session->userdata('role_slug'));
254 }
255
256 257 258 259 260 261 262 263 264
265 public function update() {
266 try {
267 if (!$this->hasPrivileges(PRIV_SYSTEM_SETTINGS, TRUE))
268 throw new Exception('You do not have the required privileges for this task!');
269
270 $this->load->library('migration');
271
272 if (!$this->migration->current())
273 throw new Exception($this->migration->error_string());
274
275 echo json_encode(AJAX_SUCCESS);
276
277 } catch(Exception $exc) {
278 echo json_encode(array(
279 'exceptions' => array(exceptionToJavaScript($exc))
280 ));
281 }
282 }
283 }
284
285
286