Source for file _itemquery.class.php
Documentation is available at _itemquery.class.php
* This file implements the ItemQuery class.
* This file is part of the evoCore framework - {@link http://evocore.net/}
* See also {@link http://sourceforge.net/projects/evocms/}.
* @copyright (c)2003-2006 by Francois PLANQUE - {@link http://fplanque.net/}
* {@internal License choice
* - If you have received this file as part of a package, please find the license.txt file in
* the same folder or the closest folder above for complete license terms.
* - If you have received this file individually (e-g: from http://cvs.sourceforge.net/viewcvs.py/evocms/)
* then you must choose one of the following licenses before using the file:
* - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
* - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
* {@internal Open Source relicensing agreement:
* {@internal Below is a list of authors who have contributed to design/coding of this file: }}
* @author fplanque: Francois PLANQUE.
* @version $Id: _itemquery.class.php,v 1.7.2.4 2007/01/30 20:15:13 blueyed Exp $
if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
require_once dirname(__FILE__
).
'/../../_misc/_sql.class.php';
* ItemQuery: help constructing queries on Items
* @param string Name of table in database
* @param string Prefix of fields in the table
* @param string Name of the ID field (including prefix)
function ItemQuery( $dbtablename, $dbprefix =
'', $dbIDname )
$this->dbtablename =
$dbtablename;
$this->dbprefix =
$dbprefix;
$this->dbIDname =
$dbIDname;
$this->FROM( $this->dbtablename.
' INNER JOIN T_postcats ON '.
$this->dbIDname.
' = postcat_post_ID
INNER JOIN T_categories ON postcat_cat_ID = cat_ID ' );
* Restrict to a specific post
function where_ID( $p =
'', $title =
'' )
// if a post number is specified, load that post
// if a post urltitle is specified, load that post
$this->WHERE_and( $this->dbprefix.
'urltitle = '.
$DB->quote($title) );
* Restrict to specific collection/chapters (blog/categories)
* @todo get rid of blog #1
* @param string List of cats to restrict to
* @param array Array of cats to restrict to
global $cat_array; // this is required for the cat_req() callback in compile_cat_array()
$blog =
intval($blog); // Extra security
// Save for future use (permission checks..)
{ // Not Special case where we aggregate all blogs
// Compile the real category list to use:
// TODO: allow to pass the compiled vars directly to this class
compile_cat_array( $cat, $catsel, /* by ref */ $cat_array, /* by ref */ $cat_modifier, $blog ==
1 ?
0 :
$blog );
if( ! empty($cat_array) )
{ // We want to restict to some cats:
if( $cat_modifier ==
'-' )
$whichcat =
'postcat_cat_ID '.
$eq.
' ('.
implode(',', $cat_array).
') ';
if( $cat_modifier ==
'*' )
{ // We want the categories combined! (i-e posts must be in ALL requested cats)
$this->GROUP_BY( $this->dbIDname.
' HAVING COUNT(postcat_cat_ID) = '.
count($cat_array) );
* Restrict to specific collection/chapters (blog/categories)
* @todo get rid of blog #1
// Save for future use (permission checks..)
{ // Not Special case where we aggregate all blogs
$this->WHERE_and( 'cat_blog_ID = '.
$blog_ID );
if( ! empty($cat_array) )
{ // We want to restict to some cats:
if( $cat_modifier ==
'-' )
$whichcat =
'postcat_cat_ID '.
$eq.
' ('.
implode(',', $cat_array).
') ';
if( $cat_modifier ==
'*' )
{ // We want the categories combined! (i-e posts must be in ALL requested cats)
$this->GROUP_BY( $this->dbIDname.
' HAVING COUNT(postcat_cat_ID) = '.
count($cat_array) );
* Restrict to the visibility/sharing statuses we want to show
* @param array Restrict to these statuses
if( !isset
( $this->blog ) )
debug_die( 'Status restriction requires to work with aspecific blog first.' );
* Restrict to specific authors
* @param string List of authors to restrict to (must have been previously validated)
if( substr( $author, 0, 1 ) ==
'-' )
{ // List starts with MINUS sign:
$author_list =
substr( $author, 1 );
$this->WHERE_and( $this->dbprefix.
'creator_user_ID '.
$eq.
' ('.
$author_list.
')' );
* Restrict to specific assignees
* @param string List of assignees to restrict to (must have been previously validated)
if( empty( $assignees ) )
{ // List is ONLY a MINUS sign (we want only those not assigned)
$this->WHERE_and( $this->dbprefix.
'assigned_user_ID IS NULL' );
elseif( substr( $assignees, 0, 1 ) ==
'-' )
{ // List starts with MINUS sign:
$this->WHERE_and( '( '.
$this->dbprefix.
'assigned_user_ID IS NULL
OR '.
$this->dbprefix.
'assigned_user_ID NOT IN ('.
substr( $assignees, 1 ).
') )' );
$this->WHERE_and( $this->dbprefix.
'assigned_user_ID IN ('.
$assignees.
')' );
* Restrict to specific assignee or author
* @param integer assignee or author to restrict to (must have been previously validated)
$this->author_assignee =
$author_assignee;
if( empty( $author_assignee ) )
$this->WHERE_and( '( '.
$this->dbprefix.
'creator_user_ID = '.
$author_assignee.
' OR '.
$this->dbprefix.
'assigned_user_ID = '.
$author_assignee.
' )' );
* Restrict to specific (exetnded) statuses
* @param string List of assignees to restrict to (must have been previously validated)
{ // List is ONLY a MINUS sign (we want only those not assigned)
$this->WHERE_and( $this->dbprefix.
'pst_ID IS NULL' );
elseif( substr( $statuses, 0, 1 ) ==
'-' )
{ // List starts with MINUS sign:
$this->WHERE_and( '( '.
$this->dbprefix.
'pst_ID IS NULL
OR '.
$this->dbprefix.
'pst_ID NOT IN ('.
substr( $statuses, 1 ).
') )' );
$this->WHERE_and( $this->dbprefix.
'pst_ID IN ('.
$statuses.
')' );
* Restricts to a specific date range. (despite thje 'start' in the name
* @see ItemList2::get_advertised_start_date()
* @param string YYYYMMDDHHMMSS (everything after YYYY is optional) or ''
* @param integer week number or ''
* @param string YYYYMMDDHHMMSS to start at, '' for first available
* @param string YYYYMMDDHHMMSS to stop at
* @param mixed Do not show posts before this timestamp, can be 'now'
* @param mixed Do not show posts after this timestamp, can be 'now'
function where_datestart( $m =
'', $w =
'', $dstart =
'', $dstop =
'', $timestamp_min =
'', $timestamp_max =
'now' )
// if a start date is specified in the querystring, crop anything before
// Add trailing 0s: YYYYMMDDHHMMSS
$dstart0 =
$dstart.
'00000000000000'; // TODO: this is NOT correct, should be 0101 for month
$dstart_mysql =
substr($dstart0,0,4).
'-'.
substr($dstart0,4,2).
'-'.
substr($dstart0,6,2).
' '
$this->WHERE_and( $this->dbprefix.
'datestart >= \''.
$dstart_mysql.
'\'
OR ( '.
$this->dbprefix.
'datedeadline IS NULL AND '.
$this->dbprefix.
'datestart >= \''.
$dstart_mysql.
'\' )' );
// if a stop date is specified in the querystring, crop anything before
// We have only year, add one to year
$dstop_mysql =
($dstop+
1).
'-01-01 00:00:00';
// We have year month, add one to month
$dstop_mysql =
date("Y-m-d H:i:s ", mktime(0, 0, 0, substr($dstop,4,2)+
1, 01, substr($dstop,0,4)));
// We have year mounth day, add one to day
$dstop_mysql =
date("Y-m-d H:i:s ", mktime(0, 0, 0, substr($dstop,4,2), (substr($dstop,6,2) +
1 ), substr($dstop,0,4)));
// We have year mounth day hour, add one to hour
$dstop_mysql =
date("Y-m-d H:i:s ", mktime( ( substr($dstop,8,2) +
1 ), 0, 0, substr($dstop,4,2), substr($dstop,6,2), substr($dstop,0,4)));
// We have year mounth day hour minute, add one to minute
$dstop_mysql =
date("Y-m-d H:i:s ", mktime( substr($dstop,8,2), ( substr($dstop,8,2) +
1 ), 0, substr($dstop,4,2), substr($dstop,6,2), substr($dstop,0,4)));
$this->WHERE_and( $this->dbprefix.
'datestart < \''.
$dstop_mysql.
'\'' ); // NOT <= comparator because we compare to the superior stop date
if( !$start_is_set ||
!$stop_is_set )
if( !is_null($w) &&
$w >=
0 // Note: week # can be 0
{ // If a week number is specified (with a year)
// Note: we use PHP to calculate week boundaries in order to handle weeks
// that overlap 2 years properly, even when start on week is monday (which MYSQL won't handle properly)
$this->WHERE_and( $this->dbprefix.
'datestart >= "'.
date('Y-m-d',$start_date_for_week).
'"' );
$this->WHERE_and( $this->dbprefix.
'datestart < "'.
date('Y-m-d',$start_date_for_week+
604800 ).
'"' ); // + 7 days
{ // We want to restrict on an interval:
if( $timestamp_min ==
'now' )
if( !empty($timestamp_min) )
// echo 'hide before '.$timestamp_min;
$date_min =
date('Y-m-d H:i:s', $timestamp_min +
$time_difference );
$this->WHERE_and( $this->dbprefix.
'datestart >= \''.
$date_min.
'\'' );
if( $timestamp_max ==
'now' )
if( !empty($timestamp_max) )
$date_max =
date('Y-m-d H:i:s', $timestamp_max +
$time_difference );
$this->WHERE_and( $this->dbprefix.
'datestart <= \''.
$date_max.
'\'' );
* @param string Keyword search string
* @param mixed Search for entire phrase or for individual words
* @param mixed Require exact match of title or contents
{ // We want exact match of title or contents
{ // The words/sentence are/is to be included in in the title or the contents
if( ($phrase ==
'1') or ($phrase ==
'sentence') )
$keywords =
$DB->escape(trim($keywords));
$search .=
'('.
$this->dbprefix.
'title LIKE \''.
$n.
$keywords.
$n.
'\') OR ('.
$this->dbprefix.
'content LIKE \''.
$n.
$keywords.
$n.
'\')';
// puts spaces instead of commas
$keywords =
trim($keywords);
$keyword_array =
explode(' ',$keywords);
for ( $i =
0; $i <
count($keyword_array); $i++
)
$search .=
' '.
$join.
' ( ('.
$this->dbprefix.
'title LIKE \''.
$n.
$DB->escape($keyword_array[$i]).
$n.
'\')
OR ('.
$this->dbprefix.
'content LIKE \''.
$n.
$DB->escape($keyword_array[$i]).
$n.
'\') ) ';
* $Log: _itemquery.class.php,v $
* Revision 1.7.2.4 2007/01/30 20:15:13 blueyed
* MFH: Fixed inclusion of sub-categories in item list (e.g. ?cat=1)
* Revision 1.7.2.3 2006/11/21 17:43:40 fplanque
* Fixed displaying of yearly archives
* Revision 1.7.2.2 2006/11/04 19:54:54 fplanque
* Reinjected old Log blocks. Removing them from CVS was a bad idea -- especially since Daniel has decided branch 1.9 was his HEAD...
* Revision 1.7 2006/07/02 21:53:31 blueyed
* time difference as seconds instead of hours; validate user#1 on upgrade; bumped new_db_version to 9300.
* Revision 1.6 2006/06/19 20:59:37 fplanque
* noone should die anonymously...
* Revision 1.5 2006/06/19 16:58:11 fplanque
* Revision 1.4 2006/06/13 21:49:15 blueyed
* Revision 1.3 2006/04/19 20:13:50 fplanque
* do not restrict to :// (does not catch subdomains, not even www.)
* Revision 1.2 2006/03/12 23:08:59 fplanque
* Revision 1.1 2006/02/23 21:11:58 fplanque
* File reorganization to MVC (Model View Controller) architecture.
* See index.hml files in folders.
* (Sorry for all the remaining bugs induced by the reorg... :/)
* Revision 1.12 2006/02/10 22:08:07 fplanque
* Revision 1.11 2006/02/03 21:58:05 fplanque
* Too many merges, too little time. I can hardly keep up. I'll try to check/debug/fine tune next week...
* Revision 1.10 2006/01/04 20:34:52 fplanque
* allow filtering on extra statuses
* Revision 1.9 2006/01/04 19:07:48 fplanque
* allow filtering on assignees
* Revision 1.8 2005/12/21 20:39:04 fplanque
* Revision 1.7 2005/12/19 19:30:14 fplanque
* Revision 1.6 2005/12/19 18:10:18 fplanque
* Normalized the exp and tracker tabs.
* Revision 1.5 2005/12/05 18:17:19 fplanque
* Added new browsing features for the Tracker Use Case.
* Revision 1.4 2005/09/06 19:38:29 fplanque
* Revision 1.3 2005/09/06 17:13:55 fplanque
* stop processing early if referer spam has been detected
* Revision 1.2 2005/09/01 17:11:46 fplanque
* Revision 1.1 2005/08/31 19:08:51 fplanque
* Factorized Item query WHERE clause.
* Fixed calendar contextual accuracy.