From 8b3f4c869611890367fe7c150cc1a28f0e787dcc Mon Sep 17 00:00:00 2001 From: Sleeping Owl Date: Tue, 4 Nov 2014 22:08:07 +0400 Subject: [PATCH] New column type: action. Use it to add custom buttons to the tableview. See documentation for details --- CHANGELOG | 60 ++++++ src/SleepingOwl/Admin/Columns/Column.php | 1 + .../Admin/Columns/Column/Action.php | 171 ++++++++++++++++++ .../Admin/Columns/Column/BaseColumn.php | 8 + .../Columns/Interfaces/ColumnInterface.php | 5 + .../Admin/Controllers/AdminController.php | 32 ++++ src/SleepingOwl/Admin/Models/ModelItem.php | 14 +- 7 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 CHANGELOG create mode 100644 src/SleepingOwl/Admin/Columns/Column/Action.php diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 00000000..f19c29ac --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,60 @@ +CHANGELOG +========= + +2014-11-04, v1.1.0 +------------------ + +* New column type: action. Use it to add custom buttons to the tableview. See documentation for details + +2014-10-29, v1.0.10 +------------------ + +* Bugfix: Administrator username maximum length reduced to 190 to support utf8mb4 charset. Fix issue #1 + +2014-10-20, v1.0.9 +------------------ + +* Bugfix: Fixed bug with wrong date format + +2014-10-20, v1.0.8 +------------------ + +* New way to add validation rules to form elements and combine it with old method + +2014-10-16, v1.0.7 +------------------ + +* New exception when "intl" extension not installed + +2014-10-16, v1.0.6 +------------------ + +* Bugfix: Default admin credentials seeding fixed + +2014-10-16, v1.0.5 +------------------ + +* PHP 5.4 support added + +2014-10-16, v1.0.4 +------------------ + +* ValidationException handler moved to service provider +* Bugfix: Menu with subitems icon problem solved + +2014-10-15, v1.0.3 +------------------ + +* Image info speed improvements +* Model compiler template update +* Added user-friendly error message for missing getList method + +2014-10-15, v1.0.2 +------------------ + +* Global validation exception handler added + +2014-10-13, v1.0.0 +------------------ + +* Initial Version diff --git a/src/SleepingOwl/Admin/Columns/Column.php b/src/SleepingOwl/Admin/Columns/Column.php index fe5585db..6144cd03 100644 --- a/src/SleepingOwl/Admin/Columns/Column.php +++ b/src/SleepingOwl/Admin/Columns/Column.php @@ -16,6 +16,7 @@ * @method static \SleepingOwl\Admin\Columns\Column\Control control() * @method static \SleepingOwl\Admin\Columns\Column\Filter filter($alias) * @method static \SleepingOwl\Admin\Columns\Column\Url url($name) + * @method static \SleepingOwl\Admin\Columns\Column\Action action($name, $label = null) */ class Column { diff --git a/src/SleepingOwl/Admin/Columns/Column/Action.php b/src/SleepingOwl/Admin/Columns/Column/Action.php new file mode 100644 index 00000000..c7a48998 --- /dev/null +++ b/src/SleepingOwl/Admin/Columns/Column/Action.php @@ -0,0 +1,171 @@ +sortable = false; + $admin = Admin::instance(); + $this->router = $admin->router; + $this->formBuilder = $admin->formBuilder; + } + + public function renderHeader() + { + return $this->htmlBuilder->tag('th', $this->getAttributesForHeader()); + } + + /** + * @param $instance + * @param int $totalCount + * @return string + */ + public function render($instance, $totalCount) + { + $buttons = []; + $buttons[] = $this->button($instance); + return $this->htmlBuilder->tag('td', ['class' => 'text-right'], implode(' ', $buttons)); + } + + /** + * @param $instance + * @return string + */ + protected function button($instance) + { + if ( ! is_null($this->url)) + { + if (is_callable($this->url)) + { + $callback = $this->url; + $url = $callback($instance); + } else + { + $url = strtr($this->url, [':id' => $instance->id]); + } + } else + { + $url = $this->router->routeToTable($this->modelItem->getAlias(), [ + 'action' => $this->name, + 'id' => $instance->id + ]); + } + $attributes = [ + 'class' => 'btn btn-default btn-sm', + 'href' => $url, + 'data-toggle' => 'tooltip', + ]; + $content = ''; + if ( ! is_null($this->icon)) + { + $content .= ''; + } + if ($this->style === 'long') + { + $content .= ' ' . $this->label; + } else + { + $attributes['title'] = $this->label; + } + if ( ! is_null($this->target)) + { + $attributes['target'] = $this->target; + } + return $this->htmlBuilder->tag('a', $attributes, $content); + } + + /** + * @param string $icon + * @return $this + */ + public function icon($icon) + { + $this->icon = $icon; + return $this; + } + + /** + * @param string $style + * @return $this + */ + public function style($style) + { + $this->style = $style; + return $this; + } + + /** + * @param string|\Closure $url + * @return $this + */ + public function url($url) + { + $this->url = $url; + return $this; + } + + /** + * @param callable $callback + * @return $this + */ + public function callback($callback) + { + $this->callback = $callback; + return $this; + } + + public function call($instance) + { + $callback = $this->callback; + return $callback($instance); + } + + /** + * @param string $target + * @return $this + */ + public function target($target) + { + $this->target = $target; + return $this; + } + +} \ No newline at end of file diff --git a/src/SleepingOwl/Admin/Columns/Column/BaseColumn.php b/src/SleepingOwl/Admin/Columns/Column/BaseColumn.php index cc199147..c90982f0 100644 --- a/src/SleepingOwl/Admin/Columns/Column/BaseColumn.php +++ b/src/SleepingOwl/Admin/Columns/Column/BaseColumn.php @@ -227,4 +227,12 @@ public function isHidden() return $this->hidden; } + /** + * @return string + */ + public function getName() + { + return $this->name; + } + } \ No newline at end of file diff --git a/src/SleepingOwl/Admin/Columns/Interfaces/ColumnInterface.php b/src/SleepingOwl/Admin/Columns/Interfaces/ColumnInterface.php index 539b71dc..ad49835f 100644 --- a/src/SleepingOwl/Admin/Columns/Interfaces/ColumnInterface.php +++ b/src/SleepingOwl/Admin/Columns/Interfaces/ColumnInterface.php @@ -20,4 +20,9 @@ public function render($instance, $totalCount); */ public function isHidden(); + /** + * @return string + */ + public function getName(); + } \ No newline at end of file diff --git a/src/SleepingOwl/Admin/Controllers/AdminController.php b/src/SleepingOwl/Admin/Controllers/AdminController.php index a94a8f14..0d7a2c02 100644 --- a/src/SleepingOwl/Admin/Controllers/AdminController.php +++ b/src/SleepingOwl/Admin/Controllers/AdminController.php @@ -3,6 +3,7 @@ use App; use AdminAuth; use SleepingOwl\Admin\Admin; +use SleepingOwl\Admin\Columns\Column\Action; use SleepingOwl\Admin\Repositories\Interfaces\ModelRepositoryInterface; use SleepingOwl\Admin\Models\ModelItem; use SleepingOwl\Admin\Session\QueryState; @@ -138,11 +139,42 @@ public function getWildcard($wildcard = '/') return $this->makeView('page', compact('title', 'content')); } + protected function checkCustomActionCall() + { + $action = Input::query('action'); + $id = Input::query('id'); + if (is_null($action) || is_null($id)) + { + return; + } + $column = $this->modelItem->getColumnByName($action); + if (is_null($column)) + { + return; + } + if ( ! $column instanceof Action) + { + return; + } + $instance = $this->modelRepository->getInstance($id); + $result = $column->call($instance); + + if ( ! $result instanceof RedirectResponse) + { + $result = Redirect::back(); + } + return $result; + } + /** * @return View */ public function table() { + if ($result = $this->checkCustomActionCall()) + { + return $result; + } $this->queryState->save(); $data = [ 'title' => $this->modelItem->getTitle(), diff --git a/src/SleepingOwl/Admin/Models/ModelItem.php b/src/SleepingOwl/Admin/Models/ModelItem.php index 344e06dd..b5c35b25 100644 --- a/src/SleepingOwl/Admin/Models/ModelItem.php +++ b/src/SleepingOwl/Admin/Models/ModelItem.php @@ -35,7 +35,7 @@ class ModelItem */ protected $alias; /** - * @var Column[] + * @var ColumnInterface[] */ public $columns; /** @@ -213,6 +213,18 @@ public function getColumns() return $this->columns; } + public function getColumnByName($name) + { + foreach ($this->columns as $column) + { + if ($column->getName() === $name) + { + return $column; + } + } + return null; + } + /** * @param $callback * @return $this