2009-08-04 05:09:26 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2009-10-22 00:26:52 +02:00
|
|
|
* Base class for filtering the subtree for certain node statuses.
|
|
|
|
*
|
|
|
|
* The simplest way of building a CMSSiteTreeFilter is to create a pagesToBeShown() method that
|
|
|
|
* returns an Iterator of maps, each entry containing the 'ID' and 'ParentID' of the pages to be
|
|
|
|
* included in the tree. The reuslt of a DB::query() can be returned directly.
|
|
|
|
*
|
|
|
|
* If you wish to make a more complex tree, you can overload includeInTree($page) to return true/
|
|
|
|
* false depending on whether the given page should be included. Note that you will need to include
|
|
|
|
* parent helper pages yourself.
|
|
|
|
*
|
2009-08-04 05:09:26 +02:00
|
|
|
* @package cms
|
|
|
|
* @subpackage content
|
|
|
|
*/
|
|
|
|
abstract class CMSSiteTreeFilter extends Object {
|
2009-10-22 00:26:52 +02:00
|
|
|
|
2009-11-22 09:23:12 +01:00
|
|
|
/**
|
|
|
|
* @var Array Search parameters, mostly properties on {@link SiteTree}.
|
|
|
|
* Caution: Unescaped data.
|
|
|
|
*/
|
|
|
|
protected $params = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Array
|
|
|
|
*/
|
|
|
|
protected $_cache_ids = null;
|
2009-08-04 05:09:26 +02:00
|
|
|
|
2009-11-22 09:23:12 +01:00
|
|
|
/**
|
|
|
|
* @var Array
|
|
|
|
*/
|
|
|
|
protected $_cache_expanded = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
|
|
|
protected $childrenMethod = null;
|
|
|
|
|
|
|
|
function __construct($params = null) {
|
|
|
|
if($params) $this->params = $params;
|
|
|
|
|
|
|
|
parent::__construct();
|
2009-08-04 05:09:26 +02:00
|
|
|
}
|
2009-11-22 09:23:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String Method on {@link Hierarchy} objects
|
|
|
|
* which is used to traverse into children relationships.
|
|
|
|
*/
|
|
|
|
function getChildrenMethod() {
|
|
|
|
return $this->childrenMethod;
|
2009-10-22 00:26:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-11-22 09:23:12 +01:00
|
|
|
* @return Array Map of Page IDs to their respective ParentID values.
|
|
|
|
*/
|
|
|
|
function pagesIncluded() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Populate the IDs of the pages returned by pagesIncluded(), also including
|
2009-10-22 00:26:52 +02:00
|
|
|
* the necessary parent helper pages.
|
|
|
|
*/
|
|
|
|
protected function populateIDs() {
|
2009-11-22 09:23:12 +01:00
|
|
|
$parents = array();
|
|
|
|
$this->_cache_ids = array();
|
|
|
|
|
|
|
|
if($pages = $this->pagesIncluded()) {
|
2009-10-22 00:26:52 +02:00
|
|
|
|
2009-11-22 09:23:12 +01:00
|
|
|
// And keep a record of parents we don't need to get
|
|
|
|
// parents of themselves, as well as IDs to mark
|
|
|
|
foreach($pages as $pageArr) {
|
|
|
|
$parents[$pageArr['ParentID']] = true;
|
|
|
|
$this->_cache_ids[$pageArr['ID']] = true;
|
2009-10-22 00:26:52 +02:00
|
|
|
}
|
2012-09-04 04:51:14 +02:00
|
|
|
|
|
|
|
while(!empty($parents)) {
|
2009-11-22 09:23:12 +01:00
|
|
|
$q = new SQLQuery();
|
2012-05-07 01:24:13 +02:00
|
|
|
$q->setSelect(array('"ID"','"ParentID"'))
|
|
|
|
->setFrom('"SiteTree"')
|
|
|
|
->setWhere('"ID" in ('.implode(',',array_keys($parents)).')');
|
2009-10-22 00:26:52 +02:00
|
|
|
|
2012-09-04 04:51:14 +02:00
|
|
|
$parents = array();
|
|
|
|
|
2009-11-22 09:23:12 +01:00
|
|
|
foreach($q->execute() as $row) {
|
2009-10-22 00:26:52 +02:00
|
|
|
if ($row['ParentID']) $parents[$row['ParentID']] = true;
|
2009-11-22 09:23:12 +01:00
|
|
|
$this->_cache_ids[$row['ID']] = true;
|
|
|
|
$this->_cache_expanded[$row['ID']] = true;
|
2009-10-22 00:26:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-11-22 09:23:12 +01:00
|
|
|
* Returns TRUE if the given page should be included in the tree.
|
|
|
|
* Caution: Does NOT check view permissions on the page.
|
|
|
|
*
|
|
|
|
* @param SiteTree $page
|
|
|
|
* @return Boolean
|
2009-10-22 00:26:52 +02:00
|
|
|
*/
|
2009-11-22 09:23:12 +01:00
|
|
|
public function isPageIncluded($page) {
|
|
|
|
if($this->_cache_ids === NULL) $this->populateIDs();
|
|
|
|
|
|
|
|
return (isset($this->_cache_ids[$page->ID]) && $this->_cache_ids[$page->ID]);
|
2009-10-22 00:26:52 +02:00
|
|
|
}
|
|
|
|
|
2009-08-04 05:09:26 +02:00
|
|
|
}
|
|
|
|
|
2009-11-22 09:23:12 +01:00
|
|
|
/**
|
|
|
|
* Works a bit different than the other filters:
|
|
|
|
* Shows all pages *including* those deleted from stage and live.
|
|
|
|
* It does not filter out pages still existing in the different stages.
|
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage content
|
|
|
|
*/
|
2009-08-04 05:09:26 +02:00
|
|
|
class CMSSiteTreeFilter_DeletedPages extends CMSSiteTreeFilter {
|
2009-11-22 09:23:12 +01:00
|
|
|
|
|
|
|
protected $childrenMethod = "AllHistoricalChildren";
|
|
|
|
|
2009-08-04 05:09:26 +02:00
|
|
|
static function title() {
|
2010-04-12 11:50:28 +02:00
|
|
|
return _t('CMSSiteTreeFilter_DeletedPages.Title', "All pages, including deleted");
|
2009-08-04 05:09:26 +02:00
|
|
|
}
|
|
|
|
|
2009-11-22 09:23:12 +01:00
|
|
|
function pagesIncluded() {
|
|
|
|
$ids = array();
|
|
|
|
// TODO Not very memory efficient, but usually not very many deleted pages exist
|
|
|
|
$pages = Versioned::get_including_deleted('SiteTree');
|
|
|
|
if($pages) foreach($pages as $page) {
|
|
|
|
$ids[] = array('ID' => $page->ID, 'ParentID' => $page->ParentID);
|
|
|
|
}
|
|
|
|
return $ids;
|
2009-08-04 05:09:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-22 09:23:12 +01:00
|
|
|
/**
|
|
|
|
* Gets all pages which have changed on stage.
|
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage content
|
|
|
|
*/
|
2009-08-04 05:09:26 +02:00
|
|
|
class CMSSiteTreeFilter_ChangedPages extends CMSSiteTreeFilter {
|
2009-11-22 09:23:12 +01:00
|
|
|
|
2009-08-04 05:09:26 +02:00
|
|
|
static function title() {
|
2009-11-22 09:45:59 +01:00
|
|
|
return _t('CMSSiteTreeFilter_ChangedPages.Title', "Changed pages");
|
2009-08-04 05:09:26 +02:00
|
|
|
}
|
|
|
|
|
2009-10-22 00:26:52 +02:00
|
|
|
function pagesIncluded() {
|
2009-11-22 09:23:12 +01:00
|
|
|
$ids = array();
|
|
|
|
$q = new SQLQuery();
|
2012-05-07 01:24:13 +02:00
|
|
|
$q->setSelect(array('"SiteTree"."ID"','"SiteTree"."ParentID"'))
|
|
|
|
->setFrom('"SiteTree"')
|
|
|
|
->addLeftJoin('SiteTree_Live', '"SiteTree_Live"."ID" = "SiteTree"."ID"')
|
|
|
|
->setWhere('"SiteTree"."Version" > "SiteTree_Live"."Version"');
|
2009-11-22 09:23:12 +01:00
|
|
|
|
|
|
|
foreach($q->execute() as $row) {
|
|
|
|
$ids[] = array('ID'=>$row['ID'],'ParentID'=>$row['ParentID']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ids;
|
2009-10-22 00:26:52 +02:00
|
|
|
}
|
2009-08-04 05:09:26 +02:00
|
|
|
}
|
|
|
|
|
2009-11-22 09:23:12 +01:00
|
|
|
/**
|
|
|
|
* @package cms
|
|
|
|
* @subpackage content
|
|
|
|
*/
|
2009-08-04 05:09:26 +02:00
|
|
|
class CMSSiteTreeFilter_Search extends CMSSiteTreeFilter {
|
2009-11-22 09:23:12 +01:00
|
|
|
|
2009-08-04 05:09:26 +02:00
|
|
|
static function title() {
|
2009-11-22 09:45:59 +01:00
|
|
|
return _t('CMSSiteTreeFilter_Search.Title', "All pages");
|
2009-08-04 05:09:26 +02:00
|
|
|
}
|
|
|
|
|
2009-10-22 00:26:52 +02:00
|
|
|
/**
|
|
|
|
* Retun an array of maps containing the keys, 'ID' and 'ParentID' for each page to be displayed
|
|
|
|
* in the search.
|
2009-11-22 09:23:12 +01:00
|
|
|
*
|
|
|
|
* @return Array
|
2009-10-22 00:26:52 +02:00
|
|
|
*/
|
2012-06-11 10:36:41 +02:00
|
|
|
public function pagesIncluded() {
|
|
|
|
$sng = singleton('SiteTree');
|
2009-11-22 09:23:12 +01:00
|
|
|
$ids = array();
|
2012-06-11 10:36:41 +02:00
|
|
|
|
|
|
|
$query = new DataQuery('SiteTree');
|
|
|
|
$query->setQueriedColumns(array('ID', 'ParentID'));
|
|
|
|
|
|
|
|
foreach($this->params as $name => $val) {
|
|
|
|
$SQL_val = Convert::raw2sql($val);
|
|
|
|
|
2009-11-22 09:23:12 +01:00
|
|
|
switch($name) {
|
2011-04-22 13:32:10 +02:00
|
|
|
case 'Term':
|
2012-06-11 10:36:41 +02:00
|
|
|
$query->whereAny(array(
|
|
|
|
"\"URLSegment\" LIKE '%$SQL_val%'",
|
|
|
|
"\"Title\" LIKE '%$SQL_val%'",
|
|
|
|
"\"MenuTitle\" LIKE '%$SQL_val%'",
|
|
|
|
"\"Content\" LIKE '%$SQL_val%'"
|
|
|
|
));
|
2009-11-22 09:23:12 +01:00
|
|
|
break;
|
2012-06-11 10:36:41 +02:00
|
|
|
|
2011-04-22 13:32:10 +02:00
|
|
|
case 'LastEditedFrom':
|
2012-08-30 12:42:45 +02:00
|
|
|
$fromDate = new DateField(null, null, $SQL_val);
|
|
|
|
$query->where("\"LastEdited\" >= '{$fromDate->dataValue()}'");
|
2011-04-22 13:32:10 +02:00
|
|
|
break;
|
2012-06-11 10:36:41 +02:00
|
|
|
|
2011-04-22 13:32:10 +02:00
|
|
|
case 'LastEditedTo':
|
2012-08-30 12:42:45 +02:00
|
|
|
$toDate = new DateField(null, null, $SQL_val);
|
|
|
|
$query->where("\"LastEdited\" <= '{$toDate->dataValue()}'");
|
2009-11-22 09:23:12 +01:00
|
|
|
break;
|
2012-06-11 10:36:41 +02:00
|
|
|
|
2009-11-22 09:23:12 +01:00
|
|
|
case 'ClassName':
|
|
|
|
if($val && $val != 'All') {
|
2012-06-11 10:36:41 +02:00
|
|
|
$query->where("\"ClassName\" = '$SQL_val'");
|
2009-11-22 09:23:12 +01:00
|
|
|
}
|
|
|
|
break;
|
2012-06-11 10:36:41 +02:00
|
|
|
|
2009-11-22 09:23:12 +01:00
|
|
|
default:
|
2012-06-11 10:36:41 +02:00
|
|
|
if(!empty($val) && $sng->hasDatabaseField($name)) {
|
|
|
|
$filter = $sng->dbObject($name)->defaultSearchFilter();
|
|
|
|
$filter->setValue($val);
|
|
|
|
$filter->apply($query);
|
2009-11-22 09:23:12 +01:00
|
|
|
}
|
2009-08-04 05:09:26 +02:00
|
|
|
}
|
|
|
|
}
|
2011-04-22 13:32:10 +02:00
|
|
|
|
2012-06-11 10:36:41 +02:00
|
|
|
foreach($query->execute() as $row) {
|
|
|
|
$ids[] = array('ID' => $row['ID'], 'ParentID' => $row['ParentID']);
|
2009-11-22 09:23:12 +01:00
|
|
|
}
|
2012-06-11 10:36:41 +02:00
|
|
|
|
2009-11-22 09:23:12 +01:00
|
|
|
return $ids;
|
2009-08-04 05:09:26 +02:00
|
|
|
}
|
2012-04-12 09:23:20 +02:00
|
|
|
}
|