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

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