b2evolution PHP Cross Reference Blogging Systems

Source: /inc/items/model/_itemquery.class.php - 826 lines - 21887 bytes - Summary - Text - Print

Description: 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/}.

   1  <?php
   2  /**
   3   * This file implements the ItemQuery class.
   4   *
   5   * This file is part of the evoCore framework - {@link http://evocore.net/}
   6   * See also {@link http://sourceforge.net/projects/evocms/}.
   7   *
   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   9   *
  10   * {@internal License choice
  11   * - If you have received this file as part of a package, please find the license.txt file in
  12   *   the same folder or the closest folder above for complete license terms.
  13   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  14   *   then you must choose one of the following licenses before using the file:
  15   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  16   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  17   * }}
  18   *
  19   * {@internal Open Source relicensing agreement:
  20   * }}
  21   *
  22   * @package evocore
  23   *
  24   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  25   * @author fplanque: Francois PLANQUE.
  26   *
  27   * @version $Id: _itemquery.class.php 6136 2014-03-08 07:59:48Z manuel $
  28   */
  29  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  30  
  31  load_class( '_core/model/db/_sql.class.php', 'SQL' );
  32  
  33  /**
  34   * ItemQuery: help constructing queries on Items
  35   * @package evocore
  36   */
  37  class ItemQuery extends SQL
  38  {
  39      var $p;
  40      var $pl;
  41      var $title;
  42      var $blog;
  43      var $cat;
  44      var $catsel;
  45      var $show_statuses;
  46      var $tags;
  47      var $author;
  48      var $author_login;
  49      var $assignees;
  50      var $assignees_login;
  51      var $statuses;
  52      var $types;
  53      var $dstart;
  54      var $dstop;
  55      var $timestamp_min;
  56      var $timestamp_max;
  57      var $keywords;
  58      var $phrase;
  59      var $exact;
  60      var $featured;
  61  
  62  
  63      /**
  64       * Constructor.
  65       *
  66       * @param string Name of table in database
  67       * @param string Prefix of fields in the table
  68       * @param string Name of the ID field (including prefix)
  69       */
  70  	function ItemQuery( $dbtablename, $dbprefix = '', $dbIDname )
  71      {
  72          $this->dbtablename = $dbtablename;
  73          $this->dbprefix = $dbprefix;
  74          $this->dbIDname = $dbIDname;
  75  
  76          $this->FROM( $this->dbtablename );
  77      }
  78  
  79  
  80      /**
  81       * Restrict to a specific post
  82       */
  83  	function where_ID( $p = '', $title = '' )
  84      {
  85          $r = false;
  86  
  87          $this->p = $p;
  88          $this->title = $title;
  89  
  90          // if a post number is specified, load that post
  91          if( !empty($p) )
  92          {
  93              if( substr( $this->p, 0, 1 ) == '-' )
  94              {    // Starts with MINUS sign:
  95                  $eq_p = ' <> ';
  96                  $this->p = substr( $this->p, 1 );
  97              }
  98              else
  99              {
 100                  $eq_p = ' = ';
 101              }
 102  
 103              $this->WHERE_and( $this->dbIDname.$eq_p.intval($this->p) );
 104              $r = true;
 105          }
 106  
 107          // if a post urltitle is specified, load that post
 108          if( !empty( $title ) )
 109          {
 110              if( substr( $this->title, 0, 1 ) == '-' )
 111              {    // Starts with MINUS sign:
 112                  $eq_title = ' <> ';
 113                  $this->title = substr( $this->title, 1 );
 114              }
 115              else
 116              {
 117                  $eq_title = ' = ';
 118              }
 119  
 120              global $DB;
 121              $this->WHERE_and( $this->dbprefix.'urltitle'.$eq_title.$DB->quote($this->title) );
 122              $r = true;
 123          }
 124  
 125          return $r;
 126      }
 127  
 128  
 129      /**
 130       * Restrict to a specific list of posts
 131       */
 132  	function where_ID_list( $pl = '' )
 133      {
 134          $r = false;
 135  
 136          $this->pl = $pl;
 137  
 138          if( empty( $pl ) ) return $r; // nothing to do
 139  
 140          if( substr( $this->pl, 0, 1 ) == '-' )
 141          {    // List starts with MINUS sign:
 142              $eq = 'NOT IN';
 143              $this->pl = substr( $this->pl, 1 );
 144          }
 145          else
 146          {
 147              $eq = 'IN';
 148          }
 149  
 150          $p_ID_array = array();
 151          $p_id_list = explode( ',', $this->pl );
 152          foreach( $p_id_list as $p_id )
 153          {
 154              $p_ID_array[] = intval( $p_id );// make sure they're all numbers
 155          }
 156  
 157          $this->pl = implode( ',', $p_ID_array );
 158  
 159          $this->WHERE_and( $this->dbIDname.' '.$eq.'( '.$this->pl.' )' );
 160          $r = true;
 161  
 162          return $r;
 163      }
 164  
 165  
 166      /**
 167       * Restrict to specific collection/chapters (blog/categories)
 168       *
 169       * @param integer
 170       * @param string List of cats to restrict to
 171       * @param array Array of cats to restrict to
 172       */
 173  	function where_chapter( $blog, $cat = '', $catsel = array() )
 174      {
 175          global $cat_array; // this is required for the cat_req() callback in compile_cat_array()
 176  
 177          $blog = intval($blog);    // Extra security
 178  
 179          // Save for future use (permission checks..)
 180          $this->blog = $blog;
 181  
 182          $this->FROM_add( 'INNER JOIN T_postcats ON '.$this->dbIDname.' = postcat_post_ID
 183                                              INNER JOIN T_categories ON postcat_cat_ID = cat_ID' );
 184  
 185          $BlogCache = & get_BlogCache();
 186          $current_Blog = $BlogCache->get_by_ID( $blog );
 187  
 188          $this->WHERE_and( $current_Blog->get_sql_where_aggregate_coll_IDs('cat_blog_ID') );
 189  
 190  
 191          $cat_array = NULL;
 192          $cat_modifier = NULL;
 193  
 194          // Compile the real category list to use:
 195          // TODO: allow to pass the compiled vars directly to this class
 196          compile_cat_array( $cat, $catsel, /* by ref */ $cat_array, /* by ref */ $cat_modifier, /* TODO $blog == 1 ? 0 : */ $blog );
 197  
 198          if( ! empty($cat_array) )
 199          {    // We want to restict to some cats:
 200              global $DB;
 201  
 202              if( $cat_modifier == '-' )
 203              {
 204                  $eq = 'NOT IN';
 205              }
 206              else
 207              {
 208                  $eq = 'IN';
 209              }
 210              $whichcat = 'postcat_cat_ID '. $eq.' ('.$DB->quote( $cat_array ). ') ';
 211  
 212              // echo $whichcat;
 213              $this->WHERE_and( $whichcat );
 214  
 215              if( $cat_modifier == '*' )
 216              { // We want the categories combined! (i-e posts must be in ALL requested cats)
 217                  $this->GROUP_BY( $this->dbIDname.' HAVING COUNT(postcat_cat_ID) = '.count($cat_array) );
 218              }
 219          }
 220      }
 221  
 222  
 223      /**
 224       * Restrict to specific collection/chapters (blog/categories)
 225       *
 226       * @param Blog
 227       * @param array
 228       * @param string
 229       * @param string 'wide' to search in extra cats too
 230       *               'main' for main cat only
 231       *               'extra' for extra cats only
 232       */
 233  	function where_chapter2( & $Blog, $cat_array, $cat_modifier, $cat_focus = 'wide' )
 234      {
 235          // Save for future use (permission checks..)
 236          $this->blog = $Blog->ID;
 237          $this->Blog = $Blog;
 238          $this->cat_array = $cat_array;
 239          $this->cat_modifier = $cat_modifier;
 240  
 241          if( $cat_focus == 'wide' || $cat_focus == 'extra' )
 242          {
 243              $sql_join_categories = ( $cat_focus == 'extra' ) ? ' AND post_main_cat_ID != cat_ID' : '';
 244              $this->FROM_add( 'INNER JOIN T_postcats ON '.$this->dbIDname.' = postcat_post_ID
 245                                                  INNER JOIN T_categories ON postcat_cat_ID = cat_ID'.$sql_join_categories );
 246              // fp> we try to restrict as close as possible to the posts but I don't know if it matters
 247              $cat_ID_field = 'postcat_cat_ID';
 248          }
 249          else
 250          {
 251              $this->FROM_add( 'INNER JOIN T_categories ON post_main_cat_ID = cat_ID' );
 252              $cat_ID_field = 'post_main_cat_ID';
 253          }
 254  
 255          if( $cat_focus == 'main' )
 256          { // We are requesting a narrow search
 257              $this->WHERE_and( 'cat_blog_ID = '.$Blog->ID );
 258          }
 259          else
 260          {
 261              $this->WHERE_and( $Blog->get_sql_where_aggregate_coll_IDs('cat_blog_ID') );
 262          }
 263  
 264  
 265          if( ! empty($cat_array) )
 266          {    // We want to restict to some cats:
 267              global $DB;
 268  
 269              if( $cat_modifier == '-' )
 270              {
 271                  $eq = 'NOT IN';
 272              }
 273              else
 274              {
 275                  $eq = 'IN';
 276              }
 277              $whichcat = $cat_ID_field.' '.$eq.' ('.$DB->quote( $cat_array ). ') ';
 278  
 279              // echo $whichcat;
 280              $this->WHERE_and( $whichcat );
 281  
 282              if( $cat_modifier == '*' )
 283              { // We want the categories combined! (i-e posts must be in ALL requested cats)
 284                  $this->GROUP_BY( $this->dbIDname.' HAVING COUNT('.$cat_ID_field.') = '.count($cat_array) );
 285              }
 286          }
 287      }
 288  
 289  
 290      /**
 291       * Restrict to the visibility/sharing statuses we want to show
 292       *
 293       * @param array Restrict to these statuses
 294       */
 295  	function where_visibility( $show_statuses )
 296      {
 297          $this->show_statuses = $show_statuses;
 298  
 299          if( !isset( $this->blog ) )
 300          {
 301              debug_die( 'Status restriction requires to work with a specific blog first.' );
 302          }
 303  
 304          $this->WHERE_and( statuses_where_clause( $show_statuses, $this->dbprefix, $this->blog, 'blog_post!', true, $this->author ) );
 305      }
 306  
 307  
 308      /**
 309       * Restrict to the featured/non featured posts if requested
 310       *
 311       * @param boolean|NULL Restrict to featured
 312       */
 313  	function where_featured( $featured = NULL )
 314      {
 315          $this->featured = $featured;
 316  
 317          if( is_null( $this->featured ) )
 318          { // no restriction
 319              return;
 320          }
 321          elseif( !empty( $this->featured ) )
 322          { // restrict to featured
 323              $this->WHERE_and( $this->dbprefix.'featured <> 0' );
 324          }
 325          else
 326          { // restrict to NON featured
 327              $this->WHERE_and( $this->dbprefix.'featured = 0' );
 328          }
 329      }
 330  
 331  
 332      /**
 333       * Restrict to specific tags
 334       *
 335       * @param string List of tags to restrict to
 336       */
 337  	function where_tags( $tags )
 338      {
 339          global $DB;
 340  
 341          $this->tags = $tags;
 342  
 343          if( empty( $tags ) )
 344          {
 345              return;
 346          }
 347  
 348          $tags = explode( ',', $tags );
 349  
 350          $this->FROM_add( 'INNER JOIN T_items__itemtag ON post_ID = itag_itm_ID
 351                                              INNER JOIN T_items__tag ON (itag_tag_ID = tag_ID AND tag_name IN ('.$DB->quote($tags).') )' );
 352      }
 353  
 354  
 355      /**
 356       * Restrict to specific authors by users IDs
 357       *
 358       * @param string List of authors IDs to restrict to (must have been previously validated)
 359       */
 360  	function where_author( $author_IDs )
 361      {
 362          $this->author = $author_IDs;
 363  
 364          if( empty( $this->author ) )
 365          {
 366              return;
 367          }
 368  
 369          if( substr( $this->author, 0, 1 ) == '-' )
 370          { // Exclude the users IF a list starts with MINUS sign:
 371              $eq = 'NOT IN';
 372              $users_IDs = substr( $this->author, 1 );
 373          }
 374          else
 375          { // Include the users:
 376              $eq = 'IN';
 377              $users_IDs = $this->author;
 378          }
 379  
 380          $this->WHERE_and( $this->dbprefix.'creator_user_ID '.$eq.' ( '.$users_IDs.' )' );
 381      }
 382  
 383  
 384      /**
 385       * Restrict to specific authors by users logins
 386       *
 387       * @param string List of authors logins to restrict to (must have been previously validated)
 388       */
 389  	function where_author_logins( $author_logins )
 390      {
 391          $this->author_login = $author_logins;
 392  
 393          if( empty( $this->author_login ) )
 394          {
 395              return;
 396          }
 397  
 398          if( substr( $this->author_login, 0, 1 ) == '-' )
 399          { // Exclude the users IF a list starts with MINUS sign:
 400              $eq = 'NOT IN';
 401              $users_IDs = get_users_IDs_by_logins( substr( $this->author_login, 1 ) );
 402          }
 403          else
 404          { // Include the users:
 405              $eq = 'IN';
 406              $users_IDs = get_users_IDs_by_logins( $this->author_login );
 407          }
 408  
 409          if( ! empty( $users_IDs ) )
 410          {
 411              $this->WHERE_and( $this->dbprefix.'creator_user_ID '.$eq.' ( '.$users_IDs.' )' );
 412          }
 413      }
 414  
 415  
 416      /**
 417       * Restrict to specific assignees by users IDs
 418       *
 419       * @param string List of assignees IDs to restrict to (must have been previously validated)
 420       * @param string List of assignees logins to restrict to (must have been previously validated)
 421       */
 422  	function where_assignees( $assignees, $assignees_logins = '' )
 423      {
 424          $this->assignees = $assignees;
 425  
 426          if( empty( $this->assignees ) )
 427          {
 428              return;
 429          }
 430  
 431          if( $this->assignees == '-' )
 432          {    // List is ONLY a MINUS sign (we want only those not assigned)
 433              $this->WHERE_and( $this->dbprefix.'assigned_user_ID IS NULL' );
 434          }
 435          elseif( substr( $this->assignees, 0, 1 ) == '-' )
 436          {    // List starts with MINUS sign:
 437              $this->WHERE_and( '( '.$this->dbprefix.'assigned_user_ID IS NULL
 438                                OR '.$this->dbprefix.'assigned_user_ID NOT IN ('.substr( $this->assignees, 1 ).') )' );
 439          }
 440          else
 441          {
 442              $this->WHERE_and( $this->dbprefix.'assigned_user_ID IN ('.$this->assignees.')' );
 443          }
 444      }
 445  
 446  
 447      /**
 448       * Restrict to specific assignees by users logins
 449       *
 450       * @param string List of assignees logins to restrict to (must have been previously validated)
 451       */
 452  	function where_assignees_logins( $assignees_logins )
 453      {
 454          $this->assignees_logins = $assignees_logins;
 455  
 456          if( empty( $this->assignees_logins ) )
 457          {
 458              return;
 459          }
 460  
 461          if( $this->assignees_logins == '-' )
 462          {    // List is ONLY a MINUS sign (we want only those not assigned)
 463              $this->WHERE_and( $this->dbprefix.'assigned_user_ID IS NULL' );
 464          }
 465          elseif( substr( $this->assignees_logins, 0, 1 ) == '-' )
 466          {    // List starts with MINUS sign:
 467              $this->WHERE_and( '( '.$this->dbprefix.'assigned_user_ID IS NULL
 468                                OR '.$this->dbprefix.'assigned_user_ID NOT IN ('.get_users_IDs_by_logins( substr( $this->assignees_logins, 1 ) ).') )' );
 469          }
 470          else
 471          {
 472              $this->WHERE_and( $this->dbprefix.'assigned_user_ID IN ('.get_users_IDs_by_logins( $this->assignees_logins ).')' );
 473          }
 474      }
 475  
 476  
 477      /**
 478       * Restrict to specific assignee or author
 479       *
 480       * @param integer assignee or author to restrict to (must have been previously validated)
 481       */
 482  	function where_author_assignee( $author_assignee )
 483      {
 484          $this->author_assignee = $author_assignee;
 485  
 486          if( empty( $author_assignee ) )
 487          {
 488              return;
 489          }
 490  
 491          $this->WHERE_and( '( '.$this->dbprefix.'creator_user_ID = '. $author_assignee.' OR '.
 492                                              $this->dbprefix.'assigned_user_ID = '.$author_assignee.' )' );
 493      }
 494  
 495  
 496      /**
 497       * Restrict to specific locale
 498       *
 499       * @param string locale to restrict to ('all' if you don't want to restrict)
 500       */
 501  	function where_locale( $locale )
 502      {
 503          global $DB;
 504  
 505          if( $locale == 'all' )
 506          {
 507              return;
 508          }
 509  
 510          $this->WHERE_and( $this->dbprefix.'locale LIKE '.$DB->quote($locale.'%') );
 511      }
 512  
 513  
 514      /**
 515       * Restrict to specific (exetnded) statuses
 516       *
 517       * @param string List of assignees to restrict to (must have been previously validated)
 518       */
 519  	function where_statuses( $statuses )
 520      {
 521          $this->statuses = $statuses;
 522  
 523          if( empty( $statuses ) )
 524          {
 525              return;
 526          }
 527  
 528          if( $statuses == '-' )
 529          {    // List is ONLY a MINUS sign (we want only those not assigned)
 530              $this->WHERE_and( $this->dbprefix.'pst_ID IS NULL' );
 531          }
 532          elseif( substr( $statuses, 0, 1 ) == '-' )
 533          {    // List starts with MINUS sign:
 534              $this->WHERE_and( '( '.$this->dbprefix.'pst_ID IS NULL
 535                                OR '.$this->dbprefix.'pst_ID NOT IN ('.substr( $statuses, 1 ).') )' );
 536          }
 537          else
 538          {
 539              $this->WHERE_and( $this->dbprefix.'pst_ID IN ('.$statuses.')' );
 540          }
 541      }
 542  
 543  
 544      /**
 545       * Restrict to specific item types
 546       *
 547       * @param string List of types to restrict to (must have been previously validated)
 548       */
 549  	function where_types( $types )
 550      {
 551          $this->types = $types;
 552  
 553          if( empty( $types ) )
 554          {
 555              return;
 556          }
 557  
 558          if( $types == '-' )
 559          {    // List is ONLY a MINUS sign (we want only those not assigned)
 560              $this->WHERE_and( $this->dbprefix.'ptyp_ID IS NULL' );
 561          }
 562          elseif( substr( $types, 0, 1 ) == '-' )
 563          {    // List starts with MINUS sign:
 564              $this->WHERE_and( '( '.$this->dbprefix.'ptyp_ID IS NULL
 565                                OR '.$this->dbprefix.'ptyp_ID NOT IN ('.substr( $types, 1 ).') )' );
 566          }
 567          else
 568          {
 569              $this->WHERE_and( $this->dbprefix.'ptyp_ID IN ('.$types.')' );
 570          }
 571      }
 572  
 573  
 574      /**
 575       * Restricts the datestart param to a specific date range.
 576       *
 577       * Start date gets restricted to minutes only (to make the query more
 578       * cachable).
 579       *
 580       * Priorities:
 581       *  -dstart and/or dstop
 582       *  -week + m
 583       *  -m
 584       * @todo  -dstart + x days
 585       * @see ItemList2::get_advertised_start_date()
 586       *
 587       * @param string YYYYMMDDHHMMSS (everything after YYYY is optional) or ''
 588       * @param integer week number or ''
 589       * @param string YYYYMMDDHHMMSS to start at, '' for first available
 590       * @param string YYYYMMDDHHMMSS to stop at
 591       * @param mixed Do not show posts before this timestamp, can be 'now'
 592       * @param mixed Do not show posts after this timestamp, can be 'now'
 593       */
 594  	function where_datestart( $m = '', $w = '', $dstart = '', $dstop = '', $timestamp_min = '', $timestamp_max = 'now' )
 595      {
 596          global $time_difference, $DB;
 597  
 598          $this->m = $m;
 599          $this->w = $w;
 600          $this->dstart = $dstart;
 601          $this->dstop = $dstop;
 602          $this->timestamp_min = $timestamp_min;
 603          $this->timestamp_max = $timestamp_max;
 604  
 605  
 606          $start_is_set = false;
 607          $stop_is_set = false;
 608  
 609  
 610          // if a start date is specified in the querystring, crop anything before
 611          if( !empty($dstart) )
 612          {
 613              // Add trailing 0s: YYYYMMDDHHMMSS
 614              $dstart0 = $dstart.'00000000000000';  // TODO: this is NOT correct, should be 0101 for month
 615  
 616              // Start date in MySQL format: seconds get omitted (rounded to lower to minute for caching purposes)
 617              $dstart_mysql = substr($dstart0,0,4).'-'.substr($dstart0,4,2).'-'.substr($dstart0,6,2).' '
 618                                              .substr($dstart0,8,2).':'.substr($dstart0,10,2);
 619  
 620              $this->WHERE_and( $this->dbprefix.'datestart >= '.$DB->quote( $dstart_mysql ).'
 621                                                      OR ( '.$this->dbprefix.'datedeadline IS NULL AND '.$this->dbprefix.'datestart >= '.$DB->quote( $dstart_mysql ).' )' );
 622  
 623              $start_is_set = true;
 624          }
 625  
 626  
 627          // if a stop date is specified in the querystring, crop anything before
 628          if( !empty($dstop) )
 629          {
 630              switch( strlen( $dstop ) )
 631              {
 632                  case '4':
 633                      // We have only year, add one to year
 634                      $dstop_mysql = ($dstop+1).'-01-01 00:00:00';
 635                      break;
 636  
 637                  case '6':
 638                      // We have year month, add one to month
 639                      $dstop_mysql = date("Y-m-d H:i:s ", mktime(0, 0, 0, substr($dstop,4,2)+1, 01, substr($dstop,0,4)));
 640                      break;
 641  
 642                  case '8':
 643                      // We have year mounth day, add one to day
 644                      $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)));
 645                      break;
 646  
 647                  case '10':
 648                      // We have year mounth day hour, add one to hour
 649                      $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)));
 650                      break;
 651  
 652                  case '12':
 653                      // We have year mounth day hour minute, add one to minute
 654                      $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)));
 655                      break;
 656  
 657                  default:
 658                      // add one to second
 659                      // Stop date in MySQL format: seconds get omitted (rounded to lower to minute for caching purposes)
 660                      $dstop_mysql = substr($dstop,0,4).'-'.substr($dstop,4,2).'-'.substr($dstop,6,2).' '
 661                                              .substr($dstop,8,2).':'.substr($dstop,10,2);
 662              }
 663  
 664              $this->WHERE_and( $this->dbprefix.'datestart < '.$DB->quote( $dstop_mysql ) ); // NOT <= comparator because we compare to the superior stop date
 665  
 666              $stop_is_set = true;
 667          }
 668  
 669  
 670          if( !$start_is_set || !$stop_is_set )
 671          {
 672  
 673              if( !is_null($w)  // Note: week # can be 0
 674                      && strlen($m) == 4 )
 675              { // If a week number is specified (with a year)
 676  
 677                  // Note: we use PHP to calculate week boundaries in order to handle weeks
 678                  // that overlap 2 years properly, even when start on week is monday (which MYSQL won't handle properly)
 679                  $start_date_for_week = get_start_date_for_week( $m, $w, locale_startofweek() );
 680  
 681                  $this->WHERE_and( $this->dbprefix."datestart >= '".date('Y-m-d',$start_date_for_week)."'" );
 682                  $this->WHERE_and( $this->dbprefix."datestart < '".date('Y-m-d',$start_date_for_week+604800 )."'" ); // + 7 days
 683  
 684                  $start_is_set = true;
 685                  $stop_is_set = true;
 686              }
 687              elseif( !empty($m) )
 688              {    // We want to restrict on an interval:
 689                  $this->WHERE_and( 'EXTRACT(YEAR FROM '.$this->dbprefix.'datestart)='.intval(substr($m,0,4)) );
 690                  if( strlen($m) > 5 )
 691                      $this->WHERE_and( 'EXTRACT(MONTH FROM '.$this->dbprefix.'datestart)='.intval(substr($m,4,2)) );
 692                  if( strlen($m) > 7 )
 693                      $this->WHERE_and( 'EXTRACT(DAY FROM '.$this->dbprefix.'datestart)='.intval(substr($m,6,2)) );
 694                  if( strlen($m) > 9 )
 695                      $this->WHERE_and( 'EXTRACT(HOUR FROM '.$this->dbprefix.'datestart)='.intval(substr($m,8,2)) );
 696                  if( strlen($m) > 11 )
 697                      $this->WHERE_and( 'EXTRACT(MINUTE FROM '.$this->dbprefix.'datestart)='.intval(substr($m,10,2)) );
 698                  if( strlen($m) > 13 )
 699                      $this->WHERE_and( 'EXTRACT(SECOND FROM '.$this->dbprefix.'datestart)='.intval(substr($m,12,2)) );
 700  
 701                  $start_is_set = true;
 702                  $stop_is_set = true;
 703              }
 704  
 705          }
 706  
 707  
 708          // TODO: start + x days
 709          // TODO: stop - x days
 710  
 711  
 712          // SILENT limits!
 713  
 714          // Timestamp limits:
 715          if( $timestamp_min == 'now' )
 716          {
 717              // echo 'hide past';
 718              $timestamp_min = time();
 719          }
 720          if( !empty($timestamp_min) )
 721          { // Hide posts before
 722              // echo 'hide before '.$timestamp_min;
 723              $date_min = remove_seconds( $timestamp_min + $time_difference );
 724              $this->WHERE_and( $this->dbprefix.'datestart >= '.$DB->quote( $date_min ) );
 725          }
 726  
 727          if( $timestamp_max == 'now' )
 728          {
 729              // echo 'hide future';
 730              $timestamp_max = time();
 731          }
 732          if( !empty($timestamp_max) )
 733          { // Hide posts after
 734              // echo 'after';
 735              $date_max = remove_seconds( $timestamp_max + $time_difference );
 736              $this->WHERE_and( $this->dbprefix.'datestart <= '.$DB->quote( $date_max ) );
 737          }
 738  
 739      }
 740  
 741  
 742      /**
 743       * Restricts creation date to a specific date range.
 744       *
 745        * @param mixed Do not show posts CREATED after this timestamp
 746       */
 747  	function where_datecreated( $timestamp_created_max = 'now' )
 748      {
 749          global $time_difference, $DB;
 750  
 751          if( !empty($timestamp_created_max) )
 752          { // Hide posts after
 753              // echo 'after';
 754              $date_max = date('Y-m-d H:i:s', $timestamp_created_max + $time_difference );
 755              $this->WHERE_and( $this->dbprefix.'datecreated <= '.$DB->quote( $date_max ) );
 756          }
 757  
 758      }
 759  
 760  
 761      /**
 762       * Restrict with keywords
 763       *
 764       * @param string Keyword search string
 765       * @param mixed Search for entire phrase or for individual words
 766       * @param mixed Require exact match of title or contents
 767       */
 768  	function where_keywords( $keywords, $phrase, $exact )
 769      {
 770          global $DB;
 771  
 772          $this->keywords = $keywords;
 773          $this->phrase = $phrase;
 774          $this->exact = $exact;
 775  
 776          if( empty($keywords) )
 777          {
 778              return;
 779          }
 780  
 781          $search = '';
 782  
 783          if( $exact )
 784          {    // We want exact match of title or contents
 785              $n = '';
 786          }
 787          else
 788          { // The words/sentence are/is to be included in in the title or the contents
 789              $n = '%';
 790          }
 791  
 792          if( ($phrase == '1') or ($phrase == 'sentence') )
 793          { // Sentence search
 794              $keywords = $DB->escape(trim($keywords));
 795              $search .= '('.$this->dbprefix.'title LIKE \''. $n. $keywords. $n. '\') OR ('.$this->dbprefix.'content LIKE \''. $n. $keywords. $n.'\')';
 796          }
 797          else
 798          { // Word search
 799              if( strtoupper( $phrase ) == 'OR' )
 800                  $swords = 'OR';
 801              else
 802                  $swords = 'AND';
 803  
 804              // puts spaces instead of commas
 805              $keywords = preg_replace('/, +/', ',', $keywords);
 806              $keywords = str_replace(',', ' ', $keywords);
 807              $keywords = str_replace('"', ' ', $keywords);
 808              $keywords = trim($keywords);
 809              $keyword_array = explode(' ',$keywords);
 810              $join = '';
 811              for ( $i = 0; $i < count($keyword_array); $i++)
 812              {
 813                  $search .= ' '. $join. ' ( ('.$this->dbprefix.'title LIKE \''. $n. $DB->escape($keyword_array[$i]). $n. '\')
 814                                                                  OR ('.$this->dbprefix.'content LIKE \''. $n. $DB->escape($keyword_array[$i]). $n.'\') ) ';
 815                  $join = $swords;
 816              }
 817          }
 818  
 819          //echo $search;
 820          $this->WHERE_and( $search );
 821      }
 822  
 823  
 824  }
 825  
 826  ?>

title

Description

title

Description

title

Description

title

title

Body