From f8ae611ec915263d7a2f47b75ee9d357bf7ee63a Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 28 Feb 2015 14:43:34 +1100 Subject: [PATCH] Refactor form widgets to use new fillFromConfig() helper --- modules/backend/formwidgets/CodeEditor.php | 67 ++++++++++++----- modules/backend/formwidgets/ColorPicker.php | 20 ++++-- modules/backend/formwidgets/DatePicker.php | 26 +++++-- modules/backend/formwidgets/FileUpload.php | 75 +++++++++++--------- modules/backend/formwidgets/RecordFinder.php | 62 +++++++++------- modules/backend/formwidgets/Relation.php | 48 ++++++++----- modules/backend/formwidgets/RichEditor.php | 28 ++++++-- 7 files changed, 212 insertions(+), 114 deletions(-) diff --git a/modules/backend/formwidgets/CodeEditor.php b/modules/backend/formwidgets/CodeEditor.php index 7dd83d759..f82e362d3 100644 --- a/modules/backend/formwidgets/CodeEditor.php +++ b/modules/backend/formwidgets/CodeEditor.php @@ -12,10 +12,9 @@ use Backend\Classes\FormWidgetBase; */ class CodeEditor extends FormWidgetBase { - /** - * {@inheritDoc} - */ - protected $defaultAlias = 'codeeditor'; + // + // Configurable properties + // /** * @var string Code language to display (php, twig) @@ -61,33 +60,41 @@ class CodeEditor extends FormWidgetBase /** * @var integer Sets the editor margin size. */ - public $margin = 10; + public $margin = 0; /** * @var $theme Ace Editor theme to use. */ public $theme = 'twilight'; + // + // Object properties + // + + /** + * {@inheritDoc} + */ + protected $defaultAlias = 'codeeditor'; + /** * {@inheritDoc} */ public function init() { - // Load the editor system settings - $editorSettings = EditorPreferences::instance(); + $this->applyEditorPreferences(); - $this->fontSize = $this->getConfig('fontSize', $editorSettings->font_size); - $this->wordWrap = $this->getConfig('wordWrap', $editorSettings->word_wrap); - $this->codeFolding = $this->getConfig('codeFolding', $editorSettings->code_folding); - $this->autoClosing = $this->getConfig('autoClosing', $editorSettings->auto_closing); - $this->tabSize = $this->getConfig('tabSize', $editorSettings->tab_size); - $this->theme = $this->getConfig('theme', $editorSettings->theme); - $this->showInvisibles = $this->getConfig('showInvisibles', $editorSettings->show_invisibles); - $this->highlightActiveLine = $this->getConfig('highlightActiveLine', $editorSettings->highlight_active_line); - $this->useSoftTabs = $this->getConfig('useSoftTabs', !$editorSettings->use_hard_tabs); - $this->showGutter = $this->getConfig('showGutter', $editorSettings->show_gutter); - $this->language = $this->getConfig('language', 'php'); - $this->margin = $this->getConfig('margin', 0); + $this->fillFromConfig([ + 'language', + 'showGutter', + 'wordWrap', + 'codeFolding', + 'autoClosing', + 'useSoftTabs', + 'tabSize', + 'fontSize', + 'margin', + 'theme', + ]); } /** @@ -135,4 +142,26 @@ class CodeEditor extends FormWidgetBase $this->addJs('vendor/ace/ext-emmet.js', 'core'); $this->addJs('js/codeeditor.js', 'core'); } + + /** + * Looks at the user preferences and overrides any set values. + * @return void + */ + protected function applyEditorPreferences() + { + // Load the editor system settings + $editorSettings = EditorPreferences::instance(); + + $this->fontSize = $editorSettings->font_size; + $this->wordWrap = $editorSettings->word_wrap; + $this->codeFolding = $editorSettings->code_folding; + $this->autoClosing = $editorSettings->auto_closing; + $this->tabSize = $editorSettings->tab_size; + $this->theme = $editorSettings->theme; + $this->showInvisibles = $editorSettings->show_invisibles; + $this->highlightActiveLine = $editorSettings->highlight_active_line; + $this->useSoftTabs = !$editorSettings->use_hard_tabs; + $this->showGutter = $editorSettings->show_gutter; + } + } diff --git a/modules/backend/formwidgets/ColorPicker.php b/modules/backend/formwidgets/ColorPicker.php index 67c33c7e5..2e3809d77 100644 --- a/modules/backend/formwidgets/ColorPicker.php +++ b/modules/backend/formwidgets/ColorPicker.php @@ -11,10 +11,9 @@ use Backend\Classes\FormWidgetBase; */ class ColorPicker extends FormWidgetBase { - /** - * {@inheritDoc} - */ - protected $defaultAlias = 'colorpicker'; + // + // Configurable properties + // /** * @var array Default available colors @@ -32,12 +31,23 @@ class ColorPicker extends FormWidgetBase '#95a5a6', '#7f8c8d', ]; + // + // Object properties + // + + /** + * {@inheritDoc} + */ + protected $defaultAlias = 'colorpicker'; + /** * {@inheritDoc} */ public function init() { - $this->availableColors = $this->getConfig('availableColors', $this->availableColors); + $this->fillFromConfig([ + 'availableColors', + ]); } /** diff --git a/modules/backend/formwidgets/DatePicker.php b/modules/backend/formwidgets/DatePicker.php index 18a4705ee..346508426 100644 --- a/modules/backend/formwidgets/DatePicker.php +++ b/modules/backend/formwidgets/DatePicker.php @@ -13,10 +13,9 @@ class DatePicker extends FormWidgetBase { const TIME_PREFIX = '___time_'; - /** - * {@inheritDoc} - */ - protected $defaultAlias = 'datepicker'; + // + // Configurable properties + // /** * @var bool Display mode: datetime, date, time. @@ -33,14 +32,27 @@ class DatePicker extends FormWidgetBase */ public $maxDate = '2020-12-31'; + // + // Object properties + // + + /** + * {@inheritDoc} + */ + protected $defaultAlias = 'datepicker'; + /** * {@inheritDoc} */ public function init() { - $this->mode = strtolower($this->getConfig('mode', $this->mode)); - $this->minDate = $this->getConfig('minDate', $this->minDate); - $this->maxDate = $this->getConfig('maxDate', $this->maxDate); + $this->fillFromConfig([ + 'mode', + 'minDate', + 'maxDate', + ]); + + $this->mode = strtolower($this->mode); } /** diff --git a/modules/backend/formwidgets/FileUpload.php b/modules/backend/formwidgets/FileUpload.php index 16ee7eddf..8133d8f7c 100644 --- a/modules/backend/formwidgets/FileUpload.php +++ b/modules/backend/formwidgets/FileUpload.php @@ -24,48 +24,59 @@ use Exception; */ class FileUpload extends FormWidgetBase { + // + // Configurable properties + // + + /** + * @var int Preview image width + */ + public $imageWidth = 100; + + /** + * @var int Preview image height + */ + public $imageHeight = 100; + + /** + * @var string Text to display when no file is associated + */ + public $previewNoFilesMessage = 'backend::lang.form.preview_no_files_message'; + + /** + * @var mixed Collection of acceptable file types. + */ + public $fileTypes = false; + + /** + * @var array Options used for generating thumbnails. + */ + public $thumbOptions = [ + 'mode' => 'crop', + 'extension' => 'auto' + ]; + + // + // Object properties + // + /** * {@inheritDoc} */ protected $defaultAlias = 'fileupload'; - /** - * @var int Preview image width - */ - public $imageWidth; - - /** - * @var int Preview image height - */ - public $imageHeight; - - /** - * @var string Text to display when no file is associated - */ - public $previewNoFilesMessage; - - /** - * @var mixed Collection of acceptable file types. - */ - public $acceptedFileTypes = false; - /** * {@inheritDoc} */ public function init() { - $this->imageHeight = $this->getConfig('imageHeight', 100); - $this->imageWidth = $this->getConfig('imageWidth', 100); - $this->acceptedFileTypes = $this->getConfig('fileTypes'); - $this->previewNoFilesMessage = $this->getConfig( + $this->fillFromConfig([ + 'imageWidth', + 'imageHeight', 'previewNoFilesMessage', - 'backend::lang.form.preview_no_files_message' - ); - - $this->thumbOptions = [ - 'mode' => 'crop', - 'extension' => 'auto' - ]; + 'fileTypes', + 'thumbOptions' + ]); $this->checkUploadPostback(); } @@ -133,7 +144,7 @@ class FileUpload extends FormWidgetBase */ public function getAcceptedFileTypes($includeDot = false) { - $types = $this->acceptedFileTypes; + $types = $this->fileTypes; if ($types === false && starts_with($this->getDisplayMode(), 'image')) { $types = 'jpg,jpeg,bmp,png,gif,svg'; } diff --git a/modules/backend/formwidgets/RecordFinder.php b/modules/backend/formwidgets/RecordFinder.php index b91bd419b..12c34c13e 100644 --- a/modules/backend/formwidgets/RecordFinder.php +++ b/modules/backend/formwidgets/RecordFinder.php @@ -21,6 +21,34 @@ use SystemException; */ class RecordFinder extends FormWidgetBase { + // + // Configurable properties + // + + /** + * @var string Field name to use for key. + */ + public $keyFrom = 'id'; + + /** + * @var string Relation column to display for the name + */ + public $nameFrom; + + /** + * @var string Relation column to display for the description + */ + public $descriptionFrom; + + /** + * @var string Prompt to display if no record is selected. + */ + public $prompt = 'Click the %s button to find a record'; + + // + // Object properties + // + /** * {@inheritDoc} */ @@ -41,26 +69,6 @@ class RecordFinder extends FormWidgetBase */ public $relationModel; - /** - * @var string Field name to use for key. - */ - public $keyFrom = 'id'; - - /** - * @var string Relation column to display for the name - */ - public $nameFrom; - - /** - * @var string Relation column to display for the description - */ - public $descriptionFrom; - - /** - * @var string Prompt to display if no record is selected. - */ - public $prompt; - /** * @var Backend\Classes\WidgetBase Reference to the widget used for viewing (list or form). */ @@ -76,13 +84,15 @@ class RecordFinder extends FormWidgetBase */ public function init() { - $this->relationName = $this->formField->valueFrom; - $this->relationType = $this->model->getRelationType($this->relationName); + $this->fillFromConfig([ + 'prompt', + 'keyFrom', + 'nameFrom', + 'descriptionFrom', + ]); - $this->prompt = $this->getConfig('prompt', 'Click the %s button to find a record'); - $this->keyFrom = $this->getConfig('keyFrom', $this->keyFrom); - $this->nameFrom = $this->getConfig('nameFrom', $this->nameFrom); - $this->descriptionFrom = $this->getConfig('descriptionFrom', $this->descriptionFrom); + $this->relationName = $this->valueFrom; + $this->relationType = $this->model->getRelationType($this->relationName); if (!$this->model->hasRelation($this->relationName)) { throw new SystemException(Lang::get('backend::lang.model.missing_relation', [ diff --git a/modules/backend/formwidgets/Relation.php b/modules/backend/formwidgets/Relation.php index a48785734..07a985fa3 100644 --- a/modules/backend/formwidgets/Relation.php +++ b/modules/backend/formwidgets/Relation.php @@ -14,6 +14,29 @@ use Illuminate\Database\Eloquent\Relations\Relation as RelationBase; */ class Relation extends FormWidgetBase { + // + // Configurable properties + // + + /** + * @var string Model column to use for the name reference + */ + public $nameFrom = 'name'; + + /** + * @var string Model column to use for the description reference + */ + public $descriptionFrom = 'description'; + + /** + * @var string Empty value to use if the relation is singluar (belongsTo) + */ + public $emptyOption; + + // + // Object properties + // + /** * {@inheritDoc} */ @@ -34,33 +57,20 @@ class Relation extends FormWidgetBase */ public $renderFormField; - /** - * @var string Model column to use for the name reference - */ - public $nameFrom = 'name'; - - /** - * @var string Model column to use for the description reference - */ - public $descriptionFrom = 'description'; - - /** - * @var string Empty value to use if the relation is singluar (belongsTo) - */ - public $emptyOption; - /** * {@inheritDoc} */ public function init() { + $this->fillFromConfig([ + 'nameFrom', + 'descriptionFrom', + 'emptyOption', + ]); + $this->relationName = $this->valueFrom; $this->relationType = $this->model->getRelationType($this->relationName); - $this->nameFrom = $this->getConfig('nameFrom', $this->nameFrom); - $this->descriptionFrom = $this->getConfig('descriptionFrom', $this->descriptionFrom); - $this->emptyOption = $this->getConfig('emptyOption'); - if (!$this->model->hasRelation($this->relationName)) { throw new SystemException(Lang::get( 'backend::lang.model.missing_relation', diff --git a/modules/backend/formwidgets/RichEditor.php b/modules/backend/formwidgets/RichEditor.php index 07d8de7e6..af4c0adb1 100644 --- a/modules/backend/formwidgets/RichEditor.php +++ b/modules/backend/formwidgets/RichEditor.php @@ -11,23 +11,39 @@ use Backend\Classes\FormWidgetBase; */ class RichEditor extends FormWidgetBase { - /** - * {@inheritDoc} - */ - protected $defaultAlias = 'richeditor'; + // + // Configurable properties + // /** * @var boolean Determines whether content has HEAD and HTML tags. */ public $fullPage = false; + // + // Object properties + // + + /** + * {@inheritDoc} + */ + protected $defaultAlias = 'richeditor'; + + /** + * {@inheritDoc} + */ + public function init() + { + $this->fillFromConfig([ + 'fullPage', + ]); + } + /** * {@inheritDoc} */ public function render() { - $this->fullPage = $this->getConfig('fullPage', $this->fullPage); - $this->prepareVars(); return $this->makePartial('richeditor'); }