src/Entity/Manufacturer.php line 13

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