ExtFieldController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2018年3月1日
  7. * 扩展字段控制器
  8. */
  9. namespace app\admin\controller\content;
  10. use core\basic\Controller;
  11. use app\admin\model\content\ExtFieldModel;
  12. class ExtFieldController extends Controller
  13. {
  14. private $model;
  15. public function __construct()
  16. {
  17. $this->model = new ExtFieldModel();
  18. }
  19. // 扩展字段列表
  20. public function index()
  21. {
  22. if ((! ! $id = get('id', 'int')) && $result = $this->model->getExtField($id)) {
  23. $this->assign('more', true);
  24. $this->assign('extfield', $result);
  25. } else {
  26. $this->assign('list', true);
  27. if (! ! ($field = get('field', 'var')) && ! ! ($keyword = get('keyword', 'vars'))) {
  28. $result = $this->model->findExtField($field, $keyword);
  29. } else {
  30. $result = $this->model->getList();
  31. }
  32. // 内容模型
  33. $models = model('admin.content.Model');
  34. $this->assign('models', $models->getSelect());
  35. $this->assign('extfields', $result);
  36. }
  37. $this->display('content/extfield.html');
  38. }
  39. // 扩展字段增加
  40. public function add()
  41. {
  42. if ($_POST) {
  43. // 获取数据
  44. $mcode = post('mcode');
  45. $name = post('name', 'var');
  46. $type = post('type', 'int');
  47. if (! ! $value = post('value')) {
  48. $value = str_replace("\r\n", ",", $value); // 替换回车
  49. $value = str_replace(",", ",", $value); // 替换中文逗号分割符
  50. }
  51. $description = post('description');
  52. $sorting = post('sorting', 'int');
  53. if (! $mcode) {
  54. alert_back('内容模型不能为空!');
  55. }
  56. if (! $name) {
  57. alert_back('字段名称不能为空!');
  58. } else {
  59. $name = "ext_" . $name;
  60. }
  61. if (! $type) {
  62. alert_back('字段类型不能为空!');
  63. }
  64. if (! $description) {
  65. alert_back('字段描述不能为空!');
  66. }
  67. // 构建数据
  68. $data = array(
  69. 'mcode' => $mcode,
  70. 'name' => $name,
  71. 'type' => $type,
  72. 'value' => $value,
  73. 'description' => $description,
  74. 'sorting' => $sorting
  75. );
  76. // 字段类型及长度
  77. switch ($type) {
  78. case '2': // 多行
  79. $mysql = 'varchar(1000)';
  80. $sqlite = 'TEXT(1000)';
  81. break;
  82. case '7': // 时间日期
  83. $mysql = 'datetime';
  84. $sqlite = 'TEXT';
  85. break;
  86. case '8': // 编辑器
  87. $mysql = 'TEXT';
  88. $sqlite = 'TEXT(10000)';
  89. break;
  90. case '10': // 多图
  91. $mysql = 'varchar(1000)';
  92. $sqlite = 'TEXT(1000)';
  93. break;
  94. default:
  95. $mysql = 'varchar(200)';
  96. $sqlite = 'TEXT(200)';
  97. }
  98. // 字段不存在时创建
  99. if (! $this->model->isExistField($name)) {
  100. if (get_db_type() == 'sqlite') {
  101. $result = $this->model->amd("ALTER TABLE ay_content_ext ADD COLUMN $name $sqlite NULL");
  102. } else {
  103. $result = $this->model->amd("ALTER TABLE ay_content_ext ADD $name $mysql NULL COMMENT '$description'");
  104. }
  105. } elseif ($this->model->checkExtField($name)) { // 字段存在且已使用则 报错
  106. alert_back('字段已经存在,不能重复添加!');
  107. }
  108. // 执行扩展字段记录添加
  109. if ($this->model->addExtField($data)) {
  110. $this->log('新增扩展字段成功!');
  111. if (! ! $backurl = get('backurl')) {
  112. success('新增成功!', base64_decode($backurl));
  113. } else {
  114. success('新增成功!', url('/admin/ExtField/index'));
  115. }
  116. } else {
  117. $this->log('新增扩展字段失败!');
  118. error('新增失败!', - 1);
  119. }
  120. }
  121. }
  122. // 扩展字段删除
  123. public function del()
  124. {
  125. if (! $id = get('id', 'int')) {
  126. error('传递的参数值错误!', - 1);
  127. }
  128. $name = $this->model->getExtFieldName($id);
  129. if ($this->model->delExtField($id)) {
  130. // mysql数据库执行字段删除,sqlite暂时不支持
  131. if (! ! $name) {
  132. if (get_db_type() == 'mysql') {
  133. $result = $this->model->amd("ALTER TABLE ay_content_ext DROP COLUMN $name");
  134. }
  135. }
  136. $this->log('删除扩展字段' . $id . '成功!');
  137. success('删除成功!', - 1);
  138. } else {
  139. $this->log('删除扩展字段' . $id . '失败!');
  140. error('删除失败!', - 1);
  141. }
  142. }
  143. // 扩展字段修改
  144. public function mod()
  145. {
  146. if (! $id = get('id', 'int')) {
  147. error('传递的参数值错误!', - 1);
  148. }
  149. // 单独修改状态
  150. if (($field = get('field', 'var')) && ! is_null($value = get('value', 'var'))) {
  151. if ($this->model->modExtField($id, "$field='$value',update_user='" . session('username') . "'")) {
  152. location(- 1);
  153. } else {
  154. alert_back('修改失败!');
  155. }
  156. }
  157. // 修改操作
  158. if ($_POST) {
  159. // 获取数据
  160. $mcode = post('mcode');
  161. $type = post('type');
  162. if (! ! $value = post('value')) {
  163. $value = str_replace("\r\n", ",", $value); // 替换回车
  164. $value = str_replace(",", ",", $value); // 替换中文逗号分割符
  165. }
  166. $description = post('description');
  167. $sorting = post('sorting', 'int');
  168. if (! $mcode) {
  169. alert_back('内容模型不能为空!');
  170. }
  171. if (! $description) {
  172. alert_back('字段描述不能为空!');
  173. }
  174. // 构建数据
  175. $data = array(
  176. 'mcode' => $mcode,
  177. 'type' => $type,
  178. 'value' => $value,
  179. 'description' => $description,
  180. 'sorting' => $sorting
  181. );
  182. // 执行修改
  183. if ($this->model->modExtField($id, $data)) {
  184. $this->log('修改扩展字段' . $id . '成功!');
  185. if (! ! $backurl = get('backurl')) {
  186. success('修改成功!', base64_decode($backurl));
  187. } else {
  188. success('修改成功!', url('/admin/ExtField/index'));
  189. }
  190. } else {
  191. location(- 1);
  192. }
  193. } else {
  194. // 调取修改内容
  195. $this->assign('mod', true);
  196. if (! $result = $this->model->getExtField($id)) {
  197. error('编辑的内容已经不存在!', - 1);
  198. }
  199. // 内容模型
  200. $models = model('admin.content.Model');
  201. $this->assign('models', $models->getSelect());
  202. $this->assign('extfield', $result);
  203. $this->display('content/extfield.html');
  204. }
  205. }
  206. }