public function __construct($canAdd = true, $canEdit = true, $canDelete = true, $editableRows = false, $aboveOrBelow = false)
    {
        parent::__construct();
        $this->blockManager = Injector::inst()->get('BlockManager');
        $controllerClass = Controller::curr()->class;
        // Get available Areas (for page) or all in case of ModelAdmin
        if ($controllerClass == 'CMSPageEditController') {
            $currentPage = Controller::curr()->currentPage();
            $areasFieldSource = $this->blockManager->getAreasForPageType($currentPage->ClassName);
        } else {
            $areasFieldSource = $this->blockManager->getAreasForTheme();
        }
        // EditableColumns only makes sense on Saveable parenst (eg Page), or inline changes won't be saved
        if ($editableRows) {
            $this->addComponent($editable = new GridFieldEditableColumns());
            $displayfields = array('TypeForGridfield' => array('title' => _t('Block.BlockType', 'Block Type'), 'field' => 'LiteralField'), 'Title' => array('title' => _t('Block.Title', 'Title'), 'field' => 'ReadonlyField'), 'BlockArea' => array('title' => _t('Block.BlockArea', 'Block Area') . '
						            ', 'callback' => function () use($areasFieldSource) {
                return DropdownField::create('BlockArea', 'Block Area', $areasFieldSource)->setHasEmptyDefault(true);
            }), 'isPublishedNice' => array('title' => _t('Block.IsPublishedField', 'Published'), 'field' => 'ReadonlyField'), 'UsageListAsString' => array('title' => _t('Block.UsageListAsString', 'Used on'), 'field' => 'LiteralField'));
            if ($aboveOrBelow) {
                $displayfields['AboveOrBelow'] = array('title' => _t('GridFieldConfigBlockManager.AboveOrBelow', 'Above or Below'), 'callback' => function () {
                    return DropdownField::create('AboveOrBelow', _t('GridFieldConfigBlockManager.AboveOrBelow', 'Above or Below'), BlockSet::config()->get('above_or_below_options'));
                });
            }
            $editable->setDisplayFields($displayfields);
        } else {
            $this->addComponent($dcols = new GridFieldDataColumns());
            $displayfields = array('singular_name' => _t('Block.BlockType', 'Block Type'), 'Title' => _t('Block.Title', 'Title'), 'BlockArea' => _t('Block.BlockArea', 'Block Area'), 'isPublishedNice' => _t('Block.IsPublishedField', 'Published'), 'UsageListAsString' => _t('Block.UsageListAsString', 'Used on'));
            $dcols->setDisplayFields($displayfields);
            $dcols->setFieldCasting(array('UsageListAsString' => 'HTMLText->Raw'));
        }
        $this->addComponent(new GridFieldButtonRow('before'));
        $this->addComponent(new GridFieldToolbarHeader());
        $this->addComponent(new GridFieldDetailForm());
        $this->addComponent($sort = new GridFieldSortableHeader());
        $this->addComponent($filter = new GridFieldFilterHeader());
        $this->addComponent(new GridFieldDetailForm());
        if ($controllerClass == 'BlockAdmin' && class_exists('GridFieldCopyButton')) {
            $this->addComponent(new GridFieldCopyButton());
        }
        $filter->setThrowExceptionOnBadDataType(false);
        $sort->setThrowExceptionOnBadDataType(false);
        if ($canAdd) {
            $multiClass = new GridFieldAddNewMultiClass();
            $classes = $this->blockManager->getBlockClasses();
            $multiClass->setClasses($classes);
            $this->addComponent($multiClass);
            //$this->addComponent(new GridFieldAddNewButton());
        }
        if ($canEdit) {
            $this->addComponent(new GridFieldEditButton());
        }
        if ($canDelete) {
            $this->addComponent(new GridFieldDeleteAction(true));
        }
        return $this;
    }
 function getGridFieldSiteTreeField($name, $title = null, SS_List $dataList = null)
 {
     $gf = GridField::create($name, $title, $dataList, $config = GridFieldConfig::create());
     $config->addComponents(new GridFieldSortableHeader(), $columns = new GridFieldDataColumns(), new GridFieldPaginator(15));
     $fields = array('getTreeTitle' => _t('SiteTree.PAGETITLE', 'Page Title'), 'singular_name' => _t('SiteTree.PAGETYPE'), 'LastEdited' => _t('SiteTree.LASTUPDATED', 'Last Updated'));
     $columns->setDisplayFields($fields);
     $columns->setFieldCasting(array('Created' => 'Datetime->Ago', 'LastEdited' => 'Datetime->Ago', 'getTreeTitle' => 'HTMLText'));
     $config->getComponentByType('GridFieldSortableHeader')->setFieldSorting(array('getTreeTitle' => 'Title'));
     $controller = $this;
     $columns->setFieldFormatting(array('getTreeTitle' => function ($value, &$item) use($controller) {
         return '<a class="action-detail" href="' . singleton('CMSPageEditController')->Link('show') . '/' . $item->ID . '">' . $item->TreeTitle . '</a>';
     }));
     return $gf;
 }
 public function getCMSFields()
 {
     $fields = new FieldList();
     $fields->push(new TabSet("Root", $mainTab = new Tab("Main")));
     $mainTab->setTitle(_t('SiteTree.TABMAIN', "Main"));
     $fields->addFieldToTab('Root.Main', new TextField('Title', _t('NewsletterAdmin.MailingListTitle', 'Mailing List Title')));
     $gridFieldConfig = GridFieldConfig::create()->addComponents(new GridFieldToolbarHeader(), new GridFieldSortableHeader(), $dataColumns = new GridFieldDataColumns(), new GridFieldFilterHeader(), new GridFieldDeleteAction(true), new GridFieldPaginator(30), new GridFieldAddNewButton(), new GridFieldDetailForm(), new GridFieldEditButton(), $autocompelete = new GridFieldAutocompleterWithFilter('before', array('FirstName', 'MiddleName', 'Surname', 'Email')));
     $dataColumns->setFieldCasting(array("Blacklisted" => "Boolean->Nice", "Verified" => "Boolean->Nice"));
     $autocompelete->filters = array("Blacklisted" => false);
     $recipientsGrid = GridField::create('Recipients', _t('NewsletterAdmin.Recipients', 'Mailing list recipients'), $this->Recipients(), $gridFieldConfig);
     $fields->addFieldToTab('Root.Main', new FieldGroup($recipientsGrid));
     $this->extend("updateCMSFields", $fields);
     if (!$this->ID) {
         $fields->removeByName('Recipients');
     }
     return $fields;
 }
 /**
  * @param $folderID The ID of the folder to display.
  * @return FormField
  */
 protected function getListField($folderID)
 {
     // Generate the folder selection field.
     $folderField = new TreeDropdownField('ParentID', _t('HtmlEditorField.FOLDER', 'Folder'), 'Folder');
     $folderField->setValue($folderID);
     // Generate the file list field.
     $config = GridFieldConfig::create();
     $config->addComponent(new GridFieldSortableHeader());
     $config->addComponent(new GridFieldFilterHeader());
     $config->addComponent($colsComponent = new GridFieldDataColumns());
     $colsComponent->setDisplayFields(array('StripThumbnail' => '', 'Title' => singleton('File')->fieldLabel('Title'), 'Created' => singleton('File')->fieldLabel('Created'), 'Size' => singleton('File')->fieldLabel('Size')));
     $colsComponent->setFieldCasting(array('Created' => 'SS_Datetime->Nice'));
     // Set configurable pagination for file list field
     $pageSize = Config::inst()->get(get_class($this), 'page_size');
     $config->addComponent(new GridFieldPaginator($pageSize));
     // If relation is to be autoset, we need to make sure we only list compatible objects.
     $baseClass = $this->parent->getRelationAutosetClass();
     // Create the data source for the list of files within the current directory.
     $files = DataList::create($baseClass)->exclude('ClassName', 'Folder');
     if ($folderID) {
         $files = $files->filter('ParentID', $folderID);
     }
     $fileField = new GridField('Files', false, $files, $config);
     $fileField->setAttribute('data-selectable', true);
     if ($this->parent->getAllowedMaxFileNumber() !== 1) {
         $fileField->setAttribute('data-multiselect', true);
     }
     $selectComposite = new CompositeField($folderField, $fileField);
     return $selectComposite;
 }
 /**
  * @param FieldList $fields
  */
 public function updateSettingsFields(FieldList $fields)
 {
     Requirements::javascript("contentreview/javascript/contentreview.js");
     // Display read-only version only
     if (!Permission::check("EDIT_CONTENT_REVIEW_FIELDS")) {
         $schedule = self::get_schedule();
         $contentOwners = ReadonlyField::create("ROContentOwners", _t("ContentReview.CONTENTOWNERS", "Content Owners"), $this->getOwnerNames());
         $nextReviewAt = DateField::create('RONextReviewDate', _t("ContentReview.NEXTREVIEWDATE", "Next review date"), $this->owner->NextReviewDate);
         if (!isset($schedule[$this->owner->ReviewPeriodDays])) {
             $reviewFreq = ReadonlyField::create("ROReviewPeriodDays", _t("ContentReview.REVIEWFREQUENCY", "Review frequency"), $schedule[0]);
         } else {
             $reviewFreq = ReadonlyField::create("ROReviewPeriodDays", _t("ContentReview.REVIEWFREQUENCY", "Review frequency"), $schedule[$this->owner->ReviewPeriodDays]);
         }
         $logConfig = GridFieldConfig::create()->addComponent(new GridFieldSortableHeader())->addComponent($logColumns = new GridFieldDataColumns());
         // Cast the value to the users preferred date format
         $logColumns->setFieldCasting(array("Created" => "DateTimeField->value"));
         $logs = GridField::create("ROReviewNotes", "Review Notes", $this->owner->ReviewLogs(), $logConfig);
         $optionsFrom = ReadonlyField::create("ROType", _t("ContentReview.SETTINGSFROM", "Options are"), $this->owner->ContentReviewType);
         $fields->addFieldsToTab("Root.ContentReview", array($contentOwners, $nextReviewAt->performReadonlyTransformation(), $reviewFreq, $optionsFrom, $logs));
         return;
     }
     $options = array();
     $options["Disabled"] = _t("ContentReview.DISABLE", "Disable content review");
     $options["Inherit"] = _t("ContentReview.INHERIT", "Inherit from parent page");
     $options["Custom"] = _t("ContentReview.CUSTOM", "Custom settings");
     $viewersOptionsField = OptionsetField::create("ContentReviewType", _t("ContentReview.OPTIONS", "Options"), $options);
     $users = Permission::get_members_by_permission(array("CMS_ACCESS_CMSMain", "ADMIN"));
     $usersMap = $users->map("ID", "Title")->toArray();
     asort($usersMap);
     $userField = ListboxField::create("OwnerUsers", _t("ContentReview.PAGEOWNERUSERS", "Users"), $usersMap)->setMultiple(true)->setAttribute("data-placeholder", _t("ContentReview.ADDUSERS", "Add users"))->setDescription(_t('ContentReview.OWNERUSERSDESCRIPTION', 'Page owners that are responsible for reviews'));
     $groupsMap = array();
     foreach (Group::get() as $group) {
         $groupsMap[$group->ID] = $group->getBreadcrumbs(" > ");
     }
     asort($groupsMap);
     $groupField = ListboxField::create("OwnerGroups", _t("ContentReview.PAGEOWNERGROUPS", "Groups"), $groupsMap)->setMultiple(true)->setAttribute("data-placeholder", _t("ContentReview.ADDGROUP", "Add groups"))->setDescription(_t("ContentReview.OWNERGROUPSDESCRIPTION", "Page owners that are responsible for reviews"));
     $reviewDate = DateField::create("NextReviewDate", _t("ContentReview.NEXTREVIEWDATE", "Next review date"))->setConfig("showcalendar", true)->setConfig("dateformat", "yyyy-MM-dd")->setConfig("datavalueformat", "yyyy-MM-dd")->setDescription(_t("ContentReview.NEXTREVIEWDATADESCRIPTION", "Leave blank for no review"));
     $reviewFrequency = DropdownField::create("ReviewPeriodDays", _t("ContentReview.REVIEWFREQUENCY", "Review frequency"), self::get_schedule())->setDescription(_t("ContentReview.REVIEWFREQUENCYDESCRIPTION", "The review date will be set to this far in the future whenever the page is published"));
     $notesField = GridField::create("ReviewNotes", "Review Notes", $this->owner->ReviewLogs(), GridFieldConfig_RecordEditor::create());
     $fields->addFieldsToTab("Root.ContentReview", array(new HeaderField(_t("ContentReview.REVIEWHEADER", "Content review"), 2), $viewersOptionsField, CompositeField::create($userField, $groupField, $reviewDate, $reviewFrequency)->addExtraClass("custom-settings"), ReadonlyField::create("ROContentOwners", _t("ContentReview.CONTENTOWNERS", "Content Owners"), $this->getOwnerNames()), ReadonlyField::create("RONextReviewDate", _t("ContentReview.NEXTREVIEWDATE", "Next review date"), $this->owner->NextReviewDate), $notesField));
 }
    public function __construct($canAdd = true, $canEdit = true, $canDelete = true, $editableRows = false, $aboveOrBelow = false)
    {
        parent::__construct();
        $this->blockManager = Injector::inst()->get('BlockManager');
        $controllerClass = Controller::curr()->class;
        // Get available Areas (for page) or all in case of ModelAdmin
        if ($controllerClass == 'CMSPageEditController') {
            $currentPage = Controller::curr()->currentPage();
            $areasFieldSource = $this->blockManager->getAreasForPageType($currentPage->ClassName);
        } else {
            $areasFieldSource = $this->blockManager->getAreasForTheme();
        }
        // EDIT
        $blockTypeArray = $this->blockManager->getBlockClasses();
        // /EDIT
        // EditableColumns only makes sense on Saveable parenst (eg Page), or inline changes won't be saved
        if ($editableRows) {
            // set project-dir in cookie to be accessible as fallback from js
            Cookie::set('js-project-dir', project(), 90, null, null, false, false);
            $this->addComponent($editable = new GridFieldEditableColumns());
            $displayfields = array('ClassName' => array('title' => _t('Block.BlockType', 'Block Type') . '
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', 'callback' => function () use($blockTypeArray) {
                return DropdownField::create('ClassName', 'Block Type', $blockTypeArray)->addExtraClass('select2blocktype')->setAttribute('data-project-dir', project());
            }), 'Title' => array('title' => _t('Block.TitleName', 'Block Name'), 'field' => 'TextField'), 'BlockArea' => array('title' => _t('Block.BlockArea', 'Block Area') . '
						&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', 'callback' => function () use($areasFieldSource) {
                return DropdownField::create('BlockArea', 'Block Area', $areasFieldSource)->setHasEmptyDefault(true);
            }), 'isPublishedNice' => array('title' => _t('Block.IsPublishedField', 'Published'), 'field' => 'ReadonlyField'), 'UsageListAsString' => array('title' => _t('Block.UsageListAsString', 'Used on'), 'field' => 'ReadonlyField'));
            if ($aboveOrBelow) {
                $displayfields['AboveOrBelow'] = array('title' => _t('GridFieldConfigBlockManager.AboveOrBelow', 'Above or Below'), 'callback' => function () {
                    return DropdownField::create('AboveOrBelow', _t('GridFieldConfigBlockManager.AboveOrBelow', 'Above or Below'), BlockSet::config()->get('above_or_below_options'));
                });
            }
            $editable->setDisplayFields($displayfields);
            // EDIT
            $this->addComponent($erow = new EditableBlockRow());
            // /EDIT
        } else {
            $this->addComponent($dcols = new GridFieldDataColumns());
            $displayfields = array('singular_name' => _t('Block.BlockType', 'Block Type'), 'Title' => _t('Block.TitleName', 'Block Name'), 'BlockArea' => _t('Block.BlockArea', 'Block Area'), 'isPublishedNice' => _t('Block.IsPublishedField', 'Published'), 'UsageListAsString' => _t('Block.UsageListAsString', 'Used on'));
            $dcols->setDisplayFields($displayfields);
            $dcols->setFieldCasting(array('UsageListAsString' => 'HTMLText->Raw'));
        }
        $this->addComponent(new GridFieldButtonRow('before'));
        // EDIT
        $this->addComponent(new GridFieldButtonRow('after'));
        // /EDIT
        $this->addComponent(new GridFieldToolbarHeader());
        $this->addComponent(new GridFieldDetailForm());
        // EDIT
        //$this->addComponent($sort = new GridFieldSortableHeader());
        //$this->addComponent($filter = new GridFieldFilterHeader());
        //$this->addComponent(new GridFieldDetailForm());
        //$filter->setThrowExceptionOnBadDataType(false);
        //$sort->setThrowExceptionOnBadDataType(false);
        // load enhancements module (eg inline editing etc, needs save action @TODO: move to SiteTree only?
        if (class_exists('GF_BlockEnhancements')) {
            $this->addComponent(new GF_BlockEnhancements());
        }
        // stuff only for BlockAdmin
        if ($controllerClass == 'BlockAdmin') {
            $this->addComponent($sort = new GridFieldSortableHeader());
            $sort->setThrowExceptionOnBadDataType(false);
            $this->addComponent($filter = new GridFieldFilterHeader());
            $filter->setThrowExceptionOnBadDataType(false);
        } else {
            // only for GF on SiteTree
            $this->addComponent(new GridFieldTitleHeader());
            $this->addComponent(new GridFieldFooter());
            // groupable
            $this->addComponent(new GridFieldGroupable('BlockArea', 'Area', 'none', $areasFieldSource));
            //            var_dump($areasFieldSource);
            //            // Get available Areas (for page) enhancements inactive when in ModelAdmin/BlockAdmin
            //            if (Controller::curr() && Controller::curr()->class == 'CMSPageEditController') {
            //                // Provide defined blockAreas to JS
            //                $blockManager = Injector::inst()->get('BlockManager');
            ////            $blockAreas = $blockManager->getAreasForPageType( Controller::curr()->currentPage()->ClassName );
            //                $blockAreas = $blockManager->getAreasForPageType( Controller::curr()->currentPage()->ClassName );
            //            }
        }
        if ($canAdd) {
            $multiClass = new GridFieldAddNewMultiClass('after');
            $classes = $this->blockManager->getBlockClasses();
            $multiClass->setClasses($classes);
            $this->addComponent($multiClass);
            //$this->addComponent(new GridFieldAddNewButton());
        }
        // /EDIT
        if ($controllerClass == 'BlockAdmin' && class_exists('GridFieldCopyButton')) {
            $this->addComponent(new GridFieldCopyButton());
        }
        if ($canEdit) {
            $this->addComponent(new GridFieldEditButton());
        }
        if ($canDelete) {
            $this->addComponent(new GridFieldDeleteAction(true));
        }
        return $this;
    }