No Description
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.

SpecialNode.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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\model;
  14. use think\Db;
  15. use think\Model;
  16. /**
  17. * 专题节点
  18. */
  19. class SpecialNode extends Model
  20. {
  21. //初始化
  22. protected function initialize()
  23. {
  24. // 需要调用`Model`的`initialize`方法
  25. parent::initialize();
  26. }
  27. public function getList($aid = 0)
  28. {
  29. $result = [];
  30. if (!empty($aid)) {
  31. $map = [
  32. 'aid' => $aid,
  33. 'status' => 1,
  34. 'is_del' => 0,
  35. 'lang' => get_admin_lang(),
  36. ];
  37. $result = Db::name('special_node')->where($map)->order('sort_order asc, node_id desc')->select();
  38. }
  39. return $result;
  40. }
  41. public function saveNode($aid = 0, $specialNode = [], $opt = 'add')
  42. {
  43. if ('add' == $opt) {
  44. $this->insertNode($aid, $specialNode);
  45. } else if ('edit' == $opt) {
  46. $this->updateNode($aid, $specialNode);
  47. }
  48. }
  49. /**
  50. * 新增节点
  51. * @param integer $aid [description]
  52. * @param array $specialNode [description]
  53. * @return [type] [description]
  54. */
  55. private function insertNode($aid = 0, $specialNode = [])
  56. {
  57. if (!empty($specialNode) && !empty($aid)) {
  58. $addData = [];
  59. $admin_lang = get_admin_lang();
  60. // 处理节点名称重复,确保节点名称的唯一性
  61. $specialNodeTitleArr = $specialNode['title'];
  62. foreach ($specialNode['title'] as $_k1 => $_v1) {
  63. $num = 0;
  64. foreach ($specialNodeTitleArr as $_k2 => $_v2) {
  65. if ($_v1 == $_v2) {
  66. $num++;
  67. }
  68. }
  69. (1 < $num) && $specialNode['title'][$_k1] = $_v1.'_'.$num;
  70. unset($specialNodeTitleArr[$_k1]);
  71. }
  72. // 处理节点标识重复,确保节点标识的唯一性
  73. $specialNodeCodeArr = $specialNode['code'];
  74. foreach ($specialNode['code'] as $_k1 => $_v1) {
  75. $num = 0;
  76. foreach ($specialNodeCodeArr as $_k2 => $_v2) {
  77. if ($_v1 == $_v2) {
  78. $num++;
  79. }
  80. }
  81. (1 < $num) && $specialNode['code'][$_k1] = $_v1.'_'.$num;
  82. unset($specialNodeCodeArr[$_k1]);
  83. }
  84. foreach ($specialNode['itemid'] as $key => $itemid) {
  85. if (!empty($itemid)) {
  86. // 关键词
  87. $keywords = !empty($specialNode['keywords'][$key]) ? trim($specialNode['keywords'][$key]) : '';
  88. $keywords = str_replace(',', ',', $keywords);
  89. $keywordsArr = explode(',', $keywords);
  90. foreach ($keywordsArr as $_k1 => $_v1) {
  91. $_v1 = trim($_v1);
  92. if (!empty($_v1)) {
  93. $keywordsArr[$_k1] = $_v1;
  94. } else {
  95. unset($keywordsArr[$_k1]);
  96. }
  97. }
  98. $keywords = implode(',', $keywordsArr);
  99. // 节点文档列表
  100. $aidlist = !empty($specialNode['aidlist'][$key]) ? trim($specialNode['aidlist'][$key]) : '';
  101. $aidlist = str_replace(',', ',', $aidlist);
  102. $aidlistArr = explode(',', $aidlist);
  103. foreach ($aidlistArr as $_k1 => $_v1) {
  104. $_v1 = trim($_v1);
  105. if (!empty($_v1)) {
  106. $aidlistArr[$_k1] = $_v1;
  107. } else {
  108. unset($aidlistArr[$_k1]);
  109. }
  110. }
  111. $aidlist = implode(',', $aidlistArr);
  112. // 排序号
  113. $sort_order = 100 + $key;
  114. $addData[] = [
  115. 'aid' => $aid,
  116. 'title' => !empty($specialNode['title'][$key]) ? trim($specialNode['title'][$key]) : '默认节点',
  117. 'code' => !empty($specialNode['code'][$key]) ? trim($specialNode['code'][$key]) : 'default',
  118. 'isauto' => !empty($specialNode['isauto'][$itemid]) ? intval($specialNode['isauto'][$itemid]) : 0,
  119. 'keywords' => $keywords,
  120. 'typeid' => !empty($specialNode['typeid'][$key]) ? trim($specialNode['typeid'][$key]) : 0,
  121. 'aidlist' => !empty($aidlist) ? $aidlist : '',
  122. 'row' => !empty($specialNode['row'][$key]) ? intval($specialNode['row'][$key]) : 10,
  123. 'status' => 1,
  124. 'is_del' => 0,
  125. 'sort_order' => $sort_order,
  126. 'lang' => $admin_lang,
  127. 'add_time' => getTime(),
  128. 'update_time' => getTime(),
  129. ];
  130. }
  131. }
  132. if (!empty($addData)) {
  133. $this->saveAll($addData);
  134. }
  135. }
  136. }
  137. /**
  138. * 编辑节点
  139. * @param integer $aid [description]
  140. * @param array $specialNode [description]
  141. * @return [type] [description]
  142. */
  143. private function updateNode($aid = 0, $specialNode = [])
  144. {
  145. if (!empty($specialNode) && !empty($aid)) {
  146. $addData = $updateData = $delData = [];
  147. $admin_lang = get_admin_lang();
  148. // 处理节点名称重复,确保节点名称的唯一性
  149. $specialNodeTitleArr = $specialNode['title'];
  150. foreach ($specialNode['title'] as $_k1 => $_v1) {
  151. $num = 0;
  152. foreach ($specialNodeTitleArr as $_k2 => $_v2) {
  153. if ($_v1 == $_v2) {
  154. $num++;
  155. }
  156. }
  157. (1 < $num) && $specialNode['title'][$_k1] = $_v1.'_'.$num;
  158. unset($specialNodeTitleArr[$_k1]);
  159. }
  160. // 处理节点标识重复,确保节点标识的唯一性
  161. $specialNodeCodeArr = $specialNode['code'];
  162. foreach ($specialNode['code'] as $_k1 => $_v1) {
  163. $num = 0;
  164. foreach ($specialNodeCodeArr as $_k2 => $_v2) {
  165. if ($_v1 == $_v2) {
  166. $num++;
  167. }
  168. }
  169. (1 < $num) && $specialNode['code'][$_k1] = $_v1.'_'.$num;
  170. unset($specialNodeCodeArr[$_k1]);
  171. }
  172. foreach ($specialNode['itemid'] as $key => $itemid) {
  173. if (!empty($itemid)) {
  174. // 关键词
  175. $keywords = !empty($specialNode['keywords'][$key]) ? trim($specialNode['keywords'][$key]) : '';
  176. $keywords = str_replace(',', ',', $keywords);
  177. $keywordsArr = explode(',', $keywords);
  178. foreach ($keywordsArr as $_k1 => $_v1) {
  179. $_v1 = trim($_v1);
  180. if (!empty($_v1)) {
  181. $keywordsArr[$_k1] = $_v1;
  182. } else {
  183. unset($keywordsArr[$_k1]);
  184. }
  185. }
  186. $keywords = implode(',', $keywordsArr);
  187. // 节点文档列表
  188. $aidlist = !empty($specialNode['aidlist'][$key]) ? trim($specialNode['aidlist'][$key]) : '';
  189. $aidlist = str_replace(',', ',', $aidlist);
  190. $aidlistArr = explode(',', $aidlist);
  191. foreach ($aidlistArr as $_k1 => $_v1) {
  192. $_v1 = trim($_v1);
  193. if (!empty($_v1)) {
  194. $aidlistArr[$_k1] = $_v1;
  195. } else {
  196. unset($aidlistArr[$_k1]);
  197. }
  198. }
  199. $aidlist = implode(',', $aidlistArr);
  200. // 排序号
  201. $sort_order = 100 + $key;
  202. // 新增或者更新
  203. $node_id = !empty($specialNode['node_id'][$key]) ? intval($specialNode['node_id'][$key]) : 0;
  204. if (empty($node_id)) {
  205. $addData[] = [
  206. 'aid' => $aid,
  207. 'title' => !empty($specialNode['title'][$key]) ? trim($specialNode['title'][$key]) : '默认节点',
  208. 'code' => !empty($specialNode['code'][$key]) ? trim($specialNode['code'][$key]) : 'default',
  209. 'isauto' => !empty($specialNode['isauto'][$itemid]) ? intval($specialNode['isauto'][$itemid]) : 0,
  210. 'keywords' => $keywords,
  211. 'typeid' => !empty($specialNode['typeid'][$key]) ? trim($specialNode['typeid'][$key]) : 0,
  212. 'aidlist' => !empty($aidlist) ? $aidlist : '',
  213. 'row' => !empty($specialNode['row'][$key]) ? intval($specialNode['row'][$key]) : 10,
  214. 'status' => 1,
  215. 'is_del' => 0,
  216. 'sort_order' => $sort_order,
  217. 'lang' => $admin_lang,
  218. 'add_time' => getTime(),
  219. 'update_time' => getTime(),
  220. ];
  221. } else {
  222. $updateData[] = [
  223. 'node_id' => $node_id,
  224. 'title' => !empty($specialNode['title'][$key]) ? trim($specialNode['title'][$key]) : '默认节点',
  225. 'code' => !empty($specialNode['code'][$key]) ? trim($specialNode['code'][$key]) : 'default',
  226. 'isauto' => !empty($specialNode['isauto'][$itemid]) ? intval($specialNode['isauto'][$itemid]) : 0,
  227. 'keywords' => $keywords,
  228. 'typeid' => !empty($specialNode['typeid'][$key]) ? trim($specialNode['typeid'][$key]) : 0,
  229. 'aidlist' => !empty($aidlist) ? $aidlist : '',
  230. 'row' => !empty($specialNode['row'][$key]) ? intval($specialNode['row'][$key]) : 10,
  231. 'sort_order' => $sort_order,
  232. 'update_time' => getTime(),
  233. ];
  234. array_push($delData, $node_id);
  235. }
  236. }
  237. }
  238. if (!empty($delData)) {
  239. $this->where(['aid'=>$aid, 'node_id'=>['NOT IN', $delData]])->delete();
  240. }
  241. if (!empty($updateData)) {
  242. $this->saveAll($updateData);
  243. }
  244. if (!empty($addData)) {
  245. $this->saveAll($addData);
  246. }
  247. }
  248. }
  249. }