心理咨询网
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.

ContentSortController.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2017年12月26日
  7. * 内容栏目控制器
  8. */
  9. namespace app\admin\controller\content;
  10. use core\basic\Controller;
  11. use app\admin\model\content\ContentSortModel;
  12. class ContentSortController extends Controller
  13. {
  14. private $count;
  15. private $blank;
  16. private $outData = array();
  17. private $model;
  18. public function __construct()
  19. {
  20. $this->model = new ContentSortModel();
  21. }
  22. // 内容栏目列表
  23. public function index()
  24. {
  25. $this->assign('list', true);
  26. $tree = $this->model->getList();
  27. $sorts = $this->makeSortList($tree);
  28. $this->assign('sorts', $sorts);
  29. // 内容模型
  30. $models = model('admin.content.Model');
  31. $this->assign('allmodels', $models->getSelectAll());
  32. $this->assign('models', $models->getSelect());
  33. // 内容栏目下拉表
  34. $sort_tree = $this->model->getSelect();
  35. $sort_select = $this->makeSortSelect($sort_tree);
  36. $this->assign('sort_select', $sort_select);
  37. // 模板文件
  38. $htmldir = $this->config('tpl_html_dir') ? '/' . $this->config('tpl_html_dir') : '';
  39. $this->assign('tpls', file_list(ROOT_PATH . current($this->config('tpl_dir')) . '/' . $this->model->getTheme() . $htmldir));
  40. // 前端地址连接符判断
  41. $url_break_char = $this->config('url_break_char') ?: '_';
  42. $this->assign('url_break_char', $url_break_char);
  43. // 获取会员分组
  44. $this->assign('groups', model('admin.member.MemberGroup')->getSelect());
  45. $this->display('content/contentsort.html');
  46. }
  47. // 生成无限级内容栏目列表
  48. private function makeSortList($tree)
  49. {
  50. // 循环生成
  51. foreach ($tree as $value) {
  52. $this->count ++;
  53. $this->outData[$this->count] = new \stdClass();
  54. foreach ($value as $k => $v) {
  55. $this->outData[$this->count]->$k = $v;
  56. }
  57. if ($value->son) {
  58. $this->outData[$this->count]->son = true;
  59. } else {
  60. $this->outData[$this->count]->son = false;
  61. }
  62. // 子菜单处理
  63. if ($value->son) {
  64. $this->blank .= '  ';
  65. $this->makeSortList($value->son);
  66. }
  67. }
  68. // 循环完后回归缩进位置
  69. $this->blank = substr($this->blank, 6);
  70. return $this->outData;
  71. }
  72. // 内容栏目增加
  73. public function add()
  74. {
  75. if ($_POST) {
  76. if (! ! $multiplename = post('multiplename')) {
  77. $multiplename = str_replace(',', ',', $multiplename);
  78. $pcode = post('pcode', 'var');
  79. $type = post('type');
  80. $mcode = post('mcode');
  81. $listtpl = basename(post('listtpl'));
  82. $contenttpl = basename(post('contenttpl'));
  83. $status = post('status');
  84. if (! $pcode) { // 父编码默认为0
  85. $pcode = 0;
  86. }
  87. if (! $mcode) {
  88. alert_back('栏目模型必须选择!');
  89. }
  90. if (! $type) {
  91. alert_back('栏目类型不能为空!');
  92. }
  93. $names = explode(',', $multiplename);
  94. $lastcode = $this->model->getLastCode();
  95. $scode = get_auto_code($lastcode);
  96. foreach ($names as $key => $value) {
  97. $data[] = array(
  98. 'acode' => session('acode'),
  99. 'pcode' => $pcode,
  100. 'scode' => $scode,
  101. 'name' => $value,
  102. 'mcode' => $mcode,
  103. 'listtpl' => $listtpl,
  104. 'contenttpl' => $contenttpl,
  105. 'status' => $status,
  106. 'gid' => 0,
  107. 'gtype' => 4,
  108. 'subname' => '',
  109. 'filename' => '',
  110. 'outlink' => '',
  111. 'ico' => '',
  112. 'pic' => '',
  113. 'title' => '',
  114. 'keywords' => '',
  115. 'description' => '',
  116. 'sorting' => 255,
  117. 'create_user' => session('username'),
  118. 'update_user' => session('username')
  119. );
  120. $scode = get_auto_code($scode);
  121. }
  122. } else {
  123. // 获取数据
  124. $scode = get_auto_code($this->model->getLastCode()); // 自动编码;
  125. $pcode = post('pcode', 'var');
  126. $name = post('name');
  127. $type = post('type');
  128. $mcode = post('mcode');
  129. $listtpl = basename(post('listtpl'));
  130. $contenttpl = basename(post('contenttpl'));
  131. $status = post('status');
  132. $subname = post('subname');
  133. $filename = trim(post('filename'), '/');
  134. $outlink = post('outlink');
  135. $ico = post('ico');
  136. $pic = post('pic');
  137. $title = post('title');
  138. $keywords = post('keywords');
  139. $description = post('description');
  140. $gid = post('gid', 'int') ?: 0;
  141. $gtype = post('gtype', 'int') ?: 4;
  142. $gnote = post('gnote');
  143. $def1 = post('def1');
  144. $def2 = post('def2');
  145. $def3 = post('def3');
  146. if (! $scode) {
  147. alert_back('编码不能为空!');
  148. }
  149. if (! $pcode) { // 父编码默认为0
  150. $pcode = 0;
  151. }
  152. if (! $name) {
  153. alert_back('栏目名不能为空!');
  154. }
  155. if (! $mcode) {
  156. alert_back('栏目模型必须选择!');
  157. }
  158. if (! $type) {
  159. alert_back('栏目类型不能为空!');
  160. }
  161. if ($filename && ! preg_match('/^[a-zA-Z0-9\-\/]+$/', $filename)) {
  162. alert_back('URL名称只允许字母、数字、横线、斜线组成!');
  163. }
  164. if ($filename && $this->model->checkUrlname($filename)) {
  165. alert_back('URL名称与模型URL名称冲突,请换一个名称!');
  166. }
  167. // 缩放缩略图
  168. if ($ico) {
  169. resize_img(ROOT_PATH . $ico, '', $this->config('ico.max_width'), $this->config('ico.max_height'));
  170. }
  171. // 检查编码
  172. if ($this->model->checkSort("scode='$scode'")) {
  173. alert_back('该内容栏目编号已经存在,不能再使用!');
  174. }
  175. // 检查自定义URL名称
  176. if ($filename) {
  177. while ($this->model->checkFilename($filename)) {
  178. $filename = $filename . '_' . mt_rand(1, 20);
  179. }
  180. }
  181. // 构建数据
  182. $data = array(
  183. 'acode' => session('acode'),
  184. 'pcode' => $pcode,
  185. 'scode' => $scode,
  186. 'name' => $name,
  187. 'mcode' => $mcode,
  188. 'listtpl' => $listtpl,
  189. 'contenttpl' => $contenttpl,
  190. 'status' => $status,
  191. 'gid' => $gid,
  192. 'gtype' => $gtype,
  193. 'gnote' => $gnote,
  194. 'subname' => $subname,
  195. 'def1' => $def1,
  196. 'def2' => $def2,
  197. 'def3' => $def3,
  198. 'filename' => $filename,
  199. 'outlink' => $outlink,
  200. 'ico' => $ico,
  201. 'pic' => $pic,
  202. 'title' => $title,
  203. 'keywords' => $keywords,
  204. 'description' => $description,
  205. 'sorting' => 255,
  206. 'create_user' => session('username'),
  207. 'update_user' => session('username')
  208. );
  209. }
  210. // 执行添加
  211. if ($this->model->addSort($data)) {
  212. if ($type == 1 && ! $outlink) { // 在填写了外链时不生成单页
  213. if ($multiplename) {
  214. foreach ($data as $key => $value) {
  215. $this->addSingle($value['scode'], $value['name']);
  216. }
  217. } else {
  218. $this->addSingle($scode, $name);
  219. }
  220. }
  221. $this->log('新增数据内容栏目' . $scode . '成功!');
  222. success('新增成功!', url('/admin/ContentSort/index'));
  223. } else {
  224. $this->log('新增数据内容栏目' . $scode . '失败!');
  225. error('新增失败!', - 1);
  226. }
  227. }
  228. }
  229. // 生成内容栏目下拉选择
  230. private function makeSortSelect($tree, $selectid = null)
  231. {
  232. $list_html = '';
  233. foreach ($tree as $value) {
  234. // 默认选择项
  235. if ($selectid == $value->scode) {
  236. $select = "selected='selected'";
  237. } else {
  238. $select = '';
  239. }
  240. if (get('scode') != $value->scode) { // 不显示本身,避免出现自身为自己的父节点
  241. $list_html .= "<option value='{$value->scode}' $select>{$this->blank}{$value->name}</option>";
  242. }
  243. // 子菜单处理
  244. if ($value->son) {
  245. $this->blank .= '  ';
  246. $list_html .= $this->makeSortSelect($value->son, $selectid);
  247. }
  248. }
  249. // 循环完后回归位置
  250. $this->blank = substr($this->blank, 0, - 6);
  251. return $list_html;
  252. }
  253. // 内容栏目删除
  254. public function del()
  255. {
  256. // 执行批量删除
  257. if ($_POST) {
  258. if (! ! $list = post('list')) {
  259. if ($this->model->delSortList($list)) {
  260. $this->log('批量删除栏目成功!');
  261. success('批量删除成功!', - 1);
  262. } else {
  263. $this->log('批量删除栏目失败!');
  264. error('批量删除失败!', - 1);
  265. }
  266. } else {
  267. alert_back('请选择要删除的栏目!');
  268. }
  269. }
  270. if (! $scode = get('scode', 'var')) {
  271. error('传递的参数值错误!', - 1);
  272. }
  273. if ($this->model->delSort($scode)) {
  274. $this->log('删除数据内容栏目' . $scode . '成功!');
  275. success('删除成功!', - 1);
  276. } else {
  277. $this->log('删除数据内容栏目' . $scode . '失败!');
  278. error('删除失败!', - 1);
  279. }
  280. }
  281. // 内容栏目修改
  282. public function mod()
  283. {
  284. if (! ! $submit = post('submit')) {
  285. switch ($submit) {
  286. case 'sorting': // 修改列表排序
  287. $listall = post('listall');
  288. if ($listall) {
  289. $sorting = post('sorting');
  290. foreach ($listall as $key => $value) {
  291. if ($sorting[$key] === '' || ! is_numeric($sorting[$key]))
  292. $sorting[$key] = 255;
  293. $this->model->modSortSorting($value, "sorting=" . $sorting[$key]);
  294. }
  295. $this->log('批量修改栏目排序成功!');
  296. success('修改成功!', - 1);
  297. } else {
  298. alert_back('排序失败,无任何内容!');
  299. }
  300. break;
  301. }
  302. }
  303. if (! $scode = get('scode', 'var')) {
  304. error('传递的参数值错误!', - 1);
  305. }
  306. // 单独修改状态
  307. if (($field = get('field', 'var')) && ! is_null($value = get('value', 'var'))) {
  308. if ($this->model->modSort($scode, "$field='$value',update_user='" . session('username') . "'")) {
  309. $this->log('修改数据内容栏目' . $scode . '状态' . $value . '成功!');
  310. location(- 1);
  311. } else {
  312. $this->log('修改数据内容栏目' . $scode . '状态' . $value . '失败!');
  313. alert_back('修改失败!');
  314. }
  315. }
  316. // 修改操作
  317. if ($_POST) {
  318. // 获取数据
  319. $pcode = post('pcode', 'var');
  320. $name = post('name');
  321. $mcode = post('mcode');
  322. $type = post('type');
  323. $listtpl = basename(post('listtpl'));
  324. $contenttpl = basename(post('contenttpl'));
  325. $status = post('status');
  326. $subname = post('subname');
  327. $filename = trim(post('filename'), '/');
  328. $outlink = post('outlink');
  329. $ico = post('ico');
  330. $pic = post('pic');
  331. $title = post('title');
  332. $keywords = post('keywords');
  333. $description = post('description');
  334. $modsub = post('modsub', 'int');
  335. $gid = post('gid', 'int') ?: 0;
  336. $gtype = post('gtype', 'int') ?: 4;
  337. $gnote = post('gnote');
  338. $def1 = post('def1');
  339. $def2 = post('def2');
  340. $def3 = post('def3');
  341. if (! $pcode) { // 父编码默认为0
  342. $pcode = 0;
  343. }
  344. if (! $name) {
  345. alert_back('栏目名不能为空!');
  346. }
  347. if (! $mcode) {
  348. alert_back('栏目模型必须选择!');
  349. }
  350. if (! $type) {
  351. alert_back('栏目类型不能为空!');
  352. }
  353. if ($filename && ! preg_match('/^[a-zA-Z0-9\-\/]+$/', $filename)) {
  354. alert_back('URL名称只允许字母、数字、横线、斜线组成!');
  355. }
  356. if ($filename && $this->model->checkUrlname($filename)) {
  357. alert_back('URL名称与模型URL名称冲突,请换一个名称!');
  358. }
  359. // 缩放缩略图
  360. if ($ico) {
  361. resize_img(ROOT_PATH . $ico, '', $this->config('ico.max_width'), $this->config('ico.max_height'));
  362. }
  363. if ($filename) {
  364. while ($this->model->checkFilename($filename, "scode<>'$scode'")) {
  365. $filename = $filename . '-' . mt_rand(1, 20);
  366. }
  367. }
  368. // 构建数据
  369. $data = array(
  370. 'pcode' => $pcode,
  371. 'name' => $name,
  372. 'mcode' => $mcode,
  373. 'listtpl' => $listtpl,
  374. 'contenttpl' => $contenttpl,
  375. 'status' => $status,
  376. 'gid' => $gid,
  377. 'gtype' => $gtype,
  378. 'gnote' => $gnote,
  379. 'subname' => $subname,
  380. 'def1' => $def1,
  381. 'def2' => $def2,
  382. 'def3' => $def3,
  383. 'filename' => $filename,
  384. 'outlink' => $outlink,
  385. 'ico' => $ico,
  386. 'pic' => $pic,
  387. 'title' => $title,
  388. 'keywords' => $keywords,
  389. 'description' => $description,
  390. 'update_user' => session('username')
  391. );
  392. // 执行添加
  393. if ($this->model->modSort($scode, $data, $modsub)) {
  394. // 如果修改为单页并且跳转,则删除单页内容,否则判断是否存在内容,不存在则添加
  395. if ($type == 1 && $outlink) {
  396. $this->model->delContent($scode);
  397. } elseif ($type == 1 && ! $this->model->findContent($scode)) {
  398. $this->addSingle($scode, $name);
  399. }
  400. $this->log('修改数据内容栏目' . $scode . '成功!');
  401. success('修改成功!', url('/admin/ContentSort/index'));
  402. } else {
  403. location(- 1);
  404. }
  405. } else { // 调取修改内容
  406. $this->assign('mod', true);
  407. $sort = $this->model->getSort($scode);
  408. if (! $sort) {
  409. error('编辑的内容已经不存在!', - 1);
  410. }
  411. $this->assign('sort', $sort);
  412. // 父编码下拉选择
  413. $sort_tree = $this->model->getSelect();
  414. $sort_select = $this->makeSortSelect($sort_tree, $sort->pcode);
  415. $this->assign('sort_select', $sort_select);
  416. // 模板文件
  417. $htmldir = $this->config('tpl_html_dir') ? '/' . $this->config('tpl_html_dir') : '';
  418. $this->assign('tpls', file_list(ROOT_PATH . current($this->config('tpl_dir')) . '/' . $this->model->getTheme() . $htmldir));
  419. // 内容模型
  420. $models = model('admin.content.Model');
  421. $this->assign('models', $models->getSelect());
  422. // 获取会员分组
  423. $this->assign('groups', model('admin.member.MemberGroup')->getSelect());
  424. $this->display('content/contentsort.html');
  425. }
  426. }
  427. // 添加栏目时执行单页内容增加
  428. public function addSingle($scode, $title)
  429. {
  430. // 构建数据
  431. $data = array(
  432. 'acode' => session('acode'),
  433. 'scode' => $scode,
  434. 'subscode' => '',
  435. 'title' => $title,
  436. 'titlecolor' => '#333333',
  437. 'subtitle' => '',
  438. 'filename' => '',
  439. 'author' => session('realname'),
  440. 'source' => '本站',
  441. 'outlink' => '',
  442. 'date' => date('Y-m-d H:i:s'),
  443. 'ico' => '',
  444. 'pics' => '',
  445. 'content' => '',
  446. 'tags' => '',
  447. 'enclosure' => '',
  448. 'keywords' => '',
  449. 'description' => '',
  450. 'sorting' => 255,
  451. 'status' => 1,
  452. 'istop' => 0,
  453. 'isrecommend' => 0,
  454. 'isheadline' => 0,
  455. 'gid' => 0,
  456. 'gtype' => 4,
  457. 'gnote' => '',
  458. 'visits' => 0,
  459. 'likes' => 0,
  460. 'oppose' => 0,
  461. 'create_user' => session('username'),
  462. 'update_user' => session('username')
  463. );
  464. // 执行添加
  465. if ($this->model->addSingle($data)) {
  466. return true;
  467. } else {
  468. return false;
  469. }
  470. }
  471. }