You are here

function _draggableviews_ascending_numbers in DraggableViews 7

Same name and namespace in other branches
  1. 6.3 draggableviews.inc \_draggableviews_ascending_numbers()

(CHECK/WRITE) Ascending Numbers.

This function is used for both renumbering and checking. Order values have to start with $offset.

Parameters

$info: The structured info array. Look at _draggableviews_info(..) to learn more.

$offset: Where we start to count.

$renumber: Renumber or check.

Return value

TRUE if no errors found.

4 calls to _draggableviews_ascending_numbers()
_draggableviews_build_hierarchy in ./draggableviews.inc
Build hierarchy
_draggableviews_click_sort in ./draggableviews.inc
Click Sort
_draggableviews_quick_check_structure in ./draggableviews.inc
_draggableviews_rebuild_hierarchy in ./draggableviews.inc
Rebuild hierarchy

File

./draggableviews.inc, line 451
Draggableviews processing functions. Rough summary of what functions in this file do:

Code

function _draggableviews_ascending_numbers(&$info, $offset = 0, $renumber = FALSE) {

  // We need to hold
  //  1)  the last nid,
  //  2)  the last order value,
  //  3)  the parent's nid of each hierarchy level,
  //  3a) the current depth (hierarchy level),
  $last_nid = -1;
  $last_order = $offset;
  $last_parent_nid = array(
    0,
  );
  $depth = 0;
  foreach ($info['nodes'] as $nid => $values) {
    $parent_nid = isset($info['hierarchy']) ? $values['parent'] : 0;

    // Let's take a look at the following expample, to understand what is beeing done.
    // The order value of the parent's level always equals with the parent's value.
    //
    // A     (0 - -) First node of level0.
    // --B   (0 1 -) First node of level1. But we continue counting.
    //   --C (0 1 2) First node of level2. But we still continue counting.
    // X     (3 - -) Now we leave 2 levels (level2->level0). We still have to continue counting.
    //
    // Imagine we are currently processing X. We are leaving 2 levels and we need to determine the
    // last order value that was used. First we check how many levels are beeing left:
    $leave_hierarchy_levels = 0;
    for ($i = 0; $i < $depth; $i++) {
      if ($parent_nid == $last_parent_nid[$i]) {

        // Found the level we go to. Calculate the number of levels that are beeing left.
        $leave_hierarchy_levels = $depth - $i;
        break;
      }
    }
    if ($leave_hierarchy_levels > 0) {

      // We leave some hierarchy levels. We simply inherit the order value of the level we come from.
      $depth -= $leave_hierarchy_levels;

      // Remove obsoleted values from stack.
      for ($i = 0; $i < $leave_hierarchy_levels; $i++) {
        array_pop($last_parent_nid);
      }
    }
    elseif ($parent_nid == $last_nid) {

      // We are entering a new level. This is the first child of the last node.
      array_push($last_parent_nid, $last_nid);
      $depth++;
    }
    elseif ($parent_nid != $last_parent_nid[$depth]) {

      // This node is neither a member of any previous hierarchy level
      // nor a child of the last node (opening a new level)
      // nor a member of the current hierarchy level
      // There's something wrong!
      if ($renumber) {
        drupal_set_message(t("Something wrong in _draggableviews_ascending_numbers (@number).", array(
          '@number' => $renumber ? 'WRITE' : 'CHECK',
        )), "error");
      }
      return FALSE;
    }

    // This function is used for both renumbering and checking the hierarchy. Decide what we have to do:
    if ($renumber) {

      // Assign order value.
      $info['nodes'][$nid]['order'][0] = $last_order;
    }
    else {

      // Check structure.
      $order = $values['order'][0];
      if ($order != $last_order) {

        // This would cause troubles with paging.
        // We better initiate a rebuild!
        return FALSE;
      }
    }
    $last_nid = $nid;
    $last_order++;
  }
  return TRUE;
}