riandaka_

Untitled

Dec 4th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2.  
  3. class Workbook_model extends CI_Model
  4. {
  5. private $_table = "workbook";
  6.  
  7. public $workbook_id;
  8. public $stage_id;
  9. public $modul_id;
  10. public $employee_id;
  11. public $employee_name;
  12. public $status;
  13. public $image = "default.jpg";
  14.  
  15. public function rules()
  16. {
  17. return [
  18.  
  19. ['field' => 'stage_id',
  20. 'rules' => 'required'],
  21.  
  22. ['field' => 'modul_id',
  23. 'rules' => 'required'],
  24.  
  25. ['field' => 'employee_id',
  26. 'rules' => 'required'],
  27.  
  28. ['field' => 'employee_name',
  29. 'rules' => 'required'],
  30.  
  31. ['field' => 'progress',
  32. 'rules' => 'required'],
  33.  
  34. ['field' => 'status',
  35. 'rules' => 'required'],
  36.  
  37. ['field' => 'image',
  38. 'rules' => 'required'],
  39. ];
  40. }
  41.  
  42. public function getAll()
  43. {
  44. return $this->db->get($this->_table)->result();
  45. }
  46.  
  47. public function getById($id)
  48. {
  49. return $this->db->get_where($this->_table, ["workbook_id" => $id])->row();
  50. }
  51.  
  52. public function get_data_stage()
  53. {
  54. $query = $this->db->get('stage');
  55. return $query;
  56. }
  57.  
  58. public function save()
  59. {
  60. // $post = $this->input->post();
  61. // print_r($post); die;
  62. // $this->workbook_id = $post["workbook_id"];
  63. $data = array (
  64. "stage_id" => $this->input->post("stage_id"),
  65. "modul_id" => $this->input->post("modul_id"),
  66. "employee_id" => $this->input->post("employee_id"),
  67. "employee_name" => $this->input->post("employee_name"),
  68. "progress" => $this->input->post("progress"),
  69. "status" => $this->input->post("status"),
  70. "image" => $this->_uploadImage()
  71. );
  72.  
  73. $this->db->insert("workbook", $data);
  74. }
  75.  
  76. public function update()
  77. {
  78. $data = array (
  79. "workbook_id" => $this->input->post("workbook_id"),
  80. "stage_id" => $this->input->post("stage_id"),
  81. "modul_id" => $this->input->post("modul_id"),
  82. "employee_id" => $this->input->post("employee_id"),
  83. "employee_name" => $this->input->post("employee_name"),
  84. "progress" => $this->input->post("progress"),
  85. "status" => $this->input->post("status"),
  86. );
  87. print_r($data); die;
  88.  
  89. if (!empty($_FILES["image"]["name"])) {
  90. $this->image = $this->_uploadImage();
  91. } else {
  92. $this->image = $this->input->post["old_image"];
  93. }
  94.  
  95. $this->db->update("workbook", $data, array('workbook_id' => $this->input->post['workbook_id']));
  96. }
  97.  
  98. public function delete($id)
  99. {
  100. $this->_deleteImage($id);
  101. return $this->db->delete("workbook", array("workbook_id" => $id));
  102. }
  103.  
  104. private function _uploadImage()
  105. {
  106. $config['upload_path'] = './upload/workbook/';
  107. $config['allowed_types'] = 'gif|jpg|png';
  108. $config['file_name'] = $this->workbook_id;
  109. $config['overwrite'] = true;
  110. $config['max_size'] = 10240; // 10MB
  111. // $config['max_width'] = 1024;
  112. // $config['max_height'] = 768;
  113.  
  114. $this->load->library('upload', $config);
  115.  
  116. if ($this->upload->do_upload('image')) {
  117. return $this->upload->data("file_name");
  118. }
  119.  
  120. return "default.jpg";
  121. }
  122.  
  123. private function _deleteImage($id)
  124. {
  125. $workbook = $this->getById($id);
  126. if ($workbook->image != "default.jpg") {
  127. $filename = explode(".", $workbook->image)[0];
  128. return array_map('unlink', glob(FCPATH."upload/workbook/$filename.*"));
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment