Source of file HasOneSelector.php
Size: 4,604 Bytes - Last Modified: 2021-12-23T10:04:57+00:00
/var/www/docs.ssmods.com/process/src/code/Columns/HasOneSelector.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 | <?php namespace Milkyway\SS\GridFieldUtils; /** * Milkyway Multimedia * HasOneSelector.php * * @package milkyway-multimedia/ss-gridfield-utils * @author Mellisa Hankins <mell@milkywaymultimedia.com.au> */ use GridField; use GridField_ColumnProvider; use GridField_SaveHandler; use GridField_HTMLProvider; use FormField; use ArrayData; use DataObjectInterface; class HasOneSelector implements GridField_ColumnProvider, GridField_SaveHandler, GridField_HTMLProvider { public $resetButtonTitle; public $columnTitle = 'Select'; public $targetFragment; protected $relation; public function __construct($relation, $columnTitle = '', $targetFragment = 'before') { $this->relation = $relation; $this->columnTitle = $columnTitle ?: FormField::name_to_label($relation); $this->targetFragment = $targetFragment; $this->resetButtonTitle = _t('GridField_HasOneSelector.RESET', 'Reset {columnTitle}', ['columnTitle' => $this->columnTitle]); } /** * Modify the list of columns displayed in the table. * * @see {@link GridFieldDataColumns->getDisplayFields()} * @see {@link GridFieldDataColumns}. * * @param GridField $gridField * @param array - List reference of all column names. */ public function augmentColumns($gridField, &$columns) { if (!in_array($this->relation, $columns)) { $columns[] = $this->relation; } } /** * Names of all columns which are affected by this component. * * @param \GridField $gridField * * @return array */ public function getColumnsHandled($gridField) { return [$this->relation]; } /** * HTML for the column, content of the <td> element. * * @param \GridField $gridField * @param \DataObject $record - Record displayed in this row * @param string $columnName * * @return string - HTML for the column. Return NULL to skip. */ public function getColumnContent($gridField, $record, $columnName) { $value = $gridField && $gridField->Form && $gridField->Form->Record ? $gridField->Form->Record->{$this->relation . 'ID'} : ''; return $record->ID ? _t( 'GridField_HasOneSelector.SELECTOR', '<input type="radio" name="{name}" value="{value}"{selected}/>', [ 'name' => sprintf('%s[%s]', $gridField->getName(), str_replace('\\', '_', __CLASS__)), 'value' => $record->ID, 'selected' => $value == $record->ID ? ' checked="checked"' : '', ] ) : ''; } /** * Attributes for the element containing the content returned by {@link getColumnContent()}. * * @param \GridField $gridField * @param \DataObject $record displayed in this row * @param string $columnName * * @return array */ public function getColumnAttributes($gridField, $record, $columnName) { return [ 'class' => 'ss-gridfield-hasOneSelector-holder ss-gridfield-hasOneSelector-col_' . $columnName, 'data-relation' => $this->relation, ]; } /** * Additional metadata about the column which can be used by other components, * e.g. to set a title for a search column header. * * @param \GridField $gridField * @param string $columnName * * @return array - Map of arbitrary metadata identifiers to their values. */ public function getColumnMetadata($gridField, $columnName) { return ['title' => $this->columnTitle]; } public function getHTMLFragments($grid) { Utilities::include_requirements(); return [ $this->targetFragment => ArrayData::create([ 'Title' => $this->resetButtonTitle, 'Relation' => $this->relation, ])->renderWith('GridField_HasOneSelector'), ]; } public function handleSave(GridField $grid, DataObjectInterface $record) { $value = $grid->Value(); if (!isset($value[str_replace('\\', '_', __CLASS__)])) { $value[str_replace('\\', '_', __CLASS__)] = 0; } if ($record->hasMethod('save' . $this->relation . 'FromGridField')) { $record->{'save' . $this->relation . 'FromGridField'}((int)$value[str_replace('\\', '_', __CLASS__)], $value); } else { $record->{$this->relation . 'ID'} = (int)$value[str_replace('\\', '_', __CLASS__)]; } } } |