| Total Complexity | 60 |
| Total Lines | 446 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like LayoutHelper 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 LayoutHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class LayoutHelper extends Helper |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * List of helpers used by this helper |
||
| 36 | * |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | public $helpers = ['Editors', 'Html', 'Link', 'Perms', 'System', 'Url']; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Is Dashboard |
||
| 43 | * |
||
| 44 | * @return bool True if visible for view |
||
| 45 | */ |
||
| 46 | public function isDashboard(): bool |
||
| 47 | { |
||
| 48 | return in_array($this->_View->getName(), ['Dashboard']); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Is Login |
||
| 53 | * |
||
| 54 | * @return bool True if visible for view |
||
| 55 | */ |
||
| 56 | public function isLogin(): bool |
||
| 57 | { |
||
| 58 | return in_array($this->_View->getName(), ['Login']); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Properties for various publication status |
||
| 63 | * |
||
| 64 | * @param array $object The object |
||
| 65 | * @return string pubstatus |
||
| 66 | */ |
||
| 67 | public function publishStatus(array $object = []): string |
||
| 68 | { |
||
| 69 | if (empty($object)) { |
||
| 70 | return ''; |
||
| 71 | } |
||
| 72 | |||
| 73 | $end = (string)Hash::get($object, 'attributes.publish_end'); |
||
| 74 | $start = (string)Hash::get($object, 'attributes.publish_start'); |
||
| 75 | |||
| 76 | if (!empty($end) && strtotime($end) <= time()) { |
||
| 77 | return 'expired'; |
||
| 78 | } |
||
| 79 | if (!empty($start) && strtotime($start) > time()) { |
||
| 80 | return 'future'; |
||
| 81 | } |
||
| 82 | if (!empty((string)Hash::get($object, 'meta.locked'))) { |
||
| 83 | return 'locked'; |
||
| 84 | } |
||
| 85 | if ((string)Hash::get($object, 'attributes.status') === 'draft') { |
||
| 86 | return 'draft'; |
||
| 87 | } |
||
| 88 | |||
| 89 | return ''; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Messages visibility |
||
| 94 | * |
||
| 95 | * @return bool True if visible for view |
||
| 96 | */ |
||
| 97 | public function messages(): bool |
||
| 98 | { |
||
| 99 | return $this->_View->getName() != 'Login'; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Return title with object title and current module name |
||
| 104 | * |
||
| 105 | * @return string title with object title and current module name separated by '|' |
||
| 106 | */ |
||
| 107 | public function title(): string |
||
| 108 | { |
||
| 109 | $module = (array)$this->getView()->get('currentModule'); |
||
| 110 | $name = (string)Hash::get($module, 'name'); |
||
| 111 | $object = (array)$this->getView()->get('object'); |
||
| 112 | $title = (string)Hash::get($object, 'attributes.title'); |
||
| 113 | $title = strip_tags($title); |
||
| 114 | |||
| 115 | return empty($title) ? $name : sprintf('%s | %s', $title, $name); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Return dashboard module link |
||
| 120 | * |
||
| 121 | * @param string $name The module name |
||
| 122 | * @param array $module The module data |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function dashboardModuleLink(string $name, array $module): string |
||
| 126 | { |
||
| 127 | if (in_array($name, ['trash', 'users'])) { |
||
| 128 | return ''; |
||
| 129 | } |
||
| 130 | $label = $name === 'objects' ? __('All objects') : Hash::get($module, 'label', $name); |
||
| 131 | $route = (array)Hash::get($module, 'route'); |
||
| 132 | $param = empty($route) ? ['_name' => 'modules:list', 'object_type' => $name, 'plugin' => null] : $route; |
||
| 133 | $count = !$this->showCounter($name) ? '' : $this->moduleCount($name); |
||
| 134 | |||
| 135 | return sprintf( |
||
| 136 | '<a href="%s" class="%s"><span>%s</span>%s%s</a>', |
||
| 137 | $this->Url->build($param), |
||
| 138 | sprintf('dashboard-item has-background-module-%s %s', $name, Hash::get($module, 'class', '')), |
||
| 139 | $this->tr($label), |
||
| 140 | $this->moduleIcon($name, $module), |
||
| 141 | $count |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Show counter for module |
||
| 147 | * |
||
| 148 | * @param string $name The module name |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | public function showCounter(string $name): bool |
||
| 152 | { |
||
| 153 | $counters = Configure::read('UI.modules.counters', ['trash']); |
||
| 154 | |||
| 155 | return is_array($counters) ? in_array($name, $counters) : $counters === 'all'; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Return module count span. |
||
| 160 | * |
||
| 161 | * @param string $name The module name |
||
| 162 | * @param string|null $moduleClass The module class |
||
| 163 | * @return string |
||
| 164 | */ |
||
| 165 | public function moduleCount(string $name, ?string $moduleClass = null): string |
||
| 166 | { |
||
| 167 | $count = CacheTools::getModuleCount($name); |
||
| 168 | |||
| 169 | return sprintf('<span class="module-count">%s</span>', $count); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Return module icon. |
||
| 174 | * |
||
| 175 | * @param string $name The module name |
||
| 176 | * @param array $module The module data |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function moduleIcon(string $name, array $module): string |
||
| 180 | { |
||
| 181 | if (Hash::get($module, 'hints.multiple_types') && !Hash::get($module, 'class')) { |
||
| 182 | return '<app-icon icon="carbon:grid" :style="{ width: \'28px\', height: \'28px\' }"></app-icon>'; |
||
| 183 | } |
||
| 184 | $icon = (string)Configure::read(sprintf('Modules.%s.icon', $name)); |
||
| 185 | if (!empty($icon)) { |
||
| 186 | return sprintf('<app-icon icon="%s" :style="{ width: \'28px\', height: \'28px\' }"></app-icon>', $icon); |
||
| 187 | } |
||
| 188 | $map = [ |
||
| 189 | 'audio' => 'carbon:document-audio', |
||
| 190 | 'categories' => 'carbon:collapse-categories', |
||
| 191 | 'documents' => 'carbon:document', |
||
| 192 | 'events' => 'carbon:event', |
||
| 193 | 'files' => 'carbon:document-blank', |
||
| 194 | 'folders' => 'carbon:tree-view', |
||
| 195 | 'images' => 'carbon:image', |
||
| 196 | 'links' => 'carbon:link', |
||
| 197 | 'locations' => 'carbon:location', |
||
| 198 | 'news' => 'carbon:calendar', |
||
| 199 | 'profiles' => 'carbon:person', |
||
| 200 | 'publications' => 'carbon:wikis', |
||
| 201 | 'tags' => 'carbon:tag', |
||
| 202 | 'users' => 'carbon:user', |
||
| 203 | 'videos' => 'carbon:video', |
||
| 204 | ]; |
||
| 205 | if (!in_array($name, array_keys($map))) { |
||
| 206 | return ''; |
||
| 207 | } |
||
| 208 | |||
| 209 | return sprintf('<app-icon icon="%s" :style="{ width: \'28px\', height: \'28px\' }"></app-icon>', $map[$name]); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Return module css class(es). |
||
| 214 | * If object is locked by parents, return base class plus 'locked' class. |
||
| 215 | * If object is locked by concurrent editors, return 'concurrent-editors' class plus publish status class. |
||
| 216 | * If object is not locked, return base class plus publish status class. |
||
| 217 | * Publish status class is 'expired', 'future', 'locked' or 'draft'. |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function moduleClass(): string |
||
| 222 | { |
||
| 223 | $moduleClasses = ['app-module-box']; |
||
| 224 | $object = (array)$this->getView()->get('object'); |
||
| 225 | if (!empty($object) && $this->Perms->isLockedByParents((string)Hash::get($object, 'id'))) { |
||
| 226 | $moduleClasses[] = 'locked'; |
||
| 227 | |||
| 228 | return trim(implode(' ', $moduleClasses)); |
||
| 229 | } |
||
| 230 | $editors = $this->Editors->list(); |
||
| 231 | if (count($editors) > 1) { |
||
| 232 | $moduleClasses = ['app-module-box-concurrent-editors']; |
||
| 233 | } |
||
| 234 | $moduleClasses[] = $this->publishStatus($object); |
||
| 235 | |||
| 236 | return trim(implode(' ', $moduleClasses)); |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Module main link |
||
| 241 | * |
||
| 242 | * @return string The link |
||
| 243 | */ |
||
| 244 | public function moduleLink(): string |
||
| 245 | { |
||
| 246 | $currentModule = (array)$this->getView()->get('currentModule'); |
||
| 247 | if (!empty($currentModule) && !empty($currentModule['name'])) { |
||
| 248 | $name = $currentModule['name']; |
||
| 249 | $label = Hash::get($currentModule, 'label', $name); |
||
| 250 | $counters = Configure::read('UI.modules.counters', ['trash']); |
||
| 251 | $showCounter = is_array($counters) ? in_array($name, $counters) : $counters === 'all'; |
||
| 252 | $count = !$showCounter ? '' : $this->moduleCount($name); |
||
| 253 | |||
| 254 | return sprintf( |
||
| 255 | '<a href="%s" class="%s"><span class="mr-05">%s</span>%s%s</a>', |
||
| 256 | $this->Url->build(['_name' => 'modules:list', 'object_type' => $name]), |
||
| 257 | sprintf('module-item has-background-module-%s', $name), |
||
| 258 | $this->tr($label), |
||
| 259 | $this->moduleIcon($name, $currentModule), |
||
| 260 | $count |
||
| 261 | ); |
||
| 262 | } |
||
| 263 | |||
| 264 | // if no `currentModule` has been set a `moduleLink` must be set in controller otherwise current link is displayed |
||
| 265 | return $this->Html->link( |
||
| 266 | $this->tr($this->getView()->getName()), |
||
| 267 | (array)$this->getView()->get('moduleLink'), |
||
| 268 | ['class' => $this->commandLinkClass()] |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Return module index default view type |
||
| 274 | * |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | public function moduleIndexDefaultViewType(): string |
||
| 278 | { |
||
| 279 | $module = (array)$this->getView()->get('currentModule'); |
||
| 280 | $name = (string)Hash::get($module, 'name'); |
||
| 281 | |||
| 282 | return $name === 'folders' ? 'tree' : 'list'; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Return module index view type |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | public function moduleIndexViewType(): string |
||
| 291 | { |
||
| 292 | $query = (array)$this->getView()->getRequest()->getQueryParams(); |
||
| 293 | |||
| 294 | return (string)Hash::get($query, 'view_type', $this->moduleIndexDefaultViewType()); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Return module index view types |
||
| 299 | * |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | public function moduleIndexViewTypes(): array |
||
| 303 | { |
||
| 304 | $defaultType = $this->moduleIndexDefaultViewType(); |
||
| 305 | |||
| 306 | return $defaultType === 'tree' ? ['tree', 'list'] : ['list']; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Return style class for command link |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | protected function commandLinkClass(): string |
||
| 315 | { |
||
| 316 | $moduleClasses = [ |
||
| 317 | 'UserProfile' => 'has-background-black icon-user', |
||
| 318 | 'Import' => 'has-background-black icon-download-alt', |
||
| 319 | 'ObjectTypes' => 'has-background-black', |
||
| 320 | 'Relations' => 'has-background-black', |
||
| 321 | 'PropertyTypes' => 'has-background-black', |
||
| 322 | 'Categories' => 'has-background-black', |
||
| 323 | 'Appearance' => 'has-background-black', |
||
| 324 | 'Applications' => 'has-background-black', |
||
| 325 | 'AsyncJobs' => 'has-background-black', |
||
| 326 | 'Config' => 'has-background-black', |
||
| 327 | 'Endpoints' => 'has-background-black', |
||
| 328 | 'Roles' => 'has-background-black', |
||
| 329 | 'RolesModules' => 'has-background-black', |
||
| 330 | 'EndpointPermissions' => 'has-background-black', |
||
| 331 | 'Tags' => 'has-background-module-tags', |
||
| 332 | 'ObjectsHistory' => 'has-background-black', |
||
| 333 | 'SystemInfo' => 'has-background-black', |
||
| 334 | 'UserAccesses' => 'has-background-black', |
||
| 335 | 'Statistics' => 'has-background-black', |
||
| 336 | ]; |
||
| 337 | |||
| 338 | return (string)Hash::get($moduleClasses, $this->_View->getName(), 'commands-menu__module'); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Get translated val by input string, using plugins (if any) translations. |
||
| 343 | * |
||
| 344 | * @param string $input The input string |
||
| 345 | * @return string|null |
||
| 346 | */ |
||
| 347 | public function tr(string $input): ?string |
||
| 348 | { |
||
| 349 | return Translate::get($input); |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Return configuration items to create JSON BEDITA object |
||
| 354 | * |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | public function metaConfig(): array |
||
| 358 | { |
||
| 359 | return [ |
||
| 360 | 'base' => $this->Link->baseUrl(), |
||
| 361 | 'currentModule' => $this->getView()->get('currentModule', ['name' => 'home']), |
||
| 362 | 'template' => $this->getView()->getTemplate(), |
||
| 363 | 'modules' => array_keys($this->getView()->get('modules', [])), |
||
| 364 | 'plugins' => \App\Plugin::loadedAppPlugins(), |
||
| 365 | 'uploadable' => $this->getView()->get('uploadable', []), |
||
| 366 | 'locale' => \Cake\I18n\I18n::getLocale(), |
||
| 367 | 'csrfToken' => $this->getCsrfToken(), |
||
| 368 | 'maxFileSize' => $this->System->getMaxFileSize(), |
||
| 369 | 'canReadUsers' => $this->Perms->canRead('users'), |
||
| 370 | 'canSave' => $this->Perms->canSave(), |
||
| 371 | 'cloneConfig' => (array)Configure::read('Clone'), |
||
| 372 | 'placeholdersConfig' => $this->System->placeholdersConfig(), |
||
| 373 | 'uploadConfig' => $this->System->uploadConfig(), |
||
| 374 | 'relationsSchema' => $this->getView()->get('relationsSchema', []), |
||
| 375 | 'richeditorConfig' => (array)Configure::read('Richeditor'), |
||
| 376 | 'richeditorByPropertyConfig' => (array)Configure::read('UI.richeditor', []), |
||
| 377 | 'indexLists' => (array)$this->indexLists(), |
||
| 378 | ]; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Get csrf token, searching in: request params, data, attributes and cookies |
||
| 383 | * |
||
| 384 | * @return string|null |
||
| 385 | */ |
||
| 386 | public function getCsrfToken(): ?string |
||
| 387 | { |
||
| 388 | if (!empty($this->getView()->getRequest()->getParam('_csrfToken'))) { |
||
| 389 | return (string)$this->getView()->getRequest()->getParam('_csrfToken'); |
||
| 390 | } |
||
| 391 | if (!empty($this->getView()->getRequest()->getData('_csrfToken'))) { |
||
| 392 | return (string)$this->getView()->getRequest()->getData('_csrfToken'); |
||
| 393 | } |
||
| 394 | if (!empty($this->getView()->getRequest()->getAttribute('csrfToken'))) { |
||
| 395 | return (string)$this->getView()->getRequest()->getAttribute('csrfToken'); |
||
| 396 | } |
||
| 397 | if (!empty($this->getView()->getRequest()->getCookie('csrfToken'))) { |
||
| 398 | return (string)$this->getView()->getRequest()->getCookie('csrfToken'); |
||
| 399 | } |
||
| 400 | |||
| 401 | return null; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Trash link by type. |
||
| 406 | * |
||
| 407 | * @param string|null $type The object type, if any. |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | public function trashLink(?string $type): string |
||
| 411 | { |
||
| 412 | if (empty($type) || $type === 'trash' || $type === 'translations') { |
||
| 413 | return ''; |
||
| 414 | } |
||
| 415 | |||
| 416 | $modules = (array)$this->_View->get('modules'); |
||
| 417 | if (!Hash::check($modules, sprintf('%s.hints.object_type', $type))) { |
||
| 418 | return ''; |
||
| 419 | } |
||
| 420 | |||
| 421 | $classes = sprintf('button icon icon-only-icon has-text-module-%s', $type); |
||
| 422 | $title = $this->tr($type) . __(' in Trashcan'); |
||
| 423 | $filter = ['type' => [$type]]; |
||
| 424 | |||
| 425 | return $this->Html->link( |
||
| 426 | sprintf('<span class="is-sr-only">%s</span><app-icon icon="carbon:trash-can"></app-icon>', __('Trash')), |
||
| 427 | ['_name' => 'trash:list', '?' => compact('filter')], |
||
| 428 | ['class' => $classes, 'title' => $title, 'escape' => false] |
||
| 429 | ); |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Return properties group for given property |
||
| 434 | * |
||
| 435 | * @param string $needle The property to search |
||
| 436 | * @return ?string |
||
| 437 | */ |
||
| 438 | public function propertyGroup(string $needle): ?string |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Return list of properties to display in `index` view per modules |
||
| 453 | * |
||
| 454 | * @return array |
||
| 455 | */ |
||
| 456 | public function indexLists(): array |
||
| 457 | { |
||
| 458 | $cacheKey = CacheTools::cacheKey('properties.indexLists'); |
||
| 478 | } |
||
| 479 | } |
||
| 480 |