1
|
|
|
<?php namespace Distilleries\Expendable\Scopes; |
2
|
|
|
|
3
|
|
|
use Distilleries\Expendable\Helpers\UserUtils; |
4
|
|
|
use Illuminate\Database\Eloquent\Builder; |
5
|
|
|
use Illuminate\Database\Eloquent\Scope; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
class StatusScope implements Scope{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* All of the extensions to be added to the builder. |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $extensions = ['WithOffline', 'OnlyOffline']; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Apply the scope to a given Eloquent query builder. |
20
|
|
|
* |
21
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
22
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
300 |
|
public function apply(Builder $builder, Model $model) |
26
|
|
|
{ |
27
|
300 |
|
if (!UserUtils::hasDisplayAllStatus()) |
28
|
|
|
{ |
29
|
96 |
|
$builder->where($model->getQualifiedStatusColumn(), true); |
30
|
|
|
} |
31
|
300 |
|
$this->extend($builder); |
32
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Extend the query builder with the needed functions. |
37
|
|
|
* |
38
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
384 |
|
public function extend(Builder $builder) |
42
|
|
|
{ |
43
|
384 |
|
foreach ($this->extensions as $extension) |
44
|
|
|
{ |
45
|
384 |
|
$this->{"add{$extension}"}($builder); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Add the with-trashed extension to the builder. |
53
|
|
|
* |
54
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
protected function addWithOffline(Builder $builder) |
58
|
|
|
{ |
59
|
384 |
|
$builder->macro('withOffline', function(Builder $builder) |
60
|
|
|
{ |
61
|
4 |
|
return $builder->withoutGlobalScope($this); |
62
|
384 |
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Add the only-trashed extension to the builder. |
68
|
|
|
* |
69
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
protected function addOnlyOffline(Builder $builder) |
73
|
|
|
{ |
74
|
384 |
|
$builder->macro('onlyOffline', function(Builder $builder) |
75
|
|
|
{ |
76
|
|
|
$model = $builder->getModel(); |
77
|
|
|
|
78
|
|
|
$builder->withoutGlobalScope($this) |
79
|
|
|
->where($model->getQualifiedStatusColumn(), '=', false); |
80
|
|
|
|
81
|
|
|
return $builder; |
82
|
384 |
|
}); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |