You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 satır
3.9KB

  1. <?php
  2. namespace domain\Product\Rotating;
  3. use common\components\ActiveRecordCommon;
  4. use common\components\Date;
  5. use domain\Distribution\DistributionRotating\DistributionRotating;
  6. use domain\Producer\Producer\Producer;
  7. use domain\Product\RotatingProduct\RotatingProduct;
  8. use yii\db\ActiveQuery;
  9. use yii\helpers\Html;
  10. class Rotating extends ActiveRecordCommon
  11. {
  12. // Produits sélectionnés dans le formulaire
  13. public $selected_products_ids;
  14. public static function tableName()
  15. {
  16. return 'rotating';
  17. }
  18. public function rules()
  19. {
  20. return [
  21. [['id_producer', 'name', 'day'], 'required'],
  22. [['name'], 'string', 'max' => 255],
  23. [['id_producer', 'day', 'status'], 'integer'],
  24. [['selected_products_ids'], 'verifySelectedProductsIds'],
  25. [['selected_products_ids'], 'safe'],
  26. ];
  27. }
  28. public function attributeLabels()
  29. {
  30. return [
  31. 'id' => 'ID',
  32. 'id_producer' => 'Producteur',
  33. 'name' => 'Nom',
  34. 'day' => 'Jour',
  35. 'status' => 'Statut',
  36. 'selected_products_ids' => "Produits"
  37. ];
  38. }
  39. public function verifySelectedProductsIds($attribute, $params)
  40. {
  41. if(count($this->getSelectedProductsIds()) < 2) {
  42. $this->addError($attribute, "Vous devez sélectionner au moins deux produits.");
  43. }
  44. }
  45. /* Méthodes */
  46. public function getRotatingProductsListAsString(bool $withHtmlLabel = true): string
  47. {
  48. return implode($withHtmlLabel ? ' ' : ', ', array_map(function(RotatingProduct $rotatingProduct) use ($withHtmlLabel) {
  49. return ($withHtmlLabel ? '<span class="label label-default">' : '') . Html::encode($rotatingProduct->getProduct()->name) . ($withHtmlLabel ? '</span>' : '');
  50. }, $this->getRotatingProducts()));
  51. }
  52. /* Getters / Setters */
  53. public function getId(): ?int
  54. {
  55. return $this->id;
  56. }
  57. public function getProducer(): Producer
  58. {
  59. return $this->producerRelation;
  60. }
  61. public function setProducer(Producer $producer): self
  62. {
  63. $this->populateFieldObject('id_producer', 'producerRelation', $producer);
  64. return $this;
  65. }
  66. public function getName(): string
  67. {
  68. return $this->name;
  69. }
  70. public function setName(string $name): self
  71. {
  72. $this->name = $name;
  73. return $this;
  74. }
  75. public function getDay(): int
  76. {
  77. return $this->day;
  78. }
  79. public function setDay(int $day): self
  80. {
  81. $this->day = $day;
  82. return $this;
  83. }
  84. public function getStatus(): int
  85. {
  86. return $this->status;
  87. }
  88. public function setStatus(int $status): self
  89. {
  90. $this->status = $status;
  91. return $this;
  92. }
  93. public function getSelectedProductsIds()
  94. {
  95. return $this->selected_products_ids;
  96. }
  97. public function setSelectedProductsIds(array $selectedProductsIdsArray): self
  98. {
  99. $this->selected_products_ids = $selectedProductsIdsArray;
  100. return $this;
  101. }
  102. public function getDistributionRotatings(): array
  103. {
  104. return $this->distributionRotatingsRelation;
  105. }
  106. public function getRotatingProducts(): array
  107. {
  108. return $this->rotatingProductsRelation;
  109. }
  110. /* Relations */
  111. public function getProducerRelation(): ActiveQuery
  112. {
  113. return $this->hasOne(Producer::class, ['id' => 'id_producer']);
  114. }
  115. public function getRotatingProductsRelation()
  116. {
  117. return $this->hasMany(RotatingProduct::class, ['id_rotating' => 'id']);
  118. }
  119. public function getDistributionRotatingsRelation()
  120. {
  121. return $this->hasMany(DistributionRotating::class, ['id_rotating' => 'id']);
  122. }
  123. /* Méthodes */
  124. public function getDayAsString(string $lang = 'fr'): string
  125. {
  126. return Date::getDayOfWeekStringByNumber($this->getDay(), $lang);
  127. }
  128. }