Diyform.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace addons\cms\controller;
  3. use addons\cms\library\Service;
  4. use addons\cms\model\Diydata;
  5. use addons\cms\model\Diyform as DiyformModel;
  6. use addons\cms\model\Fields;
  7. use think\Config;
  8. use think\Exception;
  9. use think\Hook;
  10. /**
  11. * 自定义表单控制器
  12. * Class Diyform
  13. * @package addons\cms\controller
  14. */
  15. class Diyform extends Base
  16. {
  17. protected $diyform = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $diyname = $this->request->param('diyname');
  22. if ($diyname && !is_numeric($diyname)) {
  23. $diyform = DiyformModel::getByDiyname($diyname);
  24. } else {
  25. $id = $diyname ? $diyname : $this->request->get('id', '');
  26. $diyform = DiyformModel::get($id);
  27. }
  28. if (!$diyform || $diyform['status'] != 'normal') {
  29. $this->error(__('表单未找到'));
  30. }
  31. if ($diyform['needlogin'] && !$this->auth->id) {
  32. $this->error(__('请登录后再操作'), "index/user/login");
  33. }
  34. $this->diyform = $diyform;
  35. $this->view->assign("__DIYFORM__", $diyform);
  36. }
  37. /**
  38. * 数据列表
  39. * @return string
  40. */
  41. public function index()
  42. {
  43. $diyform = $this->diyform;
  44. $config = get_addon_config('cms');
  45. $filter = $this->request->get('filter/a', []);
  46. $orderby = $this->request->get('orderby', '');
  47. $orderway = $this->request->get('orderway', '', 'strtolower');
  48. $multiple = $this->request->get('multiple/d', 0);
  49. $params = [];
  50. $filter = $this->request->get();
  51. $filter = array_diff_key($filter, array_flip(['orderby', 'orderway', 'page', 'multiple']));
  52. if (isset($filter['filter'])) {
  53. $filter = array_merge($filter, $filter['filter']);
  54. }
  55. if ($filter) {
  56. $params['filter'] = $filter;
  57. }
  58. if ($orderby) {
  59. $params['orderby'] = $orderby;
  60. }
  61. if ($orderway) {
  62. $params['orderway'] = $orderway;
  63. }
  64. if ($multiple) {
  65. $params['multiple'] = $multiple;
  66. }
  67. //默认排序字段
  68. $orders = [
  69. ['name' => 'default', 'field' => 'createtime DESC,id DESC', 'title' => __('Default')],
  70. ];
  71. //合并特殊筛选字段
  72. $orders = array_merge($orders, $diyform->getOrderFields());
  73. //获取过滤列表
  74. list($filterList, $filter, $params, $fields, $multiValueFields, $fieldsList) = Service::getFilterList('diyform', $diyform['id'], $filter, $params, $multiple);
  75. //获取排序列表
  76. list($orderList, $orderby, $orderway) = Service::getOrderList($orderby, $orderway, $orders, $params, $fieldsList);
  77. //获取过滤的条件和绑定参数
  78. list($filterWhere, $filterBind) = Service::getFilterWhereBind($filter, $multiValueFields, $multiple);
  79. $auth = $this->auth;
  80. $model = new Diydata([], $diyform);
  81. $pageList = $model
  82. ->where($filterWhere)
  83. ->bind($filterBind)
  84. ->where(function ($query) use ($diyform, $auth) {
  85. //用户过滤模式
  86. //如果是仅用户自己消息可见
  87. if ($diyform['usermode'] == 'user') {
  88. $query->where('user_id', $auth->id);
  89. }
  90. })
  91. ->where(function ($query) use ($diyform, $auth) {
  92. //状态过滤模式
  93. if ($diyform['statusmode'] === 'normal') {
  94. if ($auth->id) {
  95. $query->whereRaw("user_id='" . intval($auth->id) . "' OR status='normal'");
  96. } else {
  97. $query->where('status', 'normal');
  98. }
  99. }
  100. })
  101. ->order($orderby, $orderway)
  102. ->paginate(10, $config['pagemode'] == 'simple', ['type' => '\\addons\\cms\\library\\Bootstrap']);
  103. $this->view->assign("__FILTERLIST__", $filterList);
  104. $this->view->assign("__ORDERLIST__", $orderList);
  105. $this->view->assign("__PAGELIST__", $pageList);
  106. //设置TKD
  107. Config::set('cms.title', $diyform['seotitle'] ?: $diyform['title']);
  108. Config::set('cms.keywords', $diyform['keywords']);
  109. Config::set('cms.description', $diyform['description']);
  110. //读取模板
  111. $template = preg_replace("/\.html$/i", "", $diyform['listtpl'] ? $diyform['listtpl'] : 'diyform_list');
  112. $template = $this->request->get("noframe", "0") ? "diyform_noframe" : $template;
  113. return $this->view->fetch('/' . $template);
  114. }
  115. /**
  116. * 查看详情
  117. * @return string
  118. */
  119. public function show()
  120. {
  121. $diyform = $this->diyform;
  122. $id = $this->request->param('id/d');
  123. $auth = $this->auth;
  124. $model = new Diydata([], $diyform);
  125. $diydata = $model
  126. ->where('id', $id)
  127. ->where(function ($query) use ($diyform, $auth) {
  128. //用户过滤模式
  129. //如果是仅用户自己消息可见
  130. if ($diyform['usermode'] == 'user') {
  131. $query->where('user_id', $auth->id);
  132. }
  133. })
  134. ->where(function ($query) use ($diyform, $auth) {
  135. //状态过滤模式
  136. if ($diyform['statusmode'] === 'normal') {
  137. if ($auth->id) {
  138. $query->whereRaw("user_id='" . intval($auth->id) . "' OR status='normal'");
  139. } else {
  140. $query->where('status', 'normal');
  141. }
  142. }
  143. })
  144. ->find();
  145. if (!$diydata) {
  146. $this->error("数据未找到或正在审核");
  147. }
  148. $fieldsList = Fields::where('source', 'diyform')->where('source_id', $diyform['id'])
  149. ->order('weigh desc,id desc')->column("*", "name");
  150. $this->view->assign('fieldsList', $fieldsList);
  151. $this->view->assign("__DIYDATA__", $diydata);
  152. //设置TKD
  153. Config::set('cms.title', $diyform['name'] . '详情');
  154. Config::set('cms.keywords', '');
  155. Config::set('cms.description', '');
  156. //加载模板
  157. $template = preg_replace("/\.html$/i", "", $diyform['showtpl'] ? $diyform['showtpl'] : 'diyform_show');
  158. return $this->view->fetch('/' . $template);
  159. }
  160. /**
  161. * 自定义表单提交
  162. */
  163. public function post()
  164. {
  165. $diyform = $this->diyform;
  166. $id = $this->request->request("id/d");
  167. $diydata = new Diydata([], $diyform);
  168. if ($id) {
  169. if (!$this->auth->isLogin()) {
  170. $this->error("请登录后再操作");
  171. }
  172. $diydata = $diydata->find($id);
  173. if (!$diydata) {
  174. $this->error("未找到指定数据");
  175. }
  176. if ($diydata['user_id'] != $this->auth->id) {
  177. $this->error("无法进行越权操作");
  178. }
  179. }
  180. if ($this->request->isPost()) {
  181. $config = get_addon_config('cms');
  182. $this->token();
  183. //检测是否开启验证码
  184. if (isset($diyform['iscaptcha']) && $diyform['iscaptcha']) {
  185. $captcha = $this->request->post('captcha');
  186. if (!captcha_check($captcha)) {
  187. $this->error("验证码不正确");
  188. }
  189. }
  190. $row = $this->request->post('row/a', '', 'trim,xss_clean');
  191. unset($row['id']);
  192. $fields = DiyformModel::getDiyformFields($diyform['id']);
  193. foreach ($fields as $index => $field) {
  194. if ($field['isrequire'] && (!isset($row[$field['name']]) || $row[$field['name']] == '')) {
  195. $this->error("{$field['title']}不能为空!");
  196. }
  197. }
  198. $row['user_id'] = $this->auth->id;
  199. foreach ($row as $index => &$value) {
  200. if (is_array($value) && isset($value['field'])) {
  201. $value = json_encode(\app\common\model\Config::getArrayData($value), JSON_UNESCAPED_UNICODE);
  202. } else {
  203. $value = is_array($value) ? implode(',', $value) : $value;
  204. }
  205. }
  206. $diydata['status'] = 'hidden';
  207. try {
  208. $diydata->save($row);
  209. } catch (\Exception $e) {
  210. $this->error("发生错误:" . $e->getMessage());
  211. }
  212. //发送通知
  213. Service::notice('CMS收到新的' . $diyform['name'], $config['auditnotice'], $config['noticetemplateid']);
  214. $redirecturl = $diyform['redirecturl'] ? $diyform['redirecturl'] : $diyform['url'];
  215. $this->success($diyform['successtips'] ? $diyform['successtips'] : '提交成功!', $redirecturl);
  216. }
  217. $fields = DiyformModel::getDiyformFields($diyform['id'], $diydata->toArray());
  218. $data = [
  219. 'fields' => $fields
  220. ];
  221. $diyform['fieldslist'] = $this->fetch('common/fields', $data);
  222. // 语言检测
  223. $lang = strip_tags($this->request->langset());
  224. $site = Config::get("site");
  225. $upload = \app\common\model\Config::upload();
  226. // 上传信息配置后
  227. Hook::listen("upload_config_init", $upload);
  228. // 配置信息
  229. $config = [
  230. 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
  231. 'upload' => $upload,
  232. 'modulename' => 'addons',
  233. 'controllername' => 'diyform',
  234. 'actionname' => 'index',
  235. 'jsname' => 'diyform/index',
  236. 'moduleurl' => rtrim(url("/index", '', false), '/'),
  237. 'language' => $lang
  238. ];
  239. $config = array_merge($config, Config::get("view_replace_str"));
  240. Config::set('upload', array_merge(Config::get('upload'), $upload));
  241. // 配置信息后
  242. Hook::listen("config_init", $config);
  243. $this->view->assign('diydata', $diydata);
  244. $this->view->assign('__DIYDATA__', $diydata);
  245. $this->view->assign('jsconfig', $config);
  246. //设置TKD
  247. Config::set('cms.title', ($id ? "修改" : "发布") . $diyform['name']);
  248. Config::set('cms.keywords', '');
  249. Config::set('cms.description', '');
  250. $template = preg_replace("/\.html$/i", "", $diyform['posttpl'] ? $diyform['posttpl'] : 'diyform_post');
  251. $template = $this->request->get("noframe", "0") ? "diyform_noframe" : $template;
  252. return $this->view->fetch('/' . $template);
  253. }
  254. }