/ php-ppcms/includes/classes/base.item.content.class.php
http://php-ppcms.googlecode.com/ · PHP · 849 lines · 721 code · 99 blank · 29 comment · 217 complexity · 7e9494cee9aead626c009a29feae7481 MD5 · raw file
- <?php
- /***************************************************************
- * Copyright notice
- * (c) 2009, jianyuzhu@gmail.com
- * All rights reserved
- * This script is part of the PPEMI project.
- ***************************************************************/
-
- class BaseItemContentBase 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 = true;
- var $format_check = false;
- var $format_prefix = 'field_';
- var $item_link = false;
- var $item_link_owner = false;
- var $item_fields = array('id', 'name', 'link');
- 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 $sort_query = array();
- var $filter_fields = array();
- var $filter_map = array();
- var $filter_query = array();
-
- //
- var $table_to_category = '';
- var $table_category = '';
-
- //constructor
- function BaseItemContentBase() {
- parent::Base();
- }
-
- //methods
- function addItem($data_array, $tocategory = true, $multipage = false) {
- //$data_array[$this->prefix . 'fulltext'] = $this->_filter_content($data_array[$this->prefix . 'fulltext']);
- $data_array = func_db_input($data_array);
- $data_array[$this->prefix . 'owner'] = $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->_insert($this->table, $data_array);
-
- //link
- if( $items_id > 0 ) {
- $this->_update_item_link_datecount($items_id);
- }
-
- //category
- if( $tocategory == true ) {
- $categories_id = $data_array['categories_id'];
- $query = "insert into " . $this->table_to_category . " (" . $this->prefix . "id, categories_id) values ('" . (int)$items_id . "', '" . $categories_id . "')";
- $result = $this->adb->query($query);
- }
-
- //Multi page
- if( $multipage == true ) {
- $data_ft = $data_array[$this->prefix . 'fulltext'];
- $data_fts = explode("{{pagebreak}}", $data_ft);
- if( is_array($data_fts) ) {
- $page = 0;
- $parent = 0;
- $items_ids = array();
- for($i=0, $n=sizeof($data_fts); $i<$n; $i++) {
- if( strlen($data_fts[$i]) > 0 ) {
- $page++;
- }
- }
- if( $page > 0 ) {
- $this->updateItem($items_id, array($this->prefix . 'pagecount' => $page));
- }
- }
- }
-
- return $items_id;
- }
-
- //Multi page
- function addItemP($data_array) {
- //$data_ft = $this->_filter_content($data_array[$this->prefix . 'fulltext']);
- $data_fts = explode("{{pagebreak}}", $data_ft);
- if( is_array($data_fts) ) {
- $page = 0;
- $parent = 0;
- $items_ids = array();
- for($i=0, $n=sizeof($data_fts); $i<$n; $i++) {
- if( strlen($data_fts[$i]) > 0 ) {
- $data_array[$this->prefix . 'fulltext'] = $data_fts[$i];
- $data_array[$this->prefix . 'parent'] = $parent;
- if( $page == 0 ) {
- $items_ids[] = $parent = $this->addItem($data_array, true);
- } else {
- $items_ids[] = $this->addItem($data_array, false);
- }
- $page++;
- }
- }
- if( $page > 0 ) {
- $this->updateItem($parent, array($this->prefix . 'pagecount' => $page, $this->prefix . 'pages' => $this->_implode(', ', $items_ids)));
- }
-
- return $parent;
- } else {
- return $this->addItem($data_array);
- }
- }
-
- function addItemCategory($items_id, $categories_id) {
- if( $items_id > 0 && $categories_id > 0 ) {
- $query = "select count(*) as count from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "' "
- . " and categories_id = '" . (int)$categories_id . "'";
- $count = $this->adb->getCount($query);
- if( $count == 0 ) {
- $query = "insert into " . $this->table_to_category . " (" . $this->prefix . "id, categories_id) "
- . " values ('" . (int)$items_id . "', '" . (int)$categories_id . "')";
- $result = $this->adb->query($query);
-
- return true;
- } elseif( $nums == 1 ) {
- return 1;
- } elseif( $nums > 1 ) {
- return 2;
- }
- }
-
- return false;
- }
-
- function addItemView($items_id) {
- $query = "update " . $this->table . " set " . $this->prefix . "viewed = " . $this->prefix . "viewed + 1 "
- . " where " . $this->prefix . "id = '" . (int)$items_id . "'";
- return $this->adb->query($query);
- }
-
- function getItem($value, $type = 'id', $check = 0) {
- $value = func_db_input($value);
-
- if( $check == -1 ) {
- $query_str = "";
- } else {
- $query_str = " and c." . $this->prefix . "status = 1";
- }
- if( $type == 'id' ) {
- $query = "select c.*, c2c.categories_id from " . $this->table . " c "
- . " left join " . $this->table_to_category . " c2c on c." . $this->prefix . "id = c2c." . $this->prefix . "id "
- . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "id = '" . (int)$value . "' " . $query_str;
- } elseif( $type == 'link' ) {
- $query = "select c.*, c2c.categories_id from " . $this->table . " c "
- . " left join " . $this->table_to_category . " c2c on c." . $this->prefix . "id = c2c." . $this->prefix . "id "
- . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "link = '" . $value . "' " . $query_str;
- } else {
- return false;
- }
-
- return $this->adb->getRow($query);
- }
-
- function getPreviousItem($value, $type = 'id', $check = 0) {
- $value = func_db_input($value);
-
- $query_limit_str = " limit 1";
- if( $check == -1 ) {
- $query_str = "";
- } else {
- $query_str = " and a." . $this->prefix . "status = 1";
- }
- $query_str .= " order by " . $this->prefix . "id desc ";
- if( $type == 'link' ) {
- $item = $this->getItem($value, 'link', -1, 0);
- $value = $item[$this->prefix . 'id'];
- }
- $query = "select a.* from " . $this->table . " a "
- . " left join " . $this->table_to_category . " c2ca on a." . $this->prefix . "id = c2ca." . $this->prefix . "id, "
- . " " . $this->table . " b left join " . $this->table_to_category . " c2cb on b." . $this->prefix . "id = c2cb." . $this->prefix . "id "
- . " where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id < '" . (int)$value . "' "
- . " and b." . $this->prefix . "id = '" . (int)$value . "' and c2ca.categories_id = c2cb.categories_id "
- . $query_str . $query_limit_str;
-
- return $this->adb->getRow($query);
- }
-
- function getNextItem($value, $type = 'id', $check = 0) {
- $value = func_db_input($value);
-
- $query_limit_str = " limit 1";
- if( $check == -1 ) {
- $query_str = "";
- } else {
- $query_str = " and a." . $this->prefix . "status = 1";
- }
- $query_str .= " order by " . $this->prefix . "id asc ";
- if( $type == 'link' ) {
- $item = $this->getItem($value, 'link', -1, 0);
- $value = $item[$this->prefix . 'id'];
- }
- $query = "select a.* from " . $this->table . " "
- . " a left join " . $this->table_to_category . " c2ca on a." . $this->prefix . "id = c2ca." . $this->prefix . "id, "
- . " " . $this->table . " b left join " . $this->table_to_category . " c2cb on b." . $this->prefix . "id = c2cb." . $this->prefix . "id "
- . " where " . $this->_query_owner('a." . $this->prefix . "owner') . " and a." . $this->prefix . "id > '" . (int)$value . "' "
- . " and b." . $this->prefix . "id = '" . (int)$value . "' and c2ca.categories_id = c2cb.categories_id "
- . $query_str . $query_limit_str;
-
- return $this->adb->getRow($query);
- }
-
- function getItemsP($items_id, $check = 0) {
- if( $check == -1 ) {
- $query_str = "";
- } else {
- $query_str = " and c." . $this->prefix . "status = 1";
- }
- $query = "select c.*, c2c.categories_id from " . $this->table . " c "
- . " left join " . $this->table_to_category . " c2c on c." . $this->prefix . "id = c2c." . $this->prefix . "id "
- . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "parent = '" . (int)$items_id . "' " . $query_str;
-
- return $this->adb->getRows($query);
- }
-
- function getCategoryID($items_id) {
- $query = "select categories_id from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
- $row = $this->adb->getRow($query);
-
- return $row['categories_id'];
- }
-
- function getCategory($items_id) {
- $query = "select cc.* from " . $this->table_category . " cc, " . $this->table_to_category . " c2c "
- . " where cc.categories_id = c2c.categories_id and c2c." . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
-
- return $this->adb->getRow($query);
- }
-
- function getCategories($items_id) {
- $query = "select cc.* from " . $this->table_category . " cc, " . $this->table_to_category . " c2c "
- . " where cc.categories_id = c2c.categories_id and c2c." . $this->prefix . "id = '" . (int)$items_id . "'";
- return $this->adb->getRows($query);
- }
-
- function getCategoriesID($items_id) {
- $query = "select categories_id from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "' limit 1";
- $result = $this->adb->query($query);
- while($row = $this->adb->fetch_array($result)) {
- $rows[] = $row['categories_id'];
- }
-
- return $rows;
- }
-
- function getItems($filter = '', $check = 0, $limit = true, $level = 1) {
- $query_limit_str = "";
- if( $limit == true ) {
- $query_limit_str = $this->getQueryLimitStr();
- }
-
- $query_str_category = $this->_get_query_str($filter, $check);
- $query_str = $query_str_category['0'];
- $query_category = $query_str_category['1'];
-
- $query_order_str .= " order by c." . $this->prefix . "top desc, c." . $this->prefix . "sort_order asc, c." . $this->prefix . "id desc ";
- if( $query_category == 1 ) {
- $query = "select c.*, c2c.categories_id from " . $this->table . " c, " . $this->table_to_category . " c2c "
- . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "parent = 0 "
- . $query_str . $query_order_str . $query_limit_str;
- } else {
- $query = "select c.* from " . $this->table . " c "
- . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "parent = 0 "
- . $query_str . $query_order_str . $query_limit_str;
- }
-
- return $this->adb->getRows($query);
- }
-
- function getItemsTotal($filter = '', $check = 0, $limit = true) {
- $query_str_category = $this->_get_query_str($filter, $check);
- $query_str = $query_str_category['0'];
- $query_category = $query_str_category['1'];
-
- if( $query_category == 1 ) {
- $query = "select count(*) as count from " . $this->table . " c, " . $this->table_to_category . " c2c "
- . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "parent = 0 " . $query_str;
- } else {
- $query = "select count(*) as count from " . $this->table . " c "
- . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "parent = 0 " . $query_str;
- }
-
- return $this->adb->getCount($query);
- }
-
- function getLatestItems($categories_id = 0, $limit_length = 5) {
- if( $limit_length > 0 ) {
-
- } else {
- $limit_lenght = 5;
- }
- $query_limit_str = " limit " . $limit_length;
-
- $query_str = "";
- if( $categories_id > 0 ) {
- $query = "select c.*, cc.categories_name from " . $this->table . " c, " . $this->table_category . " cc, " . $this->table_to_category . " c2c "
- . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "parent = 0 "
- . " and c." . $this->prefix . "id = c2c." . $this->prefix . "id "
- . " and c2c.categories_id = cc.categories_id and cc.categories_id = '" . (int)$categories_id . "' "
- . $query_str . " order by c." . $this->prefix . "date_added desc, c." . $this->prefix . "sort_order asc " . $query_limit_str;
- } else {
- $query = "select c.* from " . $this->table . " c "
- . " where " . $this->_query_owner_rt('c') . " and c." . $this->prefix . "parent = 0 "
- . $query_str . " order by c." . $this->prefix . "date_added desc, c." . $this->prefix . "sort_order asc " . $query_limit_str;
- }
-
- return $this->adb->getRows($query);
- }
-
- function getLatestItemsR($categories_id = 0, $include_subs = true, $count = 5) {
- if( $count > 0 ) {
- //
- } else {
- $count = 6;
- }
- $limit_length = ($count * 3);
- $query_limit_str = " limit " . $limit_length;
- if( $categories_id > 0 ) {
- $categories_ids = $this->getCategoriesIDs($categories_id, $include_subs);
- if( is_array($categories_ids) ) {
- $category = array_merge(array($categories_id), $categories_ids);
- } else {
- $category = $categories_id;
- }
- if( $filter == '' || !is_array($filter) ) {
- $filter = array();
- $filter['category'] = $category;
- } else {
- $filter['category'] = $category;
- }
- $rows = $this->getItems($filter, $check);
- } else {
- $query_str = "";
- $query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " and " . $this->prefix . "parent = 0 "
- . $query_str . " order by " . $this->prefix . "date_added desc, " . $this->prefix . "sort_order asc " . $query_limit_str;
-
- $result = $this->adb->query($query);
- while($row = $this->adb->fetch_array($result)) {
- $rows[] = $row;
- }
- }
-
- return $this->_rows_rands($rows, $count);
- }
-
- function getFocusItems($count = 5) {
- if( $count > 0 ) {
- //
- } else {
- $count = 6;
- }
- $limit_length = ($count * 3);
- $query_limit_str = " limit " . $limit_length;
-
- $query_str = "";
- $query = "select c.*, r.* from " . $this->table . " c, pp_resources r "
- . " where " . $this->_query_owner_rt('c') . " "
- . " and c." . $this->prefix . "id = r.resources_target and r.resources_type = 'content' "
- . " and (r.resources_filetype = 'image/pjpeg' or r.resources_filetype = 'image/jpeg' or r.resources_filetype = 'image/gif') "
- . $query_str
- . " group by c." . $this->prefix . "id "
- . " order by c." . $this->prefix . "top desc, r.resources_date_added desc, c." . $this->prefix . "date_added desc, c." . $this->prefix . "sort_order asc "
- . $query_limit_str;
- $rows = $this->adb->getRows($query);
-
- return $rows;
- }
-
- function getCategoryItems($categories_id, $include_subs = true, $filter = '', $check = 0, $limit = true) {
- $categories_ids = $this->getCategoriesIDs($categories_id, $include_subs);
- if( is_array($categories_ids) ) {
- $category = array_merge(array($categories_id), $categories_ids);
- } else {
- $category = $categories_id;
- }
- if( $filter == '' || !is_array($filter) ) {
- $filter = array();
- $filter['category'] = $category;
- } else {
- $filter['category'] = $category;
- }
-
- return $this->getItems($filter, $check, $limit);
- }
-
- function getCategoryItemsTotal($categories_id, $include_subs = true, $filter = '', $check = 0) {
- $categories_ids = $this->getCategoriesIDs($categories_id, $include_subs);
- if( is_array($categories_ids) ) {
- $category = array_merge(array($categories_id), $categories_ids);
- } else {
- $category = $categories_id;
- }
- if( $filter == '' || !is_array($filter) ) {
- $filter = array();
- $filter['category'] = $category;
- } else {
- $filter['category'] = $category;
- }
-
- return $this->getItemsTotal($filter, $check);
- }
-
- function updateItem($items_id, $data_array) {
- //$data_array[$this->prefix . 'fulltext'] = $this->_filter_content($data_array[$this->prefix . 'fulltext']);
- $data_array = func_db_input($data_array);
- $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->prefix . "id = '" . (int)$items_id . "'");
- }
-
- function updateItemCategory($items_id, $categories_id) {
- return $this->adb->update("update " . $this->table_to_category . " set categories_id = '" . (int)$categories_id . "' where " . $this->prefix . "id = '" . (int)$items_id . "'");
- }
-
- function updateItemGroup($items_id, $groups_id) {
- return $this->adb->update("update " . $this->table_to_category . " set groups_id = '" . (int)$groups_id . "' where " . $this->prefix . "id = '" . (int)$items_id . "'");
- }
-
- function updateLink($items_id, $items_link) {
- $items_link = func_db_input($items_link);
-
- $query = "select count(*) as count from " . $this->table
- . " where " . $this->prefix . "id != '" . (int)$items_id . "' and " . $this->prefix . "link = '" . $items_link . "'";
- $row = $this->adb->getRow($query);
- if( $row['count'] > 0 ) {
- return -1;
- } else {
- $query = "update " . $this->table . " set " . $this->prefix . "link = '" . $items_link . "' where " . $this->prefix . "id = '" . (int)$items_id . "'";
- $result = $this->adb->query($query);
- }
-
- return 1;
- }
-
- 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->prefix . "id = '" . (int)$items_id . "'");
- }
- } else {
- $query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
- $this->_update($this->table, $data_array, $this->prefix . "id in (" . $query_ids . ")");
- }
- }
- }
-
- function updateItemsF($items_ids, $field, $value = '') {
- if( $field == 'top' ) {
- $query_str = " " . $this->prefix . "top = '1' ";
- } elseif( $field == 'untop' ) {
- $query_str = " " . $this->prefix . "top = '0' ";
- } elseif( $field == 'recommend' ) {
- $query_str = " " . $this->prefix . "recommend = '1' ";
- } elseif( $field == 'unrecommend' ) {
- $query_str = " " . $this->prefix . "recommend = '0' ";
- } elseif( $field == 'public' ) {
- $query_str = " " . $this->prefix . "status = '1' ";
- } elseif( $field == 'unpublic' ) {
- $query_str = " " . $this->prefix . "status = '0' ";
- } elseif( $field == 'statusopen' ) {
- $query_str = " " . $this->prefix . "status = '1' ";
- } elseif( $field == 'statusclose' ) {
- $query_str = " " . $this->prefix . "status = '0' ";
- } elseif( $field == 'level' ) {
- $query_str = " " . $this->prefix . "level = '" . (int)$value . "' ";
- } elseif( $field == 'unlevel' ) {
- $query_str = " " . $this->prefix . "level = '0' ";
- } else {
- return;
- }
- if( is_array($items_ids) ) {
- if( sizeof($items_ids) > 30 ) {
- foreach($items_ids as $k => $items_id) {
- $this->adb->update("update " . $this->table . " set $query_str where " . $this->prefix . "id = '" . (int)$items_id . "'");
- }
- } else {
- $query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
- $this->adb->update("update " . $this->table . " set $query_str where " . $this->prefix . "id in (" . $query_ids . ")");
- }
- }
- }
-
- function updateItemsCategory($items_ids, $categories_id) {
- if( is_array($items_ids) ) {
- if( sizeof($items_ids) > 30 ) {
- foreach($items_ids as $k => $items_id) {
- $this->adb->update("update " . $this->table_to_category . " set categories_id = '" . (int)$categories_id . "' where " . $this->prefix . "id = '" . (int)$items_id . "'");
- }
- } else {
- $query_ids = "'" . implode("', '", array_values($items_ids)) . "'";
- return $this->adb->update("update " . $this->table_to_category . " set categories_id = '" . (int)$categories_id . "' where " . $this->prefix . "id in (" . $query_ids . ")");
- }
- }
- }
-
- function updateCategoryItemstoNewCategory($categories_id, $categories_id_new) {
- $query = "update " . $this->table_to_category . " set categories_id = '" . (int)$categories_id_new . "' "
- . " where categories_id = '" . (int)$categories_id . "'";
-
- return $this->adb->update($query);
- }
-
- function deleteItem($items_id) {
- $query = "delete from " . $this->table . " where " . $this->prefix . "id = '" . (int)$items_id . "'";
- $result = $this->adb->query($query);
-
- $query = "delete from " . $this->table_to_category . " where " . $this->prefix . "id = '" . (int)$items_id . "'";
- $this->adb->query($query);
- }
-
- function deleteItems($items_ids) {
- if( is_array($items_ids) ) {
- $this->_delete_ins($this->table, $this->prefix . 'id', $items_ids);
- }
- }
-
- function deleteCategoryItems($categories_id) {
- $query = "delete pp_contents, " . $this->table_to_category . " from " . $this->table . " c, " . $this->table_to_category . " c2c "
- . " where c." . $this->prefix . "id = c2c." . $this->prefix . "id and c2c.categories_id = '" . (int)$categories_id . "'";
- $this->adb->delete($query);
-
- $this->adb->updateCategoryItemstoNewCategory($categories_id, 0);
- }
-
- 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 getCategoryObj() {
- global $ccObj;
-
- if( is_object($ccObj) && get_class($ccObj) == 'ContentCategoryBase' ) {
- //
- } else {
- include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'content/content.category.class.php');
- $ccObj = new ContentCategoryBase();
- }
-
- return $ccObj;
- }
-
- function getCategoriesIDs($categories_id, $include_subs = true) {
- $_ccObj = $this->getCategoryObj();
-
- if( $include_subs == true ) {
- $categories_ids = $_ccObj->getCategoriesAllSubsIDs($categories_id);
- } else {
- $categories_ids = $_ccObj->getCategoriesSubsIDs($categories_id);
- }
-
- return $categories_ids;
- }
-
- //private
- function _get_query_str($filter = '', $check = 0) {
- $query_category = 0;
- $query_str = "";
- if( $check == -1 ) {
- $query_str .= "";
- } else {
- $query_str .= " and c." . $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( is_array($this->filters) ) {
- foreach($this->filters as $k => $v) {
- $filter[$k] = $v;
- }
- }
- //
- 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 .= " " . $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'] . "%' ";
- } else {
- $query_str .= " and c." . $this->prefix . "subject like '%" . $filter['keyword'] . "%' ";
- }
- }
- if( isset($filter['ids']) && $this->_not_null($filter['ids']) == true ) {
- if( is_array($filter['ids']) ) {
- $query_str .= " and c." . $this->prefix . "id in (" . $this->_implode(', ', $filter['ids']) . ") ";
- } else {
- $query_str .= " and c." . $this->prefix . "id = '" . (int)$filter['ids'] . "' ";
- }
- }
- if( isset($filter['parent']) && $this->_not_null($filter['parent']) == true ) {
- if( is_array($filter['parent']) ) {
- $query_str .= " and c." . $this->prefix . "parent in (" . $this->_implode(', ', $filter['parent']) . ") ";
- } else {
- $query_str .= " and c." . $this->prefix . "parent = '" . (int)$filter['parent'] . "' ";
- }
- }
- if( $this->table_to_category != '' ) {
- if( isset($filter['category']) ) {
- $query_category = 1;
- if( is_array($filter['category']) ) {
- $query_str .= " and c." . $this->prefix . "id = c2c." . $this->prefix . "id and c2c.categories_id in (" . $this->_implode(', ', $filter['category']) . ") ";
- } else {
- $query_str .= " and c." . $this->prefix . "id = c2c." . $this->prefix . "id and c2c.categories_id = '" . (int)$filter['category'] . "' ";
- }
- }
- if( isset($filter['nocategory']) && $filter['nocategory'] > 0 ) {
- $query_str .= " and and c." . $this->prefix . "id = c2c." . $this->prefix . "id and c2c.categories_id = '0' ";
- }
- } else {
- if( isset($filter['category']) ) {
- if( is_array($filter['category']) ) {
- $query_str .= " and categories_id in (" . $this->_implode(', ', $filter['category']) . ") ";
- } else {
- $query_str .= " and categories_id = '" . (int)$filter['category'] . "' ";
- }
- }
- if( isset($filter['nocategory']) && $filter['nocategory'] > 0 ) {
- $query_str .= " and categories_id = '0' ";
- }
- }
- if( is_array($this->filter_fields) && sizeof($this->filter_fields) > 0 ) {
- $qt = $this->_get_query_type();
- foreach($filter as $k => $v) {
- if( is_array($this->filter_map) && isset($this->filter_map[$k]) ) {
- $k = $this->filter_map[$k];
- }
- if( in_array($k, $this->filter_fields) ) {
- $t = '';
- $p = $this->prefix;
- if( strpos($k, ':') !== false ) {
- $ks = explode(':', $k);
- $k = $ks['0'];
- $t = $ks['1'];
- if( sizeof($ks) > 2 ) {
- $p = $ks['2'];
- }
- }
- if( substr($k, 0, 1) == '=' ) {
- $k = substr($k, 1);
- $p = '';
- }
- if( !in_array($t, $qt) ) {
- $t = '';
- }
- if( in_array($t, array('=', '>', '>=', '<', '<=', '!=')) ) {
- if( $v === 'now()' || $v === 'now' ) {
- $query_str .= " and " . $p . $k . " " . $t . " now() ";
- } else {
- $query_str .= " and " . $p . $k . " " . $t . " '" . $v . "' ";
- }
- } elseif( in_array($t, array('[]', '[)', '(]', '()')) ) {
- if( is_array($v) ) {
- $t1 = substr($t, 0, 1);
- $t2 = substr($t, 1, 1);
- if( $t1 == '[' ) {
- $t1 = '<=';
- } elseif( $t1 == '(' ) {
- $t1 = '<';
- }
- if( $t2 == '[' ) {
- $t2 = '<=';
- } elseif( $t1 == '(' ) {
- $t2 = '<';
- }
-
- $query_str .= " and " . $p . $k . " " . $t1 . " '" . $v['0'] . "' ";
- $query_str .= " and " . $p . $k . " " . $t2 . " '" . $v['1'] . "' ";
- }
- } elseif( in_array($t, array('like', 'notlike', '%like%', '%like', 'like%',)) ) {
- if( $t == 'notlike' ) {
- $query_str .= " and " . $p . $k . " not like '%" . $v . "%' ";
- } else {
- $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
- }
- } else {
- if( $v === 'now()' || $v === 'now' ) {
- $query_str .= " and " . $p . $k . " < now() ";
- } elseif( is_array($v) && sizeof($v) > 0 ) {
- $query_str .= " and " . $p . $k . " in ('" . implode("','", $v) . "') ";
- } elseif( is_int($v) ) {
- $query_str .= " and " . $p . $k . " = '" . $v . "' ";
- } elseif( is_numeric($v) ) {
- $query_str .= " and " . $p . $k . " = '" . $v . "' ";
- } elseif( is_string($v) ) {
- $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
- } else {
- $query_str .= " and " . $p . $k . " like '%" . $v . "%' ";
- }
- }
- }
- }
- }
- }
- //
- $query_str .= $this->_get_query_str_custom($filter, $check);
-
- return array($query_str, $query_category);
- }
-
- function _get_query_str_custom($filter = '', $check = 0) {
- return "";
- }
-
- function _get_query_order_sort($order_by = '', $order_sort = '') {
- if( $order_by == '' ) {
- $order_by = $this->query_order_by;
- }
- if( $order_sort == '' ) {
- $order_sort = $this->query_order_sort;
- }
- if( $order_by != '' && $order_sort != '' ) {
- if( $order_by == 'dateadded' ) {
- $order_by = 'date_added';
- } elseif( $order_by == 'sortorder' ) {
- $order_by = 'sort_order';
- }
- //
- $sort_query = array();
- if( !is_array($this->sort_query) && is_array($this->sort_fields) ) {
- foreach($this->sort_fields as $k => $v) {
- if( substr($v, -1, 1) == '+' || substr($v, -1, 1) == '-' ) {
- $sort_query[] = substr($v, 0, -1);
- }
- }
- } else {
- $sort_query = $this->sort_query;
- }
- if( is_array($sort_query) && in_array($order_by, $sort_query) ) {
- $sort_fields = array($order_by . ($order_sort == 'desc' ? '+' : '-'));
- $this->sort_fields = $sort_fields;
- }
- }
- }
-
- function _query_owner_rt($pf = '') {
- 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;
- }
- if( $pf != '' ) {
- $p = $pf . '.' . $p;
- }
-
- return $this->_query_owner($p . $this->_table_owner_field);
- }
-
- return " 1 ";
- }
-
- function _get_query_type() {
- $query_type = array(
- '=', '>', '>=', '<', '<=', '!=',
- '[]', '()', '[)', '(]',
- 'like', 'notlike', '%like%', '%like', 'like%',
- 'regexp', 'notregexp',
- );
- return $query_type;
- }
-
- //
- function _generate_item_link($items_id, $items_name = '', $items_link = '') {
- if( $items_link == '' && $items_name != '' ) {
- $items_link = $this->_filter_link($items_name);
- }
- if( $this->_not_null($items_link) ) {
- $items_link = func_db_input($items_link);
- $query = "select count(*) as count from " . $this->table . " where " . $this->prefix . "id != '" . (int)$items_id . "' "
- . " and " . $this->prefix . "link = '" . $items_link . "'";
- $count = $this->adb->getCount($query);
- if( $count == 0 ) {
- $this->updateItem($items_id, array($this->prefix . 'link' => $items_link));
- return true;
- }
- }
- $items_link = $this->_filter_link($items_name);
- $items_link = $this->_rand_link($items_id, $items_link);
-
- $this->_generate_item_link($items_id, $items_name, $items_link);
-
- return true;
- }
-
- function _generate_item_link_datecount($items_id, $page = '1') {
- $query = "select count(*) as count from " . $this->table . " c1, " . $this->table . " c2 "
- . " where TO_DAYS(c1." . $this->prefix . "date_added) = TO_DAYS(c2." . $this->prefix . "date_added) "
- . " and c1." . $this->prefix . "id != c2." . $this->prefix . "id and c2." . $this->prefix . "id = '" . (int)$items_id . "'";
- $count = $this->adb->getCount($query);
-
- $link = strftime("%Y%m%d", time()) . $this->AlignLength($count, 4);
- if( $page > 1 ) {
- $link .= '_' . $link;
- }
-
- return $link;
- }
-
- function _update_item_link_datecount($items_id, $page = '1') {
- $link = $this->_generate_item_link_datecount($items_id, $page);
-
- $this->updateItem($items_id, array($this->prefix . 'link' => $link));
- }
-
- //
- function _filter_content($content) {
- $a = func_filter_attach_convert($content);
-
- return $a['0'];
- }
- }
- //
- ?>