View source
<?php
require_once './includes/bootstrap.inc';
if (isset($_POST['action'])) {
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
switch ($_POST['action']) {
case 'add_node':
$node->title = $_POST['title'];
$node->status = 1;
$node->uid = $user->uid;
if ($_POST['parent'] == 0 || !user_access('change content types')) {
$node->type = variable_get("outline_designer_default_type", "page");
}
else {
$result = db_query("SELECT type FROM node JOIN book ON book.vid = node.vid WHERE parent=" . $_POST['parent'] . " ORDER BY weight DESC");
$num_rows = db_num_rows($result);
if ($num_rows == 0) {
$parent = node_load($_POST['parent']);
if (user_access('create ' . $parent->type . ' content')) {
$node->type = $parent->type;
}
else {
$node->type = variable_get("outline_designer_default_type", "page");
}
}
else {
$value = db_fetch_array($result);
if (user_access('create ' . $value['type'] . ' content')) {
$node->type = $value['type'];
}
else {
$node->type = variable_get("outline_designer_default_type", "page");
}
}
}
$node->weight = -15;
$node->parent = $_POST['parent'];
node_save($node);
if ($node->type != 'book') {
db_query("INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)", $node->nid, $node->vid, $_POST['parent'], -15);
}
if ($node->type == 'book') {
$term = 'pages';
}
else {
$term = 'content';
}
if (user_access('edit ' . $node->type . ' ' . $term) || user_access('edit own ' . $node->type . ' ' . $term)) {
$allow_edit = 1;
}
else {
$allow_edit = 0;
}
print serialize(array(
$node->nid,
$node->type,
variable_get("outline_designer_" . $node->type . "_icon", drupal_get_path('module', 'outline_designer') . "/images/node.png"),
$allow_edit,
));
break;
case 'change_type':
$node = node_load($_POST['nid']);
$type = db_result(db_query("SELECT type FROM node_type WHERE name='" . $_POST['new_type'] . "'"));
if ($node->type != $type) {
$node->type = $type;
node_save($node);
}
print variable_get("outline_designer_" . $node->type . "_icon", drupal_get_path('module', 'outline_designer') . "/images/node.png");
break;
case 'delete':
$ary = unserialize($_POST['ids']);
for ($i = 0; $i < count($ary); $i++) {
node_delete($ary[$i]);
}
print 'Nodes successfully removed!';
break;
case 'drag_drop_update':
$node = node_load($_POST['nid']);
$node->parent = $_POST['parent'];
$node->log = 'Outline Designer reweighting update on drag-and-drop';
node_save($node);
break;
case 'duplicate_nodes':
$tree = array();
$value = db_fetch_array(db_query("SELECT node.nid FROM book JOIN node ON node.vid=book.vid WHERE node.nid=" . $_POST['root']));
$tree[$value['nid']] = 0;
$tree = _outline_designer_recurse_duplicate_nodes($_POST['root'], $tree);
foreach ($tree as $old_nid => $new_nid) {
$node = node_load($old_nid);
$node->nid = null;
$node->created = null;
if ($old_nid == $_POST['root']) {
$node->title = "Duplicate of " . $node->title;
}
else {
$node->parent = $tree[$node->parent];
}
node_save($node);
if ($node->type != 'book') {
db_query("INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)", $node->nid, $node->vid, $node->parent, $node->weight);
}
$tree[$old_nid] = $node->nid;
}
print_r($tree[$_POST['root']]);
break;
case 'get_icons':
if (user_access('change content types')) {
$ary = array();
$ary2 = array();
$ary = variable_get('outline_designer_content_types', array(
'page',
));
foreach ($ary as $value) {
$name = db_result(db_query("SELECT name FROM node_type WHERE type='" . $value . "'"));
if (user_access("create {$value} content")) {
array_push($ary2, array(
$name,
variable_get("outline_designer_" . $value . "_icon", drupal_get_path('module', 'outline_designer') . "/images/node.png"),
));
}
}
print serialize($ary2);
}
break;
case 'get_book_roots':
$roots = array();
$result = db_query("SELECT node.nid,title FROM node JOIN book ON book.vid = node.vid WHERE parent=0 ORDER BY node.nid");
while ($value = db_fetch_array($result)) {
array_push($roots, array(
$value['nid'],
$value['title'],
));
}
print serialize($roots);
break;
case 'load_tree':
$tree = array();
$value = db_fetch_array(db_query("SELECT node.uid,node.nid,parent,title,type FROM book JOIN node ON node.vid=book.vid WHERE node.nid=" . $_POST['nid'] . " ORDER BY weight"));
if ($value['type'] == 'book') {
$term = 'pages';
}
else {
$term = 'content';
}
if (user_access('edit ' . $value['type'] . ' ' . $term)) {
$allow_edit = 1;
}
elseif ($user->uid = $value['uid'] && user_access('edit own ' . $value['type'] . ' ' . $term)) {
$allow_edit = 1;
}
else {
$allow_edit = 0;
}
array_push($tree, array(
$value['nid'],
$value['parent'],
$value['title'],
variable_get("outline_designer_" . $value['type'] . "_icon", drupal_get_path('module', 'outline_designer') . "/images/node.png"),
$allow_edit,
));
$tree = _outline_designer_tree_recurse($_POST['nid'], $tree);
print serialize($tree);
break;
case 'rename':
$node = node_load($_POST['nid']);
$node->title = $_POST['newtitle'];
$node->revision = 1;
node_save($node);
break;
case 'save_tree':
$ary = unserialize($_POST['tree']);
$type = array();
for ($no = 0; $no < count($ary); $no++) {
$nid = $ary[$no][0];
$parent = $ary[$no][1];
$weight = $no;
$title = $ary[$no][2];
$node = node_load($nid);
if ($node->type == '') {
$node->type = variable_get("outline_designer_default_type", "page");
print serialize(array(
$node->nid,
variable_get("outline_designer_" . $node->type . "_icon", drupal_get_path('module', 'outline_designer') . "/images/node.png"),
));
}
else {
$type[$nid] = array(
$node->type,
);
$type[$parent] = array(
$node->type,
);
}
if ($node->weight != $weight || $node->parent != $parent || $node->title != $title) {
$node->weight = $weight;
$node->parent = $parent;
$node->title = $title;
$node->revision = 1;
node_save($node);
}
}
break;
case 'update_weights':
$ary = unserialize($_POST['weight']);
foreach ($ary as $weight) {
$node = node_load($weight[1]);
$node->weight = $weight[0];
$node->log = 'Outline Designer reweighting update';
node_save($node);
}
break;
}
}