Diyform.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. namespace addons\cms\controller\api;
  3. use addons\cms\library\Service;
  4. use addons\cms\model\Diydata;
  5. use addons\cms\model\Diyform as DiyformModel;
  6. use think\Config;
  7. use think\Exception;
  8. use think\Hook;
  9. /**
  10. * 自定义表单控制器
  11. */
  12. class Diyform extends Base
  13. {
  14. protected $noNeedLogin = ['*'];
  15. protected $diyform = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $diyname = $this->request->param('diyname');
  20. if (!$diyname) {
  21. //如果表单为空则取第一个表单
  22. $diyform = DiyformModel::where('status', 'normal')->order('id', 'asc')->find();
  23. } else {
  24. if ($diyname && !is_numeric($diyname)) {
  25. $diyform = DiyformModel::getByDiyname($diyname);
  26. } else {
  27. $id = $diyname ? $diyname : $this->request->get('id', '');
  28. $diyform = DiyformModel::get($id);
  29. }
  30. }
  31. if (!$diyform || $diyform['status'] != 'normal') {
  32. $this->error(__('表单未找到'));
  33. }
  34. if ($diyform['needlogin'] && !$this->auth->id) {
  35. $this->error(__('请登录后再操作'));
  36. }
  37. $diyform->hidden(['admin_id', 'table', 'posttpl', 'listtpl', 'showtpl']);
  38. $this->diyform = $diyform;
  39. }
  40. /**
  41. * 获取字段
  42. * @return void
  43. */
  44. public function index()
  45. {
  46. $id = $this->request->request("id/d"); //编辑的表单id
  47. $diydata = new Diydata([], $this->diyform);
  48. if ($id) {
  49. if (!$this->auth->isLogin()) {
  50. $this->error("请登录后再操作");
  51. }
  52. $diydata = $diydata->find($id);
  53. if (!$diydata) {
  54. $this->error("未找到指定数据");
  55. }
  56. if ($diydata['user_id'] != $this->auth->id) {
  57. $this->error("无法进行越权操作");
  58. }
  59. }
  60. $fields = DiyformModel::getDiyformFields($this->diyform['id'], $diydata->toArray());
  61. foreach ($fields as $item) {
  62. if ($item['type'] == 'array') {
  63. $item->value = html_entity_decode($item->value);
  64. }
  65. }
  66. $this->success('', [
  67. 'diyform' => $this->diyform,
  68. 'fields' => $fields
  69. ]);
  70. }
  71. /**
  72. * 提交或修改表单数据
  73. * @return void
  74. */
  75. public function postForm()
  76. {
  77. $diyform = $this->diyform;
  78. $id = $this->request->post("id/d"); //编辑的表单id
  79. $diydata = new Diydata([], $diyform);
  80. if ($id) {
  81. if (!$this->auth->isLogin()) {
  82. $this->error("请登录后再操作");
  83. }
  84. $diydata = $diydata->find($id);
  85. if (!$diydata) {
  86. $this->error("未找到指定数据");
  87. }
  88. if ($diydata['user_id'] != $this->auth->id) {
  89. $this->error("无法进行越权操作");
  90. }
  91. if(!$diyform['isedit']){
  92. $this->error("表单不允许编辑!");
  93. }
  94. }
  95. $config = get_addon_config('cms');
  96. $row = $this->request->post('', '', 'trim,xss_clean');
  97. unset($row['id']);
  98. $fields = DiyformModel::getDiyformFields($diyform['id']);
  99. foreach ($fields as $index => $field) {
  100. if ($field['isrequire'] && (!isset($row[$field['name']]) || $row[$field['name']] == '')) {
  101. $this->error("{$field['title']}不能为空!");
  102. }
  103. }
  104. $row['user_id'] = $this->auth->id;
  105. foreach ($row as $index => &$value) {
  106. if (is_array($value) && isset($value['field'])) {
  107. $value = json_encode(\app\common\model\Config::getArrayData($value), JSON_UNESCAPED_UNICODE);
  108. } else {
  109. $value = is_array($value) ? implode(',', $value) : $value;
  110. }
  111. }
  112. $diydata['status'] = 'hidden';
  113. try {
  114. $diydata->allowField(true)->save($row);
  115. } catch (\Exception $e) {
  116. $this->error("发生错误:" . $e->getMessage());
  117. }
  118. //发送通知
  119. Service::notice('CMS收到新的' . $diyform['name'], $config['auditnotice'], $config['noticetemplateid']);
  120. $this->success($diyform['successtips'] ? $diyform['successtips'] : '提交成功!');
  121. }
  122. public function formList()
  123. {
  124. $config = get_addon_config('cms');
  125. $diyform = $this->diyform;
  126. $filter = $this->request->get('filter/a', []);
  127. $orderby = $this->request->get('orderby', '');
  128. $orderway = $this->request->get('orderway', '', 'strtolower');
  129. $multiple = $this->request->get('multiple/d', 0);
  130. $keyword = $this->request->get('keyword');
  131. $params = [];
  132. $filter = $this->request->get();
  133. $filter = array_diff_key($filter, array_flip(['orderby', 'orderway', 'page', 'multiple']));
  134. if (isset($filter['filter'])) {
  135. $filter = array_merge($filter, $filter['filter']);
  136. }
  137. if ($filter) {
  138. $params['filter'] = $filter;
  139. }
  140. if ($orderby) {
  141. $params['orderby'] = $orderby;
  142. }
  143. if ($orderway) {
  144. $params['orderway'] = $orderway;
  145. }
  146. if ($multiple) {
  147. $params['multiple'] = $multiple;
  148. }
  149. //默认排序字段
  150. $orders = [
  151. ['name' => 'default', 'field' => 'createtime DESC,id DESC', 'title' => __('Default')],
  152. ];
  153. //合并特殊筛选字段
  154. $orders = array_merge($orders, $diyform->getOrderFields());
  155. //获取过滤列表
  156. list($filterList, $filter, $params, $fields, $multiValueFields, $fieldsList) = Service::getFilterList('diyform', $diyform['id'], $filter, $params, $multiple);
  157. //获取排序列表
  158. list($orderList, $orderby, $orderway) = Service::getOrderList($orderby, $orderway, $orders, $params, $fieldsList);
  159. //获取过滤的条件和绑定参数
  160. list($filterWhere, $filterBind) = Service::getFilterWhereBind($filter, $multiValueFields, $multiple);
  161. $auth = $this->auth;
  162. $model = new Diydata([], $diyform);
  163. $pageList = $model
  164. ->where($filterWhere)
  165. ->bind($filterBind)
  166. ->where(function ($query) use ($diyform, $auth) {
  167. //用户过滤模式
  168. //如果是仅用户自己消息可见
  169. if ($diyform['usermode'] == 'user') {
  170. $query->where('user_id', $auth->id);
  171. }
  172. })
  173. ->where(function ($query) use ($diyform, $auth) {
  174. //状态过滤模式
  175. if ($diyform['statusmode'] === 'normal') {
  176. if ($auth->id) {
  177. $query->whereRaw("user_id='" . intval($auth->id) . "' OR status='normal'");
  178. } else {
  179. $query->where('status', 'normal');
  180. }
  181. }
  182. })
  183. ->where(function ($query) use ($diyform, $keyword) {
  184. $field = '';
  185. $fieldArr = explode(',', $diyform->fields);
  186. if (in_array('name', $fieldArr)) {
  187. $field = 'name';
  188. } elseif (in_array('title', $fieldArr)) {
  189. $field = 'title';
  190. } elseif (in_array('content', $fieldArr)) {
  191. $field = 'content';
  192. }
  193. if ($keyword && $field) {
  194. $query->where($field, 'like', '%' . $keyword . '%');
  195. }
  196. })
  197. ->order($orderby, $orderway)
  198. ->paginate(15);
  199. foreach ($pageList as $item) {
  200. if (isset($item['images']) && !empty($item['images'])) {
  201. $images = explode(',', $item->images);
  202. foreach ($images as &$res) {
  203. $res = cdnurl($res, true);
  204. }
  205. unset($res);
  206. $item->images = $images;
  207. }
  208. if (isset($item['image']) && !empty($item['image'])) {
  209. $image = explode(',', $item->image);
  210. foreach ($image as &$res) {
  211. $res = cdnurl($res, true);
  212. }
  213. unset($res);
  214. $item->image = $image;
  215. }
  216. $item->content = mb_substr(strip_tags($item->content), 0, 80);
  217. }
  218. $this->success('', [
  219. 'pageList' => $pageList,
  220. 'orderList' => $orderList,
  221. 'filterList' => $filterList
  222. ]);
  223. }
  224. /**
  225. * 查看详情
  226. * @return string
  227. */
  228. public function show()
  229. {
  230. $diyform = $this->diyform;
  231. $id = $this->request->param('id/d');
  232. $auth = $this->auth;
  233. $model = new Diydata([], $diyform);
  234. $diydata = $model
  235. ->where('id', $id)
  236. ->where(function ($query) use ($diyform, $auth) {
  237. //用户过滤模式
  238. //如果是仅用户自己消息可见
  239. if ($diyform['usermode'] == 'user') {
  240. $query->where('user_id', $auth->id);
  241. }
  242. })
  243. ->where(function ($query) use ($diyform, $auth) {
  244. //状态过滤模式
  245. if ($diyform['statusmode'] === 'normal') {
  246. if ($auth->id) {
  247. $query->whereRaw("user_id='" . intval($auth->id) . "' OR status='normal'");
  248. } else {
  249. $query->where('status', 'normal');
  250. }
  251. }
  252. })
  253. ->find();
  254. if (!$diydata) {
  255. $this->error("数据未找到或正在审核");
  256. }
  257. $fields = DiyformModel::getDiyformFields($this->diyform['id'], $diydata->toArray());
  258. foreach ($fields as $item) {
  259. $setting = $item->setting;
  260. unset($setting['table']);
  261. $item->setting = $setting;
  262. if (in_array($item['type'], ['image', 'file'])) {
  263. $item->value = cdnurl($item->value, true);
  264. }
  265. if (in_array($item['type'], ['images', 'files'])) {
  266. $ifs = explode(',', $item->value);
  267. foreach ($ifs as &$res) {
  268. $res = cdnurl($res, true);
  269. }
  270. $item->value = $ifs;
  271. }
  272. if ($item['type'] == 'array') {
  273. $item->value = json_decode(html_entity_decode($item->value), true);
  274. }
  275. }
  276. $this->success('', [
  277. 'fieldsList' => $fields,
  278. 'diydata' => $diydata,
  279. 'diyform' => $diyform
  280. ]);
  281. }
  282. }