You are here

function subform_menu in Subform 5

File

./subform.module, line 197

Code

function subform_menu($may_cache) {
  $items = array();
  content_clear_type_cache();
  $operations = array(
    "view" => true,
    "edit" => true,
    "delete" => true,
    "hoist" => true,
  );
  $node_access = array(
    "view" => 'view',
    "edit" => 'update',
    "delete" => 'delete',
  );
  if ($may_cache) {
    foreach (node_get_types() as $type => $name) {
      $items[] = array(
        'path' => 'subform/add/' . $type,
        'title' => t($name),
        'access' => node_access('create', $type),
        'callback' => '_subform_add',
        'callback arguments' => array(
          $type,
        ),
        'type' => MENU_CALLBACK,
      );
    }
  }
  else {
    if (arg(0) == 'subform' && is_numeric(arg(1)) && $operations[arg(2)] && arg(3) == null) {
      $node = node_load(arg(1));
      if ($node->nid) {
        $items[] = array(
          'path' => 'subform/' . arg(1) . '/' . arg(2),
          'title' => t(arg(2)),
          'callback' => '_subform_page',
          'access' => node_access($node_access[arg(2)], $node),
          'type' => MENU_CALLBACK,
        );
      }
    }
  }
  if (!$may_cache) {
    $directions = array(
      "left" => true,
      "right" => true,
    );
    if (arg(0) == 'subform_related' && is_numeric(arg(1)) && $directions[arg(2)]) {
      $child_side = arg(2);
      $child_node = $child_side . "_node";
      $child_node_type = $child_side . "_node_type";
      $parent_side = $child_side == "left" ? "right" : "left";
      $parent_node = $parent_side . "_node";
      $parent_node_type = $parent_side . "_node_type";

      // it looks like a request to create a new child of a certain node on a certain relation class
      if (is_numeric(arg(3)) && arg(4) == 'add' && arg(5) == null) {
        $relation_class_node = node_load(arg(1));
        $parent_node = arg(3) > 0 ? node_load(arg(3)) : arg(3);
        if ($relation_class_node->type == "relation_class" && (is_numeric($parent_node) && $parent_node < 0 || $parent_node->type == $relation_class_node->{$parent_node_type})) {
          $items[] = array(
            // subform_related / relation class id / side / parent node / add
            'path' => 'subform_related/' . arg(1) . '/' . arg(2) . '/' . arg(3) . '/add',
            'title' => t($name),
            'access' => node_access('create', $relation_class_node->{$child_node_type}),
            'callback' => '_subform_related_add',
            'callback arguments' => array(
              $relation_class_node,
              $parent_node,
              $child_side,
            ),
            'type' => MENU_CALLBACK,
          );
        }
      }
      else {
        if ($operations[arg(3)]) {
          $relation_instance_node = relation_instance_load(arg(1));
          if (isset($relation_instance_node->id)) {
            $node = node_load($relation_instance_node->{$child_node});
            if ($node) {
              $items[] = array(
                // subform_related / 1                    / right/ hoist
                // subform_related / relation instance id / side / operation
                'path' => 'subform_related/' . arg(1) . '/' . arg(2) . '/' . arg(3),
                'title' => t(arg(3)),
                'callback' => '_subform_related_page',
                'callback arguments' => array(
                  $relation_instance_node,
                  $node->nid,
                ),
                'access' => arg(3) == "hoist" ? true : node_access($node_access[arg(3)], $node),
                'type' => MENU_CALLBACK,
              );
            }
          }
        }
      }
    }
    if (arg(0) == 'subform_select' && is_string(arg(1)) && is_string(arg(2)) && is_numeric(arg(3)) && $directions[arg(4)]) {
      if ($result = db_fetch_object(db_query("SELECT * FROM {node_field_instance} WHERE type_name = '" . arg(1) . "' AND field_name = '" . arg(2) . "'"))) {
        $items[] = array(
          // type_name / field_name / relation class / direction
          'path' => 'subform_select/' . arg(1) . '/' . arg(2) . '/' . arg(3) . '/' . arg(4),
          'title' => t("Select"),
          'callback' => '_subform_select',
          'callback arguments' => array(
            $result,
            arg(3),
            arg(4),
          ),
          'access' => TRUE,
          'type' => MENU_CALLBACK,
        );
      }
    }
  }
  return $items;
}