public static function applyAlias($bank, $header) { $aliases = Alias::findAll(['b_id' => $bank]); foreach ($aliases as $alias) { $header = str_replace($alias->original, $alias->alias, $header); } return $header; }
public function actionSetAlias() { $banks = Yii::$app->request->get('bank'); if (empty($banks)) { $error = "Fehler: Keine Bank wurde ausgewählt"; Yii::$app->session->setFlash('error', $error); return $this->redirect(['/bank/placeholders']); } $sBanks = "'" . join("','", $banks) . "'"; $action = Yii::$app->request->post('action'); if ($action == 'add') { $addAlias = Yii::$app->request->post('alias'); foreach ($banks as $bank) { if ($addAlias['new']['original'] && $addAlias['new']['alias']) { $present = Alias::findOne(['original' => $addAlias['new']['original'], 'b_id' => $bank]); if (!$present) { $alias = new Alias(); $alias->b_id = $bank; $alias->original = $addAlias['new']['original']; $alias->alias = $addAlias['new']['alias']; $alias->save(); } } } } $sql = "SELECT original, alias, count(*) as cnt FROM {{%alias}}\n\t\t\t\tWHERE b_id IN ({$sBanks})\n\t\t\t\tGROUP BY original, alias"; $command = Yii::$app->db->createCommand($sql); $aliases = $command->queryAll(); $aliasesData = Yii::$app->request->post('alias'); if (strpos($action, 'save') !== false) { $key = explode('-', $action)[1]; $aliasToSave = $aliases[$key]; if ($aliasesData[$key]['original'] && $aliasesData[$key]['alias']) { foreach ($banks as $bank) { $alias = Alias::findOne(['original' => $aliasToSave['original'], 'b_id' => $bank]); $alias->original = $aliasesData[$key]['original']; $alias->alias = $aliasesData[$key]['alias']; $alias->save(); } } } if (strpos($action, 'delete') !== false) { $key = explode('-', $action)[1]; $aliasToDelete = $aliases[$key]; foreach ($banks as $bank) { Alias::findOne(['original' => $aliasToDelete['original'], 'b_id' => $bank])->delete(); } } $sql = "SELECT original, alias, count(*) as cnt FROM {{%alias}}\n\t\t\t\tWHERE b_id IN ({$sBanks})\n\t\t\t\tGROUP BY original, alias"; $command = Yii::$app->db->createCommand($sql); $aliases = $command->queryAll(); return $this->render('alias', ['banks' => Bank::findAll(['b_id' => $banks]), 'aliases' => $aliases]); }
private function parseQuestions($text, $separator, $condition = "", &$qcount, $bank_id = null) { $data = explode($separator, trim($text)); $tmp = array(); foreach ($data as $val) { $entry = array(); $cmd = trim($val); $hidden = false; $aslist = false; $horizontal = false; if ($cmd != "") { switch ($cmd[0]) { case "#": $entry['cmd'] = "header"; $entry['text'] = substr($cmd, 1); $entry['condition'] = $condition; $tmp[] = $entry; break; case "@": $entry['cmd'] = "pagebreak"; $entry['condition'] = $condition; $tmp[] = $entry; break; case "w": $cond = array(); $stat = ""; for ($i = 4; $i < strlen($cmd); $i++) { if ($cmd[$i] == "(") { if ($stat == "condition") { $stat = "commands"; } else { $stat = "condition"; } } if ($cmd[$i] != "(" && $cmd[$i] != ")") { if (!isset($cond[$stat])) { $cond[$stat] = ''; } $cond[$stat] .= $cmd[$i]; } } $sub = $this->parseQuestions($cond['commands'], ",", $cond['condition'], $qcount); foreach ($sub as $entry) { $tmp[] = $entry; } break; case "H": case "L": case "N": $prefix = $cmd[0]; $cmd = substr($cmd, 1); if ($prefix == "H") { $hidden = true; } if ($prefix == "L") { $aslist = true; } if ($prefix == "N") { $horizontal = true; } default: $entry = Question::find()->where(['fr_id' => $cmd])->asArray()->one(); if (!$entry) { return 0; } //var_dump($entry); $entry['cmd'] = "question"; $entry['condition'] = $condition; $entry['pos'] = $qcount; if ($hidden) { $entry['hidden'] = true; } else { $entry['hidden'] = false; } if ($aslist) { $entry['aslist'] = true; } else { $entry['aslist'] = false; } if ($horizontal) { $entry['horizontal'] = true; } else { $entry['horizontal'] = false; } $qcount++; if (strpos($cmd, "!")) { $oal = explode("!", $cmd); $entry2 = Question::find()->where(['fr_id' => $oal[0]])->asArray()->one(); if (isset($entry2['frage'])) { $entry['frage'] = $entry2['frage']; $entry['dset'] = $oal[1]; } } if (strpos($cmd, "/")) { $al = explode("/", $cmd); $al = QuestionAlias::find()->where(['a_fr_id' => $al[0], 'al_id' => $al[1]])->asArray()->one(); if ($al) { $entry['frage'] = $al['alias']; } } //bank alias! if ($bank_id !== null) { $al = Alias::find()->where(['b_id' => $bank_id])->asArray()->all(); if (count($al)) { foreach ($al as $replacement) { $entry['frage'] = str_replace($replacement['original'], $replacement['alias'], $entry['frage']); } } } $tmp[] = $entry; break; } } } $data = $tmp; return $data; }