/**
  * CMS Fields
  * @return FieldList
  */
 function updateCMSFields(FieldList $fields)
 {
     if (!Permission::check("VIEW_SECTIONS")) {
         return $fields;
     }
     $SectionGrid = GridFieldConfig_RelationEditor::create()->removeComponentsByType('GridFieldAddNewButton')->addComponent(new GridFieldAddNewMultiClass())->addComponent(new GridFieldOrderableRows());
     $SectionGrid->getComponentByType('GridFieldAddExistingAutocompleter')->setSearchFields(array('AdminTitle'))->setResultsFormat('$AdminTitle');
     $SectionSubClasses = ClassInfo::subclassesfor('Section');
     unset($SectionSubClasses['Section'], $SectionSubClasses['MainSection']);
     foreach ($SectionSubClasses as $key => $value) {
         $SectionSubClasses[$key] = Section::Type($value);
     }
     # Limit sections based on type
     $LimitSectionTypes = Config::inst()->get($this->owner->ClassName, 'LimitSectionTypes');
     // debug::dump($LimitSectionTypes);
     if ($LimitSectionTypes) {
         foreach ($LimitSectionTypes as $type => $value) {
             if ($value == 0) {
                 unset($SectionSubClasses[$type]);
                 continue;
             }
             $CurrentSectionCount = $this->owner->Sections()->filter('ClassName', $type)->count();
             if ($CurrentSectionCount >= $value) {
                 unset($SectionSubClasses[$type]);
                 continue;
             }
         }
     }
     $SectionGrid->getComponentByType('GridFieldAddNewMultiClass')->setClasses($SectionSubClasses);
     # Limit total sections
     $LimitSectionTotal = Config::inst()->get($this->owner->ClassName, 'LimitSectionTotal');
     // debug::dump($PageLimits);
     if (isset($LimitSectionTotal) && $this->owner->Sections()->Count() >= $LimitSectionTotal) {
         // remove the buttons if we don't want to allow more records to be added/created
         $SectionGrid->removeComponentsByType('GridFieldAddNewButton');
         $SectionGrid->removeComponentsByType('GridFieldAddExistingAutocompleter');
         $SectionGrid->removeComponentsByType('GridFieldAddNewMultiClass');
     }
     if (!Permission::check("LINK_SECTIONS")) {
         $SectionGrid->removeComponentsByType('GridFieldAddExistingAutocompleter');
     }
     if (!Permission::check("REORDER_SECTIONS")) {
         $SectionGrid->removeComponentsByType('GridFieldOrderableRows');
     }
     if (!Permission::check("UNLINK_SECTIONS")) {
         $SectionGrid->removeComponentsByType('GridFieldDeleteAction');
     }
     $fields->addFieldToTab('Root.Section', GridField::create('Sections', 'Current Section(s)', $this->owner->Sections(), $SectionGrid));
     $fields->addFieldToTab('Root.Preview', UploadField::create('PreviewImage', 'Preview image'));
     return $fields;
 }
 /**
  * @return FieldList
  */
 public function updateCMSFields(FieldList $fields)
 {
     $allow_page_types = $this->owner->config()->SectionIncludedPageTypes;
     $allow_page_types[] = 'SectionsPage';
     if (in_array($this->owner->ClassName, $allow_page_types)) {
         $SectionSubClasses = ClassInfo::subclassesfor('Section');
         unset($SectionSubClasses['Section']);
         foreach ($SectionSubClasses as $key => $value) {
             $SectionSubClasses[$key] = Section::NiceClass($value);
         }
         $SectionGridConfig = GridFieldConfig_RelationEditor::create()->removeComponentsByType('GridFieldAddNewButton')->addComponent(new GridFieldAddNewMultiClass())->addComponent(new GridFieldOrderableRows('Sort'));
         $SectionGridConfig->getComponentByType('GridFieldAddNewMultiClass')->setClasses($SectionSubClasses);
         $fields->addFieldToTab('Root.Sections', GridField::create('Sections', 'Current Section(s)', $this->owner->Sections(), $SectionGridConfig));
     }
     return $fields;
 }
 /**
  * Lists all sections types and their settings relative to the current page type.
  * @return array
  */
 public function AvailableSectionTypes()
 {
     $AvailableTypes = ClassInfo::subclassesfor('Section');
     unset($AvailableTypes['Section']);
     # Get section options from each page type.
     $pageTypeOptions = Config::inst()->get($this->owner->ClassName, 'section_options');
     foreach ($AvailableTypes as $key => $value) {
         $Config = Config::inst();
         $selectable_option = true;
         if ($Config->get($value, 'selectable_option') !== null) {
             $selectable_option = $Config->get($value, 'selectable_option');
         }
         $AvailableTypes[$key] = array('classname' => $value, 'type' => Section::Type($value), 'presets' => $Config->get($value, 'presets'), 'selectable_option' => $selectable_option, 'limit' => $Config->get($value, 'limit'));
         if (isset($pageTypeOptions[$key])) {
             $AvailableTypes[$key] = array_merge($AvailableTypes[$key], $pageTypeOptions[$key]);
         }
         $AvailableTypes[$key]['limit_reached'] = false;
         if (isset($AvailableTypes[$key]['limit'])) {
             if ($AvailableTypes[$key]['limit'] == 0) {
                 $AvailableTypes[$key]['limit_reached'] = true;
             }
             $CurrentSectionCount = $this->owner->Sections()->filter('ClassName', $AvailableTypes[$key]['type'])->count();
             if ($CurrentSectionCount >= $AvailableTypes[$key]['limit']) {
                 $AvailableTypes[$key]['limit_reached'] = true;
             }
         }
     }
     return $AvailableTypes;
 }