1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Squanto\Domain; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Dimsav\Translatable\Translatable as BaseTranslatable; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Line |
11
|
|
|
* |
12
|
|
|
* @package Thinktomorrow\Squanto\Domain |
13
|
|
|
*/ |
14
|
|
|
class Line extends Model |
15
|
|
|
{ |
16
|
|
|
use BaseTranslatable, Translatable; |
17
|
|
|
|
18
|
|
|
public $table = 'squanto_lines'; |
19
|
36 |
|
public $translatedAttributes = ['value']; |
20
|
|
|
|
21
|
36 |
|
/** |
22
|
|
|
* @param $key |
23
|
36 |
|
* @return Line |
24
|
36 |
|
*/ |
25
|
36 |
|
public static function make($key) |
26
|
36 |
|
{ |
27
|
|
|
$linekey = new LineKey($key); |
28
|
36 |
|
|
29
|
|
|
$line = new self; |
30
|
36 |
|
$line->key = $linekey->get(); |
|
|
|
|
31
|
|
|
$line->label = $linekey->getAsLabel(); |
|
|
|
|
32
|
|
|
$line->page_id = Page::findOrCreateByKey($linekey->getPageKey())->id; |
|
|
|
|
33
|
|
|
|
34
|
|
|
$line->save(); |
35
|
|
|
|
36
|
|
|
return $line; |
37
|
12 |
|
} |
38
|
|
|
|
39
|
12 |
|
/** |
40
|
3 |
|
* @param $key |
41
|
|
|
* @return Line |
42
|
|
|
*/ |
43
|
12 |
|
public static function findOrCreateByKey($key) |
44
|
|
|
{ |
45
|
|
|
if ($line = self::findByKey($key)) { |
46
|
|
|
return $line; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return self::make($key); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
30 |
|
* Save a translated value |
54
|
|
|
* |
55
|
30 |
|
* @param $locale |
56
|
|
|
* @param $value |
57
|
30 |
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function saveValue($locale, $value) |
60
|
|
|
{ |
61
|
|
|
$this->saveTranslation($locale, 'value', $value); |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Save a translated value |
68
|
|
|
* |
69
|
|
|
* @param $locale |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function removeValue($locale) |
73
|
|
|
{ |
74
|
|
|
$this->removeTranslation($locale); |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
24 |
|
|
79
|
|
|
/** |
80
|
24 |
|
* @param null $locale |
81
|
|
|
* @param bool $fallback |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getValue($locale = null, $fallback = true) |
85
|
|
|
{ |
86
|
|
|
return $this->getTranslationFor('value', $locale, $fallback); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public static function findValue($key, $locale) |
90
|
|
|
{ |
91
|
|
|
if (!$line = self::findByKey($key)) { |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $line->getValue($locale, false); |
96
|
39 |
|
} |
97
|
|
|
|
98
|
39 |
|
/** |
99
|
|
|
* @param $key |
100
|
|
|
* @return mixed |
101
|
12 |
|
*/ |
102
|
|
|
public static function findByKey($key) |
103
|
|
|
{ |
104
|
12 |
|
return self::where('key', $key)->first(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function saveSuggestedType() |
108
|
12 |
|
{ |
109
|
12 |
|
// Based on first value found we will suggest a type |
110
|
12 |
|
if (!$value = $this->getValue()) { |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->type = (new LineType($value))->suggest(); |
|
|
|
|
115
|
|
|
$this->save(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function editInEditor() |
119
|
|
|
{ |
120
|
|
|
return $this->type == LineType::EDITOR; |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function editInTextarea() |
124
|
|
|
{ |
125
|
|
|
return $this->type == LineType::TEXTAREA; |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function editInTextinput() |
129
|
|
|
{ |
130
|
|
|
return (!$this->type || $this->type == LineType::TEXT); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get key - value pairs for all lines per locale |
135
|
|
|
* |
136
|
|
|
* @param $locale |
137
|
|
|
* @return mixed |
138
|
|
|
*/ |
139
|
|
|
public static function getValuesByLocale($locale) |
140
|
|
|
{ |
141
|
|
|
// Since the dimsav translatable model trait injects its behaviour and overwrites our results |
142
|
|
|
// with the current locale, we will need to fetch results straight from the db instead. |
143
|
|
|
$lines = DB::table('squanto_lines')->join('squanto_line_translations','squanto_lines.id','=','squanto_line_translations.line_id') |
144
|
|
|
->select(['squanto_lines.*','squanto_line_translations.locale','squanto_line_translations.value']) |
145
|
|
|
->where('squanto_line_translations.locale',$locale) |
146
|
|
|
->get(); |
147
|
|
|
|
148
|
|
|
return $lines->pluck('value', 'key')->toArray(); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.