|
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
|
36 |
|
public $table = 'squanto_lines'; |
|
20
|
|
|
public $translatedAttributes = ['value']; |
|
21
|
36 |
|
|
|
22
|
|
|
/** |
|
23
|
36 |
|
* @param $key |
|
24
|
36 |
|
* @return Line |
|
25
|
36 |
|
*/ |
|
26
|
36 |
|
public static function make($key) |
|
27
|
|
|
{ |
|
28
|
36 |
|
$linekey = new LineKey($key); |
|
29
|
|
|
|
|
30
|
36 |
|
$line = new self; |
|
31
|
|
|
$line->key = $linekey->get(); |
|
|
|
|
|
|
32
|
|
|
$line->label = $linekey->getAsLabel(); |
|
|
|
|
|
|
33
|
|
|
$line->page_id = Page::findOrCreateByKey($linekey->getPageKey())->id; |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
$line->save(); |
|
36
|
|
|
|
|
37
|
12 |
|
return $line; |
|
38
|
|
|
} |
|
39
|
12 |
|
|
|
40
|
3 |
|
/** |
|
41
|
|
|
* @param $key |
|
42
|
|
|
* @return Line |
|
43
|
12 |
|
*/ |
|
44
|
|
|
public static function findOrCreateByKey($key) |
|
45
|
|
|
{ |
|
46
|
|
|
if ($line = self::findByKey($key)) { |
|
47
|
|
|
return $line; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return self::make($key); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
30 |
|
/** |
|
54
|
|
|
* Save a translated value |
|
55
|
30 |
|
* |
|
56
|
|
|
* @param $locale |
|
57
|
30 |
|
* @param $value |
|
58
|
|
|
* @return $this |
|
59
|
|
|
*/ |
|
60
|
|
|
public function saveValue($locale, $value) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->saveTranslation($locale, 'value', $value); |
|
63
|
|
|
|
|
64
|
|
|
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
|
24 |
|
} |
|
79
|
|
|
|
|
80
|
24 |
|
/** |
|
81
|
|
|
* @param null $locale |
|
82
|
|
|
* @param bool $fallback |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getValue($locale = null, $fallback = true) |
|
86
|
|
|
{ |
|
87
|
|
|
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
|
39 |
|
return $line->getValue($locale, false); |
|
97
|
|
|
} |
|
98
|
39 |
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param $key |
|
101
|
12 |
|
* @return mixed |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function findByKey($key) |
|
104
|
12 |
|
{ |
|
105
|
|
|
return self::where('key', $key)->first(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
12 |
|
public function saveSuggestedType() |
|
109
|
12 |
|
{ |
|
110
|
12 |
|
// Based on first value found we will suggest a type |
|
111
|
|
|
if (!$value = $this->getValue()) { |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$this->type = (new LineType($value))->suggest(); |
|
|
|
|
|
|
116
|
|
|
$this->save(); |
|
117
|
|
|
} |
|
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
|
|
|
public static function getValuesByLocale($locale) |
|
146
|
|
|
{ |
|
147
|
|
|
// Since the dimsav translatable model trait injects its behaviour and overwrites our results |
|
148
|
|
|
// with the current locale, we will need to fetch results straight from the db instead. |
|
149
|
|
|
$lines = DB::table('squanto_lines')->join('squanto_line_translations', 'squanto_lines.id', '=', 'squanto_line_translations.line_id') |
|
150
|
|
|
->select(['squanto_lines.*','squanto_line_translations.locale','squanto_line_translations.value']) |
|
151
|
|
|
->where('squanto_line_translations.locale', $locale) |
|
152
|
|
|
->get(); |
|
153
|
|
|
|
|
154
|
|
|
// Assert we have a collection |
|
155
|
|
|
$lines = $lines instanceof Collection ? $lines : collect($lines); |
|
156
|
|
|
|
|
157
|
|
|
return $lines->pluck('value', 'key')->toArray(); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
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@propertyannotation 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.