Skip to content
This repository
Browse code

ENHANCEMENT: Flyout menu to directly choose the pagetype one wants to…

… add.
  • Loading branch information...
commit a18b9bd85e78028bf458fc97c5d21e8aecee408d 1 parent 4524d1c
Wolf Vollprecht authored March 19, 2012 chillu committed April 09, 2012
24  code/controllers/CMSMain.php
@@ -241,7 +241,7 @@ public function Breadcrumbs($unlinked = false) {
241 241
 	 * @return String Serialized JSON
242 242
 	 */
243 243
 	public function SiteTreeHints() {
244  
-	  $classes = ClassInfo::subclassesFor( $this->stat('tree_class') );
  244
+	 	$classes = ClassInfo::subclassesFor( $this->stat('tree_class') );
245 245
 
246 246
 		$def['Root'] = array();
247 247
 		$def['Root']['disallowedParents'] = array();
@@ -255,6 +255,26 @@ public function SiteTreeHints() {
255 255
 			
256 256
 			// SiteTree::allowedChildren() returns null rather than an empty array if SiteTree::allowed_chldren == 'none'
257 257
 			if($allowedChildren == null) $allowedChildren = array();
  258
+			
  259
+			// Exclude SiteTree from possible Children
  260
+			$possibleChildren = array_diff($allowedChildren, array("SiteTree"));
  261
+
  262
+			// Find i18n - names and build allowed children array
  263
+			foreach($possibleChildren as $child) {
  264
+				$instance = singleton($child);
  265
+				
  266
+				if($instance instanceof HiddenClass) continue;
  267
+
  268
+				if(!$instance->canCreate()) continue;
  269
+
  270
+				// skip this type if it is restricted
  271
+				if($instance->stat('need_permission') && !$this->can(singleton($class)->stat('need_permission'))) continue;
  272
+
  273
+				$title = $instance->i18n_singular_name();
  274
+
  275
+				$def[$class]['allowedChildren'][] = array("ssclass" => $child, "ssname" => $title);
  276
+			}
  277
+
258 278
 			$allowedChildren = array_keys(array_diff($classes, $allowedChildren));
259 279
 			if($allowedChildren) $def[$class]['disallowedChildren'] = $allowedChildren;
260 280
 			
@@ -264,6 +284,7 @@ public function SiteTreeHints() {
264 284
 				$def[$class]['defaultChild'] = $defaultChild;
265 285
 			
266 286
 			$defaultParent = $obj->defaultParent();
  287
+
267 288
 			$parent = SiteTree::get_by_link($defaultParent);
268 289
 			
269 290
 			$id = $parent ? $parent->id : null;
@@ -368,7 +389,6 @@ public function PageTypes() {
368 389
 		}
369 390
 		
370 391
 		$result->sort('AddAction');
371  
-		
372 392
 		return $result;
373 393
 	}
374 394
 
14  code/controllers/CMSPageAddController.php
@@ -16,6 +16,18 @@ class CMSPageAddController extends CMSPageEditController {
16 16
 	 * @return Form
17 17
 	 */
18 18
 	function AddForm() {
  19
+		// If request send from rightclick-submenu, directly add Page
  20
+		if(($pageType = $this->request->getVar('Type')) && ($parentID = $this->request->getVar('ParentID'))) {
  21
+			$data = array(
  22
+				"PageType" => (string)$pageType,
  23
+				"ParentID" => $parentID,
  24
+				"ParentModeField" => "child"
  25
+			);
  26
+			$this->doAdd($data, null);
  27
+			return;
  28
+		}
  29
+
  30
+
19 31
 		$record = $this->currentPage();
20 32
 		
21 33
 		$pageTypes = array();
@@ -89,7 +101,7 @@ function AddForm() {
89 101
 		if($parentID = $this->request->getVar('ParentID')) {
90 102
 			$form->Fields()->dataFieldByName('ParentID')->setValue((int)$parentID);
91 103
 		}
92  
-		
  104
+
93 105
 		return $form;
94 106
 	}
95 107
 
4  code/model/ErrorPage.php
@@ -22,6 +22,8 @@ class ErrorPage extends Page {
22 22
 		"ShowInSearch" => 0
23 23
 	);
24 24
 	
  25
+	static $allowed_children = array();
  26
+
25 27
 	static $description = 'Custom content for different error cases (e.g. "Page not found")';
26 28
 	
27 29
 	protected static $static_filepath = ASSETS_PATH;
@@ -50,7 +52,7 @@ class_exists('Translatable') ? Translatable::get_current_locale() : null
50 52
 			$response = new SS_HTTPResponse();	
51 53
 			
52 54
 			$response->setStatusCode($statusCode);
53  
-			$response->setBody(file_get_contents($cachedPath));
  55
+			$response->setBody(file_get_contents($cachedPath));	
54 56
 			
55 57
 			return $response;
56 58
 		}
2  code/model/RedirectorPage.php
@@ -27,6 +27,8 @@ class RedirectorPage extends Page {
27 27
 	static $many_many = array(
28 28
 	);
29 29
 	
  30
+	static $allowed_children = array();
  31
+
30 32
 	/**
31 33
 	 * Returns this page if the redirect is external, otherwise
32 34
 	 * returns the target page.
62  javascript/CMSMain.Tree.js
... ...
@@ -1,35 +1,57 @@
1 1
 (function($) {
2 2
 
3 3
 	$.entwine('ss', function($){
4  
-	
5 4
 		$('.cms-tree').entwine({
6 5
 			getTreeConfig: function() {
7 6
 				var config = this._super();
  7
+				var hints = this.getHints();
8 8
 				config.plugins.push('contextmenu');
9 9
 				config.contextmenu = {
10  
-					'items': {
11  
-						'create': null,
12  
-						"rename": null,
13  
-						"remove": null,
14  
-						"ccp": null,
15  
-						'edit': {
16  
-							'label': ss.i18n._t('Tree.EditPage'),
17  
-							'action': function(obj) {
18  
-								// TODO Fix hardcoding of link
19  
-								$('.cms-container').loadPanel('admin/page/edit/show/' + obj.data('id'));
20  
-							}
21  
-						},
22  
-						'addsubpage': {
23  
-							'label': ss.i18n._t('Tree.AddSubPage'),
24  
-							'action': function(obj) {
25  
-								// TODO Fix hardcoding of link
26  
-								$('.cms-container').loadPanel('admin/page/add/?ParentID=' + obj.data('id'));
  10
+					'items': function(node) {
  11
+						// Build a list for allowed children as submenu entries
  12
+						var ssclass = node.data('ssclass');
  13
+						var id = node.data('id');
  14
+
  15
+						var allowedChildren = new Object;
  16
+						$(hints[ssclass].allowedChildren).each(
  17
+							function(key, val){
  18
+								allowedChildren["allowedchildren-" + key ] = {
  19
+									'label': '<span class="jstree-pageicon"></span>' + val.ssname,
  20
+									'_class': 'class-' + val.ssclass,
  21
+									'action': function(obj) {
  22
+										// TODO Fix hardcoding of link
  23
+										$('.cms-container').loadPanel('admin/page/add/?ParentID=' + id + '&Type=' + val.ssclass);
  24
+									}
  25
+								};
27 26
 							}
28  
-						}
29  
-					}
  27
+						);
  28
+						var menuitems = 
  29
+							{
  30
+								'edit': {
  31
+									'label': ss.i18n._t('Tree.EditPage'),
  32
+									'action': function(obj) {
  33
+										// TODO Fix hardcoding of link
  34
+										$('.cms-container').loadPanel('admin/page/add/show/' + obj.data('id'));
  35
+									}
  36
+								}
  37
+							};
  38
+						// Test if there are any allowed Children and thus the possibility of adding some 
  39
+						if(allowedChildren.hasOwnProperty('allowedchildren-0')) {
  40
+							menuitems['addsubpage'] = {
  41
+									'label': ss.i18n._t('Tree.AddSubPage'),
  42
+									'action': function(obj) {
  43
+										// TODO Fix hardcoding of link
  44
+										$('.cms-container').loadPanel('admin/page/add/?ParentID=' + obj.data('id'));
  45
+									},
  46
+									'submenu': allowedChildren
  47
+								};
  48
+						}				
  49
+						return menuitems;
  50
+					} 
30 51
 				};
31 52
 				return config;
32 53
 			}
33 54
 		});
34 55
 	});
  56
+
35 57
 }(jQuery));
2  templates/CMSPageEditController_Tools.ss
@@ -4,7 +4,7 @@
4 4
 	</div>
5 5
 	
6 6
 	<div class="cms-panel-content center">
7  
-		<div class="cms-tree" data-url-tree="$Link(getsubtree)">
  7
+		<div class="cms-tree" data-url-tree="$Link(getsubtree)" data-hints="$SiteTreeHints">
8 8
 			$SiteTreeAsUL
9 9
 		</div>
10 10
 	</div>

0 notes on commit a18b9bd

Please sign in to comment.
Something went wrong with that request. Please try again.