| Total Complexity | 107 |
| Total Lines | 1023 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 3 | Features | 0 |
Complex classes like EntriesController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EntriesController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class EntriesController |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Get Entry ID |
||
| 19 | * |
||
| 20 | * @param array Query |
||
| 21 | */ |
||
| 22 | protected function getEntryID($query) |
||
| 23 | { |
||
| 24 | if (isset($query['id'])) { |
||
| 25 | $_id = $query['id']; |
||
| 26 | } else { |
||
| 27 | $_id = ''; |
||
| 28 | } |
||
| 29 | |||
| 30 | return $_id; |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Index page |
||
| 35 | * |
||
| 36 | * @param Request $request PSR7 request |
||
| 37 | * @param Response $response PSR7 response |
||
| 38 | * |
||
| 39 | * @return Response |
||
| 40 | */ |
||
| 41 | public function index(Request $request, Response $response) : Response |
||
| 42 | { |
||
| 43 | // Get Query Params |
||
| 44 | $query = $request->getQueryParams(); |
||
| 45 | |||
| 46 | // Set Entries ID in parts |
||
| 47 | if (isset($query['id'])) { |
||
| 48 | $parts = explode("/", $query['id']); |
||
| 49 | } else { |
||
| 50 | $parts = [0 => '']; |
||
| 51 | } |
||
| 52 | |||
| 53 | // Init Fieldsets |
||
| 54 | $fieldsets = []; |
||
| 55 | |||
| 56 | // Get fieldsets files |
||
| 57 | $fieldsets_list = Filesystem::listContents(PATH['project'] . '/fieldsets/'); |
||
| 58 | |||
| 59 | // If there is any fieldset file then go... |
||
| 60 | if (count($fieldsets_list) > 0) { |
||
| 61 | foreach ($fieldsets_list as $fieldset) { |
||
| 62 | if ($fieldset['type'] == 'file' && $fieldset['extension'] == 'yaml') { |
||
| 63 | $fieldset_content = flextype('serializers')->yaml()->decode(Filesystem::read($fieldset['path'])); |
||
| 64 | if (isset($fieldset_content['form']) && |
||
| 65 | isset($fieldset_content['form']['tabs']) && |
||
| 66 | isset($fieldset_content['form']['tabs']['main']['fields']) && |
||
| 67 | isset($fieldset_content['form']['tabs']['main']['fields']['title'])) { |
||
| 68 | if (isset($fieldset_content['hide']) && $fieldset_content['hide'] == true) { |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | $fieldsets[$fieldset['basename']] = $fieldset_content; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | $entry_current = flextype('entries')->fetch($this->getEntryID($query))->toArray(); |
||
| 78 | |||
| 79 | if (isset($entry_current['items_view'])) { |
||
| 80 | $items_view = $entry_current['items_view']; |
||
| 81 | } else { |
||
| 82 | $items_view = flextype('registry')->get('plugins.admin.settings.entries.items_view_default'); |
||
| 83 | } |
||
| 84 | |||
| 85 | $entries_list = []; |
||
| 86 | $entries_collection = []; |
||
| 87 | $entries_collection = arrays(flextype('entries') |
||
| 88 | ->fetch($this->getEntryID($query), |
||
| 89 | ['collection' => true, 'depth' => ['1']])) |
||
| 90 | ->sortBy('published_at', 'DESC') |
||
| 91 | ->toArray(); |
||
| 92 | |||
| 93 | foreach ($entries_collection as $slug => $body) { |
||
| 94 | $entries_list[$slug] = $body; |
||
| 95 | if (filesystem()->find()->in(PATH['project'] . '/entries/' . $slug)->depth('>=1')->depth('<=2')->hasResults()) { |
||
| 96 | $entries_list[$slug]['has_children'] = true; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | return flextype('twig')->render( |
||
| 101 | $response, |
||
| 102 | 'plugins/admin/templates/content/entries/index.html', |
||
| 103 | [ |
||
| 104 | 'entries_list' => $entries_list, |
||
| 105 | 'id_current' => $this->getEntryID($query), |
||
| 106 | 'entry_current' => $entry_current, |
||
| 107 | 'items_view' => $items_view, |
||
| 108 | 'menu_item' => 'entries', |
||
| 109 | 'fieldsets' => $fieldsets, |
||
| 110 | 'parts' => $parts, |
||
| 111 | 'i' => count($parts), |
||
| 112 | 'last' => array_pop($parts), |
||
| 113 | 'links' => [ |
||
| 114 | 'entries' => [ |
||
| 115 | 'link' => flextype('router')->pathFor('admin.entries.index'), |
||
| 116 | 'title' => __('admin_entries'), |
||
| 117 | 'active' => true |
||
| 118 | ] |
||
| 119 | ], |
||
| 120 | 'buttons' => [ |
||
| 121 | 'create' => [ |
||
| 122 | 'link' => 'javascript:;', |
||
| 123 | 'title' => __('admin_create_new_entry'), |
||
| 124 | 'onclick' => 'event.preventDefault(); selectEntryType("'.$this->getEntryID($query).'", 0);' |
||
| 125 | ] |
||
| 126 | ] |
||
| 127 | ] |
||
| 128 | ); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Create new entry page |
||
| 133 | * |
||
| 134 | * @param Request $request PSR7 request |
||
| 135 | * @param Response $response PSR7 response |
||
| 136 | * |
||
| 137 | * @return Response |
||
| 138 | */ |
||
| 139 | public function add(Request $request, Response $response) : Response |
||
| 140 | { |
||
| 141 | // Get Query Params |
||
| 142 | $query = $request->getQueryParams(); |
||
| 143 | |||
| 144 | // Set Entries ID in parts |
||
| 145 | if (isset($query['id'])) { |
||
| 146 | $parts = explode("/", $query['id']); |
||
| 147 | } else { |
||
| 148 | $parts = [0 => '']; |
||
| 149 | } |
||
| 150 | |||
| 151 | $type = isset($query['type']) ? $query['type']: ''; |
||
| 152 | |||
| 153 | return flextype('twig')->render( |
||
| 154 | $response, |
||
| 155 | 'plugins/admin/templates/content/entries/add.html', |
||
| 156 | [ |
||
| 157 | 'entries_list' => flextype('entries')->fetch($this->getEntryID($query), ['collection' => true])->sortBy('order_by', 'ASC')->toArray(), |
||
| 158 | 'menu_item' => 'entries', |
||
| 159 | 'current_id' => $this->getEntryID($query), |
||
| 160 | 'parts' => $parts, |
||
| 161 | 'i' => count($parts), |
||
| 162 | 'last' => array_pop($parts), |
||
| 163 | 'type' => $type, |
||
| 164 | 'links' => [ |
||
| 165 | 'entries' => [ |
||
| 166 | 'link' => flextype('router')->pathFor('admin.entries.index'), |
||
| 167 | 'title' => __('admin_entries'), |
||
| 168 | |||
| 169 | ], |
||
| 170 | 'entries_add' => [ |
||
| 171 | 'link' => flextype('router')->pathFor('admin.entries.add') . '?id=' . $this->getEntryID($query), |
||
| 172 | 'title' => __('admin_create_new_entry'), |
||
| 173 | 'active' => true |
||
| 174 | ] |
||
| 175 | ] |
||
| 176 | ] |
||
| 177 | ); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Select Entry Type - process |
||
| 182 | * |
||
| 183 | * @param Request $request PSR7 request |
||
| 184 | * @param Response $response PSR7 response |
||
| 185 | * |
||
| 186 | * @return Response |
||
| 187 | */ |
||
| 188 | public function selectEntryTypeProcess(Request $request, Response $response) : Response |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Create new entry - process |
||
| 198 | * |
||
| 199 | * @param Request $request PSR7 request |
||
| 200 | * @param Response $response PSR7 response |
||
| 201 | * |
||
| 202 | * @return Response |
||
| 203 | */ |
||
| 204 | public function addProcess(Request $request, Response $response) : Response |
||
| 205 | { |
||
| 206 | // Get data from POST |
||
| 207 | $data = $request->getParsedBody(); |
||
| 208 | |||
| 209 | // Set parent Entry ID |
||
| 210 | if ($data['current_id']) { |
||
| 211 | $parent_entry_id = $data['current_id']; |
||
| 212 | } else { |
||
| 213 | $parent_entry_id = ''; |
||
| 214 | } |
||
| 215 | |||
| 216 | // Set new Entry ID using slugify or without it |
||
| 217 | if (flextype('registry')->get('plugins.admin.settings.entries.slugify') == true) { |
||
| 218 | $id = ltrim($parent_entry_id . '/' . flextype('slugify')->slugify($data['id']), '/'); |
||
| 219 | } else { |
||
| 220 | $id = ltrim($parent_entry_id . '/' . $data['id'], '/'); |
||
| 221 | } |
||
| 222 | |||
| 223 | // Check if entry exists then try to create it |
||
| 224 | if (!flextype('entries')->has($id)) { |
||
| 225 | |||
| 226 | // Check if we have fieldset for this entry |
||
| 227 | if (flextype('fieldsets')->has($data['fieldset'])) { |
||
| 228 | |||
| 229 | // Get fieldset |
||
| 230 | $fieldset = flextype('fieldsets')->fetchSingle($data['fieldset']); |
||
| 231 | |||
| 232 | // Init entry data |
||
| 233 | $data_from_post = []; |
||
| 234 | $data_from_post_override = []; |
||
| 235 | $data_result = []; |
||
| 236 | |||
| 237 | // Define data values based on POST data |
||
| 238 | $data_from_post['created_by'] = flextype('acl')->getUserLoggedInUuid(); |
||
| 239 | $data_from_post['published_by'] = flextype('acl')->getUserLoggedInUuid(); |
||
| 240 | $data_from_post['title'] = $data['title']; |
||
| 241 | $data_from_post['fieldset'] = $data['fieldset']; |
||
| 242 | $data_from_post['visibility'] = $data['visibility']; |
||
| 243 | $data_from_post['published_at'] = date(flextype('registry')->get('flextype.settings.date_format'), time()); |
||
| 244 | $data_from_post['routable'] = isset($data['routable']) ? (bool) $data['routable'] : false; |
||
| 245 | |||
| 246 | // Themes/Templates support for Site Plugin |
||
| 247 | // We need to check if template for current fieldset is exists |
||
| 248 | // if template is not exist then `default` template will be used! |
||
| 249 | if (flextype('registry')->has('plugins.site')) { |
||
| 250 | $template_path = PATH['project'] . '/themes/' . flextype('registry')->get('plugins.site.settings.theme') . '/templates/' . $data['fieldset'] . '.html'; |
||
| 251 | $template = (Filesystem::has($template_path)) ? $data['fieldset'] : 'default'; |
||
| 252 | $data_from_post['template'] = $template; |
||
| 253 | } |
||
| 254 | |||
| 255 | //foreach ($fieldset['sections'] as $section_name => $section_body) { |
||
| 256 | // foreach ($section_body['form']['fields'] as $field => $properties) { |
||
| 257 | |||
| 258 | // Predefine data values based on fieldset default values |
||
| 259 | foreach ($fieldset['form']['tabs'] as $form_tab => $form_tab_body) { |
||
| 260 | foreach ($form_tab_body['fields'] as $field => $properties) { |
||
| 261 | |||
| 262 | // Ingnore fields where property: heading |
||
| 263 | if ($properties['type'] == 'heading') { |
||
| 264 | continue; |
||
| 265 | } |
||
| 266 | |||
| 267 | // Get values from $data_from_post |
||
| 268 | if (isset($data_from_post[$field])) { |
||
| 269 | $value = $data_from_post[$field]; |
||
| 270 | |||
| 271 | // Get values from fieldsets predefined field values |
||
| 272 | } elseif (isset($properties['value'])) { |
||
| 273 | $value = $properties['value']; |
||
| 274 | |||
| 275 | // or set empty value |
||
| 276 | } else { |
||
| 277 | $value = ''; |
||
| 278 | } |
||
| 279 | |||
| 280 | $data_from_post_override[$field] = $value; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | // Merge data |
||
| 285 | if (count($data_from_post_override) > 0) { |
||
| 286 | $data_result = array_replace_recursive($data_from_post_override, $data_from_post); |
||
| 287 | } else { |
||
| 288 | $data_result = $data_from_post; |
||
| 289 | } |
||
| 290 | |||
| 291 | if (flextype('entries')->create($id, $data_result)) { |
||
| 292 | flextype('media')->folders()->create('entries/' . $id); |
||
| 293 | flextype('flash')->addMessage('success', __('admin_message_entry_created')); |
||
| 294 | } else { |
||
| 295 | flextype('flash')->addMessage('error', __('admin_message_entry_was_not_created')); |
||
| 296 | } |
||
| 297 | } else { |
||
| 298 | flextype('flash')->addMessage('error', __('admin_message_fieldset_not_found')); |
||
| 299 | } |
||
| 300 | } else { |
||
| 301 | flextype('flash')->addMessage('error', __('admin_message_entry_was_not_created')); |
||
| 302 | } |
||
| 303 | |||
| 304 | if (isset($data['create-and-edit'])) { |
||
| 305 | return $response->withRedirect(flextype('router')->pathFor('admin.entries.edit') . '?id=' . $id . '&type=editor'); |
||
| 306 | } else { |
||
| 307 | return $response->withRedirect(flextype('router')->pathFor('admin.entries.index') . '?id=' . $parent_entry_id); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Change entry type |
||
| 313 | * |
||
| 314 | * @param Request $request PSR7 request |
||
| 315 | * @param Response $response PSR7 response |
||
| 316 | * |
||
| 317 | * @return Response |
||
| 318 | */ |
||
| 319 | public function type(Request $request, Response $response) : Response |
||
| 320 | { |
||
| 321 | // Get Query Params |
||
| 322 | $query = $request->getQueryParams(); |
||
| 323 | |||
| 324 | // Set Entries ID in parts |
||
| 325 | if (isset($query['id'])) { |
||
| 326 | $parts = explode("/", $query['id']); |
||
| 327 | } else { |
||
| 328 | $parts = [0 => '']; |
||
| 329 | } |
||
| 330 | |||
| 331 | $entry = flextype('entries')->fetch($this->getEntryID($query))->toArray(); |
||
| 332 | |||
| 333 | $fieldsets = []; |
||
| 334 | |||
| 335 | // Get fieldsets files |
||
| 336 | $_fieldsets = Filesystem::listContents(PATH['project'] . '/fieldsets/'); |
||
| 337 | |||
| 338 | // If there is any fieldsets file then go... |
||
| 339 | if (count($_fieldsets) > 0) { |
||
| 340 | foreach ($_fieldsets as $fieldset) { |
||
| 341 | if ($fieldset['type'] == 'file' && $fieldset['extension'] == 'yaml') { |
||
| 342 | $fieldset_content = flextype('serializers')->yaml()->decode(Filesystem::read($fieldset['path'])); |
||
| 343 | if (isset($fieldset_content['form']) && |
||
| 344 | isset($fieldset_content['form']['tabs']['main']) && |
||
| 345 | isset($fieldset_content['form']['tabs']['main']['fields']) && |
||
| 346 | isset($fieldset_content['form']['tabs']['main']['fields']['title'])) { |
||
| 347 | if (isset($fieldset_content['hide']) && $fieldset_content['hide'] == true) { |
||
| 348 | continue; |
||
| 349 | } |
||
| 350 | $fieldsets[$fieldset['basename']] = $fieldset_content['title']; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | $fieldset = $entry['fieldset'] ?? []; |
||
| 357 | |||
| 358 | return flextype('twig')->render( |
||
| 359 | $response, |
||
| 360 | 'plugins/admin/templates/content/entries/type.html', |
||
| 361 | [ |
||
| 362 | 'fieldset' => $fieldset, |
||
| 363 | 'fieldsets' => $fieldsets, |
||
| 364 | 'id' => $this->getEntryID($query), |
||
| 365 | 'menu_item' => 'entries', |
||
| 366 | 'parts' => $parts, |
||
| 367 | 'i' => count($parts), |
||
| 368 | 'last' => array_pop($parts), |
||
| 369 | 'links' => [ |
||
| 370 | 'entries' => [ |
||
| 371 | 'link' => flextype('router')->pathFor('admin.entries.index'), |
||
| 372 | 'title' => __('admin_entries'), |
||
| 373 | |||
| 374 | ], |
||
| 375 | 'entries_type' => [ |
||
| 376 | 'link' => flextype('router')->pathFor('admin.entries.type') . '?id=' . $this->getEntryID($query), |
||
| 377 | 'title' => __('admin_type'), |
||
| 378 | 'active' => true |
||
| 379 | ] |
||
| 380 | ] |
||
| 381 | ] |
||
| 382 | ); |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Change entry type - process |
||
| 387 | * |
||
| 388 | * @param Request $request PSR7 request |
||
| 389 | * @param Response $response PSR7 response |
||
| 390 | * |
||
| 391 | * @return Response |
||
| 392 | */ |
||
| 393 | public function typeProcess(Request $request, Response $response) : Response |
||
| 394 | { |
||
| 395 | $post_data = $request->getParsedBody(); |
||
| 396 | |||
| 397 | $id = $post_data['id']; |
||
| 398 | |||
| 399 | $entry = flextype('entries')->fetch($id)->toArray(); |
||
| 400 | |||
| 401 | Arrays::delete($entry, 'slug'); |
||
| 402 | Arrays::delete($entry, 'id'); |
||
| 403 | Arrays::delete($entry, 'modified_at'); |
||
| 404 | Arrays::delete($entry, 'created_at'); |
||
| 405 | Arrays::delete($entry, 'published_at'); |
||
| 406 | |||
| 407 | Arrays::delete($post_data, 'csrf_name'); |
||
| 408 | Arrays::delete($post_data, 'csrf_value'); |
||
| 409 | Arrays::delete($post_data, 'save_entry'); |
||
| 410 | Arrays::delete($post_data, 'id'); |
||
| 411 | |||
| 412 | $post_data['created_by'] = flextype('acl')->getUserLoggedInUuid(); |
||
| 413 | $post_data['published_by'] = flextype('acl')->getUserLoggedInUuid(); |
||
| 414 | |||
| 415 | $data = array_merge($entry, $post_data); |
||
| 416 | |||
| 417 | if (flextype('entries')->update( |
||
| 418 | $id, |
||
| 419 | $data |
||
| 420 | )) { |
||
| 421 | flextype('flash')->addMessage('success', __('admin_message_entry_changes_saved')); |
||
| 422 | } else { |
||
| 423 | flextype('flash')->addMessage('error', __('admin_message_entry_was_not_moved')); |
||
| 424 | } |
||
| 425 | |||
| 426 | return $response->withRedirect(flextype('router')->pathFor('admin.entries.index') . '?id=' . implode('/', array_slice(explode("/", $id), 0, -1))); |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Move entry |
||
| 431 | * |
||
| 432 | * @param Request $request PSR7 request |
||
| 433 | * @param Response $response PSR7 response |
||
| 434 | * |
||
| 435 | * @return Response |
||
| 436 | */ |
||
| 437 | public function move(Request $request, Response $response) : Response |
||
| 438 | { |
||
| 439 | // Get Query Params |
||
| 440 | $query = $request->getQueryParams(); |
||
| 441 | |||
| 442 | // Get Entry from Query Params |
||
| 443 | $entry_id = $this->getEntryID($query); |
||
| 444 | |||
| 445 | // Get current Entry ID |
||
| 446 | $parts = explode("/", $entry_id); |
||
| 447 | $entry_id_current = array_pop($parts); |
||
| 448 | |||
| 449 | // Fetch entry |
||
| 450 | $entry = flextype('entries')->fetch($this->getEntryID($query))->toArray(); |
||
| 451 | |||
| 452 | // Set Entries IDs in parts |
||
| 453 | if (isset($query['id'])) { |
||
| 454 | $parts = explode("/", $query['id']); |
||
| 455 | } else { |
||
| 456 | $parts = [0 => '']; |
||
| 457 | } |
||
| 458 | |||
| 459 | // Get entries list |
||
| 460 | $entries_list['/'] = '/'; |
||
| 461 | foreach (flextype('entries')->fetch('', ['collection' => true, 'find' => ['depth' => '>0'], 'filter' => ['order_by' => ['field' => ['id']]]])->toArray() as $_entry) { |
||
| 462 | if ($_entry['id'] != '') { |
||
| 463 | $entries_list[$_entry['id']] = $_entry['id']; |
||
| 464 | } else { |
||
| 465 | $entries_list[flextype('registry')->get('flextype.entries.main')] = flextype('registry')->get('flextype.entries.main'); |
||
| 466 | } |
||
| 467 | } |
||
| 468 | |||
| 469 | return flextype('twig')->render( |
||
| 470 | $response, |
||
| 471 | 'plugins/admin/templates/content/entries/move.html', |
||
| 472 | [ |
||
| 473 | 'menu_item' => 'entries', |
||
| 474 | 'entries_list' => $entries_list, |
||
| 475 | 'entry_id_current' => $entry_id_current, |
||
| 476 | 'entry_id_path_current' => $entry_id, |
||
| 477 | 'entry_id_path_parent' => implode('/', array_slice(explode("/", $entry_id), 0, -1)), |
||
| 478 | 'parts' => $parts, |
||
| 479 | 'i' => count($parts), |
||
| 480 | 'last' => array_pop($parts), |
||
| 481 | 'links' => [ |
||
| 482 | 'entries' => [ |
||
| 483 | 'link' => flextype('router')->pathFor('admin.entries.index'), |
||
| 484 | 'title' => __('admin_entries'), |
||
| 485 | |||
| 486 | ], |
||
| 487 | 'entries_move' => [ |
||
| 488 | 'link' => flextype('router')->pathFor('admin.entries.move'), |
||
| 489 | 'title' => __('admin_move'), |
||
| 490 | 'active' => true |
||
| 491 | ] |
||
| 492 | ] |
||
| 493 | ] |
||
| 494 | ); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Move entry - process |
||
| 499 | * |
||
| 500 | * @param Request $request PSR7 request |
||
| 501 | * @param Response $response PSR7 response |
||
| 502 | * |
||
| 503 | * @return Response |
||
| 504 | */ |
||
| 505 | public function moveProcess(Request $request, Response $response) |
||
| 506 | { |
||
| 507 | // Get data from POST |
||
| 508 | $data = $request->getParsedBody(); |
||
| 509 | |||
| 510 | // Set entry id current |
||
| 511 | $entry_id_current = $data['entry_id_current']; |
||
| 512 | |||
| 513 | if (!flextype('entries')->has($data['parent_entry'] . '/' . $entry_id_current)) { |
||
| 514 | if (flextype('entries')->move( |
||
| 515 | $data['entry_id_path_current'], |
||
| 516 | $data['parent_entry'] . '/' . $entry_id_current |
||
| 517 | )) { |
||
| 518 | if (! flextype('media')->folders()->has('entries/' . $data['entry_id_path_current'])) { |
||
| 519 | flextype('media')->folders()->create('entries/' . $data['entry_id_path_current']); |
||
| 520 | } |
||
| 521 | |||
| 522 | flextype('media')->folders()->move('entries/' . $data['entry_id_path_current'], 'entries/' . $data['parent_entry'] . '/' . $entry_id_current); |
||
| 523 | |||
| 524 | flextype('flash')->addMessage('success', __('admin_message_entry_moved')); |
||
| 525 | } else { |
||
| 526 | flextype('flash')->addMessage('error', __('admin_message_entry_was_not_moved')); |
||
| 527 | } |
||
| 528 | } else { |
||
| 529 | flextype('flash')->addMessage('error', __('admin_message_entry_was_not_moved')); |
||
| 530 | } |
||
| 531 | |||
| 532 | return $response->withRedirect(flextype('router')->pathFor('admin.entries.index') . '?id=' . (($data['parent_entry'] == '/') ? '' : $data['parent_entry'])); |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Rename entry |
||
| 537 | * |
||
| 538 | * @param Request $request PSR7 request |
||
| 539 | * @param Response $response PSR7 response |
||
| 540 | * |
||
| 541 | * @return Response |
||
| 542 | */ |
||
| 543 | public function rename(Request $request, Response $response) : Response |
||
| 579 | ] |
||
| 580 | ] |
||
| 581 | ] |
||
| 582 | ); |
||
| 583 | } |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Rename entry - process |
||
| 587 | * |
||
| 588 | * @param Request $request PSR7 request |
||
| 589 | * @param Response $response PSR7 response |
||
| 590 | * |
||
| 591 | * @return Response |
||
| 592 | */ |
||
| 593 | public function renameProcess(Request $request, Response $response) : Response |
||
| 594 | { |
||
| 595 | $data = $request->getParsedBody(); |
||
| 596 | |||
| 597 | // Set name |
||
| 598 | if (flextype('registry')->get('plugins.admin.settings.entries.slugify') == true) { |
||
| 599 | $name = flextype('slugify')->slugify($data['name']); |
||
| 600 | } else { |
||
| 601 | $name = $data['name']; |
||
| 602 | } |
||
| 603 | |||
| 604 | if (flextype('entries')->move( |
||
| 605 | $data['entry_path_current'], |
||
| 606 | $data['entry_parent'] . '/' . $name) |
||
| 607 | ) { |
||
| 608 | if (! flextype('media')->folders()->has('entries/' . $data['entry_path_current'])) { |
||
| 609 | flextype('media')->folders()->create('entries/' . $data['entry_path_current']); |
||
| 610 | } |
||
| 611 | |||
| 612 | flextype('media')->folders()->move('entries/' . $data['entry_path_current'], |
||
| 613 | 'entries/' . $data['entry_parent'] . '/' . flextype('slugify')->slugify($data['name'])); |
||
| 614 | flextype('flash')->addMessage('success', __('admin_message_entry_renamed')); |
||
| 615 | } else { |
||
| 616 | flextype('flash')->addMessage('error', __('admin_message_entry_was_not_renamed')); |
||
| 617 | } |
||
| 618 | |||
| 619 | return $response->withRedirect(flextype('router')->pathFor('admin.entries.index') . '?id=' . $data['entry_parent']); |
||
| 620 | } |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Delete entry - process |
||
| 624 | * |
||
| 625 | * @param Request $request PSR7 request |
||
| 626 | * @param Response $response PSR7 response |
||
| 627 | * |
||
| 628 | * @return Response |
||
| 629 | */ |
||
| 630 | public function deleteProcess(Request $request, Response $response) : Response |
||
| 651 | } |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Duplicate entry - process |
||
| 655 | * |
||
| 656 | * @param Request $request PSR7 request |
||
| 657 | * @param Response $response PSR7 response |
||
| 658 | * |
||
| 659 | * @return Response |
||
| 660 | */ |
||
| 661 | public function duplicateProcess(Request $request, Response $response) : Response |
||
| 662 | { |
||
| 663 | $data = $request->getParsedBody(); |
||
| 664 | |||
| 665 | $id = $data['id']; |
||
| 666 | $parent_id = implode('/', array_slice(explode("/", $id), 0, -1)); |
||
| 667 | |||
| 668 | $random_date = date("Ymd_His"); |
||
| 669 | |||
| 670 | flextype('entries')->copy($id, $id . '-duplicate-' . $random_date, true); |
||
| 671 | |||
| 672 | if (! flextype('media')->folders()->has('entries/' . $id)) { |
||
| 673 | flextype('media')->folders()->create('entries/' . $id); |
||
| 674 | } |
||
| 675 | |||
| 676 | flextype('media')->folders()->copy('entries/' . $id, 'entries/' . $id . '-duplicate-' . $random_date, true); |
||
| 677 | |||
| 678 | if (Filesystem::has(PATH['project'] . '/media' . '/entries/' . $id)) { |
||
| 679 | filesystem() |
||
| 680 | ->directory(PATH['project'] . '/media' . '/entries/' . $id) |
||
| 681 | ->copy(PATH['project'] . '/media' . '/entries/' . $id . '-duplicate-' . $random_date); |
||
| 682 | } else { |
||
| 683 | Filesystem::createDir(PATH['project'] . '/media' . '/entries/' . $id . '-duplicate-' . $random_date); |
||
| 684 | } |
||
| 685 | |||
| 686 | flextype('flash')->addMessage('success', __('admin_message_entry_duplicated')); |
||
| 687 | |||
| 688 | return $response->withRedirect(flextype('router')->pathFor('admin.entries.index') . '?id=' . $parent_id); |
||
| 689 | } |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Edit entry |
||
| 693 | * |
||
| 694 | * @param Request $request PSR7 request |
||
| 695 | * @param Response $response PSR7 response |
||
| 696 | * |
||
| 697 | * @return Response |
||
| 698 | */ |
||
| 699 | public function edit(Request $request, Response $response) : Response |
||
| 700 | { |
||
| 701 | // Get Query Params |
||
| 702 | $query = $request->getQueryParams(); |
||
| 703 | |||
| 704 | // Set Entries ID in parts |
||
| 705 | if (isset($query['id'])) { |
||
| 706 | $parts = explode("/", $query['id']); |
||
| 707 | } else { |
||
| 708 | $parts = [0 => '']; |
||
| 709 | } |
||
| 710 | |||
| 711 | // Get Entry type |
||
| 712 | $type = $request->getQueryParams()['type']; |
||
| 713 | |||
| 714 | flextype('registry')->set('entries.fields.parsers.settings.enabled', false); |
||
| 715 | |||
| 716 | // Get Entry |
||
| 717 | $entry = flextype('entries')->fetch($this->getEntryID($query))->toArray(); |
||
| 718 | |||
| 719 | Arrays::delete($entry, 'slug'); |
||
| 720 | Arrays::delete($entry, 'id'); |
||
| 721 | Arrays::delete($entry, 'modified_at'); |
||
| 722 | |||
| 723 | // Fieldsets for current entry template |
||
| 724 | $fieldsets_path = PATH['project'] . '/fieldsets/' . (isset($entry['fieldset']) ? $entry['fieldset'] : 'default') . '.yaml'; |
||
| 725 | $fieldsets = flextype('serializers')->yaml()->decode(Filesystem::read($fieldsets_path)); |
||
| 726 | is_null($fieldsets) and $fieldsets = []; |
||
| 727 | |||
| 728 | if ($type == 'source') { |
||
| 729 | |||
| 730 | $entrySource = filesystem()->file(flextype('entries')->getFileLocation($this->getEntryID($query)))->get(); |
||
| 731 | |||
| 732 | //$entry['published_at'] = date(flextype('registry')->get('flextype.settings.date_format'), $entry['published_at']); |
||
| 733 | //$entry['created_at'] = date(flextype('registry')->get('flextype.settings.date_format'), $entry['created_at']); |
||
| 734 | |||
| 735 | return flextype('twig')->render( |
||
| 736 | $response, |
||
| 737 | 'plugins/admin/templates/content/entries/source.html', |
||
| 738 | [ |
||
| 739 | 'parts' => $parts, |
||
| 740 | 'i' => count($parts), |
||
| 741 | 'last' => array_pop($parts), |
||
| 742 | 'id' => $this->getEntryID($query), |
||
| 743 | 'data' => $entrySource, |
||
| 744 | 'type' => $type, |
||
| 745 | 'menu_item' => 'entries', |
||
| 746 | 'links' => [ |
||
| 747 | 'entries' => [ |
||
| 748 | 'link' => flextype('router')->pathFor('admin.entries.index'), |
||
| 749 | 'title' => __('admin_entries'), |
||
| 750 | |||
| 751 | ], |
||
| 752 | 'edit_entry' => [ |
||
| 753 | 'link' => flextype('router')->pathFor('admin.entries.edit') . '?id=' . $this->getEntryID($query). '&type=editor', |
||
| 754 | 'title' => __('admin_editor'), |
||
| 755 | |||
| 756 | ], |
||
| 757 | 'edit_entry_media' => [ |
||
| 758 | 'link' => flextype('router')->pathFor('admin.entries.edit') . '?id=' . $this->getEntryID($query) . '&type=media', |
||
| 759 | 'title' => __('admin_media'), |
||
| 760 | |||
| 761 | ], |
||
| 762 | 'edit_entry_source' => [ |
||
| 763 | 'link' => flextype('router')->pathFor('admin.entries.edit') . '?id=' . $this->getEntryID($query) . '&type=source', |
||
| 764 | 'title' => __('admin_source'), |
||
| 765 | 'active' => true |
||
| 766 | ], |
||
| 767 | ], |
||
| 768 | 'buttons' => [ |
||
| 769 | 'save_entry' => [ |
||
| 770 | 'id' => 'form', |
||
| 771 | 'link' => 'javascript:;', |
||
| 772 | 'title' => __('admin_save'), |
||
| 773 | 'type' => 'action' |
||
| 774 | ], |
||
| 775 | ] |
||
| 776 | ] |
||
| 777 | ); |
||
| 778 | } elseif ($type == 'media') { |
||
| 779 | return flextype('twig')->render( |
||
| 780 | $response, |
||
| 781 | 'plugins/admin/templates/content/entries/media.html', |
||
| 782 | [ |
||
| 783 | 'parts' => $parts, |
||
| 784 | 'i' => count($parts), |
||
| 785 | 'last' => array_pop($parts), |
||
| 786 | 'id' => $this->getEntryID($query), |
||
| 787 | 'files' => $this->getMediaList($this->getEntryID($query), true), |
||
| 788 | 'menu_item' => 'entries', |
||
| 789 | 'links' => [ |
||
| 790 | 'entries' => [ |
||
| 791 | 'link' => flextype('router')->pathFor('admin.entries.index') . '?id=' . implode('/', array_slice(explode("/", $this->getEntryID($query)), 0, -1)), |
||
| 792 | 'title' => __('admin_entries'), |
||
| 793 | |||
| 794 | ], |
||
| 795 | 'edit_entry' => [ |
||
| 796 | 'link' => flextype('router')->pathFor('admin.entries.edit') . '?id=' . $this->getEntryID($query) . '&type=editor', |
||
| 797 | 'title' => __('admin_editor'), |
||
| 798 | |||
| 799 | ], |
||
| 800 | 'edit_entry_media' => [ |
||
| 801 | 'link' => flextype('router')->pathFor('admin.entries.edit') . '?id=' . $this->getEntryID($query) . '&type=media', |
||
| 802 | 'title' => __('admin_media'), |
||
| 803 | 'active' => true |
||
| 804 | ], |
||
| 805 | 'edit_entry_source' => [ |
||
| 806 | 'link' => flextype('router')->pathFor('admin.entries.edit') . '?id=' . $this->getEntryID($query) . '&type=source', |
||
| 807 | 'title' => __('admin_source'), |
||
| 808 | ], |
||
| 809 | ] |
||
| 810 | ] |
||
| 811 | ); |
||
| 812 | } else { |
||
| 813 | |||
| 814 | // Merge current entry fieldset with global fildset |
||
| 815 | if (isset($entry['entry_fieldset'])) { |
||
| 816 | $form = flextype('form')->render(array_replace_recursive($fieldsets, $entry['entry_fieldset']), $entry); |
||
| 817 | } else { |
||
| 818 | $form = flextype('form')->render($fieldsets, $entry); |
||
| 819 | } |
||
| 820 | |||
| 821 | return flextype('twig')->render( |
||
| 822 | $response, |
||
| 823 | 'plugins/admin/templates/content/entries/edit.html', |
||
| 824 | [ |
||
| 825 | 'parts' => $parts, |
||
| 826 | 'i' => count($parts), |
||
| 827 | 'last' => array_pop($parts), |
||
| 828 | 'form' => $form, |
||
| 829 | 'menu_item' => 'entries', |
||
| 830 | 'links' => [ |
||
| 831 | 'entries' => [ |
||
| 832 | 'link' => flextype('router')->pathFor('admin.entries.index') . '?id=' . implode('/', array_slice(explode("/", $this->getEntryID($query)), 0, -1)), |
||
| 833 | 'title' => __('admin_entries') |
||
| 834 | ], |
||
| 835 | 'edit_entry' => [ |
||
| 836 | 'link' => flextype('router')->pathFor('admin.entries.edit') . '?id=' . $this->getEntryID($query) . '&type=editor', |
||
| 837 | 'title' => __('admin_editor'), |
||
| 838 | 'active' => true |
||
| 839 | ], |
||
| 840 | 'edit_entry_media' => [ |
||
| 841 | 'link' => flextype('router')->pathFor('admin.entries.edit') . '?id=' . $this->getEntryID($query) . '&type=media', |
||
| 842 | 'title' => __('admin_media') |
||
| 843 | ], |
||
| 844 | 'edit_entry_source' => [ |
||
| 845 | 'link' => flextype('router')->pathFor('admin.entries.edit') . '?id=' . $this->getEntryID($query) . '&type=source', |
||
| 846 | 'title' => __('admin_source') |
||
| 847 | ], |
||
| 848 | ], |
||
| 849 | 'buttons' => [ |
||
| 850 | 'save_entry' => [ |
||
| 851 | 'id' => 'form', |
||
| 852 | 'link' => 'javascript:;', |
||
| 853 | 'title' => __('admin_save'), |
||
| 854 | 'type' => 'action' |
||
| 855 | ], |
||
| 856 | ] |
||
| 857 | ] |
||
| 858 | ); |
||
| 859 | } |
||
| 860 | } |
||
| 861 | |||
| 862 | /** |
||
| 863 | * Edit entry process |
||
| 864 | * |
||
| 865 | * @param Request $request PSR7 request |
||
| 866 | * @param Response $response PSR7 response |
||
| 867 | * |
||
| 868 | * @return Response |
||
| 869 | */ |
||
| 870 | public function editProcess(Request $request, Response $response) : Response |
||
| 871 | { |
||
| 872 | $query = $request->getQueryParams(); |
||
| 873 | |||
| 874 | // Get Entry ID and TYPE from GET param |
||
| 875 | $id = $query['id']; |
||
| 876 | $type = $query['type']; |
||
| 877 | |||
| 878 | if ($type == 'source') { |
||
| 879 | |||
| 880 | // Data from POST |
||
| 881 | $data = $request->getParsedBody(); |
||
| 882 | |||
| 883 | $entry = flextype('serializers')->frontmatter()->decode($data['data']); |
||
| 884 | |||
| 885 | $entry['created_by'] = flextype('acl')->getUserLoggedInUuid(); |
||
| 886 | $entry['published_by'] = flextype('acl')->getUserLoggedInUuid(); |
||
| 887 | |||
| 888 | Arrays::delete($entry, 'slug'); |
||
| 889 | Arrays::delete($entry, 'id'); |
||
| 890 | Arrays::delete($entry, 'modified_at'); |
||
| 891 | |||
| 892 | // Update entry |
||
| 893 | if (Filesystem::write(PATH['project'] . '/entries' . '/' . $id . '/entry.md', flextype('serializers')->frontmatter()->encode($entry))) { |
||
| 894 | flextype('flash')->addMessage('success', __('admin_message_entry_changes_saved')); |
||
| 895 | } else { |
||
| 896 | flextype('flash')->addMessage('error', __('admin_message_entry_changes_not_saved')); |
||
| 897 | } |
||
| 898 | } else { |
||
| 899 | // Result data to save |
||
| 900 | $result_data = []; |
||
| 901 | |||
| 902 | // Data from POST |
||
| 903 | $data = $request->getParsedBody(); |
||
| 904 | |||
| 905 | // Delete system fields |
||
| 906 | isset($data['slug']) and Arrays::delete($data, 'slug'); |
||
| 907 | isset($data['id']) and Arrays::delete($data, 'id'); |
||
| 908 | isset($data['csrf_value']) and Arrays::delete($data, 'csrf_value'); |
||
| 909 | isset($data['csrf_name']) and Arrays::delete($data, 'csrf_name'); |
||
| 910 | isset($data['form-save-action']) and Arrays::delete($data, 'form-save-action'); |
||
| 911 | isset($data['trumbowyg-icons-path']) and Arrays::delete($data, 'trumbowyg-icons-path'); |
||
| 912 | isset($data['trumbowyg-locale']) and Arrays::delete($data, 'trumbowyg-locale'); |
||
| 913 | isset($data['flatpickr-date-format']) and Arrays::delete($data, 'flatpickr-date-format'); |
||
| 914 | isset($data['flatpickr-locale']) and Arrays::delete($data, 'flatpickr-locale'); |
||
| 915 | |||
| 916 | $data['published_by'] = flextype('acl')->getUserLoggedInUuid(); |
||
| 917 | |||
| 918 | $entry = flextype('serializers') |
||
| 919 | ->frontmatter() |
||
| 920 | ->decode(filesystem()->file(flextype('entries')->getFileLocation($id))->get()); |
||
| 921 | |||
| 922 | Arrays::delete($entry, 'slug'); |
||
| 923 | Arrays::delete($entry, 'id'); |
||
| 924 | Arrays::delete($entry, 'modified_at'); |
||
| 925 | |||
| 926 | if (isset($data['created_at'])) { |
||
| 927 | $data['created_at'] = date(flextype('registry')->get('flextype.settings.date_format'), strtotime($data['created_at'])); |
||
| 928 | } else { |
||
| 929 | $data['created_at'] = date(flextype('registry')->get('flextype.settings.date_format'), strtotime($entry['created_at'])); |
||
| 930 | } |
||
| 931 | |||
| 932 | if (isset($data['published_at'])) { |
||
| 933 | $data['published_at'] = (string) date(flextype('registry')->get('flextype.settings.date_format'), strtotime($data['published_at'])); |
||
| 934 | } else { |
||
| 935 | $data['published_at'] = (string) date(flextype('registry')->get('flextype.settings.date_format'), strtotime($entry['published_at'])); |
||
| 936 | } |
||
| 937 | |||
| 938 | if (isset($data['routable'])) { |
||
| 939 | $data['routable'] = (bool) $data['routable']; |
||
| 940 | } elseif(isset($entry['routable'])) { |
||
| 941 | $data['routable'] = (bool) $entry['routable']; |
||
| 942 | } else { |
||
| 943 | $data['routable'] = true; |
||
| 944 | } |
||
| 945 | |||
| 946 | // Merge entry data with $data |
||
| 947 | $result_data = array_merge($entry, $data); |
||
| 948 | |||
| 949 | // Update entry |
||
| 950 | if (flextype('entries')->update($id, $result_data)) { |
||
| 951 | flextype('flash')->addMessage('success', __('admin_message_entry_changes_saved')); |
||
| 952 | } else { |
||
| 953 | flextype('flash')->addMessage('error', __('admin_message_entry_changes_not_saved')); |
||
| 954 | } |
||
| 955 | } |
||
| 956 | |||
| 957 | return $response->withRedirect(flextype('router')->pathFor('admin.entries.edit') . '?id=' . $id . '&type=' . $type); |
||
| 958 | } |
||
| 959 | |||
| 960 | /** |
||
| 961 | * Delete media file - process |
||
| 962 | * |
||
| 963 | * @param Request $request PSR7 request |
||
| 964 | * @param Response $response PSR7 response |
||
| 965 | * |
||
| 966 | * @return Response |
||
| 967 | */ |
||
| 968 | public function deleteMediaFileProcess(Request $request, Response $response) : Response |
||
| 980 | } |
||
| 981 | |||
| 982 | /** |
||
| 983 | * Upload media file - process |
||
| 984 | * |
||
| 985 | * @param Request $request PSR7 request |
||
| 986 | * @param Response $response PSR7 response |
||
| 987 | * |
||
| 988 | * @return Response |
||
| 989 | */ |
||
| 990 | public function uploadMediaFileProcess(Request $request, Response $response) : Response |
||
| 1005 | } |
||
| 1006 | |||
| 1007 | /** |
||
| 1008 | * Get media list |
||
| 1009 | * |
||
| 1010 | * @param string $id Entry ID |
||
| 1011 | * @param bool $path if true returns with url paths |
||
| 1012 | * |
||
| 1013 | * @return array |
||
| 1014 | */ |
||
| 1015 | public function getMediaList(string $id, bool $path = false) : array |
||
| 1038 | } |
||
| 1039 | |||
| 1041 |