ENHANCEMENT: Correct issue with SiteTree filtering not persisting and add drop down for page type and clear button. Part of a re-work of the search system

@todo: Tests, speed optimisation, proper connection of filtering with tree control checkboxes

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/branches/2.3@76779 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Hamish Friedlander 2009-05-14 03:36:11 +00:00 committed by Sam Minnee
parent dad1e68bcf
commit 29ab106af8
8 changed files with 216 additions and 134 deletions

View File

@ -39,7 +39,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
'dialog',
'duplicate',
'duplicatewithchildren',
'filtersitetree',
'getpagecount',
'getversion',
'publishall',
@ -55,6 +54,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
'AddPageOptionsForm',
'SiteTreeAsUL',
'getshowdeletedsubtree',
'getfilteredsubtree',
'batchactions'
);
@ -74,7 +74,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
return array(
'Title' => _t('CMSMain.TITLEOPT', 'Title', 0, 'The dropdown title in CMSMain left SiteTreeFilterOptions'),
'MenuTitle' => _t('CMSMain.MENUTITLEOPT', 'Navigation Label', 0, 'The dropdown title in CMSMain left SiteTreeFilterOptions'),
'ClassName' => _t('CMSMain.PAGETYPEOPT', 'Page Type', 0, "The dropdown title in CMSMain left SiteTreeFilterOptions"),
'Status' => _t('CMSMain.STATUSOPT', 'Status', 0, "The dropdown title in CMSMain left SiteTreeFilterOptions"),
'MetaDescription' => _t('CMSMain.METADESCOPT', 'Description', 0, "The dropdown title in CMSMain left SiteTreeFilterOptions"),
'MetaKeywords' => _t('CMSMain.METAKEYWORDSOPT', 'Keywords', 0, "The dropdown title in CMSMain left SiteTreeFilterOptions")
@ -185,7 +184,18 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
return $tree;
}
public function getfilteredsubtree() {
// Get the tree
$tree = $this->getSiteTreeFor($this->stat('tree_class'), $_REQUEST['ID'], null, 'cmsMainMarkingFilterFunction');
// Trim off the outer tag
$tree = ereg_replace('^[ \t\r\n]*<ul[^>]*>','', $tree);
$tree = ereg_replace('</ul[^>]*>[ \t\r\n]*$','', $tree);
return $tree;
}
/**
* Returns the SiteTree columns that can be filtered using the the Site Tree Search button as a DataObjectSet
*/
@ -204,44 +214,11 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
$dateField = new CalendarDateField('SiteTreeFilterDate');
return $dateField->Field();
}
/**
* Returns a filtered Site Tree
*/
public function filtersitetree() {
// Pre-cache sitetree version numbers for querying efficiency
Versioned::prepopulate_versionnumber_cache("SiteTree", "Stage");
Versioned::prepopulate_versionnumber_cache("SiteTree", "Live");
$className = 'SiteTree';
$rootID = null;
$obj = $rootID ? $this->getRecord($rootID) : singleton($className);
$obj->setMarkingFilterFunction('cmsMainMarkingFilterFunction');
$obj->markPartialTree();
if($p = $this->currentPage()) $obj->markToExpose($p);
// getChildrenAsUL is a flexible and complex way of traversing the tree
$siteTree = $obj->getChildrenAsUL("", '
"<li id=\"record-$child->ID\" class=\"" . $child->CMSTreeClasses($extraArg) . "\">" .
"<a href=\"" . Director::link(substr($extraArg->Link(),0,-1), "show", $child->ID) . "\" " . (($child->canEdit() || $child->canAddChildren()) ? "" : "class=\"disabled\"") . " title=\"' . _t('LeftAndMain.PAGETYPE') . '".$child->class."\" >" .
($child->TreeTitle()) .
"</a>"
'
,$this, true);
// Wrap the root if needs be.
if(!$rootID) {
$rootLink = $this->Link() . '0';
$siteTree = "<ul id=\"sitetree\" class=\"tree unformatted\"><li id=\"record-0\" class=\"Root nodelete\"><a href=\"$rootLink\">" .
_t('LeftAndMain.TREESITECONTENT',"Site Content",PR_HIGH,'Root node on left') . "</a>"
. $siteTree . "</li></ul>";
}
return $siteTree;
}
public function SiteTreeFilterPageTypeField() {
$types = SiteTree::page_type_classes(); array_unshift($types, 'All');
$optionsetField = new DropdownField('ClassName', 'ClassName', array_combine($types, $types), 'Any');
return $optionsetField->Field();
}
public function generateDataTreeHints() {
$classes = ClassInfo::subclassesFor( $this->stat('tree_class') );
@ -1255,6 +1232,7 @@ $filterCache = array();
// TODO: Find way to put this in a class
function cmsMainMarkingFilterFunction($node) {
global $filterCache;
// Expand all nodes
// $node->markingFinished();
@ -1278,6 +1256,11 @@ function cmsMainMarkingFilterFunction($node) {
$failed_filter = true;
}
}
// Check the ClassName
if (!empty($_REQUEST['ClassName']) && $_REQUEST['ClassName'] != 'Any') {
if ($node->ClassName != $_REQUEST['ClassName']) $failed_filter = true;
}
// Now check if a specified Criteria attribute matches
foreach (CMSMain::T_SiteTreeFilterOptions() as $key => $value)
{

View File

@ -464,10 +464,18 @@ class LeftAndMain extends Controller {
* @param $childrenMethod The method to call to get the children of the tree. For example,
* Children, AllChildrenIncludingDeleted, or AllHistoricalChildren
*/
function getSiteTreeFor($className, $rootID = null,
$childrenMethod = "AllChildrenIncludingDeleted") {
function getSiteTreeFor($className, $rootID = null, $childrenMethod = null, $filterFunction = null) {
// Default childrenMethod
if (!$childrenMethod) $childrenMethod = 'AllChildrenIncludingDeleted';
// Get the tree root
$obj = $rootID ? $this->getRecord($rootID) : singleton($className);
// Mark the nodes of the tree to return
if ($filterFunction) $obj->setMarkingFilterFunction($filterFunction);
$obj->markPartialTree(30, $this, $childrenMethod);
// Ensure current page is exposed
if($p = $this->currentPage()) $obj->markToExpose($p);
// getChildrenAsUL is a flexible and complex way of traversing the tree

View File

@ -371,14 +371,15 @@ ul.tree span.untranslated a:visited {
}
#left form.actionparams div.SearchCriteria {
width: 40%;
width: 28%;
overflow: hidden;
float: left;
}
#left form.actionparams input.SearchCriteria {
width: 43%;
#left form.actionparams input.SearchCriteria, #left form.actionparams #InputSiteTreeFilterClassName select {
width: 60%;
float: left;
margin: 0;
}
#left form.actionparams #InputSiteTreeFilterDate .calendar {
margin-left: -96px;

View File

@ -357,17 +357,25 @@ body.stillLoading select {
padding-left: 5px;
position: relative;
}
#SearchBox #SiteTreeSearchTerm {
padding: 1px 0 2px 0;
#SearchControls {
float: left;
position: relative;
margin-top:2px;
}
#SearchControls label {
display: none;
}
#SearchControls select#SiteTreeFilterAddCriteria {
width: 8.8em;
padding:1px 0; margin:0;
}
#searchIndicator {
display: none;
width: 16px;
height: 16px;
background: #EFEFEF url(../images/network-save.gif) no-repeat;
position: absolute;
left: 145px;
left: 95px;
top: 2px;
}
#searchIndicator.loading {
@ -379,13 +387,6 @@ body.stillLoading select {
padding-left: 5px;
margin-bottom: 4px;
}
#addCriteria {
float: left;
width: 100%;
}
#addCriteria label {
display: none;
}
#sitetree_ul, ul#sitetree {
padding: 3px 0 0 3px;
clear: left;

View File

@ -129,9 +129,9 @@ ShowDeletedPagesAction.prototype = {
onclick : function() {
if(this.checked) {
SiteTreeHandlers.loadTree_url = SiteTreeHandlers.controller_url + '/getshowdeletedsubtree';
$('sitetree').setCustomURL(SiteTreeHandlers.controller_url+'/getshowdeletedsubtree');
} else {
SiteTreeHandlers.loadTree_url = SiteTreeHandlers.controller_url + '/getsubtree';
$('sitetree').clearCustomURL();
}
// We can't update the tree while it's draggable; it gets b0rked.
@ -141,13 +141,8 @@ ShowDeletedPagesAction.prototype = {
__makeDraggableAfterUpdate = true;
}
var url = SiteTreeHandlers.loadTree_url + '?ID=0&ajax=1';
if($('LangSelector')) url += "&locale=" + $('LangSelector').value;
var request = new Ajax.Request(url, {
onSuccess: function(response) {
$('sitetree').innerHTML = response.responseText;
SiteTree.applyTo($('sitetree'));
$('sitetree').reload({
onSuccess: function() {
if(__makeDraggableAfterUpdate) $('sitetree').makeDraggable();
},
onFailure: function(response) {
@ -158,6 +153,107 @@ ShowDeletedPagesAction.prototype = {
}
}
/**
* Show only drafts checkbox click action
*/
showonlydrafts = Class.create();
showonlydrafts.applyTo('#publishpage_show_drafts');
showonlydrafts.prototype = {
onclick : function() {
if(this.checked) {
$('sitetree').setCustomURL(SiteTreeHandlers.controller_url+'/getfilteredsubtree', {Status:'Saved'});
} else {
$('sitetree').clearCustomURL();
}
$('sitetree').reload({
onSuccess: function() {
statusMessage(ss.i18n._t('CMSMAIN.FILTEREDTREE'),'good');
},
onFailure: function(response) {
errorMessage(ss.i18n.sprintf(
ss.i18n._t('CMSMAIN.ERRORFILTERPAGES'),
response.responseText
));
}
});
}
}
/**
* Control the site tree filter
*/
SiteTreeFilterForm = Class.create();
SiteTreeFilterForm.applyTo('form#search_options');
SiteTreeFilterForm.prototype = {
initialize: function() {
var self = this;
Form.getElements(this).each(function(el){
if (el.type == 'submit') el.onclick = function(){self.clicked = $F(this); console.log(self.clicked)};
});
},
onsubmit: function() {
var filters = $H();
if (this.clicked == 'Search') {
Form.getElements(this).each(function(el){
if (el.type == 'text') {
if ($F(el)) filters[el.name] = $F(el);
}
else if (el.type == 'select-one') {
if ($F(el) && $F(el) != 'All') filters[el.name] = $F(el);
}
});
}
else {
Form.getElements(this).each(function(el){
if (el.type == 'text') $(el).clear();
else if (el.type == 'select-one') el.value = 'All';
});
document.getElementsBySelector('.SearchCriteriaContainer', this).each(function(el){
Element.hide(el);
})
}
if (filters.keys().length) {
// Set new URL
$('sitetree').setCustomURL(SiteTreeHandlers.controller_url + '/getfilteredsubtree', filters);
// Disable checkbox tree controls that currently don't work with search.
// @todo: Make them work together
if ($('sitetree').isDraggable) $('sitetree').stopBeingDraggable();
document.getElementsBySelector('.checkboxAboveTree input[type=checkbox]').each(function(el){
el.value = false; el.disabled = true;
})
}
else {
// Reset URL to default
$('sitetree').clearCustomURL();
// Enable checkbox tree controls
document.getElementsBySelector('.checkboxAboveTree input[type=checkbox]').each(function(el){
el.disabled = false;
})
}
$('SiteTreeSearchButton').className = $('SiteTreeSearchClearButton').className = 'hidden';
$('searchIndicator').className = 'loading';
$('sitetree').reload({
onSuccess : function(response) {
$('SiteTreeSearchButton').className = $('SiteTreeSearchClearButton').className = 'action';
$('searchIndicator').className = '';
statusMessage('Filtered tree','good');
},
onFailure : function(response) {
errorMessage('Could not filter site tree<br />' + response.responseText);
}
});
return false;
}
}
/**
* Add Criteria Drop-down onchange action which allows more criteria to be shown
*/
@ -207,37 +303,6 @@ batchactionsclass.prototype = {
}
}
/**
* Show only drafts checkbox click action
*/
showonlydrafts = Class.create();
showonlydrafts.applyTo('#publishpage_show_drafts');
showonlydrafts.prototype = {
onclick : function() {
if (0 == $('SiteTreeIsFiltered').value) {
// Show all items in Site Tree again
new Ajax.Request( 'admin/filterSiteTree?Status=Saved&ajax=1', {
onSuccess: function( response ) {
$('sitetree_ul').innerHTML = response.responseText;
Behaviour.apply($('sitetree_ul'));
$('SiteTreeIsFiltered').value = 1;
$('batchactions').multiselectTransform();
statusMessage(ss.i18n._t('CMSMAIN.FILTEREDTREE'),'good');
},
onFailure : function(response) {
errorMessage(ss.i18n.sprintf(
ss.i18n._t('CMSMAIN.ERRORFILTERPAGES'),
response.responseText
));
}
});
} else {
batchActionGlobals.unfilterSiteTree();
}
}
}
// batchActionGlobals is needed because calls to observeMethod doesn't seem to preserve instance variables so a Prototype can't be used
batchActionGlobals = {
selectedNodes: { },

View File

@ -27,6 +27,48 @@ var TreeContextMenu = null;
*/
TreeAPI = Class.create();
TreeAPI.prototype = {
setCustomURL: function(url, arguments) {
this.customURL = url;
this.customArguments = $H(arguments);
},
clearCustomURL: function() {
this.customURL = this.customArguments = null;
},
url: function(args) {
var args = $H(args).merge(this.customArguments);
var url = this.customURL ? this.customURL : SiteTreeHandlers.loadTree_url;
url = url + (url.match(/\?/) ? '&' : '?') + args.toQueryString();
console.log('Loading tree from ' + url);
return url;
},
reload: function(options) {
this.innerHTML = 'Loading...';
var args = {ajax:1, ID:0};
if ($('Form_EditForm_Locale')) args.local = $('Form_EditForm_Locale').value;
url = this.url(args);
var self = this;
new Ajax.Request(url, {
onSuccess: function(response){
self.innerHTML = response.responseText;
self.castAsTreeNode(self.firstChild);
if (options.onSuccess) options.onSuccess(response);
},
onFailure: function(response){
errorMessage('error loading tree', response);
if (options.onError) options.onError(response);
}
});
},
/**
* Perform the given code on the each tree node with the given index.
* There could be more than one :-)
@ -177,11 +219,11 @@ TreeNodeAPI.prototype = {
this.cuedNewNodes[this.cuedNewNodes.length] = node;
}
var url = SiteTreeHandlers.loadTree_url;
url += (url.match(/\?/)) ? '&' : '?';
url += 'ajax=1&ID=' + this.getIdx();
if($('Form_EditForm_Locale')) url += "&locale=" + $('Form_EditForm_Locale').value;
var args = {ajax:1, ID:this.getIdx()};
if ($('Form_EditForm_Locale')) args.local = $('Form_EditForm_Locale').value;
url = this.tree.url(args);
new Ajax.Request(url, {
onSuccess : this.installSubtree.bind(this),
onFailure : this.showSubtreeLoadingError
@ -481,30 +523,3 @@ ReorganiseAction.prototype = {
}
}
}
/**
* Control the site tree filter
*/
SiteTreeFilterForm = Class.create();
SiteTreeFilterForm.applyTo('form#search_options');
SiteTreeFilterForm.prototype = {
onsubmit: function() {
$('SiteTreeSearchButton').className = 'hidden';
$('searchIndicator').className = 'loading';
Ajax.SubmitForm(this, null, {
onSuccess : function(response) {
$('SiteTreeIsFiltered').value = 1;
$('SiteTreeSearchButton').className = '';
$('searchIndicator').className = '';
$('sitetree_ul').innerHTML = response.responseText;
Behaviour.apply($('sitetree_ul'));
statusMessage('Filtered tree','good');
},
onFailure : function(response) {
errorMessage('Could not filter site tree<br />' + response.responseText);
}
});
return false;
}
}

View File

@ -147,7 +147,7 @@ $lang['en_US']['CMSMain']['VISITRESTORE'] = array(
$lang['en_US']['CMSMain_dialog.ss']['BUTTONNOTFOUND'] = 'Couldn\'t find the button name';
$lang['en_US']['CMSMain_dialog.ss']['NOLINKED'] = 'Can\'t find window.linkedObject to send the button click back to the main window';
$lang['en_US']['CMSMain_left.ss']['ADDEDNOTPUB'] = 'Added to the draft site and not published yet';
$lang['en_US']['CMSMain_left.ss']['ADDSEARCHCRITERIA'] = 'Add Criteria...';
$lang['en_US']['CMSMain_left.ss']['ADDSEARCHCRITERIA'] = 'Add Criteria';
$lang['en_US']['CMSMain_left.ss']['BATCHACTIONS'] = array(
'Batch Actions',
PR_HIGH
@ -174,6 +174,7 @@ $lang['en_US']['CMSMain_left.ss']['NEW'] = 'new';
$lang['en_US']['CMSMain_left.ss']['OPENBOX'] = 'click to open this box';
$lang['en_US']['CMSMain_left.ss']['PAGEVERSIONH'] = 'Page Version History';
$lang['en_US']['CMSMain_left.ss']['PUBLISHCONFIRM'] = 'Publish the selected pages';
$lang['en_US']['CMSMain_left.ss']['CLEAR'] = 'Clear';
$lang['en_US']['CMSMain_left.ss']['SEARCH'] = array(
'Search',
PR_HIGH

View File

@ -28,9 +28,8 @@
<div>
<input type="hidden" id="SiteTreeIsFiltered" value="0" />
<div id="SearchBox">
<input type="text" id="SiteTreeSearchTerm" name="SiteTreeSearchTerm" />
<div id="searchIndicator">&nbsp;</div>
<input type="submit" id="SiteTreeSearchButton" class="action" value="<% _t('SEARCH') %>" title="<% _t('SEARCHTITLE','Search through URL, Title, Menu Title, &amp; Content') %>" />
<div class="SearchCriteria">Text:</div>
<input type="text" id="SiteTreeSearchTerm" class='SearchCriteria' name="SiteTreeSearchTerm" />
</div>
<div id="ContainerSiteTreeFilterDate" class="SearchCriteriaContainer" style="display:none" >
@ -38,6 +37,11 @@
<div id="InputSiteTreeFilterDate">$SiteTreeFilterDateField</div>
</div>
<div id='ContainerSiteTreeFilterClassName' class='SearchCriteriaContainer' style="display:none">
<div id="TextSiteTreeFilterClassName" class="SearchCriteria">Page type: </div>
<div id="InputSiteTreeFilterClassName">$SiteTreeFilterPageTypeField</div>
</div>
<% control SiteTreeFilterOptions %>
<div id="Container$Column" class="SearchCriteriaContainer" style="display:none">
<div id="Text$Column" class="SearchCriteria">$Title:</div>
@ -45,14 +49,18 @@
</div>
<% end_control %>
<div id="addCriteria">
<div id='SearchControls'>
<select id="SiteTreeFilterAddCriteria">
<option value=""><% _t('ADDSEARCHCRITERIA','Add Criteria...') %></option>
<option value=""><% _t('ADDSEARCHCRITERIA','Add Criteria') %></option>
<option value="SiteTreeFilterDate"><% _t('EDITEDSINCE','Edited Since') %></option>
<option value="SiteTreeFilterClassName">Page type</option>
<% control SiteTreeFilterOptions %>
<option value="$Column">$Title</option>
<% end_control %>
</select>
<div id="searchIndicator">&nbsp;</div>
<input type="submit" id="SiteTreeSearchClearButton" class="action" value="<% _t('CLEAR') %>" title="<% _t('CLEARTITLE','Clear the search and view all items') %>" />
<input type="submit" id="SiteTreeSearchButton" class="action" value="<% _t('SEARCH') %>" title="<% _t('SEARCHTITLE','Search through URL, Title, Menu Title, &amp; Content') %>" />
</div>
</div>
</form>