You are here

function nodehierarchy_nodehierarchyapi in Node Hierarchy 5

Same name and namespace in other branches
  1. 6 nodehierarchy.module \nodehierarchy_nodehierarchyapi()

Implementation of hook_nodehierarchyapi(). Responds to own api calls.

File

./nodehierarchy.module, line 259
A module to make nodes hierarchical.

Code

function nodehierarchy_nodehierarchyapi($op, &$node) {
  global $user;
  switch ($op) {
    case 'node_type_form':
      $key = $node->type;
      $form = array();
      $form['nh_parent'] = array(
        '#type' => 'checkbox',
        '#title' => t('Can be parent'),
        '#default_value' => variable_get('nh_parent_' . $key, FALSE),
        '#description' => t('Other nodes can be created as child nodes of this node type.'),
      );
      $form['nh_child'] = array(
        '#type' => 'checkbox',
        '#title' => t('Can be child'),
        '#default_value' => variable_get('nh_child_' . $key, FALSE),
        '#description' => t('This node type can be created as a child of other nodes.'),
      );
      $form['nh_defaultparent'] = _nodehierarchy_get_parent_pulldown($key, variable_get('nh_defaultparent_' . $key, 0), t('Default Parent'));
      $form['nh_createmenu'] = array(
        '#type' => 'radios',
        '#title' => t('Automatically create menu items'),
        '#default_value' => variable_get('nh_createmenu_' . $key, 'optional_no'),
        '#options' => array(
          'never' => t('Never'),
          'optional_no' => t('Optional - default to no'),
          'optional_yes' => t('Optional - default to yes'),
          'always' => t('Always'),
        ),
        '#description' => t("Users must have the 'administer menu' permission to create menu items. If you want to allow users to use this feature without that permission you can enable that on !settings", array(
          '!settings' => l(t('the node hierarchy settings page'), 'admin/settings/nodehierarchy'),
        )),
      );
      return $form;
      break;
    case "node_form":
      $form = array();

      // If this node type can be a child.
      if (nodehierarchy_node_can_be_child($node)) {

        // Save the old value of the node's parent.
        $form['old_parent'] = array(
          '#type' => 'value',
          '#value' => $node->parent,
        );

        // Set the default parent if the node does not already have a parent.
        nodehierarchy_invoke_api("default_parent", $node);

        // if the current user can edit the current node's hierarchy settings (or create new children)
        $can_set_parent = user_access('edit all node parents') || $node->nid == NULL && user_access('create child nodes') || $node->uid == $user->uid && user_access('edit own node parents');
        if ($can_set_parent) {
          $form['parent'] = _nodehierarchy_get_parent_pulldown($node->type, $node->parent, t('Parent'), $node->nid);
        }
        else {

          // Non-editable parent setting.
          $form['parent'] = array(
            '#type' => 'value',
            '#value' => $node->parent,
          );
        }

        // Automatic menu item creation.
        if (module_exists('menu') && variable_get('nodehierarchy_menus', TRUE)) {
          $mid = _nodehierarchy_get_menu($node->nid);

          // (Re)create a menu.
          $form['nodehierarchy_create_menu'] = array(
            '#type' => 'value',
            '#title' => $mid ? t('Recreate Menu') : t('Create Menu'),
            '#default_value' => FALSE,
          );
          if (user_access('administer menu') || variable_get('nodehierarchy_menu_noadmin', FALSE)) {
            $create_menu = variable_get('nh_createmenu_' . $node->type, 'optional_no');
            if ($create_menu == 'optional_no' || $create_menu == 'optional_yes') {
              $form['nodehierarchy_create_menu']['#type'] = 'checkbox';
            }
            if ($create_menu == 'optional_yes' || $create_menu == 'always') {
              $form['nodehierarchy_create_menu']['#default_value'] = TRUE;
            }
          }
        }
      }
      return $form;
      break;
    case "default_parent":
      if (!isset($node->parent) || $node->parent === NULL) {
        $node->parent = variable_get('nh_defaultparent_' . $node->type, 0);

        // Get the parent from the get string. User must have update perms for parent.
        if (isset($_GET['edit']['parent']) && (int) $_GET['edit']['parent']) {
          $parent_node = node_load((int) $_GET['edit']['parent']);
          if ($parent_node && nodehierarchy_node_can_be_parent($parent_node) && node_access("update", $parent_node)) {
            $node->parent = $parent_node->nid;
          }
        }
      }
      break;
    case "update_parent":
      nodehierarchy_insert_node($node);
      _nodehierarchy_create_menu($node);
      break;
  }
}