Breadcrumb.php 9.04 KB
Newer Older
1 2 3
<?php
namespace ACP3\Core;

Tino Goratsch's avatar
Tino Goratsch committed
4
use ACP3\Core\Http\RequestInterface;
5 6 7 8 9 10 11 12 13 14 15
use Symfony\Component\DependencyInjection\Container;

/**
 * Generates the breadcrumb and page title
 * @package ACP3\Core
 */
class Breadcrumb
{
    /**
     * @var array
     */
16
    protected $steps = [];
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

    /**
     * @var array
     */
    protected $title = [
        'separator' => '-',
        'prefix' => '',
        'postfix' => ''
    ];

    /**
     * @var array
     */
    protected $breadcrumbCache = [];

    /**
     * @var \Symfony\Component\DependencyInjection\Container
     */
    protected $container;
    /**
     * @var \ACP3\Core\Lang
     */
    protected $lang;
    /**
Tino Goratsch's avatar
Tino Goratsch committed
41
     * @var \ACP3\Core\Http\RequestInterface
42 43 44 45 46 47 48 49 50
     */
    protected $request;
    /**
     * @var \ACP3\Core\Router
     */
    protected $router;
    /**
     * @var \ACP3\Core\Config
     */
51
    protected $config;
52 53 54 55

    /**
     * @param \Symfony\Component\DependencyInjection\Container $container
     * @param \ACP3\Core\Lang                                  $lang
Tino Goratsch's avatar
Tino Goratsch committed
56
     * @param \ACP3\Core\Http\RequestInterface                 $request
57 58 59 60 61 62
     * @param \ACP3\Core\Router                                $router
     * @param \ACP3\Core\Config                                $config
     */
    public function __construct(
        Container $container,
        Lang $lang,
63
        RequestInterface $request,
64 65 66 67 68 69 70 71
        Router $router,
        Config $config
    )
    {
        $this->container = $container;
        $this->lang = $lang;
        $this->request = $request;
        $this->router = $router;
72
        $this->config = $config;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    }

    /**
     *
     * @param string $value
     *
     * @return $this
     */
    public function setTitleSeparator($value)
    {
        $this->title['separator'] = $value;

        return $this;
    }

    /**
     * @return string
     */
    public function getTitleSeparator()
    {
        return ' ' . $this->title['separator'] . ' ';
    }

    /**
     *
     * @param string $value
     *
     * @return $this
     */
    public function setTitlePrefix($value)
    {
        $this->title['prefix'] = $value;

        return $this;
    }

    /**
     * Ersetzt die aktuell letzte Brotkrume mit neuen Werten
     *
     * @param string $title
     *    Bezeichnung der jeweiligen Stufe der Brotkrume
     * @param string $path
     *    Die zum $title zugehörige ACP3-interne URI
     * @param bool   $dbSteps
     *
     * @return $this
     */
    public function replaceAncestor($title, $path = '', $dbSteps = false)
    {
122 123 124 125
        if ($dbSteps === false) {
            $index = count($this->steps) - (!empty($this->steps) ? 1 : 0);
            $this->steps[$index]['title'] = $title;
            $this->steps[$index]['uri'] = !empty($path) ? $this->router->route($path) : '';
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        }

        return $this;
    }

    /**
     * Returns the breadcrumb
     *
     * @return array
     */
    public function getBreadcrumb()
    {
        if (empty($this->breadcrumbCache)) {
            $this->_setBreadcrumbCache();
        }

        return $this->breadcrumbCache;
    }

    /**
     * Returns the site title
     *
     * @return string
     */
    public function getSiteTitle()
    {
152
        return $this->config->getSettings('seo')['title'];
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    }

    /**
     * Returns the title of the current page
     *
     * @return string
     */
    public function getPageTitle()
    {
        if (empty($this->breadcrumbCache)) {
            $this->_setBreadcrumbCache();
        }

        return $this->breadcrumbCache[count($this->breadcrumbCache) - 1]['title'];
    }

    /**
     * Returns the title of the current page + the site title
     *
     * @return string
     */
    public function getSiteAndPageTitle()
    {
        $title = $this->getPageTitle();

        $separator = ' ' . $this->title['separator'] . ' ';
        if (!empty($this->title['prefix'])) {
            $title = $this->title['prefix'] . $separator . $title;
        }
        if (!empty($this->title['postfix'])) {
            $title .= $separator . $this->title['postfix'];
        }
        $title .= ' | ' . $this->getSiteTitle();

        return $title;
    }

    /**
     * Sets the breadcrumb cache for the current request
     */
    private function _setBreadcrumbCache()
    {
        // Breadcrumb of the admin panel
196
        if ($this->request->getArea() === 'admin') {
197
            $this->setBreadcrumbCacheForAdmin();
198
        } else { // Breadcrumb for frontend requests
199
            $this->setBreadcrumbCacheForFrontend();
200 201 202 203 204 205 206 207 208
        }

        // Mark the last breadcrumb
        $this->breadcrumbCache[count($this->breadcrumbCache) - 1]['last'] = true;
    }

    /**
     * Sets the breadcrumb steps cache for admin panel action requests
     */
209
    private function setBreadcrumbCacheForAdmin()
210
    {
211
        if ($this->request->getModule() !== 'acp') {
212 213 214 215 216 217 218 219
            // An postfix for the page title has been already set
            if (!empty($this->title['postfix'])) {
                $this->setTitlePostfix($this->title['postfix'] . $this->getTitleSeparator() . $this->lang->t('system', 'acp'));
            } else {
                $this->setTitlePostfix($this->lang->t('system', 'acp'));
            }
        }

220 221
        // No breadcrumb has been set yet
        if (empty($this->steps)) {
222 223
            $this->append($this->lang->t('system', 'acp'), 'acp/acp');

224 225 226 227 228 229 230
            if ($this->request->getModule() !== 'acp') {
                $this->append(
                    $this->lang->t($this->request->getModule(), $this->request->getModule()),
                    'acp/' . $this->request->getModule()
                );

                $this->setControllerActionBreadcrumbs();
231 232
            }
        } else { // Prepend breadcrumb steps, if there have been already some steps set
233 234 235 236 237
            if ($this->request->getModule() !== 'acp') {
                $this->prepend(
                    $this->lang->t($this->request->getModule(), $this->request->getModule()),
                    'acp/' . $this->request->getModule()
                );
238
            }
239

240 241
            $this->prepend($this->lang->t('system', 'acp'), 'acp/acp');
        }
242
        $this->breadcrumbCache = $this->steps;
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    }

    /**
     *
     * @param string $value
     *
     * @return $this
     */
    public function setTitlePostfix($value)
    {
        $this->title['postfix'] = $value;

        return $this;
    }

    /**
     * Zuweisung einer neuen Stufe zur Brotkrümelspur
     *
     * @param string $title
     *    Bezeichnung der jeweiligen Stufe der Brotkrume
     * @param string $path
     *    Die zum $title zugehörige ACP3-interne URI
     *
     * @return $this
     */
    public function append($title, $path = '')
    {
270
        $this->steps[] = [
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
            'title' => $title,
            'uri' => !empty($path) ? $this->router->route($path) : ''
        ];

        return $this;
    }

    /**
     * Fügt Brotkrumen an den Anfang an
     *
     * @param string $title
     *    Bezeichnung der jeweiligen Stufe der Brotkrume
     * @param string $path
     *    Die zum $title zugehörige ACP3-interne URI
     *
     * @return $this
     */
    protected function prepend($title, $path)
    {
        $step = [
            'title' => $title,
            'uri' => $this->router->route($path)
        ];
294
        array_unshift($this->steps, $step);
295 296 297 298 299 300
        return $this;
    }

    /**
     * Sets the breadcrumb steps cache for frontend action requests
     */
301
    protected function setBreadcrumbCacheForFrontend()
302 303
    {
        // No breadcrumb has been set yet
304 305 306 307 308 309
        if (empty($this->steps)) {
            if ($this->request->getModule() !== 'errors') {
                $this->append(
                    $this->lang->t($this->request->getModule(), $this->request->getModule()),
                    $this->request->getModule()
                );
310 311
            }

312
            $this->setControllerActionBreadcrumbs();
313 314
        }

315 316
        $this->breadcrumbCache = $this->steps;
    }
317

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    private function setControllerActionBreadcrumbs()
    {
        $serviceId = $this->request->getModule() . '.controller.' . $this->request->getArea() . '.' . $this->request->getController();
        if ($this->request->getController() !== 'index' &&
            method_exists($this->container->get($serviceId), 'actionIndex')
        ) {
            $this->append(
                $this->lang->t($this->request->getModule(), $this->getControllerIndexActionTitle()),
                $this->request->getModuleAndController()
            );
        }
        if ($this->request->getControllerAction() !== 'index') {
            $this->append(
                $this->lang->t($this->request->getModule(), $this->getControllerActionTitle()),
                $this->request->getFullPath()
            );
334 335
        }
    }
336 337 338 339 340 341 342 343 344 345 346 347 348 349

    /**
     * @return string
     */
    private function getControllerActionTitle()
    {
        return $this->request->getArea() . '_' . $this->request->getController() . '_' . $this->request->getControllerAction();
    }

    /**
     * @return string
     */
    private function getControllerIndexActionTitle()
    {
350
        return $this->request->getArea() . '_' . $this->request->getController() . '_index';
351
    }
352
}