コード例 #1
0
 public function getCMSFields()
 {
     //scaffold cms fields without calling 'updateCMSFields'
     $fields = $this->scaffoldFormFields(array('includeRelations' => $this->ID > 0, 'tabbed' => true, 'ajaxSafe' => true));
     if ($grid = $fields->fieldByName("Root.ProductSelections.ProductSelections")) {
         //move field to main tab
         $fields->removeByName("ProductSelections");
         $fields->addFieldToTab("Root.Main", $grid);
         $grid->setConfig($conf = new GridFieldConfig_RecordEditor());
         $conf->removeComponentsByType("GridFieldDataColumns")->removeComponentsByType("GridFieldDataColumns")->removeComponentsByType("GridFieldFilterHeader")->removeComponentsByType("GridFieldPaginator")->removeComponentsByType("GridFieldPageCount")->addComponent(new GridFieldOrderableRows())->addComponent(new GridFieldEditableColumns());
         $summaryfields = MenuProductSelection::config()->summary_fields;
         unset($summaryfields['Group']);
         //add editable group column to grid
         $groups = $this->Groups();
         if ($groups->exists()) {
             $dropdown = DropdownField::create("GroupID", 'Grouping', $groups->map('ID', 'Title')->toArray())->setHasEmptyDefault(true);
             $summaryfields['GroupID'] = array('title' => 'Group', 'callback' => function ($record, $column, $grid) use($dropdown) {
                 return $dropdown;
             });
             $conf->getComponentByType('GridFieldDetailForm')->setItemEditFormCallback(function ($form, $component) use($dropdown) {
                 $fields = $form->Fields();
                 if (!$fields->fieldByName("GroupID")) {
                     $fields->push($dropdown);
                 }
             });
         }
         $conf->getComponentByType('GridFieldEditableColumns')->setDisplayFields($summaryfields);
         //re-add edit/delete row actions so they are in the correct order
         $conf->removeComponentsByType("GridFieldEditButton")->removeComponentsByType("GridFieldDeleteAction")->addComponent(new GridFieldEditButton())->addComponent(new GridFieldDeleteAction())->addComponent($importer = new GridFieldImporter('before'));
         $loader = $importer->getLoader($grid);
         $self = $this;
         $loader->mappableFields = array('Product.InternalItemID' => 'SKU / Product Identifier', 'Group.Title' => 'Group');
         $loader->transforms = array("Product.InternalItemID" => array("create" => false, "link" => true, "required" => true), "Group.Title" => array('list' => $this->Groups()));
         $loader->duplicateChecks = array("Product.InternalItemID" => "Product.InternalItemID");
     }
     if ($grid = $fields->fieldByName("Root.Groups.Groups")) {
         $conf = $grid->getConfig()->removeComponentsByType("GridFieldAddExistingAutocompleter")->removeComponentsByType('GridFieldDataColumns')->removeComponentsByType('GridFieldAddNewButton')->removeComponentsByType("GridFieldFilterHeader")->removeComponentsByType("GridFieldPaginator")->removeComponentsByType("GridFieldPageCount")->addComponent($newbutton = new GridFieldAddNewInlineButton())->addComponent(new GridFieldEditableColumns())->addComponent(new GridFieldOrderableRows());
         $newbutton->setTitle("Add Menu Grouping");
         $conf->getComponentByType('GridFieldEditableColumns')->setDisplayFields(array('Title' => function ($record, $column, $grid) {
             return new TextField($column, 'Title');
         }));
         //re-add edit/delete row actions so they are in the correct order
         $conf->removeComponentsByType("GridFieldEditButton")->removeComponentsByType("GridFieldDeleteAction")->addComponent(new GridFieldDeleteAction());
     }
     $this->extend('updateCMSFields', $fields);
     return $fields;
 }
 /**
  * Do the import using the configured importer.
  * @param  string $filepath
  * @param  array|null $colmap
  * @return BulkLoader_Result
  */
 public function importFile($filepath, $colmap = null, $hasheader = true, $cleardata = false)
 {
     $loader = $this->component->getLoader($this->gridField);
     $loader->deleteExistingRecords = $cleardata;
     //set or merge in given col map
     if (is_array($colmap)) {
         $loader->columnMap = $loader->columnMap ? array_merge($loader->columnMap, $colmap) : $colmap;
     }
     $loader->getSource()->setFilePath($filepath)->setHasHeader($hasheader);
     return $loader->load();
 }