/ 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
- <?php
- /***************************************************************
- * Copyright notice
- * (c) 2009, jianyuzhu@gmail.com
- * All rights reserved
- * This script is part of the PPEMI project.
- ***************************************************************/
-
- class BaseItemUserBase extends UserBase {
- 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();
-
- //join
- var $join_key = '';
- var $table_join = '';
- var $table_join_key = '';
-
- //bind
- var $table_bind = 'pp_users';
- var $bind = false;
-
- //constructor
- function BaseItemUserBase() {
- parent::UserBase();
- }
-
- 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_limit_str = "";
- if( $limit == true ) {
- $query_limit_str = $this->getQueryLimitStr();
- }
-
- $query_str = "";
- $query_str .= $this->_get_query_str($filter, $check);
- //$query_order_str = " order by " . $this->prefix . "id desc";
- $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 .= " a." . $v . " " . $as;
- $i++;
- }
- } else {
- $query_order_str = " order by a." . $this->prefix . "sort_order asc";
- }
- }
-
- if( $this->bind == true ) {
- if( $this->join_key != '' && $this->table_join != '' && $this->table_join_key != '' ) {
- $query = "select j.*, b.*, a.* from " . $this->table . " a "
- . " left join " . $this->table_join . " j on a." . $this->join_key . " = j." . $this->table_join_key . ", "
- . $this->table_bind . " b "
- . " where " . $this->_query_owner('a.users_id') . " "
- . " and a.users_id = b.users_id "
- . $query_str . $query_order_str . $query_limit_str;
- } else {
- $query = "select b.*, a.* from " . $this->table . " a, " . $this->table_bind . " b where " . $this->_query_owner('a.users_id') . " "
- . " and a.users_id = b.users_id "
- . $query_str . $query_order_str . $query_limit_str;
- }
- } else {
- if( $this->join_key != '' && $this->table_join != '' && $this->table_join_key != '' ) {
- $query = "select j.*, a.* from " . $this->table . " a "
- . " left join " . $this->table_join . " j on a." . $this->join_key . " = j." . $this->table_join_key . " "
- . " where " . $this->_query_owner('a.users_id') . " "
- . $query_str . $query_order_str . $query_limit_str;
- } else {
- $query = "select a.* from " . $this->table . " a where " . $this->_query_owner('a.users_id') . " "
- . $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);
-
- if( $this->bind == true ) {
- $query = "select count(*) as count from " . $this->table . " a, " . $this->table_bind . " b "
- . " where " . $this->_query_owner('a.users_id') . " "
- . " and a.users_id = b.users_id "
- . $query_str;
- } else {
- $query = "select count(*) as count from " . $this->table . " a where " . $this->_query_owner('a.users_id') . " " . $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($items_id) {
- $query_str = "";
- $query_str .= " and " . $this->_query_owner('users_id') . " and " . $this->prefix . "id = " . (int)$items_id . "";
- return $this->adb->checkCountA($this->table, "1 " . $query_str);
- }
-
- function checkItemA($items_id) {
- if( is_array($items_id) ) {
- $items_ids = $items_id;
- } else {
- $items_ids = array($items_id);
- }
- $query_str = "";
- $query_str .= " and " . $this->_query_owner('users_id') . " and " . $this->prefix . "id in (" . $this->_implode(', ', $items_ids) . ")";
- $count = $this->adb->getCountA($this->table, "1 " . $query_str);
- if( $count == count($items_ids) ) {
- return true;
- }
-
- return false;
- }
-
- function getItem($items_id) {
- $query = "select * from " . $this->table . " where " . $this->_query_owner('users_id') . " and " . $this->prefix . "id = '" . (int)$items_id . "'";
-
- $item = $this->adb->getRow($query);
- if( $this->format == true ) {
- $item = $this->_post_field($item, '', $this->prefix, $this->format_check);
- }
-
- return $item;
- }
-
- function addItem($data_array) {
- if( isset($data_array[$this->prefix . 'id']) ) {
- return false;
- }
- $data_array = func_db_input($data_array);
- $data_array['users_id'] = $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';
-
- return $this->_insert($this->table, $data_array);
- }
-
- function addItemB($data_array) {
- if( !isset($data_array[$this->prefix . 'id']) ) {
- return false;
- }
- $count = $this->adb->getCountA($this->table, "" . $this->_query_owner('users_id') . " and " . $this->prefix . "id = '" . (int)$data_array[$this->prefix . 'id'] . "'");
- if( $count == 0 ) {
- return $this->addItem($data_array);
- } else {
- return 0;
- }
- }
-
- 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 addItemsB($data_array) {
- $items_ids = array();
- if( is_array($data_array) ) {
- foreach($data_array as $k => $v) {
- if( isset($v[$this->prefix . 'id']) && $v[$this->prefix . 'id'] > 0 ) {
- $id = $v[$this->prefix . 'id'];
- unset($v[$this->prefix . 'id']);
-
- $this->updateItem($id, $v);
- $items_ids[] = $id;
- } else {
- $items_ids[] = $this->addItem($v);
- }
- }
- }
-
- return $items_ids;
- }
-
- function updateItem($items_id, $data_array) {
- $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->_update($this->table, $data_array, "" . $this->_query_owner('users_id') . " 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('users_id') . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
- }
- } else {
- $query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
- $this->_update($this->table, $data_array, "" . $this->_query_owner('users_id') . " and " . $this->prefix . "id in (" . $query_ids . ")");
- }
- }
- }
-
- function deleteItem($items_id) {
- $query = "delete from " . $this->table . " where " . $this->_query_owner('users_id') . " and " . $this->prefix . "id = '" . (int)$items_id . "'";
- return $this->adb->query($query);
- }
-
- function deleteItems($items_ids) {
- if( is_array($items_ids) ) {
- if( sizeof($items_ids) > 30 ) {
- foreach($items_ids as $k => $items_id) {
- $this->adb->deleteA($this->table, "" . $this->_query_owner('users_id') . " and " . $this->prefix . "id = '" . (int)$items_id . "'");
- }
- } else {
- $query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
- $this->adb->deleteA($this->table, "" . $this->_query_owner('users_id') . " and " . $this->prefix . "id in (" . $query_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 . "'");
- }
-
- //bind
- function bindItemsUsersA($items) {
- $users_ids = $this->getListKey($items, 'users_id');
- if( is_array($users_ids) ) {
- $users = $this->getUsers(array('ids' => $users_ids), -1, false);
- $users = $this->setListKey($users, 'users_id');
- for($i=0, $n=sizeof($items); $i<$n; $i++) {
- $items[$i] = array_merge($items[$i], $users[$items[$i]['users_id']]);
- }
- }
-
- return $items;
- }
-
- //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 .= " " . $this->search_type . " ";
- }
- $query_str .= " a." . $this->prefix . $field . " like '%" . $filter['keyword'] . "%' ";
- $i++;
- }
- $query_str .= " ) ";
- } elseif( is_string($this->search_fields) ) {
- $query_str .= " and a." . $this->prefix . $this->search_fields . " like '%" . $filter['keyword'] . "%' ";
- }
- }
- if( isset($filter['ids']) ) {
- $query_str .= " and a." . $this->prefix . "id in (" . $this->_implode(', ', $filter['ids']) . ")";
- }
- if( isset($filter['start']) && $this->_not_null($filter['start']) == true ) {
- $query_str .= " and a." . $this->prefix . "date_added >= '" . $filter['start'] . "' ";
- }
- if( isset($filter['end']) && $this->_not_null($filter['end']) == true ) {
- $query_str .= " and a." . $this->prefix . "date_added <= '" . $filter['end'] . "' ";
- }
- if( isset($filter['status']) ) {
- $this->bind = true;
- $query_str .= " and b." . $this->prefix . "status = '" . $filter['status'] . "' ";
- }
- 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 "";
- }
- }
- //
- ?>