From 2948e8508863da2d5460536d842b11a85362a769 Mon Sep 17 00:00:00 2001 From: dimti Date: Thu, 4 Feb 2021 14:26:44 +0300 Subject: [PATCH] first commit --- .gitignore | 1 + listswitch/LICENSE | 21 ++++++ listswitch/ListSwitchField.php | 137 ++++++++++++++++++++++++++++++++++++++++ listswitch/Plugin.php | 94 +++++++++++++++++++++++++++ listswitch/README.md | 39 ++++++++++++ listswitch/lang/en/lang.php | 14 ++++ listswitch/lang/fr/lang.php | 14 ++++ listswitch/lang/hu/lang.php | 14 ++++ listswitch/lang/ru/lang.php | 14 ++++ listswitch/updates/version.yaml | 7 ++ 10 files changed, 355 insertions(+) create mode 100644 .gitignore create mode 100644 listswitch/LICENSE create mode 100644 listswitch/ListSwitchField.php create mode 100644 listswitch/Plugin.php create mode 100644 listswitch/README.md create mode 100644 listswitch/lang/en/lang.php create mode 100644 listswitch/lang/fr/lang.php create mode 100644 listswitch/lang/hu/lang.php create mode 100644 listswitch/lang/ru/lang.php create mode 100644 listswitch/updates/version.yaml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a09c56d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.idea diff --git a/listswitch/LICENSE b/listswitch/LICENSE new file mode 100644 index 0000000..860566e --- /dev/null +++ b/listswitch/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 inetis.ch + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/listswitch/ListSwitchField.php b/listswitch/ListSwitchField.php new file mode 100644 index 0000000..f57ba69 --- /dev/null +++ b/listswitch/ListSwitchField.php @@ -0,0 +1,137 @@ + true, + 'titleTrue' => 'inetis.listswitch::lang.inetis.listswitch.title_true', + 'titleFalse' => 'inetis.listswitch::lang.inetis.listswitch.title_false', + 'textTrue' => 'inetis.listswitch::lang.inetis.listswitch.text_true', + 'textFalse' => 'inetis.listswitch::lang.inetis.listswitch.text_false', + 'request' => 'onSwitchInetisListField' + ]; + + private static $listConfig = []; + + /** + * @param $field + * @param array $config + * + * @internal param $name + */ + public static function storeFieldConfig($field, array $config) + { + self::$listConfig[$field] = array_merge(self::$defaultFieldConfig, $config, ['name' => $field]); + } + + /** + * @param $value + * @param ListColumn $column + * @param Model $record + * + * @return string HTML + */ + public static function render($value, ListColumn $column, Model $record) + { + $field = new self($value, $column, $record); + $config = $field->getConfig(); + + return ' + + ' . $field->getButtonValue() . ' + +'; + } + + /** + * ListSwitchField constructor. + * + * @param $value + * @param ListColumn $column + * @param Model $record + */ + public function __construct($value, ListColumn $column, Model $record) + { + $this->name = $column->columnName; + $this->value = $value; + $this->column = $column; + $this->record = $record; + } + + /** + * @param $config + * + * @return mixed + */ + private function getConfig($config = null) + { + if (is_null($config)) { + return self::$listConfig[$this->name]; + } + + return self::$listConfig[$this->name][$config]; + } + + /** + * Return data-request-data params for the switch button + * + * @return string + */ + public function getRequestData() + { + $modelClass = str_replace('\\', '\\\\', get_class($this->record)); + + $data = [ + "id: {$this->record->{$this->record->getKeyName()}}", + "field: '$this->name'", + "model: '$modelClass'" + ]; + + if (post('page')) { + $data[] = "page: " . post('page'); + } + + return implode(', ', $data); + } + + /** + * Return button text or icon + * + * @return string + */ + public function getButtonValue() + { + if (!$this->getConfig('icon')) { + return Lang::get($this->getConfig($this->value ? 'textTrue' : 'textFalse')); + } + + if ($this->value) { + return ''; + } + + return ''; + } + + /** + * Return button hover title + * + * @return string + */ + public function getButtonTitle() + { + return Lang::get($this->getConfig($this->value ? 'titleTrue' : 'titleFalse')); + } +} \ No newline at end of file diff --git a/listswitch/Plugin.php b/listswitch/Plugin.php new file mode 100644 index 0000000..b8c12bd --- /dev/null +++ b/listswitch/Plugin.php @@ -0,0 +1,94 @@ + 'inetis.listswitch::lang.inetis.plugin.name', + 'description' => 'inetis.listswitch::lang.inetis.plugin.description', + 'author' => 'inetis', + 'icon' => 'icon-toggle-on', + 'homepage' => 'https://github.com/inetis-ch/oc-ListSwitch', + ]; + } + + /** + * Register custom list type + * + * @return array + */ + public function registerListColumnTypes() + { + return [ + 'inetis-list-switch' => [ListSwitchField::class, 'render'], + ]; + } + + /** + * Boot method, called right before the request route. + * + * @return array + */ + public function boot() + { + Event::listen('backend.list.extendColumns', function ($widget) { + /** @var \Backend\Widgets\Lists $widget */ + foreach ($widget->config->columns as $name => $config) { + if (empty($config['type']) || $config['type'] !== 'inetis-list-switch') { + continue; + } + + // Store field config here, before that unofficial fields was removed + ListSwitchField::storeFieldConfig($name, $config); + + $widget->addColumns([ + $name => array_merge($config, [ + 'clickable' => false, + ]), + ]); + } + }); + + /** + * Switch a boolean value of a model field + * @return void + */ + Controller::extend(function ($controller) { + /** @var Controller $controller */ + $controller->addDynamicMethod('index_onSwitchInetisListField', function () use ($controller) { + + $field = post('field'); + $id = post('id'); + $modelClass = post('model'); + + if (empty($field) || empty($id) || empty($modelClass)) { + Flash::error('Following parameters are required : id, field, model'); + + return; + } + + $model = new $modelClass; + $item = $model::find($id); + $item->{$field} = !$item->{$field}; + + $item->save(); + + return $controller->listRefresh($controller->primaryDefinition); + }); + }); + } +} diff --git a/listswitch/README.md b/listswitch/README.md new file mode 100644 index 0000000..71e7051 --- /dev/null +++ b/listswitch/README.md @@ -0,0 +1,39 @@ +# About +OctoberCMS plugin to add a toggleable switch as a column type to the backend. + +## Usage + +To add a switch column to a list; set the `type` of the column to `inetis-list-switch`. + +Example: +```yaml +your_column: + label: 'Your Label' + # Define the type as "inetis-list-switch" to enable this functionality + type: inetis-list-switch + + # Whether to use ✓/✗ icons to replace the Yes/No text (default: true) + icon: true + + # Overrides the title displayed on hover over the column + titleTrue: 'Unpublish item' # (default: 'inetis.listswitch::lang.inetis.listswitch.title_true') + titleFalse: 'Publish item' # (default: 'inetis.listswitch::lang.inetis.listswitch.title_false') + + # Overrides the text displayed on the button + textTrue: 'Published' # (default: 'inetis.listswitch::lang.inetis.listswitch.text_true') + textFalse: 'Unpublished' #(default: 'inetis.listswitch::lang.inetis.listswitch.text_false') +``` + + +## Demo +*Default behavior* +![switch-icon](https://cloud.githubusercontent.com/assets/12028540/23846134/2e0245c8-07cc-11e7-82a6-c5c0c940b453.gif) + +*Behavior with ` icon: false`* +![switch-text](https://cloud.githubusercontent.com/assets/12028540/23846200/a88ac8c4-07cc-11e7-89fd-ccb61a701b82.gif) + +*With custom titles* +![switch-icon-custom-titles](https://cloud.githubusercontent.com/assets/12028540/23846367/69e1f89e-07cd-11e7-9f8b-943aa9301464.gif) + +## Author +inetis is a webdesign agency in Vufflens-la-Ville, Switzerland. We love coding and creating powerful apps and sites [see our website](https://inetis.ch). diff --git a/listswitch/lang/en/lang.php b/listswitch/lang/en/lang.php new file mode 100644 index 0000000..c7edc49 --- /dev/null +++ b/listswitch/lang/en/lang.php @@ -0,0 +1,14 @@ + [ + 'plugin' => [ + 'name' => 'List Switch', + 'description' => 'Add the possibility to create buttons in backend lists that toggle a model property.', + ], + 'listswitch' => [ + 'title_true' => 'Click to switch to No', + 'title_false' => 'Click to switch to Yes', + 'text_true' => 'Yes', + 'text_false' => 'No', + ], + ], +]; diff --git a/listswitch/lang/fr/lang.php b/listswitch/lang/fr/lang.php new file mode 100644 index 0000000..86da6e9 --- /dev/null +++ b/listswitch/lang/fr/lang.php @@ -0,0 +1,14 @@ + [ + 'plugin' => [ + 'name' => 'Liste Switch', + 'description' => "Ajoute la possibilité de créer des boutons dans les listes de l'administration qui inversent la valeur d'un champ.", + ], + 'listswitch' => [ + 'title_true' => 'Cliquez pour passer le statut à Non', + 'title_false' => 'Cliquez pour passer le statut à Oui', + 'text_true' => 'Oui', + 'text_false' => 'Non', + ], + ], +]; diff --git a/listswitch/lang/hu/lang.php b/listswitch/lang/hu/lang.php new file mode 100644 index 0000000..df7c330 --- /dev/null +++ b/listswitch/lang/hu/lang.php @@ -0,0 +1,14 @@ + [ + 'plugin' => [ + 'name' => 'Gyorskapcsoló listákhoz', + 'description' => 'Kattintható mező lehetőség hozzádása az admin listákhoz.', + ], + 'listswitch' => [ + 'title_true' => 'Váltás erre: Nem', + 'title_false' => 'Váltás erre: Igen', + 'text_true' => 'Igen', + 'text_false' => 'Nem', + ], + ], +]; diff --git a/listswitch/lang/ru/lang.php b/listswitch/lang/ru/lang.php new file mode 100644 index 0000000..0c5657c --- /dev/null +++ b/listswitch/lang/ru/lang.php @@ -0,0 +1,14 @@ + [ + 'plugin' => [ + 'name' => 'List Switch', + 'description' => 'Добавьте возможность создавать кнопки в бэкэнд-списках, которые переключают свойство модели.', + ], + 'listswitch' => [ + 'title_true' => 'Нажмите чтобы переключить на Нет', + 'title_false' => 'Нажмите чтобы переключить на Да', + 'text_true' => 'Да', + 'text_false' => 'Нет', + ], + ], +]; diff --git a/listswitch/updates/version.yaml b/listswitch/updates/version.yaml new file mode 100644 index 0000000..bb65260 --- /dev/null +++ b/listswitch/updates/version.yaml @@ -0,0 +1,7 @@ +1.0.1: First version of listSwitch +1.0.2: + - Display correctly the column label + - Add Hungarian translation +1.0.3: + - Preserve column configs + - Add Russian translation