src/Entity/Manufacturer.php line 137

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. /**
  5. * Manufacturers
  6. *
  7. * @ORM\Table(name="manufacturers", indexes={
  8. * @ORM\Index(name="IDX_MANUFACTURERS_NAME", columns={"manufacturers_name"}),
  9. * @ORM\Index(name="IDX_MANUFACTURERS_CODE", columns={"manufacturers_code"})
  10. * })
  11. * @ORM\Entity
  12. */
  13. class Manufacturer extends TranslatedEntity
  14. {
  15. protected $tranlatedEntity = 'ManufactureryDescription';
  16. public const SEARCH_INDICES = [
  17. 'fr' => 'fr-manufactuers',
  18. 'en' => 'en-manufactuers',
  19. ];
  20. /**
  21. * @var int
  22. *
  23. * @ORM\Column(name="manufacturers_id", type="integer", nullable=false)
  24. * @ORM\Id
  25. * @ORM\GeneratedValue(strategy="IDENTITY")
  26. */
  27. private $id;
  28. /**
  29. * @var string
  30. *
  31. * @ORM\Column(name="manufacturers_code", type="string", length=32, nullable=false)
  32. */
  33. private $code;
  34. /**
  35. * @var string
  36. *
  37. * @ORM\Column(name="manufacturers_name", type="string", length=32, nullable=false)
  38. */
  39. private $name;
  40. /**
  41. * @var string|null
  42. *
  43. * @ORM\Column(name="manufacturers_image", type="string", length=64, nullable=true)
  44. */
  45. private $image;
  46. /**
  47. * @var \DateTime|null
  48. *
  49. * @ORM\Column(name="date_added", type="datetime", nullable=true)
  50. */
  51. private $dateAdded;
  52. /**
  53. * @var \DateTime|null
  54. *
  55. * @ORM\Column(name="last_modified", type="datetime", nullable=true)
  56. */
  57. private $lastModified;
  58. /**
  59. * @var int|null
  60. *
  61. * @ORM\Column(name="manufacturers_statuts", type="integer", nullable=true)
  62. */
  63. private $status;
  64. /**
  65. * @var int
  66. *
  67. * @ORM\Column(name="manufacturers_order", type="integer", nullable=false)
  68. */
  69. private $order = 0;
  70. /**
  71. * @ORM\OneToMany(targetEntity="App\Entity\ManufacturerDescription", mappedBy="manufacturer")
  72. */
  73. private $descriptions = [];
  74. /**
  75. * @ORM\OneToMany(targetEntity="App\Entity\ManufacturerCountry", mappedBy="manufacturer")
  76. */
  77. private $countryRestrictions = [];
  78. /**
  79. * @var int
  80. *
  81. * @ORM\Column(name="manufacturers_locked_price", type="integer", nullable=true)
  82. */
  83. private $lockedPrice = 0;
  84. /**
  85. * @var int
  86. *
  87. * @ORM\Column(name="manufacturers_welcome", type="integer", nullable=true)
  88. */
  89. private $welcome = 0;
  90. /**
  91. * @var int
  92. *
  93. * @ORM\Column(name="manufacturers_export", type="integer", nullable=false)
  94. */
  95. private $export = 0;
  96. /**
  97. * @var bool|null
  98. *
  99. * @ORM\Column(name="manufacturers_no_soleil", type="boolean", nullable=true)
  100. */
  101. private $noSoleil;
  102. public function getId() {
  103. return $this->id;
  104. }
  105. public function getCode(): string {
  106. return $this->code;
  107. }
  108. public function getName() {
  109. return $this->name;
  110. }
  111. public function getUrl($lang = null) {
  112. if(!empty($lang)){
  113. $description = $this->getManufacturerDescription($lang);
  114. if($description){
  115. $url = $description->getUrl();
  116. if(!empty($url)){
  117. return \App\Helpers\Encoder::formatUrl($url);
  118. }
  119. }
  120. }
  121. return \App\Helpers\Encoder::formatUrl($this->name);
  122. }
  123. public function getLogo() {
  124. if(empty($this->image))
  125. return $this->image;
  126. return 'assets/manufacturers/'. $this->image;
  127. }
  128. public function getImage() {
  129. return $this->image;
  130. }
  131. public function getDateAdded(): \DateTime {
  132. return is_null($this->dateAdded) ? new \DateTime() : $this->dateAdded;
  133. }
  134. public function getLastModified(): \DateTime {
  135. return is_null($this->lastModified) ? new \DateTime() : $this->lastModified;
  136. }
  137. public function getStatus() {
  138. return $this->status;
  139. }
  140. public function getOrder() {
  141. return $this->order;
  142. }
  143. public function getDescriptions() {
  144. return $this->descriptions;
  145. }
  146. public function getExport(): int {
  147. return $this->export;
  148. }
  149. public function getCountryRestrictions() {
  150. return $this->countryRestrictions;
  151. }
  152. public function getCountries() {
  153. $restrictions = $this->getCountryRestrictions();
  154. $output = [];
  155. foreach($restrictions as $r){
  156. $output[] = $r->getCountry();
  157. }
  158. return $output;
  159. }
  160. public function getLockedPrice(): ?int {
  161. return empty($this->lockedPrice)?0:1;
  162. }
  163. public function getWelcome(): ?int {
  164. return $this->welcome;
  165. }
  166. public function getNoSoleil(): ?bool {
  167. return $this->noSoleil;
  168. }
  169. public function setCode(string $code): void {
  170. $this->code = $code;
  171. }
  172. public function setName($name) {
  173. $this->name = $name;
  174. }
  175. public function setImage($image) {
  176. $this->image = $image;
  177. }
  178. public function setDateAdded(\DateTime $dateAdded) {
  179. $this->dateAdded = $dateAdded;
  180. }
  181. public function setLastModified(\DateTime $lastModified) {
  182. $this->lastModified = $lastModified;
  183. }
  184. public function setStatus($status) {
  185. $this->status = $status;
  186. }
  187. public function setOrder($order) {
  188. $this->order = $order;
  189. }
  190. public function setDescriptions($descriptions): void {
  191. $this->descriptions = $descriptions;
  192. }
  193. public function setExport(int $export): void {
  194. $this->export = $export;
  195. }
  196. public function setCountryRestrictions($countryRestrictions): void {
  197. $this->countryRestrictions = $countryRestrictions;
  198. }
  199. public function setLockedPrice($lockedPrice): void {
  200. $this->lockedPrice = empty($lockedPrice)?0:1;
  201. }
  202. public function setWelcome(int $welcome): void {
  203. $this->welcome = $welcome;
  204. }
  205. public function setNoSoleil(?bool $noSoleil): void {
  206. $this->noSoleil = $noSoleil;
  207. }
  208. public function hasRestrictions() {
  209. return count($this->getCountryRestrictions())>0;
  210. }
  211. public function hasRestrictionForCountry($country) {
  212. if($country){
  213. if($this->hasRestrictions()){
  214. foreach($this->getCountryRestrictions() as $restriction){
  215. if($restriction->getCountry() == $country){
  216. return true;
  217. }
  218. }
  219. }
  220. }
  221. return false;
  222. }
  223. public function isAvailableForCountry($country) {
  224. if($country){
  225. if($this->hasRestrictions()){
  226. foreach($this->getCountryRestrictions() as $restriction){
  227. if($restriction->getCountry() == $country){
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. }
  234. return true;
  235. }
  236. public function getManufacturerDescription($lang='fr') : ?ManufacturerDescription {
  237. foreach($this->getDescriptions() as $desc){
  238. if($desc->getLanguage()->getCode() == $lang)
  239. return $desc;
  240. }
  241. return null;
  242. }
  243. public function toArray() : array{
  244. $output = [
  245. 'id' => $this->getId(),
  246. 'name' => $this->getName(),
  247. 'image' => $this->getImage(),
  248. 'dateAdded' => $this->getDateAdded(),
  249. 'lastModified' => $this->getLastModified(),
  250. 'status' => $this->getStatus()
  251. ];
  252. return $output;
  253. }
  254. public function isActive()
  255. {
  256. return $this->getStatus() != 0;
  257. }
  258. public function hasWelcomeDiscount()
  259. {
  260. return !empty($this->welcome);
  261. }
  262. public function isNoSoleil(): bool
  263. {
  264. return !empty($this->noSoleil);
  265. }
  266. public static function getSearchIndex($lang = 'fr')
  267. {
  268. return self::SEARCH_INDICES[$lang];
  269. }
  270. }