Advertisement
Guest User

Untitled

a guest
May 25th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.80 KB | None | 0 0
  1. <?php
  2. class Store_categories extends MX_Controller
  3. {
  4.  
  5. function __construct() {
  6. parent::__construct();
  7. }
  8.  
  9. function _get_full_cat_url($update_id)
  10. {
  11. $this->load->module('site_settings');
  12. $items_segments = $this->site_settings->_get_items_segments();
  13. $data = $this->fetch_data_from_db($update_id);
  14. $cat_url = $data['cat_url'];
  15.  
  16. $full_cat_url = base_url().$items_segments.$cat_url;
  17. return $full_cat_url;
  18. }
  19.  
  20. function view($update_id)
  21. {
  22. if (!is_numeric($update_id)) {
  23. redirect('site_security/not_allowed');
  24. }
  25.  
  26. $this->load->module('site_settings');
  27. $this->load->module('custom_pagination');
  28.  
  29. //fetch the category details
  30. $data = $this->fetch_data_from_db($update_id);
  31.  
  32. //count the items that belong to this category
  33. $use_limit = FALSE;
  34. $mysql_query = $this->_generate_mysql_query($update_id, $use_limit);
  35. $query = $this->_custom_query($mysql_query);
  36. $total_items = $query->num_rows();
  37.  
  38. //fetch the items for this page
  39. $use_limit = TRUE;
  40. $mysql_query = $this->_generate_mysql_query($update_id, $use_limit);
  41.  
  42. $pagination_data['template'] = 'public_bootstrap';
  43. $pagination_data['target_base_url'] = $this->get_target_pagination_base_url();
  44. $pagination_data['total_rows'] = $total_items;
  45. $pagination_data['offset_segment'] = 4;
  46. $pagination_data['limit'] = $this->get_limit();
  47. $data['pagination'] = $this->custom_pagination->_generate_pagination($pagination_data);
  48.  
  49. $pagination_data['offset'] = $this->get_offset();
  50. $data['showing_statement'] = $this->custom_pagination->get_showing_statement($pagination_data);
  51.  
  52. $data['item_segments'] = $this->site_settings->_get_item_segments();
  53. $data['currency_symbol'] = $this->site_settings->_get_currency_symbol();
  54. $data['query'] = $this->_custom_query($mysql_query);
  55. $data['update_id'] = $update_id;
  56. $data['flash'] = $this->session->flashdata('item');
  57. $data['view_module'] = "store_categories";
  58. $data['view_file'] = "view";
  59. $this->load->module('templates');
  60. $this->templates->public_bootstrap($data);
  61. }
  62.  
  63. function get_target_pagination_base_url()
  64. {
  65. $first_bit = $this->uri->segment(1);
  66. $second_bit = $this->uri->segment(2);
  67. $third_bit = $this->uri->segment(3);
  68. $target_base_url = base_url().$first_bit."/".$second_bit."/".$third_bit;
  69. return $target_base_url;
  70. }
  71.  
  72. function _generate_mysql_query($update_id, $use_limit)
  73. {
  74. //NOTE: use_limit can be TRUE or FALSE
  75.  
  76. $mysql_query = "
  77.  
  78. SELECT store_items.item_title,
  79. store_items.item_url,
  80. store_items.item_price,
  81. store_items.small_pic,
  82. store_items.was_price
  83. FROM store_cat_assign INNER JOIN store_items ON store_cat_assign.item_id = store_items.id
  84. WHERE store_cat_assign.cat_id=$update_id and store_items.status=1
  85.  
  86. ";
  87.  
  88. if ($use_limit==TRUE) {
  89. $limit = $this->get_limit();
  90. $offset = $this->get_offset();
  91. $mysql_query.= " limit ".$offset.", ".$limit;
  92. }
  93.  
  94. return $mysql_query;
  95.  
  96. }
  97.  
  98. function get_limit()
  99. {
  100. $limit = 20;
  101. return $limit;
  102. }
  103.  
  104. function get_offset()
  105. {
  106. $offset = $this->uri->segment(4);
  107. if (!is_numeric($offset)) {
  108. $offset = 0;
  109. }
  110. return $offset;
  111. }
  112.  
  113. function _get_cat_id_from_cat_url($cat_url)
  114. {
  115. $query = $this->get_where_custom('cat_url', $cat_url);
  116. foreach($query->result() as $row) {
  117. $cat_id = $row->id;
  118. }
  119.  
  120. if (!isset($cat_id)) {
  121. $cat_id = 0;
  122. }
  123.  
  124. return $cat_id;
  125. }
  126.  
  127. function _draw_top_nav()
  128. {
  129. $mysql_query = "select * from store_categories where parent_cat_id=0 order by priority";
  130. $query = $this->_custom_query($mysql_query);
  131. foreach($query->result() as $row) {
  132. $parent_categories[$row->id] = $row->cat_title;
  133. }
  134.  
  135. $this->load->module('site_settings');
  136. $items_segments = $this->site_settings->_get_items_segments();
  137. $data['target_url_start'] = base_url().$items_segments;
  138. $data['parent_categories'] = $parent_categories;
  139. $this->load->view('top_nav', $data);
  140. }
  141.  
  142. function _get_parent_cat_title($update_id)
  143. {
  144. $data = $this->fetch_data_from_db($update_id);
  145. $parent_cat_id = $data['parent_cat_id'];
  146. $parent_cat_title = $this->_get_cat_title($parent_cat_id);
  147. return $parent_cat_title;
  148. }
  149.  
  150. function _get_all_sub_cats_for_dropdown()
  151. {
  152. //NOTE: this gets used on store_cat_assign
  153. $mysql_query = "select * from store_categories where parent_cat_id!=0 order by parent_cat_id, cat_title";
  154. $query = $this->_custom_query($mysql_query);
  155. foreach($query->result() as $row) {
  156. $parent_cat_title = $this->_get_cat_title($row->parent_cat_id);
  157. $sub_categories[$row->id] = $parent_cat_title." > ".$row->cat_title;
  158. }
  159.  
  160. if (!isset($sub_categories)) {
  161. $sub_categories = "";
  162. }
  163.  
  164. return $sub_categories;
  165. }
  166.  
  167. function sort()
  168. {
  169.  
  170. $this->load->module('site_security');
  171. $this->site_security->_make_sure_is_admin();
  172.  
  173. $number = $this->input->post('number', TRUE);
  174. for ($i=1; $i <= $number; $i++) {
  175. $update_id = $_POST['order'.$i];
  176. $data['priority'] = $i;
  177. $this->_update($update_id, $data);
  178. }
  179.  
  180. }
  181.  
  182. function _draw_sortable_list($parent_cat_id)
  183. {
  184. $mysql_query = "select * from store_categories where parent_cat_id=$parent_cat_id order by priority";
  185. $data['query'] = $this->_custom_query($mysql_query);
  186. $this->load->view('sortable_list', $data);
  187. }
  188.  
  189. function _count_sub_cats($update_id)
  190. {
  191. //return the number of sub categories, belonging to THIS category
  192. $query = $this->get_where_custom('parent_cat_id', $update_id);
  193. $num_rows = $query->num_rows();
  194. return $num_rows;
  195. }
  196.  
  197. function _get_cat_title($update_id)
  198. {
  199. $data = $this->fetch_data_from_db($update_id);
  200. $cat_title = $data['cat_title'];
  201. return $cat_title;
  202. }
  203.  
  204. function fetch_data_from_post()
  205. {
  206. $data['cat_title'] = $this->input->post('cat_title', TRUE);
  207. $data['parent_cat_id'] = $this->input->post('parent_cat_id', TRUE);
  208. return $data;
  209. }
  210.  
  211. function fetch_data_from_db($update_id)
  212. {
  213.  
  214. if (!is_numeric($update_id)) {
  215. redirect('site_security/not_allowed');
  216. }
  217.  
  218. $query = $this->get_where($update_id);
  219. foreach($query->result() as $row) {
  220. $data['cat_title'] = $row->cat_title;
  221. $data['cat_url'] = $row->cat_url;
  222. $data['parent_cat_id'] = $row->parent_cat_id;
  223. }
  224.  
  225. if (!isset($data)) {
  226. $data = "";
  227. }
  228.  
  229. return $data;
  230. }
  231.  
  232. function _get_dropdown_options($update_id)
  233. {
  234.  
  235. if (!is_numeric($update_id)) {
  236. $update_id = 0;
  237. }
  238.  
  239. $options[''] = 'Please Select...';
  240.  
  241. //build an array of all the parent categories
  242. $mysql_query = "select * from store_categories where parent_cat_id=0 and id!=$update_id";
  243. $query = $this->_custom_query($mysql_query);
  244. foreach($query->result() as $row) {
  245. $options[$row->id] = $row->cat_title;
  246. }
  247. return $options;
  248. }
  249.  
  250. function create()
  251. {
  252.  
  253. $this->load->library('session');
  254. $this->load->module('site_security');
  255. $this->site_security->_make_sure_is_admin();
  256.  
  257. $update_id = $this->uri->segment(3);
  258. $submit = $this->input->post('submit', TRUE);
  259.  
  260. if ($submit=="Cancel") {
  261. redirect('store_categories/manage');
  262. }
  263.  
  264. if ($submit=="Submit") {
  265. //process the form
  266. $this->load->library('form_validation');
  267. $this->form_validation->set_rules('cat_title', 'Category Title', 'required|max_length[240]');
  268.  
  269. if ($this->form_validation->run() == TRUE) {
  270. //get the variables
  271. $data = $this->fetch_data_from_post();
  272. $data['cat_url'] = url_title($data['cat_title']);
  273.  
  274. if (is_numeric($update_id)) {
  275. //update the category details
  276. $this->_update($update_id, $data);
  277. $flash_msg = "The category details were successfully updated.";
  278. $value = '<div class="alert alert-success" role="alert">'.$flash_msg.'</div>';
  279. $this->session->set_flashdata('item', $value);
  280. redirect('store_categories/create/'.$update_id);
  281. } else {
  282. //insert a new category
  283. $this->_insert($data);
  284. $update_id = $this->get_max(); //get the ID of the new category
  285. $flash_msg = "The category was successfully added.";
  286. $value = '<div class="alert alert-success" role="alert">'.$flash_msg.'</div>';
  287. $this->session->set_flashdata('item', $value);
  288. redirect('store_categories/create/'.$update_id);
  289. }
  290. }
  291. }
  292.  
  293. if ((is_numeric($update_id)) && ($submit!="Submit")) {
  294. $data = $this->fetch_data_from_db($update_id);
  295. } else {
  296. $data = $this->fetch_data_from_post();
  297. }
  298.  
  299. if (!is_numeric($update_id)) {
  300. $data['headline'] = "Add New Category";
  301. } else {
  302. $data['headline'] = "Update Category";
  303. }
  304.  
  305. $data['options'] = $this->_get_dropdown_options($update_id);
  306. $data['num_dropdown_options'] = count($data['options']);
  307. $data['update_id'] = $update_id;
  308. $data['flash'] = $this->session->flashdata('item');
  309. $data['view_file'] = "create";
  310. $this->load->module('templates');
  311. $this->templates->admin($data);
  312. }
  313.  
  314. function manage()
  315. {
  316. $this->load->module('site_security');
  317. $this->site_security->_make_sure_is_admin();
  318.  
  319. $parent_cat_id = $this->uri->segment(3);
  320. if (!is_numeric($parent_cat_id)) {
  321. $parent_cat_id = 0;
  322. }
  323.  
  324. $data['sort_this'] = TRUE;
  325. $data['parent_cat_id'] = $parent_cat_id;
  326. $data['flash'] = $this->session->flashdata('item');
  327. $data['query'] = $this->get_where_custom('parent_cat_id', $parent_cat_id);
  328. $data['view_file'] = "manage";
  329. $this->load->module('templates');
  330. $this->templates->admin($data);
  331. }
  332.  
  333. function get($order_by)
  334. {
  335. $this->load->model('mdl_store_categories');
  336. $query = $this->mdl_store_categories->get($order_by);
  337. return $query;
  338. }
  339.  
  340. function get_with_limit($limit, $offset, $order_by)
  341. {
  342. if ((!is_numeric($limit)) || (!is_numeric($offset))) {
  343. die('Non-numeric variable!');
  344. }
  345.  
  346. $this->load->model('mdl_store_categories');
  347. $query = $this->mdl_store_categories->get_with_limit($limit, $offset, $order_by);
  348. return $query;
  349. }
  350.  
  351. function get_where($id)
  352. {
  353. if (!is_numeric($id)) {
  354. die('Non-numeric variable!');
  355. }
  356.  
  357. $this->load->model('mdl_store_categories');
  358. $query = $this->mdl_store_categories->get_where($id);
  359. return $query;
  360. }
  361.  
  362. function get_where_custom($col, $value)
  363. {
  364. $this->load->model('mdl_store_categories');
  365. $query = $this->mdl_store_categories->get_where_custom($col, $value);
  366. return $query;
  367. }
  368.  
  369. function _insert($data)
  370. {
  371. $this->load->model('mdl_store_categories');
  372. $this->mdl_store_categories->_insert($data);
  373. }
  374.  
  375. function _update($id, $data)
  376. {
  377. if (!is_numeric($id)) {
  378. die('Non-numeric variable!');
  379. }
  380.  
  381. $this->load->model('mdl_store_categories');
  382. $this->mdl_store_categories->_update($id, $data);
  383. }
  384.  
  385. function _delete($id)
  386. {
  387. if (!is_numeric($id)) {
  388. die('Non-numeric variable!');
  389. }
  390.  
  391. $this->load->model('mdl_store_categories');
  392. $this->mdl_store_categories->_delete($id);
  393. }
  394.  
  395. function count_where($column, $value)
  396. {
  397. $this->load->model('mdl_store_categories');
  398. $count = $this->mdl_store_categories->count_where($column, $value);
  399. return $count;
  400. }
  401.  
  402. function get_max()
  403. {
  404. $this->load->model('mdl_store_categories');
  405. $max_id = $this->mdl_store_categories->get_max();
  406. return $max_id;
  407. }
  408.  
  409. function _custom_query($mysql_query)
  410. {
  411. $this->load->model('mdl_store_categories');
  412. $query = $this->mdl_store_categories->_custom_query($mysql_query);
  413. return $query;
  414. }
  415.  
  416. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement