Diyform.php 12 KB

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