PageRenderTime 28ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/common/module/taxonomy.php

http://lazycms.googlecode.com/
PHP | 790 lines | 525 code | 6 blank | 259 comment | 103 complexity | 4d0b63b52480f3e9a2e8c08e36f3ea5b MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * +---------------------------------------------------------------------------+
  4. * | LL LLLL LL L LLLL LLLL |
  5. * | LL LL L LLL LL LL L LL LL |
  6. * | LL LLLL LLLLL LL LL LL LLLL LLL LL LL LL LL |
  7. * | LL LL LL LL LL LL L LLL LL LLLLL LL LL LL |
  8. * | LL LLLLL LL LLLL LL L L LL LLLLL LL LL LL |
  9. * | LL LL LL LL LLLL LL L LL LL LLLL LL |
  10. * | LL LL LL LL LL LL L L LL L LL LLLL LL |
  11. * | LLLLLL LLLLL LLLLL LL LLLL L LL LLLL LL LLLLLL |
  12. * | LL |
  13. * | LL |
  14. * +---------------------------------------------------------------------------+
  15. * | Copyright (C) 2007-2010 LazyCMS.com All rights reserved. |
  16. * +---------------------------------------------------------------------------+
  17. * | LazyCMS is free software. See LICENSE for copyright notices and details. |
  18. * +---------------------------------------------------------------------------+
  19. */
  20. defined('COM_PATH') or die('Restricted access!');
  21. /**
  22. * ?????
  23. *
  24. * @param int $termid
  25. * @return array|null
  26. */
  27. function term_get_byid($termid) {
  28. $db = get_conn(); $termid = intval($termid);
  29. $rs = $db->query("SELECT * FROM `#@_term` WHERE `termid`=%d LIMIT 1 OFFSET 0;",$termid);
  30. if ($term = $db->fetch($rs)) {
  31. return $term;
  32. }
  33. return null;
  34. }
  35. /**
  36. * ??????
  37. *
  38. * @param $name
  39. * @return array|null
  40. */
  41. function term_get_byname($name) {
  42. $db = get_conn();
  43. $rs = $db->query("SELECT * FROM `#@_term` WHERE `name`='%s' LIMIT 1 OFFSET 0;",$name);
  44. if ($term = $db->fetch($rs)) {
  45. return $term;
  46. }
  47. return null;
  48. }
  49. /**
  50. * ????
  51. *
  52. * @param $name
  53. * @return $termid
  54. */
  55. function term_add($name) {
  56. $db = get_conn();
  57. $termid = $db->result(sprintf("SELECT `termid` FROM `#@_term` WHERE `name`='%s' LIMIT 1 OFFSET 0;",esc_sql($name)));
  58. if (!$termid) {
  59. $termid = $db->insert('#@_term',array(
  60. 'name' => $name,
  61. ));
  62. // ????
  63. fcache_delete('terms.dicts');
  64. }
  65. return $termid;
  66. }
  67. /**
  68. * ????????
  69. *
  70. * @param string $title
  71. * @param string $content
  72. * @return array
  73. */
  74. function term_gets($title=null, $content=null) {
  75. $ckey = 'terms.dicts';
  76. $dicts = fcache_get($ckey);
  77. if (fcache_is_null($dicts)) {
  78. $db = get_conn(); $dicts = array();
  79. // ???????
  80. $rs = $db->query("SELECT `name` FROM `#@_term`");
  81. while ($data = $db->fetch($rs)) {
  82. $dicts[] = $data['name'];
  83. }
  84. fcache_set($ckey,$dicts);
  85. }
  86. // ???????????
  87. if ($title===null && $content === null) return $dicts;
  88. // ????
  89. $content = clear_space(strip_tags($content));
  90. if (trim($content) == '') return null;
  91. if (mb_strlen($content,'UTF-8') > 512)
  92. $content = mb_substr($content,0,512,'UTF-8');
  93. include_file(COM_PATH.'/system/keyword.php');
  94. $splitword = new keyword($dicts);
  95. $keywords = $splitword->get($content);
  96. // ???????????
  97. if (C('TAG-Open') && (empty($keywords) || count($keywords)<=3)) {
  98. // ??keyword.lazycms.com?????
  99. include_file(COM_PATH.'/system/httplib.php');
  100. $r = @httplib_get(sprintf('http://keyword.lazycms.com/related_kw.php?title=%s&content=%s', rawurlencode($title), rawurlencode($content)),array(
  101. 'timeout' => 3
  102. ));
  103. if (httplib_retrieve_response_code($r) == '200') {
  104. $keys = array();
  105. $xml = httplib_retrieve_body($r);
  106. // ????????
  107. if (preg_match_all('/\<kw\>\<\!\[CDATA\[(.+)\]\]\>\<\/kw\>/i',$xml,$args)) {
  108. $keys = $args[1];
  109. }
  110. // ??????
  111. if (!empty($keys)) {
  112. $newkeys = array_unique(array_merge($keywords,$keys));
  113. $keywords = $newkeys;
  114. foreach ($keywords as $keyword) {
  115. foreach($newkeys as $k=>$v) {
  116. if ($keyword!=$v && stripos($keyword,$v) !== false) {
  117. unset($keywords[$k]);
  118. }
  119. }
  120. }
  121. $keywords = array_values($keywords); unset($newkeys);
  122. }
  123. }
  124. }
  125. if (empty($keywords)) {
  126. return null;
  127. } else {
  128. return $keywords;
  129. }
  130. }
  131. /**
  132. * ????????
  133. *
  134. * @param $category
  135. * @param int $num
  136. * @return string
  137. */
  138. function taxonomy_get_names($category,$num=3) {
  139. $names = array();
  140. foreach($category as $i=>$taxonomyid) {
  141. $taxonomy = taxonomy_get($taxonomyid);
  142. if ($i >= $num) {
  143. $names[] = $taxonomy['name'].'...';
  144. break;
  145. } else {
  146. $names[] = $taxonomy['name'];
  147. }
  148. }
  149. return implode(',', $names);
  150. }
  151. /**
  152. * ????
  153. *
  154. * @param string $type
  155. * @return int
  156. */
  157. function taxonomy_count($type='category') {
  158. return get_conn()->result(sprintf("SELECT COUNT(`taxonomyid`) FROM `#@_term_taxonomy` WHERE `type`='%s';", $type));
  159. }
  160. /**
  161. * ??????
  162. *
  163. * @param string $type
  164. * @return array
  165. */
  166. function taxonomy_get_list($type='category') {
  167. // TODO ????
  168. $db = get_conn(); $result = array();
  169. $rs = $db->query("SELECT `taxonomyid` FROM `#@_term_taxonomy` WHERE `type`='%s';", $type);
  170. while ($row = $db->fetch($rs)) {
  171. $result[] = $row['taxonomyid'];
  172. }
  173. return $result;
  174. }
  175. /**
  176. * ?????
  177. *
  178. * @param int $parentid
  179. * @param string $type
  180. * @return array
  181. */
  182. function taxonomy_get_trees($parentid=0,$type='category') {
  183. $result = array(); $un = array(); $parentid = intval($parentid);
  184. $taxonomy_list = taxonomy_get_list($type);
  185. foreach ($taxonomy_list as $taxonomyid) {
  186. $result[$taxonomyid] = taxonomy_get($taxonomyid);
  187. }
  188. // ???????????????????????????
  189. foreach ($result as $id => $item) {
  190. if ($item['parent']) {
  191. $result[$item['parent']]['subs'][$id] = &$result[$id];
  192. $un[] = $id;
  193. }
  194. }
  195. if ($parentid) {
  196. $result = isset($result[$parentid])?$result[$parentid]:array();
  197. }
  198. foreach($un as $v) unset($result[$v]);
  199. return $result;
  200. }
  201. /**
  202. * ????ids
  203. *
  204. * @param array $tree
  205. * @return array
  206. */
  207. function taxonomy_get_subids($tree) {
  208. static $result = array(); $func = __FUNCTION__;
  209. if (!is_array($tree)) return ;
  210. foreach ($tree as $taxonomyid=>$taxonomy) {
  211. $result[] = $taxonomyid;
  212. if (isset($taxonomy['subs'])) {
  213. $func($taxonomy['subs']);
  214. }
  215. }
  216. return $result;
  217. }
  218. /**
  219. * ????ids
  220. *
  221. * @param string $listid
  222. * @param string $listsub
  223. * @return array
  224. */
  225. function taxonomy_get_ids($listid, $listsub = 'me') {
  226. $listids = array_unique(explode(',', $listid));
  227. if ($listsub == 'me') {
  228. foreach ($listids as $id) {
  229. $t = taxonomy_get_trees($id, 'category');
  230. if (isset($t['subs'])) {
  231. $ids = taxonomy_get_subids($t['subs']);
  232. } elseif(!isset($t['taxonomyid'])) {
  233. $ids = taxonomy_get_subids($t);
  234. } else {
  235. $ids = null;
  236. }
  237. $listids = $ids ? array_merge($listids, $ids) : $listids;
  238. }
  239. } elseif($listsub) {
  240. $listsub = explode(',', $listsub);
  241. $listids = array_merge($listids, $listsub);
  242. }
  243. return array_unique($listids);
  244. }
  245. /**
  246. * ??????????
  247. *
  248. * @param $taxonomyid
  249. * @param $path ???format_path()???????
  250. * @return bool
  251. */
  252. function taxonomy_path_exists($taxonomyid,$path) {
  253. if (strpos($path,'%ID')!==false && strpos($path,'%MD5')!==false) return false;
  254. if ($taxonomyid) {
  255. $sql = sprintf("SELECT COUNT(`taxonomyid`) FROM `#@_term_taxonomy_meta` WHERE `key`='path' AND `value`='%s' AND `taxonomyid`<>'%d';", esc_sql($path), esc_sql($taxonomyid));
  256. } else {
  257. $sql = sprintf("SELECT COUNT(`taxonomyid`) FROM `#@_term_taxonomy_meta` WHERE `key`='path' AND `value`='%s';",esc_sql($path));
  258. }
  259. return !(get_conn()->result($sql) == 0);
  260. }
  261. /**
  262. * ??????
  263. *
  264. * @param int $taxonomyid
  265. * @return array|null
  266. */
  267. function taxonomy_get($taxonomyid) {
  268. $db = get_conn(); $prefix = 'taxonomy.';
  269. $taxonomyid = intval($taxonomyid);
  270. $taxonomy = fcache_get($prefix.$taxonomyid);
  271. if (fcache_not_null($taxonomy)) return $taxonomy;
  272. $rs = $db->query("SELECT * FROM `#@_term_taxonomy` WHERE `taxonomyid`=%d LIMIT 1 OFFSET 0;",$taxonomyid);
  273. if ($taxonomy = $db->fetch($rs)) {
  274. if ($term = term_get_byid($taxonomy['termid'])) {
  275. $taxonomy = array_merge($taxonomy,$term);
  276. }
  277. if ($meta = taxonomy_get_meta($taxonomy['taxonomyid'])) {
  278. foreach (array('path','page','list','model','description') as $field) {
  279. $taxonomy[$field] = $meta[$field]; unset($meta[$field]);
  280. }
  281. $taxonomy['meta'] = $meta;
  282. }
  283. $taxonomy['keywords'] = taxonomy_get_relation('sort_tag',$taxonomy['taxonomyid']);
  284. // ?????
  285. fcache_set($prefix.$taxonomyid,$taxonomy);
  286. return $taxonomy;
  287. }
  288. return null;
  289. }
  290. /**
  291. * ????????
  292. *
  293. * @param int $taxonomyid
  294. * @return array
  295. */
  296. function taxonomy_get_meta($taxonomyid) {
  297. $db = get_conn(); $result = array(); $taxonomyid = intval($taxonomyid);
  298. $rs = $db->query("SELECT * FROM `#@_term_taxonomy_meta` WHERE `taxonomyid`=%d;",$taxonomyid);
  299. while ($row = $db->fetch($rs)) {
  300. $result[$row['key']] = is_serialized($row['value']) ? unserialize($row['value']) : $row['value'];
  301. }
  302. return $result;
  303. }
  304. /**
  305. * ?????????
  306. *
  307. * @param string $type
  308. * @param int $objectid
  309. * @return array
  310. */
  311. function taxonomy_get_relation($type, $objectid) {
  312. $db = get_conn(); $result = array(); $tt_ids = array();
  313. $rs = $db->query("SELECT `taxonomyid` FROM `#@_term_taxonomy` WHERE `type`='%s';",$type);
  314. while ($tt = $db->fetch($rs)) {
  315. $tt_ids[] = $tt['taxonomyid'];
  316. }
  317. $in_tt_ids = "'" . implode("', '", $tt_ids) . "'";
  318. $rs = $db->query("SELECT DISTINCT(`tr`.`taxonomyid`) AS `taxonomyid`,`tr`.`order` AS `order` FROM `#@_term_relation` AS `tr` LEFT JOIN `#@_term_taxonomy` AS `tt` ON `tt`.`taxonomyid`=`tr`.`taxonomyid` WHERE `tr`.`objectid`=%d AND `tt`.`taxonomyid` IN({$in_tt_ids});",$objectid);
  319. while ($taxonomy = $db->fetch($rs)) {
  320. $result[$taxonomy['order']] = $taxonomy['taxonomyid'];
  321. }
  322. ksort($result);
  323. return $result;
  324. }
  325. /**
  326. * ??????
  327. *
  328. * @param $type
  329. * @param $objectid
  330. * @param $taxonomies
  331. * @return bool
  332. */
  333. function taxonomy_make_relation($type,$objectid,$taxonomies) {
  334. $db = get_conn(); $tt_ids = array(); $taxonomies = (array) $taxonomies;
  335. $rs = $db->query("SELECT `taxonomyid` FROM `#@_term_taxonomy` WHERE `type`='%s';",$type);
  336. while ($tt = $db->fetch($rs)) {
  337. $tt_ids[] = $tt['taxonomyid'];
  338. }
  339. // ??????,????
  340. $tt_ids = array_diff($tt_ids,$taxonomies);
  341. $in_tt_ids = "'" . implode("', '", $tt_ids) . "'";
  342. // ?????
  343. $rs = $db->query("SELECT DISTINCT(`tr`.`taxonomyid`) AS `taxonomyid` FROM `#@_term_relation` AS `tr` LEFT JOIN `#@_term_taxonomy` AS `tt` ON `tt`.`taxonomyid`=`tr`.`taxonomyid` WHERE `tr`.`objectid`=%d AND `tt`.`taxonomyid` IN({$in_tt_ids});",$objectid);
  344. while ($taxonomy = $db->fetch($rs)) {
  345. taxonomy_delete_relation($objectid,$taxonomy['taxonomyid']);
  346. }
  347. // ????????
  348. foreach($taxonomies as $order=>$taxonomyid) {
  349. $is_exist = $db->result(sprintf("SELECT COUNT(*) FROM `#@_term_relation` WHERE `taxonomyid`=%d AND `objectid`=%d;",esc_sql($taxonomyid),esc_sql($objectid)));
  350. if (0 < $is_exist) {
  351. $db->update('#@_term_relation',array(
  352. 'order' => $order,
  353. ),array(
  354. 'taxonomyid' => $taxonomyid,
  355. 'objectid' => $objectid,
  356. ));
  357. } else {
  358. $db->insert('#@_term_relation',array(
  359. 'taxonomyid' => $taxonomyid,
  360. 'objectid' => $objectid,
  361. 'order' => $order,
  362. ));
  363. }
  364. // ?????
  365. taxonomy_update_count($taxonomyid);
  366. }
  367. return true;
  368. }
  369. /**
  370. * ?????
  371. *
  372. * @param int $taxonomyid
  373. * @return void
  374. */
  375. function taxonomy_update_count($taxonomyid) {
  376. $db = get_conn();
  377. if ($taxonomy = taxonomy_get($taxonomyid)) {
  378. $count = $db->result(sprintf("SELECT COUNT(`objectid`) FROM `#@_term_relation` WHERE `taxonomyid`=%d;",esc_sql($taxonomyid)));
  379. if ($taxonomy['count'] != $count) {
  380. $db->update('#@_term_taxonomy',array('count'=>$count),array('taxonomyid'=>$taxonomyid));
  381. taxonomy_clean_cache($taxonomyid);
  382. }
  383. return $count;
  384. }
  385. }
  386. /**
  387. * ????
  388. *
  389. * @param $objectid
  390. * @param $taxonomyid
  391. * @return bool
  392. */
  393. function taxonomy_delete_relation($objectid,$taxonomyid) {
  394. return get_conn()->delete('#@_term_relation',array(
  395. 'taxonomyid' => $taxonomyid,
  396. 'objectid' => $objectid,
  397. ));
  398. }
  399. /**
  400. * ????
  401. *
  402. * @param $type
  403. * @param $name
  404. * @param int $parentid
  405. * @param $data
  406. * @return array|null
  407. */
  408. function taxonomy_add($type,$name,$parentid=0,$data=null) {
  409. $parentid = intval($parentid);
  410. $data = is_array($data) ? $data : array();
  411. $taxonomyid = get_conn()->insert('#@_term_taxonomy',array(
  412. 'type' => $type,
  413. 'parent' => $parentid,
  414. ));
  415. $data['name'] = $name;
  416. return taxonomy_edit($taxonomyid,$data);
  417. }
  418. /**
  419. * ??Tag
  420. *
  421. * @param string $name
  422. * @param string $type
  423. * @return array|null
  424. */
  425. function taxonomy_add_tag($name, $type='post_tag') {
  426. $db = get_conn();
  427. $taxonomyid = $db->result(sprintf("SELECT `taxonomyid` FROM `#@_term` AS `t` LEFT JOIN `#@_term_taxonomy` AS `tt` ON `tt`.`termid`=`t`.`termid` WHERE `tt`.`type`='%s' AND `t`.`name`='%s' LIMIT 1 OFFSET 0;",esc_sql($type),esc_sql($name)));
  428. if (!$taxonomyid) {
  429. $taxonomyid = $db->insert('#@_term_taxonomy',array(
  430. 'type' => $type,
  431. ));
  432. taxonomy_edit($taxonomyid,array(
  433. 'name' => $name,
  434. ));
  435. }
  436. return $taxonomyid;
  437. }
  438. /**
  439. * ??????
  440. *
  441. * @param int $taxonomyid
  442. * @param array $data
  443. * @return array|null
  444. */
  445. function taxonomy_edit($taxonomyid,$data) {
  446. $db = get_conn(); $taxonomy_rows = $term_rows = $meta_rows = array();
  447. $data = is_array($data) ? $data : array();
  448. if ($taxonomy = taxonomy_get($taxonomyid)) {
  449. // ?????
  450. if (isset($data['path'])) {
  451. $data['path'] = path_format($data['path'],array(
  452. 'ID' => $taxonomyid,
  453. 'PY' => $data['name'],
  454. 'MD5' => $taxonomyid,
  455. ));
  456. // ??????
  457. if (!empty($taxonomy['path'])) {
  458. if ($data['path']!=$taxonomy['path'] && is_dir(ABS_PATH.'/'.$taxonomy['path'])) {
  459. rmdirs(ABS_PATH.'/'.$taxonomy['path']);
  460. }
  461. }
  462. }
  463. // ?????
  464. if (isset($data['keywords']) && !empty($data['keywords'])) {
  465. if (is_array($data['keywords'])) {
  466. $keywords = $data['keywords'];
  467. } else {
  468. // ????????????
  469. $data['keywords'] = str_replace(array('?','?'),array(',',' '),$data['keywords']);
  470. // ??,?????
  471. $keywords = explode(',',$data['keywords']);
  472. // ??????????????
  473. if (count($keywords)==1) $keywords = explode(' ',$data['keywords']);
  474. }
  475. $taxonomies = array();
  476. // ????????
  477. $keywords = array_unique($keywords);
  478. // ?????????????HTML
  479. array_walk($keywords,create_function('&$s','$s=esc_html(trim($s));'));
  480. // ???????
  481. foreach($keywords as $key) {
  482. $taxonomies[] = taxonomy_add_tag($key, 'sort_tag');
  483. }
  484. // ?????
  485. $data['keywords'] = implode(',', $keywords);
  486. // ????
  487. taxonomy_make_relation('sort_tag',$taxonomyid,$taxonomies);
  488. }
  489. // ??????????
  490. foreach ($data as $field=>$value) {
  491. if ($db->is_field('#@_term_taxonomy',$field)) {
  492. $taxonomy_rows[$field] = $value;
  493. } elseif ($field=='name') {
  494. $term_rows[$field] = $value;
  495. } else {
  496. $meta_rows[$field] = $value;
  497. }
  498. }
  499. // ????
  500. unset($meta_rows['keywords']);
  501. // ????
  502. if (!empty($term_rows['name'])) $taxonomy_rows['termid'] = term_add($term_rows['name']);
  503. if ($taxonomy_rows) $db->update('#@_term_taxonomy',$taxonomy_rows,array('taxonomyid'=>$taxonomyid));
  504. if ($meta_rows) taxonomy_edit_meta($taxonomyid,$meta_rows);
  505. // ????
  506. taxonomy_clean_cache($taxonomyid);
  507. return array_merge($taxonomy,$data);
  508. }
  509. return null;
  510. }
  511. /**
  512. * ??????
  513. *
  514. * @param int $taxonomyid
  515. * @param array $data
  516. * @return bool
  517. */
  518. function taxonomy_edit_meta($taxonomyid,$data) {
  519. $db = get_conn(); $taxonomyid = intval($taxonomyid);
  520. $data = is_array($data) ? $data : array();
  521. foreach ($data as $key=>$value) {
  522. // ????????????
  523. $length = (int) $db->result(vsprintf("SELECT COUNT(*) FROM `#@_term_taxonomy_meta` WHERE `taxonomyid`=%d AND `key`='%s';",array($taxonomyid,esc_sql($key))));
  524. // update
  525. if ($length > 0) {
  526. $db->update('#@_term_taxonomy_meta',array(
  527. 'value' => $value,
  528. ),array(
  529. 'taxonomyid' => $taxonomyid,
  530. 'key' => $key,
  531. ));
  532. }
  533. // insert
  534. else {
  535. // ???????
  536. $db->insert('#@_term_taxonomy_meta',array(
  537. 'taxonomyid' => $taxonomyid,
  538. 'key' => $key,
  539. 'value' => $value,
  540. ));
  541. }
  542. }
  543. return true;
  544. }
  545. /**
  546. * ????
  547. *
  548. * @param int $taxonomyid
  549. * @return bool
  550. */
  551. function taxonomy_clean_cache($taxonomyid) {
  552. $taxonomyid = intval($taxonomyid);
  553. return fcache_delete('taxonomy.'.$taxonomyid);
  554. }
  555. /**
  556. * ????
  557. *
  558. * @param int $taxonomyid
  559. * @return bool
  560. */
  561. function taxonomy_delete($taxonomyid) {
  562. $db = get_conn();
  563. $taxonomyid = intval($taxonomyid);
  564. if (!$taxonomyid) return false;
  565. if ($taxonomy = taxonomy_get($taxonomyid)) {
  566. // ??????
  567. $db->delete('#@_term_relation',array('taxonomyid' => $taxonomyid));
  568. // ????????
  569. $db->delete('#@_term_taxonomy_meta',array('taxonomyid' => $taxonomyid));
  570. // ??????
  571. $db->delete('#@_term_taxonomy',array('taxonomyid' => $taxonomyid));
  572. // ????
  573. taxonomy_clean_cache($taxonomyid);
  574. // ????
  575. return rmdirs(ABS_PATH.'/'.$taxonomy['path']);
  576. }
  577. return false;
  578. }
  579. /**
  580. * ??????
  581. *
  582. * @param int $taxonomyid
  583. * @param int $page
  584. * @param bool $make_post ??????
  585. * @return bool
  586. */
  587. function taxonomy_create($taxonomyid,$page=1,$make_post=false) {
  588. $taxonomyid = intval($taxonomyid);
  589. if (!$taxonomyid) return false;
  590. if ($taxonomy = taxonomy_get($taxonomyid)) {
  591. $tpl = tpl_init('taxonomy');
  592. $page = $page<1 ? 1 : intval($page);
  593. $inner = $b_guid = ''; $i = 0;
  594. $suffix = C('HTML-Ext');
  595. // ????
  596. $html = tpl_loadfile(ABS_PATH.'/'.system_themes_path().'/'.$taxonomy['list']);
  597. // ?????
  598. $block = tpl_get_block($html,'post,list','list');
  599. if ($block) {
  600. // ??????
  601. $meta = tpl_get_attr($block['tag'],'meta');
  602. // ???ID
  603. $listsub = tpl_get_attr($block['tag'],'listsub');
  604. // ???????ID
  605. $notsid = tpl_get_attr($block['tag'],'listsub','!=');
  606. // ????
  607. $number = tpl_get_attr($block['tag'],'number');
  608. // ????
  609. $order = tpl_get_attr($block['tag'],'order');
  610. // ?????
  611. $zebra = tpl_get_attr($block['tag'],'zebra');
  612. // ????
  613. $listsub = $listsub == 'me' ? $listsub : validate_is($listsub, VALIDATE_IS_LIST) ? $listsub : null;
  614. $notsid = validate_is($notsid,VALIDATE_IS_LIST) ? $notsid : null;
  615. $zebra = validate_is($zebra,VALIDATE_IS_NUMERIC) ? $zebra : 0;
  616. $number = validate_is($number,VALIDATE_IS_NUMERIC) ? $number : 10;
  617. $order = instr(strtoupper($order),'ASC,DESC') ? $order : 'DESC';
  618. // ???????
  619. pages_init($number, $page);
  620. $listids = array($taxonomyid);
  621. if ($listsub !== null) {
  622. // ??IDs
  623. $listids = taxonomy_get_ids($taxonomyid, $listsub);
  624. // ??IDs
  625. $notsids = $notsid ? explode(',', $notsid) : array();
  626. // ?????IDs
  627. foreach ($listids as $k=>$id) {
  628. if (in_array($id,$notsids))
  629. unset($listids[$k]);
  630. }
  631. }
  632. $length = count($listids);
  633. if ($length == 1) {
  634. $where = sprintf(" AND `tr`.`taxonomyid`=%d", array_pop($listids));
  635. } elseif ($length > 1) {
  636. $where = sprintf(" AND `tr`.`taxonomyid` IN(%s)", implode(',', $listids));
  637. }
  638. // ?????
  639. if ($meta && (strpos($meta,':') !== false || strncasecmp($meta, 'find(', 5) === 0)) {
  640. // meta="find(value,field)"
  641. if (strncasecmp($meta, 'find(', 5) === 0) {
  642. $index = strrpos($meta, ',');
  643. $field = substring($meta, $index+1, strrpos($meta,')'));
  644. $value = substring($meta, 5, $index);
  645. $where.= sprintf(" AND FIND_IN_SET('%s', `pm`.`value`)", esc_sql($value));
  646. }
  647. // meta="field:value"
  648. elseif (($pos=strpos($meta,':')) !== false) {
  649. $field = substr($meta, 0, $pos);
  650. $value = substr($meta, $pos + 1);
  651. $where.= sprintf(" AND `pm`.`value`='%s'", esc_sql($value));
  652. }
  653. $sql = sprintf("SELECT DISTINCT(`tr`.`objectid`) AS `postid` FROM `#@_term_relation` AS `tr` LEFT JOIN `#@_post_meta` AS `pm` ON `tr`.`objectid`=`pm`.`postid` WHERE `pm`.`key`='%2\$s' %3\$s ORDER BY `objectid` %1\$s", $order, esc_sql($field), $where);
  654. } else {
  655. $where = str_replace('`tr`.', '', $where);
  656. // ??sql
  657. $sql = sprintf("SELECT `objectid` AS `postid` FROM `#@_term_relation` WHERE 1 %s ORDER BY `objectid` %s", $where, esc_sql($order));
  658. }
  659. $result = pages_query($sql);
  660. // ??????
  661. if (stripos($html,'{pagelist') !== false) {
  662. $html = preg_replace('/\{(pagelist)[^\}]*\/\}/isU',
  663. pages_list(ROOT.$taxonomy['path'].'/index$'.$suffix, '!$'),
  664. $html
  665. );
  666. }
  667. // ????
  668. if ($result) {
  669. // ???????
  670. $block['inner'] = tpl_get_block_inner($block);
  671. $suffixs = C('UPIMG-Exts');
  672. while ($data = pages_fetch($result)) {
  673. $post = post_get($data['postid']);
  674. if (empty($post)) continue;
  675. $post['list'] = taxonomy_get($post['listid']);
  676. $post['path'] = post_get_path($post['listid'],$post['path']);
  677. // ????
  678. if ($make_post) post_create($post['postid']);
  679. // ????
  680. if ($post['content'] && strpos($post['content'],'<!--pagebreak-->')!==false) {
  681. $contents = explode('<!--pagebreak-->', $post['content']);
  682. $content = array_shift($contents);
  683. } else {
  684. $content = $post['content'];
  685. }
  686. // ??????
  687. $vars = array(
  688. 'zebra' => ($i % ($zebra + 1)) ? '0' : '1',
  689. 'postid' => $post['postid'],
  690. 'userid' => $post['userid'],
  691. 'author' => $post['author'],
  692. 'title' => $post['title'],
  693. 'views' => '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=post_views&postid='.$post['postid'].'"></script>',
  694. 'comment' => '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=post_comment&postid='.$post['postid'].'"></script>',
  695. 'people' => '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=post_comment_people&postid='.$post['postid'].'"></script>',
  696. 'digg' => $post['digg'],
  697. 'path' => ROOT.$post['path'],
  698. 'content' => $content,
  699. 'date' => $post['datetime'],
  700. 'edittime' => $post['edittime'],
  701. 'keywords' => post_get_taxonomy($post['keywords']),
  702. 'description' => $post['description'],
  703. );
  704. // ????????
  705. $images = post_get_medias($post['postid'], $suffixs);
  706. foreach($images as $k=>$image) {
  707. if ($k == 0) $vars['image'] = $image['url'];
  708. $vars['images'][($k+1)] = $image['url'];
  709. }
  710. // ??????
  711. if (isset($post['list'])) {
  712. $vars['list'] = array(
  713. 'id' => $post['list']['taxonomyid'],
  714. 'name' => $post['list']['name'],
  715. 'path' => ROOT.$post['list']['path'].'/',
  716. 'count' => '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=taxonomy_count&listid='.$post['list']['taxonomyid'].'"></script>',
  717. );
  718. if (isset($post['list']['meta'])) {
  719. foreach((array)$post['list']['meta'] as $k=>$v) {
  720. $vars['list'][$k] = $v;
  721. }
  722. }
  723. }
  724. // ????
  725. tpl_clean($tpl);
  726. tpl_set_var($vars, $tpl);
  727. // ???????
  728. if (isset($post['meta'])) {
  729. tpl_set_var('post', $post['meta'], $tpl);
  730. }
  731. // ????
  732. $inner.= tpl_parse($block['inner'], $block, $tpl); $i++;
  733. }
  734. } else {
  735. $inner = __('The page is in the making, please visit later ...');
  736. }
  737. // ????????ID
  738. $b_guid = guid($block['tag']);
  739. // ???????????
  740. $html = str_replace($block['tag'], '{$'.$b_guid.'}', $html);
  741. }
  742. // ?????????????????????
  743. if ($i==0 && $page>1) return false;
  744. // ????????
  745. tpl_clean($tpl);
  746. tpl_set_var($b_guid, $inner, $tpl);
  747. tpl_set_var(array(
  748. 'listid' => $taxonomy['taxonomyid'],
  749. 'listname' => $taxonomy['name'],
  750. 'listpath' => ROOT.$taxonomy['path'].'/',
  751. 'count' => '<script type="text/javascript" src="'.ROOT.'common/gateway.php?func=taxonomy_count&listid='.$taxonomy['taxonomyid'].'"></script>',
  752. 'guide' => system_category_guide($taxonomy['taxonomyid']),
  753. 'title' => $taxonomy['name'],
  754. 'keywords' => post_get_taxonomy($taxonomy['keywords']),
  755. 'description' => $taxonomy['description'],
  756. ), $tpl);
  757. // ???????
  758. if (isset($taxonomy['meta'])) {
  759. tpl_set_var('list', $taxonomy['meta'], $tpl);
  760. }
  761. $html = tpl_parse($html, $tpl);
  762. // ???????
  763. $file = ABS_PATH.'/'.$taxonomy['path'].'/index' . ($page==1 ? '' : $page) . $suffix;
  764. // ????
  765. mkdirs(dirname($file));
  766. // ????
  767. return file_put_contents($file,$html);
  768. }
  769. return true;
  770. }
  771. /**
  772. * ???
  773. *
  774. * @return string
  775. */
  776. function taxonomy_gateway_count() {
  777. $listid = isset($_GET['listid']) ? $_GET['listid'] : 0;
  778. if ($taxonomy = taxonomy_get($listid)) {
  779. $count = $taxonomy['count'];
  780. } else {
  781. $count = 0;
  782. }
  783. return 'document.write('.$count.');';
  784. }