|
1
|
|
|
<?php namespace Distilleries\Expendable\Scopes; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Scope; |
|
4
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
|
|
7
|
|
|
class TranslatableScope implements Scope { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* All of the extensions to be added to the builder. |
|
11
|
|
|
* |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $extensions = ['WithoutTranslation']; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Apply the scope to a given Eloquent query builder. |
|
19
|
|
|
* |
|
20
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
21
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
|
|
public function apply(Builder $builder, Model $model) |
|
25
|
|
|
{ |
|
26
|
|
|
//Check if is an override |
|
27
|
|
|
// |
|
28
|
|
|
$iso = config()->get('local_override'); |
|
29
|
|
|
|
|
30
|
|
|
if (empty($iso)) { |
|
31
|
|
|
$iso = app()->getLocale(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$builder->whereExists(function ($query) use ($model, $iso) { |
|
35
|
52 |
|
$query->select(\DB::raw(1)) |
|
36
|
|
|
->from($model->getQualifiedTable()) |
|
37
|
|
|
->whereRaw($model->getTable() . '.' . $model->getKeyName() . ' = ' . $model->getQualifiedIdElementColumn()) |
|
38
|
|
|
->where($model->getQualifiedIsoColumn(), $iso) |
|
39
|
52 |
|
->where($model->getQualifiedModelColumn(), $model->getTable()); |
|
40
|
|
|
}); |
|
41
|
52 |
|
|
|
42
|
52 |
|
$this->extend($builder); |
|
43
|
52 |
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
52 |
|
/** |
|
47
|
52 |
|
* Extend the query builder with the needed functions. |
|
48
|
52 |
|
* |
|
49
|
52 |
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
50
|
52 |
|
* @return void |
|
51
|
52 |
|
*/ |
|
52
|
|
|
public function extend(Builder $builder) |
|
53
|
52 |
|
{ |
|
54
|
52 |
|
foreach ($this->extensions as $extension) { |
|
55
|
52 |
|
$this->{"add{$extension}"}($builder); |
|
56
|
|
|
} |
|
57
|
52 |
|
|
|
58
|
|
|
} |
|
59
|
52 |
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Add the with-trashed extension to the builder. |
|
63
|
|
|
* |
|
64
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
8 |
|
protected function addWithoutTranslation(Builder $builder) |
|
68
|
|
|
{ |
|
69
|
8 |
|
$builder->macro('withoutTranslation', function (Builder $builder) { |
|
70
|
|
|
return $builder->withoutGlobalScope($this); |
|
71
|
8 |
|
}); |
|
72
|
|
|
} |
|
73
|
|
|
} |