public function prepareInsights($id, $indicators) { $hospitalIndicators = Divisions::find()->where(['id' => $id])->one(); $whereArray = ['and']; foreach ($indicators as $key => $value) { array_push($whereArray, ['or', "{$key} = {$value}", "{$key} IS NULL"]); } if ($insights = InsightsDef::find()->where($whereArray)->orderBy('priority')->all()) { foreach ($insights as $insight) { foreach ($insight->content as $insightLine) { array_push($this->insightsArray, Insights::_evaluateIndicators($insightLine->content, $hospitalIndicators)); } } } else { $this->insightsArray = []; } }
/** * 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]); } }
private static function _getInsights($indicators) { $returnArray = ['exact' => ['content' => '', 'id' => 0, 'defId' => 0], 'general' => []]; $whereArray = ['and']; $whereArrayExact = ['and']; foreach ($indicators as $key => $value) { if (in_array($key, ['hospitals', 'units', 'specialities'])) { $negation = 0; if ($value[0] == '!') { array_shift($value); $negation = 1; } if (count($value) == 1) { if ($negation) { array_push($whereArray, "{$key} != {$value['0']}"); array_push($whereArrayExact, "{$key} != {$value['0']}"); } else { array_push($whereArray, "{$key} = {$value['0']}"); array_push($whereArrayExact, "{$key} = {$value['0']}"); } } else { if ($negation) { array_push($whereArray, "{$key} NOT IN (" . implode(',', $value) . ")"); array_push($whereArrayExact, "{$key} NOT IN (" . implode(',', $value) . ")"); } else { array_push($whereArray, "{$key} IN (" . implode(',', $value) . ")"); array_push($whereArrayExact, "{$key} IN (" . implode(',', $value) . ")"); } } } else { if ($value !== '' && $value !== null) { array_push($whereArray, ['or', "{$key} = {$value}", "{$key} IS NULL"]); array_push($whereArrayExact, "{$key} = {$value}"); } else { array_push($whereArray, "{$key} IS NULL"); array_push($whereArrayExact, "{$key} IS NULL"); } } } if ($insightExact = InsightsDef::find()->where($whereArrayExact)->one()) { foreach ($insightExact->content as $insightLine) { // array_push ($returnArray['exact'], $insightLine->content); $returnArray['exact']['content'] = $insightLine->content; $returnArray['exact']['id'] = $insightLine->id; $returnArray['exact']['defId'] = $insightExact->id; } } if ($insights = InsightsDef::find()->where($whereArray)->orderBy('priority')->all()) { foreach ($insights as $insight) { if (!is_object($insightExact) || $insight->id != $insightExact->id) { foreach ($insight->content as $insightLine) { array_push($returnArray['general'], $insightLine->content); } } } } return $returnArray; }
use yii\helpers\Html; use yii\widgets\ActiveForm; use app\models\InsightsDef; /* @var $this yii\web\View */ /* @var $model app\models\InsightsContent */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="insights-content-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'name')->dropDownList(InsightsDef::find()->select(['name', 'name'])->indexBy('name')->column()); ?> <?php echo $form->field($model, 'content')->textarea(['rows' => 6]); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? 'Utwórz' : 'Aktualizuj', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?>