LabelController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2018年03月23日
  7. * 自定义标签控制器
  8. */
  9. namespace app\admin\controller\content;
  10. use core\basic\Controller;
  11. use app\admin\model\content\LabelModel;
  12. class LabelController extends Controller
  13. {
  14. private $model;
  15. public function __construct()
  16. {
  17. $this->model = new LabelModel();
  18. }
  19. // 自定义标签列表
  20. public function index()
  21. {
  22. // 修改参数配置
  23. if ($_POST) {
  24. foreach ($_POST as $key => $value) {
  25. if (preg_match('/^[\w\-]+$/', $key)) { // 带有违规字符时不带入查询
  26. $data = post($key);
  27. $data = str_replace("\r\n", "<br>", $data); // 多行文本时替换回车
  28. $this->model->modValue($key, $data);
  29. }
  30. }
  31. success('修改成功!', url('admin/Label/index'));
  32. }
  33. $this->assign('list', true);
  34. $this->assign('labels', $this->model->getList());
  35. $this->display('content/label.html');
  36. }
  37. // 自定义标签字段增加
  38. public function add()
  39. {
  40. if ($_POST) {
  41. // 获取数据
  42. $name = post('name', 'var');
  43. $description = post('description');
  44. $type = post('type');
  45. if (! $name) {
  46. alert_back('标签名称不能为空!');
  47. }
  48. if (! $description) {
  49. alert_back('标题描述不能为空!');
  50. }
  51. if (! $type) {
  52. alert_back('标签类型不能为空!');
  53. }
  54. // 检查标签名称
  55. if ($this->model->checkLabel("name='$name'")) {
  56. alert_back('该自定义标签称已经存在,不能再使用!');
  57. }
  58. // 构建数据
  59. $data = array(
  60. 'name' => $name,
  61. 'description' => $description,
  62. 'value' => '', // 添加时设置为空
  63. 'type' => $type,
  64. 'create_user' => session('username'),
  65. 'update_user' => session('username')
  66. );
  67. // 执行添加
  68. if ($this->model->addLabel($data)) {
  69. $this->log('修改自定义标签' . $name . '成功!');
  70. if (! ! $backurl = get('backurl')) {
  71. success('新增成功!', base64_decode($backurl));
  72. } else {
  73. success('新增成功!', url('admin/Label/index' . get_tab('t2'), false));
  74. }
  75. } else {
  76. $this->log('新增自定义标签' . $name . '失败!');
  77. error('新增失败!', url('admin/Label/index' . get_tab('t2'), false));
  78. }
  79. }
  80. }
  81. // 自定义标签字段删除
  82. public function del()
  83. {
  84. if (! $id = get('id', 'int')) {
  85. error('传递的参数值错误!', - 1);
  86. }
  87. if ($this->model->delLabel($id)) {
  88. $this->log('删除自定义标签' . $id . '成功!');
  89. success('删除成功!', url('admin/Label/index' . get_tab('t2'), false));
  90. } else {
  91. $this->log('删除自定义标签' . $id . '失败!');
  92. error('删除失败!', - 1);
  93. }
  94. }
  95. // 自定义标签字段修改
  96. public function mod()
  97. {
  98. if (! $id = get('id', 'int')) {
  99. error('传递的参数值错误!', - 1);
  100. }
  101. // 修改操作
  102. if ($_POST) {
  103. // 获取数据
  104. $name = post('name', 'var');
  105. $description = post('description');
  106. $type = post('type');
  107. if (! $name) {
  108. alert_back('标签名称不能为空!');
  109. }
  110. if (! $description) {
  111. alert_back('标签描述不能为空!');
  112. }
  113. if (! $type) {
  114. alert_back('标签类型不能为空!');
  115. }
  116. // 检查标签名称
  117. if ($this->model->checkLabel("name='$name' AND id<>$id")) {
  118. alert_back('该自定义标签名称已经存在,不能再使用!');
  119. }
  120. // 构建数据
  121. $data = array(
  122. 'name' => $name,
  123. 'description' => $description,
  124. 'type' => $type,
  125. 'update_user' => session('username')
  126. );
  127. // 执行添加
  128. if ($this->model->modLabel($id, $data)) {
  129. $this->log('修改自定义标签字段' . $id . '成功!');
  130. success('修改成功!', url('admin/Label/index' . get_tab('t2'), false));
  131. } else {
  132. location(- 1);
  133. }
  134. } else {
  135. $this->assign('mod', true);
  136. // 调取修改内容
  137. $result = $this->model->getLabel($id);
  138. if (! $result) {
  139. error('编辑的内容已经不存在!', - 1);
  140. }
  141. $this->assign('label', $result);
  142. $this->display('content/label.html');
  143. }
  144. }
  145. }