Skip to content

Commit

Permalink
New column type: action. Use it to add custom buttons to the tablevie…
Browse files Browse the repository at this point in the history
…w. See documentation for details
  • Loading branch information
sleeping-owl committed Nov 4, 2014
1 parent 8c0ad0c commit 8b3f4c8
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 1 deletion.
60 changes: 60 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/SleepingOwl/Admin/Columns/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
171 changes: 171 additions & 0 deletions src/SleepingOwl/Admin/Columns/Column/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php namespace SleepingOwl\Admin\Columns\Column;

use SleepingOwl\Admin\Admin;
use SleepingOwl\Html\FormBuilder;
use Lang;
use SleepingOwl\Models\Interfaces\ModelWithOrderFieldInterface;

class Action extends BaseColumn
{
/**
* @var \SleepingOwl\Admin\Router
*/
protected $router;
/**
* @var FormBuilder
*/
protected $formBuilder;
/**
* @var string
*/
protected $icon = null;
/**
* @var string
*/
protected $style = 'long';
/**
* @var string|\Closure
*/
protected $url;
/**
* @var \Closure
*/
protected $callback;
/**
* @var string
*/
protected $target;

/**
*
*/
function __construct($name, $label = null)
{
parent::__construct($name, $label);
$this->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 .= '<i class="fa ' . $this->icon . '"></i>';
}
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;
}

}
8 changes: 8 additions & 0 deletions src/SleepingOwl/Admin/Columns/Column/BaseColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,12 @@ public function isHidden()
return $this->hidden;
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}

}
5 changes: 5 additions & 0 deletions src/SleepingOwl/Admin/Columns/Interfaces/ColumnInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function render($instance, $totalCount);
*/
public function isHidden();

/**
* @return string
*/
public function getName();

}
32 changes: 32 additions & 0 deletions src/SleepingOwl/Admin/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down
14 changes: 13 additions & 1 deletion src/SleepingOwl/Admin/Models/ModelItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ModelItem
*/
protected $alias;
/**
* @var Column[]
* @var ColumnInterface[]
*/
public $columns;
/**
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8b3f4c8

Please sign in to comment.