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

http://php-ppcms.googlecode.com/ · PHP · 509 lines · 424 code · 66 blank · 19 comment · 139 complexity · 549715628d560dbbbc6fbb0f3b080b47 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 BaseItemJoinBase 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 $filter_fields = array();
  29. var $filter_map = array();
  30. var $filter_query = array();
  31. //
  32. var $table_join = '';
  33. var $table_join_key = '';
  34. //constructor
  35. function BaseItemJoinBase($table = '') {
  36. parent::Base();
  37. if( $table != '' ) {
  38. $this->table = $table;
  39. }
  40. }
  41. function setProperty($name, $value = '') {
  42. if( is_array($name) ) {
  43. foreach($name as $k => $v) {
  44. $this->$k = $v;
  45. }
  46. } else {
  47. $this->$name = $value;
  48. }
  49. }
  50. function getProperty($name) {
  51. if( isset($this->$name) ) {
  52. return $this->$name;
  53. }
  54. return NULL;
  55. }
  56. function property($data) {
  57. if( is_array($data) ) {
  58. return $this->setProperty($data);
  59. } else {
  60. return $this->getProperty($data);
  61. }
  62. }
  63. function _pk() {
  64. return $this->prefix . $this->pkey;
  65. }
  66. function _pf($field) {
  67. return $this->prefix . $field;
  68. }
  69. function setFilterQuery($name, $value = '') {
  70. if( is_array($name) ) {
  71. $this->filter_query = $name;
  72. } else {
  73. $this->filter_query[$name] = $value;
  74. }
  75. }
  76. //methods
  77. function getItems($filter = '', $check = 0, $limit = true) {
  78. $query_str = "";
  79. $query_str .= $this->_get_query_str($filter, $check);
  80. $query_order_str = "";
  81. if( $this->sort_order == true ) {
  82. if( is_array($this->sort_fields) ) {
  83. $query_order_str = " order by ";
  84. $i = 0;
  85. foreach($this->sort_fields as $k => $v) {
  86. if( substr($v, 0, 1) == '=' ) {
  87. $v = substr($v, 1);
  88. } else {
  89. $v = $this->prefix . $v;
  90. }
  91. if( substr($v, -1) == '-' ) {
  92. $as = 'desc';
  93. $v = substr($v, 0, -1);
  94. } elseif( substr($v, -1) == '+' ) {
  95. $as = 'asc';
  96. $v = substr($v, 0, -1);
  97. } else {
  98. $as = 'asc';
  99. }
  100. if( $i > 0 ) {
  101. $query_order_str .= ", ";
  102. }
  103. $query_order_str .= " " . $v . " " . $as;
  104. $i++;
  105. }
  106. } else {
  107. $query_order_str = " order by " . $this->prefix . "sort_order asc";
  108. }
  109. }
  110. $query_limit_str = "";
  111. if( $limit == true ) {
  112. $query_limit_str = $this->getQueryLimitStr();
  113. }
  114. $query = "select * from " . $this->table . " a left join " . $this->table_join . " b "
  115. . " on a." . $this->prefix . "id = b." . $this->table_join_key . " "
  116. . " where " . $this->_query_owner_rt('a')
  117. . $query_str . $query_order_str . $query_limit_str;
  118. $rows = $this->adb->getRows($query);
  119. if( $this->format == true ) {
  120. for($i=0, $n=sizeof($rows); $i<$n; $i++) {
  121. $rows[$i] = $this->_post_field($rows[$i], '', $this->prefix, $this->format_check);
  122. }
  123. }
  124. return $rows;
  125. }
  126. function getItemsTotal($filter = '', $check = 0) {
  127. $query_str = "";
  128. $query_str .= $this->_get_query_str($filter, $check);
  129. $query = "select count(*) as count from " . $this->table . " a left join " . $this->table_join . " b "
  130. . " on a." . $this->prefix . "id = b." . $this->table_join_key . " "
  131. . " where " . $this->_query_owner_rt('a')
  132. . $query_str;
  133. return $this->adb->getCount($query);
  134. }
  135. function getItemsFormat($filter = '', $check = 0, $limit = true, $field = '') {
  136. $items = $this->getItems($filter, $check, $limit);
  137. $pf = $this->prefix;
  138. if( $this->format == true ) {
  139. $pf = '';
  140. }
  141. $itemsf = array();
  142. for($i=0, $n=sizeof($items); $i<$n; $i++) {
  143. $f1 = $pf . 'id';
  144. $f2 = $pf . 'id';
  145. $f3 = 0;
  146. if( is_array($field) && sizeof($field) >= 2 ) {
  147. $f1 = $field['0'];
  148. $f2 = $field['1'];
  149. if( substr($f1, 0, 1) == '=' ) {
  150. $f1 = substr($f1, 1);
  151. } elseif( substr($f1, 0, 1) == '_' ) {
  152. $f1 = $pf . substr($f1, 1);
  153. } else {
  154. $f1 = $pf . $f1;
  155. }
  156. if( substr($f2, 0, 1) == '=' ) {
  157. $f2 = substr($f2, 1);
  158. } elseif( substr($f2, 0, 1) == '_' ) {
  159. $f2 = $pf . substr($f2, 1);
  160. } else {
  161. $f2 = $pf . $f2;
  162. }
  163. } elseif( $field != '' ) {
  164. $f2 = $field;
  165. if( substr($f2, 0, 1) == '=' ) {
  166. $f2 = substr($f2, 1);
  167. } elseif( substr($f2, 0, 1) == '_' ) {
  168. $f2 = $pf . substr($f2, 1);
  169. } else {
  170. $f2 = $pf . $f2;
  171. }
  172. } elseif( isset($items[$i][$pf . 'name']) && strlen($items[$i][$pf . 'name']) > 0 ) {
  173. $f2 = $pf . 'name';
  174. } elseif( isset($items[$i][$pf . 'subject']) && strlen($items[$i][$pf . 'subject']) > 0 ) {
  175. $f2 = $pf . 'subject';
  176. }
  177. $itemsf[$items[$i][$f1]] = $items[$i][$f2];
  178. }
  179. return $itemsf;
  180. }
  181. //
  182. function checkItem($value, $type = 'id') {
  183. $value = func_db_input($value);
  184. if( is_array($this->item_fields) && sizeof($this->item_fields) > 0 ) {
  185. if( isset($this->item_fields[$type]) ) {
  186. $field = $this->item_fields[$type];
  187. if( substr($field, 0, 1) != '_' && substr($field, 0, 1) != '=' ) {
  188. $field = '_' . $field;
  189. }
  190. } elseif( in_array($type, $this->item_fields) ) {
  191. $field = '_' . $type;
  192. }
  193. }
  194. if( substr($type, 0, 1) == '_' || substr($type, 0, 1) == '=' ) {
  195. $field = $type;
  196. } elseif( in_array($type, array('id', 'key', 'link', 'name', 'subject', 'value', 'code')) ) {
  197. $field = '_' . $type;
  198. if( $type == 'id' ) {
  199. $value = intval($value);
  200. }
  201. } else {
  202. return false;
  203. }
  204. if( substr($field, 0, 1) == '_' ) {
  205. $field = $this->prefix . substr($field, 1);
  206. } elseif( substr($field, 0, 1) == '=' ) {
  207. $field = substr($field, 1);
  208. } else {
  209. $field = $this->prefix . $field;
  210. }
  211. $query = "select count(*) as count from " . $this->table . " where " . $this->_query_owner_rt()
  212. . " and " . $field . " = '" . $value . "'";
  213. return $this->adb->checkCount($query);
  214. }
  215. function getItem($value, $type = 'id') {
  216. $value = func_db_input($value);
  217. $this->doEvents("getItemBefore", $value, $type, $check);
  218. if( $check == -1 ) {
  219. //
  220. } elseif( $this->_table_status_usable == true ) {
  221. $query_str .= " and " . $this->prefix . "status = 1";
  222. }
  223. if( is_array($this->item_fields) && sizeof($this->item_fields) > 0 ) {
  224. if( isset($this->item_fields[$type]) ) {
  225. $field = $this->item_fields[$type];
  226. if( substr($field, 0, 1) != '_' && substr($field, 0, 1) != '=' ) {
  227. $field = '_' . $field;
  228. }
  229. } elseif( in_array($type, $this->item_fields) ) {
  230. $field = '_' . $type;
  231. }
  232. }
  233. if( substr($type, 0, 1) == '_' || substr($type, 0, 1) == '=' ) {
  234. $field = $type;
  235. } elseif( in_array($type, array('id', 'key', 'link', 'name', 'subject', 'value', 'code')) ) {
  236. $field = '_' . $type;
  237. if( $type == 'id' ) {
  238. $value = intval($value);
  239. }
  240. } else {
  241. return false;
  242. }
  243. if( substr($field, 0, 1) == '_' ) {
  244. $field = $this->prefix . substr($field, 1);
  245. } elseif( substr($field, 0, 1) == '=' ) {
  246. $field = substr($field, 1);
  247. } else {
  248. $field = $this->prefix . $field;
  249. }
  250. $query_str .= " and " . $field . " = '" . $value . "'";
  251. $query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " " . $query_str;
  252. $item = $this->adb->getRow($query);
  253. if( $this->format == true ) {
  254. $item = $this->_post_field($item, '', $this->prefix, $this->format_check);
  255. }
  256. return $item;
  257. }
  258. function addItem($data_array) {
  259. if( !$this->_not_null($data_array) || !is_array($data_array) ) {
  260. return false;
  261. }
  262. $data_array = func_db_input($data_array);
  263. $data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
  264. if( $this->_table_owner_usable == true ) {
  265. if( $this->_table_owner_prefix == '=' ) {
  266. $p = '';
  267. } elseif( $this->_table_owner_prefix == '+' ) {
  268. $p = $this->prefix;
  269. } elseif( $this->_table_owner_prefix == '' ) {
  270. $p = $this->prefix;
  271. } else {
  272. $p = $this->_table_owner_prefix;
  273. }
  274. $data_array[$p . $this->_table_owner_field] = $this->owner;
  275. }
  276. $data_array[$this->prefix . 'date_added'] = 'now()';
  277. $data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
  278. $data_array[$this->prefix . 'status'] = '1';
  279. $items_id = $this->adb->insertA($this->table, $data_array);
  280. return $items_id;
  281. }
  282. function addItems($data_array) {
  283. $items_ids = array();
  284. if( is_array($data_array) ) {
  285. foreach($data_array as $k => $v) {
  286. $items_ids[] = $this->addItem($v);
  287. }
  288. }
  289. return $items_ids;
  290. }
  291. function updateItem($items_id, $data_array) {
  292. if( !$this->_not_null($items_id) || !$this->_not_null($data_array) || !is_array($data_array) ) {
  293. return false;
  294. }
  295. $data_array = func_db_input($data_array);
  296. $data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
  297. $data_array[$this->prefix . 'last_modified'] = 'now()';
  298. $data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
  299. return $this->adb->updateA($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
  300. }
  301. function updateItems($items_ids, $data_array) {
  302. if( is_array($items_ids) ) {
  303. if( sizeof($items_ids) > 30 ) {
  304. foreach($items_ids as $k => $items_id) {
  305. $this->_update($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
  306. }
  307. } else {
  308. $query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
  309. $this->_update($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $this->prefix . "id in (" . $query_ids . ")");
  310. }
  311. }
  312. }
  313. function deleteItem($items_id) {
  314. if( !$this->_not_null($items_id) ) {
  315. return false;
  316. }
  317. $this->adb->deleteA($this->table, " " . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
  318. return true;
  319. }
  320. function deleteItems($items_ids) {
  321. if( is_array($items_ids) ) {
  322. $this->_delete_ins($this->table, $this->prefix . 'id', $items_ids);
  323. }
  324. }
  325. function setItemsStatus($items_ids, $status = 1) {
  326. if( is_array($items_ids) ) {
  327. $this->_update_ins($this->table, "" . $this->prefix . "status = '" . (int)$status . "'", $this->prefix . 'id', $items_ids);
  328. }
  329. }
  330. //
  331. function check($items_id) {
  332. $count = $this->adb->getCountA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
  333. return ($count > 0) ? true : false;
  334. }
  335. function get($items_id) {
  336. return $this->adb->getRowA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
  337. }
  338. function add($data_array) {
  339. if( !$this->_not_null($data_array) || !is_array($data_array) ) {
  340. return false;
  341. }
  342. $data_array = func_db_input($data_array);
  343. $data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
  344. $data_array[$this->prefix . 'date_added'] = 'now()';
  345. $data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
  346. $data_array[$this->prefix . 'status'] = '1';
  347. return $this->adb->insertA($this->table, $data_array);
  348. }
  349. function update($items_id, $data_array) {
  350. if( !$this->_not_null($items_id) ) {
  351. return false;
  352. }
  353. $data_array = func_db_input($data_array);
  354. $data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
  355. $data_array[$this->prefix . 'last_modified'] = 'now()';
  356. $data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
  357. return $this->adb->updateA($this->table, $data_array, $this->prefix . "id = '" . (int)$items_id . "'");
  358. }
  359. function delete($items_id) {
  360. return $this->adb->deleteA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
  361. }
  362. //target
  363. function addTargetItem($target_id, $data_array) {
  364. if( $this->table_join_key != '' && $this->table_join != '' ) {
  365. $data_array[$this->table_join_key] = $target_id;
  366. }
  367. return $this->addItem($data_array);
  368. }
  369. //private
  370. function _get_query_str($filter = '', $check = 0) {
  371. $query_str = "";
  372. if( $check == -1 ) {
  373. $query_str .= "";
  374. } elseif( $this->_table_status_usable == true ) {
  375. $query_str .= " and " . $this->prefix . "status = 1";
  376. }
  377. if( is_array($filter) ) {
  378. $filter = func_db_input($filter);
  379. if( is_array($this->filter_query) ) {
  380. $filter = array_merge($filter, $this->filter_query);
  381. }
  382. //
  383. if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
  384. if( is_array($this->search_fields) ) {
  385. if( $this->search_type != 'or' ) {
  386. $this->search_type = 'and';
  387. }
  388. $i = 0;
  389. $query_str .= " and ( ";
  390. foreach($this->search_fields as $k => $field) {
  391. if( $i > 0 ) {
  392. $query_str .= " " . $this->search_type . " ";
  393. }
  394. $query_str .= " " . $this->prefix . $field . " like '%" . $filter['keyword'] . "%' ";
  395. $i++;
  396. }
  397. $query_str .= " ) ";
  398. } elseif( is_string($this->search_fields) ) {
  399. $query_str .= " and " . $this->prefix . $this->search_fields . " like '%" . $filter['keyword'] . "%' ";
  400. }
  401. }
  402. if( isset($filter['ids']) && $this->_not_null($filter['ids']) == true ) {
  403. if( is_array($filter['ids']) ) {
  404. $query_str .= " and " . $this->prefix . "id in (" . $this->_implode(', ', $filter['ids']) . ") ";
  405. } else {
  406. $query_str .= " and " . $this->prefix . "id = '" . (int)$filter['ids'] . "' ";
  407. }
  408. }
  409. if( isset($filter['category']) ) {
  410. if( is_array($filter['category']) ) {
  411. $query_str .= " and categories_id in (" . $this->_implode(', ', $filter['category']) . ") ";
  412. } else {
  413. $query_str .= " and categories_id = '" . (int)$filter['category'] . "' ";
  414. }
  415. }
  416. if( is_array($this->filter_fields) && sizeof($this->filter_fields) > 0 ) {
  417. foreach($filter as $k => $v) {
  418. if( in_array($k, $this->filter_fields) ) {
  419. if( is_array($v) && sizeof($v) > 0 ) {
  420. $query_str .= " and " . $this->prefix . $k . " in ('" . implode("','", $v) . "') ";
  421. } elseif( is_int($v) ) {
  422. $query_str .= " and " . $this->prefix . $k . " = '" . $v . "' ";
  423. } elseif( is_numeric($v) ) {
  424. $query_str .= " and " . $this->prefix . $k . " = '" . $v . "' ";
  425. } elseif( is_string($v) ) {
  426. $query_str .= " and " . $this->prefix . $k . " like '%" . $v . "%' ";
  427. } else {
  428. $query_str .= " and " . $this->prefix . $k . " like '%" . $v . "%' ";
  429. }
  430. }
  431. }
  432. }
  433. }
  434. //
  435. $query_str .= $this->_get_query_str_custom($filter, $check);
  436. return $query_str;
  437. }
  438. function _get_query_str_custom($filter = '', $check = 0) {
  439. return "";
  440. }
  441. }
  442. //
  443. ?>