123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?php
- namespace addons\cms\controller\api;
- use addons\cms\library\Service;
- use addons\cms\model\Diydata;
- use addons\cms\model\Diyform as DiyformModel;
- use think\Config;
- use think\Exception;
- use think\Hook;
- /**
- * 自定义表单控制器
- */
- class Diyform extends Base
- {
- protected $noNeedLogin = ['*'];
- protected $diyform = null;
- public function _initialize()
- {
- parent::_initialize();
- $diyname = $this->request->param('diyname');
- if (!$diyname) {
- //如果表单为空则取第一个表单
- $diyform = DiyformModel::where('status', 'normal')->order('id', 'asc')->find();
- } else {
- if ($diyname && !is_numeric($diyname)) {
- $diyform = DiyformModel::getByDiyname($diyname);
- } else {
- $id = $diyname ? $diyname : $this->request->get('id', '');
- $diyform = DiyformModel::get($id);
- }
- }
- if (!$diyform || $diyform['status'] != 'normal') {
- $this->error(__('表单未找到'));
- }
- if ($diyform['needlogin'] && !$this->auth->id) {
- $this->error(__('请登录后再操作'));
- }
- $diyform->hidden(['admin_id', 'table', 'posttpl', 'listtpl', 'showtpl']);
- $this->diyform = $diyform;
- }
- /**
- * 获取字段
- * @return void
- */
- public function index()
- {
- $id = $this->request->request("id/d"); //编辑的表单id
- $diydata = new Diydata([], $this->diyform);
- if ($id) {
- if (!$this->auth->isLogin()) {
- $this->error("请登录后再操作");
- }
- $diydata = $diydata->find($id);
- if (!$diydata) {
- $this->error("未找到指定数据");
- }
- if ($diydata['user_id'] != $this->auth->id) {
- $this->error("无法进行越权操作");
- }
- }
- $fields = DiyformModel::getDiyformFields($this->diyform['id'], $diydata->toArray());
- foreach ($fields as $item) {
- if ($item['type'] == 'array') {
- $item->value = html_entity_decode($item->value);
- }
- }
- $this->success('', [
- 'diyform' => $this->diyform,
- 'fields' => $fields
- ]);
- }
- /**
- * 提交或修改表单数据
- * @return void
- */
- public function postForm()
- {
- $diyform = $this->diyform;
- $id = $this->request->post("id/d"); //编辑的表单id
- $diydata = new Diydata([], $diyform);
- if ($id) {
- if (!$this->auth->isLogin()) {
- $this->error("请登录后再操作");
- }
- $diydata = $diydata->find($id);
- if (!$diydata) {
- $this->error("未找到指定数据");
- }
- if ($diydata['user_id'] != $this->auth->id) {
- $this->error("无法进行越权操作");
- }
- if(!$diyform['isedit']){
- $this->error("表单不允许编辑!");
- }
-
- }
- $config = get_addon_config('cms');
- $row = $this->request->post('', '', 'trim,xss_clean');
- unset($row['id']);
- $fields = DiyformModel::getDiyformFields($diyform['id']);
- foreach ($fields as $index => $field) {
- if ($field['isrequire'] && (!isset($row[$field['name']]) || $row[$field['name']] == '')) {
- $this->error("{$field['title']}不能为空!");
- }
- }
- $row['user_id'] = $this->auth->id;
- foreach ($row as $index => &$value) {
- if (is_array($value) && isset($value['field'])) {
- $value = json_encode(\app\common\model\Config::getArrayData($value), JSON_UNESCAPED_UNICODE);
- } else {
- $value = is_array($value) ? implode(',', $value) : $value;
- }
- }
- $diydata['status'] = 'hidden';
- try {
- $diydata->allowField(true)->save($row);
- } catch (\Exception $e) {
- $this->error("发生错误:" . $e->getMessage());
- }
- //发送通知
- Service::notice('CMS收到新的' . $diyform['name'], $config['auditnotice'], $config['noticetemplateid']);
- $this->success($diyform['successtips'] ? $diyform['successtips'] : '提交成功!');
- }
- public function formList()
- {
- $config = get_addon_config('cms');
- $diyform = $this->diyform;
- $filter = $this->request->get('filter/a', []);
- $orderby = $this->request->get('orderby', '');
- $orderway = $this->request->get('orderway', '', 'strtolower');
- $multiple = $this->request->get('multiple/d', 0);
- $keyword = $this->request->get('keyword');
- $params = [];
- $filter = $this->request->get();
- $filter = array_diff_key($filter, array_flip(['orderby', 'orderway', 'page', 'multiple']));
- if (isset($filter['filter'])) {
- $filter = array_merge($filter, $filter['filter']);
- }
- if ($filter) {
- $params['filter'] = $filter;
- }
- if ($orderby) {
- $params['orderby'] = $orderby;
- }
- if ($orderway) {
- $params['orderway'] = $orderway;
- }
- if ($multiple) {
- $params['multiple'] = $multiple;
- }
- //默认排序字段
- $orders = [
- ['name' => 'default', 'field' => 'createtime DESC,id DESC', 'title' => __('Default')],
- ];
- //合并特殊筛选字段
- $orders = array_merge($orders, $diyform->getOrderFields());
- //获取过滤列表
- list($filterList, $filter, $params, $fields, $multiValueFields, $fieldsList) = Service::getFilterList('diyform', $diyform['id'], $filter, $params, $multiple);
- //获取排序列表
- list($orderList, $orderby, $orderway) = Service::getOrderList($orderby, $orderway, $orders, $params, $fieldsList);
- //获取过滤的条件和绑定参数
- list($filterWhere, $filterBind) = Service::getFilterWhereBind($filter, $multiValueFields, $multiple);
- $auth = $this->auth;
- $model = new Diydata([], $diyform);
- $pageList = $model
- ->where($filterWhere)
- ->bind($filterBind)
- ->where(function ($query) use ($diyform, $auth) {
- //用户过滤模式
- //如果是仅用户自己消息可见
- if ($diyform['usermode'] == 'user') {
- $query->where('user_id', $auth->id);
- }
- })
- ->where(function ($query) use ($diyform, $auth) {
- //状态过滤模式
- if ($diyform['statusmode'] === 'normal') {
- if ($auth->id) {
- $query->whereRaw("user_id='" . intval($auth->id) . "' OR status='normal'");
- } else {
- $query->where('status', 'normal');
- }
- }
- })
- ->where(function ($query) use ($diyform, $keyword) {
- $field = '';
- $fieldArr = explode(',', $diyform->fields);
- if (in_array('name', $fieldArr)) {
- $field = 'name';
- } elseif (in_array('title', $fieldArr)) {
- $field = 'title';
- } elseif (in_array('content', $fieldArr)) {
- $field = 'content';
- }
- if ($keyword && $field) {
- $query->where($field, 'like', '%' . $keyword . '%');
- }
- })
- ->order($orderby, $orderway)
- ->paginate(15);
- foreach ($pageList as $item) {
- if (isset($item['images']) && !empty($item['images'])) {
- $images = explode(',', $item->images);
- foreach ($images as &$res) {
- $res = cdnurl($res, true);
- }
- unset($res);
- $item->images = $images;
- }
- if (isset($item['image']) && !empty($item['image'])) {
- $image = explode(',', $item->image);
- foreach ($image as &$res) {
- $res = cdnurl($res, true);
- }
- unset($res);
- $item->image = $image;
- }
- $item->content = mb_substr(strip_tags($item->content), 0, 80);
- }
- $this->success('', [
- 'pageList' => $pageList,
- 'orderList' => $orderList,
- 'filterList' => $filterList
- ]);
- }
- /**
- * 查看详情
- * @return string
- */
- public function show()
- {
- $diyform = $this->diyform;
- $id = $this->request->param('id/d');
- $auth = $this->auth;
- $model = new Diydata([], $diyform);
- $diydata = $model
- ->where('id', $id)
- ->where(function ($query) use ($diyform, $auth) {
- //用户过滤模式
- //如果是仅用户自己消息可见
- if ($diyform['usermode'] == 'user') {
- $query->where('user_id', $auth->id);
- }
- })
- ->where(function ($query) use ($diyform, $auth) {
- //状态过滤模式
- if ($diyform['statusmode'] === 'normal') {
- if ($auth->id) {
- $query->whereRaw("user_id='" . intval($auth->id) . "' OR status='normal'");
- } else {
- $query->where('status', 'normal');
- }
- }
- })
- ->find();
- if (!$diydata) {
- $this->error("数据未找到或正在审核");
- }
- $fields = DiyformModel::getDiyformFields($this->diyform['id'], $diydata->toArray());
- foreach ($fields as $item) {
- $setting = $item->setting;
- unset($setting['table']);
- $item->setting = $setting;
- if (in_array($item['type'], ['image', 'file'])) {
- $item->value = cdnurl($item->value, true);
- }
- if (in_array($item['type'], ['images', 'files'])) {
- $ifs = explode(',', $item->value);
- foreach ($ifs as &$res) {
- $res = cdnurl($res, true);
- }
- $item->value = $ifs;
- }
- if ($item['type'] == 'array') {
- $item->value = json_decode(html_entity_decode($item->value), true);
- }
- }
- $this->success('', [
- 'fieldsList' => $fields,
- 'diydata' => $diydata,
- 'diyform' => $diyform
- ]);
- }
- }
|