Field.php 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\admin\controller;
  14. use think\Page;
  15. use think\Db;
  16. use app\admin\logic\FieldLogic;
  17. /**
  18. * 模型字段控制器
  19. */
  20. class Field extends Base
  21. {
  22. public $fieldLogic;
  23. public $arctype_channel_id;
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->language_access(); // 多语言功能操作权限
  28. $this->fieldLogic = new FieldLogic();
  29. $this->arctype_channel_id = config('global.arctype_channel_id');
  30. $userConfig = getUsersConfigData('users');
  31. $this->assign('userConfig', $userConfig);
  32. }
  33. /**
  34. * 模型字段管理
  35. */
  36. public function channel_index()
  37. {
  38. /*同步栏目绑定的自定义字段*/
  39. $this->syn_channelfield_bind();
  40. /*--end*/
  41. $channel_id = input('param.channel_id/d', 1);
  42. $assign_data = array();
  43. $condition = array();
  44. // 获取到所有GET参数
  45. $param = input('param.');
  46. /*同步更新附加表字段到自定义模型字段表中*/
  47. if (empty($param['searchopt'])) {
  48. $this->fieldLogic->synChannelTableColumns($channel_id);
  49. }
  50. /*--end*/
  51. // 应用搜索条件
  52. foreach (['keywords'] as $key) {
  53. if (isset($param[$key]) && $param[$key] !== '') {
  54. if ($key == 'keywords') {
  55. $condition['a.name|a.title'] = array('LIKE', "%{$param[$key]}%");
  56. // 过滤指定字段
  57. // $banFields = ['id'];
  58. // $condition['a.name'] = array(
  59. // array('LIKE', "%{$param[$key]}%"),
  60. // array('notin', $banFields),
  61. // );
  62. } else {
  63. $condition['a.' . $key] = array('eq', $param[$key]);
  64. }
  65. }
  66. }
  67. /*显示主表与附加表*/
  68. $condition['a.channel_id'] = array('IN', [$channel_id]);
  69. /*模型列表*/
  70. $channeltype_list = model('Channeltype')->getAll('*', [], 'id');
  71. $assign_data['channeltype_list'] = $channeltype_list;
  72. /*--end*/
  73. $condition['a.ifcontrol'] = 0;
  74. $cfieldM = M('channelfield');
  75. $count = $cfieldM->alias('a')->where($condition)->count('a.id');// 查询满足要求的总记录数
  76. $Page = $pager = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
  77. $list = $cfieldM->field('a.*')
  78. ->alias('a')
  79. ->where($condition)
  80. ->order('a.sort_order asc, a.ifmain asc, a.ifcontrol asc, a.id desc')
  81. ->limit($Page->firstRow . ',' . $Page->listRows)
  82. ->select();
  83. $show = $Page->show();// 分页显示输出
  84. $assign_data['page'] = $show; // 赋值分页输出
  85. $assign_data['list'] = $list; // 赋值数据集
  86. $assign_data['pager'] = $Page; // 赋值分页对象
  87. /*字段类型列表*/
  88. $assign_data['fieldtypeList'] = M('field_type')->field('name,title')->getAllWithIndex('name');
  89. /*--end*/
  90. /*模型ID*/
  91. $assign_data['channel_id'] = $channel_id;
  92. /*--end*/
  93. $this->assign($assign_data);
  94. return $this->fetch();
  95. }
  96. /**
  97. * 同步栏目绑定的自定义字段
  98. */
  99. private function syn_channelfield_bind()
  100. {
  101. $field_ids = Db::name('channelfield')->where([
  102. 'ifmain' => 0,
  103. 'channel_id' => ['NEQ', -99],
  104. ])->column('id');
  105. if (!empty($field_ids)) {
  106. $totalRow = Db::name('channelfield_bind')->count();
  107. if (empty($totalRow)) {
  108. $sveData = [];
  109. foreach ($field_ids as $key => $val) {
  110. $sveData[] = [
  111. 'typeid' => 0,
  112. 'field_id' => $val,
  113. 'add_time' => getTime(),
  114. 'update_time' => getTime(),
  115. ];
  116. }
  117. model('ChannelfieldBind')->saveAll($sveData);
  118. }
  119. }
  120. }
  121. /**
  122. * 新增-模型字段
  123. */
  124. public function channel_add()
  125. {
  126. $channel_id = input('param.channel_id/d', 0);
  127. if (empty($channel_id)) {
  128. $this->error('参数有误!');
  129. }
  130. if (IS_POST) {
  131. $post = input('post.', '', 'trim');
  132. // 判断是否存在|杠
  133. $IsData = strstr($post['dfvalue'], '|');
  134. if (!empty($IsData)) {
  135. $this->error("不可输入 | 杠");
  136. }
  137. if (empty($post['dtype']) || empty($post['title']) || empty($post['name'])) {
  138. $this->error("缺少必填信息!");
  139. }
  140. if (1 == preg_match('/^([_]+|[0-9]+)$/', $post['name'])) {
  141. $this->error("字段名称格式不正确!");
  142. } else if (preg_match('/^type/', $post['name'])) {
  143. $this->error("字段名称不允许以type开头!");
  144. } else if (preg_match('/^ey_/', $post['name'])) {
  145. $this->error("字段名称不允许以 ey_ 开头!");
  146. }
  147. // 字段类型是否具备筛选功能
  148. if (empty($post['IsScreening_status'])) {
  149. $post['is_screening'] = 0;
  150. }
  151. /*去除中文逗号,过滤左右空格与空值、以及单双引号*/
  152. $dfvalue = str_replace(',', ',', $post['dfvalue']);
  153. $dfvalue = func_preg_replace(['"', '\'', ';'], '', $dfvalue);
  154. $dfvalueArr = explode(',', $dfvalue);
  155. foreach ($dfvalueArr as $key => $val) {
  156. $tmp_val = trim($val);
  157. if (empty($tmp_val)) {
  158. unset($dfvalueArr[$key]);
  159. continue;
  160. }
  161. $dfvalueArr[$key] = $tmp_val;
  162. }
  163. $dfvalueArr = array_unique($dfvalueArr);
  164. $dfvalue = implode(',', $dfvalueArr);
  165. /*--end*/
  166. if ('region' == $post['dtype']) {
  167. if (!empty($post['region_data'])) {
  168. $post['dfvalue'] = $post['region_data']['region_id'];
  169. $post['region_data'] = serialize($post['region_data']);
  170. } else {
  171. $this->error("请选择区域范围!");
  172. }
  173. } else {
  174. /*默认值必填字段*/
  175. $fieldtype_list = model('Field')->getFieldTypeAll('name,title,ifoption', 'name');
  176. if (isset($fieldtype_list[$post['dtype']]) && 1 == $fieldtype_list[$post['dtype']]['ifoption']) {
  177. if (empty($dfvalue)) {
  178. $this->error("你设定了字段为【" . $fieldtype_list[$post['dtype']]['title'] . "】类型,默认值不能为空! ");
  179. }
  180. }
  181. /*--end*/
  182. unset($post['region_data']);
  183. }
  184. /*当前模型对应的数据表*/
  185. $table = M('channeltype')->where('id', $channel_id)->getField('table');
  186. $table = PREFIX . $table . '_content';
  187. /*--end*/
  188. /*检测字段是否存在于主表与附加表中*/
  189. if (true == $this->fieldLogic->checkChannelFieldList($table, $post['name'], $channel_id)) {
  190. $this->error("字段名称 " . $post['name'] . " 与系统字段冲突!");
  191. }
  192. /*--end*/
  193. if (empty($post['typeids'])) {
  194. $this->error('请选择可见栏目!');
  195. }
  196. /*组装完整的SQL语句,并执行新增字段*/
  197. $fieldinfos = $this->fieldLogic->GetFieldMake($post['dtype'], $post['name'], $dfvalue, $post['title']);
  198. $ntabsql = $fieldinfos[0];
  199. $buideType = $fieldinfos[1];
  200. $maxlength = $fieldinfos[2];
  201. $sql = " ALTER TABLE `$table` ADD $ntabsql ";
  202. if (false !== Db::execute($sql)) {
  203. if (!empty($post['region_data'])) {
  204. $dfvalue = $post['region_data'];
  205. unset($post['region_data']);
  206. }
  207. /*保存新增字段的记录*/
  208. $newData = array(
  209. 'dfvalue' => $dfvalue,
  210. 'maxlength' => $maxlength,
  211. 'define' => $buideType,
  212. 'ifcontrol' => 0,
  213. 'sort_order' => 100,
  214. 'add_time' => getTime(),
  215. 'update_time' => getTime(),
  216. );
  217. $data = array_merge($post, $newData);
  218. M('channelfield')->save($data);
  219. $field_id = M('channelfield')->getLastInsID();
  220. /*--end*/
  221. /*保存栏目与字段绑定的记录*/
  222. $typeids = $post['typeids'];
  223. if (!empty($typeids)) {
  224. /*多语言*/
  225. if (is_language()) {
  226. $attr_name_arr = [];
  227. foreach ($typeids as $key => $val) {
  228. $attr_name_arr[] = 'tid' . $val;
  229. }
  230. $new_typeid_arr = Db::name('language_attr')->where([
  231. 'attr_name' => ['IN', $attr_name_arr],
  232. 'attr_group' => 'arctype',
  233. ])->column('attr_value');
  234. !empty($new_typeid_arr) && $typeids = $new_typeid_arr;
  235. }
  236. /*--end*/
  237. $addData = [];
  238. foreach ($typeids as $key => $val) {
  239. if (1 < count($typeids) && empty($val)) {
  240. continue;
  241. }
  242. $addData[] = [
  243. 'typeid' => $val,
  244. 'field_id' => $field_id,
  245. 'add_time' => getTime(),
  246. 'update_time' => getTime(),
  247. ];
  248. }
  249. !empty($addData) && model('ChannelfieldBind')->saveAll($addData);
  250. }
  251. /*--end*/
  252. /*重新生成数据表字段缓存文件*/
  253. try {
  254. schemaTable($table);
  255. } catch (\Exception $e) {}
  256. /*--end*/
  257. \think\Cache::clear('channelfield');
  258. $this->success("操作成功!", url('Field/channel_index', array('channel_id' => $channel_id)));
  259. }
  260. $this->error('操作失败');
  261. }
  262. /*字段类型列表*/
  263. $assign_data['fieldtype_list'] = model('Field')->getFieldTypeAll('name,title,ifoption');
  264. /*--end*/
  265. /*允许发布文档列表的栏目*/
  266. $select_html = allow_release_arctype(0, [$channel_id]);
  267. $this->assign('select_html', $select_html);
  268. /*--end*/
  269. /*模型ID*/
  270. $assign_data['channel_id'] = $channel_id;
  271. /*--end*/
  272. $China[] = [
  273. 'id' => 0,
  274. 'name' => '全国',
  275. ];
  276. $Province = get_province_list();
  277. $assign_data['Province'] = array_merge($China, $Province);
  278. $this->assign($assign_data);
  279. return $this->fetch();
  280. }
  281. // 联动地址获取
  282. public function get_region_data()
  283. {
  284. $parent_id = input('param.parent_id/d');
  285. // 获取指定区域ID下的城市并判断是否需要处理特殊市返回值
  286. $RegionData = $this->SpecialCityDealWith($parent_id);
  287. // 处理数据
  288. $region_html = $region_names = $region_ids = '';
  289. if ($RegionData) {
  290. // 拼装下拉选项
  291. foreach ($RegionData as $key => $value) {
  292. $region_html .= "<option value='{$value['id']}'>{$value['name']}</option>";
  293. if ($key > '0') {
  294. $region_names .= ',';
  295. $region_ids .= ',';
  296. }
  297. $region_names .= $value['name'];
  298. $region_ids .= $value['id'];
  299. }
  300. }
  301. $return = [
  302. 'region_html' => $region_html,
  303. 'region_names' => $region_names,
  304. 'region_ids' => $region_ids,
  305. 'parent_array' => config('global.field_region_all_type'),
  306. ];
  307. echo json_encode($return);
  308. }
  309. // 获取指定区域ID下的城市并判断是否需要处理特殊市返回值
  310. // 特殊市:北京市,上海市,天津市,重庆市
  311. function SpecialCityDealWith($parent_id = 0)
  312. {
  313. empty($parent_id) && $parent_id = 0;
  314. /*parent_id在特殊范围内则执行*/
  315. // 处理北京市,上海市,天津市,重庆市逻辑
  316. $RegionData = Db::name('region')->where("parent_id", $parent_id)->select();
  317. $parent_array = config('global.field_region_type');
  318. if (in_array($parent_id, $parent_array)) {
  319. $region_ids = get_arr_column($RegionData, 'id');
  320. $RegionData = Db::name('region')->where('parent_id', 'IN', $region_ids)->select();
  321. }
  322. /*结束*/
  323. return $RegionData;
  324. }
  325. /**
  326. * 编辑-模型字段
  327. */
  328. public function channel_edit()
  329. {
  330. $channel_id = input('param.channel_id/d', 0);
  331. if (empty($channel_id)) {
  332. $this->error('参数有误!');
  333. }
  334. if (IS_POST) {
  335. $post = input('post.', '', 'trim');
  336. if ('checkbox' == $post['old_dtype'] && in_array($post['dtype'], ['radio', 'select'])) {
  337. $fieldtype_list = model('Field')->getFieldTypeAll('name,title', 'name');
  338. $this->error("{$fieldtype_list['checkbox']['title']}不能更改为{$fieldtype_list[$post['dtype']]['title']}!");
  339. }
  340. if (empty($post['dtype']) || empty($post['title']) || empty($post['name'])) {
  341. $this->error("缺少必填信息!");
  342. }
  343. if (1 == preg_match('/^([_]+|[0-9]+)$/', $post['name'])) {
  344. $this->error("字段名称格式不正确!");
  345. } else if (preg_match('/^type/', $post['name'])) {
  346. $this->error("字段名称不允许以type开头!");
  347. } else if (preg_match('/^ey_/', $post['name'])) {
  348. $this->error("字段名称不允许以 ey_ 开头!");
  349. }
  350. $info = model('Channelfield')->getInfo($post['id'], 'ifsystem');
  351. if (!empty($info['ifsystem'])) {
  352. $this->error('系统字段不允许更改!');
  353. }
  354. // 字段类型是否具备筛选功能
  355. if (empty($post['IsScreening_status'])) {
  356. $post['is_screening'] = 0;
  357. }
  358. $old_name = $post['old_name'];
  359. /*去除中文逗号,过滤左右空格与空值*/
  360. $dfvalue = str_replace(',', ',', $post['dfvalue']);
  361. $dfvalue = func_preg_replace(['"', '\'', ';'], '', $dfvalue);
  362. $dfvalueArr = explode(',', $dfvalue);
  363. foreach ($dfvalueArr as $key => $val) {
  364. $tmp_val = trim($val);
  365. if (empty($tmp_val)) {
  366. unset($dfvalueArr[$key]);
  367. continue;
  368. }
  369. $dfvalueArr[$key] = $tmp_val;
  370. }
  371. $dfvalueArr = array_unique($dfvalueArr);
  372. $dfvalue = implode(',', $dfvalueArr);
  373. /*--end*/
  374. if ('region' == $post['dtype']) {
  375. if (!empty($post['region_data'])) {
  376. $post['dfvalue'] = $post['region_data']['region_id'];
  377. $post['region_data'] = serialize($post['region_data']);
  378. } else {
  379. $this->error("请选择区域范围!");
  380. }
  381. } else {
  382. /*默认值必填字段*/
  383. $fieldtype_list = model('Field')->getFieldTypeAll('name,title,ifoption', 'name');
  384. if (isset($fieldtype_list[$post['dtype']]) && 1 == $fieldtype_list[$post['dtype']]['ifoption']) {
  385. if (empty($dfvalue)) {
  386. $this->error("你设定了字段为【" . $fieldtype_list[$post['dtype']]['title'] . "】类型,默认值不能为空! ");
  387. }
  388. }
  389. /*--end*/
  390. unset($post['region_data']);
  391. }
  392. /*当前模型对应的数据表*/
  393. $table = M('channeltype')->where('id', $post['channel_id'])->getField('table');
  394. $tableName = $table . '_content';
  395. $table = PREFIX . $tableName;
  396. /*--end*/
  397. /*检测字段是否存在于主表与附加表中*/
  398. if (true == $this->fieldLogic->checkChannelFieldList($table, $post['name'], $channel_id, array($old_name))) {
  399. $this->error("字段名称 " . $post['name'] . " 与系统字段冲突!");
  400. }
  401. /*--end*/
  402. if (empty($post['typeids'])) {
  403. $this->error('请选择可见栏目!');
  404. }
  405. /*针对单选项、多选项、下拉框:修改之前,将该字段不存在的值都更新为默认值第一个*/
  406. if (in_array($post['old_dtype'], ['radio', 'select', 'checkbox']) && in_array($post['dtype'], ['radio', 'select', 'checkbox'])) {
  407. $whereArr = [];
  408. $dfvalueArr = explode(',', $dfvalue);
  409. foreach($dfvalueArr as $key => $val){
  410. $whereArr[] = "`{$post['name']}` <> '{$val}'";
  411. }
  412. $whereStr = implode(' OR ', $whereArr);
  413. if (in_array($post['dtype'], ['radio', 'select', 'checkbox'])) {
  414. $new_dfvalue = NULL;
  415. } else {
  416. $new_dfvalue = '';
  417. }
  418. Db::name($tableName)->where($whereStr)->update([$post['name']=>$new_dfvalue]);
  419. }
  420. /*end*/
  421. /*组装完整的SQL语句,并执行编辑字段*/
  422. $fieldinfos = $this->fieldLogic->GetFieldMake($post['dtype'], $post['name'], $dfvalue, $post['title']);
  423. $ntabsql = $fieldinfos[0];
  424. $buideType = $fieldinfos[1];
  425. $maxlength = $fieldinfos[2];
  426. $sql = " ALTER TABLE `$table` CHANGE COLUMN `{$old_name}` $ntabsql ";
  427. if (false !== Db::execute($sql)) {
  428. /*针对单选项、多选项、下拉框:修改之前,将该字段不存在的值都更新为默认值第一个*/
  429. if (in_array($post['old_dtype'], ['radio', 'select', 'checkbox']) && in_array($post['dtype'], ['radio', 'select', 'checkbox'])) {
  430. $whereArr = [];
  431. $new_dfvalue = '';
  432. $dfvalueArr = explode(',', $dfvalue);
  433. foreach($dfvalueArr as $key => $val){
  434. if ($key == 0) {
  435. $new_dfvalue = $val;
  436. }
  437. $whereArr[] = "`{$post['name']}` <> '{$val}'";
  438. }
  439. $whereArr[] = "`{$post['name']}` is NULL";
  440. $whereArr[] = "`{$post['name']}` = ''";
  441. $whereStr = implode(' OR ', $whereArr);
  442. Db::name($tableName)->where($whereStr)->update([$post['name']=>$new_dfvalue]);
  443. }
  444. /*end*/
  445. /*保存更新字段的记录*/
  446. if (!empty($post['region_data'])) {
  447. $dfvalue = $post['region_data'];
  448. unset($post['region_data']);
  449. }
  450. $newData = array(
  451. 'dfvalue' => $dfvalue,
  452. 'maxlength' => $maxlength,
  453. 'define' => $buideType,
  454. 'update_time' => getTime(),
  455. );
  456. $data = array_merge($post, $newData);
  457. M('channelfield')->where('id', $post['id'])->cache(true, null, "channelfield")->save($data);
  458. /*--end*/
  459. /*保存栏目与字段绑定的记录*/
  460. $field_id = $post['id'];
  461. model('ChannelfieldBind')->where(['field_id' => $field_id])->delete();
  462. $typeids = $post['typeids'];
  463. if (!empty($typeids)) {
  464. /*多语言*/
  465. if (is_language()) {
  466. $attr_name_arr = [];
  467. foreach ($typeids as $key => $val) {
  468. $attr_name_arr[] = 'tid' . $val;
  469. }
  470. $new_typeid_arr = Db::name('language_attr')->where([
  471. 'attr_name' => ['IN', $attr_name_arr],
  472. 'attr_group' => 'arctype',
  473. ])->column('attr_value');
  474. !empty($new_typeid_arr) && $typeids = $new_typeid_arr;
  475. }
  476. /*--end*/
  477. $addData = [];
  478. foreach ($typeids as $key => $val) {
  479. if (1 < count($typeids) && empty($val)) {
  480. continue;
  481. }
  482. $addData[] = [
  483. 'typeid' => $val,
  484. 'field_id' => $field_id,
  485. 'add_time' => getTime(),
  486. 'update_time' => getTime(),
  487. ];
  488. }
  489. !empty($addData) && model('ChannelfieldBind')->saveAll($addData);
  490. }
  491. /*--end*/
  492. /*重新生成数据表字段缓存文件*/
  493. try {
  494. schemaTable($table);
  495. } catch (\Exception $e) {}
  496. /*--end*/
  497. $this->success("操作成功!", url('Field/channel_index', array('channel_id' => $post['channel_id'])));
  498. } else {
  499. $sql = " ALTER TABLE `$table` ADD $ntabsql ";
  500. if (false === Db::execute($sql)) {
  501. $this->error('操作失败!');
  502. }
  503. }
  504. }
  505. $id = input('param.id/d', 0);
  506. $info = array();
  507. if (!empty($id)) {
  508. $info = model('Channelfield')->getInfo($id);
  509. }
  510. if (!empty($info['ifsystem'])) {
  511. $this->error('系统字段不允许更改!');
  512. }
  513. /*字段类型列表*/
  514. $assign_data['fieldtype_list'] = model('Field')->getFieldTypeAll('name,title,ifoption');
  515. /*--end*/
  516. /*允许发布文档列表的栏目*/
  517. $typeids = Db::name('channelfield_bind')->where(['field_id' => $id])->column('typeid');
  518. $select_html = allow_release_arctype($typeids, [$channel_id]);
  519. $this->assign('select_html', $select_html);
  520. $this->assign('typeids', $typeids);
  521. /*--end*/
  522. /*模型ID*/
  523. $assign_data['channel_id'] = $channel_id;
  524. /*--end*/
  525. /*区域字段处理*/
  526. // 初始化参数
  527. $assign_data['region'] = [
  528. 'parent_id' => '-1',
  529. 'region_id' => '-1',
  530. 'region_names' => '',
  531. 'region_ids' => '',
  532. ];
  533. // 定义全国参数
  534. $China[] = [
  535. 'id' => 0,
  536. 'name' => '全国',
  537. ];
  538. // 查询省份信息并且拼装上$China数组
  539. $Province = get_province_list();
  540. $assign_data['Province'] = array_merge($China, $Province);
  541. // 区域选择时,指定不出现下级地区列表
  542. $assign_data['parent_array'] = "[]";
  543. // 如果是区域类型则执行
  544. if ('region' == $info['dtype']) {
  545. // 反序列化默认值参数
  546. $dfvalue = unserialize($info['dfvalue']);
  547. if (0 == $dfvalue['region_id']) {
  548. $parent_id = $dfvalue['region_id'];
  549. } else {
  550. // 查询当前选中的区域父级ID
  551. $parent_id = Db::name('region')->where("id", $dfvalue['region_id'])->getField('parent_id');
  552. if (0 == $parent_id) {
  553. $parent_id = $dfvalue['region_id'];
  554. }
  555. }
  556. // 查询市\区\县信息
  557. $assign_data['City'] = Db::name('region')->where("parent_id", $parent_id)->select();
  558. // 加载数据到模板
  559. $assign_data['region'] = [
  560. 'parent_id' => $parent_id,
  561. 'region_id' => $dfvalue['region_id'],
  562. 'region_names' => $dfvalue['region_names'],
  563. 'region_ids' => $dfvalue['region_ids'],
  564. ];
  565. // 删除默认值,防止切换其他类型时使用到
  566. unset($info['dfvalue']);
  567. // 区域选择时,指定不出现下级地区列表
  568. $assign_data['parent_array'] = convert_js_array(config('global.field_region_all_type'));
  569. }
  570. /*区域字段处理结束*/
  571. $assign_data['info'] = $info;
  572. $this->assign($assign_data);
  573. return $this->fetch();
  574. }
  575. /**
  576. * 删除-模型字段
  577. */
  578. public function channel_del()
  579. {
  580. $channel_id = input('channel_id/d', 0);
  581. $id = input('del_id/d', 0);
  582. if (!empty($id)) {
  583. /*删除表字段*/
  584. $row = $this->fieldLogic->delChannelField($id);
  585. /*--end*/
  586. if (0 < $row['code']) {
  587. $map = array(
  588. 'id' => array('eq', $id),
  589. 'channel_id' => $channel_id,
  590. );
  591. $result = M('channelfield')->field('channel_id,name')->where($map)->select();
  592. $name_list = get_arr_column($result, 'name');
  593. /*删除字段的记录*/
  594. M('channelfield')->where($map)->delete();
  595. /*--end*/
  596. /*删除栏目与字段绑定的记录*/
  597. model('ChannelfieldBind')->where(['field_id' => $id])->delete();
  598. /*--end*/
  599. /*获取模型标题*/
  600. $channel_title = '';
  601. if (!empty($channel_id)) {
  602. $channel_title = M('channeltype')->where('id', $channel_id)->getField('title');
  603. }
  604. /*--end*/
  605. adminLog('删除' . $channel_title . '字段:' . implode(',', $name_list));
  606. $this->success('删除成功');
  607. }
  608. \think\Cache::clear('channelfield');
  609. respose(array('status' => 0, 'msg' => $row['msg']));
  610. } else {
  611. $this->error('参数有误');
  612. }
  613. }
  614. /**
  615. * 栏目字段 - 删除多图字段的图集
  616. */
  617. public function del_arctypeimgs()
  618. {
  619. $typeid = input('typeid/d', '0');
  620. if (!empty($typeid)) {
  621. $path = input('filename', ''); // 图片路径
  622. $fieldname = input('fieldname/s', ''); // 多图字段
  623. /*除去多图字段值中的图片*/
  624. $info = M('arctype')->field("{$fieldname}")->where("id", $typeid)->find();
  625. $valueArr = explode(',', $info[$fieldname]);
  626. foreach ($valueArr as $key => $val) {
  627. if ($path == $val) {
  628. unset($valueArr[$key]);
  629. }
  630. }
  631. $value = implode(',', $valueArr);
  632. M('arctype')->where('id', $typeid)->update(array($fieldname => $value, 'update_time' => getTime()));
  633. /*--end*/
  634. }
  635. }
  636. /**
  637. * 模型字段 - 删除多图字段的图集
  638. */
  639. public function del_channelimgs()
  640. {
  641. $aid = input('aid/d', '0');
  642. $channel = input('channel/d', ''); // 模型ID
  643. if (!empty($aid) && !empty($channel)) {
  644. $path = input('filename', ''); // 图片路径
  645. $fieldname = input('fieldname/s', ''); // 多图字段
  646. /*模型附加表*/
  647. $table = M('channeltype')->where('id', $channel)->getField('table');
  648. $tableExt = $table . '_content';
  649. /*--end*/
  650. /*除去多图字段值中的图片*/
  651. $info = M($tableExt)->field("{$fieldname}")->where("aid", $aid)->find();
  652. $valueArr = explode(',', $info[$fieldname]);
  653. foreach ($valueArr as $key => $val) {
  654. if ($path == $val) {
  655. unset($valueArr[$key]);
  656. }
  657. }
  658. $value = implode(',', $valueArr);
  659. M($tableExt)->where('aid', $aid)->update(array($fieldname => $value, 'update_time' => getTime()));
  660. /*--end*/
  661. }
  662. }
  663. /**
  664. * 显示与隐藏
  665. */
  666. public function ajax_channel_show()
  667. {
  668. if (IS_POST) {
  669. $id = input('id/d');
  670. $ifeditable = input('ifeditable/d');
  671. if (!empty($id)) {
  672. $row = Db::name('channelfield')->where([
  673. 'id' => $id,
  674. ])->find();
  675. if (!empty($row) && 1 == intval($row['ifcontrol'])) {
  676. $this->error('系统内置表单,禁止操作!');
  677. }
  678. $r = Db::name('channelfield')->where([
  679. 'id' => $id,
  680. ])->update([
  681. 'ifeditable' => $ifeditable,
  682. 'update_time' => getTime(),
  683. ]);
  684. if ($r) {
  685. adminLog('操作自定义模型表单:' . $row['name']);
  686. $this->success('操作成功');
  687. } else {
  688. $this->error('操作失败');
  689. }
  690. } else {
  691. $this->error('参数有误');
  692. }
  693. }
  694. $this->error('非法访问');
  695. }
  696. /**
  697. * 栏目字段管理
  698. */
  699. public function arctype_index()
  700. {
  701. $channel_id = $this->arctype_channel_id;
  702. $assign_data = array();
  703. $condition = array();
  704. // 获取到所有GET参数
  705. $param = input('param.');
  706. /*同步更新栏目主表字段到自定义字段表中*/
  707. if (empty($param['searchopt'])) {
  708. $this->fieldLogic->synArctypeTableColumns($channel_id);
  709. }
  710. /*--end*/
  711. // 应用搜索条件
  712. foreach (['keywords'] as $key) {
  713. if (isset($param[$key]) && $param[$key] !== '') {
  714. if ($key == 'keywords') {
  715. $condition['name|title'] = array('LIKE', "%{$param[$key]}%");
  716. } else {
  717. $condition[$key] = array('eq', $param[$key]);
  718. }
  719. }
  720. }
  721. // 模型ID
  722. $condition['channel_id'] = array('eq', $channel_id);
  723. $condition['ifsystem'] = array('neq', 1);
  724. $cfieldM = M('channelfield');
  725. $count = $cfieldM->where($condition)->count('id');// 查询满足要求的总记录数
  726. $Page = $pager = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
  727. $list = $cfieldM->where($condition)->order('sort_order asc, ifsystem asc, id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
  728. $show = $Page->show();// 分页显示输出
  729. $assign_data['page'] = $show; // 赋值分页输出
  730. $assign_data['list'] = $list; // 赋值数据集
  731. $assign_data['pager'] = $Page; // 赋值分页对象
  732. /*字段类型列表*/
  733. $assign_data['fieldtypeList'] = M('field_type')->field('name,title')->getAllWithIndex('name');
  734. /*--end*/
  735. $assign_data['channel_id'] = $channel_id;
  736. $this->assign($assign_data);
  737. return $this->fetch();
  738. }
  739. /**
  740. * 新增-栏目字段
  741. */
  742. public function arctype_add()
  743. {
  744. $channel_id = $this->arctype_channel_id;
  745. if (empty($channel_id)) {
  746. $this->error('参数有误!');
  747. }
  748. if (IS_POST) {
  749. $post = input('post.', '', 'trim');
  750. if (empty($post['dtype']) || empty($post['title']) || empty($post['name'])) {
  751. $this->error("缺少必填信息!");
  752. }
  753. if (1 == preg_match('/^([_]+|[0-9]+)$/', $post['name'])) {
  754. $this->error("字段名称格式不正确!");
  755. } else if (preg_match('/^ey_/', $post['name'])) {
  756. $this->error("字段名称不允许以 ey_ 开头!");
  757. }
  758. /*去除中文逗号,过滤左右空格与空值*/
  759. $dfvalue = str_replace(',', ',', $post['dfvalue']);
  760. $dfvalue = func_preg_replace(['"', '\'', ';'], '', $dfvalue);
  761. $dfvalueArr = explode(',', $dfvalue);
  762. foreach ($dfvalueArr as $key => $val) {
  763. $tmp_val = trim($val);
  764. if (empty($tmp_val)) {
  765. unset($dfvalueArr[$key]);
  766. continue;
  767. }
  768. $dfvalueArr[$key] = $tmp_val;
  769. }
  770. $dfvalueArr = array_unique($dfvalueArr);
  771. $dfvalue = implode(',', $dfvalueArr);
  772. /*--end*/
  773. /*默认值必填字段*/
  774. $fieldtype_list = model('Field')->getFieldTypeAll('name,title,ifoption', 'name');
  775. if (isset($fieldtype_list[$post['dtype']]) && 1 == $fieldtype_list[$post['dtype']]['ifoption']) {
  776. if (empty($dfvalue)) {
  777. $this->error("你设定了字段为【" . $fieldtype_list[$post['dtype']]['title'] . "】类型,默认值不能为空! ");
  778. }
  779. }
  780. /*--end*/
  781. /*栏目对应的单页表*/
  782. $tableExt = PREFIX . 'single_content';
  783. /*--end*/
  784. /*检测字段是否存在于主表与附加表中*/
  785. if (true == $this->fieldLogic->checkChannelFieldList($tableExt, $post['name'], 6)) {
  786. $this->error("字段名称 " . $post['name'] . " 与系统字段冲突!");
  787. }
  788. /*--end*/
  789. /*组装完整的SQL语句,并执行新增字段*/
  790. $fieldinfos = $this->fieldLogic->GetFieldMake($post['dtype'], $post['name'], $dfvalue, $post['title']);
  791. $ntabsql = $fieldinfos[0];
  792. $buideType = $fieldinfos[1];
  793. $maxlength = $fieldinfos[2];
  794. $table = PREFIX . 'arctype';
  795. $sql = " ALTER TABLE `$table` ADD $ntabsql ";
  796. if (false !== Db::execute($sql)) {
  797. /*保存新增字段的记录*/
  798. $newData = array(
  799. 'dfvalue' => $dfvalue,
  800. 'maxlength' => $maxlength,
  801. 'define' => $buideType,
  802. 'ifmain' => 1,
  803. 'ifsystem' => 0,
  804. 'sort_order' => 100,
  805. 'add_time' => getTime(),
  806. 'update_time' => getTime(),
  807. );
  808. $data = array_merge($post, $newData);
  809. M('channelfield')->save($data);
  810. /*--end*/
  811. /*重新生成数据表字段缓存文件*/
  812. try {
  813. schemaTable($table);
  814. } catch (\Exception $e) {}
  815. /*--end*/
  816. \think\Cache::clear('channelfield');
  817. \think\Cache::clear("arctype");
  818. $this->success("操作成功!", url('Field/arctype_index'));
  819. }
  820. $this->error('操作失败');
  821. }
  822. /*字段类型列表*/
  823. $fieldtype_list = [];
  824. $fieldtype_list_tmp = model('Field')->getFieldTypeAll('name,title,ifoption');
  825. foreach ($fieldtype_list_tmp as $key => $val) {
  826. if (!in_array($val['name'], ['file','media'])) {
  827. $fieldtype_list[] = $val;
  828. }
  829. }
  830. $assign_data['fieldtype_list'] = $fieldtype_list;
  831. /*--end*/
  832. /*模型ID*/
  833. $assign_data['channel_id'] = $channel_id;
  834. /*--end*/
  835. $this->assign($assign_data);
  836. return $this->fetch();
  837. }
  838. /**
  839. * 编辑-栏目字段
  840. */
  841. public function arctype_edit()
  842. {
  843. $channel_id = $this->arctype_channel_id;
  844. if (empty($channel_id)) {
  845. $this->error('参数有误!');
  846. }
  847. if (IS_POST) {
  848. $post = input('post.', '', 'trim');
  849. if ('checkbox' == $post['old_dtype'] && in_array($post['dtype'], ['radio', 'select'])) {
  850. $fieldtype_list = model('Field')->getFieldTypeAll('name,title', 'name');
  851. $this->error("{$fieldtype_list['checkbox']['title']}不能更改为{$fieldtype_list[$post['dtype']]['title']}!");
  852. }
  853. if (empty($post['dtype']) || empty($post['title']) || empty($post['name'])) {
  854. $this->error("缺少必填信息!");
  855. }
  856. if (1 == preg_match('/^([_]+|[0-9]+)$/', $post['name'])) {
  857. $this->error("字段名称格式不正确!");
  858. } else if (preg_match('/^ey_/', $post['name'])) {
  859. $this->error("字段名称不允许以 ey_ 开头!");
  860. }
  861. $info = model('Channelfield')->getInfo($post['id'], 'ifsystem');
  862. if (!empty($info['ifsystem'])) {
  863. $this->error('系统字段不允许更改!');
  864. }
  865. $old_name = $post['old_name'];
  866. /*去除中文逗号,过滤左右空格与空值*/
  867. $dfvalue = str_replace(',', ',', $post['dfvalue']);
  868. $dfvalue = func_preg_replace(['"', '\'', ';'], '', $dfvalue);
  869. $dfvalueArr = explode(',', $dfvalue);
  870. foreach ($dfvalueArr as $key => $val) {
  871. $tmp_val = trim($val);
  872. if (empty($tmp_val)) {
  873. unset($dfvalueArr[$key]);
  874. continue;
  875. }
  876. $dfvalueArr[$key] = $tmp_val;
  877. }
  878. $dfvalueArr = array_unique($dfvalueArr);
  879. $dfvalue = implode(',', $dfvalueArr);
  880. /*--end*/
  881. /*默认值必填字段*/
  882. $fieldtype_list = model('Field')->getFieldTypeAll('name,title,ifoption', 'name');
  883. if (isset($fieldtype_list[$post['dtype']]) && 1 == $fieldtype_list[$post['dtype']]['ifoption']) {
  884. if (empty($dfvalue)) {
  885. $this->error("你设定了字段为【" . $fieldtype_list[$post['dtype']]['title'] . "】类型,默认值不能为空! ");
  886. }
  887. }
  888. /*--end*/
  889. /*栏目对应的单页表*/
  890. $tableExt = PREFIX . 'single_content';
  891. /*--end*/
  892. /*检测字段是否存在于主表与附加表中*/
  893. if (true == $this->fieldLogic->checkChannelFieldList($tableExt, $post['name'], 6, array($old_name))) {
  894. $this->error("字段名称 " . $post['name'] . " 与系统字段冲突!");
  895. }
  896. /*--end*/
  897. /*针对单选项、多选项、下拉框:修改之前,将该字段不存在的值都更新为默认值第一个*/
  898. if (in_array($post['old_dtype'], ['radio', 'select', 'checkbox']) && in_array($post['dtype'], ['radio', 'select', 'checkbox'])) {
  899. $whereArr = [];
  900. $dfvalueArr = explode(',', $dfvalue);
  901. foreach($dfvalueArr as $key => $val){
  902. $whereArr[] = "`{$post['name']}` <> '{$val}'";
  903. }
  904. $whereStr = implode(' OR ', $whereArr);
  905. if (in_array($post['dtype'], ['radio', 'select', 'checkbox'])) {
  906. $new_dfvalue = NULL;
  907. } else {
  908. $new_dfvalue = '';
  909. }
  910. Db::name('single_content')->where($whereStr)->update([$post['name']=>$new_dfvalue]);
  911. }
  912. /*end*/
  913. /*组装完整的SQL语句,并执行编辑字段*/
  914. $fieldinfos = $this->fieldLogic->GetFieldMake($post['dtype'], $post['name'], $dfvalue, $post['title']);
  915. $ntabsql = $fieldinfos[0];
  916. $buideType = $fieldinfos[1];
  917. $maxlength = $fieldinfos[2];
  918. $table = PREFIX . 'arctype';
  919. $sql = " ALTER TABLE `$table` CHANGE COLUMN `{$old_name}` $ntabsql ";
  920. if (false !== Db::execute($sql)) {
  921. /*针对单选项、多选项、下拉框:修改之前,将该字段不存在的值都更新为默认值第一个*/
  922. if (in_array($post['old_dtype'], ['radio', 'select', 'checkbox']) && in_array($post['dtype'], ['radio', 'select', 'checkbox'])) {
  923. $whereArr = [];
  924. $new_dfvalue = '';
  925. $dfvalueArr = explode(',', $dfvalue);
  926. foreach($dfvalueArr as $key => $val){
  927. if ($key == 0) {
  928. $new_dfvalue = $val;
  929. }
  930. $whereArr[] = "`{$post['name']}` <> '{$val}'";
  931. }
  932. $whereArr[] = "`{$post['name']}` is NULL";
  933. $whereArr[] = "`{$post['name']}` = ''";
  934. $whereStr = implode(' OR ', $whereArr);
  935. Db::name('single_content')->where($whereStr)->update([$post['name']=>$new_dfvalue]);
  936. }
  937. /*end*/
  938. /*保存更新字段的记录*/
  939. $newData = array(
  940. 'dfvalue' => $dfvalue,
  941. 'maxlength' => $maxlength,
  942. 'define' => $buideType,
  943. 'ifmain' => 1,
  944. 'ifsystem' => 0,
  945. 'update_time' => getTime(),
  946. );
  947. $data = array_merge($post, $newData);
  948. M('channelfield')->where('id', $post['id'])->cache(true, null, "channelfield")->save($data);
  949. /*--end*/
  950. /*重新生成数据表字段缓存文件*/
  951. try {
  952. schemaTable($table);
  953. } catch (\Exception $e) {}
  954. /*--end*/
  955. \think\Cache::clear("arctype");
  956. $this->success("操作成功!", url('Field/arctype_index'));
  957. } else {
  958. $sql = " ALTER TABLE `$table` ADD $ntabsql ";
  959. if (false === Db::execute($sql)) {
  960. $this->error('操作失败!');
  961. }
  962. }
  963. }
  964. $id = input('param.id/d', 0);
  965. $info = array();
  966. if (!empty($id)) {
  967. $info = model('Channelfield')->getInfo($id);
  968. }
  969. if (!empty($info['ifsystem'])) {
  970. $this->error('系统字段不允许更改!');
  971. }
  972. $assign_data['info'] = $info;
  973. /*字段类型列表*/
  974. $fieldtype_list = [];
  975. $fieldtype_list_tmp = model('Field')->getFieldTypeAll('name,title,ifoption');
  976. foreach ($fieldtype_list_tmp as $key => $val) {
  977. if (!in_array($val['name'], ['file','media'])) {
  978. $fieldtype_list[] = $val;
  979. }
  980. }
  981. $assign_data['fieldtype_list'] = $fieldtype_list;
  982. /*--end*/
  983. /*模型ID*/
  984. $assign_data['channel_id'] = $channel_id;
  985. /*--end*/
  986. $this->assign($assign_data);
  987. return $this->fetch();
  988. }
  989. /**
  990. * 删除-栏目字段
  991. */
  992. public function arctype_del()
  993. {
  994. $channel_id = $this->arctype_channel_id;
  995. $id = input('del_id/d', 0);
  996. if (!empty($id)) {
  997. /*删除表字段*/
  998. $row = $this->fieldLogic->delArctypeField($id);
  999. /*--end*/
  1000. if (0 < $row['code']) {
  1001. $map = array(
  1002. 'id' => array('eq', $id),
  1003. 'channel_id' => $channel_id,
  1004. );
  1005. $result = M('channelfield')->field('channel_id,name')->where($map)->select();
  1006. $name_list = get_arr_column($result, 'name');
  1007. /*删除字段的记录*/
  1008. M('channelfield')->where($map)->delete();
  1009. /*--end*/
  1010. adminLog('删除栏目字段:' . implode(',', $name_list));
  1011. $this->success('删除成功');
  1012. }
  1013. \think\Cache::clear('channelfield');
  1014. \think\Cache::clear("arctype");
  1015. respose(array('status' => 0, 'msg' => $row['msg']));
  1016. } else {
  1017. $this->error('参数有误');
  1018. }
  1019. }
  1020. /**
  1021. * 留言表单表单列表
  1022. */
  1023. public function attribute_index()
  1024. {
  1025. $assign_data = array();
  1026. $condition = array();
  1027. // 获取到所有GET参数
  1028. $get = input('get.');
  1029. $typeid = input('typeid/d');
  1030. // 应用搜索条件
  1031. foreach (['keywords', 'typeid'] as $key) {
  1032. if (isset($get[$key]) && $get[$key] !== '') {
  1033. if ($key == 'keywords') {
  1034. $condition['a.attr_name'] = array('LIKE', "%{$get[$key]}%");
  1035. } else if ($key == 'typeid') {
  1036. $typeids = model('Arctype')->getHasChildren($get[$key]);
  1037. $condition['a.typeid'] = array('IN', array_keys($typeids));
  1038. } else {
  1039. $condition['a.' . $key] = array('eq', $get[$key]);
  1040. }
  1041. }
  1042. }
  1043. $condition['b.id'] = ['gt', 0];
  1044. $condition['a.is_del'] = 0;
  1045. // 多语言
  1046. $condition['a.lang'] = $this->admin_lang;
  1047. /**
  1048. * 数据查询,搜索出主键ID的值
  1049. */
  1050. $count = DB::name('guestbook_attribute')->alias('a')
  1051. ->join('__ARCTYPE__ b', 'a.typeid = b.id', 'LEFT')
  1052. ->where($condition)
  1053. ->count();// 查询满足要求的总记录数
  1054. $Page = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
  1055. $list = DB::name('guestbook_attribute')
  1056. ->field("a.attr_id")
  1057. ->alias('a')
  1058. ->join('__ARCTYPE__ b', 'a.typeid = b.id', 'LEFT')
  1059. ->where($condition)
  1060. ->order('a.typeid desc, a.sort_order asc, a.attr_id asc')
  1061. ->limit($Page->firstRow . ',' . $Page->listRows)
  1062. ->getAllWithIndex('attr_id');
  1063. /**
  1064. * 完善数据集信息
  1065. * 在数据量大的情况下,经过优化的搜索逻辑,先搜索出主键ID,再通过ID将其他信息补充完整;
  1066. */
  1067. if ($list) {
  1068. $attr_ida = array_keys($list);
  1069. $fields = "b.*, a.*";
  1070. $row = DB::name('guestbook_attribute')
  1071. ->field($fields)
  1072. ->alias('a')
  1073. ->join('__ARCTYPE__ b', 'a.typeid = b.id', 'LEFT')
  1074. ->where('a.attr_id', 'in', $attr_ida)
  1075. ->getAllWithIndex('attr_id');
  1076. /*获取多语言关联绑定的值*/
  1077. $row = model('LanguageAttr')->getBindValue($row, 'guestbook_attribute', $this->main_lang); // 多语言
  1078. /*--end*/
  1079. foreach ($row as $key => $val) {
  1080. $val['fieldname'] = 'attr_' . $val['attr_id'];
  1081. $row[$key] = $val;
  1082. }
  1083. foreach ($list as $key => $val) {
  1084. $list[$key] = $row[$val['attr_id']];
  1085. }
  1086. }
  1087. $show = $Page->show(); // 分页显示输出
  1088. $assign_data['page'] = $show; // 赋值分页输出
  1089. $assign_data['list'] = $list; // 赋值数据集
  1090. $assign_data['pager'] = $Page; // 赋值分页对象
  1091. /*获取当前模型栏目*/
  1092. $select_html = allow_release_arctype($typeid, array(8));
  1093. $typeidNum = substr_count($select_html, '</option>');
  1094. $this->assign('select_html', $select_html);
  1095. $this->assign('typeidNum', $typeidNum);
  1096. /*--end*/
  1097. // 栏目ID
  1098. $assign_data['typeid'] = $typeid; // 栏目ID
  1099. /*当前栏目信息*/
  1100. $arctype_info = array();
  1101. if ($typeid > 0) {
  1102. $arctype_info = Db::name('arctype')->field('typename')->find($typeid);
  1103. }
  1104. $assign_data['arctype_info'] = $arctype_info;
  1105. /*--end*/
  1106. $assign_data['attrInputTypeArr'] = config('global.guestbook_attr_input_type'); // 表单类型
  1107. //留言模型的栏目数量
  1108. $assign_data['arctypeCount'] = Db::name('arctype')->where([
  1109. 'current_channel' => 6,
  1110. 'is_del' => 0,
  1111. 'lang' => $this->admin_lang,
  1112. ])->count();
  1113. $this->assign($assign_data);
  1114. return $this->fetch();
  1115. }
  1116. /**
  1117. * 新增留言表单
  1118. */
  1119. public function attribute_add()
  1120. {
  1121. //防止php超时
  1122. function_exists('set_time_limit') && set_time_limit(0);
  1123. $this->language_access(); // 多语言功能操作权限
  1124. if (IS_AJAX && IS_POST)//ajax提交验证
  1125. {
  1126. $model = model('GuestbookAttribute');
  1127. $attr_values = str_replace('_', '', input('attr_values')); // 替换特殊字符
  1128. $attr_values = str_replace('@', '', $attr_values); // 替换特殊字符
  1129. $attr_values = trim($attr_values);
  1130. /*过滤重复值*/
  1131. $attr_values_arr = explode(PHP_EOL, $attr_values);
  1132. foreach ($attr_values_arr as $key => $val) {
  1133. $tmp_val = trim($val);
  1134. if (empty($tmp_val)) {
  1135. unset($attr_values_arr[$key]);
  1136. continue;
  1137. }
  1138. $attr_values_arr[$key] = $tmp_val;
  1139. }
  1140. $attr_values_arr = array_unique($attr_values_arr);
  1141. $attr_values = implode(PHP_EOL, $attr_values_arr);
  1142. /*end*/
  1143. $post_data = input('post.');
  1144. $post_data['attr_values'] = $attr_values;
  1145. $attr_input_type = isset($post_data['attr_input_type']) ? $post_data['attr_input_type'] : 0;
  1146. /*前台输入是否JS验证*/
  1147. $validate_type = 0;
  1148. $validate_type_list = config("global.validate_type_list"); // 前台输入验证类型
  1149. if (!empty($validate_type_list[$attr_input_type])) {
  1150. $validate_type = $attr_input_type;
  1151. }
  1152. /*end*/
  1153. $savedata = array(
  1154. 'attr_name' => $post_data['attr_name'],
  1155. 'typeid' => $post_data['typeid'],
  1156. 'attr_input_type' => $attr_input_type,
  1157. 'attr_values' => isset($post_data['attr_values']) ? $post_data['attr_values'] : '',
  1158. 'sort_order' => 100,
  1159. 'is_showlist' => $post_data['is_showlist'],
  1160. 'required' => $post_data['required'],
  1161. 'validate_type' => $validate_type,
  1162. 'lang' => $this->admin_lang,
  1163. 'add_time' => getTime(),
  1164. 'update_time' => getTime(),
  1165. );
  1166. // 数据验证
  1167. $validate = \think\Loader::validate('GuestbookAttribute');
  1168. if (!$validate->batch()->check($savedata)) {
  1169. $error = $validate->getError();
  1170. $error_msg = array_values($error);
  1171. $return_arr = array(
  1172. 'status' => -1,
  1173. 'msg' => $error_msg[0],
  1174. 'data' => $error,
  1175. );
  1176. respose($return_arr);
  1177. } else {
  1178. $model->data($savedata, true);// 收集数据
  1179. $model->save(); // 写入数据到数据库
  1180. $insertId = $model->getLastInsID();
  1181. /*同步留言属性ID到多语言的模板变量里*/
  1182. model('GuestbookAttribute')->syn_add_language_attribute($insertId);
  1183. /*--end*/
  1184. $return_arr = array(
  1185. 'status' => 1,
  1186. 'msg' => '操作成功',
  1187. 'data' => array('url' => url('Field/attribute_index', array('typeid' => $post_data['typeid']))),
  1188. );
  1189. adminLog('新增留言表单:' . $savedata['attr_name']);
  1190. respose($return_arr);
  1191. }
  1192. }
  1193. $typeid = input('param.typeid/d', 0);
  1194. if ($typeid > 0) {
  1195. $select_html = M('arctype')->where('id', $typeid)->getField('typename');
  1196. $select_html = !empty($select_html) ? $select_html : '该栏目不存在';
  1197. } else {
  1198. $select_html = allow_release_arctype($typeid, array(8));
  1199. }
  1200. $assign_data['select_html'] = $select_html; //
  1201. $assign_data['typeid'] = $typeid; // 栏目ID
  1202. $assign_data['attrInputTypeArr'] = config('global.guestbook_attr_input_type'); // 表单类型
  1203. $this->assign($assign_data);
  1204. return $this->fetch();
  1205. }
  1206. /**
  1207. * 编辑留言表单
  1208. */
  1209. public function attribute_edit()
  1210. {
  1211. if (IS_AJAX && IS_POST)//ajax提交验证
  1212. {
  1213. $model = model('GuestbookAttribute');
  1214. $attr_values = str_replace('_', '', input('attr_values')); // 替换特殊字符
  1215. $attr_values = str_replace('@', '', $attr_values); // 替换特殊字符
  1216. $attr_values = trim($attr_values);
  1217. /*过滤重复值*/
  1218. $attr_values_arr = explode(PHP_EOL, $attr_values);
  1219. foreach ($attr_values_arr as $key => $val) {
  1220. $tmp_val = trim($val);
  1221. if (empty($tmp_val)) {
  1222. unset($attr_values_arr[$key]);
  1223. continue;
  1224. }
  1225. $attr_values_arr[$key] = $tmp_val;
  1226. }
  1227. $attr_values_arr = array_unique($attr_values_arr);
  1228. $attr_values = implode(PHP_EOL, $attr_values_arr);
  1229. /*end*/
  1230. $post_data = input('post.');
  1231. $post_data['attr_values'] = $attr_values;
  1232. $attr_input_type = isset($post_data['attr_input_type']) ? $post_data['attr_input_type'] : 0;
  1233. /*前台输入是否JS验证*/
  1234. $validate_type = 0;
  1235. $validate_type_list = config("global.validate_type_list"); // 前台输入验证类型
  1236. if (!empty($validate_type_list[$attr_input_type])) {
  1237. $validate_type = $attr_input_type;
  1238. }
  1239. /*end*/
  1240. $savedata = array(
  1241. 'attr_id' => $post_data['attr_id'],
  1242. 'attr_name' => $post_data['attr_name'],
  1243. 'typeid' => $post_data['typeid'],
  1244. 'attr_input_type' => $attr_input_type,
  1245. 'attr_values' => isset($post_data['attr_values']) ? $post_data['attr_values'] : '',
  1246. 'is_showlist' => $post_data['is_showlist'],
  1247. 'required' => $post_data['required'],
  1248. 'validate_type' => $validate_type,
  1249. 'sort_order' => 100,
  1250. 'update_time' => getTime(),
  1251. );
  1252. // 数据验证
  1253. $validate = \think\Loader::validate('GuestbookAttribute');
  1254. if (!$validate->batch()->check($savedata)) {
  1255. $error = $validate->getError();
  1256. $error_msg = array_values($error);
  1257. $return_arr = array(
  1258. 'status' => -1,
  1259. 'msg' => $error_msg[0],
  1260. 'data' => $error,
  1261. );
  1262. respose($return_arr);
  1263. } else {
  1264. $model->data($savedata, true); // 收集数据
  1265. $model->isUpdate(true, [
  1266. 'attr_id' => $post_data['attr_id'],
  1267. 'lang' => $this->admin_lang,
  1268. ])->save(); // 写入数据到数据库
  1269. $return_arr = array(
  1270. 'status' => 1,
  1271. 'msg' => '操作成功',
  1272. 'data' => array('url' => url('Field/attribute_index', array('typeid' => $post_data['typeid']))),
  1273. );
  1274. adminLog('编辑留言表单:' . $savedata['attr_name']);
  1275. respose($return_arr);
  1276. }
  1277. }
  1278. $assign_data = array();
  1279. $id = input('id/d');
  1280. /*获取多语言关联绑定的值*/
  1281. $new_id = model('LanguageAttr')->getBindValue($id, 'guestbook_attribute'); // 多语言
  1282. !empty($new_id) && $id = $new_id;
  1283. /*--end*/
  1284. $info = M('GuestbookAttribute')->where([
  1285. 'attr_id' => $id,
  1286. 'lang' => $this->admin_lang,
  1287. ])->find();
  1288. if (empty($info)) {
  1289. $this->error('数据不存在,请联系管理员!');
  1290. exit;
  1291. }
  1292. $assign_data['field'] = $info;
  1293. // 所在栏目
  1294. $select_html = M('arctype')->where('id', $info['typeid'])->getField('typename');
  1295. $select_html = !empty($select_html) ? $select_html : '该栏目不存在';
  1296. $assign_data['select_html'] = $select_html;
  1297. $assign_data['attrInputTypeArr'] = config('global.guestbook_attr_input_type'); // 表单类型
  1298. $this->assign($assign_data);
  1299. return $this->fetch();
  1300. }
  1301. /**
  1302. * 删除留言表单
  1303. */
  1304. public function attribute_del()
  1305. {
  1306. $this->language_access(); // 多语言功能操作权限
  1307. $id_arr = input('del_id/a');
  1308. $id_arr = eyIntval($id_arr);
  1309. if (!empty($id_arr)) {
  1310. /*多语言*/
  1311. if (is_language()) {
  1312. $attr_name_arr = [];
  1313. foreach ($id_arr as $key => $val) {
  1314. $attr_name_arr[] = 'attr_' . $val;
  1315. }
  1316. $new_id_arr = Db::name('language_attr')->where([
  1317. 'attr_name' => ['IN', $attr_name_arr],
  1318. 'attr_group' => 'guestbook_attribute',
  1319. ])->column('attr_value');
  1320. !empty($new_id_arr) && $id_arr = $new_id_arr;
  1321. }
  1322. /*--end*/
  1323. $r = Db::name('GuestbookAttribute')->where([
  1324. 'attr_id' => ['IN', $id_arr],
  1325. ])->update([
  1326. 'is_del' => 1,
  1327. 'update_time' => getTime(),
  1328. ]);
  1329. if ($r) {
  1330. adminLog('删除留言表单-id:' . implode(',', $id_arr));
  1331. $this->success('删除成功');
  1332. } else {
  1333. $this->error('删除失败');
  1334. }
  1335. } else {
  1336. $this->error('参数有误');
  1337. }
  1338. }
  1339. /**
  1340. * 检测列表显示字段数量是都超过4个
  1341. */
  1342. public function ajax_attribute_show()
  1343. {
  1344. if (IS_AJAX_POST) {
  1345. $typeid = input('post.typeid/d');
  1346. $is_showlist = input('post.is_showlist/d');
  1347. if ($is_showlist == 1){
  1348. $count = Db::name('guestbook_attribute')->where([
  1349. 'typeid' => $typeid,
  1350. 'is_showlist' => $is_showlist,
  1351. 'lang' => $this->admin_lang,
  1352. ])->count();
  1353. if ($count >= 4) {
  1354. $this->error('所属栏目的列表字段显示数量已达4个');
  1355. } else {
  1356. $this->success('正常');
  1357. }
  1358. }
  1359. $this->success('正常');
  1360. }
  1361. $this->error('非法访问');
  1362. }
  1363. }