ganttchart.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /* Copyright (C) 2010-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/projet/ganttchart.inc.php
  19. * \ingroup projet
  20. * \brief Gantt diagram of a project
  21. */
  22. ?>
  23. <div id="principal_content" style="margin-left: 0;">
  24. <div style="margin-left: 0; position: relative;" class="gantt" id="GanttChartDIV"></div>
  25. <script type="text/javascript">
  26. function DisplayHideRessources(boxName) {
  27. graphFormat = g.getFormat();
  28. if(boxName.checked == true) {
  29. booShowRessources = 1;
  30. }
  31. else {
  32. booShowRessources = 0;
  33. }
  34. reloadGraph();
  35. }
  36. function DisplayHideDurations(boxName) {
  37. graphFormat = g.getFormat();
  38. if(boxName.checked == true) {
  39. booShowDurations = 1;
  40. }
  41. else {
  42. booShowDurations = 0;
  43. }
  44. reloadGraph();
  45. }
  46. function DisplayHideComplete(boxName) {
  47. graphFormat = g.getFormat();
  48. if(boxName.checked == true) {
  49. booShowComplete = 1;
  50. }
  51. else {
  52. booShowComplete = 0;
  53. }
  54. reloadGraph();
  55. }
  56. function selectBarText(value) {
  57. graphFormat = g.getFormat();
  58. id=value.options[value.selectedIndex].value;
  59. barText = id;
  60. reloadGraph();
  61. }
  62. function reloadGraph() {
  63. g.setShowRes(booShowRessources);
  64. g.setShowComp(booShowComplete);
  65. g.setShowDur(booShowDurations);
  66. g.setCaptionType(barText);
  67. g.setFormat(graphFormat);
  68. g.Draw(jQuery("#tabs").width()-40);
  69. }
  70. //var g = new JSGantt.GanttChart('g', document.getElementById('GanttChartDIV'), 'day');
  71. var g = new JSGantt.GanttChart(document.getElementById('GanttChartDIV'), 'day');
  72. if (g.getDivId() != null)
  73. //if (g)
  74. {
  75. var booShowRessources = 1;
  76. var booShowDurations = 1;
  77. var booShowComplete = 1;
  78. var barText = "Resource";
  79. var graphFormat = "day";
  80. g.setDateInputFormat('<?php echo $dateformatinput; ?>'); // Set format of input dates ('mm/dd/yyyy', 'dd/mm/yyyy', does not work with 'yyyy-mm-dd')
  81. g.setDateTaskTableDisplayFormat('<?php echo $dateformat; ?>'); // Format of date used into line
  82. g.setDateTaskDisplayFormat('<?php echo $datehourformat; ?>'); // Format of date used into popup, not into line
  83. g.setDayMajorDateDisplayFormat('dd mon');
  84. g.setShowRes(1); // Show/Hide Responsible (0/1)
  85. g.setShowDur(1); // Show/Hide Duration (0/1)
  86. g.setShowComp(1); // Show/Hide % Complete(0/1)
  87. g.setShowStartDate(1); // Show/Hide % Complete(0/1)
  88. g.setShowEndDate(1); // Show/Hide % Complete(0/1)
  89. g.setShowTaskInfoLink(1);
  90. g.setFormatArr("day","week","month") // Set format options (up to 4 : "minute","hour","day","week","month","quarter")
  91. g.setCaptionType('Caption'); // Set to Show Caption (None,Caption,Resource,Duration,Complete)
  92. g.setUseFade(0);
  93. g.setDayColWidth(20);
  94. /* g.setShowTaskInfoLink(1) */
  95. g.addLang('<?php print $langs->getDefaultLang(1); ?>', vLangs['<?php print $langs->getDefaultLang(1); ?>']);
  96. g.setLang('<?php print $langs->getDefaultLang(1); ?>');
  97. <?php
  98. echo "\n";
  99. echo "/* g.AddTaskItem(new JSGantt.TaskItem(task_id, 'label', 'start_date', 'end_date', 'css', 'link', milestone, 'Resources', Compl%, Group, Parent, Open, 'Dependency', 'label','note', g)); */\n";
  100. $level = 0;
  101. $tnums = count($tasks);
  102. $old_project_id = 0;
  103. for ($tcursor = 0; $tcursor < $tnums; $tcursor++) {
  104. $t = $tasks[$tcursor];
  105. if (empty($old_project_id) || $old_project_id != $t['task_project_id']) {
  106. // Break on project, create a fictive task for project id $t['task_project_id']
  107. $projecttmp = new Project($db);
  108. $projecttmp->fetch($t['task_project_id']);
  109. $tmpt = array(
  110. 'task_id'=> '-'.$t['task_project_id'],
  111. 'task_alternate_id'=> '-'.$t['task_project_id'],
  112. 'task_name'=>$projecttmp->ref.' '.$projecttmp->title,
  113. 'task_resources'=>'',
  114. 'task_start_date'=>'',
  115. 'task_end_date'=>'',
  116. 'task_is_group'=>1, 'task_position'=>0, 'task_css'=>'ggroupblack', 'task_milestone'=> 0, 'task_parent'=>0, 'task_parent_alternate_id'=>0,
  117. 'task_notes'=>'',
  118. 'task_planned_workload'=>0
  119. );
  120. constructGanttLine($tasks, $tmpt, array(), 0, $t['task_project_id']);
  121. $old_project_id = $t['task_project_id'];
  122. }
  123. if ($t["task_parent"] <= 0) {
  124. constructGanttLine($tasks, $t, $task_dependencies, $level, $t['task_project_id']);
  125. findChildGanttLine($tasks, $t["task_id"], $task_dependencies, $level + 1);
  126. }
  127. }
  128. echo "\n";
  129. ?>
  130. g.Draw(jQuery("#tabs").width()-40);
  131. setTimeout('g.DrawDependencies()',100);
  132. }
  133. else
  134. {
  135. alert("<?php echo $langs->trans("FailedToDefinGraph"); ?>");
  136. }
  137. </script>
  138. </div>
  139. <?php
  140. /**
  141. * Add a gant chart line
  142. *
  143. * @param array $tarr Array of all tasks
  144. * @param array $task Array with properties of one task
  145. * @param array $task_dependencies Task dependencies (array(array(0=>idtask,1=>idtasktofinishfisrt))
  146. * @param int $level Level
  147. * @param int $project_id Id of project
  148. * @return void
  149. */
  150. function constructGanttLine($tarr, $task, $task_dependencies, $level = 0, $project_id = null)
  151. {
  152. global $langs;
  153. global $dateformatinput2;
  154. $start_date = $task["task_start_date"];
  155. $end_date = $task["task_end_date"];
  156. if (!$end_date) {
  157. $end_date = $start_date;
  158. }
  159. $start_date = dol_print_date($start_date, $dateformatinput2);
  160. $end_date = dol_print_date($end_date, $dateformatinput2);
  161. // Resources
  162. $resources = $task["task_resources"];
  163. // Define depend (ex: "", "4,13", ...)
  164. $depend = '';
  165. $count = 0;
  166. foreach ($task_dependencies as $value) {
  167. // Not yet used project_dependencies = array(array(0=>idtask,1=>idtasktofinishfisrt))
  168. if ($value[0] == $task['task_id']) {
  169. $depend .= ($count > 0 ? "," : "").$value[1];
  170. $count++;
  171. }
  172. }
  173. // $depend .= "\"";
  174. // Define parent
  175. if ($project_id && $level < 0) {
  176. $parent = '-'.$project_id;
  177. } else {
  178. $parent = $task["task_parent_alternate_id"];
  179. //$parent = $task["task_parent"];
  180. }
  181. // Define percent
  182. $percent = $task['task_percent_complete'] ? $task['task_percent_complete'] : 0;
  183. // Link (more information)
  184. if ($task["task_id"] < 0) {
  185. //$link=DOL_URL_ROOT.'/projet/tasks.php?withproject=1&id='.abs($task["task_id"]);
  186. $link = '';
  187. } else {
  188. $link = DOL_URL_ROOT.'/projet/tasks/contact.php?withproject=1&id='.$task["task_id"];
  189. }
  190. // Name
  191. //$name='<a href="'.DOL_URL_ROOT.'/projet/task/tasks.php?id='.$task['task_id'].'">'.$task['task_name'].'</a>';
  192. $name = $task['task_name'];
  193. /*for($i=0; $i < $level; $i++) {
  194. $name=' - '.$name;
  195. }*/
  196. // Add line to gantt
  197. /*
  198. g.AddTaskItem(new JSGantt.TaskItem(1, 'Define Chart API','', '', 'ggroupblack','', 0, 'Brian', 0, 1,0,1,'','','Some Notes text',g));
  199. g.AddTaskItem(new JSGantt.TaskItem(11,'Chart Object', '2014-02-20','2014-02-20','gmilestone', '', 1, 'Shlomy',100,0,1,1,'','','',g));
  200. </pre>
  201. <p>Method definition:
  202. <strong>TaskItem(<em>pID, pName, pStart, pEnd, pColor, pLink, pMile, pRes, pComp, pGroup, pParent, pOpen, pDepend, pCaption, pNotes, pGantt</em>)</strong></p>
  203. <dl>
  204. <dt>pID</dt><dd>(required) a unique numeric ID used to identify each row</dd>
  205. <dt>pName</dt><dd>(required) the task Label</dd>
  206. <dt>pStart</dt><dd>(required) the task start date, can enter empty date ('') for groups. You can also enter specific time (2014-02-20 12:00) for additional precision.</dd>
  207. <dt>pEnd</dt><dd>(required) the task end date, can enter empty date ('') for groups</dd>
  208. <dt>pClass</dt><dd>(required) the css class for this task</dd>
  209. <dt>pLink</dt><dd>(optional) any http link to be displayed in tool tip as the "More information" link.</dd>
  210. <dt>pMile</dt><dd>(optional) indicates whether this is a milestone task - Numeric; 1 = milestone, 0 = not milestone</dd>
  211. <dt>pRes</dt><dd>(optional) resource name</dd>
  212. <dt>pComp</dt><dd>(required) completion percent, numeric</dd>
  213. <dt>pGroup</dt><dd>(optional) indicates whether this is a group task (parent) - Numeric; 0 = normal task, 1 = standard group task, 2 = combined group task<a href='#combinedtasks' class="footnote">*</a></dd>
  214. <dt>pParent</dt><dd>(required) identifies a parent pID, this causes this task to be a child of identified task. Numeric, top level tasks should have pParent set to 0</dd>
  215. <dt>pOpen</dt><dd>(required) indicates whether a standard group task is open when chart is first drawn. Value must be set for all items but is only used by standard group tasks. Numeric, 1 = open, 0 = closed</dd>
  216. <dt>pDepend</dt><dd>(optional) comma separated list of id&#39;s this task is dependent on. A line will be drawn from each listed task to this item<br>Each id can optionally be followed by a dependency type suffix. Valid values are:<blockquote>'FS' - Finish to Start (default if suffix is omitted)<br>'SF' - Start to Finish<br>'SS' - Start to Start<br>'FF' - Finish to Finish</blockquote>If present the suffix must be added directly to the id e.g. '123SS'</dd>
  217. <dt>pCaption</dt><dd>(optional) caption that will be added after task bar if CaptionType set to "Caption"</dd>
  218. <dt>pNotes</dt><dd>(optional) Detailed task information that will be displayed in tool tip for this task</dd>
  219. <dt>pGantt</dt><dd>(required) javascript JSGantt.GanttChart object from which to take settings. Defaults to &quot;g&quot; for backwards compatibility</dd>
  220. */
  221. //$note="";
  222. $s = "\n// Add task level = ".$level." id=".$task["task_id"]." parent_id=".$task["task_parent"]." aternate_id=".$task["task_alternate_id"]." parent_aternate_id=".$task["task_parent_alternate_id"]."\n";
  223. //$task["task_is_group"]=1; // When task_is_group is 1, content will be autocalculated from sum of all low tasks
  224. // For JSGanttImproved
  225. $css = $task['task_css'];
  226. $line_is_auto_group = $task["task_is_group"];
  227. //$line_is_auto_group=0;
  228. //if ($line_is_auto_group) $css = 'ggroupblack';
  229. //$dependency = ($depend?$depend:$parent."SS");
  230. $dependency = '';
  231. //$name = str_repeat("..", $level).$name;
  232. $taskid = $task["task_alternate_id"];
  233. //$taskid = $task['task_id'];
  234. $note = $task['note'];
  235. $note = dol_concatdesc($note, $langs->trans("Workload").' : '.($task['task_planned_workload'] ? convertSecondToTime($task['task_planned_workload'], 'allhourmin') : ''));
  236. $s .= "g.AddTaskItem(new JSGantt.TaskItem('".$taskid."', '".dol_escape_js(trim($name))."', '".$start_date."', '".$end_date."', '".$css."', '".$link."', ".$task['task_milestone'].", '".dol_escape_js($resources)."', ".($percent >= 0 ? $percent : 0).", ".$line_is_auto_group.", '".$parent."', 1, '".$dependency."', '".(empty($task["task_is_group"]) ? (($percent >= 0 && $percent != '') ? $percent.'%' : '') : '')."', '".dol_escape_js($note)."', g));";
  237. echo $s;
  238. }
  239. /**
  240. * Find child Gantt line
  241. *
  242. * @param array $tarr tarr
  243. * @param int $parent Parent
  244. * @param array $task_dependencies Task dependencies
  245. * @param int $level Level
  246. * @return void
  247. */
  248. function findChildGanttLine($tarr, $parent, $task_dependencies, $level)
  249. {
  250. $n = count($tarr);
  251. $old_parent_id = 0;
  252. for ($x = 0; $x < $n; $x++) {
  253. if ($tarr[$x]["task_parent"] == $parent && $tarr[$x]["task_parent"] != $tarr[$x]["task_id"]) {
  254. // Create a grouping parent task for the new level
  255. /*if (empty($old_parent_id) || $old_parent_id != $tarr[$x]['task_project_id'])
  256. {
  257. $tmpt = array(
  258. 'task_id'=> -98, 'task_name'=>'Level '.$level, 'task_resources'=>'', 'task_start_date'=>'', 'task_end_date'=>'',
  259. 'task_is_group'=>1, 'task_css'=>'ggroupblack', 'task_milestone'=> 0, 'task_parent'=>$tarr[$x]["task_parent"], 'task_notes'=>'');
  260. constructGanttLine($tasks, $tmpt, array(), 0, $tarr[$x]['task_project_id']);
  261. $old_parent_id = $tarr[$x]['task_project_id'];
  262. }*/
  263. constructGanttLine($tarr, $tarr[$x], $task_dependencies, $level, null);
  264. findChildGanttLine($tarr, $tarr[$x]["task_id"], $task_dependencies, $level + 1);
  265. }
  266. }
  267. }