src/Entity/Category.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
  5. use App\Entity\CategoryDescription;
  6. /**
  7. * Categories
  8. *
  9. * @ORM\Table(name="categories")
  10. * @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
  11. * @ORM\HasLifecycleCallbacks
  12. */
  13. class Category extends TranslatedEntity
  14. {
  15. protected $tranlatedEntity = 'CategoryDescription';
  16. public const SEARCH_INDICES = [
  17. 'fr' => 'fr-categories',
  18. 'en' => 'en-categories',
  19. ];
  20. public const DISCOUNT_UNIVERSE = 242;
  21. /**
  22. * @var int
  23. *
  24. * @ORM\Column(name="categories_id", type="integer", nullable=false)
  25. * @ORM\Id
  26. * @ORM\GeneratedValue(strategy="IDENTITY")
  27. */
  28. private $id;
  29. /**
  30. * @var string|null
  31. *
  32. * @ORM\Column(name="categories_image", type="string", length=64, nullable=true)
  33. */
  34. private $image;
  35. /**
  36. * @var \App\Entity\Category
  37. *
  38. * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="children")
  39. * @ORM\JoinColumn(name="parent_id", referencedColumnName="categories_id", nullable=true)
  40. */
  41. private $parent;
  42. /**
  43. * @var array
  44. *
  45. * @ORM\OneToMany(targetEntity="App\Entity\Category", mappedBy="parent")
  46. * @ORM\OrderBy({"sortOrder" = "ASC"})
  47. */
  48. private $children;
  49. /**
  50. * @ORM\OneToMany(targetEntity="App\Entity\CategoryDescription", mappedBy="category")
  51. */
  52. private $descriptions = [];
  53. /**
  54. * @ORM\OneToMany(targetEntity="App\Entity\CategoryToExtraField", mappedBy="category")
  55. */
  56. private $extraFields = [];
  57. /**
  58. * @ORM\OneToMany(targetEntity="App\Entity\PictureCategory", mappedBy="category")
  59. */
  60. private $pictures = [];
  61. /**
  62. * @var int
  63. *
  64. * @ORM\Column(name="sort_order", type="integer", nullable=false)
  65. */
  66. private $sortOrder = '0';
  67. /**
  68. * @var \DateTime|null
  69. *
  70. * @ORM\Column(name="date_added", type="datetime", nullable=true)
  71. */
  72. private $dateAdded;
  73. /**
  74. * @var \DateTime|null
  75. *
  76. * @ORM\Column(name="last_modified", type="datetime", nullable=true)
  77. */
  78. private $lastModified;
  79. /**
  80. * @var int|null
  81. *
  82. * @ORM\Column(name="categories_best", type="integer", nullable=true)
  83. */
  84. private $categoriesBest;
  85. /**
  86. * @var int
  87. *
  88. * @ORM\Column(name="categories_affichage", type="integer", nullable=false, options={"default"="1"})
  89. */
  90. private $affichage = '1';
  91. /**
  92. * @var int
  93. *
  94. * @ORM\Column(name="categories_sousmenu", type="integer", nullable=false, options={"default"="1"})
  95. */
  96. private $sousmenu = '1';
  97. /**
  98. * @var int
  99. *
  100. * @ORM\Column(name="categories_index", type="integer", nullable=false, options={"default"="1"})
  101. */
  102. private $index = '1';
  103. /**
  104. * @var int
  105. *
  106. * @ORM\Column(name="categories_hide_children", type="integer", nullable=false)
  107. */
  108. private $hideChildren = '0';
  109. /**
  110. * @var string
  111. *
  112. * @ORM\Column(name="categories_position", type="string", length=64, nullable=true)
  113. */
  114. private $position = '0';
  115. /**
  116. * @var ?boolean
  117. *
  118. * @ORM\Column(name="categories_menu_spacer", type="boolean", nullable=true)
  119. */
  120. private $menuSpacer = '0';
  121. private $file;
  122. private $temp;
  123. private $currentPath;
  124. public function __construct() {
  125. $this->pictures = new \Doctrine\Common\Collections\ArrayCollection();
  126. }
  127. public function getId() {
  128. return $this->id;
  129. }
  130. public function getName() {
  131. if(!empty($this->languageId)){
  132. $description = null;
  133. foreach($this->descriptions as $d){
  134. if($d->getLanguage()->getId()==$this->languageId){
  135. $description = $d;
  136. break;
  137. }
  138. }
  139. }
  140. if(empty($description) && count($this->descriptions)){
  141. $description = $this->descriptions[0];
  142. }
  143. return empty($description)?'':$description->getName();
  144. }
  145. public function getUrl() {
  146. if(!empty($this->languageId)){
  147. $description = null;
  148. foreach($this->descriptions as $d){
  149. if($d->getLanguage()->getId()==$this->languageId){
  150. $description = $d;
  151. break;
  152. }
  153. }
  154. }
  155. if(empty($description) && count($this->descriptions)){
  156. $description = $this->descriptions[0];
  157. }
  158. return empty($description)?'':$description->getUrl();
  159. }
  160. public function getDescription() {
  161. if(!empty($this->languageId)){
  162. $description = null;
  163. foreach($this->descriptions as $d){
  164. if($d->getLanguage()->getId()==$this->languageId){
  165. $description = $d;
  166. break;
  167. }
  168. }
  169. }
  170. if(empty($description) && count($this->descriptions)){
  171. $description = $this->descriptions[0];
  172. }
  173. return empty($description)?'':$description->getDescription();
  174. }
  175. public function getDescriptions() {
  176. return $this->descriptions;
  177. }
  178. public function getExtraFields() {
  179. return $this->extraFields;
  180. }
  181. public function getImage() {
  182. return $this->image;
  183. }
  184. public function getParent(): ?\App\Entity\Category {
  185. return $this->parent;
  186. }
  187. public function getChildren() {
  188. return $this->children;
  189. }
  190. public function getSortOrder() {
  191. return $this->sortOrder;
  192. }
  193. public function getDateAdded(): ?\DateTime {
  194. return $this->dateAdded;
  195. }
  196. public function getLastModified(): ?\DateTime {
  197. return $this->lastModified;
  198. }
  199. public function getCategoriesBest() {
  200. return $this->categoriesBest;
  201. }
  202. public function getAffichage() {
  203. return $this->affichage;
  204. }
  205. public function getSousmenu() {
  206. return $this->sousmenu;
  207. }
  208. public function getIndex() {
  209. return $this->index;
  210. }
  211. public function getHideChildren() {
  212. return $this->hideChildren;
  213. }
  214. public function getCategoryDescription($lang='fr') : ?CategoryDescription {
  215. foreach($this->getDescriptions() as $desc){
  216. if($desc->getLanguage()->getCode() == $lang)
  217. return $desc;
  218. }
  219. return null;
  220. }
  221. public function getPictures() {
  222. return $this->pictures;
  223. }
  224. public function getPosition(): ?string {
  225. return $this->position;
  226. }
  227. public function getMenuSpacer() {
  228. return $this->menuSpacer;
  229. }
  230. public function getCurrentPath() {
  231. return $this->currentPath;
  232. }
  233. public function setPictures($pictures): void {
  234. $this->pictures = $pictures;
  235. }
  236. public function setDescriptions($descriptions) {
  237. $this->descriptions = $descriptions;
  238. }
  239. public function setExtraFields($extraFields): void {
  240. $this->extraFields = $extraFields;
  241. }
  242. public function setImage($image) {
  243. $this->image = $image;
  244. }
  245. public function setParent(?\App\Entity\Category $parent) {
  246. $this->parent = $parent;
  247. }
  248. public function setChildren($children) {
  249. $this->children = $children;
  250. }
  251. public function setSortOrder($sortOrder) {
  252. $this->sortOrder = $sortOrder;
  253. }
  254. public function setDateAdded(\DateTime $dateAdded) {
  255. $this->dateAdded = $dateAdded;
  256. }
  257. public function setLastModified(\DateTime $lastModified) {
  258. $this->lastModified = $lastModified;
  259. }
  260. public function setCategoriesBest($categoriesBest) {
  261. $this->categoriesBest = $categoriesBest;
  262. }
  263. public function setAffichage($affichage) {
  264. $this->affichage = $affichage;
  265. }
  266. public function setSousmenu($sousmenu) {
  267. $this->sousmenu = $sousmenu;
  268. }
  269. public function setIndex($index) {
  270. $this->index = $index;
  271. }
  272. public function setHideChildren($hideChildren) {
  273. $this->hideChildren = $hideChildren;
  274. }
  275. public function setPosition(?string $position): void {
  276. $this->position = $position;
  277. }
  278. public function setMenuSpacer($menuSpacer): void {
  279. $this->menuSpacer = $menuSpacer;
  280. }
  281. public function setCurrentPath($currentPath): void {
  282. $this->currentPath = $currentPath;
  283. }
  284. public function isRoot() {
  285. return empty($this->parent);
  286. }
  287. public function isActive() {
  288. return $this->getAffichage() == 1;
  289. }
  290. public function isVisibleInMenu() {
  291. return $this->isActive() && ($this->getSousmenu() == 1);
  292. }
  293. public function hasChildren() {
  294. return !empty($this->children) && (count($this->children)>0);
  295. }
  296. public function toArray() : array{
  297. $output = [
  298. 'id' => $this->getId(),
  299. 'leaf' => count($this->children)?false:true,
  300. 'children' => [],
  301. 'name' => $this->getName(),
  302. 'languageId' => $this->getLanguageId()
  303. ];
  304. foreach($this->children as $child){
  305. $output['children'][] = $child->toArray();
  306. }
  307. return $output;
  308. }
  309. /**
  310. * @todo Remove once 0 values in the table are converted to NULL.
  311. */
  312. public function loadDescriptions(\Doctrine\ORM\EntityManager $em)
  313. {
  314. $conn = $em->getConnection();
  315. $sql = $conn->prepare('select * from categories_description where categories_id = '.$this->getId());
  316. $sql->execute();
  317. $rows = $sql->fetchAll();
  318. $descriptions = [];
  319. foreach($rows as $desc){
  320. $description = new CategoryDescription();
  321. $description->setName($desc['categories_name']);
  322. $description->setDescription($desc['categories_description']);
  323. $description->setLanguage($em->getRepository('App:Language')->find($desc['language_id']));
  324. $descriptions[] = $description;
  325. }
  326. $this->setDescriptions($descriptions);
  327. }
  328. /**
  329. * @ORM\PostLoad
  330. *
  331. * @todo Remove once 0 values in the table are converted to NULL.
  332. */
  333. public function postLoad(\Doctrine\ORM\Event\LifecycleEventArgs $args) {
  334. if ($this->parent && $this->parent->getId() == 0) {
  335. $this->parent = null;
  336. }
  337. // $this->loadDescriptions($args->getObjectManager());
  338. // $this->translate();
  339. }
  340. public static function getSearchIndex($lang = 'fr')
  341. {
  342. return self::SEARCH_INDICES[$lang];
  343. }
  344. }