vendor/netzmacht/contao-toolkit/src/Component/ContentElement/AbstractContentElement.php line 58

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Netzmacht\Contao\Toolkit\Component\ContentElement;
  4. use Contao\Database\Result;
  5. use Contao\Model;
  6. use Contao\Model\Collection;
  7. use InvalidArgumentException;
  8. use Netzmacht\Contao\Toolkit\Component\AbstractComponent;
  9. use Netzmacht\Contao\Toolkit\Routing\RequestScopeMatcher;
  10. use Symfony\Component\Templating\EngineInterface as TemplateEngine;
  11. use function defined;
  12. use function time;
  13. use function trigger_error;
  14. use function trim;
  15. use const BE_USER_LOGGED_IN;
  16. use const E_USER_DEPRECATED;
  17. /**
  18.  * @deprecated Since 3.5.0 and get removed in 4.0.0
  19.  *
  20.  * @psalm-suppress DeprecatedClass
  21.  * @psalm-suppress DeprecatedInterface
  22.  */
  23. abstract class AbstractContentElement extends AbstractComponent implements ContentElement
  24. {
  25.     /**
  26.      * State if the request is called in the preview mode.
  27.      *
  28.      * @var bool|null
  29.      */
  30.     private $isPreviewMode;
  31.     /**
  32.      * @param Model|Collection|Result  $model               Object model or result.
  33.      * @param TemplateEngine           $templateEngine      Template engine.
  34.      * @param string                   $column              Column.
  35.      * @param RequestScopeMatcher|null $requestScopeMatcher Request scope matcher.
  36.      * @param bool|null                $isPreviewMode       State if the request is called in the preview mode.
  37.      *
  38.      * @throws InvalidArgumentException When model does not have a row method.
  39.      */
  40.     public function __construct(
  41.         $model,
  42.         TemplateEngine $templateEngine,
  43.         $column 'main',
  44.         ?RequestScopeMatcher $requestScopeMatcher null,
  45.         ?bool $isPreviewMode null
  46.     ) {
  47.         parent::__construct($model$templateEngine$column$requestScopeMatcher);
  48.         if ($isPreviewMode === null) {
  49.             // @codingStandardsIgnoreStart
  50.             @trigger_error(
  51.                 'isPreviewMode not passed as fifth argument. isPreviewMode will be required in version 4.0.0',
  52.                 E_USER_DEPRECATED
  53.             );
  54.             // @codingStandardsIgnoreEnd
  55.             $isPreviewMode defined('BE_USER_LOGGED_IN') && BE_USER_LOGGED_IN;
  56.         }
  57.         $this->isPreviewMode $isPreviewMode;
  58.     }
  59.     /** @psalm-suppress DeprecatedClass */
  60.     public function generate(): string
  61.     {
  62.         if (! $this->isVisible()) {
  63.             return '';
  64.         }
  65.         return parent::generate();
  66.     }
  67.     /**
  68.      * Check if content element is visible.
  69.      */
  70.     protected function isVisible(): bool
  71.     {
  72.         if ($this->isPreviewMode || ! $this->isFrontendRequest()) {
  73.             return true;
  74.         }
  75.         if ($this->get('invisible')) {
  76.             return false;
  77.         }
  78.         $now   time();
  79.         $start $this->get('start');
  80.         $stop  $this->get('stop');
  81.         // phpcs:disable SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedEqualOperator
  82.         return ($start == '' || $start <= $now) && ($stop == '' || $stop >= $now);
  83.         // phpcs:enable SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedEqualOperator
  84.     }
  85.     /** @psalm-suppress DeprecatedClass */
  86.     protected function compileCssClass(): string
  87.     {
  88.         return trim('ce_' $this->get('type') . ' ' parent::compileCssClass());
  89.     }
  90. }