Skip to content

Commit

Permalink
Поправлен синтаксис
Browse files Browse the repository at this point in the history
Произведен рефакторинг кода, внесено много изменений, переработаны классы Column и FormItem, они стале более гибкими, исправлено множество недочетов
При выводе списка в виде таблицы, добавлена возможность задавать ширину столбца.

Добавлен новый тип поля FormItem::wysiwyg, который позволяет подключить средствами KodiCMS редактор текста
  • Loading branch information
butschster committed Feb 18, 2016
1 parent 846f904 commit d07f439
Show file tree
Hide file tree
Showing 72 changed files with 651 additions and 230 deletions.
4 changes: 4 additions & 0 deletions src/ColumnFilters/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,11 @@ public function getWidth()
public function setWidth($width)
{
intval($width);

if ($width < 0) {
$width = 0;
}

$this->width = (int) $width;

return $this;
Expand Down Expand Up @@ -169,6 +171,7 @@ public function apply(
if (empty($search)) {
return;
}

try {
$time = Carbon::createFromFormat($this->getFormat(), $search);
} catch (Exception $e) {
Expand All @@ -178,6 +181,7 @@ public function apply(
return;
}
}

$time = $time->format($this->getSearchFormat());
$name = $column->getName();
if ($repository->hasColumn($name)) {
Expand Down
1 change: 1 addition & 0 deletions src/ColumnFilters/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public function apply(
if (! empty($from)) {
$this->getFrom()->apply($repository, $column, $query, $from, $fullSearch, '>=');
}

if (! empty($to)) {
$this->getTo()->apply($repository, $column, $query, $to, $fullSearch, '<=');
}
Expand Down
6 changes: 6 additions & 0 deletions src/ColumnFilters/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public function getOptions()
if (! is_null($this->getModel()) and ! is_null($this->getDisplay())) {
$this->loadOptions();
}

$options = $this->options;
asort($options);

Expand Down Expand Up @@ -179,14 +180,17 @@ public function apply(
if ($search === '') {
return;
}

if ($this->getFilterField()) {
$query->where($this->getFilterField(), '=', $search);

return;
}

if ($operator == 'like') {
$search = '%'.$search.'%';
}

$name = $column->getName();
if ($repository->hasColumn($name)) {
$query->where($name, $operator, $search);
Expand All @@ -205,9 +209,11 @@ protected function loadOptions()
$repository = new BaseRepository($this->getModel());
$key = $repository->getModel()->getKeyName();
$options = $repository->query()->get()->lists($this->getDisplay(), $key);

if ($options instanceof Collection) {
$options = $options->all();
}

$options = array_unique($options);
$this->setOptions($options);
}
Expand Down
3 changes: 3 additions & 0 deletions src/ColumnFilters/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@ public function apply(
if (empty($search)) {
return;
}

if ($operator == 'like') {
$search = '%'.$search.'%';
}

$name = $column->getName();

if ($repository->hasColumn($name)) {
$query->where($name, $operator, $search);
} elseif (strpos($name, '.') !== false) {
Expand Down
18 changes: 11 additions & 7 deletions src/Columns/Column/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@ class Action extends NamedColumn implements ColumnActionInterface
*/
protected $url;

/**
* @var string
*/
protected $view = 'column.action';

/**
* @param string $name
*/
public function __construct($name)
{
parent::__construct($name);
$this->setOrderable(false);

$this->setAttribute('class', 'row-action');
}

/**
Expand Down Expand Up @@ -148,19 +155,15 @@ public function setValue($value)
return $this;
}

/**
* Render action button.
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
*/
public function render()
public function toArray()
{
return app('sleeping_owl.template')->view('column.action', [
return parent::toArray() + [
'icon' => $this->icon(),
'style' => $this->style(),
'value' => $this->value(),
'target' => $this->target(),
'url' => $this->url(),
]);
];
}

/**
Expand All @@ -172,6 +175,7 @@ public function getUrl()
if (is_callable($this->url)) {
return call_user_func($this->url, $this->getModel());
}

if (! is_null($this->getModel())) {
return strtr($this->url, [':id' => $this->getModel()->getKey()]);
}
Expand Down
69 changes: 64 additions & 5 deletions src/Columns/Column/BaseColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,49 @@

use Meta;
use Illuminate\Database\Eloquent\Model;
use KodiCMS\Support\Traits\HtmlAttributes;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Renderable;
use KodiCMS\SleepingOwlAdmin\Model\ModelConfiguration;
use KodiCMS\SleepingOwlAdmin\Interfaces\ColumnInterface;

abstract class BaseColumn implements Renderable, ColumnInterface
abstract class BaseColumn implements Renderable, ColumnInterface, Arrayable
{
use HtmlAttributes;

/**
* Column header.
*
* @var ColumnHeader
*/
protected $header;

/**
* Model instance currently rendering.
*
* @var Model
*/
protected $model;

/**
* Column appendant.
*
* @var ColumnInterface
*/
protected $append;

/**
* @var
* Column width.
*
* @var string
*/
protected $width = null;

/**
* @var string
*/
protected $view;

public function __construct()
{
$this->header = new ColumnHeader;
Expand Down Expand Up @@ -74,6 +88,27 @@ public function setWidth($width)
return $this;
}

/**
* @return string
*/
public function getView()
{
if (is_null($this->view)) {
$reflect = new \ReflectionClass($this);
$this->view = 'column.'.strtolower($reflect->getShortName());
}

return $this->view;
}

/**
* @param string $view
*/
public function setView($view)
{
$this->view = $view;
}

/**
* @return ColumnInterface
*/
Expand Down Expand Up @@ -111,6 +146,7 @@ public function setModel(Model $model)
{
$this->model = $model;
$append = $this->getAppend();

if (! is_null($append) && ($append instanceof ColumnInterface)) {
$append->setModel($model);
}
Expand All @@ -136,7 +172,7 @@ protected function getModelConfiguration()
*/
public function setLabel($title)
{
$this->header->setTitle($title);
$this->getHeader()->setTitle($title);

return $this;
}
Expand All @@ -148,7 +184,7 @@ public function setLabel($title)
*/
public function setOrderable($orderable)
{
$this->header->setOrderable($orderable);
$this->getHeader()->setOrderable($orderable);

return $this;
}
Expand All @@ -159,7 +195,19 @@ public function setOrderable($orderable)
*/
public function isOrderable()
{
return $this->header()->isOrderable();
return $this->getHeader()->isOrderable();
}

/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
return [
'attributes' => $this->getAttributes()
];
}

/**
Expand All @@ -169,4 +217,15 @@ public function __toString()
{
return (string) $this->render();
}

/**
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
*/
public function render()
{
return app('sleeping_owl.template')->view(
$this->getView(),
$this->toArray()
);
}
}
21 changes: 16 additions & 5 deletions src/Columns/Column/Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,33 @@

namespace KodiCMS\SleepingOwlAdmin\Columns\Column;

use Form;

class Checkbox extends BaseColumn
{
/**
* @var string
*/
protected $view = 'column.checkbox';

public function __construct()
{
parent::__construct();
$this->setLabel('<input type="checkbox" class="adminCheckboxAll"/>');
$this->setLabel(
Form::checkbox(null, 1, ['class' => 'adminCheckboxAll']
));

$this->setOrderable(false);
$this->setAttribute('class', 'row-checkbox');
}

/**
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
* @return array
*/
public function render()
public function toArray()
{
return app('sleeping_owl.template')->view('column.checkbox', [
return parent::toArray() + [
'value' => $this->getModel()->getKey(),
]);
];
}
}
2 changes: 1 addition & 1 deletion src/Columns/Column/ColumnHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function render()
{
return app('sleeping_owl.template')->view('column.header', [
'title' => $this->getTitle(),
'orderable' => $this->isOrderable(),
'orderable' => $this->isOrderable() ? 'true' : 'false',
]);
}

Expand Down
Loading

0 comments on commit d07f439

Please sign in to comment.