first commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/.idea
|
21
listswitch/LICENSE
Normal file
21
listswitch/LICENSE
Normal file
@ -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.
|
137
listswitch/ListSwitchField.php
Normal file
137
listswitch/ListSwitchField.php
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
<?php namespace Inetis\ListSwitch;
|
||||||
|
|
||||||
|
use Backend\Classes\ListColumn;
|
||||||
|
use Lang;
|
||||||
|
use Model;
|
||||||
|
|
||||||
|
class ListSwitchField
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Default field configuration
|
||||||
|
* all these params can be overrided by column config
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $defaultFieldConfig = [
|
||||||
|
'icon' => 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 '
|
||||||
|
<a href="javascript:;"
|
||||||
|
data-request="' . $config['request'] . '"
|
||||||
|
data-request-data="' . $field->getRequestData() . '"
|
||||||
|
data-stripe-load-indicator
|
||||||
|
title="' . $field->getButtonTitle() . '">
|
||||||
|
' . $field->getButtonValue() . '
|
||||||
|
</a>
|
||||||
|
';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 '<i class="oc-icon-check"></i>';
|
||||||
|
}
|
||||||
|
|
||||||
|
return '<i class="oc-icon-times"></i>';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return button hover title
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getButtonTitle()
|
||||||
|
{
|
||||||
|
return Lang::get($this->getConfig($this->value ? 'titleTrue' : 'titleFalse'));
|
||||||
|
}
|
||||||
|
}
|
94
listswitch/Plugin.php
Normal file
94
listswitch/Plugin.php
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
<?php namespace Inetis\ListSwitch;
|
||||||
|
|
||||||
|
use Backend\Classes\Controller;
|
||||||
|
use Event;
|
||||||
|
use Flash;
|
||||||
|
use System\Classes\PluginBase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* listSwitch Plugin Information File
|
||||||
|
*/
|
||||||
|
class Plugin extends PluginBase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns information about this plugin.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function pluginDetails()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'name' => '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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
39
listswitch/README.md
Normal file
39
listswitch/README.md
Normal file
@ -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*
|
||||||
|

|
||||||
|
|
||||||
|
*Behavior with ` icon: false`*
|
||||||
|

|
||||||
|
|
||||||
|
*With custom titles*
|
||||||
|

|
||||||
|
|
||||||
|
## 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).
|
14
listswitch/lang/en/lang.php
Normal file
14
listswitch/lang/en/lang.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php return [
|
||||||
|
'inetis' => [
|
||||||
|
'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',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
14
listswitch/lang/fr/lang.php
Normal file
14
listswitch/lang/fr/lang.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php return [
|
||||||
|
'inetis' => [
|
||||||
|
'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',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
14
listswitch/lang/hu/lang.php
Normal file
14
listswitch/lang/hu/lang.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php return [
|
||||||
|
'inetis' => [
|
||||||
|
'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',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
14
listswitch/lang/ru/lang.php
Normal file
14
listswitch/lang/ru/lang.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php return [
|
||||||
|
'inetis' => [
|
||||||
|
'plugin' => [
|
||||||
|
'name' => 'List Switch',
|
||||||
|
'description' => 'Добавьте возможность создавать кнопки в бэкэнд-списках, которые переключают свойство модели.',
|
||||||
|
],
|
||||||
|
'listswitch' => [
|
||||||
|
'title_true' => 'Нажмите чтобы переключить на Нет',
|
||||||
|
'title_false' => 'Нажмите чтобы переключить на Да',
|
||||||
|
'text_true' => 'Да',
|
||||||
|
'text_false' => 'Нет',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
7
listswitch/updates/version.yaml
Normal file
7
listswitch/updates/version.yaml
Normal file
@ -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
|
Reference in New Issue
Block a user