'php:d/m/Y'], [['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'auto_payment'], 'boolean'], [['id_point_sale', 'id_producer', 'date_begin'], 'required', 'message' => 'Champs obligatoire'], [['products', 'id_user', 'username'], 'safe'], ['id_user', function ($attribute, $params) { if (!$this->id_user && !strlen($this->username)) { $this->addError($attribute, 'Vous devez sélectionner ou saisir un utilisateur.'); } }, 'skipOnEmpty' => false], ]; } public function attributeLabels() { return [ 'id' => 'ID', 'id_user' => 'Utilisateur', 'id_producer' => 'Producteur', 'id_point_sale' => 'Point de vente', 'date_begin' => 'Date de début', 'date_end' => 'Date de fin', 'monday' => 'Lundi', 'tuesday' => 'Mardi', 'wednesday' => 'Mercredi', 'thursday' => 'Jeudi', 'friday' => 'Vendredi', 'saturday' => 'Samedi', 'sunday' => 'Dimanche', 'week_frequency' => 'Périodicité (semaines)', 'username' => 'Nom d\'utilisateur', 'auto_payment' => 'Paiement automatique' ]; } /** * Enregistre l'abonnement. * * @return boolean */ public function save() { if ($this->id) { $subscription = Subscription::searchOne(['id' => $this->id]) ; } else { $subscription = new Subscription ; } if ($subscription) { $subscription->id_user = $this->id_user; $subscription->username = $this->username; $subscription->id_producer = $this->id_producer; $subscription->id_point_sale = $this->id_point_sale; $subscription->date_begin = date( 'Y-m-d', strtotime(str_replace('/', '-', $this->date_begin) )); if (strlen($this->date_end)) { $subscription->date_end = date( 'Y-m-d', strtotime(str_replace('/', '-', $this->date_end) )); } $subscription->monday = $this->monday; $subscription->tuesday = $this->tuesday; $subscription->wednesday = $this->wednesday; $subscription->thursday = $this->thursday; $subscription->friday = $this->friday; $subscription->saturday = $this->saturday; $subscription->sunday = $this->sunday; $subscription->week_frequency = $this->week_frequency; $subscription->auto_payment = $this->auto_payment; $subscription->save(); // produits if ($this->id) { ProductSubscription::deleteAll(['id_subscription' => $this->id]); } foreach ($this->products as $nameInput => $quantity) { if ($quantity) { $idProduct = str_replace('product_', '', $nameInput); $subscriptionProduct = new ProductSubscription; $subscriptionProduct->id_subscription = $subscription->id; $subscriptionProduct->id_product = $idProduct; $subscriptionProduct->quantity = $quantity; $subscriptionProduct->save(); } } } return true; } }