src/Entity/Manufacturer.php line 112

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