Example #1
0
 /**
  * Updates an existing Product model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $product = $this->findModel($id);
     $product->setScenario('product-update');
     $ingredients = $product->ingredients;
     if ($product->load(Yii::$app->request->post())) {
         $oldIDs = ArrayHelper::map($ingredients, 'id', 'id');
         $ingredients = Model::createMultiple(Ingredients::classname(), $ingredients);
         Model::loadMultiple($ingredients, Yii::$app->request->post());
         $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($ingredients, 'id', 'id')));
         // ajax validation
         if (Yii::$app->request->isAjax) {
             Yii::$app->response->format = Response::FORMAT_JSON;
             return ArrayHelper::merge(ActiveForm::validateMultiple($ingredients), ActiveForm::validate($product));
         }
         $product->seen = 0;
         $product->sold = 0;
         $product->user_id = 0;
         // change to admin id or user id
         $product->created_by = Yii::$app->user->identity->username;
         $product->created_date = date('Y-m-d h:m:s');
         $product->modified_by = Yii::$app->user->identity->username;
         $product->modified_date = date('Y-m-d h:m:s');
         $product->status = Status::STATUS_ACTIVE;
         // validate all models
         $valid = $product->validate();
         foreach ($ingredients as $detail) {
             $fields = array('ingredient');
             $valid = $detail->validate($fields) && $valid;
         }
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $product->save(false)) {
                     if (!empty($deletedIDs)) {
                         //Ingredients::updateAll(array( 'status' => Status::STATUS_DELETED ), 'id IN ( ' . implode(",", $deletedIDs) .')' );
                         Ingredients::deleteAll(['id' => $deletedIDs]);
                     }
                     foreach ($ingredients as $ingredient) {
                         $ingredient->created_by = Yii::$app->user->identity->username;
                         $ingredient->created_date = date('Y-m-d h:m:s');
                         $ingredient->modified_by = Yii::$app->user->identity->username;
                         $ingredient->modified_date = date('Y-m-d h:m:s');
                         $ingredient->status = Status::STATUS_ACTIVE;
                         $ingredient->product_id = $product->id;
                         if (!($flag = $ingredient->save(false))) {
                             $transaction->rollBack();
                             break;
                         }
                     }
                     $product->files = UploadedFile::getInstances($product, 'files');
                     if ($product->files) {
                         foreach ($product->files as $file) {
                             $productPhoto = new ProductPhoto();
                             $imageName = $file->baseName . substr(md5(rand()), 0, 7);
                             $productPhoto->caption = $imageName;
                             $productPhoto->product_id = $product->id;
                             $productPhoto->product_photo_order = 0;
                             $productPhoto->created_by = Yii::$app->user->identity->username;
                             $productPhoto->created_date = date('Y-m-d h:m:s');
                             $productPhoto->modified_by = Yii::$app->user->identity->username;
                             $productPhoto->modified_date = date('Y-m-d h:m:s');
                             $productPhoto->status = Status::STATUS_ACTIVE;
                             $productPhoto->image_path = 'uploads/product/' . $imageName . '.' . $file->extension;
                             if (!($flag = $productPhoto->save(false))) {
                                 $transaction->rollBack();
                                 break;
                             } else {
                                 $file->saveAs('uploads/product/' . $imageName . '.' . $file->extension);
                             }
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $product->id]);
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     } else {
         return $this->render('create', ['product' => $product, 'ingredients' => empty($ingredients) ? [new Ingredients()] : $ingredients]);
     }
 }