PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/app/modules/events/controllers/Events.php

https://gitlab.com/buitenzorg812/garapic.cms
PHP | 301 lines | 210 code | 47 blank | 44 comment | 28 complexity | 9b68fbd200ef9fb240d1c47e73f12598 MD5 | raw file
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. class Events extends MX_Controller {
  4. private $table_name = 'event';
  5. private $url = 'events';
  6. private $perpage = 5;
  7. private $data = array();
  8. private $update = '';
  9. private $delete = '';
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. // for security check
  14. $this->menu->has_access();
  15. // for header
  16. $this->data = $this->header->button_header();
  17. $this->data['title'] = 'Events';
  18. // for button link
  19. $this->data['button_link'] = (($this->uri->segment(2) == 'add') || ($this->uri->segment(2) == 'update')) ? site_url($this->url) : site_url($this->url).'/add' ;
  20. // for debug purpose only
  21. $this->output->enable_profiler(false);
  22. }
  23. public function index( $offset = 0 )
  24. {
  25. // pagination
  26. $this->db
  27. ->select('a.id')
  28. ->from($this->table_name . ' a')
  29. ->join('event_lang b', 'b.id = a.id', 'left')
  30. ->group_by('b.id');
  31. $query = $this->db->get();
  32. $count = $query->num_rows();
  33. $config['per_page'] = $this->perpage;
  34. $this->pagination->initialize($this->general_model->pagination_rules(site_url($this->url), $count, $this->perpage));
  35. $this->data['pagination'] = $this->pagination->create_links();
  36. // get all data
  37. $this->db
  38. ->select('a.id, b.title, b.place, DATE_FORMAT(a.start_date,\'%d %M %Y\') start_date, DATE_FORMAT(a.due_date, \'%d %M %Y\') due_date')
  39. ->from($this->table_name . ' a')
  40. ->join('event_lang b', 'b.id = a.id', 'left')
  41. ->order_by('a.updated_at','desc')
  42. ->group_by('b.id')
  43. ->limit($this->perpage, $offset);
  44. $query = $this->db->get();
  45. // if not empty
  46. if($query->num_rows() > 0) {
  47. foreach($query->result() as $list) {
  48. // check for update access
  49. if($this->menu->crud_access('update')) {
  50. $this->update = '<a href="'.site_url($this->url.'/'.'update'.'/'.$list->id).'" class="btn btn-xs btn-default"><i class="fa fa-pencil"></i></a>';
  51. }
  52. // check for delete access
  53. if($this->menu->crud_access('delete')) {
  54. $this->delete = '<a href="'.site_url($this->url.'/'.'delete'.'/'.$list->id).'" class="delete btn btn-xs btn-default"><i class="fa fa-trash"></i></a>';
  55. }
  56. // return to table data
  57. $table_row = array(
  58. $list->title,
  59. $list->place,
  60. $list->start_date,
  61. $list->due_date
  62. );
  63. // check for additional link access
  64. if($this->menu->crud_access('update') || $this->menu->crud_access('delete')) {
  65. $table_row = array_merge($table_row, array($this->delete.' '.$this->update));
  66. }
  67. $this->table->add_row($table_row);
  68. }
  69. // generate table header
  70. $this->table->set_template(array('table_open' => '<table class="table">'));
  71. if($this->menu->crud_access('update') || $this->menu->crud_access('delete')) {
  72. $this->table->set_heading('Event', 'Place', 'From', 'Until', '');
  73. } else {
  74. $this->table->set_heading('Category', 'Place', 'From', 'Until');
  75. }
  76. // output
  77. $this->data['list'] = $this->table->generate();
  78. $this->data['content'] = $this->load->view('admin/list', $this->data, TRUE);
  79. } else {
  80. // if no data
  81. $this->data['content'] = $this->load->view('admin/blank', $this->data, TRUE);
  82. }
  83. // make an output
  84. $this->load->view('admin/content', $this->data);
  85. }
  86. // Add a new item
  87. public function add()
  88. {
  89. // check for write access
  90. $this->menu->check_access('write');
  91. // get language list
  92. $query = $this->db->get('lang');
  93. $this->data['language'] = $query->result();
  94. // validation rules
  95. foreach($this->data['language'] as $lang) {
  96. $this->form_validation->set_rules("title[".$lang->shortname."]", "Event in ".$lang->name, 'required');
  97. $this->form_validation->set_rules("place[".$lang->shortname."]", "Place in ".$lang->name, 'required');
  98. }
  99. $this->form_validation->set_rules("start", "Start Date", 'required');
  100. // if break the rules
  101. if ($this->form_validation->run() == FALSE) {
  102. $this->data['content'] = $this->load->view('events/add', $this->data, true);
  103. } else {
  104. // save to category first
  105. $record = array(
  106. 'status' => $this->input->post('status'),
  107. 'start_date' => date('Y-m-d', strtotime($this->input->post('start'))),
  108. 'due_date' => date('Y-m-d', strtotime($this->input->post('due'))),
  109. 'users_id' => $this->session->userdata('user_id'),
  110. 'created_at' => date('Y-m-d h:i:s'),
  111. 'updated_at' => date('Y-m-d h:i:s')
  112. );
  113. // start transaction
  114. $this->db->insert($this->table_name, $record);
  115. $id = $this->db->insert_id();
  116. // then repeat by language
  117. foreach($this->input->post('title') as $key => $val) {
  118. $record = array(
  119. 'id' => $id,
  120. 'lang' => $key,
  121. 'title' => $val,
  122. 'content' => $this->input->post('desc')[$key],
  123. 'place' => $this->input->post('place')[$key]
  124. );
  125. $this->db->trans_start();
  126. $this->db->insert('event_lang', $record);
  127. $this->db->trans_complete();
  128. }
  129. // check for status
  130. if ($this->db->trans_status() === FALSE) {
  131. $db_error = $this->db->error();
  132. $this->data['message'] = $db_error['message'];
  133. $this->data['content'] = $this->load->view('events/add', $this->data, true);
  134. } else {
  135. $this->data['message'] = 'Your record has been saved.';
  136. $this->data['content'] = $this->load->view('admin/redirect', $this->data, TRUE);
  137. }
  138. }
  139. // make an output
  140. $this->load->view('admin/content', $this->data);
  141. }
  142. // Add a new item
  143. public function update($id = NULL)
  144. {
  145. // check for write access
  146. $this->menu->check_access('update');
  147. // get language list
  148. $query = $this->db->get('lang');
  149. $this->data['language'] = $query->result();
  150. // create output by available language
  151. foreach($this->data['language'] as $lang) {
  152. // get current data
  153. $this->db
  154. ->select('a.*, b.*')
  155. ->from($this->table_name . ' a')
  156. ->join('event_lang b', 'b.id = a.id', 'left')
  157. ->where('a.id', $id)
  158. ->where('b.lang', $lang->shortname)
  159. ->order_by('a.updated_at','desc');
  160. $query = $this->db->get();
  161. // return data
  162. $this->data['form'][$lang->shortname] = $query->row();
  163. }
  164. // validation rules
  165. foreach($this->data['language'] as $lang) {
  166. $this->form_validation->set_rules("title[".$lang->shortname."]", "Event in ".$lang->name, 'required');
  167. $this->form_validation->set_rules("place[".$lang->shortname."]", "Place in ".$lang->name, 'required');
  168. }
  169. $this->form_validation->set_rules("start", "Start Date", 'required');
  170. // if break the rules
  171. if ($this->form_validation->run() == FALSE) {
  172. $this->data['content'] = $this->load->view('events/edit', $this->data, true);
  173. } else {
  174. // save to category first
  175. $data = array(
  176. 'status' => $this->input->post('status'),
  177. 'start_date' => date('Y-m-d', strtotime($this->input->post('start'))),
  178. 'due_date' => date('Y-m-d', strtotime($this->input->post('due'))),
  179. 'users_id' => $this->session->userdata('user_id'),
  180. 'created_at' => date('Y-m-d h:i:s'),
  181. 'updated_at' => date('Y-m-d h:i:s')
  182. );
  183. // start transaction
  184. $this->db->where('id', $this->input->post('id'));
  185. $this->db->update($this->table_name, $data);
  186. // then repeat by language
  187. foreach($this->input->post('title') as $key => $val) {
  188. $record = array(
  189. 'title' => $val,
  190. 'content' => $this->input->post('desc')[$key],
  191. 'place' => $this->input->post('place')[$key]
  192. );
  193. $this->db->trans_start();
  194. $this->db
  195. ->where('lang', $key)
  196. ->where('id', $id);
  197. $this->db->update('event_lang', $record);
  198. $this->db->trans_complete();
  199. }
  200. // check for status
  201. if ($this->db->trans_status() === FALSE) {
  202. $db_error = $this->db->error();
  203. $this->data['message'] = $db_error['message'];
  204. $this->data['content'] = $this->load->view('events/edit', $this->data, true);
  205. } else {
  206. $this->data['message'] = 'Your record has been saved.';
  207. $this->data['content'] = $this->load->view('admin/redirect', $this->data, TRUE);
  208. }
  209. }
  210. // make an output
  211. $this->load->view('admin/content', $this->data);
  212. }
  213. //Delete one item
  214. public function delete( $id = NULL )
  215. {
  216. // security check
  217. $this->menu->check_access('delete');
  218. // direct link
  219. $this->data['button_link'] = site_url('events');
  220. // transaction
  221. $this->db->trans_start();
  222. $this->db->where('id', $id);
  223. $this->db->delete($this->table_name);
  224. $this->db->trans_complete();
  225. if ($this->db->trans_status() === FALSE) {
  226. $message = 'Failed while removed your record.';
  227. } else {
  228. $this->db->where('id', $id);
  229. $this->db->delete('event_lang');
  230. $message = 'Your record has been removed.';
  231. }
  232. $this->data['message'] = $message;
  233. $this->data['content'] = $this->load->view('admin/redirect', $this->data, TRUE);
  234. $this->load->view('admin/content', $this->data);
  235. }
  236. public function getParent($parent_id)
  237. {
  238. $output = '-';
  239. if(isset($parent_id) && ($parent_id != 0)) {
  240. $this->db
  241. ->select('category')
  242. ->from($this->table_name)
  243. ->where('id', $parent_id);
  244. $parents = $this->db->get();
  245. if($parents->num_rows() > 0 ){
  246. $parent = $parents->row();
  247. $output = $parent->category;
  248. }
  249. }
  250. return $output;
  251. }
  252. }
  253. /* End of file Category.php */
  254. /* Location: ./application/modules/admin/controllers/Category.php */