1 | <?php |
||
2 | |||
3 | namespace Translation\Models; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Model; |
||
6 | |||
7 | class Language extends Model |
||
8 | { |
||
9 | use \Illuminate\Database\Eloquent\SoftDeletes; |
||
10 | |||
11 | /** |
||
12 | * Table name in the database. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $table = 'languages'; |
||
17 | |||
18 | public $incrementing = false; |
||
19 | protected $casts = [ |
||
20 | 'code' => 'string', |
||
21 | ]; |
||
22 | protected $primaryKey = 'code'; |
||
23 | protected $keyType = 'string'; |
||
24 | |||
25 | /** |
||
26 | * List of variables that cannot be mass assigned |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $fillable = ['name']; |
||
31 | |||
32 | /** |
||
33 | * Each language may have several translations. |
||
34 | * |
||
35 | * SOmente se nao tiver pais |
||
36 | */ |
||
37 | public function translations() |
||
38 | { |
||
39 | return $this->hasMany(Translation::class, 'locale', 'code'); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Each language may have several translations. |
||
44 | */ |
||
45 | public function locales() |
||
46 | { |
||
47 | return $this->hasMany(Locale::class, 'language_code', 'code'); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Returns the name of this language in the current selected language. |
||
52 | * |
||
53 | * @return string |
||
54 | */ |
||
55 | public function getLanguageCodeAttribute() |
||
56 | { |
||
57 | return "languages.{$this->language_code}"; |
||
58 | } |
||
59 | |||
60 | public function getImageUrl( $withBaseUrl = false ) |
||
61 | { |
||
62 | if(!$this->icon) { return null; |
||
0 ignored issues
–
show
|
|||
63 | } |
||
64 | |||
65 | $imgDir = '/images/languages/' . $this->id; |
||
66 | $url = $imgDir . '/' . $this->icon; |
||
67 | |||
68 | return $withBaseUrl ? URL::asset($url) : $url; |
||
0 ignored issues
–
show
|
|||
69 | } |
||
70 | |||
71 | } |
||
72 | |||
73 | |||
74 | /** |
||
75 | * VEio do Wiki |
||
76 | */ |
||
77 | // <?php |
||
78 | |||
79 | // namespace Population\Models\Components\Wiki; |
||
80 | |||
81 | // use Pedreiro\Models\Base; //use Illuminate\Database\Eloquent\Model; |
||
82 | // use Illuminate\Database\Eloquent\SoftDeletes; |
||
83 | // use DomainException; |
||
84 | // use Session; |
||
85 | |||
86 | // class Language extends Base |
||
87 | // { |
||
88 | // use SoftDeletes; |
||
89 | |||
90 | // // Meta ======================================================================== |
||
91 | |||
92 | // /** |
||
93 | // * The attributes that are not mass-assignable. |
||
94 | // * |
||
95 | // * @var array |
||
96 | // */ |
||
97 | // protected $guarded = ['id', 'created_at', 'updated_at', 'deleted_at']; |
||
98 | |||
99 | // /** |
||
100 | // * The attributes that should be casted to native types. |
||
101 | // * |
||
102 | // * @var array |
||
103 | // */ |
||
104 | // protected $casts = ['is_default' => 'boolean']; |
||
105 | |||
106 | // /** |
||
107 | // * What should be returned when this model is converted to string. |
||
108 | // * |
||
109 | // * @return string |
||
110 | // */ |
||
111 | // public function __toString() |
||
112 | // { |
||
113 | // return (string) $this->name; |
||
114 | // } |
||
115 | |||
116 | // /** |
||
117 | // * Get the human-friendly singular name of the resource. |
||
118 | // * |
||
119 | // * @return string |
||
120 | // */ |
||
121 | // protected function getSingularAttribute() |
||
122 | // { |
||
123 | // return _('Language'); |
||
124 | // } |
||
125 | |||
126 | // /** |
||
127 | // * Get the human-friendly plural name of the resource. |
||
128 | // * |
||
129 | // * @return string |
||
130 | // */ |
||
131 | // protected function getPluralAttribute() |
||
132 | // { |
||
133 | // return _('Languages'); |
||
134 | // } |
||
135 | |||
136 | // // Events ====================================================================== |
||
137 | |||
138 | // public static function boot() |
||
139 | // { |
||
140 | // // NOTE events cycle is as follows: |
||
141 | // // saving -> creating -> created -> saved |
||
142 | // // saving -> updating -> updated -> saved |
||
143 | // // deleting -> deleted -> restoring -> restored |
||
144 | |||
145 | // parent::boot(); |
||
146 | |||
147 | // static::updating( |
||
148 | // function ($language) { |
||
149 | // // Make sure to keep the only default language as default |
||
150 | // if($language->getOriginal('is_default') and ! $language->is_default) { |
||
151 | // throw new DomainException(_('The defaulf status cannot be removed') . '. ' . _('Make another language the default one instead.')); |
||
152 | // } |
||
153 | // } |
||
154 | // ); |
||
155 | |||
156 | // static::saved( |
||
157 | // function ($language) { |
||
158 | // // Only one Language can be the default |
||
159 | // if($language->is_default) { |
||
160 | // self::where('id', '<>', $language->id)->update(array('is_default' => 0)); |
||
161 | // } |
||
162 | // } |
||
163 | // ); |
||
164 | // } |
||
165 | |||
166 | // // Relationships =============================================================== |
||
167 | |||
168 | // public function users() |
||
169 | // { |
||
170 | // return $this->hasMany(\Illuminate\Support\Facades\Config::get('sitec.core.models.user', \App\Models\User::class)); |
||
171 | // } |
||
172 | |||
173 | // // Static Methods ============================================================== |
||
174 | |||
175 | // /** |
||
176 | // * Detect the language that the application should use. |
||
177 | // * |
||
178 | // * @return Language |
||
179 | // */ |
||
180 | // public static function detect() |
||
181 | // { |
||
182 | // // Get all languages from database |
||
183 | // $all = self::orderBy('is_default', 'desc')->get(); |
||
184 | // if(! $all->count()) { |
||
185 | // return new static; |
||
186 | // } |
||
187 | |||
188 | // // Helper function for adding information about where the language was detected from |
||
189 | // $addOrigin = function ($language, $origin) { |
||
190 | // $language->detectedFrom = $origin; |
||
191 | |||
192 | // return $language; |
||
193 | // }; |
||
194 | |||
195 | // // Try with previous value from session |
||
196 | // if($language = Session::get('language') and isset($language->id) and $all->contains($language->id)) { |
||
197 | // return $addOrigin($all->find($language->id), 'session'); |
||
198 | // } |
||
199 | |||
200 | // // Try with user's browser language |
||
201 | // $language = with(new \Browser\Language)->getLanguageLocale('_'); |
||
202 | // $type = (strlen($language) === 2) ? 'code' : 'locale'; |
||
203 | // $languages = $all->where($type, $language); |
||
204 | // if($languages->count()) { |
||
205 | // return $addOrigin($languages->first(), 'browser'); |
||
206 | // } |
||
207 | |||
208 | // // Browser doesn't have any known language. Fallback to default |
||
209 | // return $addOrigin($all->first(), 'default'); |
||
210 | // } |
||
211 | |||
212 | // /** |
||
213 | // * Forget session language. |
||
214 | // * |
||
215 | // * @return void |
||
216 | // */ |
||
217 | // public static function forget() |
||
218 | // { |
||
219 | // Session::forget('language'); |
||
220 | // } |
||
221 | |||
222 | // // Bussiness logic ============================================================= |
||
223 | |||
224 | // /** |
||
225 | // * Scope to filter by 'is_default' column. |
||
226 | // * |
||
227 | // * @param \Illuminate\Database\Eloquent\Builder |
||
228 | // * @param boolean |
||
229 | // * @return \Illuminate\Database\Eloquent\Builder |
||
230 | // */ |
||
231 | // public function scopeIsDefault($query, $isDefault = true) |
||
232 | // { |
||
233 | // $query->where('is_default', $isDefault); |
||
234 | // } |
||
235 | |||
236 | // /** |
||
237 | // * Store session language. |
||
238 | // * |
||
239 | // * @return Language |
||
240 | // */ |
||
241 | // public function remember() |
||
242 | // { |
||
243 | // if($this->getKey()) { |
||
244 | // Session::put('language', (object) $this->toArray()); |
||
245 | // } |
||
246 | |||
247 | // return $this; |
||
248 | // } |
||
249 | |||
250 | // /** |
||
251 | // * Set $this language as the application language |
||
252 | // * |
||
253 | // * @param integer $category see http://php.net/manual/en/function.setlocale.php |
||
254 | // * @param boolean $force |
||
255 | // * @return Language |
||
256 | // */ |
||
257 | // public function apply($category = LC_ALL, $force = false) |
||
258 | // { |
||
259 | // bindtextdomain('messages', base_path('resources/lang/')); |
||
260 | // textdomain('messages'); |
||
261 | // $locale = $this->locale; |
||
262 | |||
263 | // $currentLocale = setlocale($category, "$locale.UTF-8", "$locale.utf-8", "$locale.utf8", "$locale UTF8", "$locale UTF-8", "$locale utf-8", "$locale utf8", "$locale UTF8", $locale); |
||
264 | |||
265 | // if($force and $currentLocale === false) { |
||
266 | // abort(500, sprintf('Failed to set %s locale: The locale does not exist on your system, the category name is invalid or the locale functionality is not implemented on your platform.', $locale)); |
||
267 | // } |
||
268 | |||
269 | // return $this; |
||
270 | // } |
||
271 | // } |
||
272 |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.