You are here

function _outline_designer_ajax in Outline Designer 6

Same name and namespace in other branches
  1. 5 outline_designer.module \_outline_designer_ajax()
  2. 6.2 outline_designer.module \_outline_designer_ajax()
  3. 7.2 outline_designer.module \_outline_designer_ajax()
  4. 7 outline_designer.module \_outline_designer_ajax()

Implementation of the ajax menu hook

@args $token = user token to block CSF attack $action = action that's being performed $var1 = typically nid being acted on $var2 = additional data like title, ect. $var3 = even more data like type

3 string references to '_outline_designer_ajax'
outline_child_pages_menu in outline_child_pages/outline_child_pages.module
Implementation of hook_menu().
outline_designer_menu in ./outline_designer.module
Implementation of hook_menu().
outline_designer_og_menu in outline_designer_og/outline_designer_og.module
Implementation of hook_menu().

File

./outline_designer.module, line 517
Massive usability improvement for quickly structuring / creating content.

Code

function _outline_designer_ajax($token, $action, $var1, $var2, $var3) {
  global $user;
  if (drupal_valid_token($token)) {
    switch ($action) {

      // in: active nid, parent nid, new weight
      // action: reorganize nodes
      // out: node id,  node type,  icon to render
      case 'drag_drop':
        $nid = $var1;
        $parent_nid = $var2;
        $weight = $var3;

        // load the active node
        $node = node_load($nid);

        // make sure they can update this node
        if (node_access('update', $node)) {

          // load parent
          $parent = node_load($parent_nid);

          // set parent
          $node->book['plid'] = $parent->book['mlid'];
          $node->book['weight'] = $weight;
          $node->revision = 1;
          $node->log = t("Outline Designer -- nid:%nid parent nid changed to %parent_nid", array(
            '%nid' => $nid,
            '%parent_nid' => $parent_nid,
          ));
          node_save($node);
          print t("Content position has been updated (nid:%nid)", array(
            '%nid' => $nid,
          ));
        }
        else {
          print t("Content position has not been updated due to invalid permissions!");
        }
        break;

      // in: node id, weight
      // action: saved / updated node
      // out: message
      case 'reweight':
        $nid = $var1;
        $weight = $var2;
        $node = node_load($nid);

        // make sure they can update this node
        if (node_access('update', $node)) {

          // set parent / weight
          $node->book['weight'] = $weight;
          node_save($node);
          print t("Content position has been updated (nid:%nid)", array(
            '%nid' => $nid,
          ));
        }
        else {
          print t("Content position has not been updated due to invalid permissions!");
        }
        break;

      // in: nothing
      // action: rebuild the HTML of the table
      // out: AHAH tabledrag object
      case 'reload_table':
        $nid = $var1;
        module_load_include('inc', 'book', 'book.admin');
        $output = drupal_get_form('book_admin_edit', node_load($nid));
        drupal_json(array(
          'status' => TRUE,
          'data' => $output,
        ));
        break;

      // in: node title,  node parent id
      // action: Insert a node
      // out: parent menu id, and message / new nid
      case 'add_content':
        $title = $var1;
        $type = $var2;
        $parent_nid = $var3;

        // need to account for the 3 weird characters in URLs
        $title = str_replace("@2@F@", '/', $title);
        $title = str_replace("@2@3@", '#', $title);
        $title = str_replace("@2@B@", '+', $title);
        $title = str_replace("@2@6@", '&', $title);

        // set the node
        $node = new stdClass();
        $node->title = $title;
        $node->type = $type;
        $node->uid = $user->uid;

        // load up the parent of this new item and then copy over the book structure stuff
        $parent = node_load($parent_nid);
        $node->book['weight'] = -15;
        $node->book['plid'] = $parent->book['mlid'];
        $node->book['bid'] = $parent->book['bid'];
        $node->book['menu_name'] = $parent->book['menu_name'];
        $node->book['module'] = $parent->book['module'];

        // Allow other modules to alter the new book node.
        drupal_alter('new_book_object', $node);
        if (node_access('create', $node)) {
          node_save($node);
          watchdog('content', '%type: added %title.', array(
            '%type' => $node->type,
            '%title' => $node->title,
          ));
          print $parent->book['mlid'] . ';msg:' . t('%type added (nid: %nid)', array(
            '%type' => $node->type,
            '%nid' => $node->nid,
          ));
        }
        else {
          print 0;
        }
        break;

      // in: node id,  title
      // action: rename a single node
      // out: nothing
      case 'rename':
        $nid = $var1;
        $newtitle = $var2;

        // need to account for the 3 weird characters in URLs
        $newtitle = str_replace("@2@F@", '/', $newtitle);
        $newtitle = str_replace("@2@3@", '#', $newtitle);
        $newtitle = str_replace("@2@B@", '+', $newtitle);
        $newtitle = str_replace("@2@6@", '&', $newtitle);
        $node = node_load($nid);
        if (node_access('update', $node)) {
          $node->log = t("Outline Designer -- node renamed from %title to %newtitle", array(
            '%title' => $node->title,
            '%newtitle' => $newtitle,
          ));
          watchdog('content', t("Outline Designer -- node %nid renamed from %title to %newtitle", array(
            '%nid' => $nid,
            '%title' => $node->title,
            '%newtitle' => $newtitle,
          )));
          print t("Content renamed from %title to %newtitle", array(
            '%title' => $node->title,
            '%newtitle' => $newtitle,
          ));
          $node->title = $newtitle;
          $node->revision = 1;
          node_save($node);
        }
        else {
          print t("You don't have permissions to rename this content");
        }
        break;

      // in: root node id
      // action: duplicate the node and associate the tree to the new branch (if multiple)
      // out: new root node
      case 'duplicate':
        $nid = $var1;
        $multiple = $var2;
        $dup_title = $var3;

        // need to account for the 3 weird characters in URLs
        $dup_title = str_replace("@2@F@", '/', $dup_title);
        $dup_title = str_replace("@2@3@", '#', $dup_title);
        $dup_title = str_replace("@2@B@", '+', $dup_title);

        // only duplicate 1 item
        if ($multiple == 0) {
          $node = node_load($nid);
          $orig_node = $node;
          $node->nid = NULL;
          $node->created = NULL;
          $node->book['mlid'] = NULL;
          $node->book['has_children'] = 0;
          $node->uid = $user->uid;

          // swap out the title
          $new_title = str_replace('@title', $node->title, $dup_title);
          $node->title = $new_title;
          if (node_access('create', $node) && node_access('view', $orig_node)) {
            node_save($node);
            print t('Content duplicated from %title (%nid).', array(
              '%title' => $node->title,
              '%nid' => $nid,
            ));
            watchdog('content', t('%type: duplicated %title from node %nid.', array(
              '%type' => $node->type,
              '%title' => $node->title,
              '%nid' => $nid,
            )));
          }
          else {
            print 0;
          }
        }
        else {
          $permission_fail = FALSE;
          $nids = $nid;
          $map = array();
          $node = node_load($nid);

          // pull only the nodes that have the original node as a parent
          $mlid = $node->book['mlid'];
          $result = db_query("\n        SELECT link_path\n        FROM {menu_links}\n        WHERE p2=%d OR p3=%d OR p4=%d OR p5=%d OR p6=%d OR p7=%d OR p8=%d OR p9=%d\n        ORDER BY depth ASC", $mlid, $mlid, $mlid, $mlid, $mlid, $mlid, $mlid, $mlid);
          while ($value = db_fetch_array($result)) {
            $dup_node = node_load(str_replace('node/', '', $value['link_path']));
            $orig_node = $dup_node;
            $current_nid = $dup_node->nid;
            $old_nid = $dup_node->nid;
            $dup_node->nid = NULL;
            $dup_node->created = NULL;
            $dup_node->path = NULL;
            $dup_node->uid = $user->uid;
            $dup_node->revision = 1;

            // swap out the title
            $dup_node->title = str_replace('@title', $dup_node->title, $dup_title);
            $old_mlid = $dup_node->book['mlid'];
            $dup_node->book['mlid'] = NULL;
            $dup_node->log = t("Outline Designer -- Duplicate of old node nid:%old_nid", array(
              '%old_nid' => $old_nid,
            ));
            if (isset($map[$dup_node->book['plid']])) {
              $dup_node->book['plid'] = $map[$dup_node->book['plid']];
            }
            if (node_access('create', $dup_node) && node_access('view', $orig_node)) {
              node_save($dup_node);
              $map[$old_mlid] = $dup_node->book['mlid'];
              watchdog('content', '%type: duplicated %title from node %current_nid.', array(
                '%type' => $dup_node->type,
                '%title' => $dup_node->title,
                '%current_nid' => $current_nid,
              ));
              $nids .= ', ' . $old_nid;
            }
            else {
              $permission_fail = TRUE;
            }
          }
          if ($permission_fail) {
            print 0;
          }
          else {
            print t('Content duplicated (nids:%nids)', array(
              '%nids' => $nids,
            ));
          }
        }
        break;

      // in: list of node ids to delete
      // action: delete nodes
      // out: completion response
      case 'delete':
        $nid = $var1;
        $multiple = $var2;
        $node = node_load($nid);
        if ($multiple == 0) {
          if (node_access('delete', $node)) {
            print t('Content deleted (nid:%nid)', array(
              '%nid' => $nid,
            ));
            node_delete($nid);
          }
          else {
            print 0;
          }
        }
        else {
          $node = node_load($nid);

          // pull only the nodes that have this node as a parent
          $mlid = $node->book['mlid'];

          // make sure this isn't used to delete non book nodes
          $del_count = 0;
          $count = 0;
          if ($mlid != 0 && $mlid != '') {
            $result = db_query("SELECT link_path FROM {menu_links} WHERE module='book' AND (p1=%d OR p2=%d OR p3=%d OR p4=%d OR p5=%d OR p6=%d OR p7=%d OR p8=%d OR p9=%d)", $mlid, $mlid, $mlid, $mlid, $mlid, $mlid, $mlid, $mlid, $mlid);
            while ($value = db_fetch_array($result)) {
              $count++;
              $node = node_load(str_replace('node/', '', $value['link_path']));
              if (node_access('delete', $node)) {
                $del_count++;
                node_delete($node->nid);
              }
            }
          }
          if ($del_count == 0) {
            print 0;
          }
          elseif ($count != $del_count) {
            print t("A multiple delete was commited but you didn't have the permissions to delete all content so some remain.");
          }
          else {
            print t("All content deleted successfully.");
          }
        }
        break;

      // in: node id,  node type name
      // action: change node type and resave
      // out: icon to render
      case 'change_type':
        $nid = $var1;
        $new_type = $var2;
        $node = node_load($nid);
        if (node_access('update', $node)) {
          $node->log = t('Outline Designer -- Content Type changed from %type to %new_type', array(
            '%type' => $node->type,
            '%new_type' => $new_type,
          ));
          watchdog('content', t('Outline Designer -- Content Type changed from %type to %new_type', array(
            '%type' => $node->type,
            '%new_type' => $new_type,
          )));
          print t('Content type changed from %type to %new_type', array(
            '%type' => $node->type,
            '%new_type' => $new_type,
          ));
          $node->type = $new_type;
          node_save($node);
        }
        else {
          print 0;
        }
        break;
    }
  }
  exit;
}