|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Distilleries\Expendable\Scopes; |
|
4
|
|
|
|
|
5
|
|
|
use Distilleries\Expendable\Contracts\TranslatableObserverContract; |
|
6
|
|
|
use Distilleries\Expendable\Models\Translation; |
|
7
|
|
|
|
|
8
|
|
|
trait Translatable |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Boot the soft deleting trait for a model. |
|
12
|
|
|
* |
|
13
|
|
|
* @return void |
|
14
|
|
|
*/ |
|
15
|
|
|
public static function bootTranslatable() |
|
16
|
|
|
{ |
|
17
|
|
|
static::addGlobalScope(new TranslatableScope); |
|
18
|
|
|
try { |
|
19
|
|
|
$observer = app(TranslatableObserverContract::class); |
|
20
|
|
|
static::observe($observer); |
|
21
|
|
|
} catch (\Exception $exception) {} |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Get a new query builder that only includes soft deletes. |
|
26
|
|
|
* |
|
27
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|static |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function withoutTranslation() |
|
30
|
|
|
{ |
|
31
|
|
|
$instance = new static; |
|
32
|
|
|
|
|
33
|
|
|
return $instance->withoutGlobalScope(TranslatableScope::class); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Get the fully qualified "iso" column. |
|
39
|
|
|
* |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getQualifiedIsoColumn() |
|
43
|
|
|
{ |
|
44
|
|
|
$translation = new Translation; |
|
45
|
|
|
|
|
46
|
|
|
return $translation->getTable().'.'.$translation->getIsoColumn(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get the fully qualified "id_element" column. |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getQualifiedIdElementColumn() |
|
55
|
|
|
{ |
|
56
|
|
|
$translation = new Translation; |
|
57
|
|
|
|
|
58
|
|
|
return $translation->getTable().'.'.$translation->getIdElementColumn(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get the fully qualified "id_element" column. |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getQualifiedModelColumn() |
|
67
|
|
|
{ |
|
68
|
|
|
$translation = new Translation; |
|
69
|
|
|
|
|
70
|
|
|
return $translation->getTable().'.'.$translation->getModelColumn(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the fully qualified "id_source" column. |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getQualifiedIdSourceColumn() |
|
79
|
|
|
{ |
|
80
|
|
|
$translation = new Translation; |
|
81
|
|
|
|
|
82
|
|
|
return $translation->getTable().'.'.$translation->getIdSourceColumn(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get the fully qualified "table" column. |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getQualifiedTable() |
|
91
|
|
|
{ |
|
92
|
|
|
$translation = new Translation; |
|
93
|
|
|
|
|
94
|
|
|
return $translation->getTable(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function setTranslation($id_element, $model, $id_source, $iso) |
|
98
|
|
|
{ |
|
99
|
|
|
|
|
100
|
|
|
$translation = Translation::where($this->getQualifiedIdElementColumn(), '=', $id_element) |
|
101
|
|
|
->where($this->getQualifiedModelColumn(), '=', $model) |
|
102
|
|
|
->where($this->getQualifiedIsoColumn(), '=', $iso)->get(); |
|
103
|
|
|
|
|
104
|
|
|
if (!$translation->isEmpty()) { |
|
105
|
|
|
$translation->each(function($trans) { |
|
106
|
|
|
$trans->delete(); |
|
107
|
|
|
}); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
$translation = new Translation; |
|
112
|
|
|
$translation->id_element = $id_element; |
|
|
|
|
|
|
113
|
|
|
$translation->model = $model; |
|
|
|
|
|
|
114
|
|
|
$translation->id_source = $id_source; |
|
|
|
|
|
|
115
|
|
|
$translation->iso = $iso; |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
return $translation->save(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function getIso($model, $id_element) |
|
121
|
|
|
{ |
|
122
|
|
|
$translation = Translation::where($this->getQualifiedIdElementColumn(), '=', $id_element) |
|
123
|
|
|
->where($this->getQualifiedModelColumn(), '=', $model)->get()->last(); |
|
124
|
|
|
|
|
125
|
|
|
return (!empty($translation->iso)) ? $translation->iso : false; |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function hasBeenTranslated($model, $id_source, $iso) |
|
129
|
|
|
{ |
|
130
|
|
|
$translation = Translation::where($this->getQualifiedIdSourceColumn(), '=', $id_source) |
|
131
|
|
|
->where($this->getQualifiedModelColumn(), '=', $model) |
|
132
|
|
|
->where($this->getQualifiedIsoColumn(), '=', $iso) |
|
133
|
|
|
->get()->last(); |
|
134
|
|
|
|
|
135
|
|
|
return (!empty($translation->id_element)) ? $translation->id_element : false; |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function hasTranslation($model, $id_element) |
|
139
|
|
|
{ |
|
140
|
|
|
$translation = Translation::where($this->getQualifiedIdElementColumn(), '=', $id_element) |
|
141
|
|
|
->where($this->getQualifiedModelColumn(), '=', $model)->count(); |
|
142
|
|
|
|
|
143
|
|
|
return (!empty($translation)) ? true : false; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|