Skip to content

Commit

Permalink
API CHANGE Combined "Pages" and "Edit Page" into a single menu entry
Browse files Browse the repository at this point in the history
ENHANCEMENT Namespaced tree search parameters in CMSMain, in order to detect more reliably if a filter has been applied. Changing page search form to standard pushState behaviour, same as ModelAdmin (for both tree and list view).
MINOR Refactored list view loading
  • Loading branch information
chillu committed Apr 17, 2012
1 parent dcdb0b4 commit 6aeac37
Show file tree
Hide file tree
Showing 22 changed files with 314 additions and 312 deletions.
1 change: 1 addition & 0 deletions _config.php
Expand Up @@ -37,6 +37,7 @@
Object::add_extension('File', 'SiteTreeFileExtension');

// TODO Remove once we can configure CMSMenu through static, nested configuration files
CMSMenu::remove_menu_item('CMSMain');
CMSMenu::remove_menu_item('CMSPageEditController');
CMSMenu::remove_menu_item('CMSPageSettingsController');
CMSMenu::remove_menu_item('CMSPageHistoryController');
Expand Down
190 changes: 139 additions & 51 deletions code/controllers/CMSMain.php
Expand Up @@ -11,13 +11,13 @@
*/
class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionProvider {

static $url_segment = 'page';
static $url_segment = 'pages';

static $url_rule = '/$Action/$ID/$OtherID';

// Maintain a lower priority than other administration sections
// so that Director does not think they are actions of CMSMain
static $url_priority = 40;
static $url_priority = 39;

static $menu_title = 'Edit Page';

Expand All @@ -43,9 +43,9 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
'SiteTreeAsUL',
'getshowdeletedsubtree',
'batchactions',
'ListView',
'getListView',
'listchildren',
'treeview',
'listview',
'ListViewForm',
);

public function init() {
Expand Down Expand Up @@ -78,7 +78,24 @@ public function init() {
CMSBatchActionHandler::register('delete', 'CMSBatchAction_Delete');
CMSBatchActionHandler::register('deletefromlive', 'CMSBatchAction_DeleteFromLive');
}


function index($request) {
// In case we're not showing a specific record, explicitly remove any session state,
// to avoid it being highlighted in the tree, and causing an edit form to show.
if(!$request->param('Action')) $this->setCurrentPageId(null);

return parent::index($request);
}

protected function getResponseNegotiator() {
$negotiator = parent::getResponseNegotiator();
$controller = $this;
$negotiator->setCallback('ListViewForm', function() use(&$controller) {
return $controller->ListViewForm()->forTemplate();
});
return $negotiator;
}

/**
* If this is set to true, the "switchView" context in the
* template is shown, with links to the staging and publish site.
Expand Down Expand Up @@ -123,6 +140,62 @@ public function Link($action = null) {
"$action"
);
}

public function LinkPages() {
return singleton('CMSPagesController')->Link();
}

public function LinkTreeView() {
return $this->LinkWithSearch(singleton('CMSMain')->Link('treeview'));
}

public function LinkListView() {
return $this->LinkWithSearch(singleton('CMSMain')->Link('listview'));
}

public function LinkGalleryView() {
return $this->LinkWithSearch(singleton('CMSMain')->Link('galleryview'));
}

public function LinkPageEdit() {
if($id = $this->currentPageID()) {
return $this->LinkWithSearch(
Controller::join_links(singleton('CMSPageEditController')->Link('show'), $id)
);
}
}

public function LinkPageSettings() {
if($id = $this->currentPageID()) {
return $this->LinkWithSearch(
Controller::join_links(singleton('CMSPageSettingsController')->Link('show'), $id)
);
}
}

public function LinkPageHistory() {
if($id = $this->currentPageID()) {
return $this->LinkWithSearch(
Controller::join_links(singleton('CMSPageHistoryController')->Link('show'), $id)
);
}
}

protected function LinkWithSearch($link) {
// Whitelist to avoid side effects
$params = array(
'q' => (array)$this->request->getVar('q'),
'ParentID' => $this->request->getVar('ParentID')
);
return Controller::join_links(
$link,
array_filter(array_values($params)) ? '?' . http_build_query($params) : null
);
}

function LinkPageAdd() {
return singleton("CMSPageAddController")->Link();
}

/**
* @return string
Expand Down Expand Up @@ -155,6 +228,13 @@ public function SiteTreeAsUL() {

return $html;
}

/**
* @return boolean
*/
public function TreeIsFiltered() {
return $this->request->getVar('q');
}

function SearchForm() {
// get all page types in a dropdown-compatible format
Expand All @@ -177,19 +257,19 @@ function SearchForm() {
);

$fields = new FieldList(
new TextField('Term', _t('CMSSearch.FILTERLABELTEXT', 'Content')),
new TextField('q[Term]', _t('CMSSearch.FILTERLABELTEXT', 'Content')),
$dateGroup = new FieldGroup(
new HeaderField('Date', _t('CMSSearch.FILTERDATEHEADING', 'Date'), 4),
$dateFrom = new DateField('LastEditedFrom', _t('CMSSearch.FILTERDATEFROM', 'From')),
$dateTo = new DateField('LastEditedTo', _t('CMSSearch.FILTERDATETO', 'To'))
new HeaderField('q[Date]', _t('CMSSearch.FILTERDATEHEADING', 'Date'), 4),
$dateFrom = new DateField('q[LastEditedFrom]', _t('CMSSearch.FILTERDATEFROM', 'From')),
$dateTo = new DateField('q[LastEditedTo]', _t('CMSSearch.FILTERDATETO', 'To'))
),
new DropdownField(
'FilterClass',
'q[FilterClass]',
_t('CMSMain.PAGES', 'Pages'),
$filterMap
),
new DropdownField(
'ClassName',
'q[ClassName]',
_t('CMSMain.PAGETYPEOPT','Page Type', 'Dropdown for limiting search to a page type'),
$pageTypes,
null,
Expand All @@ -211,11 +291,14 @@ function SearchForm() {
// Use <button> to allow full jQuery UI styling
foreach($actions->dataFields() as $action) $action->setUseButtonTag(true);

$form = new Form($this, 'SearchForm', $fields, $actions);
$form->setFormMethod('GET');
$form->disableSecurityToken();
$form->unsetValidator();

$form = Form::create($this, 'SearchForm', $fields, $actions)
->addExtraClass('cms-search-form')
->setFormMethod('GET')
->setFormAction($this->Link())
->disableSecurityToken()
->unsetValidator();
$form->loadDataFrom($this->request->getVars());

return $form;
}

Expand Down Expand Up @@ -527,7 +610,7 @@ public function getEditForm($id = null, $fields = null) {
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
// TODO Can't merge $FormAttributes in template at the moment
$form->addExtraClass('center ss-tabset ' . $this->BaseCSSClasses());
if($form->Fields()->hasTabset()) $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet');
// if($form->Fields()->hasTabset()) $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet');

if(!$record->canEdit() || $deletedFromStage) {
$readonlyFields = $form->Fields()->makeReadonly();
Expand All @@ -542,52 +625,60 @@ public function getEditForm($id = null, $fields = null) {
);
}
}

/**
* @return String HTML
*/
public function treeview($request) {
return $this->renderWith($this->getTemplatesWithSuffix('_TreeView'));
}

public function listview($request) {
return $this->renderWith($this->getTemplatesWithSuffix('_ListView'));
}

/**
* Returns the pages meet a certain criteria as {@see CMSSiteTreeFilter} or the subpages of a parent page
* defaulting to no filter and show all pages in first level.
* Doubles as search results, if any search parameters are set through {@link SearchForm()}.
*
* @param Array Search filter criteria
* @param Int Optional parent node to filter on (can't be combined with other search criteria)
* @return SS_List
*/
public function getList(&$filterOn) {
public function getList($params, $parentID = 0) {
$list = new DataList($this->stat('tree_class'));

$request = $this->request;
$filter = null;
$ids = array();
if($filterClass = $request->requestVar('FilterClass')){
if(isset($params['FilterClass']) && $filterClass = $params['FilterClass']){
if(!is_subclass_of($filterClass, 'CMSSiteTreeFilter')) {
throw new Exception(sprintf('Invalid filter class passed: %s', $filterClass));
}
$filter = new $filterClass($request->requestVars());
$filter = new $filterClass($params);
$filterOn = true;
foreach($pages=$filter->pagesIncluded() as $pageMap){
$ids[] = $pageMap['ID'];
}
if(count($ids)) $list->where('"'.$this->stat('tree_class').'"."ID" IN ('.implode(",", $ids).')');
}else{
$parentID = 0;
if($this->urlParams['Action'] == 'listchildren' && $this->urlParams['ID']){
$parentID = $this->urlParams['ID'];
}
$list->filter("ParentID", $parentID);
} else {
$list->filter("ParentID", is_numeric($parentID) ? $parentID : 0);
}

return $list;
}

public function getListView(){
$filterOn = false;
$list = $this->getList($filterOn);
public function ListViewForm(){
$params = $this->request->requestVar('q');
$list = $this->getList($params, $this->request->requestVar('ParentID'));
$gridFieldConfig = GridFieldConfig::create()->addComponents(
new GridFieldSortableHeader(),
new GridFieldDataColumns(),
new GridFieldPaginator(15)
);
$gridField = new GridField('Page','Pages', $list, $gridFieldConfig);

if($filterOn){
// Don't allow navigating into children nodes on filtered lists
if($params){
$gridField->setDisplayFields(array(
'getTreeTitle' => _t('SiteTree.PAGETITLE', 'Page Title'),
'Created' => _t('SiteTree.CREATED', 'Date Created'),
Expand All @@ -607,13 +698,26 @@ public function getListView(){
'LastEdited' => 'Date->Ago',
));

$controller = $this;
$gridField->setFieldFormatting(array(
'getTreeTitle' => '<a class=\"cms-panel-link\" href=\"admin/page/edit/show/$ID\">$value</a>'
'listChildrenLink' => function(&$item) use($controller) {
$num = $item->numChildren();
if($num) {
return sprintf(
'<a class="cms-panel-link list-children-link" data-pjax="ListViewForm" data-target-panel="#Form_ListViewForm" href="%s?ParentID=%d&view=list">%s</a>',
$controller->Link(),
$item->ID,
$num
);
}
},
'getTreeTitle' => '<a class=\"cms-panel-link\" href=\"' .
singleton('CMSPageEditController')->Link('show') . '/$ID\">$value</a>'
));

$listview = new Form(
$this,
'ListView',
'ListViewForm',
new FieldList($gridField),
new FieldList()
);
Expand All @@ -624,14 +728,6 @@ public function getListView(){
return $listview;
}

public function getListViewHTML(){
return $this->getListView()->forTemplate();
}

public function ListView() {
return $this->getListView();
}

public function currentPageID() {
$id = parent::currentPageID();

Expand All @@ -645,14 +741,6 @@ public function currentPageID() {
return $id;
}

public function listchildren(){
if(Director::is_ajax()){
return $this->getListViewHTML();
}else{
return $this;
}
}

//------------------------------------------------------------------------------------------//
// Data saving handlers

Expand Down
2 changes: 1 addition & 1 deletion code/controllers/CMSPageAddController.php
@@ -1,7 +1,7 @@
<?php
class CMSPageAddController extends CMSPageEditController {

static $url_segment = 'page/add';
static $url_segment = 'pages/add';
static $url_rule = '/$Action/$ID/$OtherID';
static $url_priority = 42;
static $menu_title = 'Add page';
Expand Down
9 changes: 1 addition & 8 deletions code/controllers/CMSPageEditController.php
Expand Up @@ -5,15 +5,8 @@
*/
class CMSPageEditController extends CMSMain {

static $url_segment = 'page/edit';
static $url_segment = 'pages/edit';
static $url_rule = '/$Action/$ID/$OtherID';
static $url_priority = 41;
static $required_permission_codes = 'CMS_ACCESS_CMSMain';

public function Breadcrumbs($unlinked = false) {
$crumbs = parent::Breadcrumbs($unlinked);
// Remove "root" element, as its already shown in the tree panel
$crumbs->shift();
return $crumbs;
}
}
8 changes: 1 addition & 7 deletions code/controllers/CMSPageHistoryController.php
Expand Up @@ -6,7 +6,7 @@
*/
class CMSPageHistoryController extends CMSMain {

static $url_segment = 'page/history';
static $url_segment = 'pages/history';
static $url_rule = '/$Action/$ID/$VersionID/$OtherVersionID';
static $url_priority = 42;
static $menu_title = 'History';
Expand Down Expand Up @@ -370,10 +370,4 @@ function CompareVersionsForm($versionID, $otherVersionID) {
}
}

public function Breadcrumbs($unlinked = false) {
$crumbs = parent::Breadcrumbs($unlinked);
// Remove "root" element, as its already shown in the tree panel
$crumbs->shift();
return $crumbs;
}
}
8 changes: 1 addition & 7 deletions code/controllers/CMSPageSettingsController.php
Expand Up @@ -5,7 +5,7 @@
*/
class CMSPageSettingsController extends CMSMain {

static $url_segment = 'page/settings';
static $url_segment = 'pages/settings';
static $url_rule = '/$Action/$ID/$OtherID';
static $url_priority = 42;
static $required_permission_codes = 'CMS_ACCESS_CMSMain';
Expand All @@ -16,10 +16,4 @@ function getEditForm($id = null, $fields = null) {
return parent::getEditForm($record, ($record) ? $record->getSettingsFields() : null);
}

public function Breadcrumbs($unlinked = false) {
$crumbs = parent::Breadcrumbs($unlinked);
// Remove "root" element, as its already shown in the tree panel
$crumbs->shift();
return $crumbs;
}
}

0 comments on commit 6aeac37

Please sign in to comment.