1
|
|
|
<?php namespace Rocket\Translation\Model; |
2
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
4
|
|
|
use Eloquent; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @property integer $id |
8
|
|
|
* @property DateTime $date_creation |
9
|
|
|
* @property string $context |
10
|
|
|
* @property string $string |
11
|
|
|
*/ |
12
|
|
|
class StringModel extends Eloquent |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var bool Indicates if the model should be timestamped. |
16
|
|
|
*/ |
17
|
|
|
public $timestamps = false; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string The table associated with the model. |
21
|
|
|
*/ |
22
|
|
|
protected $table = 'strings'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The attributes that should be mutated to dates. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $dates = ['date_creation']; |
30
|
|
|
|
31
|
|
|
public static function getStringId($context, $keyString) { |
32
|
|
|
return StringModel::where('string', $keyString)->where('context', $context)->value('id'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public static function getOrCreateTranslation($context, $keyString, $languageId, $defaultLanguageId) { |
36
|
|
|
$stringId = static::getStringId($context, $keyString); |
37
|
|
|
if ($stringId) { |
38
|
|
|
return Translation::getTranslation($stringId, $languageId); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
static::createString($context, $keyString, $defaultLanguageId); |
42
|
|
|
|
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected static function createString($context, $text, $defaultLanguageId) |
47
|
|
|
{ |
48
|
|
|
$string = new StringModel(); |
49
|
|
|
$string->date_creation = Carbon::now(); |
50
|
|
|
$string->context = $context; |
51
|
|
|
$string->string = $text; |
52
|
|
|
$string->save(); |
53
|
|
|
|
54
|
|
|
// Insert the app's default language id as default translation; |
55
|
|
|
return Translation::setTranslation($string->id, $defaultLanguageId, $text); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|