osclass / Osclass
Code Issues 84 Pull requests 20 Projects 0 Wiki Pulse
oc-includes/osclass/model/Search.php
<?php if ( !defined('ABS_PATH') ) exit('ABS_PATH is not loaded. Direct access is not allowed.');
/*
* Copyright 2014 Osclass
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
*
*/
class Search extends DAO
{
/**
*
* @var type
*/
private $conditions;
private $itemConditions;
private $tables;
private $tables_join; // ?
private $sql;
private $order_column;
private $order_direction;
private $limit_init;
private $results_per_page;
private $cities;
private $city_areas;
private $regions;
private $countries;
private $categories;
private $search_fields;
private $total_results;
private $total_results_table;
private $sPattern;
private $sEmail;
private $groupBy;
private $having;
private $locale_code;
private $withPattern;
private $withPicture;
private $withLocations;
private $withCategoryId;
private $withUserId;
private $withItemId;
private $withNoUserEmail;
private $onlyPremium;
private $price_min;
private $price_max;
private $user_ids;
private $itemId;
private $userTableLoaded;
private static $instance;
public static function newInstance()
{
if( !self::$instance instanceof self ) {
self::$instance = new self;
}
return self::$instance;
}
/**
*
*/
function __construct($expired = false)
{
parent::__construct();
$this->setTableName('t_item');
$this->setFields( array('pk_i_id') );
$this->withPattern = false;
$this->withLocations = false;
$this->withCategoryId = false;
$this->withUserId = false;
$this->withPicture = false;
$this->withNoUserEmail = false;
$this->onlyPremium = false;
$this->price_min = null;
$this->price_max = null;
$this->user_ids = null;
$this->itemId = null;
$this->userTableLoaded = false;
$this->city_areas = array();
$this->cities = array();
$this->regions = array();
$this->countries = array();
$this->categories = array();
$this->conditions = array();
$this->tables = array();
$this->tables_join = array();
$this->search_fields = array();
$this->itemConditions = array();
$this->locale_code = array();
$this->groupBy = '';
$this->having = '';
$this->order();
$this->limit();
$this->results_per_page = 10;
if(!$expired) {
// t_item
$this->addItemConditions(sprintf("%st_item.b_enabled = 1 ", DB_TABLE_PREFIX));
$this->addItemConditions(sprintf("%st_item.b_active = 1 ", DB_TABLE_PREFIX));
$this->addItemConditions(sprintf("%st_item.b_spam = 0", DB_TABLE_PREFIX));
$this->addItemConditions(sprintf("(%st_item.b_premium = 1 || %st_item.dt_expiration >= '%s')", DB_TABLE_PREFIX, DB_TABLE_PREFIX, date('Y-m-d H:i:s')) );
}
$this->total_results = null;
$this->total_results_table = null;
// get all item_location data
if(OC_ADMIN) {
$this->addField(sprintf('%st_item_location.*', DB_TABLE_PREFIX) );
}
}
/**
* Return an array with columns allowed for sorting
*
* @return array
*/
public static function getAllowedColumnsForSorting()
{
return( array('i_price', 'dt_pub_date', 'dt_expiration') );
}
/**
* Return an array with type of sorting
*
* @return array
*/
public static function getAllowedTypesForSorting()
{
return ( array (0 => 'asc', 1 => 'desc') );
}
// juanramon: little hack to get alerts work in search layout
public function reconnect()
{
// $this->conn = getConnection();
}
/**
* Add conditions to the search
*
* @access public
* @since unknown
* @param mixed $conditions
*/
public function addConditions($conditions)
{
if(is_array($conditions)) {
foreach($conditions as $condition) {
$condition = trim($condition);
if($condition!='') {
if(!in_array($condition, $this->conditions)) {
$this->conditions[] = $condition;
}
}
}
} else {
$conditions = trim($conditions);
if($conditions!='') {
if(!in_array($conditions, $this->conditions)) {
$this->conditions[] = $conditions;
}
}
}
}
/**
* Add item conditions to the search
*
* @access public
* @since unknown
* @param mixed $conditions
*/
public function addItemConditions($conditions)
{
if(is_array($conditions)) {
foreach($conditions as $condition) {
$condition = trim($condition);
if($condition!='') {
if(!in_array($condition, $this->itemConditions)) {
$this->itemConditions[] = $condition;
}
}
}
} else {
$conditions = trim($conditions);
if($conditions!='') {
if(!in_array($conditions, $this->itemConditions)) {
$this->itemConditions[] = $conditions;
}
}
}
}
/**
* Add locale conditions to the search
*
* @access public
* @since 3.2
* @param string $locale
*/
public function addLocale($locale)
{
if(is_array($locale)) {
foreach($locale as $l) {
if($l!='') {
$this->locale_code[$l] = $l;
}
}
} else {
if($locale!='') {
$this->locale_code[$locale] = $locale;
}
}
}
/**
* Add new fields to the search
*
* @access public
* @since unknown
* @param mixed $fields
*/
public function addField($fields)
{
if(is_array($fields)) {
foreach($fields as $field) {
$field = trim($field);
if($field!='') {
if(!in_array($field, $this->fields)) {
$this->search_fields[] = $field;
}
}
}
} else {
$fields = trim($fields);
if($fields!='') {
if(!in_array($fields, $this->fields)) {
$this->search_fields[] = $fields;
}
}
}
}
/**
* Add extra table to the search
*
* @access public
* @since unknown
* @param mixed $tables
*/
public function addTable($tables)
{
if(is_array($tables)) {
foreach($tables as $table) {
$table = trim($table);
if($table!='') {
if(!in_array($table, $this->tables)) {
$this->tables[] = $table;
}
}
}
} else {
$tables = trim($tables);
if($tables!='') {
if(!in_array($tables, $this->tables)) {
$this->tables[] = $tables;