/** * Generate export fields for EXCEL. * * @param GridField $gridField * @return array */ public function generateExportFileData($gridField) { $excelColumns = $this->exportColumns ? $this->exportColumns : singleton($gridField->getModelClass())->summaryFields(); $objPHPExcel = new \PHPExcel\Spreadsheet(); $worksheet = $objPHPExcel->getActiveSheet()->setTitle($gridField->getModelClass()); $col = 'A'; foreach ($excelColumns as $columnSource => $columnHeader) { $heading = !is_string($columnHeader) && is_callable($columnHeader) ? $columnSource : $columnHeader; $worksheet->setCellValue($col . '1', $heading); $col++; } $worksheet->freezePane('A2'); $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); } } $row = 2; foreach ($items->limit(null) as $item) { $columnData = array(); $col = 'A'; foreach ($excelColumns 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); } $worksheet->getCell($col . $row)->setValueExplicit($value, PHPExcel\Cell\DataType::TYPE_STRING); $col++; } $row++; $item->destroy(); } $writer = PHPExcel\IOFactory::createWriter($objPHPExcel, 'Excel2007'); ob_start(); $writer->save('php://output'); $data = ob_get_clean(); return $data; }
/** * Helper function to extract the item list out of the GridField. * @param GridField $gridField * @return SS_list */ protected function getItems(GridField $gridField) { $gridField->getConfig()->removeComponentsByType('GridFieldPaginator'); $items = $gridField->getManipulatedList(); foreach ($gridField->getConfig()->getComponents() as $component) { if ($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) { $items = $component->getManipulatedData($gridField, $items); } } $arrayList = new ArrayList(); foreach ($items->limit(null) as $item) { if (!$item->hasMethod('canView') || $item->canView()) { $arrayList->add($item); } } return $arrayList; }
public function testChainedDataManipulators() { $config = new GridFieldConfig(); $data = new ArrayList(array(1, 2, 3, 4, 5, 6)); $gridField = new GridField('testfield', 'testfield', $data, $config); $endList = $gridField->getManipulatedList(); $this->assertEquals($endList->Count(), 6); $config->addComponent(new GridFieldTest_Component2()); $endList = $gridField->getManipulatedList(); $this->assertEquals($endList->Count(), 12); $config->addComponent(new GridFieldPaginator(10)); $endList = $gridField->getManipulatedList(); $this->assertEquals($endList->Count(), 10); }
/** * Generate export fields for CSV. * * @param GridField $gridField * @return array */ public function generateExportFileData($gridField) { $separator = $this->csvSeparator; $csvColumns = $this->exportColumns ? $this->exportColumns : singleton($gridField->getModelClass())->summaryFields(); $fileData = ''; $columnData = array(); $fieldItems = new ArrayList(); if ($this->csvHasHeader) { $headers = array(); // determine the CSV headers. If a field is callable (e.g. anonymous function) then use the // source name as the header instead foreach ($csvColumns as $columnSource => $columnHeader) { $headers[] = !is_string($columnHeader) && is_callable($columnHeader) ? $columnSource : $columnHeader; } $fileData .= "\"" . implode("\"{$separator}\"", array_values($headers)) . "\""; $fileData .= "\n"; } $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) { $columnData = array(); foreach ($csvColumns 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); } $value = str_replace(array("\r", "\n"), "\n", $value); $columnData[] = '"' . str_replace('"', '\\"', $value) . '"'; } $fileData .= implode($separator, $columnData); $fileData .= "\n"; $item->destroy(); } return $fileData; }
/** * Handles requests to move an item to the previous or next page. */ public function handleMoveToPage(GridField $grid, $request) { if (!($paginator = $grid->getConfig()->getComponentByType('GridFieldPaginator'))) { $this->httpError(404, 'Paginator component not found'); } $move = $request->postVar('move'); $field = $this->getSortField(); $list = $grid->getList(); $manip = $grid->getManipulatedList(); $existing = $manip->map('ID', $field)->toArray(); $values = $existing; $order = array(); $id = isset($move['id']) ? (int) $move['id'] : null; $to = isset($move['page']) ? $move['page'] : null; if (!isset($values[$id])) { $this->httpError(400, 'Invalid item ID'); } $this->populateSortValues($list); $page = (int) $grid->getState()->GridFieldPaginator->currentPage ?: 1; $per = $paginator->getItemsPerPage(); if ($to == 'prev') { $swap = $list->limit(1, ($page - 1) * $per - 1)->first(); $values[$swap->ID] = $swap->{$field}; $order[] = $id; $order[] = $swap->ID; foreach ($existing as $_id => $sort) { if ($id != $_id) { $order[] = $_id; } } } elseif ($to == 'next') { $swap = $list->limit(1, $page * $per)->first(); $values[$swap->ID] = $swap->{$field}; foreach ($existing as $_id => $sort) { if ($id != $_id) { $order[] = $_id; } } $order[] = $swap->ID; $order[] = $id; } else { $this->httpError(400, 'Invalid page target'); } $this->reorderItems($list, $values, $order); return $grid->FieldHolder(); }
/** * 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; }
/** * Generate export fields for CSV. * * @param GridField $gridField * @return array */ public function generateExportFileData($gridField) { $csvColumns = $this->exportColumns ? $this->exportColumns : singleton($gridField->getModelClass())->summaryFields(); $fileData = array(); if ($this->csvHasHeader) { $headers = array(); // determine the CSV headers. If a field is callable (e.g. anonymous function) then use the // source name as the header instead foreach ($csvColumns as $columnSource => $columnHeader) { $headers[] = !is_string($columnHeader) && is_callable($columnHeader) ? $columnSource : $columnHeader; } $fileData[] = $headers; } //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()) { $columnData = array(); foreach ($csvColumns 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) { $value = $gridField->getDataFieldValue($item, $columnHeader); } } $columnData[] = $value; } $fileData[] = $columnData; } if ($item->hasMethod('destroy')) { $item->destroy(); } } // Convert the $fileData array into csv by capturing fputcsv's output $csv = fopen('php://temp', 'r+'); foreach ($fileData as $line) { fputcsv($csv, $line, $this->csvSeparator, $this->csvEnclosure); } rewind($csv); return stream_get_contents($csv); }
/** * Generate export fields for EXCEL. * * @param GridField $gridField * @return array */ public function generateExportFileData($gridField) { $excelColumns = $this->exportColumns ? $this->exportColumns : singleton($gridField->getModelClass())->summaryFields(); $objPHPExcel = new PHPExcel(); $worksheet = $objPHPExcel->getActiveSheet()->setTitle(singleton($gridField->getModelClass())->i18n_singular_name()); $col = 'A'; foreach ($excelColumns as $columnSource => $columnHeader) { $heading = !is_string($columnHeader) && is_callable($columnHeader) ? $columnSource : $columnHeader; $worksheet->setCellValue($col . '1', $heading); $col++; } $worksheet->freezePane('A2'); $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); } } $row = 2; foreach ($items->limit(null) as $item) { if (!$item->hasMethod('canView') || $item->canView()) { $columnData = array(); $col = 'A'; foreach ($excelColumns 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); $worksheet->getCell($col . $row)->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING); } else { $component = $item; $value = $gridField->getDataFieldValue($item, $columnSource); if (strpos($columnSource, '.') !== false) { $relations = explode('.', $columnSource); foreach ($relations as $relation) { if ($component->hasMethod($relation)) { $component = $component->{$relation}(); } elseif ($component instanceof SS_List) { $component = $component->relation($relation); } elseif ($component instanceof DataObject && ($dbObject = $component->obj($relation, null, false))) { $component = $dbObject; } } } elseif ($component instanceof DataObject && ($dbObject = $component->obj($columnSource, null, false))) { $component = $dbObject; } if (!$value) { $component = $item; $value = $gridField->getDataFieldValue($item, $columnHeader); if (strpos($columnHeader, '.') !== false) { $relations = explode('.', $columnHeader); foreach ($relations as $relation) { if ($component->hasMethod($relation)) { $component = $component->{$relation}(); } elseif ($component instanceof SS_List) { $component = $component->relation($relation); } elseif ($component instanceof DataObject && ($dbObject = $component->obj($relation, null, false))) { $component = $dbObject; } } } elseif ($component instanceof DataObject && ($dbObject = $component->obj($columnHeader, null, false))) { $component = $dbObject; } } if ($component && ($component instanceof Decimal || $component instanceof Float || $component instanceof Int || is_numeric($component))) { $worksheet->getCell($col . $row)->setValue($value); } else { $worksheet->getCell($col . $row)->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING); } } $col++; } $row++; } if ($item->hasMethod('destroy')) { $item->destroy(); } } $writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); ob_start(); $writer->save('php://output'); $data = ob_get_clean(); return $data; }