Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Field.php 73KB

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