vendor/kunstmaan/node-bundle/Entity/NodeTranslation.php line 27

Open in your IDE?
  1. <?php
  2. namespace Kunstmaan\NodeBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\ORM\EntityManager;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Kunstmaan\AdminBundle\Entity\AbstractEntity;
  7. use Kunstmaan\NodeBundle\Form\NodeTranslationAdminType;
  8. use Kunstmaan\NodeBundle\Repository\NodeTranslationRepository;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. /**
  11.  * @ORM\Entity(repositoryClass="Kunstmaan\NodeBundle\Repository\NodeTranslationRepository")
  12.  * @ORM\Table(
  13.  *     name="kuma_node_translations",
  14.  *     uniqueConstraints={@ORM\UniqueConstraint(name="ix_kuma_node_translations_node_lang", columns={"node_id", "lang"})},
  15.  *     indexes={@ORM\Index(name="idx__node_translation_lang_url", columns={"lang", "url"}, options={"lengths": {null, 255}})}
  16.  * )
  17.  * @ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
  18.  */
  19. #[ORM\Entity(repositoryClassNodeTranslationRepository::class)]
  20. #[ORM\Table(name'kuma_node_translations')]
  21. #[ORM\UniqueConstraint(name'ix_kuma_node_translations_node_lang'columns: ['node_id''lang'])]
  22. #[ORM\Index(name'idx__node_translation_lang_url'columns: ['lang''url'], options: ['lengths' => [null255]])]
  23. #[ORM\ChangeTrackingPolicy('DEFERRED_EXPLICIT')]
  24. class NodeTranslation extends AbstractEntity
  25. {
  26.     /**
  27.      * @var Node
  28.      *
  29.      * @ORM\ManyToOne(targetEntity="Node", inversedBy="nodeTranslations")
  30.      * @ORM\JoinColumn(name="node_id", referencedColumnName="id")
  31.      */
  32.     #[ORM\ManyToOne(targetEntityNode::class, inversedBy'nodeTranslations')]
  33.     #[ORM\JoinColumn(name'node_id'referencedColumnName'id')]
  34.     protected $node;
  35.     /**
  36.      * @var string
  37.      *
  38.      * @ORM\Column(type="string")
  39.      */
  40.     #[ORM\Column(name'lang'type'string')]
  41.     protected $lang;
  42.     /**
  43.      * @var bool
  44.      *
  45.      * @ORM\Column(type="boolean")
  46.      */
  47.     #[ORM\Column(name'online'type'boolean')]
  48.     protected $online false;
  49.     /**
  50.      * @var string
  51.      *
  52.      * @ORM\Column(type="string")
  53.      */
  54.     #[ORM\Column(name'title'type'string')]
  55.     protected $title;
  56.     /**
  57.      * @var string
  58.      *
  59.      * @ORM\Column(type="string", nullable=true)
  60.      */
  61.     #[ORM\Column(name'slug'type'string'nullabletrue)]
  62.     #[Assert\Regex('/^[a-zA-Z0-9\-_\/]+$/')]
  63.     #[Assert\Length(max'255')]
  64.     protected $slug;
  65.     /**
  66.      * @var string
  67.      *
  68.      * @ORM\Column(type="text", nullable=true)
  69.      */
  70.     #[ORM\Column(name'url'type'text'nullabletrue)]
  71.     #[Assert\Length(max'1000')]
  72.     protected $url;
  73.     /**
  74.      * @var NodeVersion
  75.      *
  76.      * @ORM\ManyToOne(targetEntity="NodeVersion")
  77.      * @ORM\JoinColumn(name="public_node_version_id", referencedColumnName="id")
  78.      */
  79.     #[ORM\ManyToOne(targetEntityNodeVersion::class)]
  80.     #[ORM\JoinColumn(name'public_node_version_id'referencedColumnName'id')]
  81.     protected $publicNodeVersion;
  82.     /**
  83.      * @var ArrayCollection
  84.      *
  85.      * @ORM\OneToMany(targetEntity="NodeVersion", mappedBy="nodeTranslation")
  86.      * @ORM\OrderBy({"created" = "ASC"})
  87.      */
  88.     #[ORM\OneToMany(targetEntityNodeVersion::class, mappedBy'nodeTranslation')]
  89.     #[ORM\OrderBy(['created' => 'ASC'])]
  90.     #[Assert\Valid]
  91.     protected $nodeVersions;
  92.     /**
  93.      * @var int
  94.      *
  95.      * @ORM\Column(type="smallint", nullable=true)
  96.      */
  97.     #[ORM\Column(name'weight'type'smallint'nullabletrue)]
  98.     protected $weight;
  99.     /**
  100.      * @var \DateTime
  101.      *
  102.      * @ORM\Column(type="datetime", nullable=true)
  103.      */
  104.     #[ORM\Column(name'created'type'datetime'nullabletrue)]
  105.     protected $created;
  106.     /**
  107.      * @var \DateTime
  108.      *
  109.      * @ORM\Column(type="datetime", nullable=true)
  110.      */
  111.     #[ORM\Column(name'updated'type'datetime'nullabletrue)]
  112.     protected $updated;
  113.     /**
  114.      * contructor
  115.      */
  116.     public function __construct()
  117.     {
  118.         $this->nodeVersions = new ArrayCollection();
  119.         $this->setCreated(new \DateTime());
  120.         $this->setUpdated(new \DateTime());
  121.     }
  122.     /**
  123.      * Set node
  124.      *
  125.      * @param Node $node
  126.      *
  127.      * @return NodeTranslation
  128.      */
  129.     public function setNode($node)
  130.     {
  131.         $this->node $node;
  132.         return $this;
  133.     }
  134.     /**
  135.      * Get Node
  136.      *
  137.      * @return Node
  138.      */
  139.     public function getNode()
  140.     {
  141.         return $this->node;
  142.     }
  143.     /**
  144.      * Set lang
  145.      *
  146.      * @param string $lang
  147.      *
  148.      * @return NodeTranslation
  149.      */
  150.     public function setLang($lang)
  151.     {
  152.         $this->lang $lang;
  153.         return $this;
  154.     }
  155.     /**
  156.      * Get lang
  157.      *
  158.      * @return string
  159.      */
  160.     public function getLang()
  161.     {
  162.         return $this->lang;
  163.     }
  164.     /**
  165.      * Is online
  166.      *
  167.      * @return bool
  168.      */
  169.     public function isOnline()
  170.     {
  171.         return $this->online;
  172.     }
  173.     /**
  174.      * Set online
  175.      *
  176.      * @param bool $online
  177.      *
  178.      * @return NodeTranslation
  179.      */
  180.     public function setOnline($online)
  181.     {
  182.         $this->online $online;
  183.         return $this;
  184.     }
  185.     /**
  186.      * Set title
  187.      *
  188.      * @param string $title
  189.      *
  190.      * @return NodeTranslation
  191.      */
  192.     public function setTitle($title)
  193.     {
  194.         $this->title $title;
  195.         return $this;
  196.     }
  197.     /**
  198.      * Get title
  199.      *
  200.      * @return string
  201.      */
  202.     public function getTitle()
  203.     {
  204.         return $this->title;
  205.     }
  206.     /**
  207.      * Set slug
  208.      *
  209.      * @param string $slug
  210.      *
  211.      * @return NodeTranslation
  212.      */
  213.     public function setSlug($slug)
  214.     {
  215.         $this->slug $slug;
  216.         return $this;
  217.     }
  218.     /**
  219.      * Get slug
  220.      *
  221.      * @return string
  222.      */
  223.     public function getFullSlug()
  224.     {
  225.         $slug $this->getSlugPart();
  226.         if (empty($slug)) {
  227.             return null;
  228.         }
  229.         return $slug;
  230.     }
  231.     /**
  232.      * @return string
  233.      */
  234.     public function getSlugPart()
  235.     {
  236.         $slug '';
  237.         $parentNode $this->getNode()->getParent();
  238.         if ($parentNode !== null) {
  239.             $nodeTranslation $parentNode->getNodeTranslation($this->langtrue);
  240.             if ($nodeTranslation !== null) {
  241.                 $parentSlug $nodeTranslation->getSlugPart();
  242.                 if (!empty($parentSlug)) {
  243.                     $slug rtrim($parentSlug'/') . '/';
  244.                 }
  245.             }
  246.         }
  247.         $slug .= $this->getSlug();
  248.         return $slug;
  249.     }
  250.     /**
  251.      * Get slug
  252.      *
  253.      * @return string
  254.      */
  255.     public function getSlug()
  256.     {
  257.         return $this->slug;
  258.     }
  259.     /**
  260.      * @return NodeTranslation
  261.      */
  262.     public function setPublicNodeVersion(NodeVersion $publicNodeVersion)
  263.     {
  264.         $this->publicNodeVersion $publicNodeVersion;
  265.         return $this;
  266.     }
  267.     /**
  268.      * @return NodeVersion
  269.      */
  270.     public function getPublicNodeVersion()
  271.     {
  272.         return $this->publicNodeVersion;
  273.     }
  274.     /**
  275.      * @return NodeVersion
  276.      */
  277.     public function getDraftNodeVersion()
  278.     {
  279.         return $this->getNodeVersion('draft');
  280.     }
  281.     /**
  282.      * @return ArrayCollection
  283.      */
  284.     public function getNodeVersions()
  285.     {
  286.         return $this->nodeVersions;
  287.     }
  288.     /**
  289.      * @return NodeTranslation
  290.      */
  291.     public function setNodeVersions(ArrayCollection $nodeVersions)
  292.     {
  293.         $this->nodeVersions $nodeVersions;
  294.         return $this;
  295.     }
  296.     /**
  297.      * @param string $type
  298.      *
  299.      * @return NodeVersion|null
  300.      */
  301.     public function getNodeVersion($type)
  302.     {
  303.         if ($type == 'public') {
  304.             return $this->publicNodeVersion;
  305.         }
  306.         $nodeVersions $this->getNodeVersions();
  307.         $max \count($nodeVersions);
  308.         for ($i $max 1$i >= 0; --$i) {
  309.             /* @var NodeVersion $nodeVersion */
  310.             $nodeVersion $nodeVersions[$i];
  311.             if ($type == $nodeVersion->getType()) {
  312.                 return $nodeVersion;
  313.             }
  314.         }
  315.         return null;
  316.     }
  317.     /**
  318.      * Add nodeVersion
  319.      *
  320.      * @return NodeTranslation
  321.      */
  322.     public function addNodeVersion(NodeVersion $nodeVersion)
  323.     {
  324.         $this->nodeVersions[] = $nodeVersion;
  325.         $nodeVersion->setNodeTranslation($this);
  326.         if ($nodeVersion->getType() == 'public') {
  327.             $this->publicNodeVersion $nodeVersion;
  328.         }
  329.         return $this;
  330.     }
  331.     /**
  332.      * @return string
  333.      */
  334.     public function getDefaultAdminType()
  335.     {
  336.         return NodeTranslationAdminType::class;
  337.     }
  338.     /**
  339.      * @param EntityManager $em   The entity manager
  340.      * @param string        $type The type
  341.      *
  342.      * @return object|null
  343.      */
  344.     public function getRef(EntityManager $em$type 'public')
  345.     {
  346.         $nodeVersion $this->getNodeVersion($type);
  347.         if ($nodeVersion) {
  348.             return $em->getRepository($nodeVersion->getRefEntityName())->find($nodeVersion->getRefId());
  349.         }
  350.         return null;
  351.     }
  352.     /**
  353.      * @param string $url
  354.      *
  355.      * @return NodeTranslation
  356.      */
  357.     public function setUrl($url)
  358.     {
  359.         $this->url $url;
  360.         return $this;
  361.     }
  362.     /**
  363.      * @return string
  364.      */
  365.     public function getUrl()
  366.     {
  367.         return $this->url;
  368.     }
  369.     /**
  370.      * @param int $weight
  371.      *
  372.      * @return NodeTranslation
  373.      */
  374.     public function setWeight($weight)
  375.     {
  376.         $this->weight $weight;
  377.         return $this;
  378.     }
  379.     /**
  380.      * @return int
  381.      */
  382.     public function getWeight()
  383.     {
  384.         return $this->weight;
  385.     }
  386.     /**
  387.      * @return \DateTime
  388.      */
  389.     public function getCreated()
  390.     {
  391.         return $this->created;
  392.     }
  393.     /**
  394.      * @param \DateTime $created
  395.      *
  396.      * @return NodeTranslation
  397.      */
  398.     public function setCreated($created)
  399.     {
  400.         $this->created $created;
  401.         return $this;
  402.     }
  403.     /**
  404.      * @return \DateTime
  405.      */
  406.     public function getUpdated()
  407.     {
  408.         return $this->updated;
  409.     }
  410.     /**
  411.      * @param \DateTime $updated
  412.      *
  413.      * @return NodeTranslation
  414.      */
  415.     public function setUpdated($updated)
  416.     {
  417.         $this->updated $updated;
  418.         return $this;
  419.     }
  420. }