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\Collection; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Line |
12
|
|
|
* |
13
|
|
|
* @package Thinktomorrow\Squanto\Domain |
14
|
|
|
*/ |
15
|
|
|
class Line extends Model |
16
|
|
|
{ |
17
|
|
|
use BaseTranslatable, Translatable; |
18
|
|
|
|
19
|
24 |
|
public $table = 'squanto_lines'; |
20
|
|
|
public $translatedAttributes = ['value']; |
21
|
24 |
|
|
22
|
|
|
/** |
23
|
24 |
|
* @param $key |
24
|
24 |
|
* @return Line |
25
|
24 |
|
*/ |
26
|
41 |
|
public static function make($key) |
27
|
|
|
{ |
28
|
41 |
|
$linekey = new LineKey($key); |
29
|
|
|
|
30
|
41 |
|
$line = new self; |
31
|
17 |
|
$line->key = $linekey->get(); |
|
|
|
|
32
|
17 |
|
$line->label = $linekey->getAsLabel(); |
|
|
|
|
33
|
17 |
|
$line->page_id = Page::findOrCreateByKey($linekey->getPageKey())->id; |
|
|
|
|
34
|
|
|
|
35
|
17 |
|
$line->save(); |
36
|
|
|
|
37
|
25 |
|
return $line; |
38
|
|
|
} |
39
|
8 |
|
|
40
|
2 |
|
/** |
41
|
|
|
* @param $key |
42
|
|
|
* @return Line |
43
|
8 |
|
*/ |
44
|
4 |
|
public static function findOrCreateByKey($key) |
45
|
|
|
{ |
46
|
4 |
|
if ($line = self::findByKey($key)) { |
47
|
1 |
|
return $line; |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
return self::make($key); |
51
|
|
|
} |
52
|
|
|
|
53
|
20 |
|
/** |
54
|
|
|
* Save a translated value |
55
|
20 |
|
* |
56
|
|
|
* @param $locale |
57
|
20 |
|
* @param $value |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
15 |
|
public function saveValue($locale, $value) |
61
|
|
|
{ |
62
|
15 |
|
$this->saveTranslation($locale, 'value', $value); |
63
|
|
|
|
64
|
15 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Save a translated value |
69
|
|
|
* |
70
|
|
|
* @param $locale |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function removeValue($locale) |
74
|
|
|
{ |
75
|
|
|
$this->removeTranslation($locale); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
16 |
|
} |
79
|
|
|
|
80
|
16 |
|
/** |
81
|
|
|
* @param null $locale |
82
|
|
|
* @param bool $fallback |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
8 |
|
public function getValue($locale = null, $fallback = true) |
86
|
|
|
{ |
87
|
8 |
|
return $this->getTranslationFor('value', $locale, $fallback); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function findValue($key, $locale) |
91
|
|
|
{ |
92
|
|
|
if (!$line = self::findByKey($key)) { |
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
26 |
|
return $line->getValue($locale, false); |
97
|
|
|
} |
98
|
26 |
|
|
99
|
|
|
/** |
100
|
|
|
* @param $key |
101
|
8 |
|
* @return mixed |
102
|
|
|
*/ |
103
|
14 |
|
public static function findByKey($key) |
104
|
8 |
|
{ |
105
|
14 |
|
return self::where('key', $key)->first(); |
106
|
|
|
} |
107
|
|
|
|
108
|
12 |
|
public function saveSuggestedType() |
109
|
8 |
|
{ |
110
|
8 |
|
// Based on first value found we will suggest a type |
111
|
4 |
|
if (!$value = $this->getValue()) { |
112
|
|
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
4 |
|
$this->type = (new LineType($value))->suggest(); |
|
|
|
|
116
|
4 |
|
$this->save(); |
117
|
4 |
|
} |
118
|
|
|
|
119
|
|
|
public function editInEditor() |
120
|
|
|
{ |
121
|
|
|
return $this->type == LineType::EDITOR; |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function editInTextarea() |
125
|
|
|
{ |
126
|
|
|
return $this->type == LineType::TEXTAREA; |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function editInTextinput() |
130
|
|
|
{ |
131
|
|
|
return (!$this->type || $this->type == LineType::TEXT); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function areParagraphsAllowed() |
135
|
|
|
{ |
136
|
|
|
return (false !== strpos($this->allowed_html,'<p>')); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get key - value pairs for all lines per locale |
141
|
|
|
* |
142
|
|
|
* @param $locale |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
4 |
|
public static function getValuesByLocale($locale) |
146
|
|
|
{ |
147
|
4 |
|
return self::getValuesByLocaleAndPage($locale); |
148
|
|
|
} |
149
|
|
|
|
150
|
5 |
|
public static function getValuesByLocaleAndPage($locale, $pagekey = null) |
151
|
|
|
{ |
152
|
5 |
|
$locale = $locale?: app()->getLocale(); |
153
|
|
|
|
154
|
|
|
// Since the dimsav translatable model trait injects its behaviour and overwrites our results |
155
|
|
|
// with the current locale, we will need to fetch results straight from the db instead. |
156
|
5 |
|
$lines = DB::table('squanto_lines') |
157
|
5 |
|
->join('squanto_line_translations', 'squanto_lines.id', '=', 'squanto_line_translations.line_id') |
158
|
5 |
|
->select(['squanto_lines.*','squanto_line_translations.locale','squanto_line_translations.value']) |
159
|
5 |
|
->where('squanto_line_translations.locale', $locale); |
160
|
|
|
|
161
|
5 |
|
if($pagekey) |
162
|
|
|
{ |
163
|
|
|
$lines = $lines |
164
|
1 |
|
->join('squanto_pages', 'squanto_lines.page_id', '=', 'squanto_pages.id') |
165
|
1 |
|
->where('squanto_pages.key', $pagekey); |
166
|
|
|
} |
167
|
|
|
|
168
|
5 |
|
$lines = $lines->get(); |
169
|
|
|
|
170
|
|
|
// Assert we have a collection |
171
|
5 |
|
$lines = $lines instanceof Collection ? $lines : collect($lines); |
172
|
|
|
|
173
|
5 |
|
return $lines->pluck('value', 'key')->toArray(); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
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.