You are here

function _pmtask_get_tree in Drupal PM (Project Management) 7

Provides a tree of tasks for a given project and (if selected) parent task.

5 calls to _pmtask_get_tree()
pmexpense_form in pmexpense/pmexpense.module
Implements hook_pmexpense_form().
pmnote_form in pmnote/pmnote.module
Implements hook_form().
pmtask_form in pmtask/pmtask.module
Implements hook_form().
pmticket_form in pmticket/pmticket.module
Implements hook_form().
pmtimetracking_form in pmtimetracking/pmtimetracking.module
Implements hook_form().

File

pmtask/pmtask.module, line 669

Code

function _pmtask_get_tree($project_nid, $parent_nid = 0, $depth = -1, $max_depth = NULL, $where = NULL, $args = array()) {
  static $children, $parents, $tasks;
  $depth++;

  // We cache trees, so it's not CPU-intensive to call get_tree() on a term
  // and its children, too.
  if (!isset($children[$project_nid])) {
    $children[$project_nid] = array();
    $args = array_merge($args, array(
      $project_nid,
    ));
    $query = db_select('node', 'n');
    $query
      ->join('pmtask', 'pmta', 'n.vid = pmta.vid');
    $query
      ->fields('n', array(
      'title',
      'uid',
    ))
      ->fields('pmta')
      ->condition('n.status', 1)
      ->condition('n.type', 'pmtask')
      ->condition('pmta.project_nid', $args)
      ->addTag('node_access')
      ->orderBy('pmta.weight', 'ASC');
    $result = $query
      ->execute();
    foreach ($result as $task) {
      $children[$project_nid][$task->parent_nid][] = $task->nid;
      $parents[$project_nid][$task->nid][] = $task->parent_nid;
      $tasks[$project_nid][$task->nid] = $task;
    }
  }
  $max_depth = is_null($max_depth) ? count($children[$project_nid]) : $max_depth;
  if (isset($children[$project_nid][$parent_nid]) && $children[$project_nid][$parent_nid]) {
    foreach ($children[$project_nid][$parent_nid] as $child_nid) {
      if ($max_depth > $depth) {
        $task = clone $tasks[$project_nid][$child_nid];
        $task->depth = $depth;
        $task->parents = $parents[$project_nid][$child_nid];
        $tree[] = $task;
        if (isset($children[$project_nid][$child_nid])) {
          $tree = array_merge($tree, _pmtask_get_tree($project_nid, $child_nid, $depth, $max_depth, $where, $args));
        }
      }
    }
  }
  return isset($tree) ? $tree : array();
}