1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Larrock\Core\Plugins; |
4
|
|
|
|
5
|
|
|
use Larrock\ComponentFeed\Models\Feed; |
6
|
|
|
use Larrock\Core\Models\Link; |
7
|
|
|
use LarrockAdminSeo; |
8
|
|
|
use LarrockFeed; |
9
|
|
|
use Larrock\Core\Helpers\MessageLarrock; |
10
|
|
|
use Larrock\Core\Models\Seo; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Аттач/детач данных плагинов и связанных полей |
14
|
|
|
* Class ComponentPlugin |
15
|
|
|
* @package Larrock\Core\Plugins |
16
|
|
|
*/ |
17
|
|
|
class ComponentPlugin |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Сохранение связей полей и данных плагинов |
22
|
|
|
* @param $event |
23
|
|
|
* @throws \Exception |
24
|
|
|
*/ |
25
|
|
|
public function attach($event) |
26
|
|
|
{ |
27
|
|
|
$this->attachRows($event); |
28
|
|
|
$this->attachSeo($event); |
29
|
|
|
$this->attachAnonsModule($event); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Сохранение данных сео-плагина |
34
|
|
|
* @param $event |
35
|
|
|
* @throws \Exception |
36
|
|
|
* @return \Larrock\Core\Models\Seo|Seo|null |
37
|
|
|
*/ |
38
|
|
|
protected function attachSeo($event) |
39
|
|
|
{ |
40
|
|
|
/** @var Seo $seo */ |
41
|
|
|
$seo = LarrockAdminSeo::getModel()->whereSeoIdConnect($event->model->id)->whereSeoTypeConnect($event->component->name)->first(); |
42
|
|
|
|
43
|
|
|
if($seo){ |
44
|
|
|
if( !empty($event->request->get('seo_title')) || |
45
|
|
|
!empty($event->request->get('seo_description')) || |
46
|
|
|
!empty($event->request->get('seo_seo_keywords'))){ |
47
|
|
|
$seo->seo_id_connect = $event->model->id; |
48
|
|
|
$seo->seo_url_connect = $event->request->get('url_connect'); |
49
|
|
|
$seo->seo_title = $event->request->get('seo_title'); |
50
|
|
|
$seo->seo_description = $event->request->get('seo_description'); |
51
|
|
|
$seo->seo_keywords = $event->request->get('seo_keywords'); |
52
|
|
|
$seo->seo_type_connect = $event->component->name; |
53
|
|
|
if($seo->save()){ |
54
|
|
|
MessageLarrock::success('SEO обновлено'); |
55
|
|
|
return $seo; |
56
|
|
|
} |
57
|
|
|
}else{ |
58
|
|
|
$seo->delete(); |
59
|
|
|
MessageLarrock::success('SEO удалено'); |
60
|
|
|
return $seo; |
61
|
|
|
} |
62
|
|
|
}else{ |
63
|
|
|
if( !empty($event->request->get('seo_title')) || |
64
|
|
|
!empty($event->request->get('seo_description')) || |
65
|
|
|
!empty($event->request->get('seo_seo_keywords'))){ |
66
|
|
|
$seo = LarrockAdminSeo::getModel(); |
67
|
|
|
$seo->seo_id_connect = $event->request->get('id_connect'); |
68
|
|
|
$seo->seo_url_connect = $event->request->get('url_connect'); |
69
|
|
|
$seo->seo_title = $event->request->get('seo_title'); |
70
|
|
|
$seo->seo_description = $event->request->get('seo_description'); |
71
|
|
|
$seo->seo_keywords = $event->request->get('seo_keywords'); |
72
|
|
|
$seo->seo_type_connect = $event->request->get('type_connect'); |
73
|
|
|
if($seo->save()){ |
74
|
|
|
MessageLarrock::success('SEO добавлено'); |
75
|
|
|
return $seo; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
return null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Создание анонса материала через модуль anonsCategory |
84
|
|
|
* @param $event |
85
|
|
|
* @return bool|Feed|null |
86
|
|
|
* @throws \Exception |
87
|
|
|
*/ |
88
|
|
|
protected function attachAnonsModule($event) |
89
|
|
|
{ |
90
|
|
|
if( !$event->request->has('_jsvalidation') && ($event->request->has('anons_merge') || !empty($event->request->get('anons_description')))){ |
91
|
|
|
if( !config('larrock.feed.anonsCategory')){ |
92
|
|
|
MessageLarrock::danger('larrock.feed.anonsCategory не задан. Анонс создан не будет'); |
93
|
|
|
return TRUE; |
94
|
|
|
} |
95
|
|
|
/** @var Feed $anons */ |
96
|
|
|
$anons = LarrockFeed::getModel(); |
97
|
|
|
$anons->title = $event->request->get('title'); |
98
|
|
|
$anons->url = 'anons_'. $event->model->id .''. random_int(1,9999); |
99
|
|
|
$anons->category = LarrockFeed::getConfig()->settings['anons_category']; |
100
|
|
|
$anons->user_id = \Auth::id(); |
101
|
|
|
$anons->active = 1; |
102
|
|
|
$anons->position = LarrockFeed::getModel()->whereCategory(LarrockFeed::getConfig()->settings['anons_category'])->max('position') +1; |
103
|
|
|
|
104
|
|
|
if($event->request->has('anons_merge')){ |
105
|
|
|
$original = LarrockFeed::getModel()->whereId($event->request->get('id_connect'))->first(); |
106
|
|
|
$anons->short = '<a href="'. $original->full_url .'">'. $event->request->get('title') .'</a>'; |
107
|
|
|
}else{ |
108
|
|
|
$anons->short = $event->request->get('anons_description'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if($anons->save()){ |
112
|
|
|
MessageLarrock::success('Анонс добавлен'); |
113
|
|
|
return $anons; |
114
|
|
|
} |
115
|
|
|
MessageLarrock::danger('Анонс не добавлен'); |
116
|
|
|
} |
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Сохранение связей полей |
122
|
|
|
* @param $event |
123
|
|
|
*/ |
124
|
|
|
protected function attachRows($event) |
125
|
|
|
{ |
126
|
|
|
if(\is_array($event->component->rows)) { |
127
|
|
|
foreach ($event->component->rows as $row) { |
128
|
|
|
if (isset($row->modelParent, $row->modelChild)) { |
129
|
|
|
$add_params = ['model_parent' => $row->modelParent, 'model_child' => $row->modelChild]; |
130
|
|
|
|
131
|
|
|
if($event->request->has($row->name)){ |
132
|
|
|
if(\is_array($event->request->get($row->name))){ |
133
|
|
|
foreach ($event->request->get($row->name) as $value){ |
134
|
|
|
$params[$value] = $add_params; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
if(isset($params)){ |
137
|
|
|
$event->model->getLink($row->modelChild)->sync($params); |
138
|
|
|
} |
139
|
|
|
}else{ |
140
|
|
|
$params[$event->request->get($row->name)] = $add_params; |
|
|
|
|
141
|
|
|
$event->model->getLink($row->modelChild)->sync($params); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Удаление всех связей материала компонента |
151
|
|
|
* @param $event |
152
|
|
|
*/ |
153
|
|
|
public function detach($event) |
154
|
|
|
{ |
155
|
|
|
$this->detachRows($event); |
156
|
|
|
$this->detachPlugins($event); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Удаление связей полей |
161
|
|
|
* @param $event |
162
|
|
|
*/ |
163
|
|
|
protected function detachRows($event) |
164
|
|
|
{ |
165
|
|
|
if(\is_array($event->component->rows)){ |
166
|
|
|
foreach ($event->component->rows as $row){ |
167
|
|
|
if(isset($row->modelParent, $row->modelChild)){ |
168
|
|
|
//Получаем id прилинкованных данных |
169
|
|
|
$getLink = $event->model->getLink($row->modelChild)->get(); |
170
|
|
|
|
171
|
|
|
//Удаляем связи |
172
|
|
|
$event->model->linkQuery($row->modelChild)->delete(); |
173
|
|
|
|
174
|
|
|
//Удаляем поля в modelChild если у поля указана опция setDeleteIfNoLink и больше связей к этому материалу нет |
175
|
|
|
if($row->deleteIfNoLink){ |
176
|
|
|
//Проверяем остались ли связи до modelChild от modelParent |
177
|
|
|
foreach ($getLink as $link){ |
178
|
|
|
$linkModel = new Link(); |
179
|
|
|
if( !$linkModel::whereIdChild($link->id)->whereModelChild($row->modelChild)->first()){ |
|
|
|
|
180
|
|
|
$modelChild = new $row->modelChild(); |
181
|
|
|
$modelChild->whereId($link->id)->delete(); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Удаление данных плагинов |
192
|
|
|
* @param $event |
193
|
|
|
*/ |
194
|
|
|
protected function detachPlugins($event) |
195
|
|
|
{ |
196
|
|
|
if(\is_array($event->component->plugins_backend)){ |
197
|
|
|
foreach ($event->component->plugins_backend as $key_plugin => $value_plugin){ |
198
|
|
|
//Detach SEO plugin |
199
|
|
|
if($key_plugin === 'seo' && $seo = LarrockAdminSeo::getModel()->whereSeoIdConnect($event->model->id) |
200
|
|
|
->whereSeoTypeConnect($event->component->name)->first()){ |
201
|
|
|
$seo->delete(); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.