111
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.
 
 
 
 
 

687 lines
18 KiB

  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2018年2月14日
  7. * 标签解析引擎模型
  8. */
  9. namespace app\home\model;
  10. use core\basic\Model;
  11. class ParserModel extends Model
  12. {
  13. // 存储分类及子编码
  14. protected $scodes = array();
  15. // 存储分类查询数据
  16. protected $sorts;
  17. // 存储栏目位置
  18. protected $position = array();
  19. // 上一篇
  20. protected $pre;
  21. // 下一篇
  22. protected $next;
  23. // 站点配置信息
  24. public function getSite()
  25. {
  26. return parent::table('ay_site')->where("acode='" . get_lg() . "'")->find();
  27. }
  28. // 公司信息
  29. public function getCompany()
  30. {
  31. return parent::table('ay_company')->where("acode='" . get_lg() . "'")->find();
  32. }
  33. // 自定义标签
  34. public function getLabel()
  35. {
  36. return parent::table('ay_label')->decode()->column('value,type', 'name');
  37. }
  38. // 单个分类信息,不区分语言,兼容跨语言
  39. public function getSort($scode)
  40. {
  41. $field = array(
  42. 'a.*',
  43. 'c.name AS parentname',
  44. 'b.type'
  45. );
  46. $join = array(
  47. array(
  48. 'ay_model b',
  49. 'a.mcode=b.mcode',
  50. 'LEFT'
  51. ),
  52. array(
  53. 'ay_content_sort c',
  54. 'a.pcode=c.scode',
  55. 'LEFT'
  56. )
  57. );
  58. return parent::table('ay_content_sort a')->field($field)
  59. ->where("a.scode='$scode' OR a.filename='$scode'")
  60. ->join($join)
  61. ->find();
  62. }
  63. // 多个分类信息
  64. public function getMultSort($scodes)
  65. {
  66. $field = array(
  67. 'a.*',
  68. 'c.name AS parentname',
  69. 'b.type'
  70. );
  71. $join = array(
  72. array(
  73. 'ay_model b',
  74. 'a.mcode=b.mcode',
  75. 'LEFT'
  76. ),
  77. array(
  78. 'ay_content_sort c',
  79. 'a.pcode=c.scode',
  80. 'LEFT'
  81. )
  82. );
  83. return parent::table('ay_content_sort a')->field($field)
  84. ->where("a.acode='" . get_lg() . "'")
  85. ->in('a.scode', $scodes)
  86. ->join($join)
  87. ->order('a.sorting,a.id')
  88. ->select();
  89. }
  90. // 指定分类数量
  91. public function getSortRows($scode)
  92. {
  93. $this->scodes = array(); // 先清空
  94. // 获取多分类子类
  95. $arr = explode(',', $scode);
  96. foreach ($arr as $value) {
  97. $scodes = $this->getSubScodes(trim($value));
  98. }
  99. // 拼接条件
  100. $where1 = array(
  101. "scode in (" . implode_quot(',', $scodes) . ")",
  102. "subscode='$scode'"
  103. );
  104. $where2 = array(
  105. "acode='" . get_lg() . "'",
  106. 'status=1',
  107. "date<'" . date('Y-m-d H:i:s') . "'"
  108. );
  109. $result = parent::table('ay_content')->where($where1, 'OR')
  110. ->where($where2)
  111. ->column('id');
  112. return count($result);
  113. }
  114. // 分类栏目列表关系树
  115. public function getSortsTree()
  116. {
  117. $fields = array(
  118. 'a.*',
  119. 'b.type'
  120. );
  121. $join = array(
  122. 'ay_model b',
  123. 'a.mcode=b.mcode',
  124. 'LEFT'
  125. );
  126. $result = parent::table('ay_content_sort a')->where("a.acode='" . get_lg() . "'")
  127. ->where('a.status=1')
  128. ->join($join)
  129. ->order('a.pcode,a.sorting,a.id')
  130. ->column($fields, 'scode');
  131. foreach ($result as $key => $value) {
  132. if ($value['pcode']) {
  133. $result[$value['pcode']]['son'][] = $value; // 记录到关系树
  134. } else {
  135. $data['top'][] = $value; // 记录顶级菜单
  136. }
  137. }
  138. $data['tree'] = $result;
  139. return $data;
  140. }
  141. // 获取分类名称
  142. public function getSortName($scode)
  143. {
  144. $result = $this->getSortList();
  145. return $result[$scode]['name'];
  146. }
  147. // 分类顶级编码
  148. public function getSortTopScode($scode)
  149. {
  150. $result = $this->getSortList();
  151. return $this->getTopParent($scode, $result);
  152. }
  153. // 获取位置
  154. public function getPosition($scode)
  155. {
  156. $result = $this->getSortList();
  157. $this->position = array(); // 重置
  158. $this->getTopParent($scode, $result);
  159. return array_reverse($this->position);
  160. }
  161. // 分类顶级编码
  162. private function getTopParent($scode, $sorts)
  163. {
  164. if (! $scode || ! $sorts) {
  165. return;
  166. }
  167. $this->position[] = $sorts[$scode];
  168. if ($sorts[$scode]['pcode']) {
  169. return $this->getTopParent($sorts[$scode]['pcode'], $sorts);
  170. } else {
  171. return $sorts[$scode]['scode'];
  172. }
  173. }
  174. // 分类子类集
  175. private function getSubScodes($scode)
  176. {
  177. if (! $scode) {
  178. return;
  179. }
  180. $this->scodes[] = $scode;
  181. $subs = parent::table('ay_content_sort')->where("pcode='$scode'")->column('scode');
  182. if ($subs) {
  183. foreach ($subs as $value) {
  184. $this->getSubScodes($value);
  185. }
  186. }
  187. return $this->scodes;
  188. }
  189. // 获取栏目清单
  190. private function getSortList()
  191. {
  192. if (! isset($this->sorts)) {
  193. $fields = array(
  194. 'a.id',
  195. 'a.pcode',
  196. 'a.scode',
  197. 'a.name',
  198. 'a.filename',
  199. 'a.outlink',
  200. 'b.type'
  201. );
  202. $join = array(
  203. 'ay_model b',
  204. 'a.mcode=b.mcode',
  205. 'LEFT'
  206. );
  207. $this->sorts = parent::table('ay_content_sort a')->where("a.acode='" . get_lg() . "'")
  208. ->join($join)
  209. ->column($fields, 'scode');
  210. }
  211. return $this->sorts;
  212. }
  213. // 获取筛选字段数据
  214. public function getSelect($field)
  215. {
  216. return parent::table('ay_extfield')->where("name='$field'")->value('value');
  217. }
  218. // 列表内容,带分页,不区分语言,兼容跨语言
  219. public function getLists($scode, $num, $order, $filter = array(), $tags = array(), $select = array(), $fuzzy = true, $start = 1)
  220. {
  221. $fields = array(
  222. 'a.*',
  223. 'b.name as sortname',
  224. 'b.filename as sortfilename',
  225. 'c.name as subsortname',
  226. 'c.filename as subfilename',
  227. 'd.type',
  228. 'd.name as modelname',
  229. 'e.*'
  230. );
  231. $join = array(
  232. array(
  233. 'ay_content_sort b',
  234. 'a.scode=b.scode',
  235. 'LEFT'
  236. ),
  237. array(
  238. 'ay_content_sort c',
  239. 'a.subscode=c.scode',
  240. 'LEFT'
  241. ),
  242. array(
  243. 'ay_model d',
  244. 'b.mcode=d.mcode',
  245. 'LEFT'
  246. ),
  247. array(
  248. 'ay_content_ext e',
  249. 'a.id=e.contentid',
  250. 'LEFT'
  251. )
  252. );
  253. $scode_arr = array();
  254. if ($scode) {
  255. // 获取所有子类分类编码
  256. $this->scodes = array(); // 先清空
  257. $arr = explode(',', $scode); // 传递有多个分类时进行遍历
  258. foreach ($arr as $value) {
  259. $scodes = $this->getSubScodes(trim($value));
  260. }
  261. // 拼接条件
  262. $scode_arr = array(
  263. "a.scode in (" . implode_quot(',', $scodes) . ")",
  264. "a.subscode='$scode'"
  265. );
  266. }
  267. $where = array(
  268. 'a.status=1',
  269. 'd.type=2',
  270. "a.date<'" . date('Y-m-d H:i:s') . "'"
  271. );
  272. // 筛选条件支持模糊匹配
  273. return parent::table('ay_content a')->field($fields)
  274. ->where($scode_arr, 'OR')
  275. ->where($where)
  276. ->where($select, 'AND', 'AND', $fuzzy)
  277. ->where($filter, 'OR')
  278. ->where($tags, 'OR')
  279. ->join($join)
  280. ->order($order)
  281. ->page(1, $num, $start)
  282. ->decode()
  283. ->select();
  284. }
  285. // 列表内容,不带分页,不区分语言,兼容跨语言
  286. public function getList($scode, $num, $order, $filter = array(), $tags = array(), $select = array(), $fuzzy = true, $start = 1)
  287. {
  288. $fields = array(
  289. 'a.*',
  290. 'b.name as sortname',
  291. 'b.filename as sortfilename',
  292. 'c.name as subsortname',
  293. 'c.filename as subfilename',
  294. 'd.type',
  295. 'd.name as modelname',
  296. 'e.*'
  297. );
  298. $join = array(
  299. array(
  300. 'ay_content_sort b',
  301. 'a.scode=b.scode',
  302. 'LEFT'
  303. ),
  304. array(
  305. 'ay_content_sort c',
  306. 'a.subscode=c.scode',
  307. 'LEFT'
  308. ),
  309. array(
  310. 'ay_model d',
  311. 'b.mcode=d.mcode',
  312. 'LEFT'
  313. ),
  314. array(
  315. 'ay_content_ext e',
  316. 'a.id=e.contentid',
  317. 'LEFT'
  318. )
  319. );
  320. $scode_arr = array();
  321. if ($scode) {
  322. // 获取所有子类分类编码
  323. $this->scodes = array(); // 先清空
  324. $arr = explode(',', $scode); // 传递有多个分类时进行遍历
  325. foreach ($arr as $value) {
  326. $scodes = $this->getSubScodes(trim($value));
  327. }
  328. // 拼接条件
  329. $scode_arr = array(
  330. "a.scode in (" . implode_quot(',', $scodes) . ")",
  331. "a.subscode='$scode'"
  332. );
  333. }
  334. $where = array(
  335. 'a.status=1',
  336. 'd.type=2',
  337. "a.date<'" . date('Y-m-d H:i:s') . "'"
  338. );
  339. // 筛选条件支持模糊匹配
  340. return parent::table('ay_content a')->field($fields)
  341. ->where($scode_arr, 'OR')
  342. ->where($where)
  343. ->where($select, 'AND', 'AND', $fuzzy)
  344. ->where($filter, 'OR')
  345. ->where($tags, 'OR')
  346. ->join($join)
  347. ->order($order)
  348. ->limit($start - 1, $num)
  349. ->decode()
  350. ->select();
  351. }
  352. // 内容详情,不区分语言,兼容跨语言
  353. public function getContent($id)
  354. {
  355. $field = array(
  356. 'a.*',
  357. 'b.name as sortname',
  358. 'b.filename as sortfilename',
  359. 'c.name as subsortname',
  360. 'c.filename as subfilename',
  361. 'd.type',
  362. 'd.name as modelname',
  363. 'e.*'
  364. );
  365. $join = array(
  366. array(
  367. 'ay_content_sort b',
  368. 'a.scode=b.scode',
  369. 'LEFT'
  370. ),
  371. array(
  372. 'ay_content_sort c',
  373. 'a.subscode=c.scode',
  374. 'LEFT'
  375. ),
  376. array(
  377. 'ay_model d',
  378. 'b.mcode=d.mcode',
  379. 'LEFT'
  380. ),
  381. array(
  382. 'ay_content_ext e',
  383. 'a.id=e.contentid',
  384. 'LEFT'
  385. )
  386. );
  387. $result = parent::table('ay_content a')->field($field)
  388. ->where("a.id='$id' OR a.filename='$id'")
  389. ->where('a.status=1')
  390. ->join($join)
  391. ->decode()
  392. ->find();
  393. return $result;
  394. }
  395. // 单篇详情,不区分语言,兼容跨语言
  396. public function getAbout($scode)
  397. {
  398. $field = array(
  399. 'a.*',
  400. 'b.name as sortname',
  401. 'b.filename as sortfilename',
  402. 'c.name as subsortname',
  403. 'c.filename as subfilename',
  404. 'd.type',
  405. 'd.name as modelname',
  406. 'e.*'
  407. );
  408. $join = array(
  409. array(
  410. 'ay_content_sort b',
  411. 'a.scode=b.scode',
  412. 'LEFT'
  413. ),
  414. array(
  415. 'ay_content_sort c',
  416. 'a.subscode=c.scode',
  417. 'LEFT'
  418. ),
  419. array(
  420. 'ay_model d',
  421. 'b.mcode=d.mcode',
  422. 'LEFT'
  423. ),
  424. array(
  425. 'ay_content_ext e',
  426. 'a.id=e.contentid',
  427. 'LEFT'
  428. )
  429. );
  430. $result = parent::table('ay_content a')->field($field)
  431. ->where("a.scode='$scode' OR b.filename='$scode'")
  432. ->where('a.status=1')
  433. ->join($join)
  434. ->decode()
  435. ->order('id DESC')
  436. ->find();
  437. return $result;
  438. }
  439. // 指定内容多图,不区分语言,兼容跨语言
  440. public function getContentPics($id)
  441. {
  442. $result = parent::table('ay_content')->where("id='$id'")
  443. ->where('status=1')
  444. ->value('pics');
  445. return $result;
  446. }
  447. // 指定内容多选调用
  448. public function getContentCheckbox($id, $field)
  449. {
  450. $result = parent::table('ay_content_ext')->where("contentid='$id'")->value($field);
  451. return $result;
  452. }
  453. // 指定内容标签调用
  454. public function getContentTags($id)
  455. {
  456. $result = parent::table('ay_content')->field('scode,tags')
  457. ->where("id='$id'")
  458. ->find();
  459. return $result;
  460. }
  461. // 指定分类标签调用
  462. public function getAllTags($scode)
  463. {
  464. $join = array(
  465. array(
  466. 'ay_content_sort b',
  467. 'a.scode=b.scode',
  468. 'LEFT'
  469. ),
  470. array(
  471. 'ay_model c',
  472. 'b.mcode=c.mcode',
  473. 'LEFT'
  474. )
  475. );
  476. $scode_arr = array();
  477. if ($scode) {
  478. // 获取所有子类分类编码
  479. $this->scodes = array(); // 先清空
  480. $scodes = $this->getSubScodes(trim($scode)); // 获取子类
  481. // 拼接条件
  482. $scode_arr = array(
  483. "a.scode in (" . implode_quot(',', $scodes) . ")",
  484. "a.subscode='$scode'"
  485. );
  486. }
  487. $result = parent::table('ay_content a')->where("c.type=2 AND a.tags<>''")
  488. ->where($scode_arr, 'OR')
  489. ->join($join)
  490. ->order('a.visits DESC')
  491. ->column('a.tags');
  492. return $result;
  493. }
  494. // 上一篇内容
  495. public function getContentPre($scode, $id)
  496. {
  497. if (! $this->pre) {
  498. $this->scodes = array();
  499. $scodes = $this->getSubScodes($scode);
  500. $this->pre = parent::table('ay_content')->field('id,title,filename')
  501. ->where("id<$id")
  502. ->in('scode', $scodes)
  503. ->where("acode='" . get_lg() . "'")
  504. ->where('status=1')
  505. ->order('id DESC')
  506. ->find();
  507. }
  508. return $this->pre;
  509. }
  510. // 下一篇内容
  511. public function getContentNext($scode, $id)
  512. {
  513. if (! $this->next) {
  514. $this->scodes = array();
  515. $scodes = $this->getSubScodes($scode);
  516. $this->next = parent::table('ay_content')->field('id,title,filename')
  517. ->where("id>$id")
  518. ->in('scode', $scodes)
  519. ->where("acode='" . get_lg() . "'")
  520. ->where('status=1')
  521. ->order('id ASC')
  522. ->find();
  523. }
  524. return $this->next;
  525. }
  526. // 幻灯片
  527. public function getSlides($gid, $num, $start = 1)
  528. {
  529. $result = parent::table('ay_slide')->where("gid='$gid'")
  530. ->order('sorting ASC,id ASC')
  531. ->limit($start - 1, $num)
  532. ->select();
  533. return $result;
  534. }
  535. // 友情链接
  536. public function getLinks($gid, $num, $start = 1)
  537. {
  538. $result = parent::table('ay_link')->where("gid='$gid'")
  539. ->order('sorting ASC,id ASC')
  540. ->limit($start - 1, $num)
  541. ->select();
  542. return $result;
  543. }
  544. // 获取留言
  545. public function getMessage($num, $page = true, $start = 1, $lg = null)
  546. {
  547. if ($lg == 'all') {
  548. $where = array();
  549. } elseif ($lg) {
  550. $where = array(
  551. 'acode' => $lg
  552. );
  553. } else {
  554. $where = array(
  555. 'acode' => get_lg()
  556. );
  557. }
  558. if ($page) {
  559. return parent::table('ay_message')->where("status=1")
  560. ->where($where)
  561. ->order('id DESC')
  562. ->decode(false)
  563. ->page(1, $num, $start)
  564. ->select();
  565. } else {
  566. return parent::table('ay_message')->where("status=1")
  567. ->where($where)
  568. ->order('id DESC')
  569. ->decode(false)
  570. ->limit($start - 1, $num)
  571. ->select();
  572. }
  573. }
  574. // 新增留言
  575. public function addMessage($data)
  576. {
  577. return parent::table('ay_message')->autoTime()->insert($data);
  578. }
  579. // 获取表单字段
  580. public function getFormField($fcode)
  581. {
  582. $field = array(
  583. 'a.table_name',
  584. 'a.form_name',
  585. 'b.name',
  586. 'b.required',
  587. 'b.description'
  588. );
  589. $join = array(
  590. 'ay_form_field b',
  591. 'a.fcode=b.fcode',
  592. 'LEFT'
  593. );
  594. return parent::table('ay_form a')->field($field)
  595. ->where("a.fcode='$fcode'")
  596. ->join($join)
  597. ->order('b.sorting ASC,b.id ASC')
  598. ->select();
  599. }
  600. // 获取表单表名称
  601. public function getFormTable($fcode)
  602. {
  603. return parent::table('ay_form')->where("fcode='$fcode'")->value('table_name');
  604. }
  605. // 获取表单数据
  606. public function getForm($table, $num, $page = true, $start = 1)
  607. {
  608. if ($page) {
  609. return parent::table($table)->order('id DESC')
  610. ->decode(false)
  611. ->page(1, $num, $start)
  612. ->select();
  613. } else {
  614. return parent::table($table)->order('id DESC')
  615. ->decode(false)
  616. ->limit($start - 1, $num)
  617. ->select();
  618. }
  619. }
  620. // 新增表单数据
  621. public function addForm($table, $data)
  622. {
  623. return parent::table($table)->insert($data);
  624. }
  625. // 文章内链
  626. public function getTags()
  627. {
  628. return parent::table('ay_tags')->field('name,link')
  629. ->order('length(name) desc')
  630. ->select();
  631. }
  632. }