/** * Updates an existing Category model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $groupProperties = []; foreach ($model->groupProperties as $group) { $groupProperties[] = ['group' => $group, 'properties' => $group->properties]; } $request = Yii::$app->request; $post = $request->post(); $groupPost = $request->post('GroupProperty'); $propertyPost = $request->post('Property'); $model->scenario = $model::SCENARIO_UPDATE; if ($model->load($post)) { $oldProperties = ArrayHelper::flatten(ArrayHelper::getColumn($groupProperties, 'properties')); $oldGroups = ArrayHelper::flatten(ArrayHelper::getColumn($groupProperties, 'group')); $groupProperties = Model::loadProperties($groupPost, $propertyPost, true); if (!$groupProperties) { Yii::$app->session->setFlash('error', 'Failed to update category.'); return $this->redirect('index'); } $oldPropertyIds = ArrayHelper::getColumn($oldProperties, 'id'); $newPropertyIds = []; array_walk_recursive($propertyPost, function ($item, $key) use(&$newPropertyIds) { if ($key == 'id') { $newPropertyIds[] = $item; } }); $oldGroupIds = ArrayHelper::getColumn($oldGroups, 'id'); $newGroupIds = []; array_walk_recursive($groupPost, function ($item, $key) use(&$newGroupIds) { if ($key == 'id') { $newGroupIds[] = $item; } }); $deletedPropertiesIds = array_diff($oldPropertyIds, $newPropertyIds); $deletedGroupsIds = array_diff($oldGroupIds, $newGroupIds); if ($model->validate() && Model::validateProperties($groupProperties)) { $transaction = $model::getDb()->beginTransaction(); try { $oldImage = $model->getOldAttribute('image'); $oldIcon = $model->getOldAttribute('icon'); $oldIconHover = $model->getOldAttribute('icon_hover'); $model->image = UploadedFile::getInstance($model, 'image'); $model->icon = UploadedFile::getInstance($model, 'icon'); $model->icon_hover = UploadedFile::getInstance($model, 'icon_hover'); if ($model->image || $model->icon || $model->icon_hover) { if ($save = $model->save()) { $model->removeImageFromFolder($oldImage); $model->removeIconFromFolder($oldIcon); $model->removeIconHoverFromFolder($oldIconHover); if (!$model->upload()) { throw new \yii\base\Exception(); } } } else { $model->image = $oldImage; $model->icon = $oldIcon; $model->icon_hover = $oldIconHover; $save = $model->save(); } if ($save) { if (!empty($deletedPropertiesIds)) { Property::deleteAll(['id' => $deletedPropertiesIds]); } if (!empty($deletedGroupsIds)) { GroupProperty::deleteAll(['id' => $deletedGroupsIds]); } Model::linkProperties($model, $groupProperties); } Yii::$app->session->setFlash('success', 'Category have been updated.'); $transaction->commit(); } catch (\Exception $e) { $transaction->rollBack(); Yii::$app->session->setFlash('error', 'Failed to update category.'); } } } else { if (empty($groupProperties)) { $groupProperties[] = ['group' => new GroupProperty(), 'properties' => [new Property()]]; } return $this->render('update', ['model' => $model, 'groupProperties' => $groupProperties]); } return $this->redirect('index'); }