adapted admin section for FAQ to new database scheme

main
Jan-Philipp Litza 13 years ago
parent 0ce217e3eb
commit 34582524ba

@ -1,16 +1,20 @@
<?php <?php
if(sql_num_query("DESCRIBE `FAQ` `Sprache`") === 0) { if(sql_num_query("DESCRIBE `FAQ` `Sprache`") === 0 && sql_num_query("DESCRIBE `FAQ` `QID`") == 0) {
sql_query("ALTER TABLE `FAQ` sql_query("ALTER TABLE `FAQ`
ADD `Sprache` SET('de', 'en') NOT NULL, ADD `Sprache` SET('de', 'en') NOT NULL,
ADD `QID` INT NOT NULL,
ADD INDEX(`Sprache`)"); ADD INDEX(`Sprache`)");
$res = sql_query("SELECT * FROM `FAQ` WHERE `Sprache` = ''"); $res = sql_query("SELECT * FROM `FAQ` WHERE `Sprache` = ''");
$i = 0;
while($row = mysql_fetch_assoc($res)) { while($row = mysql_fetch_assoc($res)) {
$question = explode('<br>', $row['Frage'], 2); $question = preg_split('#(?:<|&lt;)br(?:>|&gt;)#i', $row['Frage'], 2, PREG_SPLIT_NO_EMPTY);
$answer = explode('<br>', $row['Antwort'], 2); $answer = preg_split('#(?:<|&lt;)br(?:>|&gt;)#i', $row['Antwort'], 2, PREG_SPLIT_NO_EMPTY);
if(count($question) == 2 && count($answer) == 2) if(count($question) == 2 && count($answer) == 2)
sql_query("INSERT INTO `FAQ` (`Frage`, `Antwort`, `Sprache`) VALUES ('" . sql_escape(trim($question[1])) . "', '" . sql_escape(trim($answer[1])) . "', 'en')"); sql_query("INSERT INTO `FAQ` (`Frage`, `Antwort`, `Sprache`, `QID`) VALUES ('" . sql_escape(trim($question[1])) . "', '" . sql_escape(trim($answer[1])) . "', 'en', $i)");
sql_query("UPDATE `FAQ` SET `Frage` = '" . sql_escape(trim($question[0])) . "', `Antwort` = '" . sql_escape(trim($answer[0])) . "', `Sprache` = 'de' WHERE `FID` = " . $row['FID']); sql_query("UPDATE `FAQ` SET `Frage` = '" . sql_escape(trim($question[0])) . "', `Antwort` = '" . sql_escape(trim($answer[0])) . "', `Sprache` = 'de', `QID` = $i WHERE `FID` = " . $row['FID']);
$i++;
} }
_add_index('FAQ', array('QID', 'Sprache'), 'UNIQUE');
$applied = true; $applied = true;
} }

@ -1,48 +1,79 @@
<?php <?php
function admin_faq_create_edit_table($languages, $prefills = array()) {
$form_questions = array('Question');
$form_answers = array('Answer');
foreach ($languages as $language) {
$form_questions[] = '<textarea name="question[' . $language . ']" style="height: 4em;">'
. (!empty($prefills[$language])? $prefills[$language]['question'] : '')
. '</textarea>';
$form_answers[] = '<textarea name="answer[' . $language . ']" style="height: 4em;">'
. (!empty($prefills[$language])? $prefills[$language]['answer'] : '')
. '</textarea>';
}
return table(
array_merge(array(''), $languages),
array($form_questions, $form_answers),
false);
}
function admin_faq() { function admin_faq() {
$languages = sql_select("SELECT DISTINCT `Sprache` FROM `FAQ`");
$languages = array_map('array_shift', $languages);
if (!isset ($_REQUEST['action'])) { if (!isset ($_REQUEST['action'])) {
$faqs_html = ""; $faqs = array();
$faqs = sql_select("SELECT * FROM `FAQ`"); foreach ($languages as $language) {
foreach ($faqs as $faq) { $lang_html .= '<th>' . $language . "</th>\n";
$faqs_html .= sprintf('<tr><td> <dl><dt>%s</dt><dd>%s</dd></dl> </td>' . '<td> <dl><dt>%s</dt><dd>%s</dd></dl> </td>' . '<td><a href="%s&action=edit&id=%s">Edit</a></td></tr>', $faq['Frage_de'], $faq['Antwort_de'], $faq['Frage_en'], $faq['Antwort_en'], page_link_to('admin_faq'), $faq['FID']); $langfaqs = sql_select("SELECT `QID`, `Frage`, `Antwort` FROM `FAQ` WHERE `Sprache` = '" . sql_escape($language) . "'");
foreach ($langfaqs as $langfaq) {
if (!isset($faqs[$langfaq['QID']]))
$faqs[$langfaq['QID']] = array();
$faqs[$langfaq['QID']][$language] = sprintf('<dl><dt>%s</dt><dd>%s</dd></dl>', $langfaq['Frage'], $langfaq['Antwort']);
$faqs[$langfaq['QID']]['edit'] = sprintf('<a href="%s&action=edit&id=%s">Edit</a>', page_link_to('admin_faq'), $langfaq['QID']);
}
} }
$faqs_html = table(array_merge(array_combine($languages, $languages), array('edit' => '')), $faqs);
return template_render('../templates/admin_faq.html', array ( return template_render('../templates/admin_faq.html', array (
'link' => page_link_to("admin_faq"), 'link' => page_link_to("admin_faq"),
'faqs' => $faqs_html 'faqs' => $faqs_html,
'new_form' => admin_faq_create_edit_table($languages)
)); ));
} else { } else {
switch ($_REQUEST['action']) { switch ($_REQUEST['action']) {
case 'create' : case 'create' :
$frage = strip_request_item_nl('frage');
$antwort = strip_request_item_nl('antwort');
$question = strip_request_item_nl('question');
$answer = strip_request_item_nl('answer');
sql_query("INSERT INTO `FAQ` SET `Frage_de`='" . sql_escape($frage) . "', `Frage_en`='" . sql_escape($question) . "', `Antwort_de`='" . sql_escape($antwort) . "', `Antwort_en`='" . sql_escape($answer) . "'");
header("Location: " . page_link_to("admin_faq"));
break;
case 'save' : case 'save' :
if ($_REQUEST['action'] == 'create') {
sql_query("START TRANSACTION");
$qid = sql_select("SELECT MAX(`QID`)+1 AS QID FROM `FAQ`");
$qid = $qid[0]['QID'];
}
else {
if (isset ($_REQUEST['id']) && preg_match("/^[0-9]{1,11}$/", $_REQUEST['id'])) if (isset ($_REQUEST['id']) && preg_match("/^[0-9]{1,11}$/", $_REQUEST['id']))
$id = $_REQUEST['id']; $qid = $_REQUEST['id'];
else else
return error("Incomplete call, missing FAQ ID.", true); return error("Incomplete call, missing FAQ ID.", true);
$faq = sql_select("SELECT * FROM `FAQ` WHERE `FID`=" . sql_escape($id) . " LIMIT 1"); $faq = sql_select("SELECT `QID` FROM `FAQ` WHERE `QID`=" . sql_escape($qid));
if (count($faq) > 0) { if (count($faq) == 0)
list ($faq) = $faq; return error("No FAQ found.", true);
}
$frage = strip_request_item_nl('frage'); $values = array();
$antwort = strip_request_item_nl('antwort'); foreach ($_POST['question'] as $lang => $question) {
$question = strip_request_item_nl('question'); if (!in_array($lang, $languages))
$answer = strip_request_item_nl('answer'); continue;
if (empty($question))
sql_query("UPDATE `FAQ` SET `Frage_de`='" . sql_escape($frage) . "', `Frage_en`='" . sql_escape($question) . "', `Antwort_de`='" . sql_escape($antwort) . "', `Antwort_en`='" . sql_escape($answer) . "' WHERE `FID`=" . sql_escape($id) . " LIMIT 1"); sql_query("DELETE IGNORE FROM `FAQ` WHERE `QID` = $qid AND `Sprache` = '" . sql_escape($lang) . "'");
else {
$question = strip_item($question);
$answer = strip_item($_POST['answer'][$lang]);
$values[] = "('" . sql_escape($lang) . "', '" . sql_escape($question) . "', '" . sql_escape($answer) . "', $qid)";
}
}
if (!empty($values))
sql_query("REPLACE INTO `FAQ` (`Sprache`, `Frage`, `Antwort`, `QID`) VALUES " . implode(', ', $values));
sql_query("COMMIT");
header("Location: " . page_link_to("admin_faq")); header("Location: " . page_link_to("admin_faq"));
} else
return error("No FAQ found.", true);
break; break;
case 'edit' : case 'edit' :
@ -51,17 +82,17 @@ function admin_faq() {
else else
return error("Incomplete call, missing FAQ ID.", true); return error("Incomplete call, missing FAQ ID.", true);
$faq = sql_select("SELECT * FROM `FAQ` WHERE `FID`=" . sql_escape($id) . " LIMIT 1"); $faq = sql_select("SELECT `Sprache`, `Frage`, `Antwort` FROM `FAQ` WHERE `QID`=" . sql_escape($id));
if (count($faq) > 0) { if (count($faq) > 0) {
list ($faq) = $faq; $prefills = array();
foreach ($faq as $row) {
$prefills[$row['Sprache']] = array('question' => $row['Frage'], 'answer' => $row['Antwort']);
}
return template_render('../templates/admin_faq_edit_form.html', array ( return template_render('../templates/admin_faq_edit_form.html', array (
'link' => page_link_to("admin_faq"), 'link' => page_link_to("admin_faq"),
'id' => $id, 'id' => $id,
'frage' => $faq['Frage_de'], 'form' => admin_faq_create_edit_table($languages, $prefills)
'antwort' => $faq['Antwort_de'],
'question' => $faq['Frage_en'],
'answer' => $faq['Antwort_en']
)); ));
} else } else
return error("No FAQ found.", true); return error("No FAQ found.", true);
@ -73,11 +104,8 @@ function admin_faq() {
else else
return error("Incomplete call, missing FAQ ID.", true); return error("Incomplete call, missing FAQ ID.", true);
$faq = sql_select("SELECT * FROM `FAQ` WHERE `FID`=" . sql_escape($id) . " LIMIT 1"); $deleted = sql_query("DELETE FROM `FAQ` WHERE `QID`=" . sql_escape($id));
if (count($faq) > 0) { if ($deleted) {
list ($faq) = $faq;
sql_query("DELETE FROM `FAQ` WHERE `FID`=" . sql_escape($id) . " LIMIT 1");
header("Location: " . page_link_to("admin_faq")); header("Location: " . page_link_to("admin_faq"));
} else } else
return error("No FAQ found.", true); return error("No FAQ found.", true);

@ -1,56 +1,6 @@
<table> %faqs%
<thead>
<tr>
<th>
Deutsch
</th>
<th>
English
</th>
<th>
&nbsp;
</th>
</tr>
</thead>
<tbody>
%faqs%
</tbody>
</table>
<hr/>Create new FAQ: <hr/>Create new FAQ:
<form action="%link%&action=create" method="post"> <form action="%link%&action=create" method="post">
<table> %new_form%
<tr>
<td>
Frage
</td>
<td>
<textarea name="frage"></textarea>
</td>
</tr>
<tr>
<td>
Antwort
</td>
<td>
<textarea name="antwort"></textarea>
</td>
</tr>
<tr>
<td>
Question
</td>
<td>
<textarea name="question"></textarea>
</td>
</tr>
<tr>
<td>
Answer
</td>
<td>
<textarea name="answer"></textarea>
</td>
</tr>
</table>
<input type="submit" name="submit" value="Create" /> <input type="submit" name="submit" value="Create" />
</form> </form>

@ -1,39 +1,6 @@
Edit FAQ: Edit FAQ:
<form action="%link%&action=save" method="post"> <form action="%link%&action=save" method="post">
<table> %form%
<tr>
<td>
Frage
</td>
<td>
<textarea name="frage">%frage%</textarea>
</td>
</tr>
<tr>
<td>
Antwort
</td>
<td>
<textarea name="antwort">%antwort%</textarea>
</td>
</tr>
<tr>
<td>
Question
</td>
<td>
<textarea name="question">%question%</textarea>
</td>
</tr>
<tr>
<td>
Answer
</td>
<td>
<textarea name="answer">%answer%</textarea>
</td>
</tr>
</table>
<input type="hidden" name="id" value="%id%" /><input type="submit" name="submit" value="Save" /> <input type="hidden" name="id" value="%id%" /><input type="submit" name="submit" value="Save" />
</form><hr/> </form><hr/>
<form action="%link%&action=delete" method="post"> <form action="%link%&action=delete" method="post">

Loading…
Cancel
Save