modules/CDev/SimpleCMS/src/Model/Page.php line 14

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace CDev\SimpleCMS\Model;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use XLite\Core\Layout;
  9. /**
  10.  * @ORM\Entity
  11.  * @ORM\Table  (name="pages",
  12.  *      indexes={
  13.  *          @ORM\Index (name="enabled", columns={"enabled"}),
  14.  *      }
  15.  * )
  16.  */
  17. class Page extends \XLite\Model\Base\Catalog
  18. {
  19.     public const TYPE_PRIMARY 'primary';
  20.     public const TYPE_DEFAULT 'content';
  21.     public const TYPE_SERVICE 'service';
  22.     /**
  23.      * Unique ID
  24.      *
  25.      * @var integer
  26.      *
  27.      * @ORM\Id
  28.      * @ORM\GeneratedValue (strategy="AUTO")
  29.      * @ORM\Column         (type="integer", options={ "unsigned": true })
  30.      */
  31.     protected $id;
  32.     /**
  33.      * Is menu enabled or not
  34.      *
  35.      * @var boolean
  36.      *
  37.      * @ORM\Column (type="boolean")
  38.      */
  39.     protected $enabled true;
  40.     /**
  41.      * One-to-one relation with page_images table
  42.      *
  43.      * @var \CDev\SimpleCMS\Model\Image\Page\Image
  44.      *
  45.      * @ORM\OneToOne  (targetEntity="CDev\SimpleCMS\Model\Image\Page\Image", mappedBy="page", cascade={"all"})
  46.      */
  47.     protected $image;
  48.     /**
  49.      * Clean URLs
  50.      *
  51.      * @var \Doctrine\Common\Collections\Collection
  52.      *
  53.      * @ORM\OneToMany (targetEntity="XLite\Model\CleanURL", mappedBy="page", cascade={"all"})
  54.      * @ORM\OrderBy   ({"id" = "ASC"})
  55.      */
  56.     protected $cleanURLs;
  57.     /**
  58.      * Meta description type
  59.      *
  60.      * @var string
  61.      *
  62.      * @ORM\Column (type="string", length=1)
  63.      */
  64.     protected $metaDescType 'A';
  65.     /**
  66.      * Tab position
  67.      *
  68.      * @var integer
  69.      *
  70.      * @ORM\Column (type="integer")
  71.      */
  72.     protected $position 0;
  73.     /**
  74.      * @var string
  75.      *
  76.      * @ORM\Column (type="string", length=10, options={"default": "content"})
  77.      */
  78.     protected $type self::TYPE_DEFAULT;
  79.     /**
  80.      * @var string
  81.      *
  82.      * @ORM\Column (type="string", length=10, options={"default": "default"})
  83.      */
  84.     protected $layoutGroup Layout::LAYOUT_GROUP_DEFAULT;
  85.     /**
  86.      * @var string
  87.      *
  88.      * @ORM\Column (type="string", length=255, nullable = true)
  89.      */
  90.     protected $adminUrl;
  91.     /**
  92.      * @var string
  93.      *
  94.      * @ORM\Column (type="string", length=255, nullable = true)
  95.      */
  96.     protected $frontUrl;
  97.     /**
  98.      * @var string
  99.      *
  100.      * @ORM\Column (type="string", length=255, nullable = true)
  101.      */
  102.     protected $tooltipText;
  103.     /**
  104.      * @var string
  105.      *
  106.      * @ORM\Column (type="string", length=128, nullable = true)
  107.      */
  108.     protected $module;
  109.     /**
  110.      * @var \Doctrine\Common\Collections\Collection
  111.      *
  112.      * @ORM\OneToMany (targetEntity="CDev\SimpleCMS\Model\PageTranslation", mappedBy="owner", cascade={"all"})
  113.      */
  114.     protected $translations;
  115.     /**
  116.      * Returns meta description
  117.      * todo: rename to getMetaDesc()
  118.      *
  119.      * @return string
  120.      */
  121.     public function getTeaser()
  122.     {
  123.         return $this->getMetaDescType() === 'A' || !$this->getSoftTranslation()->getTeaser()
  124.             ? static::postprocessMetaDescription($this->getBody())
  125.             : $this->getSoftTranslation()->getTeaser();
  126.     }
  127.     /**
  128.      * Returns meta description type
  129.      *
  130.      * @return string
  131.      */
  132.     public function getMetaDescType()
  133.     {
  134.         $result $this->metaDescType;
  135.         if (!$result) {
  136.             $metaDescPresent array_reduce($this->getTranslations()->toArray(), static function ($carry$item) {
  137.                 return $carry ?: (bool) $item->getMetaDesc();
  138.             }, false);
  139.             $result $metaDescPresent 'C' 'A';
  140.         }
  141.         return $result;
  142.     }
  143.     /**
  144.      * Get id
  145.      *
  146.      * @return integer
  147.      */
  148.     public function getId()
  149.     {
  150.         return $this->id;
  151.     }
  152.     /**
  153.      * Set enabled
  154.      *
  155.      * @param boolean $enabled
  156.      * @return Page
  157.      */
  158.     public function setEnabled($enabled)
  159.     {
  160.         $this->enabled = (bool)$enabled;
  161.         return $this;
  162.     }
  163.     /**
  164.      * Get enabled
  165.      *
  166.      * @return boolean
  167.      */
  168.     public function getEnabled()
  169.     {
  170.         return $this->enabled;
  171.     }
  172.     /**
  173.      * Set metaDescType
  174.      *
  175.      * @param string $metaDescType
  176.      * @return Page
  177.      */
  178.     public function setMetaDescType($metaDescType)
  179.     {
  180.         $this->metaDescType $metaDescType;
  181.         return $this;
  182.     }
  183.     /**
  184.      * Set image
  185.      *
  186.      * @param \CDev\SimpleCMS\Model\Image\Page\Image $image
  187.      * @return Page
  188.      */
  189.     public function setImage(\CDev\SimpleCMS\Model\Image\Page\Image $image null)
  190.     {
  191.         $this->image $image;
  192.         return $this;
  193.     }
  194.     /**
  195.      * Get image
  196.      *
  197.      * @return \CDev\SimpleCMS\Model\Image\Page\Image
  198.      */
  199.     public function getImage()
  200.     {
  201.         return $this->image;
  202.     }
  203.     /**
  204.      * Add cleanURLs
  205.      *
  206.      * @param \XLite\Model\CleanURL $cleanURLs
  207.      * @return Page
  208.      */
  209.     public function addCleanURLs(\XLite\Model\CleanURL $cleanURLs)
  210.     {
  211.         $this->cleanURLs[] = $cleanURLs;
  212.         return $this;
  213.     }
  214.     /**
  215.      * Get cleanURLs
  216.      *
  217.      * @return \Doctrine\Common\Collections\Collection
  218.      */
  219.     public function getCleanURLs()
  220.     {
  221.         return $this->cleanURLs;
  222.     }
  223.     /**
  224.      * Return Position
  225.      *
  226.      * @return int
  227.      */
  228.     public function getPosition()
  229.     {
  230.         return $this->position;
  231.     }
  232.     /**
  233.      * Set Position
  234.      *
  235.      * @param int $position
  236.      *
  237.      * @return $this
  238.      */
  239.     public function setPosition($position)
  240.     {
  241.         $this->position $position;
  242.         return $this;
  243.     }
  244.     /**
  245.      * @return string
  246.      */
  247.     public function getType()
  248.     {
  249.         return $this->type;
  250.     }
  251.     /**
  252.      * @param string $type
  253.      * @return Page
  254.      */
  255.     public function setType($type)
  256.     {
  257.         $this->type $type;
  258.         return $this;
  259.     }
  260.     /**
  261.      * @return string
  262.      */
  263.     public function getLayoutGroup()
  264.     {
  265.         return $this->layoutGroup;
  266.     }
  267.     /**
  268.      * @param string $layoutGroup
  269.      * @return Page
  270.      */
  271.     public function setLayoutGroup($layoutGroup)
  272.     {
  273.         $this->layoutGroup $layoutGroup;
  274.         return $this;
  275.     }
  276.     /**
  277.      * @return string
  278.      */
  279.     public function getFrontUrl()
  280.     {
  281.         return $this->frontUrl;
  282.     }
  283.     /**
  284.      * @return string
  285.      */
  286.     public function getAdminUrl()
  287.     {
  288.         return $this->adminUrl;
  289.     }
  290.     /**
  291.      * @return string|null
  292.      */
  293.     public function getModule()
  294.     {
  295.         return $this->module;
  296.     }
  297.     /**
  298.      * @return bool
  299.      */
  300.     public function isPrimaryPage()
  301.     {
  302.         return $this->getType() === self::TYPE_PRIMARY;
  303.     }
  304.     /**
  305.      * @return bool
  306.      */
  307.     public function isServicePage()
  308.     {
  309.         return $this->getType() === self::TYPE_SERVICE;
  310.     }
  311.     /**
  312.      * @return string
  313.      */
  314.     public function getLayoutType()
  315.     {
  316.         return Layout::getInstance()->getLayoutTypeByGroup($this->getLayoutGroup());
  317.     }
  318.     /**
  319.      * @return string
  320.      */
  321.     public function getLayoutTypeImageUrl()
  322.     {
  323.         return Layout::getInstance()->getResourceWebPath('modules/CDev/SimpleCMS/items_list/cells/layout/images/' $this->getLayoutType() . '.svg');
  324.     }
  325.     /**
  326.      * @return string
  327.      */
  328.     public function getTooltipText()
  329.     {
  330.         return $this->tooltipText;
  331.     }
  332.     /**
  333.      * @return bool
  334.      */
  335.     public function isCategoryPage()
  336.     {
  337.         return str_contains($this->getFrontUrl() ?? '''target=category');
  338.     }
  339.     /**
  340.      * @return bool
  341.      */
  342.     public function isProductPage()
  343.     {
  344.         return str_contains($this->getFrontUrl() ?? '''target=product');
  345.     }
  346.     /**
  347.      * @return bool
  348.      */
  349.     public function isFrontPage()
  350.     {
  351.         return $this->getFrontUrl() === '/';
  352.     }
  353.     /**
  354.      * @return bool
  355.      */
  356.     public function isBrandPage()
  357.     {
  358.         return str_contains($this->getFrontUrl() ?? '''target=brand');
  359.     }
  360.     // {{{ Translation Getters / setters
  361.     /**
  362.      * @param string $teaser
  363.      *
  364.      * @return \XLite\Model\Base\Translation
  365.      */
  366.     public function setTeaser($teaser)
  367.     {
  368.         return $this->setTranslationField(__FUNCTION__$teaser);
  369.     }
  370.     /**
  371.      * @return string
  372.      */
  373.     public function getBody()
  374.     {
  375.         return $this->getTranslationField(__FUNCTION__);
  376.     }
  377.     /**
  378.      * @param string $body
  379.      *
  380.      * @return \XLite\Model\Base\Translation
  381.      */
  382.     public function setBody($body)
  383.     {
  384.         return $this->setTranslationField(__FUNCTION__$body);
  385.     }
  386.     /**
  387.      * @return string
  388.      */
  389.     public function getMetaKeywords()
  390.     {
  391.         return $this->getTranslationField(__FUNCTION__);
  392.     }
  393.     /**
  394.      * @param string $metaKeywords
  395.      *
  396.      * @return \XLite\Model\Base\Translation
  397.      */
  398.     public function setMetaKeywords($metaKeywords)
  399.     {
  400.         return $this->setTranslationField(__FUNCTION__$metaKeywords);
  401.     }
  402.     /**
  403.      * @return string
  404.      */
  405.     public function getMetaTitle()
  406.     {
  407.         return $this->getTranslationField(__FUNCTION__);
  408.     }
  409.     /**
  410.      * @param string $metaTitle
  411.      *
  412.      * @return \XLite\Model\Base\Translation
  413.      */
  414.     public function setMetaTitle($metaTitle)
  415.     {
  416.         return $this->setTranslationField(__FUNCTION__$metaTitle);
  417.     }
  418.     /**
  419.      * @return bool
  420.      */
  421.     public function isOneItemPage()
  422.     {
  423.         return $this->isCategoryPage()
  424.             || $this->isProductPage()
  425.             || $this->isBrandPage();
  426.     }
  427.     /**
  428.      * @return string
  429.      */
  430.     public function getCleanURLbyFrontURL()
  431.     {
  432.         $result '';
  433.         $frontUrl $this->getFrontUrl();
  434.         if (
  435.             $frontUrl
  436.             && !$this->isOneItemPage()
  437.             && !$this->isFrontPage()
  438.         ) {
  439.             $urlParams = [];
  440.             $urlData explode('&'str_replace('?'''$frontUrl));
  441.             if (is_string($urlData)) {
  442.                 $urlData = [$urlData];
  443.             }
  444.             foreach ($urlData as $keyval) {
  445.                 $keyval explode('='$keyval);
  446.                 if (count($keyval) == 2) {
  447.                     $name $keyval[0];
  448.                     $value $keyval[1];
  449.                     $urlParams[$name] = $value;
  450.                 }
  451.             }
  452.             if ($urlParams && isset($urlParams['target'])) {
  453.                 $target $urlParams['target'];
  454.                 $action $urlParams['action'] ?? '';
  455.                 unset($urlParams['target'], $urlParams['action']);
  456.                 $result \XLite\Core\Converter::buildCleanURL($target$action$urlParams);
  457.             }
  458.         }
  459.         return $result;
  460.     }
  461.     /**
  462.      * @return bool
  463.      */
  464.     public function isIncludeToSitemap()
  465.     {
  466.         return !$this->isOneItemPage()
  467.             && !$this->isFrontPage()
  468.             && !$this->isServicePage();
  469.     }
  470.     // }}}
  471. }