/** * Creates a new InsightsDef model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate($idCategory = 4, $id = 0) { $model = new InsightsDef(); $content = new InsightsContent(); $model->name = $id; $model->id_category = $idCategory; if ($model->load(Yii::$app->request->post())) { $priorityCounter = 0; foreach (Yii::$app->request->post()['InsightsDef'] as $key => $value) { if (!in_array($key, ['id', 'id_category', 'priority', 'hospitals', 'units', 'specialities', 'name']) && $value != '') { $priorityCounter++; } } $model->priority = $priorityCounter; $model->save(); $content->id_insights_def = $model->id; $content->content = Yii::$app->request->post()['InsightsContent']['content']; $content->lang = 'pl'; $content->save(); return $this->redirect(['create', 'idCategory' => $model->id_category, 'id' => $model->id]); } else { $categories = Categories::find()->select(['name', 'id'])->indexBy('id')->column(); $indicatorNames = CategoriesIndicators::find()->where('id_categories = ' . $idCategory)->all(); $insights = InsightsDef::find()->where('id_category = ' . $idCategory)->all(); $insightsCollection = [0 => '']; foreach ($insights as $insight) { $name = ''; foreach ($insight->toArray() as $key => $value) { if (!in_array($key, ['id', 'id_category', 'priority', 'hospitals', 'units', 'specialities']) && $value !== null) { $name .= $key . '(' . $value . ')'; } } $insightsCollection[$insight->id] = $name; } return $this->render('create', ['model' => $model, 'content' => $content, 'categories' => $categories, 'indicatorNames' => $indicatorNames, 'insights' => $insightsCollection]); } }
/** * Updates an existing Categories model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { if (\Yii::$app->user->isGuest) { return $this->goHome(); } $model = $this->findModel($id); $indicatorsAvailable = IndicatorNames::find()->select(['name', 'id'])->indexBy('id')->column(); $indicatorsPresent = $model->getIndicators()->select(['name', 'id'])->indexBy('id')->column(); foreach ($indicatorsPresent as $value) { if (($key = array_search($value, $indicatorsAvailable)) !== false) { unset($indicatorsAvailable[$key]); } } if ($model->load(Yii::$app->request->post()) && $model->save()) { CategoriesIndicators::deleteAll('id_categories = ' . $id); foreach (Yii::$app->request->post()['Categories']['indicators'] as $id_indicator) { $catInd = new CategoriesIndicators(); $catInd->id_indicator_names = $id_indicator; $catInd->id_categories = $model->id; $catInd->save(); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'indicatorsAvailable' => $indicatorsAvailable, 'indicatorsPresent' => $indicatorsPresent]); } }