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

http://php-ppcms.googlecode.com/ · PHP · 402 lines · 325 code · 60 blank · 17 comment · 95 complexity · 160f0daa231c6fcf11ada15a98147d2b 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 BaseItemDescriptionBase extends BaseItemBase {
  9. //constructor
  10. function BaseItemDescriptionBase() {
  11. parent::BaseItemBase();
  12. }
  13. //methods
  14. function getItems($filter = '', $check = 0, $limit = true, $level = 1) {
  15. $query_limit_str = "";
  16. if( $limit == true ) {
  17. $query_limit_str = $this->getQueryLimitStr();
  18. }
  19. $query_category = 0;
  20. $query_str = "";
  21. if( $check == -1 ) {
  22. $query_str .= "";
  23. } else {
  24. $query_str .= " and a." . $this->prefix . "status = 1";
  25. }
  26. if( is_array($filter) ) {
  27. $filter = func_db_input($filter);
  28. if( is_array($this->filter_query) ) {
  29. $filter = array_merge($filter, $this->filter_query);
  30. }
  31. //
  32. if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
  33. $query_str .= " and ad." . $this->prefix . "subject like '%" . $filter['keyword'] . "%' ";
  34. }
  35. if( isset($filter['category']) && $this->_not_null($filter['category']) == true ) {
  36. $query_category = 1;
  37. if( is_array($filter['category']) ) {
  38. $query_str .= " and a." . $this->prefix . "id = a2c." . $this->prefix . "id and a2c.categories_id in (" . implode(', ', $filter['category']) . ") ";
  39. } else {
  40. $query_str .= " and a." . $this->prefix . "id = a2c." . $this->prefix . "id and a2c.categories_id = '" . (int)$filter['category'] . "' ";
  41. }
  42. }
  43. if( isset($filter['ids']) && $this->_not_null($filter['ids']) == true ) {
  44. if( is_array($filter['ids']) ) {
  45. $query_str .= " and a." . $this->prefix . "id in (" . implode(', ', $filter['ids']) . ") ";
  46. } else {
  47. $query_str .= " and a." . $this->prefix . "id = '" . (int)$filter['ids'] . "' ";
  48. }
  49. }
  50. }
  51. if( $level == 0 ) {
  52. if( $query_category == 1 ) {
  53. $query = "select a.*, ad." . $this->prefix . "subject from " . $this->table . " a, " . $this->table_description . " ad, " . $this->table_to_category . " a2c where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str . $query_limit_str;
  54. } else {
  55. $query = "select a.*, ad." . $this->prefix . "subject from " . $this->table . " a, " . $this->table_description . " ad where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str . $query_limit_str;
  56. }
  57. } else {
  58. if( $query_category == 1 ) {
  59. $query = "select a.*, ad.* from " . $this->table . " a, " . $this->table_description . " ad, " . $this->table_to_category . " a2c where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str . $query_limit_str;
  60. } else {
  61. $query = "select a.*, ad.* from " . $this->table . " a, " . $this->table_description . " ad where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str . $query_limit_str;
  62. }
  63. }
  64. $result = $this->adb->query($query);
  65. while($row = $this->adb->fetch_array($result)) {
  66. $rows[] = $row;
  67. }
  68. return $rows;
  69. }
  70. function getItemsTotal($filter = '', $check = 0, $limit = true) {
  71. $query_category = 0;
  72. $query_str = "";
  73. if( $check == -1 ) {
  74. $query_str .= "";
  75. } else {
  76. $query_str .= " and a." . $this->prefix . "status = 1";
  77. }
  78. if( is_array($filter) ) {
  79. $filter = func_db_input($filter);
  80. if( is_array($this->filter_query) ) {
  81. $filter = array_merge($filter, $this->filter_query);
  82. }
  83. //
  84. if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
  85. $query_str .= " and ad." . $this->prefix . "subject like '%" . $filter['keyword'] . "%' ";
  86. }
  87. if( isset($filter['category']) && $this->_not_null($filter['category']) == true ) {
  88. $query_category = 1;
  89. if( is_array($filter['category']) ) {
  90. $query_str .= " and a." . $this->prefix . "id = a2c." . $this->prefix . "id and a2c.categories_id in (" . implode(', ', $filter['category']) . ") ";
  91. } else {
  92. $query_str .= " and a." . $this->prefix . "id = a2c." . $this->prefix . "id and a2c.categories_id = '" . (int)$filter['category'] . "' ";
  93. }
  94. }
  95. }
  96. if( $query_category == 1 ) {
  97. $query = "select distinct count(*) as count from " . $this->table . " a, " . $this->table_description . " ad, " . $this->table_to_category . " a2c where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str;
  98. } else {
  99. $query = "select distinct count(*) as count from " . $this->table . " a, " . $this->table_description . " ad where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str;
  100. }
  101. $row = $this->adb->getRow($query);
  102. return $row['count'];
  103. }
  104. function getLatestItems($limit_length = 5) {
  105. if( $limit_length > 0 ) {
  106. } else {
  107. $limit_lenght = 5;
  108. }
  109. $query_limit_str = " limit " . $limit_length;
  110. $query = "select a.*, ad." . $this->prefix . "subject from " . $this->table . " a, " . $this->table_description . " ad where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' order by a." . $this->prefix . "date_added desc " . $query_limit_str;
  111. $result = $this->adb->query($query);
  112. while($row = $this->adb->fetch_array($result)) {
  113. $rows[] = $row;
  114. }
  115. return $rows;
  116. }
  117. //
  118. function addItem($data_array) {
  119. $data_array = func_db_input($data_array);
  120. $data_array[$this->prefix . 'owner'] = $this->owner;
  121. $data_array[$this->prefix . 'date_added'] = 'now()';
  122. $data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
  123. $data_array[$this->prefix . 'status'] = '1';
  124. $items_id = $this->_insert($this->table, $data_array);
  125. //description
  126. $data_array[$this->prefix . 'id'] = $items_id;
  127. $data_array['languages_id'] = $this->languages_id;
  128. $this->_add($this->table_description, $data_array);
  129. //link
  130. if( $items_id > 0 ) {
  131. $this->_generate_item_link($items_id, $data_array[$this->prefix . 'subject']);
  132. }
  133. //category
  134. $categories_id = $data_array['categories_id'];
  135. $query = "insert into " . $this->table_to_category . " (" . $this->prefix . "id, categories_id) values ('" . $items_id . "', '" . $categories_id . "')";
  136. $result = $this->adb->query($query);
  137. return true;
  138. }
  139. function addItemCategory($items_id, $categories_id) {
  140. if( $items_id > 0 && $categories_id > 0 ) {
  141. $query = "select count(*) as count from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "' and categories_id = '" . (int)$categories_id . "'";
  142. $count = $this->adb->getCount($query);
  143. if( $count == 0 ) {
  144. $query = "insert into " . $this->table_to_category . " (" . $this->prefix . "id, categories_id) values ('" . (int)$items_id . "', '" . (int)$categories_id . "')";
  145. $result = $this->adb->query($query);
  146. return true;
  147. } elseif( $nums == 1 ) {
  148. return 1;
  149. } elseif( $nums > 1 ) {
  150. return 2;
  151. }
  152. }
  153. return false;
  154. }
  155. function addItemView($items_id) {
  156. $query = "update " . $this->table . " set " . $this->prefix . "viewed = " . $this->prefix . "viewed + 1 where " . $this->prefix . "id = '" . (int)$items_id . "'";
  157. $result = $this->adb->query($query);
  158. return true;
  159. }
  160. function getItem($value, $type = 'id', $check = 0) {
  161. $value = func_db_input($value);
  162. if( $check == -1 ) {
  163. $query_str = "";
  164. } else {
  165. $query_str = " and a." . $this->prefix . "status = 1";
  166. }
  167. if( $type == 'id' ) {
  168. $query = "select a.*, ad.* from " . $this->table . " a, " . $this->table_description . " ad "
  169. . " where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id = '" . (int)$value . "' "
  170. . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str;
  171. } elseif( $type == 'link' ) {
  172. $query = "select a.*, ad.* from " . $this->table . " a, " . $this->table_description . " ad "
  173. . " where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "link = '" . $value . "' "
  174. . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' " . $query_str;
  175. } else {
  176. return false;
  177. }
  178. return $this->adb->getRow($query);
  179. }
  180. function getPreviousItem($value, $type = 'id', $check = 0) {
  181. $value = func_db_input($value);
  182. $query_limit_str = " limit 1";
  183. if( $check == -1 ) {
  184. $query_str = "";
  185. } else {
  186. $query_str = " and a." . $this->prefix . "status = 1";
  187. }
  188. $query_str .= " order by " . $this->prefix . "id desc ";
  189. if( $type == 'link' ) {
  190. $item = $this->getItem($value, 'link', -1, 0);
  191. $value = $item[$this->prefix . 'id'];
  192. }
  193. $query = "select a.*, ad.* from " . $this->table . " a, " . $this->table_description . " ad "
  194. . " where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id < '" . (int)$value . "' "
  195. . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' "
  196. . $query_str . $query_limit_str;
  197. return $this->adb->getRow($query);
  198. }
  199. function getNextItem($value, $type = 'id', $check = 0) {
  200. $value = func_db_input($value);
  201. $query_limit_str = " limit 1";
  202. if( $check == -1 ) {
  203. $query_str = "";
  204. } else {
  205. $query_str = " and a." . $this->prefix . "status = 1";
  206. }
  207. $query_str .= " order by " . $this->prefix . "id asc ";
  208. if( $type == 'link' ) {
  209. $item = $this->getItem($value, 'link', -1, 0);
  210. $value = $item[$this->prefix . 'id'];
  211. }
  212. $query = "select a.*, ad.* from " . $this->table . " a, " . $this->table_description . " ad "
  213. . " where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id > '" . (int)$value . "' "
  214. . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' "
  215. . $query_str . $query_limit_str;
  216. return $this->adb->getRow($query);
  217. }
  218. function getCategoryID($items_id) {
  219. $query = "select categories_id from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
  220. $row = $this->adb->getRow($query);
  221. return $row['categories_id'];
  222. }
  223. function getCategory($items_id) {
  224. $query = "select c.*, cd.categories_name from " . $this->prefix . "categories c, " . $this->prefix . "categories_description cd, " . $this->table_to_category . " a2c where c.categories_id = cd.categories_id and cd.languages_id = '" . (int)$this->languages_id . "' and c.categories_id = a2c.categories_id and a2c." . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
  225. return $this->adb->getRow($query);
  226. }
  227. function getCategories($items_id) {
  228. $query = "select c.*, cd.categories_name from " . $this->prefix . "categories c, " . $this->prefix . "categories_description cd, " . $this->table_to_category . " a2c where c.categories_id = cd.categories_id and cd.languages_id = '" . (int)$this->languages_id . "' and c.categories_id = a2c.categories_id and a2c." . $this->prefix . "id = '" . (int)$items_id . "'";
  229. $result = $this->adb->query($query);
  230. while($row = $this->adb->fetch_array($result)) {
  231. $rows[] = $row;
  232. }
  233. return $row;
  234. }
  235. function getCategoriesID($items_id) {
  236. $query = "select categories_id from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
  237. $result = $this->adb->query($query);
  238. while($row = $this->adb->fetch_array($result)) {
  239. $rows[] = $row['categories_id'];
  240. }
  241. return $rows;
  242. }
  243. function updateItem($items_id, $data_array) {
  244. $data_array = func_db_input($data_array);
  245. $i = 0;
  246. $_fields = array($this->prefix . 'owner', $this->prefix . 'link', $this->prefix . 'type', $this->prefix . 'start_time', $this->prefix . 'end_time', $this->prefix . 'sort_order', $this->prefix . 'status');
  247. $query = "update " . $this->table . " set ";
  248. foreach($data_array as $k => $v) {
  249. if( in_array($k, $_fields) ) {
  250. if( $i == 0 ) {
  251. $query .= " " . $k . " = '" . $v . "' ";
  252. } else {
  253. $query .= " ," . $k . " = '" . $v . "' ";
  254. }
  255. $i++;
  256. }
  257. }
  258. if( $i > 0 ) {
  259. $query .= ", " . $this->prefix . "last_modified = now(), " . $this->prefix . "modified_by = '' where " . $this->prefix . "id = '" . (int)$items_id . "'";
  260. $result = $this->adb->query($query);
  261. }
  262. //desc
  263. $i = 0;
  264. reset($data_array);
  265. $_description_fields = array($this->prefix . 'subject', $this->prefix . 'subject2', $this->prefix . 'keywords', $this->prefix . 'introtext', $this->prefix . 'fulltext');
  266. $query = "update " . $this->table_description . " ad set ";
  267. foreach($data_array as $k => $v) {
  268. if( in_array($k, $_description_fields) ) {
  269. if( $i == 0 ) {
  270. $query .= " " . $k . " = '" . $v . "' ";
  271. } else {
  272. $query .= ", " . $k . " = '" . $v . "' ";
  273. }
  274. $i++;
  275. }
  276. }
  277. if( $i > 0 ) {
  278. $query .= " where " . $this->prefix . "id = '" . (int)$items_id . "' and languages_id = '" . (int)$this->languages_id . "'";
  279. $result = $this->adb->query($query);
  280. }
  281. return true;
  282. }
  283. function updateLink($items_id, $items_link) {
  284. $items_link = func_db_input($items_link);
  285. $query = "select count(*) as count from " . $this->table . " where " . $this->prefix . "id != '" . $items_id . "' and " . $this->prefix . "link = '" . $items_link . "'";
  286. $row = $this->adb->getRow($query);
  287. if( $row['count'] > 0 ) {
  288. return -1;
  289. } else {
  290. $query = "update " . $this->table . " set " . $this->prefix . "link = '" . $items_link . "' where " . $this->prefix . "id = '" . (int)$items_id . "'";
  291. $result = $this->adb->query($query);
  292. }
  293. return 1;
  294. }
  295. function deleteItem($items_id) {
  296. $query = "delete " . $this->table . ", " . $this->table_description . " from " . $this->table . " a, " . $this->table_description . " ad "
  297. . " where a." . $this->prefix . "id = '" . (int)$items_id . "' "
  298. . " and a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "'";
  299. return $this->adb->delete($query);
  300. }
  301. //private
  302. function _generate_item_link($items_id, $items_name = '', $items_link = '') {
  303. if( $items_link == '' && $items_name != '' ) {
  304. $items_link = $this->_filter_link($items_name);
  305. }
  306. if( $this->_not_null($items_link) ) {
  307. $items_link = func_db_input($items_link);
  308. $query = "select count(*) as count from " . $this->table . " where " . $this->prefix . "id != '" . (int)$items_id . "' "
  309. . " and " . $this->prefix . "link = '" . $items_link . "'";
  310. $count = $this->adb->getCount($query);
  311. if( $count == 0 ) {
  312. $this->updateItem($items_id, array($this->prefix . 'link' => $items_link));
  313. return true;
  314. }
  315. }
  316. $items_link = $this->_filter_link($items_name);
  317. $items_link = $this->_rand_link($items_id, $items_link);
  318. $this->_generate_item_link($items_id, $items_name, $items_link);
  319. return true;
  320. }
  321. function _generate_item_link_lang($items_id) {
  322. $item = $this->getItem($items_id);
  323. $item_link = $item[$this->prefix . 'link'];
  324. if( $this->_not_null($item_link) ) {
  325. $query = "select count(*) as count from " . $this->table . " a, " . $this->table_description . " ad "
  326. . " where a." . $this->prefix . "id = ad." . $this->prefix . "id and ad.languages_id = '" . (int)$this->languages_id . "' "
  327. . " and a." . $this->prefix . "id != '" . $items_id . "' and a." . $this->prefix . "link = '" . $item_link . "'";
  328. $count = $this->adb->getCount($query);
  329. if( $count == 0 ) {
  330. $this->updateItem($items_id, array($this->prefix . 'link' => $items_link));
  331. return false;
  332. }
  333. }
  334. $items_link = $this->_filter_link($item[$this->prefix . 'subject']);
  335. $items_link = $this->_rand_link($items_id, $items_link);
  336. $this->updateItem($items_id, array($this->prefix . 'link' => $items_link));
  337. $this->_generate_item_link($items_id);
  338. return true;
  339. }
  340. }
  341. //
  342. ?>