fadlyshafa

Untitled

Mar 20th, 2020
191
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. class Subcategory1 extends CI_Controller
  4. {
  5. function __construct() {
  6. parent::__construct();
  7. $this->load->model('subcategory1_model');
  8. $this->load->model('log_model');
  9. }
  10. public function index(){
  11. $data['data'] = $this->subcategory1_model->getSubcategory();
  12. $this->load->view('subcategory1/list',$data);
  13. }
  14. /*
  15. call add subcategory view to add subcategory
  16. */
  17. public function add(){
  18. $data= $this->getCategory();
  19. $this->load->view('subcategory1/add',$data);
  20. }
  21. /*
  22. this function is used to get category list to select
  23. */
  24. public function getCategory(){
  25. $data['data'] = $this->subcategory1_model->getCategory();
  26. return $data;
  27. }
  28. /*
  29. this function is used to add subcategory record in database
  30. */
  31. public function addSubcategory(){
  32. $this->form_validation->set_rules('subcategory_name', 'Subcategory Name', 'trim|required|min_length[3]|callback_alpha_dash_space');
  33. $this->form_validation->set_rules('category', 'Category', 'trim|required|greater_than[0]');
  34. if ($this->form_validation->run() == FALSE)
  35. {
  36. $this->add();
  37. }
  38. else
  39. {
  40. // $subcategory_code = $this->subcategory_model->getMaxId();
  41. $data = array(
  42. "category_id" => $this->input->post('category'),
  43. "sub_category_code" => $this->input->post('subcategory_code'),
  44. "sub_category_name" => $this->input->post('subcategory_name')
  45. );
  46.  
  47. if($id = $this->subcategory1_model->addModel($data)){
  48. $log_data = array(
  49. 'user_id' => $this->session->userdata('user_id'),
  50. 'table_id' => $id,
  51. 'message' => 'Subcategory Inserted'
  52. );
  53. $this->log_model->insert_log($log_data);
  54. redirect('subcategory1');
  55. }
  56. else{
  57. $this->session->set_flashdata('fail', 'Subcategory can not be Inserted.');
  58. redirect("subcategory1",'refresh');
  59. }
  60. }
  61. }
  62. /*
  63. call edit view to edit record
  64. */
  65. public function edit($id){
  66. $data['category'] = $this->subcategory1_model->getCategory1();
  67. $data['data'] = $this->subcategory1_model->getRecord($id);
  68. $this->load->view('subcategory1/edit',$data);
  69. }
  70. /*
  71. this function is used to save edited record in database
  72. */
  73. public function editSubcategory(){
  74. $id = $this->input->post('id');
  75. $this->form_validation->set_rules('subcategory_name', 'Subcategory Name', 'trim|required|min_length[3]|callback_alpha_dash_space');
  76. $this->form_validation->set_rules('category', 'Category', 'trim|required|greater_than[0]');
  77. if ($this->form_validation->run() == FALSE)
  78. {
  79. $this->edit($id);
  80. }
  81. else
  82. {
  83. $data = array(
  84. "category_id" => $this->input->post('category'),
  85. "sub_category_name" => $this->input->post('subcategory_name')
  86. );
  87. if($this->subcategory1_model->editModel($data,$id)){
  88. $proses = $this->subcategory1_model->editModel($data,$id);
  89. var_dump($proses);
  90. die;
  91.  
  92. $log_data = array(
  93. 'user_id' => $this->session->userdata('user_id'),
  94. 'table_id' => $id,
  95. 'message' => 'Subcategory Updated'
  96. );
  97. $this->log_model->insert_log($log_data);
  98. redirect('subcategory1');
  99. }
  100. else{
  101. $this->session->set_flashdata('fail', 'Subcategory1 can not be Updated.');
  102. redirect("subcategory1",'refresh');
  103. }
  104. }
  105. }
  106. /*
  107. This function is to delete subcategory from database
  108. */
  109. public function delete($id){
  110. if($this->subcategory1_model->deleteModel($id)){
  111. $log_data = array(
  112. 'user_id' => $this->session->userdata('user_id'),
  113. 'table_id' => $id,
  114. 'message' => 'Subcategory Deleted'
  115. );
  116. $this->log_model->insert_log($log_data);
  117. redirect('subcategory1','refresh');
  118. }
  119. else{
  120. $this->session->set_flashdata('fail', 'Subcategory1 can not be Deleted.');
  121. redirect("subcategory1",'refresh');
  122. }
  123. }
  124. function alpha_dash_space($str) {
  125. if (! preg_match("/^([-a-zA-Z ])+$/i", $str))
  126. {
  127. $this->form_validation->set_message('alpha_dash_space', 'The %s field may only contain alpha, spaces and dashes.');
  128. return FALSE;
  129. }
  130. else
  131. {
  132. return TRUE;
  133. }
  134. }
  135. }
  136. ?>
RAW Paste Data