<?php

namespace AppBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * Page
 *
 * @ORM\Table(name="page", uniqueConstraints={@ORM\UniqueConstraint(name="keyword", columns={"keyword"})}, indexes={@ORM\Index(name="FK_page_travel_country", columns={"country_id"})})
 * @ORM\Entity(repositoryClass="AppBundle\Entity\PageRepository")
 * @Gedmo\Tree(type="nested")
 */
class Page
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @Gedmo\TreeLeft
     * @ORM\Column(type="integer", nullable=true)
     */
    protected $lft;

    /**
     * @Gedmo\TreeLevel()
     * @ORM\Column(type="integer", nullable=true)
     */
    protected $lvl;

    /**
     * @Gedmo\TreeRight
     * @ORM\Column(type="integer", nullable=true)
     */
    protected $rgt;

    /**
     * @Gedmo\TreeRoot
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Page")
     * @ORM\JoinColumn(name="tree_root", referencedColumnName="id", onDelete="CASCADE")
     */
    private $root;

    /**
     * @Gedmo\TreeParent
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Page", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $parent;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Page", mappedBy="parent")
     * @ORM\OrderBy({"lft" = "ASC"})
     */
    private $children;

    /**
     * @var integer
     *
     * @ORM\Column(name="owner", type="integer", nullable=true)
     */
    private $owner = '0';

    /**
     * @var integer
     *
     * @ORM\Column(name="owner_second", type="integer", nullable=false)
     */
    private $ownerSecond;

    /**
     * @var integer
     *
     * @ORM\Column(name="catalog_id", type="integer", nullable=true)
     */
    private $catalogId;

    /**
     * @var integer
     *
     * @ORM\Column(name="catalog_index", type="integer", nullable=true)
     */
    private $catalogIndex = '0';

    /**
     * @var string
     *
     * @ORM\Column(name="slug", type="string", length=255, nullable=true)
     */
    private $slug;

    /**
     * @var TravelProgram
     *
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\TravelProgram", inversedBy="page")
     * @ORM\JoinColumn(name="travel_program", referencedColumnName="id", onDelete="SET NULL")
     */
    private $travelProgram;

    /**
     * @var FewoLodging
     *
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\FewoLodging", inversedBy="page")
     * @ORM\JoinColumn(name="fewo_lodging", referencedColumnName="id", onDelete="SET NULL")
     */
    private $fewoLodging;

    /**
     * @var integer
     *
     * @ORM\Column(name="status", type="integer", nullable=true)
     */
    private $status = '1';

    /**
     * @var integer
     *
     * @ORM\Column(name="show_in_navi", type="integer", nullable=true)
     */
    private $showInNavi = '1';

    /**
     * @var integer
     *
     * @ORM\Column(name="order", type="integer", nullable=true)
     */
    private $order = '0';

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255, nullable=true)
     */
    private $title;

    /**
     * @var string
     *
     * @ORM\Column(name="pagetitle", type="string", length=255, nullable=true)
     */
    private $pagetitle;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text", length=65535, nullable=true)
     */
    private $description;

    /**
     * @var string
     *
     * @ORM\Column(name="keywords", type="text", length=65535, nullable=true)
     */
    private $keywords;

    /**
     * @var string
     *
     * @ORM\Column(name="content", type="text", length=65535, nullable=true)
     */
    private $content;

    /**
     * @ORM\Column(type="text", length=65535, nullable=true)
     *
     * @todo Remove this field as soon as the new page is launched and instead use "content" again
     */
    protected $contentNew;

    /**
     * @var string
     *
     * @ORM\Column(name="buma_destination", type="string", length=255, nullable=true)
     */
    private $bumaDestination;

    /**
     * @var integer
     *
     * @ORM\Column(name="OLD_CatalogID", type="integer", nullable=true)
     */
    private $oldCatalogid;

    /**
     * @var integer
     *
     * @ORM\Column(name="OLD_OwnerID", type="integer", nullable=true)
     */
    private $oldOwnerid;

    /**
     * @var integer
     *
     * @ORM\Column(name="buma_gjr", type="integer", nullable=true)
     */
    private $bumaGjr;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="date", type="datetime", nullable=false)
     */
    private $date;

    /**
     * @var boolean
     *
     * @ORM\Column(name="`price-tags`", type="boolean", nullable=false)
     */
    private $priceTags = '0';

    /**
     * @var string
     *
     * @ORM\Column(name="text_right", type="text", length=65535, nullable=true)
     */
    private $textRight;

    /**
     * @var string
     *
     * @ORM\Column(name="keyword", type="string", length=255, nullable=true)
     */
    private $keyword;

    /**
     * @var string
     *
     * @ORM\Column(name="canonical_url", type="string", length=511, nullable=true)
     */
    private $canonicalUrl;

    /**
     * @var \AppBundle\Entity\TravelCountry
     *
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\TravelCountry")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="country_id", referencedColumnName="id")
     * })
     */
    private $country;

    /**
     * @ORM\Column(type="string", length=80, nullable=true)
     */
    protected $template;

    /**
     * @ORM\Column(type="string", length=200, nullable=true, unique=true)
     */
    protected $realUrlPath;
    
    /**
     * @ORM\Column(type="string", length=65535, nullable=true)
     */
    protected $boxBody;
    
    /**
     * @ORM\Column(type="string", length=200, nullable=true)
     */
    protected $boxImageUrl;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $boxStar;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $boxDiscount;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    protected $cmsSettings;

    /**
     * Set owner
     *
     * @param integer $owner
     *
     * @return Page
     */
    public function setOwner($owner)
    {
        $this->owner = $owner;

        return $this;
    }

    /**
     * Get owner
     *
     * @return integer
     */
    public function getOwner()
    {
        return $this->owner;
    }

    /**
     * Set ownerSecond
     *
     * @param integer $ownerSecond
     *
     * @return Page
     */
    public function setOwnerSecond($ownerSecond)
    {
        $this->ownerSecond = $ownerSecond;

        return $this;
    }

    /**
     * Get ownerSecond
     *
     * @return integer
     */
    public function getOwnerSecond()
    {
        return $this->ownerSecond;
    }

    /**
     * Set catalogId
     *
     * @param integer $catalogId
     *
     * @return Page
     */
    public function setCatalogId($catalogId)
    {
        $this->catalogId = $catalogId;

        return $this;
    }

    /**
     * Get catalogId
     *
     * @return integer
     */
    public function getCatalogId()
    {
        return $this->catalogId;
    }

    /**
     * Set catalogIndex
     *
     * @param integer $catalogIndex
     *
     * @return Page
     */
    public function setCatalogIndex($catalogIndex)
    {
        $this->catalogIndex = $catalogIndex;

        return $this;
    }

    /**
     * Get catalogIndex
     *
     * @return integer
     */
    public function getCatalogIndex()
    {
        return $this->catalogIndex;
    }

    /**
     * Set slug
     *
     * @param string $slug
     *
     * @return Page
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

    /**
     * Get slug
     *
     * @return string
     */
    public function getSlug()
    {
        return $this->slug;
    }

    /**
     * Set status
     *
     * @param integer $status
     *
     * @return Page
     */
    public function setStatus($status)
    {
        $this->status = $status;

        return $this;
    }

    /**
     * Get status
     *
     * @return integer
     */
    public function getStatus()
    {
        return $this->status;
    }

    /**
     * Set showInNavi
     *
     * @param integer $showInNavi
     *
     * @return Page
     */
    public function setShowInNavi($showInNavi)
    {
        $this->showInNavi = $showInNavi;

        return $this;
    }

    /**
     * Get showInNavi
     *
     * @return integer
     */
    public function getShowInNavi()
    {
        return $this->showInNavi;
    }

    /**
     * Set order
     *
     * @param integer $order
     *
     * @return Page
     */
    public function setOrder($order)
    {
        $this->order = $order;

        return $this;
    }

    /**
     * Get order
     *
     * @return integer
     */
    public function getOrder()
    {
        return $this->order;
    }

    /**
     * Set title
     *
     * @param string $title
     *
     * @return Page
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set pagetitle
     *
     * @param string $pagetitle
     *
     * @return Page
     */
    public function setPagetitle($pagetitle)
    {
        $this->pagetitle = $pagetitle;

        return $this;
    }

    /**
     * Get pagetitle
     *
     * @return string
     */
    public function getPagetitle()
    {
        return $this->pagetitle;
    }

    /**
     * Set description
     *
     * @param string $description
     *
     * @return Page
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set keywords
     *
     * @param string $keywords
     *
     * @return Page
     */
    public function setKeywords($keywords)
    {
        $this->keywords = $keywords;

        return $this;
    }

    /**
     * Get keywords
     *
     * @return string
     */
    public function getKeywords()
    {
        return $this->keywords;
    }

    /**
     * Set content
     *
     * @param string $content
     *
     * @return Page
     */
    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }

    /**
     * Get content
     *
     * @return string
     */
    public function getContent()
    {
        return $this->contentNew ?? $this->content;
    }

    /**
     * Set contentNew
     *
     * @param string $contentNew
     *
     * @return Page
     */
    public function setContentNew($contentNew)
    {
        $this->contentNew = $contentNew;

        return $this;
    }

    /**
     * Get contentNew
     *
     * @return string
     */
    public function getContentNew()
    {
        return $this->contentNew;
    }

    /**
     * Set bumaDestination
     *
     * @param string $bumaDestination
     *
     * @return Page
     */
    public function setBumaDestination($bumaDestination)
    {
        $this->bumaDestination = $bumaDestination;

        return $this;
    }

    /**
     * Get bumaDestination
     *
     * @return string
     */
    public function getBumaDestination()
    {
        return $this->bumaDestination;
    }

    /**
     * Set oldCatalogid
     *
     * @param integer $oldCatalogid
     *
     * @return Page
     */
    public function setOldCatalogid($oldCatalogid)
    {
        $this->oldCatalogid = $oldCatalogid;

        return $this;
    }

    /**
     * Get oldCatalogid
     *
     * @return integer
     */
    public function getOldCatalogid()
    {
        return $this->oldCatalogid;
    }

    /**
     * Set oldOwnerid
     *
     * @param integer $oldOwnerid
     *
     * @return Page
     */
    public function setOldOwnerid($oldOwnerid)
    {
        $this->oldOwnerid = $oldOwnerid;

        return $this;
    }

    /**
     * Get oldOwnerid
     *
     * @return integer
     */
    public function getOldOwnerid()
    {
        return $this->oldOwnerid;
    }

    /**
     * Set bumaGjr
     *
     * @param integer $bumaGjr
     *
     * @return Page
     */
    public function setBumaGjr($bumaGjr)
    {
        $this->bumaGjr = $bumaGjr;

        return $this;
    }

    /**
     * Get bumaGjr
     *
     * @return integer
     */
    public function getBumaGjr()
    {
        return $this->bumaGjr;
    }

    /**
     * Set date
     *
     * @param \DateTime $date
     *
     * @return Page
     */
    public function setDate($date)
    {
        $this->date = $date;

        return $this;
    }

    /**
     * Get date
     *
     * @return \DateTime
     */
    public function getDate()
    {
        return $this->date;
    }

    /**
     * Set priceTags
     *
     * @param boolean $priceTags
     *
     * @return Page
     */
    public function setPriceTags($priceTags)
    {
        $this->priceTags = $priceTags;

        return $this;
    }

    /**
     * Get priceTags
     *
     * @return boolean
     */
    public function getPriceTags()
    {
        return $this->priceTags;
    }

    /**
     * Set textRight
     *
     * @param string $textRight
     *
     * @return Page
     */
    public function setTextRight($textRight)
    {
        $this->textRight = $textRight;

        return $this;
    }

    /**
     * Get textRight
     *
     * @return string
     */
    public function getTextRight()
    {
        return $this->textRight;
    }

    /**
     * Set keyword
     *
     * @param string $keyword
     *
     * @return Page
     */
    public function setKeyword($keyword)
    {
        $this->keyword = $keyword;

        return $this;
    }

    /**
     * Get keyword
     *
     * @return string
     */
    public function getKeyword()
    {
        return $this->keyword;
    }

    /**
     * Set canonicalUrl
     *
     * @param string $canonicalUrl
     *
     * @return Page
     */
    public function setCanonicalUrl($canonicalUrl)
    {
        $this->canonicalUrl = $canonicalUrl;

        return $this;
    }

    /**
     * Get canonicalUrl
     *
     * @return string
     */
    public function getCanonicalUrl()
    {
        return $this->canonicalUrl;
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set country
     *
     * @param \AppBundle\Entity\TravelCountry $country
     *
     * @return Page
     */
    public function setCountry(\AppBundle\Entity\TravelCountry $country = null)
    {
        $this->country = $country;

        return $this;
    }

    /**
     * Get country
     *
     * @return \AppBundle\Entity\TravelCountry
     */
    public function getCountry()
    {
        return $this->country;
    }
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->children = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Set lft
     *
     * @param integer $lft
     *
     * @return Page
     */
    public function setLft($lft)
    {
        $this->lft = $lft;

        return $this;
    }

    /**
     * Get lft
     *
     * @return integer
     */
    public function getLft()
    {
        return $this->lft;
    }

    /**
     * Set lvl
     *
     * @param integer $lvl
     *
     * @return Page
     */
    public function setLvl($lvl)
    {
        $this->lvl = $lvl;

        return $this;
    }

    /**
     * Get lvl
     *
     * @return integer
     */
    public function getLvl()
    {
        return $this->lvl;
    }

    /**
     * Set rgt
     *
     * @param integer $rgt
     *
     * @return Page
     */
    public function setRgt($rgt)
    {
        $this->rgt = $rgt;

        return $this;
    }

    /**
     * Get rgt
     *
     * @return integer
     */
    public function getRgt()
    {
        return $this->rgt;
    }

    /**
     * Set root
     *
     * @param \AppBundle\Entity\Page $root
     *
     * @return Page
     */
    public function setRoot(\AppBundle\Entity\Page $root = null)
    {
        $this->root = $root;

        return $this;
    }

    /**
     * Get root
     *
     * @return \AppBundle\Entity\Page
     */
    public function getRoot()
    {
        return $this->root;
    }

    /**
     * Set parent
     *
     * @param \AppBundle\Entity\Page $parent
     *
     * @return Page
     */
    public function setParent(\AppBundle\Entity\Page $parent = null)
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * Get parent
     *
     * @return \AppBundle\Entity\Page
     */
    public function getParent()
    {
        return $this->parent;
    }

    /**
     * Add child
     *
     * @param \AppBundle\Entity\Page $child
     *
     * @return Page
     */
    public function addChild(\AppBundle\Entity\Page $child)
    {
        $this->children[] = $child;

        return $this;
    }

    /**
     * Remove child
     *
     * @param \AppBundle\Entity\Page $child
     */
    public function removeChild(\AppBundle\Entity\Page $child)
    {
        $this->children->removeElement($child);
    }

    /**
     * Get children
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getChildren()
    {
        return $this->children;
    }

    /**
     * Set template
     *
     * @param string $template
     *
     * @return Page
     */
    public function setTemplate($template)
    {
        $this->template = $template;

        return $this;
    }

    /**
     * Get template
     *
     * @return string
     */
    public function getTemplate()
    {
        return $this->template;
    }

    /**
     * Set travelProgram
     *
     * @param \AppBundle\Entity\TravelProgram $travelProgram
     *
     * @return Page
     */
    public function setTravelProgram(\AppBundle\Entity\TravelProgram $travelProgram = null)
    {
        $this->travelProgram = $travelProgram;

        return $this;
    }

    /**
     * Get travelProgram
     *
     * @return \AppBundle\Entity\TravelProgram
     */
    public function getTravelProgram()
    {
        return $this->travelProgram;
    }

    /**
     * Set fewoLodging
     *
     * @param \AppBundle\Entity\FewoLodging $fewoLodging
     *
     * @return Page
     */
    public function setFewoLodging(\AppBundle\Entity\FewoLodging $fewoLodging = null)
    {
        $this->fewoLodging = $fewoLodging;

        return $this;
    }

    /**
     * Get fewoLodging
     *
     * @return \AppBundle\Entity\FewoLodging
     */
    public function getFewoLodging()
    {
        return $this->fewoLodging;
    }

    /**
     * @param string|null $realUrlPath
     *
     * @return Page
     */
    public function setRealUrlPath($realUrlPath)
    {
        $this->realUrlPath = $realUrlPath;
        return $this;
    }

    public function getRealUrlPath()
    {
        return $this->realUrlPath;
    }

    public function getUrlPath()
    {
        if ($this->realUrlPath !== null)
        {
            return $this->realUrlPath;
        }

        $urlParts = [];
        $page = $this;
        do
        {
            $urlParts[] = $page->getSlug();
            $page = $page->getParent();
        } while ($page !== null);
        return '/'. implode('/', array_reverse($urlParts));
    }

    /**
     * Set boxBody
     *
     * @param string $boxBody
     *
     * @return Page
     */
    public function setBoxBody($boxBody)
    {
        $this->boxBody = $boxBody;

        return $this;
    }

    /**
     * Get boxBody
     *
     * @return string
     */
    public function getBoxBody()
    {
        return $this->boxBody;
    }

    /**
     * Set boxImageUrl
     *
     * @param string $boxImageUrl
     *
     * @return Page
     */
    public function setBoxImageUrl($boxImageUrl)
    {
        $this->boxImageUrl = $boxImageUrl;

        return $this;
    }

    /**
     * Get boxImageUrl
     *
     * @return string
     */
    public function getBoxImageUrl()
    {
        return $this->boxImageUrl;
    }

    /**
     * Set boxStar
     *
     * @param string $boxStar
     *
     * @return Page
     */
    public function setBoxStar($boxStar)
    {
        $this->boxStar = $boxStar;

        return $this;
    }

    /**
     * Get boxStar
     *
     * @return string
     */
    public function getBoxStar()
    {
        return $this->boxStar;
    }

    /**
     * Set boxDiscount
     *
     * @param string $boxDiscount
     *
     * @return Page
     */
    public function setBoxDiscount($boxDiscount)
    {
        $this->boxDiscount = $boxDiscount;

        return $this;
    }

    /**
     * Get boxDiscount
     *
     * @return string
     */
    public function getBoxDiscount()
    {
        return $this->boxDiscount;
    }

    public function getHasChildren()
    {
        return $this->lft != null && $this->rgt != null && $this->rgt - $this->lft > 1;
    }

    /**
     * @return TravelCountry|null The country. If no country is set, trace the ancestor page nodes for a country.
     */
    public function getEffectiveCountry()
    {
        $node = $this;
        do
        {
            if ($node->getCountry())
            {
                return $node->getCountry();
            }
        } while ($node = $node->getParent());
        return null;
    }

    /**
     * Set cmsSettings
     *
     * @param mixed $cmsSettings
     *
     * @return Page
     */
    public function setCmsSettings($cmsSettings)
    {
        $this->cmsSettings = is_array($cmsSettings) ? json_encode($cmsSettings) : $cmsSettings;

        return $this;
    }

    /**
     * Get cmsSettings
     *
     * @return mixed
     */
    public function getCmsSettings()
    {
        $ret = json_decode($this->cmsSettings, true);
        if (empty($ret) || !is_array($ret))
        {
            return $this->cmsSettings;
        }
        return $ret;
    }
}
