1 | <?php |
||
2 | |||
3 | namespace Backend\Modules\Location\Actions; |
||
4 | |||
5 | use Backend\Core\Engine\Authentication as BackendAuthentication; |
||
6 | use Backend\Core\Engine\Base\ActionEdit as BackendBaseActionEdit; |
||
7 | use Backend\Core\Engine\Form as BackendForm; |
||
8 | use Backend\Core\Language\Language as BL; |
||
9 | use Backend\Core\Engine\Model as BackendModel; |
||
10 | use Backend\Form\Type\DeleteType; |
||
11 | use Backend\Modules\Location\Engine\Model as BackendLocationModel; |
||
12 | use ForkCMS\Utility\Geolocation; |
||
13 | use Symfony\Component\Intl\Intl; |
||
14 | use Frontend\Modules\Location\Engine\Model as FrontendLocationModel; |
||
15 | |||
16 | /** |
||
17 | * This is the edit-action, it will display a form to create a new item |
||
18 | */ |
||
19 | class Edit extends BackendBaseActionEdit |
||
20 | { |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $settings = []; |
||
25 | |||
26 | /** |
||
27 | * The settings form |
||
28 | * |
||
29 | * @var BackendForm |
||
30 | */ |
||
31 | protected $settingsForm; |
||
32 | |||
33 | public function execute(): void |
||
34 | { |
||
35 | $this->id = $this->getRequest()->query->getInt('id'); |
||
36 | |||
37 | // does the item exists |
||
38 | if ($this->id !== 0 && BackendLocationModel::exists($this->id)) { |
||
39 | $this->header->addJS(FrontendLocationModel::getPathToMapStyles()); |
||
40 | parent::execute(); |
||
41 | |||
42 | // define Google Maps API key |
||
43 | $apikey = $this->get('fork.settings')->get('Core', 'google_maps_key'); |
||
44 | |||
45 | // check Google Maps API key, otherwise redirect to settings |
||
46 | if ($apikey === null) { |
||
47 | $this->redirect(BackendModel::createUrlForAction('Index', 'Settings')); |
||
48 | } |
||
49 | |||
50 | $this->header->addJS( |
||
51 | 'https://maps.googleapis.com/maps/api/js?key=' . $apikey . '&language=' . BL::getInterfaceLanguage() |
||
52 | ); |
||
53 | |||
54 | $this->loadData(); |
||
55 | |||
56 | $this->loadForm(); |
||
57 | $this->validateForm(); |
||
58 | $this->loadDeleteForm(); |
||
59 | |||
60 | $this->loadSettingsForm(); |
||
61 | |||
62 | $this->parse(); |
||
63 | $this->display(); |
||
64 | } else { |
||
65 | $this->redirect(BackendModel::createUrlForAction('Index') . '&error=non-existing'); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | private function loadData(): void |
||
70 | { |
||
71 | $this->record = (array) BackendLocationModel::get($this->id); |
||
72 | |||
73 | // no item found, throw an exceptions, because somebody is fucking with our URL |
||
74 | if (empty($this->record)) { |
||
75 | $this->redirect(BackendModel::createUrlForAction('Index') . '&error=non-existing'); |
||
76 | } |
||
77 | |||
78 | $this->settings = BackendLocationModel::getMapSettings($this->id); |
||
79 | |||
80 | // load the settings from the general settings |
||
81 | if (empty($this->settings)) { |
||
82 | $settings = $this->get('fork.settings')->getForModule('Location'); |
||
83 | |||
84 | $this->settings['width'] = $settings['width_widget']; |
||
85 | $this->settings['height'] = $settings['height_widget']; |
||
86 | $this->settings['map_type'] = $settings['map_type_widget']; |
||
87 | $this->settings['map_style'] = isset($settings['map_style_widget']) ? $settings['map_style_widget'] : 'standard'; |
||
88 | $this->settings['zoom_level'] = $settings['zoom_level_widget']; |
||
89 | $this->settings['center']['lat'] = $this->record['lat']; |
||
90 | $this->settings['center']['lng'] = $this->record['lng']; |
||
91 | } |
||
92 | |||
93 | // no center point given yet, use the first occurrence |
||
94 | if (!isset($this->settings['center'])) { |
||
95 | $this->settings['center']['lat'] = $this->record['lat']; |
||
96 | $this->settings['center']['lng'] = $this->record['lng']; |
||
97 | } |
||
98 | |||
99 | $this->settings['full_url'] = (isset($this->settings['full_url'])) ? ($this->settings['full_url']) : false; |
||
100 | $this->settings['directions'] = (isset($this->settings['directions'])) ? ($this->settings['directions']) : false; |
||
101 | } |
||
102 | |||
103 | private function loadForm(): void |
||
104 | { |
||
105 | $this->form = new BackendForm('edit'); |
||
106 | $this->form->addText('title', $this->record['title'], null, 'form-control title', 'form-control danger title')->makeRequired(); |
||
107 | $this->form->addText('street', $this->record['street'])->makeRequired(); |
||
108 | $this->form->addText('number', $this->record['number'])->makeRequired(); |
||
109 | $this->form->addText('zip', $this->record['zip'])->makeRequired(); |
||
110 | $this->form->addText('city', $this->record['city'])->makeRequired(); |
||
111 | $this->form->addDropdown('country', Intl::getRegionBundle()->getCountryNames(BL::getInterfaceLanguage()), $this->record['country'])->makeRequired(); |
||
0 ignored issues
–
show
|
|||
112 | $this->form->addHidden('redirect', 'overview'); |
||
113 | } |
||
114 | |||
115 | protected function loadSettingsForm(): void |
||
116 | { |
||
117 | $mapTypes = [ |
||
118 | 'ROADMAP' => BL::lbl('Roadmap', $this->getModule()), |
||
119 | 'SATELLITE' => BL::lbl('Satellite', $this->getModule()), |
||
120 | 'HYBRID' => BL::lbl('Hybrid', $this->getModule()), |
||
121 | 'TERRAIN' => BL::lbl('Terrain', $this->getModule()), |
||
122 | 'STREET_VIEW' => BL::lbl('StreetView', $this->getModule()), |
||
123 | ]; |
||
124 | $mapStyles = [ |
||
125 | 'standard' => BL::lbl('Default', $this->getModule()), |
||
126 | 'custom' => BL::lbl('Custom', $this->getModule()), |
||
127 | 'gray' => BL::lbl('Gray', $this->getModule()), |
||
128 | 'blue' => BL::lbl('Blue', $this->getModule()), |
||
129 | ]; |
||
130 | |||
131 | $zoomLevels = array_combine( |
||
132 | array_merge(['auto'], range(1, 18)), |
||
133 | array_merge([BL::lbl('Auto', $this->getModule())], range(1, 18)) |
||
134 | ); |
||
135 | |||
136 | $this->settingsForm = new BackendForm('settings'); |
||
137 | |||
138 | // add map info (overview map) |
||
139 | $this->settingsForm->addHidden('map_id', $this->id); |
||
140 | $this->settingsForm->addDropdown('zoom_level', $zoomLevels, $this->settings['zoom_level']); |
||
141 | $this->settingsForm->addText('width', $this->settings['width']); |
||
142 | $this->settingsForm->addText('height', $this->settings['height']); |
||
143 | $this->settingsForm->addDropdown('map_type', $mapTypes, $this->settings['map_type']); |
||
144 | $this->settingsForm->addDropdown( |
||
145 | 'map_style', |
||
146 | $mapStyles, |
||
147 | $this->settings['map_style'] ?? null |
||
148 | ); |
||
149 | $this->settingsForm->addCheckbox('full_url', $this->settings['full_url']); |
||
150 | $this->settingsForm->addCheckbox('directions', $this->settings['directions']); |
||
151 | $this->settingsForm->addCheckbox('marker_overview', $this->record['show_overview']); |
||
152 | } |
||
153 | |||
154 | protected function parse(): void |
||
155 | { |
||
156 | parent::parse(); |
||
157 | |||
158 | // assign to template |
||
159 | $this->template->assign('item', $this->record); |
||
160 | $this->template->assign('settings', $this->settings); |
||
161 | $this->template->assign('godUser', BackendAuthentication::getUser()->isGod()); |
||
162 | |||
163 | $this->settingsForm->parse($this->template); |
||
164 | |||
165 | // assign message if address was not be geocoded |
||
166 | if ($this->record['lat'] == null || $this->record['lng'] == null) { |
||
167 | $this->template->assign('errorMessage', BL::err('AddressCouldNotBeGeocoded')); |
||
168 | } |
||
169 | |||
170 | $this->header->appendDetailToBreadcrumbs($this->record['title']); |
||
171 | } |
||
172 | |||
173 | private function validateForm(): void |
||
174 | { |
||
175 | if ($this->form->isSubmitted()) { |
||
176 | $this->form->cleanupFields(); |
||
177 | |||
178 | // validate fields |
||
179 | $this->form->getField('title')->isFilled(BL::err('TitleIsRequired')); |
||
180 | $this->form->getField('street')->isFilled(BL::err('FieldIsRequired')); |
||
181 | $this->form->getField('number')->isFilled(BL::err('FieldIsRequired')); |
||
182 | $this->form->getField('zip')->isFilled(BL::err('FieldIsRequired')); |
||
183 | $this->form->getField('city')->isFilled(BL::err('FieldIsRequired')); |
||
184 | |||
185 | if ($this->form->isCorrect()) { |
||
186 | // build item |
||
187 | $item = []; |
||
188 | $item['id'] = $this->id; |
||
189 | $item['language'] = BL::getWorkingLanguage(); |
||
190 | $item['extra_id'] = $this->record['extra_id']; |
||
191 | $item['title'] = $this->form->getField('title')->getValue(); |
||
192 | $item['street'] = $this->form->getField('street')->getValue(); |
||
193 | $item['number'] = $this->form->getField('number')->getValue(); |
||
194 | $item['zip'] = $this->form->getField('zip')->getValue(); |
||
195 | $item['city'] = $this->form->getField('city')->getValue(); |
||
196 | $item['country'] = $this->form->getField('country')->getValue(); |
||
197 | |||
198 | // check if it's necessary to geocode again |
||
199 | if ($this->record['lat'] === null || $this->record['lng'] === null || $item['street'] != $this->record['street'] || $item['number'] != $this->record['number'] || $item['zip'] != $this->record['zip'] || $item['city'] != $this->record['city'] || $item['country'] != $this->record['country']) { |
||
200 | // define coordinates |
||
201 | $coordinates = BackendModel::get(Geolocation::class)->getCoordinates( |
||
202 | $item['street'], |
||
203 | $item['number'], |
||
204 | $item['city'], |
||
205 | $item['zip'], |
||
206 | $item['country'] |
||
207 | ); |
||
208 | |||
209 | // define latitude and longitude |
||
210 | $item['lat'] = $coordinates['latitude']; |
||
211 | $item['lng'] = $coordinates['longitude']; |
||
212 | } else { |
||
213 | $item['lat'] = $this->record['lat']; |
||
214 | $item['lng'] = $this->record['lng']; |
||
215 | } |
||
216 | |||
217 | // insert the item |
||
218 | BackendLocationModel::update($item); |
||
219 | |||
220 | // redirect to the overview |
||
221 | if ($this->form->getField('redirect')->getValue() == 'overview') { |
||
222 | $this->redirect(BackendModel::createUrlForAction('Index') . '&report=edited&var=' . rawurlencode($item['title']) . '&highlight=row-' . $item['id']); |
||
223 | } else { |
||
224 | $this->redirect(BackendModel::createUrlForAction('Edit') . '&id=' . $item['id'] . '&report=edited'); |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | |||
230 | private function loadDeleteForm(): void |
||
231 | { |
||
232 | $deleteForm = $this->createForm( |
||
233 | DeleteType::class, |
||
234 | ['id' => $this->record['id']], |
||
235 | ['module' => $this->getModule()] |
||
236 | ); |
||
237 | $this->template->assign('deleteForm', $deleteForm->createView()); |
||
238 | } |
||
239 | } |
||
240 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.