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

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