Skip to content
Code-Schnipsel Gruppen Projekte
Timeline.php 4,63 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    
    /**
     * This file contains QUI\Timeline\Controls\Timeline
     */
    
    namespace QUI\Timeline\Controls;
    
    use QUI;
    
    use QUI\Exception;
    
    use QUI\Projects\Site\Utils;
    
    /**
     * Class Timeline
     *
     * @package quiqqer/timeline
     */
    class Timeline extends QUI\Control
    {
        /**
         * constructor
         *
         * @param array $attributes
         */
    
        public function __construct(array $attributes = [])
    
            $this->setAttributes([
                'nodeName' => 'section',
                'class' => 'timeline-section',
                'order' => 'c_date ASC',
    
                'parentInputList' => false, //todo später für brick
    
                'showLinks' => true,
                'display' => 'VerticalBothSides',
    
                'limit' => 10,
                'imageFillMode' => 'cover', // cover / contain
    
                // Custom children template (path to html file); overwrites "display"
                'displayTemplate' => false,
                // Custom children template css (path to css file); overwrites "display"
    
                'displayCss' => false,
                'itemtype' => 'http://schema.org/ItemList',
                'child-itemtype' => 'https://schema.org/ListItem',
                'child-itemprop' => 'itemListElement'
            ]);
    
    
            parent::__construct($attributes);
        }
    
        /**
         * Return the inner body of the element
         * Can be overwritten
         *
         * @return String
         */
    
        public function getBody(): string
    
            $Engine = QUI::getTemplateManager()->getEngine();
            $Site = $this->getSite();
    
            $Project = $this->getProject();
    
            if (!$Site && !$this->getAttribute('parentInputList')) {
                return '';
            }
    
    
            $limit = $this->getAttribute('limit') ?: 10;
            $parents = $this->getAttribute('parentInputList') ?: $Site->getId();
    
    
            // only active sites
            $where['active'] = 1;
    
            if ($this->getAttribute('parentInputList')) {
                // for bricks
    
                $children = Utils::getSitesByInputList($Project, $parents, [
    
                    'order' => $this->getAttribute('order'),
                    'limit' => $limit,
    
                $children = $Site->getChildren([
    
                    'order' => $this->getAttribute('order'),
                    'limit' => $limit,
    
            $Engine->assign([
                'this' => $this,
                'Site' => $Site,
                'Project' => $this->getProject(),
                'children' => $children,
    
                'showLinks' => $this->getAttribute('showLinks'),
    
                'imageFit' => $this->getAttribute('imageFillMode')
    
            if ($this->getAttribute('imageFillMode')) {
                $this->setCustomVariable('imageFillMode', $this->getAttribute('imageFillMode'));
            }
    
    
            if (
                $this->getAttribute('displayTemplate')
    
                && file_exists($this->getAttribute('displayTemplate'))
            ) {
    
                if (
                    $this->getAttribute('displayCss')
    
                    && file_exists($this->getAttribute('displayCss'))
                ) {
                    $this->addCSSFile($this->getAttribute('displayCss'));
                }
    
                return $Engine->fetch($this->getAttribute('displayTemplate'));
            }
    
    
            // Load default template
            $cssFile = dirname(__FILE__) . '/Timeline.' . $this->getAttribute('display') . '.css';
            $templateFile = dirname(__FILE__) . '/Timeline.' . $this->getAttribute('display') . '.html';
    
            $this->addCSSFile($cssFile);
    
            return $Engine->fetch($templateFile);
    
         * @return null|QUI\Projects\Site
    
         * @throws Exception
    
        protected function getSite(): ?QUI\Interfaces\Projects\Site
    
        {
            if ($this->getAttribute('Site')) {
                return $this->getAttribute('Site');
            }
    
            $Site = QUI::getRewrite()->getSite();
    
            $this->setAttribute('Site', $Site);
    
            return $Site;
        }
    
    
        /**
         * Set custom css variable to the control as inline style
         *   --_qui-timeline-timeline-$name: var(--qui-timeline-timeline-$name, $value);
         *
         * Example:
         *   --_qui-timeline-timeline-imageFillMode: var(--qui-timeline-timeline-imageFillMode, 'cover');
         *
         * @param string $name
         * @param string $value
         *
         * @return void
         */
        private function setCustomVariable(string $name, string $value): void
        {
            if (!$name || !$value) {
                return;
            }
    
            $this->setStyle(
                '--_qui-timeline-timeline-' . $name,
                'var(--qui-timeline-timeline-' . $name . ', ' . $value . ')'
            );
        }