Source of file SEOEditorMetaDescriptionColumn.php
Size: 6,652 Bytes - Last Modified: 2021-12-23T10:02:19+00:00
/var/www/docs.ssmods.com/process/src/src/Forms/GridField/SEOEditorMetaDescriptionColumn.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 | <?php namespace LittleGiant\SEOEditor\Forms\GridField; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Core\Convert; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridField_ColumnProvider; use SilverStripe\Forms\GridField\GridField_HTMLProvider; use SilverStripe\Forms\GridField\GridField_URLHandler; use SilverStripe\Forms\GridField\GridFieldDataColumns; use SilverStripe\Forms\TextareaField; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObjectInterface; use SilverStripe\ORM\DB; /** * Class SEOEditorMetaDescriptionColumn */ class SEOEditorMetaDescriptionColumn extends GridFieldDataColumns implements GridField_ColumnProvider, GridField_HTMLProvider, GridField_URLHandler { /** * 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) { $columns[] = 'MetaDescription'; } /** * Names of all columns which are affected by this component. * * @param GridField $gridField * @return array */ public function getColumnsHandled($gridField) { return [ 'MetaDescription', ]; } /** * 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) { $errors = $this->getErrors($record); return [ 'class' => count($errors) ? 'seo-editor-error ' . implode(' ', $errors) : 'seo-editor-valid', ]; } /** * 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) { $field = new TextareaField('MetaDescription'); $value = $gridField->getDataFieldValue($record, $columnName); $value = $this->formatValue($gridField, $record, $columnName, $value); $field->setName($this->getFieldName($field->getName(), $gridField, $record)); $field->setValue($value); $field->setAttribute('data-name', 'MetaDescription'); return $field->Field() . $this->getErrorMessages(); } /** * 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 $column * @return array - Map of arbitrary metadata identifiers to their values. */ public function getColumnMetadata($gridField, $column) { return [ 'title' => 'MetaDescription', ]; } /** * Get the errors which are specific to MetaDescription * * @param DataObject $record * @return array */ public function getErrors(DataObject $record) { $errors = []; if (strlen($record->MetaDescription) < 10) { $errors[] = 'seo-editor-error-too-short'; } if (strlen($record->MetaDescription) > 160) { $errors[] = 'seo-editor-error-too-long'; } if (strlen(SiteTree::get()->filter('MetaDescription', $record->MetaDescription)->count() > 1)) { $errors[] = 'seo-editor-error-duplicate'; } return $errors; } /** * Return all the error messages * * @return string */ public function getErrorMessages() { return '<div class="seo-editor-errors">' . '<span class="seo-editor-message seo-editor-message-too-short">This meta description is too short. It should be greater than 10 characters long.</span>' . '<span class="seo-editor-message seo-editor-message-too-long">This meta description is too long. It should be less than 160 characters long.</span>' . '<span class="seo-editor-message seo-editor-message-duplicate">This meta description is a duplicate. It should be unique.</span>' . '</div>'; } /** * Add a class to the gridfield * * @param $gridField * @return array|void */ public function getHTMLFragments($gridField) { $gridField->addExtraClass('ss-seo-editor'); } /** * @param $name * @param GridField $gridField * @param DataObjectInterface $record * @return string */ protected function getFieldName($name, GridField $gridField, DataObjectInterface $record) { return sprintf( '%s[%s][%s]', $gridField->getName(), $record->ID, $name ); } /** * Return URLs to be handled by this grid field, in an array the same form as $url_handlers. * Handler methods will be called on the component, rather than the grid field. * * @param $gridField * @return array */ public function getURLHandlers($gridField) { return [ 'updateMetaDescription/$ID' => 'handleAction', ]; } /** * @param $gridField * @param $request * @return string */ public function handleAction($gridField, $request) { $data = $request->postVar($gridField->getName()); foreach ($data as $id => $params) { $page = $gridField->getList()->byId((int)$id); foreach ($params as $fieldName => $val) { $sqlValue = Convert::raw2sql($val); $page->$fieldName = $sqlValue; DB::query("UPDATE SiteTree SET {$fieldName} = '{$sqlValue}' WHERE ID = {$page->ID}"); if ($page->isPublished()) { DB::query("UPDATE SiteTree_Live SET {$fieldName} = '{$sqlValue}' WHERE ID = {$page->ID}"); } return json_encode([ 'type' => 'success', 'message' => $fieldName . ' saved', 'errors' => $this->getErrors($page), ]); } } return json_encode([ 'type' => 'error', 'message' => 'An error occurred while saving', ]); } } |