-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
andreydolgov
committed
Sep 14, 2018
1 parent
0d39bda
commit 3e17914
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 !!} /> {{$item->title}} | ||
</label> | ||
</div> | ||
@endforeach | ||
</div> | ||
<hr/> | ||
@endforeach | ||
<input type="hidden" name="{{$name}}[]" /> | ||
@include('admin::form.help-block') | ||
|
||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |