Source of file SilverStripeCsvUtility.php
Size: 6,917 Bytes - Last Modified: 2021-12-24T06:45:32+00:00
/var/www/docs.ssmods.com/process/src/src/utility/SilverStripeCsvUtility.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 | <?php namespace Dynamic\CsvUtility\Utility; use \DataObject; use \Injector; /** * Class SilverStripeCsvUtility */ class SilverStripeCsvUtility extends CsvUtility { /** * @var string */ private $relation_name; /** * @var string */ private $data_object_class; /** * @var */ private $request; /** * @var array */ private $allowed_report_types = []; /** * @var string */ private $report_type; /** * @var array */ private $pattern; /** * @var string */ private $utility_interface = 'Dynamic\CsvUtility\UtilInterface\CsvUtilityInterface'; public function __construct($data, $request) { parent::__construct($data); $this->setRequest($request); } /** * Override CsvUtility::getHeaderFields() to generate header fields based on the model's declaration * * @return $this */ public function getHeaderFields() { return array_values($this->getPattern()); } /** * @param $relationName * @return $this */ public function setRelationName($relationName) { $this->relation_name = $relationName; return $this; } /** * @return mixed */ public function getRelationName() { return $this->relation_name; } /** * @param string $dataObjectClass * @return $this */ public function setDataObjectClass($dataObjectClass = '') { if ($dataObjectClass === '' || !is_string($dataObjectClass)) { $reportType = $this->getReportType(); $dataObjectClass = $this->getAllowedReportTypes()[$reportType]; } $this->data_object_class = $dataObjectClass; return $this; } /** * @return string */ public function getDataObjectClass() { if (!$this->data_object_class) { $this->setDataObjectClass(); } return $this->data_object_class; } /** * @param $request * @return $this */ public function setRequest($request) { $this->request = $request; return $this; } /** * @return mixed */ public function getRequest() { return $this->request; } /** * @param array $types * @return $this */ public function setAllowedReportTypes($types = []) { if ((array)$types === $types && !empty($types)) { $this->allowed_report_types = $types; } return $this; } /** * @return array */ public function getAllowedReportTypes() { return $this->allowed_report_types; } /** * @return $this */ public function setReportType() { if (!$request = $this->getRequest()) { user_error("The \$request isn't accessible or isn't set.", E_USER_ERROR); } if (!$reportRequestType = $request->getVar('ReportType')) { user_error("The request variable \"ReportType\" isn't accessible or isn't set.", E_USER_ERROR); } if (!array_key_exists($reportRequestType, $this->getAllowedReportTypes()) ) { user_error("The requested report type \"{$reportRequestType}\" isn't allowed.", E_USER_ERROR); } $this->report_type = $reportRequestType; return $this; } /** * @return string */ public function getReportType() { if (!$this->report_type) { $this->setReportType(); } return $this->report_type; } /** * @param $interface * @return $this */ public function setUtilityInterface($interface) { $this->utility_interface = $interface; return $this; } /** * @return string */ public function getUtilityInterface() { return $this->utility_interface; } /** * @return $this */ public function setPattern() { $class = $this->getDataObjectClass(); $extensions = Injector::inst()->create($class)->getExtensionInstances(); $extensions[$class] = $class; if (!$this->getImplementsUtilInterface($extensions)) { user_error("Class {$class} is required to implement {$this->getUtilityInterface()} before a report can be generated.", E_USER_ERROR); } $this->pattern = Injector::inst()->create($class)->getExportFields(); return $this; } /** * @param array $extensions * @return bool */ public function getImplementsUtilInterface($extensions = []) { $implements = false; foreach ($extensions as $key => $val) { if (!$implements) { if (in_array($this->getUtilityInterface(), class_implements($key))) { $implements = true; } } } return $implements; } /** * @return array */ public function getPattern() { if (!$this->pattern) { $this->setPattern(); } return $this->pattern; } /** * Allow for traversing a single has_one relation level through dot notation * * @param $data * @return array */ public function preProcessData($data) { $data = parent::preProcessData($data); $arrayData = []; $pattern = $this->getPattern(); $relationName = $this->getRelationName(); if ($data instanceof DataObject) { $addToArrayData = function ($key, $val) use ( &$data, &$relationName, &$arrayData ) { if (strpos($key, '.') && $relationName && is_string($relationName) ) { $parts = explode('.', $key); if (count($parts) == 3) { $relationCall = ($parts[0] == $relationName) ? $relationName : $parts[0]; $secondRelationCall = $parts[1]; $varName = $parts[2]; $value = $data->$relationCall() ->$secondRelationCall()->$varName; } else { $relationCall = ($parts[0] == $relationName) ? $relationName : $parts[0]; $varName = $parts[1]; $value = $data->$relationCall()->$varName; } } else { $value = $data->$key; } $arrayData[] = $value; }; foreach ($pattern as $key => $val) { $addToArrayData($key, $val); } } return $arrayData; } } |