-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathEloquentTreeRepository.php
116 lines (98 loc) · 3.21 KB
/
EloquentTreeRepository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php namespace Anomaly\Streams\Platform\Model;
use Anomaly\Streams\Platform\Ui\Tree\Contract\TreeRepositoryInterface;
use Anomaly\Streams\Platform\Ui\Tree\Event\TreeIsQuerying;
use Anomaly\Streams\Platform\Ui\Tree\TreeBuilder;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Collection;
/**
* Class EloquentTreeRepositoryInterface
*
* @link http://pyrocms.com/
* @author PyroCMS, Inc. <[email protected]>
* @author Ryan Thompson <[email protected]>
*/
class EloquentTreeRepository implements TreeRepositoryInterface
{
use DispatchesJobs;
/**
* The repository model.
*
* @var EloquentModel
*/
protected $model;
/**
* Create a new EloquentModel instance.
*
* @param EloquentModel $model
*/
public function __construct(EloquentModel $model)
{
$this->model = $model;
}
/**
* Get the tree entries.
*
* @param TreeBuilder $builder
* @return Collection
*/
public function get(TreeBuilder $builder)
{
// Start a new query.
$query = $this->model->newQuery();
/*
* Prevent joins from overriding intended columns
* by prefixing with the model's tree name.
*/
$query = $query->select($this->model->getTable() . '.*');
/*
* Eager load any relations to
* save resources and queries.
*/
$query = $query->with($builder->getTreeOption('eager', []));
/*
* Raise and fire an event here to allow
* other things (including filters / views)
* to modify the query before proceeding.
*/
$builder->fire('querying', compact('builder', 'query'));
event(new TreeIsQuerying($builder, $query));
/*
* Before we actually adjust the baseline query
* set the total amount of entries possible back
* on the tree so it can be used later.
*/
$total = $query->count();
$builder->setTreeOption('total_results', $total);
/*
* Order the query results.
*/
foreach ($builder->getTreeOption('order_by', ['sort_order' => 'asc']) as $column => $direction) {
$query = $query->orderBy($column, $direction);
}
return $query->get();
}
/**
* Save the tree.
*
* @param TreeBuilder $builder
* @param array $items
* @param null $parent
*/
public function save(TreeBuilder $builder, array $items = [], $parent = null)
{
$model = $builder->getTreeModel();
$items = $items ?: $builder->getRequestValue('items');
foreach ($items as $index => $item) {
/* @var EloquentModel $entry */
$entry = $model->find($item['id']);
if ($entry->sort_order != $index + 1 || $entry->parent_id != $parent) {
$entry->{$builder->getTreeOption('sort_column', 'sort_order')} = $index + 1;
$entry->{$builder->getTreeOption('parent_column', 'parent_id')} = $parent;
$entry->save();
}
if (isset($item['children'])) {
$this->save($builder, $item['children'], $item['id']);
}
}
}
}