diff --git a/src/ColumnFilters/BaseColumnFilter.php b/src/ColumnFilters/BaseColumnFilter.php index 5f1c764..0ed0f1b 100644 --- a/src/ColumnFilters/BaseColumnFilter.php +++ b/src/ColumnFilters/BaseColumnFilter.php @@ -51,7 +51,7 @@ public function toArray() */ public function render() { - return app('sleeping_owl.template')->view('columnfilter.'.$this->getView(), $this->getParams()); + return app('sleeping_owl.template')->view('columnfilter.'.$this->getView(), $this->getParams())->render(); } /** diff --git a/src/ColumnFilters/Range.php b/src/ColumnFilters/Range.php index 3d0b8e8..4568fd6 100644 --- a/src/ColumnFilters/Range.php +++ b/src/ColumnFilters/Range.php @@ -50,7 +50,7 @@ public function getFrom() */ public function setFrom(ColumnFilterInterface $from) { - $this->from = (int) $from; + $this->from = $from; return $this; } @@ -70,7 +70,7 @@ public function getTo() */ public function setTo(ColumnFilterInterface $to) { - $this->to = (int) $to; + $this->to = $to; return $this; } diff --git a/src/ColumnFilters/Select.php b/src/ColumnFilters/Select.php index 2e184fe..d28a368 100644 --- a/src/ColumnFilters/Select.php +++ b/src/ColumnFilters/Select.php @@ -50,12 +50,21 @@ public function getModel() } /** - * @param Model $model + * @param Model|string $model * * @return $this + * @throws \Exception */ - public function setModel(Model $model) + public function setModel($model) { + if (is_string($model) and class_exists($model)) { + $model = new $model; + } + + if (! ($model instanceof Model)) { + throw new \Exception('Model must be an instance of Illuminate\Database\Eloquent\Model'); + } + $this->model = $model; return $this; @@ -155,7 +164,7 @@ public function getParams() { return parent::getParams() + [ 'options' => $this->getOptions(), - 'placeholder' => $this->getPlaceholder(), + 'placeholder' => $this->getPlaceholder() ]; } @@ -208,7 +217,7 @@ protected function loadOptions() { $repository = new BaseRepository($this->getModel()); $key = $repository->getModel()->getKeyName(); - $options = $repository->query()->get()->lists($this->getDisplay(), $key); + $options = $repository->getQuery()->get()->lists($this->getDisplay(), $key); if ($options instanceof Collection) { $options = $options->all(); diff --git a/src/Columns/Column/BaseColumn.php b/src/Columns/Column/BaseColumn.php index c472fba..6790e27 100644 --- a/src/Columns/Column/BaseColumn.php +++ b/src/Columns/Column/BaseColumn.php @@ -112,7 +112,7 @@ public function setView($view) /** * @return ColumnInterface */ - public function getAppend() + public function getAppends() { return $this->append; } @@ -122,7 +122,7 @@ public function getAppend() * * @return $this */ - public function setAppend(ColumnInterface $append) + public function append(ColumnInterface $append) { $this->append = $append; @@ -145,9 +145,9 @@ public function getModel() public function setModel(Model $model) { $this->model = $model; - $append = $this->getAppend(); + $append = $this->getAppends(); - if (! is_null($append) && ($append instanceof ColumnInterface)) { + if (! is_null($append)) { $append->setModel($model); } diff --git a/src/Columns/Column/Count.php b/src/Columns/Column/Count.php index 8bc99b8..41210ed 100644 --- a/src/Columns/Column/Count.php +++ b/src/Columns/Column/Count.php @@ -22,7 +22,7 @@ public function toArray() { return parent::toArray() + [ 'value' => count($this->getModelValue()), - 'append' => $this->getAppend(), + 'append' => $this->getAppends(), ]; } } diff --git a/src/Columns/Column/Custom.php b/src/Columns/Column/Custom.php index 9cfeac6..311b600 100644 --- a/src/Columns/Column/Custom.php +++ b/src/Columns/Column/Custom.php @@ -3,6 +3,7 @@ namespace KodiCMS\SleepingOwlAdmin\Columns\Column; use Closure; +use Illuminate\Database\Eloquent\Model; class Custom extends BaseColumn { @@ -68,7 +69,7 @@ public function toArray() { return parent::toArray() + [ 'value' => $this->getValue($this->getModel()), - 'append' => $this->getAppend(), + 'append' => $this->getAppends(), ]; } } diff --git a/src/Columns/Column/DateTime.php b/src/Columns/Column/DateTime.php index dd80990..482f412 100644 --- a/src/Columns/Column/DateTime.php +++ b/src/Columns/Column/DateTime.php @@ -59,7 +59,7 @@ public function toArray() return parent::toArray() + [ 'value' => $value, 'originalValue' => $originalValue, - 'append' => $this->getAppend(), + 'append' => $this->getAppends(), ]; } } diff --git a/src/Columns/Column/Email.php b/src/Columns/Column/Email.php index b335743..e51bad8 100644 --- a/src/Columns/Column/Email.php +++ b/src/Columns/Column/Email.php @@ -17,7 +17,7 @@ public function toArray() { return parent::toArray() + [ 'value' => $this->getModelValue(), - 'append' => $this->getAppend(), + 'append' => $this->getAppends(), ]; } } diff --git a/src/Columns/Column/Filter.php b/src/Columns/Column/Filter.php index dc9c785..6f75120 100644 --- a/src/Columns/Column/Filter.php +++ b/src/Columns/Column/Filter.php @@ -28,7 +28,7 @@ public function __construct($name) public function getRelatedModel() { if (is_null($this->relatedModel)) { - $this->setModel(get_class($this->getModel())); + $this->setRelatedModel($this->getModel()); } return $this->relatedModel; @@ -72,9 +72,9 @@ public function setField($field) */ public function getUrl() { - $value = $this->getValue($this->getModel(), $this->getField()); + $value = $this->getModelValue($this->getModel(), $this->getField()); - return app('sleeping_owl')->getModel($this->relatedModel)->getDisplayUrl([$this->getName() => $value]); + return app('sleeping_owl')->getModel($this->getRelatedModel())->getDisplayUrl([$this->getName() => $value]); } /** @@ -83,7 +83,7 @@ public function getUrl() */ protected function isSelf() { - return get_class($this->getModel()) == $this->getRelatedModel(); + return get_class($this->getModel()) == get_class($this->getRelatedModel()); } /** @@ -95,7 +95,7 @@ public function toArray() 'icon' => $this->isSelf() ? 'fa-filter' : 'fa-arrow-circle-o-right', 'title' => $this->isSelf() ? trans('sleepingowl::core.table.filter') : trans('sleepingowl::core.table.filter-goto'), 'url' => $this->getUrl(), - 'value' => $this->getValue($this->getModel(), $this->getField()), + 'value' => $this->getModelValue($this->getModel(), $this->getField()), ]; } } diff --git a/src/Columns/Column/Image.php b/src/Columns/Column/Image.php index 176a391..6683ab5 100644 --- a/src/Columns/Column/Image.php +++ b/src/Columns/Column/Image.php @@ -27,7 +27,7 @@ public function toArray() return parent::toArray() + [ 'value' => $value, - 'append' => $this->getAppend(), + 'append' => $this->getAppends(), ]; } } diff --git a/src/Columns/Column/Link.php b/src/Columns/Column/Link.php index b5d6fd9..1d9fc3c 100644 --- a/src/Columns/Column/Link.php +++ b/src/Columns/Column/Link.php @@ -42,7 +42,7 @@ public function toArray() return parent::toArray() + [ 'value' => $this->getModelValue(), 'link' => $this->getModelConfiguration()->getEditUrl($this->getModel()->getKey()), - 'append' => $this->getAppend(), + 'append' => $this->getAppends(), 'linkAttributes' => $this->getLinkAttributes() ]; } diff --git a/src/Columns/Column/Lists.php b/src/Columns/Column/Lists.php index 4d39465..c6707b0 100644 --- a/src/Columns/Column/Lists.php +++ b/src/Columns/Column/Lists.php @@ -20,7 +20,7 @@ public function toArray() { return parent::toArray() + [ 'values' => $this->getModelValue(), - 'append' => $this->getAppend(), + 'append' => $this->getAppends(), ]; } } diff --git a/src/Columns/Column/NamedColumn.php b/src/Columns/Column/NamedColumn.php index 64f89b4..d580151 100644 --- a/src/Columns/Column/NamedColumn.php +++ b/src/Columns/Column/NamedColumn.php @@ -65,9 +65,9 @@ protected function getValueFromObject($instance, $name) $part = array_shift($parts); if ($instance instanceof Collection) { - $instance = $instance->lists($part); + $instance = $instance->pluck($part); } else { - $instance = $instance->{$part}; + $instance = $instance->getAttribute($part); } if (! empty($parts) && ! is_null($instance)) { diff --git a/src/Columns/Column/String.php b/src/Columns/Column/String.php index 00dc84b..e27ae60 100644 --- a/src/Columns/Column/String.php +++ b/src/Columns/Column/String.php @@ -22,7 +22,7 @@ public function toArray() { return parent::toArray() + [ 'value' => $this->getModelValue(), - 'append' => $this->getAppend(), + 'append' => $this->getAppends(), ]; } } diff --git a/src/Display/DisplayDatatables.php b/src/Display/DisplayDatatables.php index 2849481..4369fef 100644 --- a/src/Display/DisplayDatatables.php +++ b/src/Display/DisplayDatatables.php @@ -6,6 +6,10 @@ class DisplayDatatables extends DisplayTable { + const FILTER_POSITION_TOP = 0; + const FILTER_POSITION_BOTTOM = 2; + const FILTER_POSITION_BOTH = 2; + /** * View to render. * @var string @@ -25,7 +29,12 @@ class DisplayDatatables extends DisplayTable /** * @var array */ - protected $attributes = []; + protected $datatableAttributes = []; + + /** + * @var int + */ + protected $filterPosition = self::FILTER_POSITION_BOTH; /** * Initialize display. @@ -38,6 +47,42 @@ public function initialize() $columnFilter->initialize(); } } + + $this->setAttribute('data-order', json_encode($this->getOrder())); + $this->setAttribute('data-attributes', json_encode($this->getDatatableAttributes(), JSON_FORCE_OBJECT)); + } + + /** + * TODO: сделать чтобы работал + * @return array + */ + public function getDatatableAttributes() + { + return $this->datatableAttributes; + } + + /** + * @param array $datatableAttributes + */ + public function setDatatableAttributes(array $datatableAttributes) + { + $this->datatableAttributes = $datatableAttributes; + } + + /** + * @return int + */ + public function getFilterPosition() + { + return $this->filterPosition; + } + + /** + * @param int $filterPosition + */ + public function setFilterPosition($filterPosition) + { + $this->filterPosition = $filterPosition; } /** @@ -88,30 +133,6 @@ public function setColumnFilters($columnFilters) return $this; } - /** - * @return array - */ - public function getAttributes() - { - return $this->attributes; - } - - /** - * @param array|string $attributes - * - * @return $this - */ - public function setAttributes($attributes) - { - if (! is_array($attributes)) { - $attributes = func_get_args(); - } - - $this->attributes = $attributes; - - return $this; - } - /** * Get view render parameters. * @return array @@ -121,7 +142,7 @@ public function getParams() $params = parent::getParams(); $params['order'] = $this->getOrder(); $params['columnFilters'] = $this->getColumnFilters(); - $params['attributes'] = $this->getAttributes(); + $params['filterPosition'] = $this->getFilterPosition(); return $params; } diff --git a/src/Display/DisplayTable.php b/src/Display/DisplayTable.php index 62fa1f1..0a0e307 100644 --- a/src/Display/DisplayTable.php +++ b/src/Display/DisplayTable.php @@ -5,6 +5,7 @@ use Input; use Closure; use Illuminate\Database\Eloquent\Builder; +use KodiCMS\Support\Traits\HtmlAttributes; use Illuminate\Contracts\Support\Renderable; use KodiCMS\SleepingOwlAdmin\Columns\Column; use KodiCMS\SleepingOwlAdmin\Model\ModelConfiguration; @@ -18,6 +19,8 @@ class DisplayTable implements Renderable, DisplayInterface { + use HtmlAttributes; + /** * @var string */ @@ -88,9 +91,17 @@ public function setClass($class) } } + /** + * @return string + */ + public function getClass() + { + return $this->class; + } + public function initialize() { - $this->repository = new BaseRepository($this->class); + $this->repository = new BaseRepository($this->getClass()); $this->repository->setWith($this->getWith()); $this->initializeFilters(); foreach ($this->getAllColumns() as $column) { @@ -98,6 +109,8 @@ public function initialize() $column->initialize(); } } + + $this->setAttribute('class', 'table table-striped'); } /** @@ -163,6 +176,8 @@ public function setWith($with) public function setApply(Closure $apply) { $this->apply = $apply; + + return $this; } /** @@ -214,12 +229,8 @@ public function getScopes() * * @return $this */ - public function setScopes($scopes) + public function setScopes(array $scopes) { - if (! is_array($scopes)) { - $scopes = func_get_args(); - } - $this->scopes = $scopes; return $this; @@ -232,7 +243,11 @@ public function setScopes($scopes) */ public function appendScope($scope) { - $this->filters[] = $scope; + if (! is_array($scope)) { + $scope = func_get_args(); + } + + $this->scopes[] = $scope; return $this; } @@ -329,7 +344,7 @@ public function getActions() { foreach ($this->actions as $action) { $action->setUrl($this->getModel()->getDeleteUrl([ - '_action' => $action->name(), + '_action' => $action->getName(), '_ids' => '', ])); } @@ -378,6 +393,7 @@ public function getParams() 'creatable' => ! is_null($model->fireCreate()), 'createUrl' => $model->getCreateUrl($this->getParameters() + Input::all()), 'actions' => $this->getActions(), + 'attributes' => $this->getAttributes() ]; } @@ -394,6 +410,11 @@ public function render() return app('sleeping_owl.template')->view('display.'.$this->view, $params); } + public function getColection() + { + + } + /** * Get the instance as an array. * @@ -469,6 +490,7 @@ protected function modifyQuery(Builder $query) foreach ($this->getScopes() as $scope) { if (! is_null($scope)) { $method = array_shift($scope); + call_user_func_array([ $query, $method, @@ -487,7 +509,7 @@ protected function modifyQuery(Builder $query) */ protected function getModel() { - return app('sleeping_owl')->getModel($this->class); + return app('sleeping_owl')->getModel($this->getClass()); } /** diff --git a/src/Model/ModelConfiguration.php b/src/Model/ModelConfiguration.php index 0a15a8d..c154c08 100644 --- a/src/Model/ModelConfiguration.php +++ b/src/Model/ModelConfiguration.php @@ -279,7 +279,8 @@ public function fireEdit($id) if (! is_callable($this->edit)) { return; } - $edit = app()->call($this->edit, [$id]); + + $edit = app()->call($this->edit, ['id' => $id]); if ($edit instanceof DisplayInterface) { $edit->setClass($this->getClass()); $edit->initialize(); @@ -296,6 +297,7 @@ public function fireEdit($id) public function fireFullEdit($id) { $edit = $this->fireEdit($id); + if ($edit instanceof FormInterface) { $edit->setAction($this->getUpdateUrl($id)); $edit->setId($id); diff --git a/src/Providers/ModuleServiceProvider.php b/src/Providers/ModuleServiceProvider.php index d58ebec..f4c995f 100644 --- a/src/Providers/ModuleServiceProvider.php +++ b/src/Providers/ModuleServiceProvider.php @@ -4,6 +4,7 @@ use Route; use Event; +use ModulesLoader; use ModulesFileSystem; use KodiCMS\Navigation\Navigation; use KodiCMS\Support\ServiceProvider; diff --git a/src/SleepingOwlAdmin.php b/src/SleepingOwlAdmin.php index cbe0fb3..d548542 100644 --- a/src/SleepingOwlAdmin.php +++ b/src/SleepingOwlAdmin.php @@ -60,6 +60,10 @@ public function registerModel($class, Closure $callback = null) */ public function getModel($class) { + if (is_object($class)) { + $class = get_class($class); + } + if (! $this->hasModel($class)) { $this->registerModel($class); } diff --git a/src/resources/views/default/column/filter.blade.php b/src/resources/views/default/column/filter.blade.php index 781bde5..e80df61 100644 --- a/src/resources/views/default/column/filter.blade.php +++ b/src/resources/views/default/column/filter.blade.php @@ -1,5 +1,5 @@ @if ( ! is_null($value)) - + @endif diff --git a/src/resources/views/default/column/order.blade.php b/src/resources/views/default/column/order.blade.php index 611f821..8875480 100644 --- a/src/resources/views/default/column/order.blade.php +++ b/src/resources/views/default/column/order.blade.php @@ -1,20 +1,18 @@