<?php
// Key of Project in List
$key = $_SESSION['project_id'];

// Control if actualize field  of tasks of project

$Updated=0;
if ( $Updated == 0 ) {

// Create tables auxiliares of order
$sql = "SELECT task_id,if (parent IS NULL,'',parent) parent , name, projects_project_id, start_date, end_date
FROM tasks Where projects_project_id = $key order by start_date";

$results = array(); // All Tasks of Project

if ($resql = DB::Query($sql)) {
    /* obtener array asociativo */
    while ($row = $resql->fetchAssoc()) {
        $results[] = $row;
    }
}

// Create ARRAY with  Hierarchy of tasks
function convertToHierarchy($results, $idField='task_id', $parentIdField='parent', $childrenField='children') {
	$hierarchy = array(); // -- Stores the final data
	$itemReferences = array(); // -- temporary array, storing references to all items in a single-dimention

	foreach ( $results as $item ) {
		$id       = $item[$idField];
		$parentId = $item[$parentIdField];
		if (isset($itemReferences[$parentId])) { // parent exists
			$itemReferences[$parentId][$childrenField][$id] = $item; // assign item to parent
			$itemReferences[$id] =& $itemReferences[$parentId][$childrenField][$id]; // reference parent's item in single-dimentional array
		} elseif (!$parentId || !isset($hierarchy[$parentId])) { // -- parent Id empty or does not exist. Add it to the root
			$hierarchy[$id] = $item;
			$itemReferences[$id] =& $hierarchy[$id];
		}
	}
	unset($results, $item, $id, $parentId);
	// -- Run through the root one more time. If any child got added before it's parent, fix it.
	foreach ( $hierarchy as $id => &$item ) {
		$parentId = $item[$parentIdField];

		if ( isset($itemReferences[$parentId] ) ) { // -- parent DOES exist
			$itemReferences[$parentId][$childrenField][$id] = $item; // -- assign it to the parent's list of children
			unset($hierarchy[$id]); // -- remove it from the root of the hierarchy
		}
	}
	unset($itemReferences, $id, $item, $parentId);
	return $hierarchy;
}
// Transforma el ARRAY de la jerarquía en un ARRAY plano y añade información de Orden, Nivel y si es Registro Maestro ( tiene registros dependientes)
// Transform the array of the hierarchy into a flat array and adds order information, level and if it is a master record (it has dependent records)
function makeOneLevelArray($in, &$out, $level, &$num_record) {
    foreach ($in as $v) {
        $num_record++;
        $out[$num_record] = $v;
        $out[$num_record]['order'] = $num_record;
        $out[$num_record]['level'] = $level;
        if (!empty($v['children'])) {
            $out[$num_record]['Maestro'] = 1;
            unset($out[$num_record]['children']); //  delete children of array of out
            makeOneLevelArray($v['children'], $out, $level+1, $num_record );
        } else {
           $out[$num_record]['Maestro'] = 0; 
        }       
    }
}

$Hierarchy = convertToHierarchy($results);

$taskArray = array();
$level=1;
$num_record = 0;
makeOneLevelArray($Hierarchy, $taskArray, $level, $num_record);

foreach ($taskArray as $v) {
	$data = array();
	$keyvalues = array();
	$data["level"] = $v['level'];
	$data["order"]  = $v['order'];
	$data["dynamic"] = $v['Maestro'];
	$keyvalues["task_id"] = $v['task_id'];
	DB::Update("tasks", $data, $keyvalues );
}
 require_once ("MyCode/calc_task_dymanic_and_project.php");
}
?>