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

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