Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class ClientProjectCategory extends DataObject {
- private static $db = array(
- 'Title' => 'Varchar(32)',
- 'SortOrder' => 'Int',
- 'URLSegment' => 'Varchar(32)',
- 'Description' => 'HTMLText'
- );
- private static $has_one = array(
- 'ParentCategory' => 'ClientProjectCategory'
- );
- private static $default_sort = array(
- 'SortOrder',
- );
- public function getCMSFields(){
- $fields = parent::getCMSFields();
- $fields->addFieldToTab('Root.Main', DropdownField::create('ParentCategoryID', 'Parent Category')
- ->setSource($this->ClientCategoriesForDropdown()->map('ID', 'Title'))
- ->setEmptyString(' -No Parent- '), 'Description');
- $fields->addFieldToTab('Root.Main', GridField::create('ClientProjectCategory', 'SubCategories', $this->getAllChildren(@$this->ID), GridFieldConfig_RelationEditor::create()));
- return $fields;
- }
- public function getAllChildren( $currentCategoryID = null ){
- if( $currentCategoryID == null ){
- return ClientProjectCategory::get()->filter('ParentCategoryID', -1);
- }
- $resultCategories = new ArrayList();
- $childrenCategories = ClientProjectCategory::get()->filter('ParentCategoryID', $currentCategoryID);
- if( $childrenCategories->exists() ){
- $resultCategories->merge($childrenCategories->toArray());
- foreach( $childrenCategories as $childCategory ){
- $resultCategories->merge($this->getAllChildren($childCategory->ID));
- }
- } else {
- return $childrenCategories;
- }
- return $resultCategories;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement