From a18b9bd85e78028bf458fc97c5d21e8aecee408d Mon Sep 17 00:00:00 2001 From: Wolf Vollprecht Date: Mon, 19 Mar 2012 02:05:09 +0100 Subject: [PATCH] ENHANCEMENT: Flyout menu to directly choose the pagetype one wants to add. --- code/controllers/CMSMain.php | 24 ++++++++- code/controllers/CMSPageAddController.php | 14 ++++- code/model/ErrorPage.php | 4 +- code/model/RedirectorPage.php | 2 + javascript/CMSMain.Tree.js | 62 +++++++++++++++-------- templates/CMSPageEditController_Tools.ss | 2 +- 6 files changed, 83 insertions(+), 25 deletions(-) diff --git a/code/controllers/CMSMain.php b/code/controllers/CMSMain.php index 68e2bd7a..2a92e07a 100644 --- a/code/controllers/CMSMain.php +++ b/code/controllers/CMSMain.php @@ -241,7 +241,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr * @return String Serialized JSON */ public function SiteTreeHints() { - $classes = ClassInfo::subclassesFor( $this->stat('tree_class') ); + $classes = ClassInfo::subclassesFor( $this->stat('tree_class') ); $def['Root'] = array(); $def['Root']['disallowedParents'] = array(); @@ -255,6 +255,26 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr // SiteTree::allowedChildren() returns null rather than an empty array if SiteTree::allowed_chldren == 'none' if($allowedChildren == null) $allowedChildren = array(); + + // Exclude SiteTree from possible Children + $possibleChildren = array_diff($allowedChildren, array("SiteTree")); + + // Find i18n - names and build allowed children array + foreach($possibleChildren as $child) { + $instance = singleton($child); + + if($instance instanceof HiddenClass) continue; + + if(!$instance->canCreate()) continue; + + // skip this type if it is restricted + if($instance->stat('need_permission') && !$this->can(singleton($class)->stat('need_permission'))) continue; + + $title = $instance->i18n_singular_name(); + + $def[$class]['allowedChildren'][] = array("ssclass" => $child, "ssname" => $title); + } + $allowedChildren = array_keys(array_diff($classes, $allowedChildren)); if($allowedChildren) $def[$class]['disallowedChildren'] = $allowedChildren; @@ -264,6 +284,7 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr $def[$class]['defaultChild'] = $defaultChild; $defaultParent = $obj->defaultParent(); + $parent = SiteTree::get_by_link($defaultParent); $id = $parent ? $parent->id : null; @@ -368,7 +389,6 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr } $result->sort('AddAction'); - return $result; } diff --git a/code/controllers/CMSPageAddController.php b/code/controllers/CMSPageAddController.php index 95cdbb03..2caa06a0 100644 --- a/code/controllers/CMSPageAddController.php +++ b/code/controllers/CMSPageAddController.php @@ -16,6 +16,18 @@ class CMSPageAddController extends CMSPageEditController { * @return Form */ function AddForm() { + // If request send from rightclick-submenu, directly add Page + if(($pageType = $this->request->getVar('Type')) && ($parentID = $this->request->getVar('ParentID'))) { + $data = array( + "PageType" => (string)$pageType, + "ParentID" => $parentID, + "ParentModeField" => "child" + ); + $this->doAdd($data, null); + return; + } + + $record = $this->currentPage(); $pageTypes = array(); @@ -89,7 +101,7 @@ class CMSPageAddController extends CMSPageEditController { if($parentID = $this->request->getVar('ParentID')) { $form->Fields()->dataFieldByName('ParentID')->setValue((int)$parentID); } - + return $form; } diff --git a/code/model/ErrorPage.php b/code/model/ErrorPage.php index 28af7056..d8973b92 100644 --- a/code/model/ErrorPage.php +++ b/code/model/ErrorPage.php @@ -22,6 +22,8 @@ class ErrorPage extends Page { "ShowInSearch" => 0 ); + static $allowed_children = array(); + static $description = 'Custom content for different error cases (e.g. "Page not found")'; protected static $static_filepath = ASSETS_PATH; @@ -50,7 +52,7 @@ class ErrorPage extends Page { $response = new SS_HTTPResponse(); $response->setStatusCode($statusCode); - $response->setBody(file_get_contents($cachedPath)); + $response->setBody(file_get_contents($cachedPath)); return $response; } diff --git a/code/model/RedirectorPage.php b/code/model/RedirectorPage.php index 08c267f2..9d5c30fe 100644 --- a/code/model/RedirectorPage.php +++ b/code/model/RedirectorPage.php @@ -27,6 +27,8 @@ class RedirectorPage extends Page { static $many_many = array( ); + static $allowed_children = array(); + /** * Returns this page if the redirect is external, otherwise * returns the target page. diff --git a/javascript/CMSMain.Tree.js b/javascript/CMSMain.Tree.js index 3ba1fe35..4b352cec 100644 --- a/javascript/CMSMain.Tree.js +++ b/javascript/CMSMain.Tree.js @@ -1,35 +1,57 @@ (function($) { $.entwine('ss', function($){ - $('.cms-tree').entwine({ getTreeConfig: function() { var config = this._super(); + var hints = this.getHints(); config.plugins.push('contextmenu'); config.contextmenu = { - 'items': { - 'create': null, - "rename": null, - "remove": null, - "ccp": null, - 'edit': { - 'label': ss.i18n._t('Tree.EditPage'), - 'action': function(obj) { - // TODO Fix hardcoding of link - $('.cms-container').loadPanel('admin/page/edit/show/' + obj.data('id')); + 'items': function(node) { + // Build a list for allowed children as submenu entries + var ssclass = node.data('ssclass'); + var id = node.data('id'); + + var allowedChildren = new Object; + $(hints[ssclass].allowedChildren).each( + function(key, val){ + allowedChildren["allowedchildren-" + key ] = { + 'label': '' + val.ssname, + '_class': 'class-' + val.ssclass, + 'action': function(obj) { + // TODO Fix hardcoding of link + $('.cms-container').loadPanel('admin/page/add/?ParentID=' + id + '&Type=' + val.ssclass); + } + }; } - }, - 'addsubpage': { - 'label': ss.i18n._t('Tree.AddSubPage'), - 'action': function(obj) { - // TODO Fix hardcoding of link - $('.cms-container').loadPanel('admin/page/add/?ParentID=' + obj.data('id')); - } - } - } + ); + var menuitems = + { + 'edit': { + 'label': ss.i18n._t('Tree.EditPage'), + 'action': function(obj) { + // TODO Fix hardcoding of link + $('.cms-container').loadPanel('admin/page/add/show/' + obj.data('id')); + } + } + }; + // Test if there are any allowed Children and thus the possibility of adding some + if(allowedChildren.hasOwnProperty('allowedchildren-0')) { + menuitems['addsubpage'] = { + 'label': ss.i18n._t('Tree.AddSubPage'), + 'action': function(obj) { + // TODO Fix hardcoding of link + $('.cms-container').loadPanel('admin/page/add/?ParentID=' + obj.data('id')); + }, + 'submenu': allowedChildren + }; + } + return menuitems; + } }; return config; } }); }); + }(jQuery)); \ No newline at end of file diff --git a/templates/CMSPageEditController_Tools.ss b/templates/CMSPageEditController_Tools.ss index b3ed137a..524daa59 100644 --- a/templates/CMSPageEditController_Tools.ss +++ b/templates/CMSPageEditController_Tools.ss @@ -4,7 +4,7 @@
-
+
$SiteTreeAsUL