Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
70.83% |
34 / 48 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| FrontEndSiteComponent | |
70.83% |
34 / 48 |
|
0.00% |
0 / 1 |
15.57 | |
0.00% |
0 / 1 |
| beforeRender | |
70.83% |
34 / 48 |
|
0.00% |
0 / 1 |
15.57 | |||
| 1 | <?php |
| 2 | namespace DefaultTheme\Controller\Component; |
| 3 | |
| 4 | use Cake\Controller\Component; |
| 5 | use Cake\Event\EventInterface; |
| 6 | use App\Utility\I18nManager; |
| 7 | use App\Utility\SettingsManager; |
| 8 | use Cake\Core\Configure; |
| 9 | |
| 10 | /** |
| 11 | * FrontEndSiteComponent |
| 12 | * |
| 13 | * This component is responsible for preparing and setting up data |
| 14 | * for the front-end of the site, specifically the article tree and tag list. |
| 15 | * It automatically runs before rendering non-admin pages. |
| 16 | */ |
| 17 | class FrontEndSiteComponent extends Component |
| 18 | { |
| 19 | /** |
| 20 | * Default configuration. |
| 21 | * |
| 22 | * Defines the events this component listens to. |
| 23 | * |
| 24 | * @var array |
| 25 | */ |
| 26 | protected array $_defaultConfig = [ |
| 27 | 'implementedEvents' => [ |
| 28 | 'Controller.beforeRender' => 'beforeRender' |
| 29 | ] |
| 30 | ]; |
| 31 | |
| 32 | /** |
| 33 | * Before render callback. |
| 34 | * |
| 35 | * This method is automatically called before the view is rendered |
| 36 | * using the DefaultTheme. It prepares and sets data |
| 37 | * for use in the views. |
| 38 | * |
| 39 | * @param \Cake\Event\EventInterface $event The beforeRender event that was fired. |
| 40 | * @return void |
| 41 | */ |
| 42 | public function beforeRender(EventInterface $event): void |
| 43 | { |
| 44 | $controller = $this->getController(); |
| 45 | $request = $controller->getRequest(); |
| 46 | |
| 47 | // Skip processing for admin routes |
| 48 | if ($request->getParam('prefix') === 'Admin') { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | // Skip processing for certain user actions during tests |
| 53 | if (Configure::read('debug') && $request->getParam('controller') === 'Users') { |
| 54 | $skipActions = ['login', 'logout', 'register', 'edit', 'forgotPassword', 'resetPassword', 'confirmEmail']; |
| 55 | if (in_array($request->getParam('action'), $skipActions)) { |
| 56 | // Set minimal required variables |
| 57 | // Note: Featured articles and archives are now handled by View Cells |
| 58 | $controller->set([ |
| 59 | 'menuPages' => [], |
| 60 | 'rootTags' => [], |
| 61 | 'siteLanguages' => I18nManager::getEnabledLanguages(), |
| 62 | 'selectedSiteLanguage' => $request->getParam('lang', 'en') |
| 63 | ]); |
| 64 | return; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | $cacheKey = $controller->cacheKey; |
| 69 | $articlesTable = $controller->fetchTable('Articles'); |
| 70 | $tagsTable = $controller->fetchTable('Tags'); |
| 71 | |
| 72 | // Fetch navigation data (still needed for menu elements) |
| 73 | $menuPages = []; |
| 74 | switch(SettingsManager::read('SitePages.mainMenuShow', 'root')) { |
| 75 | case "root": |
| 76 | $menuPages = $articlesTable->getRootPages($cacheKey); |
| 77 | break; |
| 78 | case "selected": |
| 79 | $menuPages = $articlesTable->getMainMenuPages($cacheKey); |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | $rootTags = []; |
| 84 | switch(SettingsManager::read('SitePages.mainTagMenuShow', 'root')) { |
| 85 | case "root": |
| 86 | $rootTags = $tagsTable->getRootTags($cacheKey); |
| 87 | break; |
| 88 | case "selected": |
| 89 | $rootTags = $tagsTable->getMainMenuTags($cacheKey); |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | // Fetch privacy policy for footer |
| 94 | $privacyPolicyId = SettingsManager::read('SitePages.privacyPolicy', null); |
| 95 | if ($privacyPolicyId && $privacyPolicyId != 'None') { |
| 96 | $privacyPolicy = $articlesTable->find() |
| 97 | ->select(['id', 'title', 'slug']) |
| 98 | ->where(['id' => $privacyPolicyId]) |
| 99 | ->cache($cacheKey . 'priv_page', 'content') |
| 100 | ->first(); |
| 101 | |
| 102 | if ($privacyPolicy) { |
| 103 | $controller->set('sitePrivacyPolicy', $privacyPolicy->toArray()); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Set navigation data |
| 108 | // Note: Featured articles and archives are now handled by View Cells |
| 109 | $controller->set(compact( |
| 110 | 'menuPages', |
| 111 | 'rootTags', |
| 112 | )); |
| 113 | |
| 114 | $controller->set('siteLanguages', I18nManager::getEnabledLanguages()); |
| 115 | $controller->set('selectedSiteLanguage', $request->getParam('lang', 'en')); |
| 116 | } |
| 117 | } |