PageRenderTime 58ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/application/controllers/Rooms.php

https://github.com/craigrodway/classroombookings
PHP | 590 lines | 357 code | 179 blank | 54 comment | 32 complexity | 759ca03898fb7d195bfdd70c648ff74a MD5 | raw file
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. class Rooms extends MY_Controller
  4. {
  5. public function __construct()
  6. {
  7. parent::__construct();
  8. $this->require_logged_in();
  9. $this->load->model('crud_model');
  10. $this->load->model('rooms_model');
  11. $this->load->model('users_model');
  12. $this->load->helper('number');
  13. $this->data['max_size_bytes'] = max_upload_file_size();
  14. $this->data['max_size_human'] = byte_format(max_upload_file_size());
  15. $this->data['showtitle'] = 'Rooms';
  16. $this->data['rooms_icons'] = [
  17. ['rooms', 'Rooms', 'school_manage_rooms.png'],
  18. ['rooms/fields', 'Custom Fields', 'room_fields.png'],
  19. ['access_control', 'Access Control', 'key.png'],
  20. ];
  21. }
  22. function info($room_id = NULL)
  23. {
  24. if (empty($room_id)) {
  25. show_error('No room to show');
  26. }
  27. $this->data['room'] = $this->rooms_model->Get($room_id);
  28. if (empty($this->data['room'])) {
  29. show_error("The requested room could not be found.");
  30. }
  31. $this->load->library('table');
  32. $photo_path = "uploads/{$this->data['room']->photo}";
  33. $has_photo = (strlen($this->data['room']->photo) && is_file(FCPATH . $photo_path));
  34. $this->data['photo_url'] = $has_photo ? base_url($photo_path) : FALSE;
  35. // Get all info with formatted values
  36. $this->data['room_info'] = $this->rooms_model->room_info($room_id);
  37. $this->data['fields'] = $this->rooms_model->GetFields();
  38. $this->data['fieldvalues'] = $this->rooms_model->GetFieldValues($room_id);
  39. $this->load->view('rooms/room_info', $this->data);
  40. }
  41. public function photo($room_id = NULL)
  42. {
  43. if (empty($room_id)) {
  44. show_error('No room to show');
  45. }
  46. $room = $this->rooms_model->Get($room_id);
  47. if (empty($room)) {
  48. show_error("The requested room could not be found.");
  49. }
  50. if ( ! strlen($room->photo)) {
  51. show_error('No photo available.');
  52. }
  53. $photo_path = "uploads/{$room->photo}";
  54. if ( ! is_file(FCPATH . $photo_path)) {
  55. show_error('Photo file does not exist.');
  56. }
  57. $url = base_url($photo_path);
  58. $img_el = img($url);
  59. $room_name = html_escape($room->name);
  60. $title = "<h4>{$room_name}</h4>";
  61. echo "<div class='room-photo'>{$title}{$img_el}</div>";
  62. }
  63. function index()
  64. {
  65. $this->require_auth_level(ADMINISTRATOR);
  66. $this->data['rooms'] = $this->rooms_model->Get();
  67. $this->data['title'] = 'Rooms';
  68. $icons = iconbar($this->data['rooms_icons'], 'rooms');
  69. $body = $this->load->view('rooms/rooms_index', $this->data, TRUE);
  70. $this->data['body'] = $icons . $body;
  71. return $this->render();
  72. }
  73. /**
  74. * Controller function to handle the Add page
  75. */
  76. function add()
  77. {
  78. $this->require_auth_level(ADMINISTRATOR);
  79. // Get list of users
  80. $this->data['users'] = $this->users_model->Get(NULL, NULL, NULL);
  81. $this->data['fields'] = $this->rooms_model->GetFields();
  82. $this->data['fieldvalues'] = array();
  83. $this->data['title'] = 'Add Room';
  84. // $this->data['showtitle'] = $this->data['title'];
  85. $columns = array(
  86. 'c1' => array(
  87. 'content' => $this->load->view('rooms/rooms_add', $this->data, TRUE),
  88. 'width' => '70%',
  89. ),
  90. 'c2' => array(
  91. 'content' => $this->load->view('rooms/rooms_add_side', $this->data, TRUE),
  92. 'width' => '30%',
  93. ),
  94. );
  95. $icons = iconbar($this->data['rooms_icons'], 'rooms');
  96. $title = "<h2>{$this->data['title']}</h2>";
  97. $columns = $this->load->view('columns', $columns, TRUE);
  98. $this->data['body'] = $icons . $title . $columns;
  99. return $this->render();
  100. }
  101. /**
  102. * Controller function to handle an edit
  103. */
  104. function edit($id = NULL)
  105. {
  106. $this->require_auth_level(ADMINISTRATOR);
  107. $this->data['room'] = $this->rooms_model->Get($id);
  108. if (empty($this->data['room'])) {
  109. show_404();
  110. }
  111. $this->data['users'] = $this->users_model->Get(NULL, NULL, NULL);
  112. $this->data['fields'] = $this->rooms_model->GetFields();
  113. $this->data['fieldvalues'] = $this->rooms_model->GetFieldValues($id);
  114. $this->data['title'] = 'Edit Room';
  115. // $this->data['showtitle'] = $this->data['title'];
  116. $columns = array(
  117. 'c1' => array(
  118. 'content' => $this->load->view('rooms/rooms_add', $this->data, TRUE),
  119. 'width' => '70%',
  120. ),
  121. 'c2' => array(
  122. 'content' => $this->load->view('rooms/rooms_add_side', $this->data, TRUE),
  123. 'width' => '30%',
  124. ),
  125. );
  126. $icons = iconbar($this->data['rooms_icons'], 'rooms');
  127. $title = "<h2>{$this->data['title']}</h2>";
  128. $columns = $this->load->view('columns', $columns, TRUE);
  129. $this->data['body'] = $icons . $title . $columns;
  130. return $this->render();
  131. }
  132. /**
  133. * Save
  134. *
  135. */
  136. function save()
  137. {
  138. $this->require_auth_level(ADMINISTRATOR);
  139. $room_id = $this->input->post('room_id');
  140. $this->load->library('form_validation');
  141. $this->form_validation->set_rules('room_id', 'ID', 'integer');
  142. $this->form_validation->set_rules('name', 'Name', 'required|min_length[1]|max_length[20]');
  143. $this->form_validation->set_rules('user_id', 'User', 'integer');
  144. $this->form_validation->set_rules('location', 'Location', 'max_length[40]');
  145. $this->form_validation->set_rules('notes', 'Notes', 'max_length[255]');
  146. $this->form_validation->set_rules('bookable', 'Bookable', 'integer');
  147. if ($this->form_validation->run() == FALSE) {
  148. return (empty($room_id) ? $this->add() : $this->edit($room_id));
  149. }
  150. $room_data = array(
  151. 'name' => $this->input->post('name'),
  152. 'user_id' => $this->input->post('user_id'),
  153. 'location' => $this->input->post('location'),
  154. 'notes' => $this->input->post('notes'),
  155. 'bookable' => $this->input->post('bookable'),
  156. );
  157. if (empty($room_id)) {
  158. $room_id = $this->rooms_model->add($room_data);
  159. if ($room_id) {
  160. $line = sprintf($this->lang->line('crbs_action_added'), $room_data['name']);
  161. $flashmsg = msgbox('info', $line);
  162. } else {
  163. $line = sprintf($this->lang->line('crbs_action_dberror'), 'adding');
  164. $flashmsg = msgbox('error', $line);
  165. }
  166. } else {
  167. if ($this->rooms_model->edit($room_id, $room_data)) {
  168. $line = sprintf($this->lang->line('crbs_action_saved'), $room_data['name']);
  169. $flashmsg = msgbox('info', $line);
  170. } else {
  171. $line = sprintf($this->lang->line('crbs_action_dberror'), 'editing');
  172. $flashmsg = msgbox('error', $line);
  173. }
  174. }
  175. $this->session->set_flashdata('saved', $flashmsg);
  176. // Process image things
  177. //
  178. $image_status = $this->process_image($room_id);
  179. if ( ! $image_status) {
  180. return (empty($room_id) ? $this->add() : $this->edit($room_id));
  181. }
  182. // Process field-related things
  183. //
  184. $fields_status = $this->process_fields($room_id);
  185. if ( ! $fields_status) {
  186. return (empty($room_id) ? $this->add() : $this->edit($room_id));
  187. }
  188. redirect('rooms');
  189. }
  190. /**
  191. * Handle the uploading of an image when saving a room.
  192. *
  193. */
  194. private function process_image($room_id = NULL)
  195. {
  196. if (empty($room_id)) {
  197. return TRUE;
  198. }
  199. if ($this->input->post('photo_delete')) {
  200. $this->rooms_model->delete_photo($room_id);
  201. }
  202. $has_image = (isset($_FILES['userfile'])
  203. && isset($_FILES['userfile']['name'])
  204. && ! empty($_FILES['userfile']['name']));
  205. if ( ! $has_image) {
  206. return TRUE;
  207. }
  208. // Upload config
  209. //
  210. $upload_config = array(
  211. 'upload_path' => FCPATH . 'uploads',
  212. 'allowed_types' => 'jpg|jpeg|png|gif',
  213. 'max_size' => $this->data['max_size_bytes'],
  214. 'encrypt_name' => TRUE,
  215. );
  216. $this->load->library('upload', $upload_config);
  217. if ( ! $this->upload->do_upload()) {
  218. $error = $this->upload->display_errors('','');
  219. $this->session->set_flashdata('image_error', $error);
  220. $image_error = $error;
  221. return FALSE;
  222. }
  223. // File uploaded
  224. //
  225. $upload_data = $this->upload->data();
  226. $this->load->library('image_lib');
  227. $image_config = array(
  228. 'image_library' => 'gd2',
  229. 'source_image' => $upload_data['full_path'],
  230. 'maintain_ratio' => TRUE,
  231. 'width' => 1280,
  232. 'height' => 1280,
  233. 'master_dim' => 'auto',
  234. );
  235. $this->image_lib->initialize($image_config);
  236. $res = $this->image_lib->resize();
  237. if ( ! $res) {
  238. $this->session->set_flashdata('image_error', $this->image_lib->display_errors());
  239. return FALSE;
  240. }
  241. // Remove previous photo
  242. $this->rooms_model->delete_photo($room_id);
  243. // Update DB with new photo
  244. $this->rooms_model->edit($room_id, array(
  245. 'photo' => $upload_data['file_name'],
  246. ));
  247. return TRUE;
  248. }
  249. /**
  250. * Process the updating of field values when saving a room
  251. *
  252. */
  253. private function process_fields($room_id = NULL)
  254. {
  255. if (empty($room_id)) {
  256. return TRUE;
  257. }
  258. $fieldvalues = array();
  259. $fields = $this->rooms_model->GetFields();
  260. $fields = (is_array($fields) ? $fields : array());
  261. foreach ($fields as $field) {
  262. $key = $field->field_id;
  263. $value = $this->input->post("f{$key}");
  264. $fieldvalues[ $key ] = $value;
  265. }
  266. return $this->rooms_model->save_field_values($room_id, $fieldvalues);
  267. }
  268. /**
  269. * Controller function to delete a room
  270. *
  271. */
  272. function delete($id = NULL)
  273. {
  274. $this->require_auth_level(ADMINISTRATOR);
  275. if ($this->input->post('id')) {
  276. $this->rooms_model->delete($this->input->post('id'));
  277. $flashmsg = msgbox('info', $this->lang->line('crbs_action_deleted'));
  278. $this->session->set_flashdata('saved', $flashmsg);
  279. redirect('rooms');
  280. }
  281. $this->data['action'] = 'rooms/delete';
  282. $this->data['id'] = $id;
  283. $this->data['cancel'] = 'rooms';
  284. $this->data['text'] = 'If you delete this room, <strong>all bookings</strong> for this room will be <strong>permanently deleted</strong> as well.';
  285. $row = $this->rooms_model->Get($id);
  286. $this->data['title'] = 'Delete Room ('.html_escape($row->name).')';
  287. $icons = iconbar($this->data['rooms_icons'], 'rooms');
  288. $title = "<h2>{$this->data['title']}</h2>";
  289. $body = $this->load->view('partials/deleteconfirm', $this->data, TRUE);
  290. $this->data['body'] = $icons . $title . $body;
  291. return $this->render();
  292. }
  293. /**
  294. * Fields
  295. *
  296. */
  297. function fields()
  298. {
  299. $this->require_auth_level(ADMINISTRATOR);
  300. $this->data['options_list'] = $this->rooms_model->options;
  301. $this->data['fields'] = $this->rooms_model->GetFields();
  302. $this->data['title'] = 'Custom Fields';
  303. // $this->data['showtitle'] = 'Custom Fields';
  304. $icons = iconbar($this->data['rooms_icons'], 'rooms/fields');
  305. $body = $this->load->view('rooms/fields/index', $this->data, TRUE);
  306. $this->data['body'] = $icons . $body;
  307. return $this->render();
  308. }
  309. function add_field()
  310. {
  311. $this->require_auth_level(ADMINISTRATOR);
  312. $this->data['options_list'] = $this->rooms_model->options;
  313. $this->data['title'] = 'Add Field';
  314. // $this->data['showtitle'] = $this->data['title'];
  315. $columns = array(
  316. 'c1' => array(
  317. 'content'=> $this->load->view('rooms/fields/add', $this->data, TRUE),
  318. 'width' => '70%',
  319. ),
  320. 'c2' => array(
  321. 'content' => '',
  322. 'width' => '30%',
  323. ),
  324. );
  325. $icons = iconbar($this->data['rooms_icons'], 'rooms/fields');
  326. $title = "<h2>{$this->data['title']}</h2>";
  327. $columns = $this->load->view('columns', $columns, TRUE);
  328. $this->data['body'] = $icons . $title . $columns;
  329. return $this->render();
  330. }
  331. /**
  332. * Controller function to handle an edit
  333. *
  334. */
  335. function edit_field($id = NULL)
  336. {
  337. $this->require_auth_level(ADMINISTRATOR);
  338. $this->data['field'] = $this->rooms_model->GetFields($id);
  339. $this->data['options_list'] = $this->rooms_model->options;
  340. $this->data['title'] = 'Edit Field';
  341. // $this->data['showtitle'] = $this->data['title'];
  342. $columns = array(
  343. 'c1' => array(
  344. 'content'=> $this->load->view('rooms/fields/add', $this->data, TRUE),
  345. 'width' => '70%',
  346. ),
  347. 'c2' => array(
  348. 'content' => '',
  349. 'width' => '30%',
  350. ),
  351. );
  352. $icons = iconbar($this->data['rooms_icons'], 'rooms/fields');
  353. $title = "<h2>{$this->data['title']}</h2>";
  354. $columns = $this->load->view('columns', $columns, TRUE);
  355. $this->data['body'] = $icons . $title . $columns;
  356. return $this->render();
  357. }
  358. function save_field()
  359. {
  360. $this->require_auth_level(ADMINISTRATOR);
  361. // Get ID from form
  362. $field_id = $this->input->post('field_id');
  363. $this->load->library('form_validation');
  364. $this->form_validation->set_rules('field_id', 'ID', 'integer');
  365. $this->form_validation->set_rules('name', 'Name', 'required|min_length[1]|max_length[64]');
  366. $this->form_validation->set_rules('options', 'Items', '');
  367. if ($this->form_validation->run() == FALSE){
  368. return (empty($field_id) ? $this->add_field() : $this->edit_field($field_id));
  369. }
  370. // Validation succeeded!
  371. $field_data = array(
  372. 'name' => $this->input->post('name'),
  373. 'type' => $this->input->post('type'),
  374. 'options' => $this->input->post('options'),
  375. );
  376. if (empty($field_id)) {
  377. $field_id = $this->rooms_model->field_add($field_data);
  378. $flashmsg = msgbox('info', "The {$field_data['name']} field has been added.");
  379. } else {
  380. $this->rooms_model->field_edit($field_id, $field_data);
  381. $flashmsg = msgbox('info', "The {$field_data['name']} field has been updated.");
  382. }
  383. $this->session->set_flashdata('saved', $flashmsg, TRUE);
  384. redirect('rooms/fields');
  385. }
  386. /**
  387. * Delete a field
  388. *
  389. */
  390. function delete_field($id = NULL)
  391. {
  392. $this->require_auth_level(ADMINISTRATOR);
  393. if ($this->input->post('id')) {
  394. $this->rooms_model->field_delete($this->input->post('id'));
  395. $flashmsg = msgbox('info', $this->lang->line('crbs_action_deleted'));
  396. return redirect('rooms/fields');
  397. }
  398. $this->data['action'] = 'rooms/delete_field';
  399. $this->data['id'] = $id;
  400. $this->data['cancel'] = 'rooms/fields';
  401. $row = $this->rooms_model->GetFields($id);
  402. $this->data['title'] = 'Delete Field ('.html_escape($row->name).')';
  403. // $this->data['showtitle'] = $this->data['title'];
  404. $this->data['body'] = $this->load->view('partials/deleteconfirm', $this->data, TRUE);
  405. $icons = iconbar($this->data['rooms_icons'], 'rooms/fields');
  406. $title = "<h2>{$this->data['title']}</h2>";
  407. $body = $this->load->view('partials/deleteconfirm', $this->data, TRUE);
  408. $this->data['body'] = $icons . $title . $body;
  409. return $this->render();
  410. }
  411. }