// This swiss-knife blade deletes one record from the specified table
// using the specified key name and value.
-function useDeleteBlade ($tablename, $keyname, $keyvalue, $quotekey = TRUE)
+function useDeleteBlade ($tablename, $keyname, $keyvalue, $quotekey = TRUE, $deleteall = FALSE)
{
global $dbxlink;
if ($quotekey == TRUE)
- $query = "delete from ${tablename} where ${keyname}='$keyvalue' limit 1";
- else
- $query = "delete from ${tablename} where ${keyname}=$keyvalue limit 1";
+ $keyvalue = "'${keyvalue}'";
+ $query = "delete from ${tablename} where ${keyname}=$keyvalue";
+ if (!$deleteall)
+ $query .= ' limit 1';
$result = $dbxlink->exec ($query);
if ($result === NULL)
return FALSE;
return $taglist;
}
+function commitCreateTag ($tagname = '', $parent_id = 0)
+{
+ if ($tagname == '' or $parent_id === 0)
+ return "Invalid args to " . __FUNCTION__;
+ $result = useInsertBlade
+ (
+ 'TagTree',
+ array
+ (
+ 'tag' => "'${tagname}'",
+ 'parent_id' => $parent_id
+ )
+ );
+ if ($result)
+ return '';
+ else
+ return "SQL query failed in " . __FUNCTION__;
+}
+
+function commitDestroyTag ($tagid = 0)
+{
+ if ($tagid == 0)
+ return 'Invalid arg to ' . __FUNCTION__;
+ if (useDeleteBlade ('TagTree', 'id', $tagid, FALSE))
+ return '';
+ else
+ return 'useDeleteBlade() failed in ' . __FUNCTION__;
+}
?>
echo "</table>";
}
-function renderTagRow ($taginfo, $level = 0)
+function renderTagRowForViewer ($taginfo, $level = 0)
{
echo '<tr><td>';
for ($i = 0; $i < $level; $i++)
printImageHREF ('spacer');
echo $taginfo['tag'] . "</td></tr>\n";
foreach ($taginfo['kids'] as $kid)
- renderTagRow ($kid, $level + 1);
+ renderTagRowForViewer ($kid, $level + 1);
+}
+
+function renderTagRowForEditor ($taginfo, $level = 0)
+{
+ global $root, $pageno, $tabno;
+ echo '<tr><td>';
+ // FIXME: check [supplied] refcnt for each tag
+ echo "<a href='${root}process.php?page=${pageno}&tab=${tabno}&op=destroyTag&id=${taginfo['id']}'>";
+ printImageHREF ('delete', 'Destroy tag');
+ echo "</a></td>\n<td>";
+ for ($i = 0; $i < $level; $i++)
+ printImageHREF ('spacer');
+ echo $taginfo['tag'] . "</td><td> </td></tr>\n";
+ foreach ($taginfo['kids'] as $kid)
+ renderTagRowForEditor ($kid, $level + 1);
}
function renderTagTree ()
{
- $tree = getTagTree();
echo '<table>';
- foreach ($tree as $taginfo)
- renderTagRow ($taginfo);
+ foreach (getTagTree() as $taginfo)
+ {
+ echo '<tr>';
+ renderTagRowForViewer ($taginfo);
+ echo "</tr>\n";
+ }
echo '</table>';
}
function renderTagTreeEditor ()
{
- dragon();
+ global $root, $pageno, $tabno;
+ showMessageOrError();
+ echo "<table cellspacing=0 cellpadding=5 align=center class=widetable>\n";
+ echo "<tr><th> </th><th>tag</th><th> </th></tr>\n";
+ foreach (getTagTree() as $taginfo)
+ {
+ renderTagRowForEditor ($taginfo, TRUE);
+ }
+ echo "<form action='${root}process.php' method=post>";
+ echo "<input type=hidden name=page value='${pageno}'>";
+ echo "<input type=hidden name=tab value='${tabno}'>";
+ echo "<input type=hidden name=op value='createTag'>";
+ echo "<tr><td>";
+ printImageHREF ('grant', 'Create tag', TRUE);
+ echo '</td><td><input type=text name=tagname> under <select name=parent_id>';
+ echo "<option value=0>-- NONE --</option>\n";
+ foreach (getTagList() as $taginfo)
+ echo "<option value=${taginfo['id']}>${taginfo['tag']}</option>";
+ echo "</select></td><td> </td></tr>";
+ echo "</form>\n";
+ echo '</table>';
}
function renderTagOption ($taginfo, $level = 0)
$tab['tagtree']['edit'] = 'Change';
$tabhandler['tagtree']['default'] = 'renderTagTree';
$tabhandler['tagtree']['edit'] = 'renderTagTreeEditor';
+$ophandler['tagtree']['edit']['destroyTag'] = 'destroyTag';
+$ophandler['tagtree']['edit']['createTag'] = 'createTag';
+$ophandler['tagtree']['edit']['updateTag'] = 'updateTag';
$page['reports']['title'] = 'Reports';
$page['reports']['parent'] = 'index';
$object_id = $_REQUEST['object_id'];
// Build a trail from the submitted data, minimize it,
// then wipe existing records and store the new set instead.
- useDeleteBlade ('RackObjectTags', 'object_id', $object_id);
+ useDeleteBlade ('RackObjectTags', 'object_id', $object_id, FALSE, TRUE);
$newtrail = getExplicitTagsOnly (buildTrailFromIds ($_REQUEST['taglist']));
$n_succeeds = $n_errors = 0;
foreach ($newtrail as $taginfo)
return "${root}?page=${pageno}&tab=${tabno}&object_id=${object_id}&message=" . urlencode ("New trail is ${n_succeeds} tags long");
}
+function destroyTag ()
+{
+ global $root, $pageno, $tabno;
+ assertUIntArg ('id');
+ if (($ret = commitDestroyTag ($_REQUEST['id'])) == '')
+ return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Successfully deleted tag.");
+ else
+ return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Error deleting tag: '${ret}'");
+}
+
+function createTag ()
+{
+ global $root, $pageno, $tabno;
+ assertStringArg ('tagname');
+ assertUIntArg ('parent_id', TRUE);
+ $tagname = trim ($_REQUEST['tagname']);
+ if (($parent_id = $_REQUEST['parent_id']) <= 0)
+ $parent_id = 'NULL';
+ if (($ret = commitCreateTag ($tagname, $parent_id)) == '')
+ return "${root}?page=${pageno}&tab=${tabno}&message=" . urlencode ("Created tag '${tagname}'.");
+ else
+ return "${root}?page=${pageno}&tab=${tabno}&error=" . urlencode ("Could not create tag '${tagname}' because of error '${ret}'");
+}
+
?>