| Total Complexity | 45 |
| Total Lines | 327 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like GridFieldExportButton often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GridFieldExportButton, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class GridFieldExportButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var array Map of a property name on the exported objects, with values being the column title in the CSV file. |
||
| 18 | * Note that titles are only used when {@link $csvHasHeader} is set to TRUE. |
||
| 19 | */ |
||
| 20 | protected $exportColumns; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $csvSeparator = ","; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $csvEnclosure = '"'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var boolean |
||
| 34 | */ |
||
| 35 | protected $csvHasHeader = true; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Fragment to write the button to |
||
| 39 | */ |
||
| 40 | protected $targetFragment; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Set to true to disable XLS sanitisation |
||
| 44 | * [SS-2017-007] Ensure all cells with leading [@=+] have a leading tab |
||
| 45 | * |
||
| 46 | * @config |
||
| 47 | * @var bool |
||
| 48 | */ |
||
| 49 | private static $xls_export_disabled = false; |
||
|
|
|||
| 50 | |||
| 51 | /** |
||
| 52 | * @param string $targetFragment The HTML fragment to write the button into |
||
| 53 | * @param array $exportColumns The columns to include in the export |
||
| 54 | */ |
||
| 55 | public function __construct($targetFragment = "after", $exportColumns = null) |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Place the export button in a <p> tag below the field |
||
| 63 | * |
||
| 64 | * @param GridField $gridField |
||
| 65 | * |
||
| 66 | * @return array |
||
| 67 | */ |
||
| 68 | public function getHTMLFragments($gridField) |
||
| 69 | { |
||
| 70 | $button = new GridField_FormAction( |
||
| 71 | $gridField, |
||
| 72 | 'export', |
||
| 73 | _t('SilverStripe\\Forms\\GridField\\GridField.CSVEXPORT', 'Export to CSV'), |
||
| 74 | 'export', |
||
| 75 | null |
||
| 76 | ); |
||
| 77 | $button->addExtraClass('btn btn-secondary no-ajax font-icon-down-circled action_export'); |
||
| 78 | $button->setForm($gridField->getForm()); |
||
| 79 | return [ |
||
| 80 | $this->targetFragment => $button->Field(), |
||
| 81 | ]; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * export is an action button |
||
| 86 | * |
||
| 87 | * @param GridField $gridField |
||
| 88 | * |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | public function getActions($gridField) |
||
| 94 | } |
||
| 95 | |||
| 96 | public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
||
| 97 | { |
||
| 98 | if ($actionName == 'export') { |
||
| 99 | return $this->handleExport($gridField); |
||
| 100 | } |
||
| 101 | return null; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * it is also a URL |
||
| 106 | * |
||
| 107 | * @param GridField $gridField |
||
| 108 | * |
||
| 109 | * @return array |
||
| 110 | */ |
||
| 111 | public function getURLHandlers($gridField) |
||
| 115 | ]; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Handle the export, for both the action button and the URL |
||
| 120 | * |
||
| 121 | * @param GridField $gridField |
||
| 122 | * @param HTTPRequest $request |
||
| 123 | * |
||
| 124 | * @return HTTPResponse |
||
| 125 | */ |
||
| 126 | public function handleExport($gridField, $request = null) |
||
| 127 | { |
||
| 128 | $now = date("d-m-Y-H-i"); |
||
| 129 | $fileName = "export-$now.csv"; |
||
| 130 | |||
| 131 | if ($fileData = $this->generateExportFileData($gridField)) { |
||
| 132 | return HTTPRequest::send_file($fileData, $fileName, 'text/csv'); |
||
| 133 | } |
||
| 134 | return null; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Return the columns to export |
||
| 139 | * |
||
| 140 | * @param GridField $gridField |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | protected function getExportColumnsForGridField(GridField $gridField) |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Generate export fields for CSV. |
||
| 161 | * |
||
| 162 | * @param GridField $gridField |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function generateExportFileData($gridField) |
||
| 167 | { |
||
| 168 | $csvColumns = $this->getExportColumnsForGridField($gridField); |
||
| 169 | |||
| 170 | $csvWriter = Writer::createFromFileObject(new \SplTempFileObject()); |
||
| 171 | $csvWriter->setDelimiter($this->getCsvSeparator()); |
||
| 172 | $csvWriter->setEnclosure($this->getCsvEnclosure()); |
||
| 173 | $csvWriter->setNewline("\r\n"); //use windows line endings for compatibility with some csv libraries |
||
| 174 | $csvWriter->setOutputBOM(Writer::BOM_UTF8); |
||
| 175 | |||
| 176 | if (!Config::inst()->get(get_class($this), 'xls_export_disabled')) { |
||
| 177 | $csvWriter->addFormatter(function (array $row) { |
||
| 178 | foreach ($row as &$item) { |
||
| 179 | // [SS-2017-007] Sanitise XLS executable column values with a leading tab |
||
| 180 | if (preg_match('/^[-@=+].*/', $item)) { |
||
| 181 | $item = "\t" . $item; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | return $row; |
||
| 185 | }); |
||
| 186 | } |
||
| 187 | |||
| 188 | if ($this->csvHasHeader) { |
||
| 189 | $headers = []; |
||
| 190 | |||
| 191 | // determine the CSV headers. If a field is callable (e.g. anonymous function) then use the |
||
| 192 | // source name as the header instead |
||
| 193 | foreach ($csvColumns as $columnSource => $columnHeader) { |
||
| 194 | if (is_array($columnHeader) && array_key_exists('title', $columnHeader)) { |
||
| 195 | $headers[] = $columnHeader['title']; |
||
| 196 | } else { |
||
| 197 | $headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) ? $columnSource : $columnHeader; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | $csvWriter->insertOne($headers); |
||
| 202 | unset($headers); |
||
| 203 | } |
||
| 204 | |||
| 205 | //Remove GridFieldPaginator as we're going to export the entire list. |
||
| 206 | $gridField->getConfig()->removeComponentsByType(GridFieldPaginator::class); |
||
| 207 | |||
| 208 | $items = $gridField->getManipulatedList(); |
||
| 209 | |||
| 210 | // @todo should GridFieldComponents change behaviour based on whether others are available in the config? |
||
| 211 | foreach ($gridField->getConfig()->getComponents() as $component) { |
||
| 212 | if ($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) { |
||
| 213 | $items = $component->getManipulatedData($gridField, $items); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /** @var GridFieldDataColumns|null $gridFieldColumnsComponent */ |
||
| 218 | $gridFieldColumnsComponent = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class); |
||
| 219 | $columnsHandled = ($gridFieldColumnsComponent) |
||
| 220 | ? $gridFieldColumnsComponent->getColumnsHandled($gridField) |
||
| 221 | : []; |
||
| 222 | |||
| 223 | /** @var DataObject $item */ |
||
| 224 | foreach ($items->limit(null) as $item) { |
||
| 225 | if (!$item->hasMethod('canView') || $item->canView()) { |
||
| 226 | $columnData = []; |
||
| 227 | |||
| 228 | foreach ($csvColumns as $columnSource => $columnHeader) { |
||
| 229 | if (!is_string($columnHeader) && is_callable($columnHeader)) { |
||
| 230 | if ($item->hasMethod($columnSource)) { |
||
| 231 | $relObj = $item->{$columnSource}(); |
||
| 232 | } else { |
||
| 233 | $relObj = $item->relObject($columnSource); |
||
| 234 | } |
||
| 235 | |||
| 236 | $value = $columnHeader($relObj); |
||
| 237 | } elseif ($gridFieldColumnsComponent && array_key_exists($columnSource, $columnsHandled)) { |
||
| 238 | $value = strip_tags( |
||
| 239 | $gridFieldColumnsComponent->getColumnContent($gridField, $item, $columnSource) |
||
| 240 | ); |
||
| 241 | } else { |
||
| 242 | $value = $gridField->getDataFieldValue($item, $columnSource); |
||
| 243 | |||
| 244 | if ($value === null) { |
||
| 245 | $value = $gridField->getDataFieldValue($item, $columnHeader); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | |||
| 249 | $columnData[] = $value; |
||
| 250 | } |
||
| 251 | |||
| 252 | $csvWriter->insertOne($columnData); |
||
| 253 | } |
||
| 254 | |||
| 255 | if ($item->hasMethod('destroy')) { |
||
| 256 | $item->destroy(); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | if (method_exists($csvWriter, 'getContent')) { |
||
| 261 | return $csvWriter->getContent(); |
||
| 262 | } |
||
| 263 | |||
| 264 | return (string)$csvWriter; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return array |
||
| 269 | */ |
||
| 270 | public function getExportColumns() |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param array $cols |
||
| 277 | * |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function setExportColumns($cols) |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function getCsvSeparator() |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $separator |
||
| 296 | * |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function setCsvSeparator($separator) |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public function getCsvEnclosure() |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $enclosure |
||
| 315 | * |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function setCsvEnclosure($enclosure) |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return boolean |
||
| 326 | */ |
||
| 327 | public function getCsvHasHeader() |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param boolean $bool |
||
| 334 | * |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | public function setCsvHasHeader($bool) |
||
| 343 |