PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://php-ppcms.googlecode.com/
PHP | 449 lines | 395 code | 25 blank | 29 comment | 136 complexity | 07175721bdf584e46b9304a274398f4e MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  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. //
  9. include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');
  10. class BaseItemItemsBase extends BaseItemBase {
  11. var $join_key = 'id';
  12. var $relation_key = 'id';
  13. //join
  14. var $table_join = '';
  15. var $table_join_key = '';
  16. var $table_join_params = '';
  17. var $table_join_fields = array();
  18. //relation
  19. var $table_relation = '';
  20. var $table_relation_key = '';
  21. var $table_relation_params = '';
  22. var $table_relation_fields = array();
  23. //joins
  24. // table, key, fkey, params, fields
  25. //
  26. var $joins = array();
  27. //relations
  28. // table, key, fkey, params, fields
  29. // params
  30. // 2 =
  31. // 3 key
  32. //
  33. var $relations = array();
  34. //constructor
  35. function BaseItemItemsBase() {
  36. parent::BaseItemBase();
  37. }
  38. //methods
  39. function getItems($filter = '', $check = 0, $limit = true) {
  40. $query_field_str = '*';
  41. if( $this->list_fields == 'ALL' ) {
  42. $query_field_str = "*";
  43. } elseif( $this->list_fields == '*' ) {
  44. $query_field_str = "a.*";
  45. } elseif( is_array($this->list_fields) && sizeof($this->list_fields) > 0 ) {
  46. $query_field_str = implode(', a.', $this->list_fields);
  47. }
  48. $query_str = "";
  49. $query_str .= $this->_get_query_str($filter, $check);
  50. $query_order_str = "";
  51. if( $this->sort_order == true ) {
  52. if( is_array($this->sort_fields) ) {
  53. $query_order_str = " order by ";
  54. $i = 0;
  55. foreach($this->sort_fields as $k => $v) {
  56. if( substr($v, 0, 1) == '=' ) {
  57. $v = substr($v, 1);
  58. } else {
  59. $v = $this->prefix . $v;
  60. }
  61. if( substr($v, -1) == '-' ) {
  62. $as = 'desc';
  63. $v = substr($v, 0, -1);
  64. } elseif( substr($v, -1) == '+' ) {
  65. $as = 'asc';
  66. $v = substr($v, 0, -1);
  67. } else {
  68. $as = 'asc';
  69. }
  70. if( $i > 0 ) {
  71. $query_order_str .= ", ";
  72. }
  73. $query_order_str .= " a." . $v . " " . $as;
  74. $i++;
  75. }
  76. } else {
  77. $query_order_str = " order by a." . $this->prefix . "sort_order asc";
  78. }
  79. }
  80. $query_limit_str = "";
  81. if( $limit == true ) {
  82. $query_limit_str = $this->getQueryLimitStr();
  83. }
  84. //
  85. $query = "select " . $query_field_str . " ";
  86. if( $this->table_join != '' ) {
  87. if( $this->table_join_fields == '*' ) {
  88. $query .= ", b.*";
  89. } elseif( is_array($this->table_join_fields) ) {
  90. foreach($this->table_join_fields as $k => $v) {
  91. $query .= ", b." . $v;
  92. }
  93. }
  94. }
  95. if( $this->table_relation != '' ) {
  96. if( $this->table_relation_fields == '*' ) {
  97. $query .= ", c.*";
  98. } elseif( is_array($this->table_relation_fields) ) {
  99. foreach($this->table_relation_fields as $k => $v) {
  100. $query .= ", c." . $v;
  101. }
  102. }
  103. }
  104. if( is_array($this->joins) ) {
  105. foreach($this->joins as $jk => $join) {
  106. if( isset($join['fields']) ) {
  107. if( $join['fields'] == '*' ) {
  108. $query .= ", tj.*";
  109. } elseif( is_array($join['fields']) ) {
  110. foreach($join['fields'] as $k => $v) {
  111. $query .= ", tj" . $jk . "." . $v;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. if( is_array($this->relations) ) {
  118. foreach($this->relations as $rk => $relation) {
  119. if( isset($relation['fields']) ) {
  120. if( $relation['fields'] == '*' ) {
  121. $query .= ", tr.*";
  122. } elseif( is_array($relation['fields']) ) {
  123. foreach($relation['fields'] as $k => $v) {
  124. $query .= ", tr" . $rk . "." . $v;
  125. }
  126. }
  127. }
  128. }
  129. }
  130. //
  131. $query .= " from " . $this->table . " a ";
  132. if( $this->table_join != '' ) {
  133. $query .= " left join " . $this->table_join . " b " . " on a." . $this->join_key . " = b." . $this->table_join_key . " ";
  134. if( is_array($this->table_join_params) ) {
  135. foreach($this->table_join_params as $k => $v) {
  136. $query_str .= " and b." . $k . " = '" . $v . "'";
  137. }
  138. }
  139. }
  140. if( is_array($this->joins) ) {
  141. foreach($this->joins as $jk => $join) {
  142. $query .= " left join " . $join['table'] . " tj" . $jk . " on a." . $this->prefix . $join['fkey'] . " = tj" . $jk . "." . $join['key'] . " ";
  143. if( $join['params'] != '' ) {
  144. if( is_array($join['params']) ) {
  145. foreach($join['params'] as $k => $v) {
  146. $query .= " and tj" . $jk . "." . $k . " = '" . $v . "' ";
  147. }
  148. }
  149. }
  150. }
  151. }
  152. if( $this->table_relation != '' ) {
  153. $query .= ", " . $this->table_relation . " c ";
  154. }
  155. if( is_array($this->relations) ) {
  156. foreach($this->relations as $rk => $relation) {
  157. $query .= ", " . $relation['table'] . " tr" . $rk . " ";
  158. }
  159. }
  160. $query .= " where " . $this->_query_owner_rt('a')
  161. . $query_str . $query_order_str . $query_limit_str;
  162. $rows = $this->adb->getRows($query);
  163. //
  164. if( $this->format == true ) {
  165. for($i=0, $n=sizeof($rows); $i<$n; $i++) {
  166. $rows[$i] = $this->_post_field($rows[$i], '', $this->prefix, $this->format_check);
  167. }
  168. }
  169. return $rows;
  170. }
  171. function getItemsTotal($filter = '', $check = 0) {
  172. $query_str = "";
  173. $query_str .= $this->_get_query_str($filter, $check);
  174. //
  175. $query = "select distinct a." . $this->prefix . "id from " . $this->table . " a ";
  176. if( $this->table_join != '' ) {
  177. $query .= " left join " . $this->table_join . " b " . " on a." . $this->join_key . " = b." . $this->table_join_key . " ";
  178. if( is_array($this->table_join_params) ) {
  179. foreach($this->table_join_params as $k => $v) {
  180. $query_str .= " and b." . $k . " = '" . $v . "'";
  181. }
  182. }
  183. }
  184. if( is_array($this->joins) ) {
  185. foreach($this->joins as $jk => $join) {
  186. $query .= " left join " . $join['table'] . " tj" . $jk . " on a." . $this->prefix . $join['fkey'] . " = tj" . $jk . "." . $join['key'] . " ";
  187. if( $join['params'] != '' ) {
  188. if( is_array($join['params']) ) {
  189. foreach($join['params'] as $k => $v) {
  190. $query .= " and tj" . $jk . "." . $k . " = '" . $v . "' ";
  191. }
  192. }
  193. }
  194. }
  195. }
  196. if( $this->table_relation != '' ) {
  197. $query .= ", " . $this->table_relation . " c ";
  198. }
  199. if( is_array($this->relations) ) {
  200. foreach($this->relations as $rk => $relation) {
  201. $query .= ", " . $relation['table'] . " tr" . $rk . " ";
  202. }
  203. }
  204. $query .= " where " . $this->_query_owner_rt('a')
  205. . $query_str;
  206. return $this->adb->getCount($query);
  207. }
  208. //private
  209. function _get_query_str($filter = '', $check = 0) {
  210. $query_str = "";
  211. if( $check == -1 ) {
  212. $query_str .= "";
  213. } elseif( $this->_table_status_usable == true ) {
  214. $query_str .= " and a." . $this->prefix . "status = 1";
  215. }
  216. if( is_array($filter) ) {
  217. $filter = func_db_input($filter);
  218. if( is_array($this->filter_query) ) {
  219. $filter = array_merge($filter, $this->filter_query);
  220. }
  221. if( is_array($this->filters) ) {
  222. foreach($this->filters as $k => $v) {
  223. $filter[$k] = $v;
  224. }
  225. }
  226. //
  227. if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
  228. if( is_array($this->search_fields) ) {
  229. if( $this->search_type != 'or' ) {
  230. $this->search_type = 'and';
  231. }
  232. $i = 0;
  233. $query_str .= " and ( ";
  234. foreach($this->search_fields as $k => $field) {
  235. if( $i > 0 ) {
  236. $query_str .= " " . $this->search_type . " ";
  237. }
  238. $query_str .= " a." . $this->prefix . $field . " like '%" . $filter['keyword'] . "%' ";
  239. $i++;
  240. }
  241. $query_str .= " ) ";
  242. } elseif( is_string($this->search_fields) ) {
  243. $query_str .= " and a." . $this->prefix . $this->search_fields . " like '%" . $filter['keyword'] . "%' ";
  244. }
  245. }
  246. if( isset($filter['ids']) && $this->_not_null($filter['ids']) == true ) {
  247. if( is_array($filter['ids']) ) {
  248. $query_str .= " and a." . $this->prefix . "id in (" . $this->_implode(', ', $filter['ids']) . ") ";
  249. } else {
  250. $query_str .= " and a." . $this->prefix . "id = '" . (int)$filter['ids'] . "' ";
  251. }
  252. }
  253. if( isset($filter['category']) ) {
  254. if( is_array($filter['category']) ) {
  255. $query_str .= " and a.categories_id in (" . $this->_implode(', ', $filter['category']) . ") ";
  256. } else {
  257. $query_str .= " and a.categories_id = '" . (int)$filter['category'] . "' ";
  258. }
  259. }
  260. if( is_array($this->filter_fields) && sizeof($this->filter_fields) > 0 ) {
  261. $qt = $this->_get_query_type();
  262. foreach($filter as $k => $v) {
  263. if( is_array($this->filter_map) && isset($this->filter_map[$k]) ) {
  264. $k = $this->filter_map[$k];
  265. }
  266. if( in_array($k, $this->filter_fields) ) {
  267. $t = '';
  268. $p = 'a.' . $this->prefix;
  269. if( strpos($k, ':') !== false ) {
  270. $ks = explode(':', $k);
  271. $k = $ks['0'];
  272. $t = $ks['1'];
  273. if( sizeof($ks) > 2 ) {
  274. $p = $ks['2'];
  275. }
  276. }
  277. if( substr($k, 0, 1) == '=' ) {
  278. $k = substr($k, 1);
  279. $p = 'a.';
  280. }
  281. if( !in_array($t, $qt) ) {
  282. $t = '';
  283. }
  284. if( in_array($t, array('=', '>', '>=', '<', '<=', '!=')) ) {
  285. if( $v === 'now()' || $v === 'now' ) {
  286. $query_str .= " and " . $p . $k . " " . $t . " now() ";
  287. } else {
  288. $query_str .= " and " . $p . $k . " " . $t . " '" . $v . "' ";
  289. }
  290. } elseif( in_array($t, array('[]', '[)', '(]', '()')) ) {
  291. if( is_array($v) ) {
  292. $t1 = substr($t, 0, 1);
  293. $t2 = substr($t, 1, 1);
  294. if( $t1 == '[' ) {
  295. $t1 = '<=';
  296. } elseif( $t1 == '(' ) {
  297. $t1 = '<';
  298. }
  299. if( $t2 == '[' ) {
  300. $t2 = '<=';
  301. } elseif( $t1 == '(' ) {
  302. $t2 = '<';
  303. }
  304. $query_str .= " and " . $p . $k . " " . $t1 . " '" . $v['0'] . "' ";
  305. $query_str .= " and " . $p . $k . " " . $t2 . " '" . $v['1'] . "' ";
  306. }
  307. } elseif( in_array($t, array('like', 'notlike', '%like%', '%like', 'like%',)) ) {
  308. if( $t == 'notlike' ) {
  309. $query_str .= " and " . $p . $k . " not like '%" . $v . "%' ";
  310. } else {
  311. $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
  312. }
  313. } else {
  314. if( $v === 'now()' || $v === 'now' ) {
  315. $query_str .= " and " . $p . $k . " < now() ";
  316. } elseif( is_array($v) && sizeof($v) > 0 ) {
  317. $query_str .= " and " . $p . $k . " in ('" . implode("','", $v) . "') ";
  318. } elseif( is_int($v) ) {
  319. $query_str .= " and " . $p . $k . " = '" . $v . "' ";
  320. } elseif( is_numeric($v) ) {
  321. $query_str .= " and " . $p . $k . " = '" . $v . "' ";
  322. } elseif( is_string($v) ) {
  323. $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
  324. } else {
  325. $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
  326. }
  327. }
  328. }
  329. }
  330. }
  331. }
  332. //
  333. $query_str .= $this->_get_query_str_relation($filter, $check);
  334. //
  335. $query_str .= $this->_get_query_str_custom($filter, $check);
  336. return $query_str;
  337. }
  338. function _get_query_str_relation($filter = '', $check = 0) {
  339. $query_str = "";
  340. if( $this->table_relation != '' ) {
  341. $query_str .= " and a." . $this->relation_key . " = c." . $this->table_relation_key;
  342. if( is_array($this->table_relation_params) ) {
  343. foreach($this->table_relation_params as $k => $v) {
  344. $query_str .= " and c." . $k . " = '" . $v . "'";
  345. }
  346. }
  347. }
  348. if( is_array($this->relations) ) {
  349. $qt = $this->_get_query_type();
  350. foreach($this->relations as $rk => $relation) {
  351. $query .= " and a." . $relation['fkey'] . " = tr" . $rk . "." . $relation['key'] . " ";
  352. if( $relation['params'] != '' ) {
  353. if( is_array($relation['params']) ) {
  354. foreach($relation['params'] as $k => $v) {
  355. $t = '';
  356. $p = "tr" . $rk . ".";
  357. if( strpos($k, ':') !== false ) {
  358. $ks = explode(':', $k);
  359. $k = $ks['0'];
  360. $t = $ks['1'];
  361. if( sizeof($ks) > 2 ) {
  362. $p = $ks['2'];
  363. }
  364. }
  365. if( !in_array($t, $qt) ) {
  366. $t = '';
  367. }
  368. if( in_array($t, array('=', '>', '>=', '<', '<=', '!=')) ) {
  369. if( $v === 'now()' || $v === 'now' ) {
  370. $query_str .= " and " . $p . $k . " " . $t . " now() ";
  371. } else {
  372. $query_str .= " and " . $p . $k . " " . $t . " '" . $v . "' ";
  373. }
  374. } elseif( in_array($t, array('[]', '[)', '(]', '()')) ) {
  375. if( is_array($v) ) {
  376. $t1 = substr($t, 0, 1);
  377. $t2 = substr($t, 1, 1);
  378. if( $t1 == '[' ) {
  379. $t1 = '<=';
  380. } elseif( $t1 == '(' ) {
  381. $t1 = '<';
  382. }
  383. if( $t2 == '[' ) {
  384. $t2 = '<=';
  385. } elseif( $t1 == '(' ) {
  386. $t2 = '<';
  387. }
  388. $query_str .= " and " . $p . $k . " " . $t1 . " '" . $v['0'] . "' ";
  389. $query_str .= " and " . $p . $k . " " . $t2 . " '" . $v['1'] . "' ";
  390. }
  391. } elseif( in_array($t, array('like', 'notlike', '%like%', '%like', 'like%',)) ) {
  392. if( $t == 'notlike' ) {
  393. $query_str .= " and " . $p . $k . " not like '%" . $v . "%' ";
  394. } else {
  395. $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
  396. }
  397. } else {
  398. if( $v === 'now()' || $v === 'now' ) {
  399. $query_str .= " and " . $p . $k . " < now() ";
  400. } elseif( is_array($v) && sizeof($v) > 0 ) {
  401. $query_str .= " and " . $p . $k . " in ('" . implode("','", $v) . "') ";
  402. } elseif( is_int($v) ) {
  403. $query_str .= " and " . $p . $k . " = '" . $v . "' ";
  404. } elseif( is_numeric($v) ) {
  405. $query_str .= " and " . $p . $k . " = '" . $v . "' ";
  406. } elseif( is_string($v) ) {
  407. $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
  408. } else {
  409. $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
  410. }
  411. }
  412. }
  413. }
  414. }
  415. }
  416. }
  417. return $query_str;
  418. }
  419. function _get_query_str_custom($filter = '', $check = 0) {
  420. return "";
  421. }
  422. }
  423. //
  424. ?>