1
|
|
|
<?php namespace Distilleries\Expendable\Scopes; |
2
|
|
|
|
3
|
|
|
use Distilleries\Expendable\Models\Translation; |
4
|
|
|
|
5
|
|
|
trait Translatable { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Boot the soft deleting trait for a model. |
9
|
|
|
* |
10
|
|
|
* @return void |
11
|
|
|
*/ |
12
|
|
|
public static function bootTranslatable() |
13
|
|
|
{ |
14
|
|
|
static::addGlobalScope(new TranslatableScope); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get a new query builder that only includes soft deletes. |
20
|
|
|
* |
21
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
22
|
|
|
*/ |
23
|
|
|
public static function withoutTranslation() |
24
|
|
|
{ |
25
|
|
|
$instance = new static; |
26
|
|
|
|
27
|
|
|
return $instance->withoutGlobalScope(TranslatableScope::class); |
|
|
|
|
28
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the fully qualified "iso" column. |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function getQualifiedIsoColumn() |
37
|
|
|
{ |
38
|
|
|
$translation = new Translation; |
39
|
|
|
|
40
|
|
|
return $translation->getTable() . '.' . $translation->getIsoColumn(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get the fully qualified "id_element" column. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function getQualifiedIdElementColumn() |
49
|
|
|
{ |
50
|
|
|
$translation = new Translation; |
51
|
|
|
|
52
|
|
|
return $translation->getTable() . '.' . $translation->getIdElementColumn(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the fully qualified "id_element" column. |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getQualifiedModelColumn() |
61
|
|
|
{ |
62
|
|
|
$translation = new Translation; |
63
|
|
|
|
64
|
|
|
return $translation->getTable() . '.' . $translation->getModelColumn(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the fully qualified "id_source" column. |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function getQualifiedIdSourceColumn() |
73
|
|
|
{ |
74
|
|
|
$translation = new Translation; |
75
|
|
|
|
76
|
|
|
return $translation->getTable() . '.' . $translation->getIdSourceColumn(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the fully qualified "table" column. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getQualifiedTable() |
85
|
|
|
{ |
86
|
|
|
$translation = new Translation; |
87
|
|
|
|
88
|
|
|
return $translation->getTable(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setTranslation($id_element, $model, $id_source, $iso) |
92
|
|
|
{ |
93
|
|
|
|
94
|
|
|
$translation = Translation::where($this->getQualifiedIdElementColumn(), '=', $id_element) |
95
|
|
|
->where($this->getQualifiedModelColumn(), '=', $model) |
96
|
|
|
->where($this->getQualifiedIsoColumn(), '=', $iso)->get(); |
97
|
|
|
|
98
|
|
|
if (!$translation->isEmpty()) { |
99
|
|
|
$translation->delete(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
$translation = new Translation; |
104
|
|
|
$translation->id_element = $id_element; |
105
|
|
|
$translation->model = $model; |
106
|
|
|
$translation->id_source = $id_source; |
107
|
|
|
$translation->iso = $iso; |
108
|
|
|
|
109
|
|
|
return $translation->save(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
public function getIso($model, $id_element) |
113
|
|
|
{ |
114
|
|
|
$translation = Translation::where($this->getQualifiedIdElementColumn(), '=', $id_element) |
115
|
|
|
->where($this->getQualifiedModelColumn(), '=', $model)->get()->last(); |
116
|
|
|
|
117
|
|
|
return (!empty($translation->iso)) ? $translation->iso : false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
View Code Duplication |
public function hasBeenTranslated($model, $id_source, $iso) |
121
|
|
|
{ |
122
|
|
|
$translation = Translation::where($this->getQualifiedIdSourceColumn(), '=', $id_source) |
123
|
|
|
->where($this->getQualifiedModelColumn(), '=', $model) |
124
|
|
|
->where($this->getQualifiedIsoColumn(), '=', $iso) |
125
|
|
|
->get()->last(); |
126
|
|
|
|
127
|
|
|
return (!empty($translation->id_element)) ? $translation->id_element : false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
View Code Duplication |
public function hasTranslation($model, $id_element) |
131
|
|
|
{ |
132
|
|
|
$translation = Translation::where($this->getQualifiedIdElementColumn(), '=', $id_element) |
133
|
|
|
->where($this->getQualifiedModelColumn(), '=', $model)->count(); |
134
|
|
|
|
135
|
|
|
return (!empty($translation)) ? true : false; |
136
|
|
|
} |
137
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.