<?php
/* $Id: overview.inc.php,v 1.7 2005/03/29 17:10:59 k-fish Exp $ */

// adding file revision into global array
if(function_exists("revisionInit")) revisionInit("\$Revision: 1.7 $", __FILE__);

// Function project_overview_list
// This should print a nice ordered list for the overview page
// Changed function Mar 2005 to find ALL sub-tasks.  MLK
function project_overview_list($projectid,$parentid,$indent,&$rows) {
  global $conn;
  // select tasks for project, sort by startdate
  $sql = 'SELECT assignee, task_id, title, description, hours, usedhours, billable, comments FROM mgw_projects_tasks WHERE project_id='.$projectid.' AND status=1 AND parent_id='.$parentid.' ORDER BY startdate';
  if(!$tasks = $conn->Execute($sql))
  exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  while($task = $tasks->FetchRow()) {
    if ( hasAssignedSubTasks($projectid, $task['task_id']) ||
    $task['assignee'] == $_SESSION['MGW']->userid) {
      // Bracket tasks which are not assigned to the current user, but user has assigned subtasks
      if ($task['assignee'] <> $_SESSION['MGW']->userid) {
        $task['title'] = '(' . $task['title'] . ')';
      }
      $task['indent'] = $indent;
      $row['tasks'][] = $task;
      $rows[] = $row;
      unset($row);
    }
    //Recurssive
    project_overview_list($projectid,$task['task_id'],$indent+10,$rows);
  }
}

// Added this function...MLK
// See if this this task has any sub-tasks (or sub-sub-tasks, or sub-sub-sub-tasks, etc)
// assigned to the given assignee
// March 22, 2005 by Marc Kelsey (kelsey@agency71.com)
function hasAssignedSubTasks($projectid, $taskID) {
  global $conn;
  // select tasks for project, sort by startdate
  $hasTasks = false;
  $sql = 'SELECT assignee, task_id FROM mgw_projects_tasks WHERE project_id='.$projectid.' AND status=1 AND parent_id='.$taskID;
  if(!$tasks = $conn->Execute($sql))
  exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
  while($task = $tasks->FetchRow()) {
    if ($task['assignee'] == $_SESSION['MGW']->userid) {
      $hasTasks = true;
    }
    else {
      $hasTasks = hasAssignedSubTasks($projectid, $task['task_id']);
    }
    if ($hasTasks) {
      break;
    }
  }
  return $hasTasks;
}


// =============================================================
// NEWS fuer Uebersicht holen
// =============================================================
function getprojects() {
  global $smarty, $conn, $myEnv;

  if(!query_access_module('projects'))
  return '';

  $rows = array();
  // tdawson: Sorting Projects display by StartDate
  $sql = 'SELECT DISTINCT project_id FROM mgw_projects_tasks WHERE status=1 AND assignee='.$_SESSION['MGW']->userid;
  if(!$projects = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));

  while($project = $projects->FetchRow()) {
    // fetch project name
    $sql = 'SELECT title from mgw_projects WHERE project_id='.$project['project_id'];
    if(!$pname = $conn->GetOne($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    $row['pid'] = $project['project_id'];
    $row['ptitle'] = $pname;

    // Push Project Info into Array
    $rows[]=$row;
    // Fetch all the tasks
    project_overview_list($row['pid'],0,0,$rows);
  }
  $smarty->assign('projects', $rows);
  return $smarty->fetch("file:".ROOTPATH."/modules/projects/templates/default/".$myEnv["output"]."/projects_overview.tpl");
}

?>