src/Entity/Web/SiteTranslation.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Web;
  3. use Doctrine\ORM\Event\LifecycleEventArgs;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use App\Entity\Behaviors\TranslationTrait;
  6. use App\Entity\Behaviors\SluggableTrait;
  7. use Knp\DoctrineBehaviors\Contract\Entity\TranslationInterface;
  8. use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
  9. use Doctrine\ORM\Mapping\Index;
  10. /**
  11. * @ORM\Table(name="web_sites_translation",uniqueConstraints={@ORM\UniqueConstraint(name="sluging", columns={"locale", "slug"})},
  12. * indexes={@Index(name="locale_slug", columns={"locale","slug"})})
  13. * @ORM\Entity(repositoryClass="App\Repository\TranslationRepository")
  14. * @ORM\EntityListeners({"App\Listener\EntityListener"})
  15. * @ORM\HasLifecycleCallbacks()
  16. */
  17. class SiteTranslation implements TranslationInterface, SluggableInterface
  18. {
  19. use TranslationTrait;
  20. use SluggableTrait;
  21. protected $isEmptyOff = true;
  22. /**
  23. * @ORM\Column(type="string", length=255)
  24. */
  25. private $title;
  26. /**
  27. * @ORM\Column(type="text")
  28. */
  29. private $description;
  30. /**
  31. * @ORM\Column(type="string", length=2)
  32. */
  33. protected $locale;
  34. /**
  35. * @ORM\Column(type="string", length=190)
  36. */
  37. protected $slug;
  38. public function __construct($locale = null) {
  39. if($locale !== null){
  40. $this->setLocale($locale);
  41. }
  42. }
  43. public function getId(): ?int
  44. {
  45. return $this->id;
  46. }
  47. public function getTitle(): ?string
  48. {
  49. return $this->title;
  50. }
  51. public function setTitle(string $title): self
  52. {
  53. $this->title = $title;
  54. return $this;
  55. }
  56. public function getDescription(): ?string
  57. {
  58. return $this->description;
  59. }
  60. public function setDescription(string $description): self
  61. {
  62. $this->description = $description;
  63. return $this;
  64. }
  65. public function getSluggableFields(): array
  66. {
  67. return ['title'];
  68. }
  69. public function generateSlug(): void { }
  70. /**
  71. * @ORM\PreUpdate()
  72. * @ORM\PrePersist()
  73. */
  74. public function updateSlug( LifecycleEventArgs $eventArgs )
  75. {
  76. $translatable = $this->getTranslatable();
  77. $oldSlug = $translatable->getSlug();
  78. if ( $this->getRegenerateSlugOnUpdate() || empty( $this->slug ) ) {
  79. $fields = $this->getSluggableFields();
  80. $values = [];
  81. foreach ( $fields as $field ) {
  82. if ( property_exists( $this, $field ) ) {
  83. $val = $this->{$field};
  84. } else {
  85. $methodName = 'get' . ucfirst( $field );
  86. if ( method_exists( $this, $methodName ) ) {
  87. $val = $this->{$methodName}();
  88. } else {
  89. $val = null;
  90. }
  91. }
  92. $values[] = $val;
  93. }
  94. $this->slug = $this->generateSlugValue( $values );
  95. if ( $oldSlug !== $this->slug ) {
  96. $repo = $eventArgs->getEntityManager()->getRepository( get_class( $this ) );
  97. $repo->updateSlug( $this );
  98. }
  99. }
  100. }
  101. }