src/Entity/Manufacturer.php line 123

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. public function getId() {
  97. return $this->id;
  98. }
  99. public function getCode(): string {
  100. return $this->code;
  101. }
  102. public function getName() {
  103. return $this->name;
  104. }
  105. public function getUrl($lang = null) {
  106. if(!empty($lang)){
  107. $description = $this->getManufacturerDescription($lang);
  108. if($description){
  109. $url = $description->getUrl();
  110. if(!empty($url)){
  111. return \App\Helpers\Encoder::formatUrl($url);
  112. }
  113. }
  114. }
  115. return \App\Helpers\Encoder::formatUrl($this->name);
  116. }
  117. public function getLogo() {
  118. if(empty($this->image))
  119. return $this->image;
  120. return 'assets/manufacturers/'. $this->image;
  121. }
  122. public function getImage() {
  123. return $this->image;
  124. }
  125. public function getDateAdded(): \DateTime {
  126. return is_null($this->dateAdded) ? new \DateTime() : $this->dateAdded;
  127. }
  128. public function getLastModified(): \DateTime {
  129. return is_null($this->lastModified) ? new \DateTime() : $this->lastModified;
  130. }
  131. public function getStatus() {
  132. return $this->status;
  133. }
  134. public function getOrder() {
  135. return $this->order;
  136. }
  137. public function getDescriptions() {
  138. return $this->descriptions;
  139. }
  140. public function getExport(): int {
  141. return $this->export;
  142. }
  143. public function getCountryRestrictions() {
  144. return $this->countryRestrictions;
  145. }
  146. public function getCountries() {
  147. $restrictions = $this->getCountryRestrictions();
  148. $output = [];
  149. foreach($restrictions as $r){
  150. $output[] = $r->getCountry();
  151. }
  152. return $output;
  153. }
  154. public function getLockedPrice(): ?int {
  155. return empty($this->lockedPrice)?0:1;
  156. }
  157. public function getWelcome(): ?int {
  158. return $this->welcome;
  159. }
  160. public function setCode(string $code): void {
  161. $this->code = $code;
  162. }
  163. public function setName($name) {
  164. $this->name = $name;
  165. }
  166. public function setImage($image) {
  167. $this->image = $image;
  168. }
  169. public function setDateAdded(\DateTime $dateAdded) {
  170. $this->dateAdded = $dateAdded;
  171. }
  172. public function setLastModified(\DateTime $lastModified) {
  173. $this->lastModified = $lastModified;
  174. }
  175. public function setStatus($status) {
  176. $this->status = $status;
  177. }
  178. public function setOrder($order) {
  179. $this->order = $order;
  180. }
  181. public function setDescriptions($descriptions): void {
  182. $this->descriptions = $descriptions;
  183. }
  184. public function setExport(int $export): void {
  185. $this->export = $export;
  186. }
  187. public function setCountryRestrictions($countryRestrictions): void {
  188. $this->countryRestrictions = $countryRestrictions;
  189. }
  190. public function setLockedPrice($lockedPrice): void {
  191. $this->lockedPrice = empty($lockedPrice)?0:1;
  192. }
  193. public function setWelcome(int $welcome): void {
  194. $this->welcome = $welcome;
  195. }
  196. public function hasRestrictions() {
  197. return count($this->getCountryRestrictions())>0;
  198. }
  199. public function hasRestrictionForCountry($country) {
  200. if($country){
  201. if($this->hasRestrictions()){
  202. foreach($this->getCountryRestrictions() as $restriction){
  203. if($restriction->getCountry() == $country){
  204. return true;
  205. }
  206. }
  207. }
  208. }
  209. return false;
  210. }
  211. public function isAvailableForCountry($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. return false;
  220. }
  221. }
  222. return true;
  223. }
  224. public function getManufacturerDescription($lang='fr') : ?ManufacturerDescription {
  225. foreach($this->getDescriptions() as $desc){
  226. if($desc->getLanguage()->getCode() == $lang)
  227. return $desc;
  228. }
  229. return null;
  230. }
  231. public function toArray() : array{
  232. $output = [
  233. 'id' => $this->getId(),
  234. 'name' => $this->getName(),
  235. 'image' => $this->getImage(),
  236. 'dateAdded' => $this->getDateAdded(),
  237. 'lastModified' => $this->getLastModified(),
  238. 'status' => $this->getStatus()
  239. ];
  240. return $output;
  241. }
  242. public function isActive()
  243. {
  244. return $this->getStatus() != 0;
  245. }
  246. public function hasWelcomeDiscount()
  247. {
  248. return !empty($this->welcome);
  249. }
  250. public static function getSearchIndex($lang = 'fr')
  251. {
  252. return self::SEARCH_INDICES[$lang];
  253. }
  254. }