|
- <?php
-
- namespace domain\Product\Rotating;
-
- use common\components\ActiveRecordCommon;
- use common\components\Date;
- use domain\Distribution\DistributionRotating\DistributionRotating;
- use domain\Producer\Producer\Producer;
- use domain\Product\RotatingProduct\RotatingProduct;
- use yii\db\ActiveQuery;
- use yii\helpers\Html;
-
- class Rotating extends ActiveRecordCommon
- {
- // Produits sélectionnés dans le formulaire
- public $selected_products_ids;
-
- public static function tableName()
- {
- return 'rotating';
- }
-
- public function rules()
- {
- return [
- [['id_producer', 'name', 'day'], 'required'],
- [['name'], 'string', 'max' => 255],
- [['id_producer', 'day', 'status'], 'integer'],
- [['selected_products_ids'], 'verifySelectedProductsIds'],
- [['selected_products_ids'], 'safe'],
- ];
- }
-
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'id_producer' => 'Producteur',
- 'name' => 'Nom',
- 'day' => 'Jour',
- 'status' => 'Statut',
- 'selected_products_ids' => "Produits"
- ];
- }
-
- public function verifySelectedProductsIds($attribute, $params)
- {
- if(count($this->getSelectedProductsIds()) < 2) {
- $this->addError($attribute, "Vous devez sélectionner au moins deux produits.");
- }
- }
-
- /* Méthodes */
-
- public function getRotatingProductsListAsString(bool $withHtmlLabel = true): string
- {
- return implode($withHtmlLabel ? ' ' : ', ', array_map(function(RotatingProduct $rotatingProduct) use ($withHtmlLabel) {
- return ($withHtmlLabel ? '<span class="label label-default">' : '') . Html::encode($rotatingProduct->getProduct()->name) . ($withHtmlLabel ? '</span>' : '');
- }, $this->getRotatingProducts()));
- }
-
- /* Getters / Setters */
-
- public function getId(): ?int
- {
- return $this->id;
- }
-
- public function getProducer(): Producer
- {
- return $this->producerRelation;
- }
-
- public function setProducer(Producer $producer): self
- {
- $this->populateFieldObject('id_producer', 'producerRelation', $producer);
- return $this;
- }
-
- public function getName(): string
- {
- return $this->name;
- }
-
- public function setName(string $name): self
- {
- $this->name = $name;
- return $this;
- }
-
- public function getDay(): int
- {
- return $this->day;
- }
-
- public function setDay(int $day): self
- {
- $this->day = $day;
- return $this;
- }
-
- public function getStatus(): int
- {
- return $this->status;
- }
-
- public function setStatus(int $status): self
- {
- $this->status = $status;
- return $this;
- }
-
- public function getSelectedProductsIds()
- {
- return $this->selected_products_ids;
- }
-
- public function setSelectedProductsIds(array $selectedProductsIdsArray): self
- {
- $this->selected_products_ids = $selectedProductsIdsArray;
- return $this;
- }
-
- public function getDistributionRotatings(): array
- {
- return $this->distributionRotatingsRelation;
- }
-
- public function getRotatingProducts(): array
- {
- return $this->rotatingProductsRelation;
- }
-
- /* Relations */
-
- public function getProducerRelation(): ActiveQuery
- {
- return $this->hasOne(Producer::class, ['id' => 'id_producer']);
- }
-
- public function getRotatingProductsRelation()
- {
- return $this->hasMany(RotatingProduct::class, ['id_rotating' => 'id']);
- }
-
- public function getDistributionRotatingsRelation()
- {
- return $this->hasMany(DistributionRotating::class, ['id_rotating' => 'id']);
- }
-
- /* Méthodes */
-
- public function getDayAsString(string $lang = 'fr'): string
- {
- return Date::getDayOfWeekStringByNumber($this->getDay(), $lang);
- }
- }
|