1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel MultiLang package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\LaravelMultiLang\Models; |
12
|
|
|
|
13
|
|
|
use Illuminate\Database\Eloquent\Builder; |
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
use Illuminate\Database\Eloquent\ScopeInterface; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class LocalizableScope implements ScopeInterface |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Apply the scope to a given Eloquent query builder. |
22
|
|
|
* |
23
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
24
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
|
public function apply(Builder $builder, Model $model) |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
if (!$this->queryHasLocalizableColumn($builder)) { |
31
|
|
|
$builder->where($model->getQualifiedLocalizableColumn(), '=', app()->getLocale()); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function queryHasLocalizableColumn(Builder $builder) |
36
|
|
|
{ |
37
|
|
|
$wheres = $builder->getQuery()->wheres; |
38
|
|
|
$column = $this->getLocalizableColumn($builder); |
39
|
|
|
foreach($wheres as $where) { |
40
|
|
|
if ($where['column'] == $column) { |
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the "deleted at" column for the builder. |
49
|
|
|
* |
50
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
protected function getLocalizableColumn(Builder $builder) |
54
|
|
|
{ |
55
|
|
|
if (count($builder->getQuery()->joins) > 0) { |
56
|
|
|
return $builder->getModel()->getQualifiedLocalizableColumn(); |
57
|
|
|
} else { |
58
|
|
|
return $builder->getModel()->getLocalizableColumn(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Remove the scope from the given Eloquent query builder. |
65
|
|
|
* |
66
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
67
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public function remove(Builder $builder, Model $model) |
71
|
|
|
{ |
72
|
|
|
$column = $model->getQualifiedDeletedAtColumn(); |
|
|
|
|
73
|
|
|
|
74
|
|
|
$query = $builder->getQuery(); |
75
|
|
|
|
76
|
|
|
foreach ((array) $query->wheres as $key => $where) |
77
|
|
|
{ |
78
|
|
|
// If the where clause is a soft delete date constraint, we will remove it from |
79
|
|
|
// the query and reset the keys on the wheres. This allows this developer to |
80
|
|
|
// include deleted model in a relationship result set that is lazy loaded. |
81
|
|
|
if ($this->isSoftDeleteConstraint($where, $column)) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
unset($query->wheres[$key]); |
84
|
|
|
|
85
|
|
|
$query->wheres = array_values($query->wheres); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.