Skip to content

Commit

Permalink
multiselectfilter
Browse files Browse the repository at this point in the history
  • Loading branch information
andreydolgov committed Sep 14, 2018
1 parent 0d39bda commit 3e17914
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
30 changes: 30 additions & 0 deletions resources/views/form/multipleselectfilters.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div class="form-group {!! !$errors->has($errorKey) ?: 'has-error' !!}">

<div class="col-sm-12">

@include('admin::form.error')
{{--@php--}}
{{--var_dump($column);--}}
{{--var_dump($value);--}}
{{--var_dump((array)old($column, $value));--}}
{{--@endphp--}}
@foreach($data as $group)
<div class ="row">
<div class="col-sm-12">
{{$group['info']->title}}
</div>
@foreach($group['filters'] as $item)
<div class="col-sm-2">
<label class="checkbox-inline">
<input type="checkbox" name="{{$name}}[]" value="{{$item->id}}" class="{{$class}}" {{ in_array($item->id, (array)old($column, $value)) ?'checked':'' }} {!! $attributes !!} />&nbsp;{{$item->title}}
</label>
</div>
@endforeach
</div>
<hr/>
@endforeach
<input type="hidden" name="{{$name}}[]" />
@include('admin::form.help-block')

</div>
</div>
2 changes: 2 additions & 0 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
* @method Field\MultipleFile multipleFile($column, $label = '')
* @method Field\Captcha captcha($column, $label = '')
* @method Field\Listbox listbox($column, $label = '')
* @method Field\MultipleSelectFilters multipleSelectFilters($column, $label = '')
*/
class Form implements Renderable
{
Expand Down Expand Up @@ -1318,6 +1319,7 @@ public static function registerBuiltinFields()
'multipleImage' => Field\MultipleImage::class,
'captcha' => Field\Captcha::class,
'listbox' => Field\Listbox::class,
'multipleSelectFilters' => Field\MultipleSelectFilters::class,
];

foreach ($map as $abstract => $class) {
Expand Down
118 changes: 118 additions & 0 deletions src/Form/Field/MultipleSelectFilters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Encore\Admin\Form\Field;

use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Encore\Admin\Admin;

class MultipleSelectFilters extends Select
{
/**
* Other key for many-to-many relation.
*
* @var string
*/
protected $otherKey;

/**
* Get other key for this many-to-many relation.
*
* @throws \Exception
*
* @return string
*/
protected function getOtherKey()
{
if ($this->otherKey) {
return $this->otherKey;
}

if (is_callable([$this->form->model(), $this->column]) &&
($relation = $this->form->model()->{$this->column}()) instanceof BelongsToMany
) {
/* @var BelongsToMany $relation */
$fullKey = $relation->getQualifiedRelatedPivotKeyName();
$fullKeyArray = explode('.', $fullKey);

return $this->otherKey = end($fullKeyArray);
}

throw new \Exception('Column of this field must be a `BelongsToMany` relation.');
}

public function fill($data)
{
$relations = array_get($data, $this->column);

if (is_string($relations)) {
$this->value = explode(',', $relations);
}

if (is_array($relations)) {
if (is_string(current($relations))) {
$this->value = $relations;
} else {
foreach ($relations as $relation) {
$this->value[] = array_get($relation, "pivot.{$this->getOtherKey()}");
}
}
}
}

public function setOriginal($data)
{
$relations = array_get($data, $this->column);

if (is_string($relations)) {
$this->original = explode(',', $relations);
}

if (is_array($relations)) {
if (is_string(current($relations))) {
$this->original = $relations;
} else {
foreach ($relations as $relation) {
$this->original[] = array_get($relation, "pivot.{$this->getOtherKey()}");
}
}
}
}

public function prepare($value)
{
$value = (array) $value;

return array_filter($value);
}

public function options($options = [])
{
$this->options = $options;
return $this;
}

public function prepare_data($data){

$result = array();
foreach ($data['data'] as $item){
if(isset($result[$item->{$data['key']}])){
$result[$item->{$data['key']}]['filters'][] = $item;
}else{
$result[$item->{$data['key']}]['info'] = $item->{$data['field']}()->first();
$result[$item->{$data['key']}]['filters'][] = $item;
}
}

return $result;
}

public function render()
{
if (empty($this->script)) {
$this->script = "$('{$this->getElementClassSelector()}').iCheck({checkboxClass:'icheckbox_minimal-blue'});"; }
Admin::script($this->script);
$data = $this->prepare_data($this->options);

return view($this->getView(), $this->variables())->with(['options' => $this->options,'data'=>$data]);
}
}

0 comments on commit 3e17914

Please sign in to comment.