product.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. ! defined('BASEPATH') && exit('No direct script access allowed');
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', '1');
  5. class Product extends MY_Admin_Controller
  6. {
  7. protected $check_access = FALSE;
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. $this->load->model('admin_model', 'admin');
  12. $this->load->model('product_model', 'product');
  13. $this->load->model('type_model', 'type');
  14. }
  15. public function index()
  16. {
  17. if ( ! $this->checkAdminLogin()) {
  18. $this->load->view('adminLoginView');
  19. } else {
  20. $items = $this->product->all();
  21. $type = $this->type->all_index_id();
  22. $data['type'] = $type;
  23. $data['items'] = $items;
  24. $this->load->view('productView', $data);
  25. }
  26. }
  27. public function add()
  28. {
  29. $type = $this->type->all();
  30. $data['type'] = $type;
  31. $action = $this->input->get('action');
  32. if ($action == 'insert') {
  33. $_POST[] = $this->security->xss_clean($_POST);
  34. $type_str = implode(',', $_POST['type']);
  35. $parm['type'] = $type_str;
  36. $parm['title'] = trim($_POST['title']);
  37. $parm['pid'] = trim($_POST['pid']);
  38. $parm['level'] = 1;
  39. $parm['publishtime'] = strtotime(trim($_POST['publishtime']));
  40. $parm['addtime'] = time();
  41. foreach ($_POST['img'] as $key => $value) {
  42. $t['img'] = $value;
  43. $json[] = $t;
  44. }
  45. $parm['img'] = json_encode($json);
  46. $parm['desc'] = str_replace("\n", "<br>", trim($_POST['desc']));
  47. $parm['sence'] = trim($_POST['sence']);
  48. if ($this->product->insert($parm)) {
  49. exit('<script language="javascript">alert("添加成功"); document.location.href="' . ADMIN_PATH . '/product";</script>');
  50. } else {
  51. exit('<script language="javascript">alert("添加失败"); document.location.href="' . ADMIN_PATH . '/product";</script>');
  52. }
  53. }
  54. $this->load->view('productAddView', $data);
  55. }
  56. public function edit()
  57. {
  58. $id = $this->input->get('id', TRUE);
  59. $action = $this->input->get('action');
  60. $type = $this->type->all();
  61. $data['type'] = $type;
  62. if ($action == 'update') {
  63. $_POST[] = $this->security->xss_clean($_POST);
  64. $id = $_POST['id'];
  65. $type_str = implode(',', $_POST['type']);
  66. $parm['type'] = $type_str;
  67. $parm['title'] = trim($_POST['title']);
  68. $parm['pid'] = trim($_POST['pid']);
  69. $parm['level'] = 1;
  70. $parm['publishtime'] = strtotime(trim($_POST['publishtime']));
  71. $parm['addtime'] = time();
  72. foreach ($_POST['img'] as $key => $value) {
  73. $t['img'] = $value;
  74. $json[] = $t;
  75. }
  76. $parm['img'] = json_encode($json);
  77. $parm['desc'] = str_replace("\n", "<br>", trim($_POST['desc']));
  78. $parm['sence'] = trim($_POST['sence']);
  79. if ($this->product->update($id, $parm)) {
  80. exit('<script language="javascript">alert("更新成功"); document.location.href="' . ADMIN_PATH . '/product";</script>');
  81. } else {
  82. exit('<script language="javascript">alert("更新失败"); document.location.href="' . ADMIN_PATH . '/product";</script>');
  83. }
  84. }
  85. $item = $this->product->get($id);
  86. $item['img'] = json_decode($item['img'], true);
  87. $data['item'] = $item;
  88. $this->load->view('productEditView', $data);
  89. }
  90. public function del()
  91. {
  92. $id = $this->input->get('id', TRUE);
  93. if ($this->product->delete($id)) {
  94. exit('<script language="javascript">alert("删除成功"); document.location.href="' . ADMIN_PATH . '/product";</script>');
  95. } else {
  96. exit('<script language="javascript">alert("删除失败"); document.location.href="' . ADMIN_PATH . '/product";</script>');
  97. }
  98. }
  99. }