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

http://php-ppcms.googlecode.com/ · PHP · 1129 lines · 955 code · 134 blank · 40 comment · 330 complexity · dbe140563ca13f5bcb3b27f25e6efd80 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 BaseItemBase extends Base {
  9. var $table = '';
  10. var $prefix = '';
  11. var $pkey = 'id';
  12. var $fkey = '';
  13. //
  14. var $_table_owner_field = 'owner';
  15. var $_table_owner_prefix = '';
  16. var $_table_owner_usable = true;
  17. var $_table_status_usable = true;
  18. var $_table_status_value = '1';
  19. var $_table_link_usable = false;
  20. var $_table_link_gfield = 'name';
  21. //
  22. var $format = true;
  23. var $format_check = false;
  24. var $format_prefix = 'field_';
  25. var $item_link = false;
  26. var $item_link_owner = false;
  27. var $item_fields = array('id', 'name', 'link');
  28. var $list_fields = array();
  29. var $search_fields = array('name');
  30. var $search_type = 'or';
  31. var $sort_order = false;
  32. var $sort_fields = array('sort_order+', 'id+');
  33. var $sort_group = false;
  34. var $sort_groups = array('name');
  35. var $sort_query = array();
  36. var $filter_fields = array();
  37. var $filter_map = array();
  38. var $filter_query = array();
  39. //
  40. var $filters = array();
  41. var $triggers = array();
  42. //constructor
  43. function BaseItemBase($table = '') {
  44. parent::Base();
  45. if( $table != '' ) {
  46. $this->table = $table;
  47. }
  48. }
  49. function setProperty($name, $value = '') {
  50. if( is_array($name) ) {
  51. foreach($name as $k => $v) {
  52. $this->$k = $v;
  53. }
  54. } else {
  55. $this->$name = $value;
  56. }
  57. }
  58. function getProperty($name) {
  59. if( isset($this->$name) ) {
  60. return $this->$name;
  61. }
  62. return NULL;
  63. }
  64. function property($data) {
  65. if( is_array($data) ) {
  66. return $this->setProperty($data);
  67. } else {
  68. return $this->getProperty($data);
  69. }
  70. }
  71. function _pk($check = false) {
  72. if( $check == true && $this->format == true ) {
  73. return $this->pkey;
  74. }
  75. return $this->prefix . $this->pkey;
  76. }
  77. function _pf($field, $check = false) {
  78. if( $check == true && $this->format == true ) {
  79. return $field;
  80. }
  81. return $this->prefix . $field;
  82. }
  83. function setFilterQuery($name, $value = '') {
  84. if( is_array($name) ) {
  85. $this->filter_query = $name;
  86. } else {
  87. $this->filter_query[$name] = $value;
  88. }
  89. }
  90. //methods
  91. function getItems($filter = '', $check = 0, $limit = true) {
  92. $query_field_str = '*';
  93. if( is_array($this->list_fields) && sizeof($this->list_fields) > 0 ) {
  94. $query_field_str = implode(', ', $this->list_fields);
  95. }
  96. $query_str = "";
  97. $query_str .= $this->_get_query_str($filter, $check);
  98. $query_group_str = "";
  99. if( $this->sort_group == true && !empty($this->sort_groups) ) {
  100. $query_group_str .= " group by ";
  101. foreach($this->sort_groups as $k => $v) {
  102. $query_group_str .= $k . ", ";
  103. }
  104. $query_group_str = substr($query_group_str, 0, -2);
  105. }
  106. $query_order_str = "";
  107. if( $this->sort_order == true ) {
  108. $this->_get_query_order_sort();
  109. if( is_array($this->sort_fields) ) {
  110. $query_order_str = " order by ";
  111. $i = 0;
  112. foreach($this->sort_fields as $k => $v) {
  113. if( substr($v, 0, 1) == '=' ) {
  114. $v = substr($v, 1);
  115. } else {
  116. $v = $this->prefix . $v;
  117. }
  118. if( substr($v, -1) == '-' ) {
  119. $as = 'desc';
  120. $v = substr($v, 0, -1);
  121. } elseif( substr($v, -1) == '+' ) {
  122. $as = 'asc';
  123. $v = substr($v, 0, -1);
  124. } else {
  125. $as = 'asc';
  126. }
  127. if( $i > 0 ) {
  128. $query_order_str .= ", ";
  129. }
  130. $query_order_str .= " " . $v . " " . $as;
  131. $i++;
  132. }
  133. } else {
  134. $query_order_str = " order by " . $this->prefix . "sort_order asc";
  135. }
  136. }
  137. $query_limit_str = "";
  138. if( $limit == true ) {
  139. $query_limit_str = $this->getQueryLimitStr();
  140. }
  141. $query = "select " . $query_field_str . " from " . $this->table . " where " . $this->_query_owner_rt() . " "
  142. . $query_str . $query_group_str . $query_order_str . $query_limit_str;
  143. $rows = $this->adb->getRows($query);
  144. if( $this->format == true ) {
  145. for($i=0, $n=sizeof($rows); $i<$n; $i++) {
  146. $rows[$i] = $this->_post_field($rows[$i], '', $this->prefix, $this->format_check);
  147. }
  148. }
  149. return $rows;
  150. }
  151. function getItemsTotal($filter = '', $check = 0) {
  152. $query_str = "";
  153. $query_str .= $this->_get_query_str($filter, $check);
  154. return $this->adb->getCountA($this->table, " " . $this->_query_owner_rt() . " " . $query_str . " ");
  155. }
  156. function getItemsStats($filter = '', $check = 0, $type = 'count', $field = '+id') {
  157. if( !in_array($type, array('count', 'sum', 'min', 'max', 'avg')) ) {
  158. return false;
  159. }
  160. $query_str = "";
  161. $query_str .= $this->_get_query_str($filter, $check);
  162. $query_group_str = "";
  163. if( $this->sort_group == true && !empty($this->sort_groups) ) {
  164. $query_group_str .= " group by ";
  165. foreach($this->sort_groups as $k => $v) {
  166. $query_group_str .= $k . ", ";
  167. }
  168. $query_group_str = substr($query_group_str, 0, -2);
  169. }
  170. if( $field == '' ) {
  171. $field = '*';
  172. } elseif( substr($field, 0, 1) == '=' ) {
  173. $field = substr($field, 1);
  174. } elseif( substr($field, 0, 1) == '+' ) {
  175. $field = $this->prefix . substr($field, 1);
  176. } elseif( substr($field, 0, 1) == '-' ) {
  177. $field = $this->prefix . substr($field, 1);
  178. } elseif( substr($field, 0, 1) == '_' ) {
  179. $field = $this->prefix . substr($field, 1);
  180. } else {
  181. $field = $this->prefix . substr($field, 1);
  182. }
  183. $query = "select " . $type . " (" . $field . ") as count from " . $this->table . " where " . $this->_query_owner_rt() . " "
  184. . $query_str . $query_group_str;
  185. return $this->adb->getCount($query);
  186. }
  187. function getItemsIDs($filter = '', $check = 0, $limit = true) {
  188. $items = $this->getItems($filter, $check, $limit);
  189. $pf = $this->prefix;
  190. if( $this->format == true ) {
  191. $pf = '';
  192. }
  193. $pk = $pf . 'id';
  194. $ids = array();
  195. for($i=0, $n=sizeof($items); $i<$n; $i++) {
  196. $ids[] = $items[$i][$pk];
  197. }
  198. return $ids;
  199. }
  200. function getItemsFormat($filter = '', $check = 0, $limit = true, $field = '') {
  201. $items = $this->getItems($filter, $check, $limit);
  202. $pf = $this->prefix;
  203. if( $this->format == true ) {
  204. $pf = '';
  205. }
  206. $itemsf = array();
  207. for($i=0, $n=sizeof($items); $i<$n; $i++) {
  208. $f1 = $pf . 'id';
  209. $f2 = $pf . 'id';
  210. $f3 = 0;
  211. if( is_array($field) && sizeof($field) >= 2 ) {
  212. $f1 = $field['0'];
  213. $f2 = $field['1'];
  214. if( substr($f1, 0, 1) == '=' ) {
  215. $f1 = substr($f1, 1);
  216. } elseif( substr($f1, 0, 1) == '_' ) {
  217. $f1 = $pf . substr($f1, 1);
  218. } else {
  219. $f1 = $pf . $f1;
  220. }
  221. if( substr($f2, 0, 1) == '=' ) {
  222. $f2 = substr($f2, 1);
  223. } elseif( substr($f2, 0, 1) == '_' ) {
  224. $f2 = $pf . substr($f2, 1);
  225. } else {
  226. $f2 = $pf . $f2;
  227. }
  228. } elseif( $field != '' ) {
  229. $f2 = $field;
  230. if( substr($f2, 0, 1) == '=' ) {
  231. $f2 = substr($f2, 1);
  232. } elseif( substr($f2, 0, 1) == '_' ) {
  233. $f2 = $pf . substr($f2, 1);
  234. } else {
  235. $f2 = $pf . $f2;
  236. }
  237. } elseif( isset($items[$i][$pf . 'name']) && strlen($items[$i][$pf . 'name']) > 0 ) {
  238. $f2 = $pf . 'name';
  239. } elseif( isset($items[$i][$pf . 'subject']) && strlen($items[$i][$pf . 'subject']) > 0 ) {
  240. $f2 = $pf . 'subject';
  241. }
  242. $itemsf[$items[$i][$f1]] = $items[$i][$f2];
  243. }
  244. return $itemsf;
  245. }
  246. function getLatestItems($limit_length = 5) {
  247. if( $limit_length <= 0 ) {
  248. $limit_length = 5;
  249. }
  250. $query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " "
  251. . " order by " . $this->prefix . "id desc "
  252. . " limit " . (int)$limit_length;
  253. return $this->adb->getRows($query);
  254. }
  255. //
  256. function checkItem($value, $type = 'id', $check = 0) {
  257. $value = func_db_input($value);
  258. if( is_array($this->item_fields) && sizeof($this->item_fields) > 0 ) {
  259. if( isset($this->item_fields[$type]) ) {
  260. $field = $this->item_fields[$type];
  261. if( substr($field, 0, 1) != '_' && substr($field, 0, 1) != '=' ) {
  262. $field = '_' . $field;
  263. }
  264. } elseif( in_array($type, $this->item_fields) ) {
  265. $field = '_' . $type;
  266. }
  267. }
  268. if( substr($type, 0, 1) == '_' || substr($type, 0, 1) == '=' ) {
  269. $field = $type;
  270. } elseif( in_array($type, array('id', 'key', 'link', 'name', 'subject', 'value', 'code')) ) {
  271. $field = '_' . $type;
  272. if( $type == 'id' ) {
  273. $value = intval($value);
  274. }
  275. } else {
  276. return false;
  277. }
  278. if( substr($field, 0, 1) == '_' ) {
  279. $field = $this->prefix . substr($field, 1);
  280. } elseif( substr($field, 0, 1) == '=' ) {
  281. $field = substr($field, 1);
  282. } else {
  283. $field = $this->prefix . $field;
  284. }
  285. $query = "select count(*) as count from " . $this->table . " where " . $this->_query_owner_rt()
  286. . " and " . $field . " = '" . $value . "'";
  287. return $this->adb->checkCount($query);
  288. }
  289. function getItem($value, $type = 'id', $check = 0) {
  290. $value = func_db_input($value);
  291. $this->doEvents("getItemBefore", $value, $type, $check);
  292. if( $check == -1 ) {
  293. //
  294. } elseif( $this->_table_status_usable == true ) {
  295. $query_str .= " and " . $this->prefix . "status = 1";
  296. }
  297. if( is_array($this->item_fields) && sizeof($this->item_fields) > 0 ) {
  298. if( isset($this->item_fields[$type]) ) {
  299. $field = $this->item_fields[$type];
  300. if( substr($field, 0, 1) != '_' && substr($field, 0, 1) != '=' ) {
  301. $field = '_' . $field;
  302. }
  303. } elseif( in_array($type, $this->item_fields) ) {
  304. $field = '_' . $type;
  305. }
  306. }
  307. if( substr($type, 0, 1) == '_' || substr($type, 0, 1) == '=' ) {
  308. $field = $type;
  309. } elseif( in_array($type, array('id', 'key', 'link', 'name', 'subject', 'value', 'code')) ) {
  310. $field = '_' . $type;
  311. if( $type == 'id' ) {
  312. $value = intval($value);
  313. }
  314. } else {
  315. return false;
  316. }
  317. if( substr($field, 0, 1) == '_' ) {
  318. $field = $this->prefix . substr($field, 1);
  319. } elseif( substr($field, 0, 1) == '=' ) {
  320. $field = substr($field, 1);
  321. } else {
  322. $field = $this->prefix . $field;
  323. }
  324. $query_str .= " and " . $field . " = '" . $value . "'";
  325. $query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " " . $query_str;
  326. $item = $this->adb->getRow($query);
  327. if( $this->format == true ) {
  328. $item = $this->_post_field($item, '', $this->prefix, $this->format_check);
  329. }
  330. $this->doEvents("getItemAfter", $value, $type, $check, $item);
  331. $item = $this->doEvents("formatItem", $item);
  332. return $item;
  333. }
  334. function getItemBy($value, $type = 'id', $check = 0) {
  335. $value = func_db_input($value);
  336. $this->doEvents("getItemBefore", $value, $type, $check);
  337. if( $check == -1 ) {
  338. //
  339. } elseif( $this->_table_status_usable == true ) {
  340. $query_str .= " and " . $this->prefix . "status = 1";
  341. }
  342. if( in_array($type, array('id', 'link')) ) {
  343. $field = $this->prefix . $type;
  344. } elseif( substr($type, 0, 1) == '=' ) {
  345. $field = substr($type, 1);
  346. } else {
  347. $field = $this->prefix . $type;
  348. }
  349. $query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " "
  350. . " and " . $field . " = '" . $value . "'";
  351. $item = $this->adb->getRow($query);
  352. if( $this->format == true ) {
  353. $item = $this->_post_field($item, '', $this->prefix, $this->format_check);
  354. }
  355. $this->doEvents("getItemAfter", $value, $type, $check, $item);
  356. $item = $this->doEvents("formatItem", $item);
  357. return $item;
  358. }
  359. function getPreviousItem($value, $type = 'id', $check = 0) {
  360. $query_str = "";
  361. if( $check == -1 ) {
  362. //
  363. } elseif( $this->_table_status_usable == true ) {
  364. $query_str .= " and " . $this->prefix . "status = 1";
  365. }
  366. $query_order_str = " order by " . $this->prefix . "id desc ";
  367. $query_limit_str = " limit 1";
  368. if( $type != 'id' ) {
  369. $item = $this->getItem($value, $type, -1);
  370. if( $this->format == true ) {
  371. $value = $item['id'];
  372. } else {
  373. $value = $item[$this->prefix . 'id'];
  374. }
  375. }
  376. if( $value > 0 ) {
  377. $query = "select * from " . $this->table . " where " . $this->prefix . "id < '" . (int)$value . "' "
  378. . $query_str . $query_order_str . $query_limit_str;
  379. $item = $this->adb->getRow($query);
  380. if( $this->format == true ) {
  381. $item = $this->_post_field($item, '', $this->prefix, $this->format_check);
  382. }
  383. return $item;
  384. }
  385. return false;
  386. }
  387. function getNextItem($value, $type = 'id', $check = 0) {
  388. $query_str = "";
  389. if( $check == -1 ) {
  390. //
  391. } elseif( $this->_table_status_usable == true ) {
  392. $query_str .= " and " . $this->prefix . "status = 1";
  393. }
  394. $query_order_str = " order by " . $this->prefix . "id asc ";
  395. $query_limit_str = " limit 1";
  396. if( $type != 'id' ) {
  397. $item = $this->getItem($value, $type, -1);
  398. if( $this->format == true ) {
  399. $value = $item['id'];
  400. } else {
  401. $value = $item[$this->prefix . 'id'];
  402. }
  403. }
  404. if( $value > 0 ) {
  405. $query = "select * from " . $this->table . " where " . $this->prefix . "id > '" . (int)$value . "' "
  406. . $query_str . $query_order_str . $query_limit_str;
  407. $item = $this->adb->getRow($query);
  408. if( $this->format == true ) {
  409. $item = $this->_post_field($item, '', $this->prefix, $this->format_check);
  410. }
  411. return $item;
  412. }
  413. return false;
  414. }
  415. function addItem($data_array) {
  416. if( !$this->_not_null($data_array) || !is_array($data_array) ) {
  417. return false;
  418. }
  419. $this->doEvents("addItemBefore", $data_array);
  420. $data_array = func_db_input($data_array);
  421. $data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
  422. //
  423. $data_array = $this->doEvents("addItemFilter", $data_array);
  424. //
  425. if( $this->_table_owner_usable == true ) {
  426. if( $this->_table_owner_prefix == '=' ) {
  427. $p = '';
  428. } elseif( $this->_table_owner_prefix == '+' ) {
  429. $p = $this->prefix;
  430. } elseif( $this->_table_owner_prefix == '' ) {
  431. $p = $this->prefix;
  432. } else {
  433. $p = $this->_table_owner_prefix;
  434. }
  435. $data_array[$p . $this->_table_owner_field] = $this->owner;
  436. }
  437. $data_array[$this->prefix . 'date_added'] = 'now()';
  438. $data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
  439. $data_array[$this->prefix . 'status'] = $this->_table_status_value;
  440. //$items_id = $this->adb->insertA($this->table, $data_array);
  441. $items_id = $this->_insert($this->table, $data_array);
  442. $this->doEvents("addItemAfter", $data_array, $items_id);
  443. //
  444. if( $items_id > 0 && $this->item_link == true ) {
  445. $name = isset($data_array[$this->prefix . 'subject']) ? $data_array[$this->prefix . 'subject'] : '';
  446. if( $name == '' ) {
  447. $name = isset($data_array[$this->prefix . 'name']) ? $data_array[$this->prefix . 'name'] : '';
  448. }
  449. $this->_generate_item_link($items_id, $name, $data_array[$this->prefix . 'link']);
  450. }
  451. return $items_id;
  452. }
  453. function addItems($data_array) {
  454. $items_ids = array();
  455. if( is_array($data_array) ) {
  456. foreach($data_array as $k => $v) {
  457. $items_ids[] = $this->addItem($v);
  458. }
  459. }
  460. return $items_ids;
  461. }
  462. function addItemsB($data_array) {
  463. $items_ids = array();
  464. if( is_array($data_array) ) {
  465. foreach($data_array as $k => $v) {
  466. if( isset($v[$this->prefix . 'id']) && $v[$this->prefix . 'id'] > 0 ) {
  467. $id = $v[$this->prefix . 'id'];
  468. unset($v[$this->prefix . 'id']);
  469. $this->updateItem($id, $v);
  470. $items_ids[] = $id;
  471. } else {
  472. $items_ids[] = $this->addItem($v);
  473. }
  474. }
  475. }
  476. return $items_ids;
  477. }
  478. function updateItem($items_id, $data_array, $params = '') {
  479. if( !$this->_not_null($items_id) || !$this->_not_null($data_array) || !is_array($data_array) ) {
  480. return false;
  481. }
  482. $this->doEvents("updateItemBefore", $items_id, $data_array);
  483. $data_array = func_db_input($data_array);
  484. $data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
  485. //
  486. $data_array = $this->doEvents("updateItemFilter", $items_id, $data_array);
  487. //
  488. $data_array[$this->prefix . 'last_modified'] = 'now()';
  489. $data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
  490. $params = ($params == '') ? "" : " and " . $params;
  491. $params = "" . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'" . $params;
  492. //$result = $this->adb->updateA($this->table, $data_array, $params);
  493. $result = $this->_update($this->table, $data_array, $params);
  494. $this->doEvents("updateItemAfter", $data_array, $items_id, $result);
  495. return $result;
  496. }
  497. function updateItems($items_ids, $data_array, $params = '') {
  498. if( is_array($items_ids) ) {
  499. $params = ($params == '') ? "" : " and " . $params;
  500. if( sizeof($items_ids) > 30 ) {
  501. foreach($items_ids as $k => $items_id) {
  502. $this->_update($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'" . $params);
  503. }
  504. } else {
  505. $query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
  506. $this->_update($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $this->prefix . "id in (" . $query_ids . ")" . $params);
  507. }
  508. }
  509. }
  510. function updateItemsF($items_ids, $field, $value = '') {
  511. $fs = array(
  512. 'top' => array('flag_top', '1'),
  513. 'recommend' => array('flag_recommend', '1'),
  514. 'untop' => array('falg_top', '0'),
  515. 'unrecommend' => array('flag_recommend', '0'),
  516. 'statusopen' => array('status', '1'),
  517. 'statusclose' => array('status', '0'),
  518. );
  519. if( $field && isset($fs[$field]) ) {
  520. $data_array = array($this->prefix . $fs[$field]['0'] => $fs[$field]['1']);
  521. return $this->updateItems($items_ids, $data_array);
  522. }
  523. return false;
  524. }
  525. function updateItemsCategory($items_ids, $categories_id) {
  526. if( is_array($items_ids) ) {
  527. if( sizeof($items_ids) > 30 ) {
  528. foreach($items_ids as $k => $items_id) {
  529. $this->adb->update("update " . $this->table . " set " . $this->prefix . "cid = '" . (int)$categories_id . "' where " . $this->prefix . "id = '" . (int)$items_id . "'");
  530. }
  531. } else {
  532. $query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
  533. return $this->adb->update("update " . $this->table . " set " . $this->prefix . "cid = '" . (int)$categories_id . "' where " . $this->prefix . "id in (" . $query_ids . ")");
  534. }
  535. }
  536. }
  537. function updateCategoryItemstoNewCategory($categories_id, $categories_id_new) {
  538. return $this->adb->update("update " . $this->table . " set " . $this->prefix . "cid = '" . (int)$categories_id_new . "' where " . $this->prefix . "cid = '" . (int)$categories_id . "'");
  539. }
  540. function updateItemsByFilter($data_array, $filter = '', $check = 0) {
  541. $query_str = "";
  542. $query_str .= $this->_get_query_str($filter, $check);
  543. $check = $this->getItemsTotal($filter, $check);
  544. if( $check == 0 ) {
  545. return false;
  546. }
  547. return $this->_update($this->table, $data_array, " " . $this->_query_owner_rt() . " " . $query_str . " ");
  548. }
  549. function deleteItem($items_id) {
  550. if( !$this->_not_null($items_id) ) {
  551. return false;
  552. }
  553. $this->doEvents("deleteItemBefore", $items_id);
  554. $this->adb->deleteA($this->table, " " . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
  555. $this->doEvents("deleteItemAfter", $items_id);
  556. return true;
  557. }
  558. function deleteItems($items_ids) {
  559. if( is_string($items_ids) && strpos($items_ids, ',') !== false ) {
  560. $items_ids = explode(',', $items_ids);
  561. }
  562. $this->doEvents("deleteItemsBefore", $items_ids);
  563. if( is_array($items_ids) ) {
  564. $this->_delete_ins($this->table, $this->prefix . 'id', $items_ids);
  565. }
  566. $this->doEvents("deleteItemsAfter", $items_ids);
  567. }
  568. function setItemsStatus($items_ids, $status = 1) {
  569. if( is_array($items_ids) ) {
  570. $this->_update_ins($this->table, "" . $this->prefix . "status = '" . (int)$status . "'", $this->prefix . 'id', $items_ids);
  571. }
  572. }
  573. //
  574. function check($items_id) {
  575. $count = $this->adb->getCountA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
  576. return ($count > 0) ? true : false;
  577. }
  578. function get($items_id) {
  579. return $this->adb->getRowA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
  580. }
  581. function add($data_array) {
  582. if( !$this->_not_null($data_array) || !is_array($data_array) ) {
  583. return false;
  584. }
  585. $data_array = func_db_input($data_array);
  586. $data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
  587. $data_array[$this->prefix . 'date_added'] = 'now()';
  588. $data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
  589. $data_array[$this->prefix . 'status'] = $this->_table_status_value;
  590. return $this->adb->insertA($this->table, $data_array);
  591. }
  592. function update($items_id, $data_array) {
  593. if( !$this->_not_null($items_id) ) {
  594. return false;
  595. }
  596. $data_array = func_db_input($data_array);
  597. $data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
  598. $data_array[$this->prefix . 'last_modified'] = 'now()';
  599. $data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
  600. return $this->adb->updateA($this->table, $data_array, $this->prefix . "id = '" . (int)$items_id . "'");
  601. }
  602. function delete($items_id) {
  603. return $this->adb->deleteA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
  604. }
  605. //private
  606. function _get_query_str($filter = '', $check = 0) {
  607. $query_str = "";
  608. if( $check == -1 ) {
  609. $query_str .= "";
  610. } elseif( $this->_table_status_usable == true ) {
  611. $query_str .= " and " . $this->prefix . "status = 1";
  612. }
  613. if( is_array($filter) ) {
  614. $filter = func_db_input($filter);
  615. if( is_array($this->filter_query) ) {
  616. $filter = array_merge($filter, $this->filter_query);
  617. }
  618. if( is_array($this->filters) ) {
  619. foreach($this->filters as $k => $v) {
  620. $filter[$k] = $v;
  621. }
  622. }
  623. //
  624. if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
  625. if( is_array($this->search_fields) ) {
  626. if( $this->search_type != 'or' ) {
  627. $this->search_type = 'and';
  628. }
  629. $i = 0;
  630. $query_str .= " and ( ";
  631. foreach($this->search_fields as $k => $field) {
  632. if( $i > 0 ) {
  633. $query_str .= " " . $this->search_type . " ";
  634. }
  635. $query_str .= " " . $this->prefix . $field . " like '%" . $filter['keyword'] . "%' ";
  636. $i++;
  637. }
  638. $query_str .= " ) ";
  639. } elseif( is_string($this->search_fields) ) {
  640. $query_str .= " and " . $this->prefix . $this->search_fields . " like '%" . $filter['keyword'] . "%' ";
  641. }
  642. }
  643. if( isset($filter['ids']) && $this->_not_null($filter['ids']) == true ) {
  644. if( is_array($filter['ids']) ) {
  645. $query_str .= " and " . $this->prefix . "id in (" . $this->_implode(', ', $filter['ids']) . ") ";
  646. } else {
  647. $query_str .= " and " . $this->prefix . "id = '" . (int)$filter['ids'] . "' ";
  648. }
  649. }
  650. if( isset($filter['parent']) && $this->_not_null($filter['parent']) == true ) {
  651. if( is_array($filter['parent']) ) {
  652. $query_str .= " and " . $this->prefix . "parent in (" . $this->_implode(', ', $filter['parent']) . ") ";
  653. } else {
  654. $query_str .= " and " . $this->prefix . "parent = '" . (int)$filter['parent'] . "' ";
  655. }
  656. }
  657. if( isset($filter['category']) ) {
  658. if( is_array($filter['category']) ) {
  659. $query_str .= " and categories_id in (" . $this->_implode(', ', $filter['category']) . ") ";
  660. } else {
  661. $query_str .= " and categories_id = '" . (int)$filter['category'] . "' ";
  662. }
  663. }
  664. if( isset($filter['nocategory']) && $filter['nocategory'] > 0 ) {
  665. $query_str .= " and categories_id = '0' ";
  666. }
  667. if( is_array($this->filter_fields) && sizeof($this->filter_fields) > 0 ) {
  668. $qt = $this->_get_query_type();
  669. foreach($filter as $k => $v) {
  670. if( is_array($this->filter_map) && isset($this->filter_map[$k]) ) {
  671. $k = $this->filter_map[$k];
  672. }
  673. if( in_array($k, $this->filter_fields) ) {
  674. $t = '';
  675. $p = $this->prefix;
  676. if( strpos($k, ':') !== false ) {
  677. $ks = explode(':', $k);
  678. $k = $ks['0'];
  679. $t = $ks['1'];
  680. if( sizeof($ks) > 2 ) {
  681. $p = $ks['2'];
  682. }
  683. }
  684. if( substr($k, 0, 1) == '=' ) {
  685. $k = substr($k, 1);
  686. $p = '';
  687. }
  688. if( !in_array($t, $qt) ) {
  689. $t = '';
  690. }
  691. if( in_array($t, array('=', '>', '>=', '<', '<=', '!=')) ) {
  692. if( $v === 'now()' || $v === 'now' ) {
  693. $query_str .= " and " . $p . $k . " " . $t . " now() ";
  694. } else {
  695. $query_str .= " and " . $p . $k . " " . $t . " '" . $v . "' ";
  696. }
  697. } elseif( in_array($t, array('[]', '[)', '(]', '()')) ) {
  698. if( is_array($v) ) {
  699. $t1 = substr($t, 0, 1);
  700. $t2 = substr($t, 1, 1);
  701. if( $t1 == '[' ) {
  702. $t1 = '<=';
  703. } elseif( $t1 == '(' ) {
  704. $t1 = '<';
  705. }
  706. if( $t2 == '[' ) {
  707. $t2 = '<=';
  708. } elseif( $t1 == '(' ) {
  709. $t2 = '<';
  710. }
  711. $query_str .= " and " . $p . $k . " " . $t1 . " '" . $v['0'] . "' ";
  712. $query_str .= " and " . $p . $k . " " . $t2 . " '" . $v['1'] . "' ";
  713. }
  714. } elseif( in_array($t, array('like', 'notlike', '%like%', '%like', 'like%',)) ) {
  715. if( $t == 'notlike' ) {
  716. $query_str .= " and " . $p . $k . " not like '%" . $v . "%' ";
  717. } else {
  718. $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
  719. }
  720. } else {
  721. if( $v === 'now()' || $v === 'now' ) {
  722. $query_str .= " and " . $p . $k . " < now() ";
  723. } elseif( is_array($v) && sizeof($v) > 0 ) {
  724. $query_str .= " and " . $p . $k . " in ('" . implode("','", $v) . "') ";
  725. } elseif( is_int($v) ) {
  726. $query_str .= " and " . $p . $k . " = '" . $v . "' ";
  727. } elseif( is_numeric($v) ) {
  728. $query_str .= " and " . $p . $k . " = '" . $v . "' ";
  729. } elseif( is_string($v) ) {
  730. $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
  731. } else {
  732. $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
  733. }
  734. }
  735. }
  736. }
  737. }
  738. }
  739. //
  740. $query_str .= $this->_get_query_str_custom($filter, $check);
  741. return $query_str;
  742. }
  743. function _get_query_str_custom($filter = '', $check = 0) {
  744. return "";
  745. }
  746. function _get_query_order_sort($order_by = '', $order_sort = '') {
  747. if( $order_by == '' ) {
  748. $order_by = $this->query_order_by;
  749. }
  750. if( $order_sort == '' ) {
  751. $order_sort = $this->query_order_sort;
  752. }
  753. if( $order_by != '' && $order_sort != '' ) {
  754. if( $order_by == 'dateadded' ) {
  755. $order_by = 'date_added';
  756. } elseif( $order_by == 'sortorder' ) {
  757. $order_by = 'sort_order';
  758. }
  759. //
  760. $sort_query = array();
  761. if( !is_array($this->sort_query) && is_array($this->sort_fields) ) {
  762. foreach($this->sort_fields as $k => $v) {
  763. if( substr($v, -1, 1) == '+' || substr($v, -1, 1) == '-' ) {
  764. $sort_query[] = substr($v, 0, -1);
  765. }
  766. }
  767. } else {
  768. $sort_query = $this->sort_query;
  769. }
  770. if( is_array($sort_query) && in_array($order_by, $sort_query) ) {
  771. $sort_fields = array($order_by . ($order_sort == 'desc' ? '+' : '-'));
  772. $this->sort_fields = $sort_fields;
  773. }
  774. }
  775. }
  776. function _query_owner_rt($pf = '') {
  777. if( $this->_table_owner_usable == true ) {
  778. if( $this->_table_owner_prefix == '=' ) {
  779. $p = '';
  780. } elseif( $this->_table_owner_prefix == '+' ) {
  781. $p = $this->prefix;
  782. } elseif( $this->_table_owner_prefix == '' ) {
  783. $p = $this->prefix;
  784. } else {
  785. $p = $this->_table_owner_prefix;
  786. }
  787. if( $pf != '' ) {
  788. $p = $pf . '.' . $p;
  789. }
  790. return $this->_query_owner($p . $this->_table_owner_field);
  791. }
  792. return " 1 ";
  793. }
  794. function _get_query_type() {
  795. $query_type = array(
  796. '=', '>', '>=', '<', '<=', '!=',
  797. '[]', '()', '[)', '(]',
  798. 'like', 'notlike', '%like%', '%like', 'like%',
  799. 'regexp', 'notregexp',
  800. );
  801. return $query_type;
  802. }
  803. //
  804. function _generate_item_link($items_id, $items_name = '', $items_link = '') {
  805. if( $items_link == '' && $items_name != '' ) {
  806. $items_link = $this->_filter_link($items_name);
  807. }
  808. if( $this->_not_null($items_link) ) {
  809. $items_link = func_db_input($items_link);
  810. if( $this->item_link_owner == true ) {
  811. $query = "select count(*) as count from " . $this->table . " where " . $this->_query_owner_rt() . " "
  812. . " and " . $this->prefix . "id != '" . (int)$items_id . "' "
  813. . " and " . $this->prefix . "link = '" . $items_link . "'";
  814. } else {
  815. $query = "select count(*) as count from " . $this->table . " where " . $this->prefix . "id != '" . (int)$items_id . "' "
  816. . " and " . $this->prefix . "link = '" . $items_link . "'";
  817. }
  818. $count = $this->adb->getCount($query);
  819. if( $count == 0 ) {
  820. $this->updateItem($items_id, array('_link' => $items_link));
  821. return true;
  822. }
  823. }
  824. $items_link = $this->_filter_link($items_name);
  825. $items_link = $this->_rand_link($items_id, $items_link);
  826. $this->_generate_item_link($items_id, $items_name, $items_link);
  827. return true;
  828. }
  829. //
  830. //Event
  831. function doEvents() {
  832. $_args = func_get_args();
  833. $_argc = func_num_args();
  834. if( $_argc == 0 ) {
  835. return false;
  836. }
  837. $method = array_shift($_args);
  838. $params = $_args;
  839. $method = 'on' . ucfirst($method);
  840. if( method_exists($this, $method) ) {
  841. //if( $params == '' ) {
  842. // $this->$method();
  843. //} else {
  844. // $this->$method($params);
  845. //}
  846. if( $_argc == 1 ) {
  847. $result = call_user_func_array(array($this, $method));
  848. } elseif( strstr($method, 'Filter') !== false && sizeof($params) == 1 ) {
  849. //$result = call_user_func_array(array($this, $method), array(&$params['0']));
  850. $result = $this->$method($params['0']);
  851. } else {
  852. $result = call_user_func_array(array($this, $method), $params);
  853. }
  854. }
  855. return $result;
  856. }
  857. function doTrigger($trigger, $params) {
  858. if( $trigger['type'] == 'cm' ) {
  859. if( $trigger['class_file'] != '' && file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $trigger['class_file']) ) {
  860. include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $trigger['class_file']);
  861. }
  862. $cmObj = new $trigger['class_name']();
  863. $vs = call_user_func_array(array($cmObj, $trigger['method']), $params);
  864. } elseif( $trigger['type'] == 'm' ) {
  865. $vs = call_user_func_array(array($this, $trigger['method']), $params);
  866. } elseif( $trigger['type'] == 'f' ) {
  867. $vs = call_user_func_array($trigger['function'], $params);
  868. }
  869. }
  870. function onAddItemBefore() {
  871. return true;
  872. }
  873. function onAddItemFilter($data_array) {
  874. $fb = $this->onAddItemFilterBind($data_array);
  875. if( is_array($fb) ) {
  876. foreach($fb as $k => $v) {
  877. if( substr($k, 0, 1) == '_' ) {
  878. $data_array[$this->prefix . substr($k, 1)] = $v;
  879. } elseif( substr($k, 0, 1) == '=' ) {
  880. $data_array[substr($k, 1)] = $v;
  881. } else {
  882. $data_array[$k] = $v;
  883. }
  884. }
  885. }
  886. //
  887. $fc = $this->onAddItemFilterClean($data_array);
  888. if( is_array($fc) ) {
  889. foreach($fc as $k => $v) {
  890. if( substr($k, 0, 1) == '_' ) {
  891. $k = $this->prefix . substr($k, 1);
  892. } elseif( substr($k, 0, 1) == '=' ) {
  893. $k = substr($k, 1);
  894. }
  895. if( isset($data_array[$k]) ) {
  896. unset($data_array[$k]);
  897. }
  898. }
  899. }
  900. return $data_array;
  901. }
  902. function onAddItemFilterBind($data_array) {
  903. return false;
  904. }
  905. function onAddItemFilterClean($data_array) {
  906. return false;
  907. }
  908. function onAddItemAfter($data_array, $items_id) {
  909. if( isset($this->triggers['additemafter']) ) {
  910. $trigger = $this->triggers['additemafter'];
  911. $params = func_get_args();
  912. if( $trigger['type'] == 'cm' ) {
  913. if( $trigger['class_file'] != '' && file_exists(CONFIG_PATH . CONFIG_DIR_CLASSES . $trigger['class_file']) ) {
  914. include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . $trigger['class_file']);
  915. }
  916. $cmObj = new $trigger['class_name']();
  917. $vs = call_user_func_array(array($cmObj, $trigger['method']), $params);
  918. } elseif( $trigger['type'] == 'm' ) {
  919. $vs = call_user_func_array(array($this, $trigger['method']), $params);
  920. } elseif( $trigger['type'] == 'f' ) {
  921. $vs = call_user_func_array($trigger['function'], $params);
  922. }
  923. }
  924. return true;
  925. }
  926. function onUpdateItemBefore($items_id, $data_array) {
  927. return true;
  928. }
  929. function onUpdateItemFilter($items_id, $data_array) {
  930. $fb = $this->onUpdateItemFilterBind($items_id, $data_array);
  931. if( is_array($fb) ) {
  932. foreach($fb as $k => $v) {
  933. if( substr($k, 0, 1) == '_' ) {
  934. $data_array[$this->prefix . substr($k, 1)] = $v;
  935. } elseif( substr($k, 0, 1) == '=' ) {
  936. $data_array[substr($k, 1)] = $v;
  937. } else {
  938. $data_array[$k] = $v;
  939. }
  940. }
  941. }
  942. //
  943. $fc = $this->onUpdateItemFilterClean($items_id, $data_array);
  944. if( is_array($fc) ) {
  945. foreach($fc as $k => $v) {
  946. if( substr($k, 0, 1) == '_' ) {
  947. $k = $this->prefix . substr($k, 1);
  948. } elseif( substr($k, 0, 1) == '=' ) {
  949. $k = substr($k, 1);
  950. }
  951. if( isset($data_array[$k]) ) {
  952. unset($data_array[$k]);
  953. }
  954. }
  955. }
  956. return $data_array;
  957. }
  958. function onUpdateItemFilterBind($items_id, $data_array) {
  959. return false;
  960. }
  961. function onUpdateItemFilterClean($items_id, $data_array) {
  962. return false;
  963. }
  964. function onUpdateItemAfter($items_id, $data_array, $result) {
  965. if( isset($this->triggers['updateitemafter']) ) {
  966. $trigger = $this->triggers['updateitemafter'];
  967. $params = func_get_args();
  968. $this->doTrigger($trigger, $params);
  969. }
  970. return true;
  971. }
  972. function onDeleteItemBefore($items_id) {
  973. return true;
  974. }
  975. function onDeleteItemAfter($items_id) {
  976. return true;
  977. }
  978. function onGetItemBefore($value, $type, $check) {
  979. return true;
  980. }
  981. function onGetItemAfter($value, $type, $check, $item) {
  982. return true;
  983. }
  984. function onGetItemsBefore() {
  985. return true;
  986. }
  987. function onGetItemsAfter() {
  988. return true;
  989. }
  990. function onFormatItem($item) {
  991. return $item;
  992. }
  993. }
  994. //
  995. ?>