src/Entity/ProductModel.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\ProductModelDescription;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
  6. use App\Helpers\Encoder;
  7. /**
  8. * Products
  9. *
  10. * @ORM\Table(name="products_models")
  11. * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
  12. * @ORM\HasLifecycleCallbacks()
  13. */
  14. class ProductModel extends TranslatedEntity
  15. {
  16. public const VAT_RATE = 0.2;
  17. public const SEARCH_INDICES = [
  18. 'fr' => 'fr-models',
  19. 'en' => 'en-models',
  20. ];
  21. protected $tranlatedEntity = 'ProductModelDescription';
  22. /**
  23. * @var int
  24. *
  25. * @ORM\Column(name="id", type="integer", nullable=false)
  26. * @ORM\Id
  27. * @ORM\GeneratedValue(strategy="IDENTITY")
  28. */
  29. private $id;
  30. /**
  31. * @var string
  32. *
  33. * @ORM\Column(name="code", type="string", length=50, nullable=true)
  34. */
  35. private $code;
  36. /**
  37. * @ORM\OneToMany(targetEntity="App\Entity\ProductModelAttribute", mappedBy="productModel")
  38. */
  39. private $attributes;
  40. /**
  41. * @ORM\OneToMany(targetEntity="App\Entity\ProductModel", mappedBy="parent")
  42. */
  43. private $children = [];
  44. /**
  45. * @ORM\ManyToOne(targetEntity="App\Entity\ProductModel", inversedBy="children")
  46. * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
  47. */
  48. private $parent;
  49. /**
  50. * @ORM\OneToMany(targetEntity="App\Entity\ProductModelDescription", mappedBy="model")
  51. */
  52. private $descriptions = [];
  53. /**
  54. * @var App\Entity\Manufacturer|null
  55. *
  56. * @ORM\ManyToOne(targetEntity="App\Entity\Manufacturer")
  57. * @ORM\JoinColumn(name="manufacturers_id", referencedColumnName="manufacturers_id", nullable=true)
  58. */
  59. private $manufacturer;
  60. /**
  61. * @ORM\OneToMany(targetEntity="App\Entity\ProductModelCountryRestriction", mappedBy="model")
  62. */
  63. private $countryRestrictions;
  64. /**
  65. * @var \DateTime
  66. *
  67. * @ORM\Column(name="date_added", type="datetime", nullable=false)
  68. */
  69. private $dateAdded;
  70. /**
  71. * @var \DateTime|null
  72. *
  73. * @ORM\Column(name="last_modified", type="datetime", nullable=true)
  74. */
  75. private $lastModified;
  76. /**
  77. * @var bool
  78. *
  79. * @ORM\Column(name="status", type="boolean", nullable=false)
  80. */
  81. private $status = '0';
  82. /**
  83. * @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="productModel")
  84. */
  85. private $products = [];
  86. /**
  87. * @var App\Entity\ProductModelVariant|null
  88. *
  89. * @ORM\ManyToOne(targetEntity="App\Entity\ProductModelVariant")
  90. * @ORM\JoinColumn(name="variant_id", referencedColumnName="id", nullable=true)
  91. */
  92. private $variant;
  93. public function __construct()
  94. {
  95. $this->children = new \Doctrine\Common\Collections\ArrayCollection();
  96. $this->descriptions = new \Doctrine\Common\Collections\ArrayCollection();
  97. }
  98. public function getId(): int {
  99. return $this->id;
  100. }
  101. public function getAttributes() {
  102. return $this->attributes;
  103. }
  104. public function getChildren() {
  105. return $this->children;
  106. }
  107. public function getParent() {
  108. return $this->parent;
  109. }
  110. public function getDescriptions() {
  111. return $this->descriptions;
  112. }
  113. public function getPictures() {
  114. return $this->pictures;
  115. }
  116. public function getManufacturer(): ? Manufacturer {
  117. return $this->manufacturer;
  118. }
  119. public function getCountryRestrictions() {
  120. return $this->countryRestrictions;
  121. }
  122. public function getCode(): ?string {
  123. return $this->code;
  124. }
  125. public function getDateAdded(): \DateTime {
  126. return $this->dateAdded;
  127. }
  128. public function getLastModified(): ?\DateTime {
  129. return $this->lastModified;
  130. }
  131. public function getStatus(): bool {
  132. return $this->status;
  133. }
  134. public function getVariant(): ?ProductModelVariant {
  135. return $this->variant;
  136. }
  137. public function setId(int $id): void {
  138. $this->id = $id;
  139. }
  140. public function setAttributes($attributes): void {
  141. $this->attributes = $attributes;
  142. }
  143. public function setChildren($children): void {
  144. $this->children = $children;
  145. }
  146. public function setParent($parent): void {
  147. $this->parent = $parent;
  148. }
  149. public function setDescriptions($descriptions): void {
  150. $this->descriptions = $descriptions;
  151. }
  152. public function setPictures($pictures): void {
  153. $this->pictures = $pictures;
  154. }
  155. public function setManufacturer(?Manufacturer $manufacturer): void {
  156. $this->manufacturer = $manufacturer;
  157. }
  158. public function setCountryRestrictions($countryRestrictions): void {
  159. $this->countryRestrictions = $countryRestrictions;
  160. }
  161. public function setCode(?string $code): void {
  162. $this->code = $code;
  163. }
  164. public function setDateAdded(\DateTime $dateAdded): void {
  165. $this->dateAdded = $dateAdded;
  166. }
  167. public function setLastModified(?\DateTime $lastModified): void {
  168. $this->lastModified = $lastModified;
  169. }
  170. public function setStatus(bool $status): void {
  171. $this->status = $status;
  172. }
  173. public function setVariant(?ProductModelVariant $variant): void {
  174. $this->variant = $variant;
  175. }
  176. public function hasParent() {
  177. return !empty($this->parent);
  178. }
  179. public function hasChildren() {
  180. return count($this->children) > 0;
  181. }
  182. public function toArray() : array{
  183. $parent = $this->getParent();
  184. $manufacturer = $this->getManufacturer();
  185. $output = [
  186. 'id' => $this->getId(),
  187. 'code' => $this->getCode(),
  188. 'name' => $this->getName(),
  189. 'description' => $this->getDescription(),
  190. 'quantity' => $this->getQuantity(),
  191. 'image' => $this->getImage(),
  192. 'price' => $this->getPrice(),
  193. 'dateAdded' => $this->getDateAdded(),
  194. 'lastModified' => $this->getLastModified(),
  195. 'dateAvailable' => $this->getDateAvailable(),
  196. 'weight' => $this->getWeight(),
  197. 'status' => $this->getStatus()?1:0,
  198. 'taxClass ' => $this->getTaxClass()?$this->getTaxClass()->toArray():0,
  199. 'ordered' => $this->getOrdered(),
  200. 'parent' => empty($parent)?null:$parent->toArray(),
  201. 'manufacturer' => empty($manufacturer)?null:$manufacturer->toArray(),
  202. 'productsQtyBlocks' => $this->getProductsQtyBlocks(),
  203. 'soleil' => $this->getSoleil(),
  204. 'nbChildren' => count($this->getChildren()),
  205. 'limitePanier' => $this->getLimitePanier(),
  206. 'page' => $this->getPage(),
  207. 'garantie' => $this->getGuaranty(),
  208. 'defaultCategoryId' => $this->getDefaultCategoryId(),
  209. 'index' => $this->getIndex(),
  210. 'mvente' => $this->getMvente(),
  211. 'strategique' => $this->getStrategique(),
  212. 'reapproNon' => $this->getReapproNon(),
  213. 'multiple' => $this->getMultiple()
  214. ];
  215. return $output;
  216. }
  217. /**
  218. * @todo Remove once 0 values in the table are converted to NULL.
  219. */
  220. public function loadDescriptions(\Doctrine\ORM\EntityManager $em)
  221. {
  222. $conn = $em->getConnection();
  223. $sql = $conn->prepare('select * from products_description where products_id = '.$this->getId());
  224. $sql->execute();
  225. $rows = $sql->fetchAll();
  226. $descriptions = [];
  227. foreach($rows as $desc){
  228. $description = new ProductModelDescription();
  229. $description->setName($desc['products_name']);
  230. $description->setDescription($desc['products_description']);
  231. $description->setLanguage($em->getRepository('App:Language')->find($desc['language_id']==1?1:4));
  232. $descriptions[] = $description;
  233. }
  234. $this->setDescriptions($descriptions);
  235. }
  236. public function getProductModelDescription($lang='fr') : ?ProductModelDescription {
  237. foreach($this->getDescriptions() as $desc){
  238. if($desc->getLanguage()->getCode() == $lang)
  239. return $desc;
  240. }
  241. return null;
  242. }
  243. public function getProducts() {
  244. return $this->products;
  245. }
  246. public function setProducts($products): void {
  247. $this->products = $products;
  248. }
  249. public function getDefaultProduct() : ?Product {
  250. foreach($this->getProducts() as $product) {
  251. if($product->isDefaultVariant())
  252. return $product;
  253. }
  254. return $this->getProducts()->first();
  255. }
  256. public function getAllProducts() {
  257. $products = [];
  258. if($this->hasChildren()) {
  259. foreach($this->getChildren() as $child) {
  260. $cProducts = $child->getAllProducts();
  261. if(!empty($cProducts)) {
  262. $products = array_merge ($products, $cProducts);
  263. }
  264. }
  265. }else{
  266. $products = $this->getProducts()->getValues();
  267. }
  268. return $products;
  269. }
  270. public function getAssets() {
  271. $output = [];
  272. foreach($this->getAllProducts() as $p) {
  273. foreach($p->getPictures() as $picture) {
  274. if(isset($output[$p->getModel()])) {
  275. $output[$p->getModel()][] = $picture;
  276. }else{
  277. $output[$p->getModel()] = [$picture];
  278. }
  279. }
  280. }
  281. return $output;
  282. }
  283. /**
  284. * @ORM\PrePersist
  285. */
  286. public function prePersit(\Doctrine\ORM\Event\LifecycleEventArgs $args) {
  287. $this->setDateAdded(new \DateTime);
  288. }
  289. public static function getSearchIndex($lang = 'fr')
  290. {
  291. return self::SEARCH_INDICES[$lang];
  292. }
  293. }