|
1
|
|
|
<?php namespace Anomaly\Streams\Platform\Ui\Table\Component\Action\Handler; |
|
2
|
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Model\EloquentModel; |
|
4
|
|
|
use Anomaly\Streams\Platform\Ui\Table\Component\Action\ActionHandler; |
|
5
|
|
|
use Anomaly\Streams\Platform\Ui\Table\TableBuilder; |
|
6
|
|
|
use Illuminate\Contracts\Bus\SelfHandling; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ReorderActionHandler |
|
11
|
|
|
* |
|
12
|
|
|
* @link http://anomaly.is/streams-platform |
|
13
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
|
14
|
|
|
* @author Ryan Thompson <[email protected]> |
|
15
|
|
|
* @package Anomaly\Streams\Platform\Ui\Table\Component\Action\Handler |
|
16
|
|
|
*/ |
|
17
|
|
|
class Reorder extends ActionHandler implements SelfHandling |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Save the order of the entries. |
|
22
|
|
|
* |
|
23
|
|
|
* @param TableBuilder $builder |
|
24
|
|
|
* @param Request $request |
|
25
|
|
|
*/ |
|
26
|
|
|
public function handle(TableBuilder $builder, Request $request) |
|
27
|
|
|
{ |
|
28
|
|
|
$count = 0; |
|
29
|
|
|
|
|
30
|
|
|
$model = $builder->getTableModel(); |
|
31
|
|
|
|
|
32
|
|
|
/* @var EloquentModel $entry */ |
|
33
|
|
|
foreach ($request->get($builder->getTableOption('prefix') . 'order', []) as $k => $id) { |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* If this is a many to many relationship (multiple) then use the entry_id and related_id |
|
37
|
|
|
* rather than the id |
|
38
|
|
|
*/ |
|
39
|
|
|
if (strstr($id, ':')) { |
|
40
|
|
|
$idParts = explode(':', $id); |
|
41
|
|
|
if($model->where('entry_id', $idParts[0])->where('related_id', $idParts[1])->update(['sort_order' => $k + 1])) { |
|
|
|
|
|
|
42
|
|
|
$count++; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Otherwise just use the id |
|
48
|
|
|
*/ |
|
49
|
|
|
if (is_numeric($id) && $entry = $model->find($id)) { |
|
|
|
|
|
|
50
|
|
|
if (($entry->sort_order = $k + 1) && $entry->save()) { |
|
51
|
|
|
$count++; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$builder->fire('reordered', compact('count', 'builder')); |
|
57
|
|
|
|
|
58
|
|
|
$this->messages->success(trans('streams::message.reorder_success', compact('count'))); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: