function updateEditForm(Form $form) { /* @var $owner ModelAdmin */ $owner = $this->owner; $class = $owner->modelClass; $classConfig = $owner->config(); $gridfield = $form->Fields()->dataFieldByName($class); if (!$gridfield) { return; } /* @var $config GridFieldConfig */ $config = $gridfield->getConfig(); // Bulk manage if ($classConfig->bulk_manage && class_exists('GridFieldBulkManager')) { $already = $config->getComponentByType('GridFieldBulkManager'); if (!$already) { $config->addComponent($bulkManager = new GridFieldBulkManager()); $bulkManager->removeBulkAction('unLink'); } } if ($classConfig->export_csv) { /* @var $export GridFieldExportButton */ $export = $config->getComponentByType('GridFieldExportButton'); $export->setExportColumns(ExcelImportExport::exportFieldsForClass($class)); } else { $config->removeComponentsByType('GridFieldExportButton'); } if ($classConfig->export_excel) { $config->addComponent(new ExcelGridFieldExportButton('buttons-before-left')); } }
/** * Generate export fields for Excel. * * @param GridField $gridField * @return PHPExcel */ public function generateExportFileData($gridField) { $class = $gridField->getModelClass(); $columns = $this->exportColumns ? $this->exportColumns : ExcelImportExport::exportFieldsForClass($class); $fileData = ''; $singl = singleton($class); $singular = $class ? $singl->i18n_singular_name() : ''; $plural = $class ? $singl->i18n_plural_name() : ''; $filter = new FileNameFilter(); if ($this->exportName) { $this->exportName = $filter->filter($this->exportName); } else { $this->exportName = $filter->filter('export-' . $plural); } $excel = new PHPExcel(); $excelProperties = $excel->getProperties(); $excelProperties->setTitle($this->exportName); $sheet = $excel->getActiveSheet(); if ($plural) { $sheet->setTitle($plural); } $row = 1; $col = 0; if ($this->hasHeader) { $headers = array(); // determine the headers. If a field is callable (e.g. anonymous function) then use the // source name as the header instead foreach ($columns as $columnSource => $columnHeader) { $headers[] = !is_string($columnHeader) && is_callable($columnHeader) ? $columnSource : $columnHeader; } foreach ($headers as $header) { $sheet->setCellValueByColumnAndRow($col, $row, $header); $col++; } $endcol = PHPExcel_Cell::stringFromColumnIndex($col - 1); $sheet->setAutoFilter("A1:{$endcol}1"); $sheet->getStyle("A1:{$endcol}1")->getFont()->setBold(true); $col = 0; $row++; } // Autosize $cellIterator = $sheet->getRowIterator()->current()->getCellIterator(); try { $cellIterator->setIterateOnlyExistingCells(true); } catch (Exception $ex) { continue; } foreach ($cellIterator as $cell) { $sheet->getColumnDimension($cell->getColumn())->setAutoSize(true); } //Remove GridFieldPaginator as we're going to export the entire list. $gridField->getConfig()->removeComponentsByType('GridFieldPaginator'); $items = $gridField->getManipulatedList(); // @todo should GridFieldComponents change behaviour based on whether others are available in the config? foreach ($gridField->getConfig()->getComponents() as $component) { if ($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) { $items = $component->getManipulatedData($gridField, $items); } } foreach ($items->limit(null) as $item) { if (!$item->hasMethod('canView') || $item->canView()) { foreach ($columns as $columnSource => $columnHeader) { if (!is_string($columnHeader) && is_callable($columnHeader)) { if ($item->hasMethod($columnSource)) { $relObj = $item->{$columnSource}(); } else { $relObj = $item->relObject($columnSource); } $value = $columnHeader($relObj); } else { $value = $gridField->getDataFieldValue($item, $columnSource); if ($value === null) { $value = $gridField->getDataFieldValue($item, $columnHeader); } } $value = str_replace(array("\r", "\n"), "\n", $value); $sheet->setCellValueByColumnAndRow($col, $row, $value); $col++; } } if ($item->hasMethod('destroy')) { $item->destroy(); } $col = 0; $row++; } return $excel; }
protected function setupGridField(GridField $gridfield, Config_ForClass $classConfig) { if (!$gridfield) { return; } $config = $gridfield->getConfig(); $class = $gridfield->getModelClass(); // More item per page $paginator = $config->getComponentByType('GridFieldPaginator'); $paginator->setItemsPerPage(50); // Bulk manage if ($classConfig->bulk_manage && class_exists('GridFieldBulkManager')) { $already = $config->getComponentByType('GridFieldBulkManager'); if (!$already) { $config->addComponent($bulkManager = new GridFieldBulkManager()); $bulkManager->removeBulkAction('unLink'); } } // Better export if ($classConfig->export_csv) { /* @var $export GridFieldExportButton */ $export = $config->getComponentByType('GridFieldExportButton'); $export->setExportColumns(ExcelImportExport::exportFieldsForClass($class)); } else { $config->removeComponentsByType('GridFieldExportButton'); } if ($classConfig->export_excel) { if ($class == 'Group') { $config->addComponent(new GridFieldButtonRow('after')); } $config->addComponent(new ExcelGridFieldExportButton('buttons-after-left')); } }