new added hide_from_cms_tree and hide_from_hierarchy

This commit is contained in:
John Milmine 2016-05-15 15:19:39 +12:00
parent 2aec495a7c
commit c401d9daff
3 changed files with 121 additions and 6 deletions

View File

@ -38,6 +38,32 @@ class Hierarchy extends DataExtension {
*/
private static $node_threshold_leaf = 250;
/**
* A list of classnames to exclude from display in both the CMS and front end
* displays. ->Children() and ->AllChildren affected.
* Especially useful for big sets of pages like listings
* If you use this, and still need the classes to be editable
* then add a model admin for the class
* Note: Does not filter subclasses (non-inheriting)
*
* @var array
* @config
*/
private static $hide_from_hierarchy = array();
/**
* A list of classnames to exclude from display in the page tree views of the CMS,
* unlike $hide_from_hierarchy above which effects both CMS and front end.
* Especially useful for big sets of pages like listings
* If you use this, and still need the classes to be editable
* then add a model admin for the class
* Note: Does not filter subclasses (non-inheriting)
*
* @var array
* @config
*/
private static $hide_from_cms_tree = array();
public static function get_extra_config($class, $extension, $args) {
return array(
'has_one' => array('Parent' => $class)
@ -605,6 +631,18 @@ class Hierarchy extends DataExtension {
return $this->_cache_numChildren;
}
/**
* Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree?
*
* @return bool
*/
public function showingCMSTree() {
if (!Controller::has_curr()) return false;
$controller = Controller::curr();
return $controller instanceof LeftAndMain
&& in_array($controller->getAction(), array("treeview", "listview", "getsubtree"));
}
/**
* Return children from the stage site
*
@ -614,9 +652,17 @@ class Hierarchy extends DataExtension {
*/
public function stageChildren($showAll = false) {
$baseClass = ClassInfo::baseDataClass($this->owner->class);
$hide_from_hierarchy = $this->owner->config()->hide_from_hierarchy;
$hide_from_cms_tree = $this->owner->config()->hide_from_cms_tree;
$staged = $baseClass::get()
->filter('ParentID', (int)$this->owner->ID)
->exclude('ID', (int)$this->owner->ID);
->filter('ParentID', (int)$this->owner->ID)
->exclude('ID', (int)$this->owner->ID);
if ($hide_from_hierarchy) {
$staged = $staged->exclude('ClassName', $hide_from_hierarchy);
}
if ($hide_from_cms_tree && $this->showingCMSTree()) {
$staged = $staged->exclude('ClassName', $hide_from_cms_tree);
}
if (!$showAll && $this->owner->db('ShowInMenus')) {
$staged = $staged->filter('ShowInMenus', 1);
}
@ -638,6 +684,8 @@ class Hierarchy extends DataExtension {
}
$baseClass = ClassInfo::baseDataClass($this->owner->class);
$hide_from_hierarchy = $this->owner->config()->hide_from_hierarchy;
$hide_from_cms_tree = $this->owner->config()->hide_from_cms_tree;
$children = $baseClass::get()
->filter('ParentID', (int)$this->owner->ID)
->exclude('ID', (int)$this->owner->ID)
@ -645,8 +693,13 @@ class Hierarchy extends DataExtension {
'Versioned.mode' => $onlyDeletedFromStage ? 'stage_unique' : 'stage',
'Versioned.stage' => 'Live'
));
if(!$showAll) $children = $children->filter('ShowInMenus', 1);
if ($hide_from_hierarchy) {
$children = $children->exclude('ClassName', $hide_from_hierarchy);
}
if ($hide_from_cms_tree && $this->showingCMSTree()) {
$children = $children->exclude('ClassName', $hide_from_cms_tree);
}
if(!$showAll && $this->owner->db('ShowInMenus')) $children = $children->filter('ShowInMenus', 1);
return $children;
}

View File

@ -5,11 +5,13 @@ class HierarchyTest extends SapphireTest {
protected static $fixture_file = 'HierarchyTest.yml';
protected $requiredExtensions = array(
'HierarchyTest_Object' => array('Hierarchy', 'Versioned')
'HierarchyTest_Object' => array('Hierarchy', 'Versioned'),
'HierarchyHideTest_Object' => array('Hierarchy', 'Versioned'),
);
protected $extraDataObjects = array(
'HierarchyTest_Object'
'HierarchyTest_Object',
'HierarchyHideTest_Object'
);
/**
@ -482,6 +484,35 @@ class HierarchyTest extends SapphireTest {
$this->assertEquals('unexpanded jstree-closed closed', $nodeClass, 'obj2 should have children in the sitetree');
}
public function testNoHideFromHeirarchy() {
$obj4 = $this->objFromFixture('HierarchyHideTest_Object', 'obj4');
$obj4->publish("Stage", "Live");
foreach($obj4->stageChildren() as $child) {
$child->publish("Stage", "Live");
}
$this->assertEquals($obj4->stageChildren()->Count(), 2);
$this->assertEquals($obj4->liveChildren()->Count(), 2);
}
public function testHideFromHeirarchy() {
HierarchyHideTest_Object::config()->hide_from_hierarchy = array('HierarchyHideTest_SubObject');
$obj4 = $this->objFromFixture('HierarchyHideTest_Object', 'obj4');
$obj4->publish("Stage", "Live");
// load without using stage children otherwise it'll bbe filtered before it's publish
// we need to publish all of them, and expect liveChildren to return some.
$children = HierarchyHideTest_Object::get()
->filter('ParentID', (int)$obj4->ID)
->exclude('ID', (int)$obj4->ID);
foreach($children as $child) {
$child->publish("Stage", "Live");
}
$this->assertEquals($obj4->stageChildren()->Count(), 1);
$this->assertEquals($obj4->liveChildren()->Count(), 1);
}
/**
* @param String $html [description]
* @param array $nodes Breadcrumb path as array
@ -543,3 +574,22 @@ class HierarchyTest_Object extends DataObject implements TestOnly {
return $this->markingClasses();
}
}
class HierarchyHideTest_Object extends DataObject implements TestOnly {
private static $db = array(
'Title' => 'Varchar'
);
private static $extensions = array(
'Hierarchy',
"Versioned('Stage', 'Live')",
);
public function cmstreeclasses() {
return $this->markingClasses();
}
}
class HierarchyHideTest_SubObject extends HierarchyHideTest_Object {
}

View File

@ -32,3 +32,15 @@ HierarchyTest_Object:
obj3ab:
Parent: =>HierarchyTest_Object.obj3a
Title: Obj 3ab
HierarchyHideTest_Object:
obj4:
Title: Obj 4
obj4a:
Parent: =>HierarchyHideTest_Object.obj4
Title: Obj 4a
HierarchyHideTest_SubObject:
obj4b:
Parent: =>HierarchyHideTest_Object.obj4
Title: Obj 4b