Schlaefer /
Saito
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | /** |
||||
| 6 | * Saito - The Threaded Web Forum |
||||
| 7 | * |
||||
| 8 | * @copyright Copyright (c) the Saito Project Developers |
||||
| 9 | * @link https://github.com/Schlaefer/Saito |
||||
| 10 | * @license http://opensource.org/licenses/MIT |
||||
| 11 | */ |
||||
| 12 | |||||
| 13 | namespace App\Controller\Component; |
||||
| 14 | |||||
| 15 | use App\Controller\ErrorController; |
||||
| 16 | use Cake\Controller\Component; |
||||
| 17 | use Cake\Controller\Controller; |
||||
| 18 | use Cake\Core\Configure; |
||||
| 19 | use Cake\Event\Event; |
||||
| 20 | use Cake\Utility\Text; |
||||
| 21 | use Saito\Posting\Posting; |
||||
| 22 | |||||
| 23 | class TitleComponent extends Component |
||||
| 24 | { |
||||
| 25 | /** |
||||
| 26 | * {@inheritDoc} |
||||
| 27 | */ |
||||
| 28 | public function beforeRender(Event $event) |
||||
| 29 | { |
||||
| 30 | $controller = $event->getSubject(); |
||||
| 31 | |||||
| 32 | $forum = $this->getForumName(); |
||||
| 33 | $controller->set('forumName', $forum); |
||||
| 34 | |||||
| 35 | if ($controller instanceof ErrorController) { |
||||
| 36 | return; |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | $page = $this->getPageTitle($controller); |
||||
| 40 | $title = $this->getTitleForLayout($controller, $page, $forum); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 41 | $controller->set(['titleForLayout' => $title, 'titleForPage' => $page ]); |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * Get title for page shown on header on page |
||||
| 46 | * |
||||
| 47 | * @param Controller $controller The controller |
||||
| 48 | * @return string |
||||
| 49 | */ |
||||
| 50 | protected function getPageTitle(Controller $controller): string |
||||
|
0 ignored issues
–
show
The parameter
$controller is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 51 | { |
||||
| 52 | $controller = $this->getController(); |
||||
| 53 | //= title for page, shown in default.ctp in header on page |
||||
| 54 | if (isset($controller->viewVars['titleForPage'])) { |
||||
| 55 | return $controller->viewVars['titleForPage']; |
||||
| 56 | } |
||||
| 57 | |||||
| 58 | $ctrler = $controller->request->getParam('controller'); |
||||
| 59 | $action = $controller->request->getParam('action'); |
||||
| 60 | $key = lcfirst($ctrler) . '/' . $action; |
||||
| 61 | $page = __d('page_titles', $key); |
||||
| 62 | if ($key === $page) { |
||||
| 63 | $page = ''; |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | return $page; |
||||
|
0 ignored issues
–
show
|
|||||
| 67 | } |
||||
| 68 | |||||
| 69 | /** |
||||
| 70 | * Gets forum name |
||||
| 71 | * |
||||
| 72 | * @return string|null |
||||
| 73 | */ |
||||
| 74 | public function getForumName(): ?string |
||||
| 75 | { |
||||
| 76 | return Configure::read('Saito.Settings.forum_name'); |
||||
| 77 | } |
||||
| 78 | |||||
| 79 | /** |
||||
| 80 | * title + forum name for layout, shown in HTML-<title>-tag |
||||
| 81 | * |
||||
| 82 | * @param Controller $controller The controller |
||||
| 83 | * @param string $page Title of the current page. |
||||
| 84 | * @param string $forum Title of the forum. |
||||
| 85 | * @return string |
||||
| 86 | */ |
||||
| 87 | protected function getTitleForLayout(Controller $controller, string $page, string $forum): string |
||||
| 88 | { |
||||
| 89 | if (isset($controller->viewVars['titleForLayout'])) { |
||||
| 90 | $layout = $controller->viewVars['titleForLayout']; |
||||
| 91 | } else { |
||||
| 92 | $layout = $page; |
||||
| 93 | } |
||||
| 94 | if ($layout) { |
||||
| 95 | $layout = Text::insert( |
||||
| 96 | __('forum-title-template'), |
||||
| 97 | ['page' => $layout, 'forum' => $forum] |
||||
| 98 | ); |
||||
| 99 | } else { |
||||
| 100 | $layout = $forum; |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | return $layout; |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | /** |
||||
| 107 | * Set title |
||||
| 108 | * |
||||
| 109 | * @param Posting $posting posting |
||||
| 110 | * @param string $type type |
||||
| 111 | * @return void |
||||
| 112 | */ |
||||
| 113 | public function setFromPosting(Posting $posting, $type = null) |
||||
| 114 | { |
||||
| 115 | if ($type === null) { |
||||
| 116 | $template = __(':subject | :category'); |
||||
| 117 | } else { |
||||
| 118 | $template = __(':subject (:type) | :category'); |
||||
| 119 | } |
||||
| 120 | $this->getController()->set( |
||||
| 121 | 'titleForLayout', |
||||
| 122 | Text::insert( |
||||
| 123 | $template, |
||||
| 124 | [ |
||||
| 125 | 'category' => $posting->get('category')['category'], |
||||
| 126 | 'subject' => $posting->get('subject'), |
||||
| 127 | 'type' => $type, |
||||
| 128 | ] |
||||
| 129 | ) |
||||
| 130 | ); |
||||
| 131 | } |
||||
| 132 | } |
||||
| 133 |