/ 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
- <?php
- /***************************************************************
- * Copyright notice
- * (c) 2009, jianyuzhu@gmail.com
- * All rights reserved
- * This script is part of the PPEMI project.
- ***************************************************************/
-
- class BaseItemBindBase extends Base {
- var $table = '';
- var $prefix = '';
-
- //
- var $_table_owner_field = 'owner';
- var $_table_owner_prefix = '';
- var $_table_owner_usable = true;
- var $_table_status_usable = false;
-
- //
- var $format = false;
- var $format_check = false;
- var $format_prefix = 'field_';
- var $item_link = false;
- var $item_link_owner = false;
- var $item_fields = array();
- var $list_fields = array();
- var $search_fields = array('name');
- var $search_type = 'or';
- var $sort_order = true;
- var $sort_fields = array('sort_order+', 'name+');
- var $filter_fields = array();
- var $filter_map = array();
- var $filter_query = array();
-
- //
- var $bind_key = '';
- var $table_bind = '';
- var $table_bind_key = '';
- var $table_bind_join = array();
- var $table_bind_join_key = '';
- var $table_bind_join_params = array();
-
- //constructor
- function BaseItemBindBase($table = '') {
- parent::Base();
-
- if( $table != '' ) {
- $this->table = $table;
- }
- }
-
- function setProperty($name, $value = '') {
- if( is_array($name) ) {
- foreach($name as $k => $v) {
- $this->$k = $v;
- }
- } else {
- $this->$name = $value;
- }
- }
-
- function getProperty($name) {
- if( isset($this->$name) ) {
- return $this->$name;
- }
-
- return NULL;
- }
-
- function property($data) {
- if( is_array($data) ) {
- return $this->setProperty($data);
- } else {
- return $this->getProperty($data);
- }
- }
-
- function _pk() {
- return $this->prefix . $this->pkey;
- }
-
- function _pf($field) {
- return $this->prefix . $field;
- }
-
- function setFilterQuery($name, $value = '') {
- if( is_array($name) ) {
- $this->filter_query = $name;
- } else {
- $this->filter_query[$name] = $value;
- }
- }
-
- //methods
- function getItems($filter = '', $check = 0, $limit = true) {
- $query_str = "";
- $query_str .= $this->_get_query_str($filter, $check);
-
- $query_order_str = "";
- if( $this->sort_order == true ) {
- if( is_array($this->sort_fields) ) {
- $query_order_str = " order by ";
- $i = 0;
- foreach($this->sort_fields as $k => $v) {
- if( substr($v, 0, 1) == '=' ) {
- $v = substr($v, 1);
- } else {
- $v = $this->prefix . $v;
- }
- if( substr($v, -1) == '-' ) {
- $as = 'desc';
- $v = substr($v, 0, -1);
- } elseif( substr($v, -1) == '+' ) {
- $as = 'asc';
- $v = substr($v, 0, -1);
- } else {
- $as = 'asc';
- }
- if( $i > 0 ) {
- $query_order_str .= ", ";
- }
- $query_order_str .= " " . $v . " " . $as;
- $i++;
- }
- } else {
- $query_order_str = " order by " . $this->prefix . "sort_order asc";
- }
- }
-
- $query_limit_str = "";
- if( $limit == true ) {
- $query_limit_str = $this->getQueryLimitStr();
- }
-
- $query = "select * from " . $this->table . " a, " . $this->table_bind . " b ";
- if( $this->table_bind_join != '' && $this->table_bind_join_key != '' ) {
- $query .= " left join " . $this->table_bind_join . " bj on b." . $this->table_bind_join_key . " = bj." . $this->table_bind_join_key . " ";
- if( is_array($this->table_bind_join_params) ) {
- foreach($this->table_bind_join_params as $k => $v) {
- $query .= " and bj." . $k . " = '" . $v . "' ";
- }
- }
- }
- $query .= " where " . $this->_query_owner_rt('a')
- . " and a." . $this->bind_key . " = b." . $this->table_bind_key . " "
- . $query_str . $query_order_str . $query_limit_str;
- $rows = $this->adb->getRows($query);
-
- if( $this->format == true ) {
- for($i=0, $n=sizeof($rows); $i<$n; $i++) {
- $rows[$i] = $this->_post_field($rows[$i], '', $this->prefix, $this->format_check);
- }
- }
-
- return $rows;
- }
-
- function getItemsTotal($filter = '', $check = 0) {
- $query_str = "";
- $query_str .= $this->_get_query_str($filter, $check);
-
- $query = "select count(*) as count from " . $this->table . " a, " . $this->table_bind . " b ";
- if( $this->table_bind_join != '' && $this->table_bind_join_key != '' ) {
- $query .= " left join " . $this->table_bind_join . " bj on b." . $this->table_bind_join_key . " = bj." . $this->table_bind_join_key . " ";
- if( is_array($this->table_bind_join_params) ) {
- foreach($this->table_bind_join_params as $k => $v) {
- $query .= " and bj." . $k . " = '" . $v . "' ";
- }
- }
- }
- $query .= " where " . $this->_query_owner_rt('a')
- . " and a." . $this->bind_key . " = b." . $this->table_bind_key . " "
- . $query_str;
-
- return $this->adb->getCount($query);
- }
-
- function getItemsFormat($filter = '', $check = 0, $limit = true, $field = '') {
- $items = $this->getItems($filter, $check, $limit);
- $pf = $this->prefix;
- if( $this->format == true ) {
- $pf = '';
- }
- $itemsf = array();
- for($i=0, $n=sizeof($items); $i<$n; $i++) {
- $f1 = $pf . 'id';
- $f2 = $pf . 'id';
- $f3 = 0;
- if( is_array($field) && sizeof($field) >= 2 ) {
- $f1 = $field['0'];
- $f2 = $field['1'];
- if( substr($f1, 0, 1) == '=' ) {
- $f1 = substr($f1, 1);
- } elseif( substr($f1, 0, 1) == '_' ) {
- $f1 = $pf . substr($f1, 1);
- } else {
- $f1 = $pf . $f1;
- }
- if( substr($f2, 0, 1) == '=' ) {
- $f2 = substr($f2, 1);
- } elseif( substr($f2, 0, 1) == '_' ) {
- $f2 = $pf . substr($f2, 1);
- } else {
- $f2 = $pf . $f2;
- }
- } elseif( $field != '' ) {
- $f2 = $field;
- if( substr($f2, 0, 1) == '=' ) {
- $f2 = substr($f2, 1);
- } elseif( substr($f2, 0, 1) == '_' ) {
- $f2 = $pf . substr($f2, 1);
- } else {
- $f2 = $pf . $f2;
- }
- } elseif( isset($items[$i][$pf . 'name']) && strlen($items[$i][$pf . 'name']) > 0 ) {
- $f2 = $pf . 'name';
- } elseif( isset($items[$i][$pf . 'subject']) && strlen($items[$i][$pf . 'subject']) > 0 ) {
- $f2 = $pf . 'subject';
- }
- $itemsf[$items[$i][$f1]] = $items[$i][$f2];
- }
-
- return $itemsf;
- }
-
- //
- function checkItem($value, $type = 'id') {
- $value = func_db_input($value);
-
- if( is_array($this->item_fields) && sizeof($this->item_fields) > 0 ) {
- if( isset($this->item_fields[$type]) ) {
- $field = $this->item_fields[$type];
- if( substr($field, 0, 1) != '_' && substr($field, 0, 1) != '=' ) {
- $field = '_' . $field;
- }
- } elseif( in_array($type, $this->item_fields) ) {
- $field = '_' . $type;
- }
- }
- if( substr($type, 0, 1) == '_' || substr($type, 0, 1) == '=' ) {
- $field = $type;
- } elseif( in_array($type, array('id', 'key', 'link', 'name', 'subject', 'value', 'code')) ) {
- $field = '_' . $type;
- if( $type == 'id' ) {
- $value = intval($value);
- }
- } else {
- return false;
- }
- if( substr($field, 0, 1) == '_' ) {
- $field = $this->prefix . substr($field, 1);
- } elseif( substr($field, 0, 1) == '=' ) {
- $field = substr($field, 1);
- } else {
- $field = $this->prefix . $field;
- }
-
- $query = "select count(*) as count from " . $this->table . " where " . $this->_query_owner_rt()
- . " and " . $field . " = '" . $value . "'";
-
- return $this->adb->checkCount($query);
- }
-
- function getItem($value, $type = 'id') {
- $value = func_db_input($value);
-
- $this->doEvents("getItemBefore", $value, $type, $check);
- if( $check == -1 ) {
- //
- } elseif( $this->_table_status_usable == true ) {
- $query_str .= " and " . $this->prefix . "status = 1";
- }
- if( is_array($this->item_fields) && sizeof($this->item_fields) > 0 ) {
- if( isset($this->item_fields[$type]) ) {
- $field = $this->item_fields[$type];
- if( substr($field, 0, 1) != '_' && substr($field, 0, 1) != '=' ) {
- $field = '_' . $field;
- }
- } elseif( in_array($type, $this->item_fields) ) {
- $field = '_' . $type;
- }
- }
- if( substr($type, 0, 1) == '_' || substr($type, 0, 1) == '=' ) {
- $field = $type;
- } elseif( in_array($type, array('id', 'key', 'link', 'name', 'subject', 'value', 'code')) ) {
- $field = '_' . $type;
- if( $type == 'id' ) {
- $value = intval($value);
- }
- } else {
- return false;
- }
- if( substr($field, 0, 1) == '_' ) {
- $field = $this->prefix . substr($field, 1);
- } elseif( substr($field, 0, 1) == '=' ) {
- $field = substr($field, 1);
- } else {
- $field = $this->prefix . $field;
- }
- $query_str .= " and " . $field . " = '" . $value . "'";
- $query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " " . $query_str;
-
- $item = $this->adb->getRow($query);
- if( $this->format == true ) {
- $item = $this->_post_field($item, '', $this->prefix, $this->format_check);
- }
-
- return $item;
- }
-
- function getItemTarget($value) {
- $query_str = " and a." . $this->bind_key . " = '" . (int)$value . "'";
-
- $query = "select a.* from " . $this->table . " a, " . $this->table_bind . " b ";
- if( $this->table_bind_join != '' && $this->table_bind_join_key != '' ) {
- $query .= " left join " . $this->table_bind_join . " bj on b." . $this->table_bind_join_key . " = bj." . $this->table_bind_join_key . " ";
- if( is_array($this->table_bind_join_params) ) {
- foreach($this->table_bind_join_params as $k => $v) {
- $query .= " and bj." . $k . " = '" . $v . "' ";
- }
- }
- }
-
- $query_order_str = "";
- $query_limit_str = "";
-
- $query .= " where " . $this->_query_owner_rt('a')
- . " and a." . $this->bind_key . " = b." . $this->table_bind_key . " "
- . $query_str . $query_order_str . $query_limit_str;
-
- return $this->adb->getRow($query);
- }
-
- function getTargetItem($value) {
- $query_str = " and b." . $this->table_bind_key . " = '" . (int)$value . "'";
-
- $query = "select a.* from " . $this->table . " a, " . $this->table_bind . " b ";
- if( $this->table_bind_join != '' && $this->table_bind_join_key != '' ) {
- $query .= " left join " . $this->table_bind_join . " bj on b." . $this->table_bind_join_key . " = bj." . $this->table_bind_join_key . " ";
- if( is_array($this->table_bind_join_params) ) {
- foreach($this->table_bind_join_params as $k => $v) {
- $query .= " and bj." . $k . " = '" . $v . "' ";
- }
- }
- }
-
- $query_order_str = "";
- $query_limit_str = "";
-
- $query .= " where " . $this->_query_owner_rt('a')
- . " and a." . $this->bind_key . " = b." . $this->table_bind_key . " "
- . $query_str . $query_order_str . $query_limit_str;
-
- return $this->adb->getRow($query);
- }
-
- function addItem($data_array) {
- if( !$this->_not_null($data_array) || !is_array($data_array) ) {
- return false;
- }
-
- $data_array = func_db_input($data_array);
- $data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
-
- if( $this->_table_owner_usable == true ) {
- if( $this->_table_owner_prefix == '=' ) {
- $p = '';
- } elseif( $this->_table_owner_prefix == '+' ) {
- $p = $this->prefix;
- } elseif( $this->_table_owner_prefix == '' ) {
- $p = $this->prefix;
- } else {
- $p = $this->_table_owner_prefix;
- }
- $data_array[$p . $this->_table_owner_field] = $this->owner;
- }
- $data_array[$this->prefix . 'date_added'] = 'now()';
- $data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
- $data_array[$this->prefix . 'status'] = '1';
-
- $items_id = $this->adb->insertA($this->table, $data_array);
-
- return $items_id;
- }
-
- function addItems($data_array) {
- $items_ids = array();
- if( is_array($data_array) ) {
- foreach($data_array as $k => $v) {
- $items_ids[] = $this->addItem($v);
- }
- }
-
- return $items_ids;
- }
-
- function updateItem($items_id, $data_array) {
- if( !$this->_not_null($items_id) || !$this->_not_null($data_array) || !is_array($data_array) ) {
- return false;
- }
-
- $data_array = func_db_input($data_array);
- $data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
-
- $data_array[$this->prefix . 'last_modified'] = 'now()';
- $data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
-
- return $this->adb->updateA($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
- }
-
- function updateItems($items_ids, $data_array) {
- if( is_array($items_ids) ) {
- if( sizeof($items_ids) > 30 ) {
- foreach($items_ids as $k => $items_id) {
- $this->_update($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
- }
- } else {
- $query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
- $this->_update($this->table, $data_array, "" . $this->_query_owner_rt() . " and " . $this->prefix . "id in (" . $query_ids . ")");
- }
- }
- }
-
- function deleteItem($items_id) {
- if( !$this->_not_null($items_id) ) {
- return false;
- }
-
- $this->adb->deleteA($this->table, " " . $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
-
- return true;
- }
-
- function deleteItems($items_ids) {
- if( is_array($items_ids) ) {
- $this->_delete_ins($this->table, $this->prefix . 'id', $items_ids);
- }
- }
-
- function setItemsStatus($items_ids, $status = 1) {
- if( is_array($items_ids) ) {
- $this->_update_ins($this->table, "" . $this->prefix . "status = '" . (int)$status . "'", $this->prefix . 'id', $items_ids);
- }
- }
-
- //
- function check($items_id) {
- $count = $this->adb->getCountA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
-
- return ($count > 0) ? true : false;
- }
-
- function get($items_id) {
- return $this->adb->getRowA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
- }
-
- function add($data_array) {
- if( !$this->_not_null($data_array) || !is_array($data_array) ) {
- return false;
- }
-
- $data_array = func_db_input($data_array);
- $data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
-
- $data_array[$this->prefix . 'date_added'] = 'now()';
- $data_array[$this->prefix . 'added_by'] = $this->_get_current_user();
- $data_array[$this->prefix . 'status'] = '1';
-
- return $this->adb->insertA($this->table, $data_array);
- }
-
- function update($items_id, $data_array) {
- if( !$this->_not_null($items_id) ) {
- return false;
- }
-
- $data_array = func_db_input($data_array);
- $data_array = $this->_post_field($data_array, $this->prefix, $this->format_prefix, $this->format_check);
-
- $data_array[$this->prefix . 'last_modified'] = 'now()';
- $data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
-
- return $this->adb->updateA($this->table, $data_array, $this->prefix . "id = '" . (int)$items_id . "'");
- }
-
- function delete($items_id) {
- return $this->adb->deleteA($this->table, $this->prefix . "id = '" . (int)$items_id . "'");
- }
-
- //private
- function _get_query_str($filter = '', $check = 0) {
- $query_str = "";
- if( $check == -1 ) {
- $query_str .= "";
- } else {
- $query_str .= " and a." . $this->prefix . "status = 1";
- }
- if( is_array($filter) ) {
- $filter = func_db_input($filter);
- if( is_array($this->filter_query) ) {
- $filter = array_merge($filter, $this->filter_query);
- }
- //
- if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
- if( is_array($this->search_fields) ) {
- if( $this->search_type != 'or' ) {
- $this->search_type = 'and';
- }
- $i = 0;
- $query_str .= " and ( ";
- foreach($this->search_fields as $k => $field) {
- if( $i > 0 ) {
- $query_str .= " a." . $this->search_type . " ";
- }
- $query_str .= " a." . $this->prefix . $field . " like '%" . $filter['keyword'] . "%' ";
- $i++;
- }
- $query_str .= " ) ";
- } elseif( is_string($this->search_fields) ) {
- $query_str .= " and " . $this->prefix . $this->search_fields . " like '%" . $filter['keyword'] . "%' ";
- }
- }
- if( isset($filter['ids']) && $this->_not_null($filter['ids']) == true ) {
- if( is_array($filter['ids']) ) {
- $query_str .= " and a." . $this->prefix . "id in (" . $this->_implode(', ', $filter['ids']) . ") ";
- } else {
- $query_str .= " and a." . $this->prefix . "id = '" . (int)$filter['ids'] . "' ";
- }
- }
- if( isset($filter['category']) ) {
- if( is_array($filter['category']) ) {
- $query_str .= " and a.categories_id in (" . $this->_implode(', ', $filter['category']) . ") ";
- } else {
- $query_str .= " and a.categories_id = '" . (int)$filter['category'] . "' ";
- }
- }
- if( is_array($this->filter_fields) && sizeof($this->filter_fields) > 0 ) {
- foreach($filter as $k => $v) {
- if( in_array($k, $this->filter_fields) ) {
- if( is_array($v) && sizeof($v) > 0 ) {
- $query_str .= " and a." . $this->prefix . $k . " in ('" . implode("','", $v) . "') ";
- } elseif( is_int($v) ) {
- $query_str .= " and a." . $this->prefix . $k . " = '" . $v . "' ";
- } elseif( is_numeric($v) ) {
- $query_str .= " and a." . $this->prefix . $k . " = '" . $v . "' ";
- } elseif( is_string($v) ) {
- $query_str .= " and a." . $this->prefix . $k . " like '%" . $v . "%' ";
- } else {
- $query_str .= " and a." . $this->prefix . $k . " like '%" . $v . "%' ";
- }
- }
- }
- }
- }
- //
- $query_str .= $this->_get_query_str_custom($filter, $check);
-
- return $query_str;
- }
-
- function _get_query_str_custom($filter = '', $check = 0) {
- return "";
- }
- }
- //
- ?>