Advertisement
Guest User

Category_controller

a guest
Dec 10th, 2022
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. <?php
  2.  
  3. defined('BASEPATH') OR exit('No direct script access allowed');
  4.  
  5. class Category extends MY_Controller {
  6.  
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. $role = $this->session->userdata('role');
  11. if ($role != 'admin') {
  12. redirect(base_url('/'));
  13. return;
  14. }
  15. }
  16.  
  17.  
  18. public function index($page = null)
  19. {
  20. $data['title'] = 'Admin: Category';
  21. $data['content'] = $this->category->paginate($page)->get();
  22. $data['total_rows'] = $this->category->count();
  23. $data['pagination'] = $this->category->makePagination(
  24. base_url('category'), 2, $data['total_rows']
  25. );
  26. $data['page'] = 'pages/category/index';
  27.  
  28. $this->view($data);
  29. }
  30.  
  31. public function search($page = null)
  32. {
  33. if (isset($_POST['keyword'])) {
  34. $this->session->set_userdata('keyword', $this->input->post('keyword'));
  35. } else {
  36. redirect(base_url('category'));
  37. }
  38.  
  39. $keyword = $this->session->userdata('keyword');
  40. $data['title'] = 'Admin: Category';
  41. $data['content'] = $this->category->like('title', $keyword)->paginate($page)->get();
  42. $data['total_rows'] = $this->category->like('title', $keyword)->count();
  43. $data['pagination'] = $this->category->makePagination(
  44. base_url('category/search'), 3, $data['total_rows']
  45. );
  46. $data['page'] = 'pages/category/index';
  47.  
  48. $this->view($data);
  49. }
  50.  
  51. public function reset()
  52. {
  53. $this->session->unset_userdata('keyword');
  54. redirect(base_url('category'));
  55. }
  56.  
  57. public function create()
  58. {
  59. if (!$_POST) {
  60. $input = (object) $this->category->getDefaultValues();
  61. } else {
  62. $input = (object) $this->input->post(null, true);
  63. }
  64.  
  65. if (!$this->category->validate()) {
  66. $data['title'] = 'Tambah Kategori';
  67. $data['input'] = $input;
  68. $data['form_action'] = base_url('category/create');
  69. $data['page'] = 'pages/category/form';
  70.  
  71. $this->view($data);
  72. return;
  73. }
  74.  
  75. if ($this->category->create($input)) {
  76. $this->session->set_flashdata('success', 'Data berhasil disimpan!');
  77. } else {
  78. $this->session->set_flashdata('error', 'Oops! Terjadi suatu kesalahan');
  79. }
  80.  
  81. redirect(base_url('category'));
  82. }
  83.  
  84. public function edit($id)
  85. {
  86. $data['content'] = $this->category->where('id', $id)->first();
  87.  
  88. if (! $data['content']) {
  89. $this->session->set_flashdata('warning', 'Maaf! Data tidak ditemukan!');
  90. redirect(base_url('category'));
  91. }
  92.  
  93. if (!$_POST) {
  94. $data['input'] = $data['content'];
  95. } else {
  96. $data['input'] = (object) $this->input->post(null, true);
  97. }
  98.  
  99. if (!$this->category->validate()) {
  100. $data['title'] = 'Ubah Kategori';
  101. $data['form_action'] = base_url("category/edit/$id");
  102. $data['page'] = 'pages/category/form';
  103.  
  104. $this->view($data);
  105. return;
  106. }
  107.  
  108. if ($this->category->where('id', $id)->update($data['input'])) {
  109. $this->session->set_flashdata('success', 'Data berhasil diperbaharui!');
  110. } else {
  111. $this->session->set_flashdata('error', 'Oops! Terjadi suatu kesalahan.');
  112. }
  113.  
  114. redirect(base_url('category'));
  115. }
  116.  
  117. public function delete($id)
  118. {
  119. if (!$_POST) {
  120. redirect(base_url('category'));
  121. }
  122.  
  123. if (! $this->category->where('id', $id)->first()) {
  124. $this->session->set_flashdata('warning', 'Maaf! Data tidak ditemukan.');
  125. redirect(base_url('category'));
  126. }
  127.  
  128. if ($this->category->where('id', $id)->delete()) {
  129. $this->session->set_flashdata('success', 'Data sudah berhasil dihapus!');
  130. } else {
  131. $this->session->set_flashdata('error', 'Oops! Terjadi suatu kesalahan.');
  132. }
  133.  
  134. redirect(base_url('category'));
  135. }
  136.  
  137. public function unique_slug()
  138. {
  139. $slug = $this->input->post('slug');
  140. $id = $this->input->post('id');
  141. $category = $this->category->where('slug', $slug)->first();
  142.  
  143. if ($category) {
  144. if ($id == $category->id) {
  145. return true;
  146. }
  147. $this->load->library('form_validation');
  148. $this->form_validation->set_message('unique_slug', '%s sudah digunakan!');
  149. return false;
  150. }
  151.  
  152. return true;
  153. }
  154.  
  155. }
  156.  
  157. /* End of file Category.php */
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement