Source of file HTTPBulkToolsResponse.php
Size: 10,090 Bytes - Last Modified: 2021-12-24T06:41:11+00:00
/var/www/docs.ssmods.com/process/src/src/BulkTools/HTTPBulkToolsResponse.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 | <?php namespace Colymba\BulkTools; use SilverStripe\Control\HTTPResponse; use SilverStripe\Core\Convert; use SilverStripe\Forms\GridField\GridField; use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\SS_List; use SilverStripe\View\HTML; /** * Custom HTTPResponse for all bulk tools to use * for a unified response format and facilitate forn-end handling * * Add custom methods and tool to create a common json output format: * { * isDestructive: false, * isError: false, * isWarning: false, * message: "General response error or not message for the cms user", * successClasses: ['list', 'of-new', 'classes', 'to-add', 'bt-done'], * failedClasses: ['list', 'of-new', 'classes', 'to-add', 'bt-done'] * records: { * success: [{ * id: 1, * class: 'ObjectClass', * row: 'tr .ss-gridfield-item html markup for this record' * }], * failed: [{ * id: 2, * class: 'AnotherClass', * message: 'Erro message for that object.' * }] * } * } * * @author colymba */ class HTTPBulkToolsResponse extends HTTPResponse { /** * We always return JSON * @var array */ protected $headers = array( "content-type" => "application/json; charset=utf-8", ); /** * Does the bulk action removes rows? * * @var boolean */ protected $removesRows; /** * Bulk action result message * * @var string */ protected $message = ''; /** * GridField instance. * * @var GridField */ protected $gridField; /** * List of DataObject that has been modified successfully by the bulk action * * @var array */ protected $successRecords = []; /** * List of css classes to add to gridfield row modified successfully * * @var array */ protected $successClasses = []; /** * List of DataObject IDs that failed to be modified by the bulk action * * @var array */ protected $failedRecords = []; /** * List of css classes to add to gridfield row with errors * * @var array */ protected $failedClasses = []; /** * Create a new bulk tools HTTP response * * @param boolean $removesRows Does the action removes rows? * @param GridField $gridfield gridfield instance that holds the records list * @param int $statusCode The numeric status code - 200, 404, etc * @param string $statusDescription The text to be given alongside the status code. * See {@link setStatusCode()} for more information. */ public function __construct($removesRows, $gridfield, $statusCode = null) { $this->removesRows = $removesRows; $this->gridField = $gridfield; parent::__construct(null, $statusCode); } /** * Overridden here so content-type cannot be changed * Add a HTTP header to the response, replacing any header of the same name. * * @param string $header Example: "content-type" * @param string $value Example: "text/xml" * @return $this */ public function addHeader($header, $value) { if($header === "content-type") { return $this; } return parent::addHeader($header, $value); } /** * Overriden here so content-type cannot be changed * Remove an existing HTTP header by its name, * e.g. "Content-Type". * * @param string $header * @return $this */ public function removeHeader($header) { if($header === "content-type") { return $this; } return parent::removeHeader($header); } /** * Overridden here so the response body cannot be set manually * * @param string $body * @return $this */ public function setBody($body) { return $this; } /** * Makes sure body is created before being returned * @return string */ public function getBody() { $this->createBody(); return $this->body; } /** * Set the general response message * * @param string $message * @return $this */ public function setMessage($message) { $this->message = $message; return $this; } /** * Add a record to the successfully modified list * @param DataObject $record the newly modified dataObject * @return $this */ public function addSuccessRecord($record) { $this->successRecords[] = $record; return $this; } /** * Add a list of records to the successfully modified list * @param SS_List $records newly modified dataObjects list * @return $this */ public function addSuccessRecords(SS_List $records) { array_push($this->successRecords, $records->toArray()); return $this; } /** * Add an ID to the successfully modified list * @param int $id the newly modified ID * @param string $className object class name (default to gridfield getModelClass()) * @return $this */ /*public function addSuccessID($id, $className = null) { if (!$className) { $className = $this->>gridField->getModelClass(); } //we use all caps ID since DO have their ID ion all caps, so createBody finds it... $this->successRecords[] = (object) ['ID' => (int) $id, 'ClassName' => $className]; return $this; }*/ /** * Add an array of ID to the successfully modified list * @param array $ids of the newly modified objects * @return $this */ /*public function addSuccessIDs($ids, $className = null) { //we use all caps ID since DO have their ID ion all caps, so createBody finds it... foreach ($ids as $id) { $this->successRecords[] = (object) ['ID' => (int) $id]; } return $this; }*/ /** * Return the list of successful records * @return array */ public function getSuccessRecords() { return $this->successRecords; } /** * Add a record to the failed to modified list with its error message * @param DataObject $record the failed dataObject * @param string $message error message * @return $this */ public function addFailedRecord($record, $message) { $this->failedRecords[] = array('id' => $record->ID, 'class' => $record->ClassName, 'message' => $message); return $this; } /** * Add a list of records to the failed to modified list with a common error message * @param SS_List $records the failed dataObject list * @param string $message error message * @return $this */ public function addFailedRecords(SS_List $records, $message) { foreach ($records as $record) { $this->failedRecords[] = array('id' => $record->ID, 'class' => $record->ClassName, 'message' => $message); } return $this; } /** * Return the list of failed records * @return array */ public function getFailedRecords() { return $this->failedRecords; } /** * Creates a gridfield table row for a given record * @param DataObject $record the record to create the row for * @return string the html TR tag */ protected function getRecordGridfieldRow($record) { $this->gridField->setList(new ArrayList(array($record))); $rowContent = ''; foreach ($this->gridField->getColumns() as $column) { $colContent = $this->gridField->getColumnContent($record, $column); // Null means this columns should be skipped altogether. if ($colContent === null) { continue; } $colAttributes = $this->gridField->getColumnAttributes($record, $column); $rowContent .= HTML::createTag( 'td', $colAttributes, $colContent ); } $rowAttributes = array( 'class' => 'ss-gridfield-item ' . implode(' ', $this->successClasses), 'data-id' => $record->ID, 'data-class' => $record->ClassName, ); $row = HTML::createTag( 'tr', $rowAttributes, $rowContent ); return $row; } /** * Creates the response JSON body */ public function createBody() { $body = array( 'isDestructive' => $this->removesRows, 'isError' => $this->isError(), 'isWarning' => false, 'message' => $this->message, 'successClasses' => $this->successClasses, 'failedClasses' => $this->failedClasses ); if (!$this->isError()) { $body['records'] = array( 'success' => array(), 'failed' => array() ); foreach ($this->successRecords as $record) { $data = array('id' => $record->ID, 'class' => $record->ClassName); if (!$this->removesRows) { $data['row'] = $this->getRecordGridfieldRow($record); } $body['records']['success'][] = $data; } $body['records']['failed'] = $this->failedRecords; } if (isset($body['records']['success']) && count($body['records']['success']) === 0) { $body['isWarning'] = true; } $this->body = Convert::raw2json($body); } /** * Make sure the body has been created before output * Output body of this response to the browser */ protected function outputBody() { $this->createBody(); parent::outputBody(); } /** * @deprecated 3.1.0 This function was used to catch PHP Errors and inject additional information in the response */ public function shutdown() { // noop } } |