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", cascade={"persist"})
  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. /**
  122. * @var string
  123. *
  124. * @ORM\Column(name="categories_code", type="string", length=50, nullable=true)
  125. */
  126. private $code = '0';
  127. private $file;
  128. private $temp;
  129. private $currentPath;
  130. public function __construct() {
  131. $this->pictures = new \Doctrine\Common\Collections\ArrayCollection();
  132. }
  133. public function getId() {
  134. return $this->id;
  135. }
  136. public function getName() {
  137. if(!empty($this->languageId)){
  138. $description = null;
  139. foreach($this->descriptions as $d){
  140. if($d->getLanguage()->getId()==$this->languageId){
  141. $description = $d;
  142. break;
  143. }
  144. }
  145. }
  146. if(empty($description) && count($this->descriptions)){
  147. $description = $this->descriptions[0];
  148. }
  149. return empty($description)?'':$description->getName();
  150. }
  151. public function getUrl() {
  152. if(!empty($this->languageId)){
  153. $description = null;
  154. foreach($this->descriptions as $d){
  155. if($d->getLanguage()->getId()==$this->languageId){
  156. $description = $d;
  157. break;
  158. }
  159. }
  160. }
  161. if(empty($description) && count($this->descriptions)){
  162. $description = $this->descriptions[0];
  163. }
  164. return empty($description)?'':$description->getUrl();
  165. }
  166. public function getDescription() {
  167. if(!empty($this->languageId)){
  168. $description = null;
  169. foreach($this->descriptions as $d){
  170. if($d->getLanguage()->getId()==$this->languageId){
  171. $description = $d;
  172. break;
  173. }
  174. }
  175. }
  176. if(empty($description) && count($this->descriptions)){
  177. $description = $this->descriptions[0];
  178. }
  179. return empty($description)?'':$description->getDescription();
  180. }
  181. public function getDescriptions() {
  182. return $this->descriptions;
  183. }
  184. public function getExtraFields() {
  185. return $this->extraFields;
  186. }
  187. public function getImage() {
  188. return $this->image;
  189. }
  190. public function getParent(): ?\App\Entity\Category {
  191. return $this->parent;
  192. }
  193. public function getChildren() {
  194. return $this->children;
  195. }
  196. public function getSortOrder() {
  197. return $this->sortOrder;
  198. }
  199. public function getDateAdded(): ?\DateTime {
  200. return $this->dateAdded;
  201. }
  202. public function getLastModified(): ?\DateTime {
  203. return $this->lastModified;
  204. }
  205. public function getCategoriesBest() {
  206. return $this->categoriesBest;
  207. }
  208. public function getAffichage() {
  209. return $this->affichage;
  210. }
  211. public function getSousmenu() {
  212. return $this->sousmenu;
  213. }
  214. public function getIndex() {
  215. return $this->index;
  216. }
  217. public function getHideChildren() {
  218. return $this->hideChildren;
  219. }
  220. public function getCategoryDescription($lang='fr') : ?CategoryDescription {
  221. foreach($this->getDescriptions() as $desc){
  222. if($desc->getLanguage()->getCode() == $lang)
  223. return $desc;
  224. }
  225. return null;
  226. }
  227. public function getPictures() {
  228. return $this->pictures;
  229. }
  230. public function getPosition(): ?string {
  231. return $this->position;
  232. }
  233. public function getMenuSpacer() {
  234. return $this->menuSpacer;
  235. }
  236. public function getCurrentPath() {
  237. return $this->currentPath;
  238. }
  239. public function getCode(): string {
  240. return $this->code;
  241. }
  242. public function setPictures($pictures): void {
  243. $this->pictures = $pictures;
  244. }
  245. public function setDescriptions($descriptions) {
  246. $this->descriptions = $descriptions;
  247. }
  248. public function setExtraFields($extraFields): void {
  249. $this->extraFields = $extraFields;
  250. }
  251. public function setImage($image) {
  252. $this->image = $image;
  253. }
  254. public function setParent(?\App\Entity\Category $parent) {
  255. $this->parent = $parent;
  256. }
  257. public function setChildren($children) {
  258. $this->children = $children;
  259. }
  260. public function setSortOrder($sortOrder) {
  261. $this->sortOrder = $sortOrder;
  262. }
  263. public function setDateAdded(\DateTime $dateAdded) {
  264. $this->dateAdded = $dateAdded;
  265. }
  266. public function setLastModified(\DateTime $lastModified) {
  267. $this->lastModified = $lastModified;
  268. }
  269. public function setCategoriesBest($categoriesBest) {
  270. $this->categoriesBest = $categoriesBest;
  271. }
  272. public function setAffichage($affichage) {
  273. $this->affichage = $affichage;
  274. }
  275. public function setSousmenu($sousmenu) {
  276. $this->sousmenu = $sousmenu;
  277. }
  278. public function setIndex($index) {
  279. $this->index = $index;
  280. }
  281. public function setHideChildren($hideChildren) {
  282. $this->hideChildren = $hideChildren;
  283. }
  284. public function setPosition(?string $position): void {
  285. $this->position = $position;
  286. }
  287. public function setMenuSpacer($menuSpacer): void {
  288. $this->menuSpacer = $menuSpacer;
  289. }
  290. public function setCurrentPath($currentPath): void {
  291. $this->currentPath = $currentPath;
  292. }
  293. public function setCode(string $code): void {
  294. $this->code = $code;
  295. }
  296. public function isRoot() {
  297. return empty($this->parent);
  298. }
  299. public function isActive() {
  300. return $this->getAffichage() == 1;
  301. }
  302. public function isVisibleInMenu() {
  303. return $this->isActive() && ($this->getSousmenu() == 1);
  304. }
  305. public function hasChildren() {
  306. return !empty($this->children) && (count($this->children)>0);
  307. }
  308. public function toArray() : array{
  309. $output = [
  310. 'id' => $this->getId(),
  311. 'leaf' => count($this->children)?false:true,
  312. 'children' => [],
  313. 'name' => $this->getName(),
  314. 'languageId' => $this->getLanguageId()
  315. ];
  316. foreach($this->children as $child){
  317. $output['children'][] = $child->toArray();
  318. }
  319. return $output;
  320. }
  321. /**
  322. * @todo Remove once 0 values in the table are converted to NULL.
  323. */
  324. public function loadDescriptions(\Doctrine\ORM\EntityManager $em)
  325. {
  326. $conn = $em->getConnection();
  327. $sql = $conn->prepare('select * from categories_description where categories_id = '.$this->getId());
  328. $sql->execute();
  329. $rows = $sql->fetchAll();
  330. $descriptions = [];
  331. foreach($rows as $desc){
  332. $description = new CategoryDescription();
  333. $description->setName($desc['categories_name']);
  334. $description->setDescription($desc['categories_description']);
  335. $description->setLanguage($em->getRepository('App:Language')->find($desc['language_id']));
  336. $descriptions[] = $description;
  337. }
  338. $this->setDescriptions($descriptions);
  339. }
  340. /**
  341. * @ORM\PostLoad
  342. *
  343. * @todo Remove once 0 values in the table are converted to NULL.
  344. */
  345. public function postLoad(\Doctrine\ORM\Event\LifecycleEventArgs $args) {
  346. if ($this->parent && $this->parent->getId() == 0) {
  347. $this->parent = null;
  348. }
  349. // $this->loadDescriptions($args->getObjectManager());
  350. // $this->translate();
  351. }
  352. public static function getSearchIndex($lang = 'fr')
  353. {
  354. return self::SEARCH_INDICES[$lang];
  355. }
  356. }