<?php
require('includes/scriptorium_inc.php');
if (!$_SESSION['auth']['isLoggedIn'] || $_SESSION['auth']['level'] < 50) {
header("Location: index.php");
}
function displayForm() { /* {{{ */
global $db, $LOCALES;
$f = new HTML_Form($_SERVER['SCRIPT_NAME'],"post","editForm","return typeDeletionWarning()");
$output = $f->returnStart(true);
$output .= $f->returnHidden('referer',$_SERVER['HTTP_REFERER']);
$output .= $f->returnHidden('action','add');
$output .= '<h2>'.lib('title_type').'</h2>'
.'<p>'.lib('add_edit_type_details').'</p>'
.'<p>'.lib('types_common').'</p>'
.'<h3>'.lib('new_types').'</h3>';
$t = new HTML_Table(' border="0" width="100%" cellpadding="2" cellspacing="0"');
$cols = array();
foreach ($LOCALES as $lc) {
$cols[] = "<img src='images/locale_$lc.png' alt='$lc'>";
}
$t->addRow($cols);
$cols = array();
foreach ($LOCALES as $lc) {
if ($lc == 'us') {
$new = "newtypes";
} else {
$new = $lc."_newtyp";
}
$cols[] = '<textarea name="'.$new.'" cols="15" rows="5"></textarea>';
}
$t->addRow($cols);
$i = 0;
foreach ($LOCALES as $lc) {
$t->setColAttributes($i++, 'align="center"');
}
$output .= $t->toHTML();
$output .= '<p align="center"><input type="submit" value="'
.lib('submit').'" class="button" /></p>';
$sql = "SELECT id, name FROM scriptorium_types ORDER BY name ASC";
$result = $db->query($sql);
while ($type = $result->fetchRow(DB_FETCHMODE_OBJECT)) {
$types[$type->id] = stripslashes($type->name);
}
if (sizeof($types) > 0) {
$t = new HTML_Table(' border="0" width="100%" cellpadding="2" cellspacing="0"');
$output .= '<h3>'.lib('current_types').'</h3><br />';
// current types are displayed in a nested table
// $t->addRow(array(lib('delete').'?',' '),'');
$cols = array(lib('delete').'?');
foreach ($LOCALES as $lc) {
$cols[] = "<img src='images/locale_$lc.png' alt='$lc'>";
}
$t->addRow($cols);
foreach ($types as $id => $name) {
$cols = array(
$f->returnCheckbox("deletion_ids[]",$id).
$f->returnHidden("{$name}_id",$id)
);
foreach ($LOCALES as $lc) {
if ($lc == 'us') {
$cols[] = $f->returnText("type_{$name}",$name,"","textbox");
} else {
$v = libDB('type_'.$id, $lc);
$cols[] = $f->returnText($lc."_typ_".$name,$v,"","textbox");
}
}
$t->addRow($cols,'');
}
$t->setColAttributes(0,'valign="bottom" width="20" align="center"');
$i = 1;
foreach ($LOCALES as $lc) {
$t->setColAttributes($i++, 'align="center" valign="bottom"');
}
}
$output .= $t->toHTML();
$t->addRow(array("<b>".lib('new_types')."</b>",$field));
$t->addRow(array(" ",$f->returnSubmit(lib('submit'))));
$output .= '<p align="center"><input type="submit" value="'
.lib('submit').'" class="button" /></p>';
$output .= $f->returnEnd();
return $output;
} /* }}} */
function commitChanges() { /* {{{ */
global $db, $LOCALES;
$newtypes = trim($_POST['newtypes']);
if ($newtypes != "") {
$newtypes = str_replace("\r",'', $newtypes);
$type_list = explode("\n",$newtypes);
$lc_env = array();
foreach ($LOCALES as $lc) {
$lc_env[$lc] = explode("\n", $_POST[$lc.'_newtyp']);
}
if ($type_list[0] != "") {
$pos = 0;
foreach ($type_list as $type) {
$nextId = $db->nextId("scriptorium_types");
$sql_insert_new = "INSERT INTO scriptorium_types (id, name) "
." VALUES (".$db->quote($nextId).","
.$db->quote($type) . ")";
$result = $db->query($sql_insert_new);
if ($db->isError($result)) {
$message = "<p><strong>$type</strong> : "
.lib('type_name_exist')."</p>"
.'<p><a href="edit_types.php">'
.lib('type_refresh_list').'</a></p>';
return displayError($message);
} else {
foreach ($LOCALES as $lc) {
setLibDB('type_'.$nextId, $lc, $lc_env[$lc][$pos]);
}
}
$pos++;
}
}
}
// update existing types
$sql_update_type = "UPDATE scriptorium_types SET name=? WHERE name=?";
$sql_delete_type = "DELETE FROM scriptorium_types WHERE id=!";
$sql_update_scripts = "UPDATE scriptorium_scripts SET type_id=0, category_id=0 WHERE type_id=!";
$update_query = $db->prepare($sql_update_type);
$delete_query = $db->prepare($sql_delete_type);
$update_scripts = $db->prepare($sql_update_scripts);
foreach($_POST as $key => $value) {
if (strpos($key,"type_") !== false) {
$key = str_replace("type_","",$key);
if ($value == "") {
$id = $_POST[$key . "_id"];
$db->execute($delete_query,array($id));
$db->execute($update_scripts,array($id));
} else {
$id = $_POST[$key . "_id"];
// Update translations
foreach ($LOCALES as $lc) {
if ($lc != 'us') {
setLibDB('type_'.$id, $lc, $_POST[$lc.'_typ_'.$key]);
}
}
$db->execute($update_query,array($value,$key));
}
}
}
// also delete types whose checkbox has been checked
if (isset($_POST['deletion_ids'])) {
foreach ($_POST['deletion_ids'] as $id) {
deleteLibDB('type_'.$id);
}
$db->executeMultiple($delete_query,$_POST['deletion_ids']);
}
// the session needs to be updated
unset($_SESSION['scriptorium_types']);
// after changes have been committed, reload this page
// (don't go to refering page as in the past)
header("Location: {$_SERVER['SCRIPT_NAME']}");
}
if (isset($_POST['action']) && $_POST['action'] == "add") {
$content = commitChanges();
} else {
$content = displayForm();
}
$smarty->assign('content',$content);
$smarty->assign('page_title', lib('title_type'));
$smarty->display('main.tpl.html');
?>