/ php-ppcms/includes/classes/base.item.content.class.php

http://php-ppcms.googlecode.com/ · PHP · 849 lines · 721 code · 99 blank · 29 comment · 217 complexity · 7e9494cee9aead626c009a29feae7481 MD5 · raw file

  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. * (c) 2009, jianyuzhu@gmail.com
  5. * All rights reserved
  6. * This script is part of the PPEMI project.
  7. ***************************************************************/
  8. class BaseItemContentBase extends Base {
  9. var $table = '';
  10. var $prefix = '';
  11. //
  12. var $_table_owner_field = 'owner';
  13. var $_table_owner_prefix = '';
  14. var $_table_owner_usable = true;
  15. var $_table_status_usable = false;
  16. //
  17. var $format = true;
  18. var $format_check = false;
  19. var $format_prefix = 'field_';
  20. var $item_link = false;
  21. var $item_link_owner = false;
  22. var $item_fields = array('id', 'name', 'link');
  23. var $list_fields = array();
  24. var $search_fields = array('name');
  25. var $search_type = 'or';
  26. var $sort_order = true;
  27. var $sort_fields = array('sort_order+', 'name+');
  28. var $sort_query = array();
  29. var $filter_fields = array();
  30. var $filter_map = array();
  31. var $filter_query = array();
  32. //
  33. var $table_to_category = '';
  34. var $table_category = '';
  35. //constructor
  36. function BaseItemContentBase() {
  37. parent::Base();
  38. }
  39. //methods
  40. function addItem($data_array, $tocategory = true, $multipage = false) {
  41. //$data_array[$this->prefix . 'fulltext'] = $this->_filter_content($data_array[$this->prefix . 'fulltext']);
  42. $data_array = func_db_input($data_array);
  43. $data_array[$this->prefix . 'owner'] = $this->owner;
  44. $data_array[$this->prefix . 'date_added'] = 'now()';
  45. $data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
  46. $data_array[$this->prefix . 'status'] = '1';
  47. $items_id = $this->_insert($this->table, $data_array);
  48. //link
  49. if( $items_id > 0 ) {
  50. $this->_update_item_link_datecount($items_id);
  51. }
  52. //category
  53. if( $tocategory == true ) {
  54. $categories_id = $data_array['categories_id'];
  55. $query = "insert into " . $this->table_to_category . " (" . $this->prefix . "id, categories_id) values ('" . (int)$items_id . "', '" . $categories_id . "')";
  56. $result = $this->adb->query($query);
  57. }
  58. //Multi page
  59. if( $multipage == true ) {
  60. $data_ft = $data_array[$this->prefix . 'fulltext'];
  61. $data_fts = explode("{{pagebreak}}", $data_ft);
  62. if( is_array($data_fts) ) {
  63. $page = 0;
  64. $parent = 0;
  65. $items_ids = array();
  66. for($i=0, $n=sizeof($data_fts); $i<$n; $i++) {
  67. if( strlen($data_fts[$i]) > 0 ) {
  68. $page++;
  69. }
  70. }
  71. if( $page > 0 ) {
  72. $this->updateItem($items_id, array($this->prefix . 'pagecount' => $page));
  73. }
  74. }
  75. }
  76. return $items_id;
  77. }
  78. //Multi page
  79. function addItemP($data_array) {
  80. //$data_ft = $this->_filter_content($data_array[$this->prefix . 'fulltext']);
  81. $data_fts = explode("{{pagebreak}}", $data_ft);
  82. if( is_array($data_fts) ) {
  83. $page = 0;
  84. $parent = 0;
  85. $items_ids = array();
  86. for($i=0, $n=sizeof($data_fts); $i<$n; $i++) {
  87. if( strlen($data_fts[$i]) > 0 ) {
  88. $data_array[$this->prefix . 'fulltext'] = $data_fts[$i];
  89. $data_array[$this->prefix . 'parent'] = $parent;
  90. if( $page == 0 ) {
  91. $items_ids[] = $parent = $this->addItem($data_array, true);
  92. } else {
  93. $items_ids[] = $this->addItem($data_array, false);
  94. }
  95. $page++;
  96. }
  97. }
  98. if( $page > 0 ) {
  99. $this->updateItem($parent, array($this->prefix . 'pagecount' => $page, $this->prefix . 'pages' => $this->_implode(', ', $items_ids)));
  100. }
  101. return $parent;
  102. } else {
  103. return $this->addItem($data_array);
  104. }
  105. }
  106. function addItemCategory($items_id, $categories_id) {
  107. if( $items_id > 0 && $categories_id > 0 ) {
  108. $query = "select count(*) as count from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "' "
  109. . " and categories_id = '" . (int)$categories_id . "'";
  110. $count = $this->adb->getCount($query);
  111. if( $count == 0 ) {
  112. $query = "insert into " . $this->table_to_category . " (" . $this->prefix . "id, categories_id) "
  113. . " values ('" . (int)$items_id . "', '" . (int)$categories_id . "')";
  114. $result = $this->adb->query($query);
  115. return true;
  116. } elseif( $nums == 1 ) {
  117. return 1;
  118. } elseif( $nums > 1 ) {
  119. return 2;
  120. }
  121. }
  122. return false;
  123. }
  124. function addItemView($items_id) {
  125. $query = "update " . $this->table . " set " . $this->prefix . "viewed = " . $this->prefix . "viewed + 1 "
  126. . " where " . $this->prefix . "id = '" . (int)$items_id . "'";
  127. return $this->adb->query($query);
  128. }
  129. function getItem($value, $type = 'id', $check = 0) {
  130. $value = func_db_input($value);
  131. if( $check == -1 ) {
  132. $query_str = "";
  133. } else {
  134. $query_str = " and c." . $this->prefix . "status = 1";
  135. }
  136. if( $type == 'id' ) {
  137. $query = "select c.*, c2c.categories_id from " . $this->table . " c "
  138. . " left join " . $this->table_to_category . " c2c on c." . $this->prefix . "id = c2c." . $this->prefix . "id "
  139. . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "id = '" . (int)$value . "' " . $query_str;
  140. } elseif( $type == 'link' ) {
  141. $query = "select c.*, c2c.categories_id from " . $this->table . " c "
  142. . " left join " . $this->table_to_category . " c2c on c." . $this->prefix . "id = c2c." . $this->prefix . "id "
  143. . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "link = '" . $value . "' " . $query_str;
  144. } else {
  145. return false;
  146. }
  147. return $this->adb->getRow($query);
  148. }
  149. function getPreviousItem($value, $type = 'id', $check = 0) {
  150. $value = func_db_input($value);
  151. $query_limit_str = " limit 1";
  152. if( $check == -1 ) {
  153. $query_str = "";
  154. } else {
  155. $query_str = " and a." . $this->prefix . "status = 1";
  156. }
  157. $query_str .= " order by " . $this->prefix . "id desc ";
  158. if( $type == 'link' ) {
  159. $item = $this->getItem($value, 'link', -1, 0);
  160. $value = $item[$this->prefix . 'id'];
  161. }
  162. $query = "select a.* from " . $this->table . " a "
  163. . " left join " . $this->table_to_category . " c2ca on a." . $this->prefix . "id = c2ca." . $this->prefix . "id, "
  164. . " " . $this->table . " b left join " . $this->table_to_category . " c2cb on b." . $this->prefix . "id = c2cb." . $this->prefix . "id "
  165. . " where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id < '" . (int)$value . "' "
  166. . " and b." . $this->prefix . "id = '" . (int)$value . "' and c2ca.categories_id = c2cb.categories_id "
  167. . $query_str . $query_limit_str;
  168. return $this->adb->getRow($query);
  169. }
  170. function getNextItem($value, $type = 'id', $check = 0) {
  171. $value = func_db_input($value);
  172. $query_limit_str = " limit 1";
  173. if( $check == -1 ) {
  174. $query_str = "";
  175. } else {
  176. $query_str = " and a." . $this->prefix . "status = 1";
  177. }
  178. $query_str .= " order by " . $this->prefix . "id asc ";
  179. if( $type == 'link' ) {
  180. $item = $this->getItem($value, 'link', -1, 0);
  181. $value = $item[$this->prefix . 'id'];
  182. }
  183. $query = "select a.* from " . $this->table . " "
  184. . " a left join " . $this->table_to_category . " c2ca on a." . $this->prefix . "id = c2ca." . $this->prefix . "id, "
  185. . " " . $this->table . " b left join " . $this->table_to_category . " c2cb on b." . $this->prefix . "id = c2cb." . $this->prefix . "id "
  186. . " where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id > '" . (int)$value . "' "
  187. . " and b." . $this->prefix . "id = '" . (int)$value . "' and c2ca.categories_id = c2cb.categories_id "
  188. . $query_str . $query_limit_str;
  189. return $this->adb->getRow($query);
  190. }
  191. function getItemsP($items_id, $check = 0) {
  192. if( $check == -1 ) {
  193. $query_str = "";
  194. } else {
  195. $query_str = " and c." . $this->prefix . "status = 1";
  196. }
  197. $query = "select c.*, c2c.categories_id from " . $this->table . " c "
  198. . " left join " . $this->table_to_category . " c2c on c." . $this->prefix . "id = c2c." . $this->prefix . "id "
  199. . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "parent = '" . (int)$items_id . "' " . $query_str;
  200. return $this->adb->getRows($query);
  201. }
  202. function getCategoryID($items_id) {
  203. $query = "select categories_id from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
  204. $row = $this->adb->getRow($query);
  205. return $row['categories_id'];
  206. }
  207. function getCategory($items_id) {
  208. $query = "select cc.* from " . $this->table_category . " cc, " . $this->table_to_category . " c2c "
  209. . " where cc.categories_id = c2c.categories_id and c2c." . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
  210. return $this->adb->getRow($query);
  211. }
  212. function getCategories($items_id) {
  213. $query = "select cc.* from " . $this->table_category . " cc, " . $this->table_to_category . " c2c "
  214. . " where cc.categories_id = c2c.categories_id and c2c." . $this->prefix . "id = '" . (int)$items_id . "'";
  215. return $this->adb->getRows($query);
  216. }
  217. function getCategoriesID($items_id) {
  218. $query = "select categories_id from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
  219. $result = $this->adb->query($query);
  220. while($row = $this->adb->fetch_array($result)) {
  221. $rows[] = $row['categories_id'];
  222. }
  223. return $rows;
  224. }
  225. function getItems($filter = '', $check = 0, $limit = true, $level = 1) {
  226. $query_limit_str = "";
  227. if( $limit == true ) {
  228. $query_limit_str = $this->getQueryLimitStr();
  229. }
  230. $query_str_category = $this->_get_query_str($filter, $check);
  231. $query_str = $query_str_category['0'];
  232. $query_category = $query_str_category['1'];
  233. $query_order_str .= " order by c." . $this->prefix . "top desc, c." . $this->prefix . "sort_order asc, c." . $this->prefix . "id desc ";
  234. if( $query_category == 1 ) {
  235. $query = "select c.*, c2c.categories_id from " . $this->table . " c, " . $this->table_to_category . " c2c "
  236. . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "parent = 0 "
  237. . $query_str . $query_order_str . $query_limit_str;
  238. } else {
  239. $query = "select c.* from " . $this->table . " c "
  240. . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "parent = 0 "
  241. . $query_str . $query_order_str . $query_limit_str;
  242. }
  243. return $this->adb->getRows($query);
  244. }
  245. function getItemsTotal($filter = '', $check = 0, $limit = true) {
  246. $query_str_category = $this->_get_query_str($filter, $check);
  247. $query_str = $query_str_category['0'];
  248. $query_category = $query_str_category['1'];
  249. if( $query_category == 1 ) {
  250. $query = "select count(*) as count from " . $this->table . " c, " . $this->table_to_category . " c2c "
  251. . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "parent = 0 " . $query_str;
  252. } else {
  253. $query = "select count(*) as count from " . $this->table . " c "
  254. . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "parent = 0 " . $query_str;
  255. }
  256. return $this->adb->getCount($query);
  257. }
  258. function getLatestItems($categories_id = 0, $limit_length = 5) {
  259. if( $limit_length > 0 ) {
  260. } else {
  261. $limit_lenght = 5;
  262. }
  263. $query_limit_str = " limit " . $limit_length;
  264. $query_str = "";
  265. if( $categories_id > 0 ) {
  266. $query = "select c.*, cc.categories_name from " . $this->table . " c, " . $this->table_category . " cc, " . $this->table_to_category . " c2c "
  267. . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "parent = 0 "
  268. . " and c." . $this->prefix . "id = c2c." . $this->prefix . "id "
  269. . " and c2c.categories_id = cc.categories_id and cc.categories_id = '" . (int)$categories_id . "' "
  270. . $query_str . " order by c." . $this->prefix . "date_added desc, c." . $this->prefix . "sort_order asc " . $query_limit_str;
  271. } else {
  272. $query = "select c.* from " . $this->table . " c "
  273. . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "parent = 0 "
  274. . $query_str . " order by c." . $this->prefix . "date_added desc, c." . $this->prefix . "sort_order asc " . $query_limit_str;
  275. }
  276. return $this->adb->getRows($query);
  277. }
  278. function getLatestItemsR($categories_id = 0, $include_subs = true, $count = 5) {
  279. if( $count > 0 ) {
  280. //
  281. } else {
  282. $count = 6;
  283. }
  284. $limit_length = ($count * 3);
  285. $query_limit_str = " limit " . $limit_length;
  286. if( $categories_id > 0 ) {
  287. $categories_ids = $this->getCategoriesIDs($categories_id, $include_subs);
  288. if( is_array($categories_ids) ) {
  289. $category = array_merge(array($categories_id), $categories_ids);
  290. } else {
  291. $category = $categories_id;
  292. }
  293. if( $filter == '' || !is_array($filter) ) {
  294. $filter = array();
  295. $filter['category'] = $category;
  296. } else {
  297. $filter['category'] = $category;
  298. }
  299. $rows = $this->getItems($filter, $check);
  300. } else {
  301. $query_str = "";
  302. $query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " and " . $this->prefix . "parent = 0 "
  303. . $query_str . " order by " . $this->prefix . "date_added desc, " . $this->prefix . "sort_order asc " . $query_limit_str;
  304. $result = $this->adb->query($query);
  305. while($row = $this->adb->fetch_array($result)) {
  306. $rows[] = $row;
  307. }
  308. }
  309. return $this->_rows_rands($rows, $count);
  310. }
  311. function getFocusItems($count = 5) {
  312. if( $count > 0 ) {
  313. //
  314. } else {
  315. $count = 6;
  316. }
  317. $limit_length = ($count * 3);
  318. $query_limit_str = " limit " . $limit_length;
  319. $query_str = "";
  320. $query = "select c.*, r.* from " . $this->table . " c, pp_resources r "
  321. . " where " . $this->_query_owner_rt('c') . " "
  322. . " and c." . $this->prefix . "id = r.resources_target and r.resources_type = 'content' "
  323. . " and (r.resources_filetype = 'image/pjpeg' or r.resources_filetype = 'image/jpeg' or r.resources_filetype = 'image/gif') "
  324. . $query_str
  325. . " group by c." . $this->prefix . "id "
  326. . " order by c." . $this->prefix . "top desc, r.resources_date_added desc, c." . $this->prefix . "date_added desc, c." . $this->prefix . "sort_order asc "
  327. . $query_limit_str;
  328. $rows = $this->adb->getRows($query);
  329. return $rows;
  330. }
  331. function getCategoryItems($categories_id, $include_subs = true, $filter = '', $check = 0, $limit = true) {
  332. $categories_ids = $this->getCategoriesIDs($categories_id, $include_subs);
  333. if( is_array($categories_ids) ) {
  334. $category = array_merge(array($categories_id), $categories_ids);
  335. } else {
  336. $category = $categories_id;
  337. }
  338. if( $filter == '' || !is_array($filter) ) {
  339. $filter = array();
  340. $filter['category'] = $category;
  341. } else {
  342. $filter['category'] = $category;
  343. }
  344. return $this->getItems($filter, $check, $limit);
  345. }
  346. function getCategoryItemsTotal($categories_id, $include_subs = true, $filter = '', $check = 0) {
  347. $categories_ids = $this->getCategoriesIDs($categories_id, $include_subs);
  348. if( is_array($categories_ids) ) {
  349. $category = array_merge(array($categories_id), $categories_ids);
  350. } else {
  351. $category = $categories_id;
  352. }
  353. if( $filter == '' || !is_array($filter) ) {
  354. $filter = array();
  355. $filter['category'] = $category;
  356. } else {
  357. $filter['category'] = $category;
  358. }
  359. return $this->getItemsTotal($filter, $check);
  360. }
  361. function updateItem($items_id, $data_array) {
  362. //$data_array[$this->prefix . 'fulltext'] = $this->_filter_content($data_array[$this->prefix . 'fulltext']);
  363. $data_array = func_db_input($data_array);
  364. $data_array[$this->prefix . 'last_modified'] = 'now()';
  365. $data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
  366. return $this->_update($this->table, $data_array, $this->prefix . "id = '" . (int)$items_id . "'");
  367. }
  368. function updateItemCategory($items_id, $categories_id) {
  369. return $this->adb->update("update " . $this->table_to_category . " set categories_id = '" . (int)$categories_id . "' where " . $this->prefix . "id = '" . (int)$items_id . "'");
  370. }
  371. function updateItemGroup($items_id, $groups_id) {
  372. return $this->adb->update("update " . $this->table_to_category . " set groups_id = '" . (int)$groups_id . "' where " . $this->prefix . "id = '" . (int)$items_id . "'");
  373. }
  374. function updateLink($items_id, $items_link) {
  375. $items_link = func_db_input($items_link);
  376. $query = "select count(*) as count from " . $this->table
  377. . " where " . $this->prefix . "id != '" . (int)$items_id . "' and " . $this->prefix . "link = '" . $items_link . "'";
  378. $row = $this->adb->getRow($query);
  379. if( $row['count'] > 0 ) {
  380. return -1;
  381. } else {
  382. $query = "update " . $this->table . " set " . $this->prefix . "link = '" . $items_link . "' where " . $this->prefix . "id = '" . (int)$items_id . "'";
  383. $result = $this->adb->query($query);
  384. }
  385. return 1;
  386. }
  387. function updateItems($items_ids, $data_array) {
  388. if( is_array($items_ids) ) {
  389. if( sizeof($items_ids) > 30 ) {
  390. foreach($items_ids as $k => $items_id) {
  391. $this->_update($this->table, $data_array, $this->prefix . "id = '" . (int)$items_id . "'");
  392. }
  393. } else {
  394. $query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
  395. $this->_update($this->table, $data_array, $this->prefix . "id in (" . $query_ids . ")");
  396. }
  397. }
  398. }
  399. function updateItemsF($items_ids, $field, $value = '') {
  400. if( $field == 'top' ) {
  401. $query_str = " " . $this->prefix . "top = '1' ";
  402. } elseif( $field == 'untop' ) {
  403. $query_str = " " . $this->prefix . "top = '0' ";
  404. } elseif( $field == 'recommend' ) {
  405. $query_str = " " . $this->prefix . "recommend = '1' ";
  406. } elseif( $field == 'unrecommend' ) {
  407. $query_str = " " . $this->prefix . "recommend = '0' ";
  408. } elseif( $field == 'public' ) {
  409. $query_str = " " . $this->prefix . "status = '1' ";
  410. } elseif( $field == 'unpublic' ) {
  411. $query_str = " " . $this->prefix . "status = '0' ";
  412. } elseif( $field == 'statusopen' ) {
  413. $query_str = " " . $this->prefix . "status = '1' ";
  414. } elseif( $field == 'statusclose' ) {
  415. $query_str = " " . $this->prefix . "status = '0' ";
  416. } elseif( $field == 'level' ) {
  417. $query_str = " " . $this->prefix . "level = '" . (int)$value . "' ";
  418. } elseif( $field == 'unlevel' ) {
  419. $query_str = " " . $this->prefix . "level = '0' ";
  420. } else {
  421. return;
  422. }
  423. if( is_array($items_ids) ) {
  424. if( sizeof($items_ids) > 30 ) {
  425. foreach($items_ids as $k => $items_id) {
  426. $this->adb->update("update " . $this->table . " set $query_str where " . $this->prefix . "id = '" . (int)$items_id . "'");
  427. }
  428. } else {
  429. $query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
  430. $this->adb->update("update " . $this->table . " set $query_str where " . $this->prefix . "id in (" . $query_ids . ")");
  431. }
  432. }
  433. }
  434. function updateItemsCategory($items_ids, $categories_id) {
  435. if( is_array($items_ids) ) {
  436. if( sizeof($items_ids) > 30 ) {
  437. foreach($items_ids as $k => $items_id) {
  438. $this->adb->update("update " . $this->table_to_category . " set categories_id = '" . (int)$categories_id . "' where " . $this->prefix . "id = '" . (int)$items_id . "'");
  439. }
  440. } else {
  441. $query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
  442. return $this->adb->update("update " . $this->table_to_category . " set categories_id = '" . (int)$categories_id . "' where " . $this->prefix . "id in (" . $query_ids . ")");
  443. }
  444. }
  445. }
  446. function updateCategoryItemstoNewCategory($categories_id, $categories_id_new) {
  447. $query = "update " . $this->table_to_category . " set categories_id = '" . (int)$categories_id_new . "' "
  448. . " where categories_id = '" . (int)$categories_id . "'";
  449. return $this->adb->update($query);
  450. }
  451. function deleteItem($items_id) {
  452. $query = "delete from " . $this->table . " where " . $this->prefix . "id = '" . (int)$items_id . "'";
  453. $result = $this->adb->query($query);
  454. $query = "delete from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "'";
  455. $this->adb->query($query);
  456. }
  457. function deleteItems($items_ids) {
  458. if( is_array($items_ids) ) {
  459. $this->_delete_ins($this->table, $this->prefix . 'id', $items_ids);
  460. }
  461. }
  462. function deleteCategoryItems($categories_id) {
  463. $query = "delete pp_contents, " . $this->table_to_category . " from " . $this->table . " c, " . $this->table_to_category . " c2c "
  464. . " where c." . $this->prefix . "id = c2c." . $this->prefix . "id and c2c.categories_id = '" . (int)$categories_id . "'";
  465. $this->adb->delete($query);
  466. $this->adb->updateCategoryItemstoNewCategory($categories_id, 0);
  467. }
  468. function setItemsStatus($items_ids, $status = 1) {
  469. if( is_array($items_ids) ) {
  470. $this->_update_ins($this->table, "" . $this->prefix . "status = '" . (int)$status . "'", $this->prefix . 'id', $items_ids);
  471. }
  472. }
  473. //
  474. function getCategoryObj() {
  475. global $ccObj;
  476. if( is_object($ccObj) && get_class($ccObj) == 'ContentCategoryBase' ) {
  477. //
  478. } else {
  479. include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'content/content.category.class.php');
  480. $ccObj = new ContentCategoryBase();
  481. }
  482. return $ccObj;
  483. }
  484. function getCategoriesIDs($categories_id, $include_subs = true) {
  485. $_ccObj = $this->getCategoryObj();
  486. if( $include_subs == true ) {
  487. $categories_ids = $_ccObj->getCategoriesAllSubsIDs($categories_id);
  488. } else {
  489. $categories_ids = $_ccObj->getCategoriesSubsIDs($categories_id);
  490. }
  491. return $categories_ids;
  492. }
  493. //private
  494. function _get_query_str($filter = '', $check = 0) {
  495. $query_category = 0;
  496. $query_str = "";
  497. if( $check == -1 ) {
  498. $query_str .= "";
  499. } else {
  500. $query_str .= " and c." . $this->prefix . "status = 1";
  501. }
  502. if( is_array($filter) ) {
  503. $filter = func_db_input($filter);
  504. if( is_array($this->filter_query) ) {
  505. $filter = array_merge($filter, $this->filter_query);
  506. }
  507. if( is_array($this->filters) ) {
  508. foreach($this->filters as $k => $v) {
  509. $filter[$k] = $v;
  510. }
  511. }
  512. //
  513. if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
  514. if( is_array($this->search_fields) ) {
  515. if( $this->search_type != 'or' ) {
  516. $this->search_type = 'and';
  517. }
  518. $i = 0;
  519. $query_str .= " and ( ";
  520. foreach($this->search_fields as $k => $field) {
  521. if( $i > 0 ) {
  522. $query_str .= " " . $this->search_type . " ";
  523. }
  524. $query_str .= " " . $this->prefix . $field . " like '%" . $filter['keyword'] . "%' ";
  525. $i++;
  526. }
  527. $query_str .= " ) ";
  528. } elseif( is_string($this->search_fields) ) {
  529. $query_str .= " and " . $this->prefix . $this->search_fields . " like '%" . $filter['keyword'] . "%' ";
  530. } else {
  531. $query_str .= " and c." . $this->prefix . "subject like '%" . $filter['keyword'] . "%' ";
  532. }
  533. }
  534. if( isset($filter['ids']) && $this->_not_null($filter['ids']) == true ) {
  535. if( is_array($filter['ids']) ) {
  536. $query_str .= " and c." . $this->prefix . "id in (" . $this->_implode(', ', $filter['ids']) . ") ";
  537. } else {
  538. $query_str .= " and c." . $this->prefix . "id = '" . (int)$filter['ids'] . "' ";
  539. }
  540. }
  541. if( isset($filter['parent']) && $this->_not_null($filter['parent']) == true ) {
  542. if( is_array($filter['parent']) ) {
  543. $query_str .= " and c." . $this->prefix . "parent in (" . $this->_implode(', ', $filter['parent']) . ") ";
  544. } else {
  545. $query_str .= " and c." . $this->prefix . "parent = '" . (int)$filter['parent'] . "' ";
  546. }
  547. }
  548. if( $this->table_to_category != '' ) {
  549. if( isset($filter['category']) ) {
  550. $query_category = 1;
  551. if( is_array($filter['category']) ) {
  552. $query_str .= " and c." . $this->prefix . "id = c2c." . $this->prefix . "id and c2c.categories_id in (" . $this->_implode(', ', $filter['category']) . ") ";
  553. } else {
  554. $query_str .= " and c." . $this->prefix . "id = c2c." . $this->prefix . "id and c2c.categories_id = '" . (int)$filter['category'] . "' ";
  555. }
  556. }
  557. if( isset($filter['nocategory']) && $filter['nocategory'] > 0 ) {
  558. $query_str .= " and and c." . $this->prefix . "id = c2c." . $this->prefix . "id and c2c.categories_id = '0' ";
  559. }
  560. } else {
  561. if( isset($filter['category']) ) {
  562. if( is_array($filter['category']) ) {
  563. $query_str .= " and categories_id in (" . $this->_implode(', ', $filter['category']) . ") ";
  564. } else {
  565. $query_str .= " and categories_id = '" . (int)$filter['category'] . "' ";
  566. }
  567. }
  568. if( isset($filter['nocategory']) && $filter['nocategory'] > 0 ) {
  569. $query_str .= " and categories_id = '0' ";
  570. }
  571. }
  572. if( is_array($this->filter_fields) && sizeof($this->filter_fields) > 0 ) {
  573. $qt = $this->_get_query_type();
  574. foreach($filter as $k => $v) {
  575. if( is_array($this->filter_map) && isset($this->filter_map[$k]) ) {
  576. $k = $this->filter_map[$k];
  577. }
  578. if( in_array($k, $this->filter_fields) ) {
  579. $t = '';
  580. $p = $this->prefix;
  581. if( strpos($k, ':') !== false ) {
  582. $ks = explode(':', $k);
  583. $k = $ks['0'];
  584. $t = $ks['1'];
  585. if( sizeof($ks) > 2 ) {
  586. $p = $ks['2'];
  587. }
  588. }
  589. if( substr($k, 0, 1) == '=' ) {
  590. $k = substr($k, 1);
  591. $p = '';
  592. }
  593. if( !in_array($t, $qt) ) {
  594. $t = '';
  595. }
  596. if( in_array($t, array('=', '>', '>=', '<', '<=', '!=')) ) {
  597. if( $v === 'now()' || $v === 'now' ) {
  598. $query_str .= " and " . $p . $k . " " . $t . " now() ";
  599. } else {
  600. $query_str .= " and " . $p . $k . " " . $t . " '" . $v . "' ";
  601. }
  602. } elseif( in_array($t, array('[]', '[)', '(]', '()')) ) {
  603. if( is_array($v) ) {
  604. $t1 = substr($t, 0, 1);
  605. $t2 = substr($t, 1, 1);
  606. if( $t1 == '[' ) {
  607. $t1 = '<=';
  608. } elseif( $t1 == '(' ) {
  609. $t1 = '<';
  610. }
  611. if( $t2 == '[' ) {
  612. $t2 = '<=';
  613. } elseif( $t1 == '(' ) {
  614. $t2 = '<';
  615. }
  616. $query_str .= " and " . $p . $k . " " . $t1 . " '" . $v['0'] . "' ";
  617. $query_str .= " and " . $p . $k . " " . $t2 . " '" . $v['1'] . "' ";
  618. }
  619. } elseif( in_array($t, array('like', 'notlike', '%like%', '%like', 'like%',)) ) {
  620. if( $t == 'notlike' ) {
  621. $query_str .= " and " . $p . $k . " not like '%" . $v . "%' ";
  622. } else {
  623. $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
  624. }
  625. } else {
  626. if( $v === 'now()' || $v === 'now' ) {
  627. $query_str .= " and " . $p . $k . " < now() ";
  628. } elseif( is_array($v) && sizeof($v) > 0 ) {
  629. $query_str .= " and " . $p . $k . " in ('" . implode("','", $v) . "') ";
  630. } elseif( is_int($v) ) {
  631. $query_str .= " and " . $p . $k . " = '" . $v . "' ";
  632. } elseif( is_numeric($v) ) {
  633. $query_str .= " and " . $p . $k . " = '" . $v . "' ";
  634. } elseif( is_string($v) ) {
  635. $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
  636. } else {
  637. $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
  638. }
  639. }
  640. }
  641. }
  642. }
  643. }
  644. //
  645. $query_str .= $this->_get_query_str_custom($filter, $check);
  646. return array($query_str, $query_category);
  647. }
  648. function _get_query_str_custom($filter = '', $check = 0) {
  649. return "";
  650. }
  651. function _get_query_order_sort($order_by = '', $order_sort = '') {
  652. if( $order_by == '' ) {
  653. $order_by = $this->query_order_by;
  654. }
  655. if( $order_sort == '' ) {
  656. $order_sort = $this->query_order_sort;
  657. }
  658. if( $order_by != '' && $order_sort != '' ) {
  659. if( $order_by == 'dateadded' ) {
  660. $order_by = 'date_added';
  661. } elseif( $order_by == 'sortorder' ) {
  662. $order_by = 'sort_order';
  663. }
  664. //
  665. $sort_query = array();
  666. if( !is_array($this->sort_query) && is_array($this->sort_fields) ) {
  667. foreach($this->sort_fields as $k => $v) {
  668. if( substr($v, -1, 1) == '+' || substr($v, -1, 1) == '-' ) {
  669. $sort_query[] = substr($v, 0, -1);
  670. }
  671. }
  672. } else {
  673. $sort_query = $this->sort_query;
  674. }
  675. if( is_array($sort_query) && in_array($order_by, $sort_query) ) {
  676. $sort_fields = array($order_by . ($order_sort == 'desc' ? '+' : '-'));
  677. $this->sort_fields = $sort_fields;
  678. }
  679. }
  680. }
  681. function _query_owner_rt($pf = '') {
  682. if( $this->_table_owner_usable == true ) {
  683. if( $this->_table_owner_prefix == '=' ) {
  684. $p = '';
  685. } elseif( $this->_table_owner_prefix == '+' ) {
  686. $p = $this->prefix;
  687. } elseif( $this->_table_owner_prefix == '' ) {
  688. $p = $this->prefix;
  689. } else {
  690. $p = $this->_table_owner_prefix;
  691. }
  692. if( $pf != '' ) {
  693. $p = $pf . '.' . $p;
  694. }
  695. return $this->_query_owner($p . $this->_table_owner_field);
  696. }
  697. return " 1 ";
  698. }
  699. function _get_query_type() {
  700. $query_type = array(
  701. '=', '>', '>=', '<', '<=', '!=',
  702. '[]', '()', '[)', '(]',
  703. 'like', 'notlike', '%like%', '%like', 'like%',
  704. 'regexp', 'notregexp',
  705. );
  706. return $query_type;
  707. }
  708. //
  709. function _generate_item_link($items_id, $items_name = '', $items_link = '') {
  710. if( $items_link == '' && $items_name != '' ) {
  711. $items_link = $this->_filter_link($items_name);
  712. }
  713. if( $this->_not_null($items_link) ) {
  714. $items_link = func_db_input($items_link);
  715. $query = "select count(*) as count from " . $this->table . " where " . $this->prefix . "id != '" . (int)$items_id . "' "
  716. . " and " . $this->prefix . "link = '" . $items_link . "'";
  717. $count = $this->adb->getCount($query);
  718. if( $count == 0 ) {
  719. $this->updateItem($items_id, array($this->prefix . 'link' => $items_link));
  720. return true;
  721. }
  722. }
  723. $items_link = $this->_filter_link($items_name);
  724. $items_link = $this->_rand_link($items_id, $items_link);
  725. $this->_generate_item_link($items_id, $items_name, $items_link);
  726. return true;
  727. }
  728. function _generate_item_link_datecount($items_id, $page = '1') {
  729. $query = "select count(*) as count from " . $this->table . " c1, " . $this->table . " c2 "
  730. . " where TO_DAYS(c1." . $this->prefix . "date_added) = TO_DAYS(c2." . $this->prefix . "date_added) "
  731. . " and c1." . $this->prefix . "id != c2." . $this->prefix . "id and c2." . $this->prefix . "id = '" . (int)$items_id . "'";
  732. $count = $this->adb->getCount($query);
  733. $link = strftime("%Y%m%d", time()) . $this->AlignLength($count, 4);
  734. if( $page > 1 ) {
  735. $link .= '_' . $link;
  736. }
  737. return $link;
  738. }
  739. function _update_item_link_datecount($items_id, $page = '1') {
  740. $link = $this->_generate_item_link_datecount($items_id, $page);
  741. $this->updateItem($items_id, array($this->prefix . 'link' => $link));
  742. }
  743. //
  744. function _filter_content($content) {
  745. $a = func_filter_attach_convert($content);
  746. return $a['0'];
  747. }
  748. }
  749. //
  750. ?>