project.lib.php 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. <?php
  2. /* Copyright (C) 2006-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2010 Regis Houssin <regis.houssin@capnetworks.com>
  4. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * or see http://www.gnu.org/
  19. */
  20. /**
  21. * \file htdocs/core/lib/project.lib.php
  22. * \brief Functions used by project module
  23. * \ingroup project
  24. */
  25. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  26. /**
  27. * Prepare array with list of tabs
  28. *
  29. * @param Object $object Object related to tabs
  30. * @return array Array of tabs to show
  31. */
  32. function project_prepare_head($object)
  33. {
  34. global $langs, $conf, $user;
  35. $h = 0;
  36. $head = array();
  37. $head[$h][0] = DOL_URL_ROOT.'/projet/card.php?id='.$object->id;
  38. $head[$h][1] = $langs->trans("Project");
  39. $head[$h][2] = 'project';
  40. $h++;
  41. $head[$h][0] = DOL_URL_ROOT.'/projet/contact.php?id='.$object->id;
  42. $head[$h][1] = $langs->trans("ProjectContact");
  43. $head[$h][2] = 'contact';
  44. $h++;
  45. if (! empty($conf->fournisseur->enabled) || ! empty($conf->propal->enabled) || ! empty($conf->commande->enabled)
  46. || ! empty($conf->facture->enabled) || ! empty($conf->contrat->enabled)
  47. || ! empty($conf->ficheinter->enabled) || ! empty($conf->agenda->enabled) || ! empty($conf->deplacement->enabled))
  48. {
  49. $head[$h][0] = DOL_URL_ROOT.'/projet/element.php?id='.$object->id;
  50. $head[$h][1] = $langs->trans("Overview");
  51. $head[$h][2] = 'element';
  52. $h++;
  53. }
  54. // Show more tabs from modules
  55. // Entries must be declared in modules descriptor with line
  56. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  57. // $this->tabs = array('entity:-tabname); to remove a tab
  58. complete_head_from_modules($conf,$langs,$object,$head,$h,'project');
  59. if (empty($conf->global->MAIN_DISABLE_NOTES_TAB))
  60. {
  61. $nbNote = 0;
  62. if(!empty($object->note_private)) $nbNote++;
  63. if(!empty($object->note_public)) $nbNote++;
  64. $head[$h][0] = DOL_URL_ROOT.'/projet/note.php?id='.$object->id;
  65. $head[$h][1] = $langs->trans('Notes');
  66. if ($nbNote > 0) $head[$h][1].= ' <span class="badge">'.$nbNote.'</span>';
  67. $head[$h][2] = 'notes';
  68. $h++;
  69. }
  70. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  71. $upload_dir = $conf->projet->dir_output . "/" . dol_sanitizeFileName($object->ref);
  72. $nbFiles = count(dol_dir_list($upload_dir,'files',0,'','(\.meta|_preview\.png)$'));
  73. $head[$h][0] = DOL_URL_ROOT.'/projet/document.php?id='.$object->id;
  74. $head[$h][1] = $langs->trans('Documents');
  75. if($nbFiles > 0) $head[$h][1].= ' <span class="badge">'.$nbFiles.'</span>';
  76. $head[$h][2] = 'document';
  77. $h++;
  78. if (empty($conf->global->PROJECT_HIDE_TASKS))
  79. {
  80. // Then tab for sub level of projet, i mean tasks
  81. $head[$h][0] = DOL_URL_ROOT.'/projet/tasks.php?id='.$object->id;
  82. $head[$h][1] = $langs->trans("Tasks");
  83. $head[$h][2] = 'tasks';
  84. $h++;
  85. $head[$h][0] = DOL_URL_ROOT.'/projet/ganttview.php?id='.$object->id;
  86. $head[$h][1] = $langs->trans("Gantt");
  87. $head[$h][2] = 'gantt';
  88. $h++;
  89. }
  90. complete_head_from_modules($conf,$langs,$object,$head,$h,'project','remove');
  91. return $head;
  92. }
  93. /**
  94. * Prepare array with list of tabs
  95. *
  96. * @param Object $object Object related to tabs
  97. * @return array Array of tabs to show
  98. */
  99. function task_prepare_head($object)
  100. {
  101. global $langs, $conf, $user;
  102. $h = 0;
  103. $head = array();
  104. $head[$h][0] = DOL_URL_ROOT.'/projet/tasks/task.php?id='.$object->id.(GETPOST('withproject')?'&withproject=1':'');
  105. $head[$h][1] = $langs->trans("Card");
  106. $head[$h][2] = 'task_task';
  107. $h++;
  108. $head[$h][0] = DOL_URL_ROOT.'/projet/tasks/contact.php?id='.$object->id.(GETPOST('withproject')?'&withproject=1':'');
  109. $head[$h][1] = $langs->trans("TaskRessourceLinks");
  110. $head[$h][2] = 'task_contact';
  111. $h++;
  112. $head[$h][0] = DOL_URL_ROOT.'/projet/tasks/time.php?id='.$object->id.(GETPOST('withproject')?'&withproject=1':'');
  113. $head[$h][1] = $langs->trans("TimeSpent");
  114. $head[$h][2] = 'task_time';
  115. $h++;
  116. // Show more tabs from modules
  117. // Entries must be declared in modules descriptor with line
  118. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  119. // $this->tabs = array('entity:-tabname); to remove a tab
  120. complete_head_from_modules($conf,$langs,$object,$head,$h,'task');
  121. if (empty($conf->global->MAIN_DISABLE_NOTES_TAB))
  122. {
  123. $nbNote = 0;
  124. if(!empty($object->note_private)) $nbNote++;
  125. if(!empty($object->note_public)) $nbNote++;
  126. $head[$h][0] = DOL_URL_ROOT.'/projet/tasks/note.php?id='.$object->id.(GETPOST('withproject')?'&withproject=1':'');
  127. $head[$h][1] = $langs->trans('Notes');
  128. if ($nbNote > 0) $head[$h][1].= ' <span class="badge">'.$nbNote.'</span>';
  129. $head[$h][2] = 'task_notes';
  130. $h++;
  131. }
  132. $head[$h][0] = DOL_URL_ROOT.'/projet/tasks/document.php?id='.$object->id.(GETPOST('withproject')?'&withproject=1':'');
  133. $filesdir = $conf->projet->dir_output . "/" . dol_sanitizeFileName($object->project->ref) . '/' .dol_sanitizeFileName($object->ref);
  134. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  135. $listoffiles=dol_dir_list($filesdir,'files',1,'','thumbs');
  136. $head[$h][1] = (count($listoffiles)?$langs->trans('DocumentsNb',count($listoffiles)):$langs->trans('Documents'));
  137. $head[$h][2] = 'task_document';
  138. $h++;
  139. complete_head_from_modules($conf,$langs,$object,$head,$h,'task','remove');
  140. return $head;
  141. }
  142. /**
  143. * Prepare array with list of tabs
  144. *
  145. * @param string $mode Mode
  146. * @return array Array of tabs to show
  147. */
  148. function project_timesheet_prepare_head($mode)
  149. {
  150. global $langs, $conf, $user;
  151. $h = 0;
  152. $head = array();
  153. $h = 0;
  154. if (empty($conf->global->PROJECT_DISABLE_TIMESHEET_PERWEEK))
  155. {
  156. $head[$h][0] = DOL_URL_ROOT."/projet/activity/perweek.php".($mode?'?mode='.$mode:'');
  157. $head[$h][1] = $langs->trans("InputPerWeek");
  158. $head[$h][2] = 'inputperweek';
  159. $h++;
  160. }
  161. if (empty($conf->global->PROJECT_DISABLE_TIMESHEET_PERTIME))
  162. {
  163. $head[$h][0] = DOL_URL_ROOT."/projet/activity/perday.php".($mode?'?mode='.$mode:'');
  164. $head[$h][1] = $langs->trans("InputPerDay");
  165. $head[$h][2] = 'inputperday';
  166. $h++;
  167. }
  168. /*if (empty($conf->global->PROJECT_DISABLE_TIMESHEET_PERACTION))
  169. {
  170. $head[$h][0] = DOL_URL_ROOT."/projet/activity/peraction.php".($mode?'?mode='.$mode:'');
  171. $head[$h][1] = $langs->trans("InputPerAction");
  172. $head[$h][2] = 'inputperaction';
  173. $h++;
  174. }*/
  175. complete_head_from_modules($conf,$langs,null,$head,$h,'project_timesheet');
  176. complete_head_from_modules($conf,$langs,null,$head,$h,'project_timesheet','remove');
  177. return $head;
  178. }
  179. /**
  180. * Prepare array with list of tabs
  181. *
  182. * @return array Array of tabs to show
  183. */
  184. function project_admin_prepare_head()
  185. {
  186. global $langs, $conf, $user;
  187. $h = 0;
  188. $head = array();
  189. $h = 0;
  190. $head[$h][0] = DOL_URL_ROOT."/projet/admin/project.php";
  191. $head[$h][1] = $langs->trans("Projects");
  192. $head[$h][2] = 'project';
  193. $h++;
  194. complete_head_from_modules($conf,$langs,null,$head,$h,'project_admin');
  195. $head[$h][0] = DOL_URL_ROOT."/projet/admin/project_extrafields.php";
  196. $head[$h][1] = $langs->trans("ExtraFieldsProject");
  197. $head[$h][2] = 'attributes';
  198. $h++;
  199. $head[$h][0] = DOL_URL_ROOT.'/projet/admin/project_task_extrafields.php';
  200. $head[$h][1] = $langs->trans("ExtraFieldsProjectTask");
  201. $head[$h][2] = 'attributes_task';
  202. $h++;
  203. complete_head_from_modules($conf,$langs,null,$head,$h,'project_admin','remove');
  204. return $head;
  205. }
  206. /**
  207. * Show task lines with a particular parent
  208. *
  209. * @param string $inc Line number (start to 0, then increased by recursive call)
  210. * @param string $parent Id of parent project to show (0 to show all)
  211. * @param Task[] $lines Array of lines
  212. * @param int $level Level (start to 0, then increased/decrease by recursive call)
  213. * @param string $var Color
  214. * @param int $showproject Show project columns
  215. * @param int $taskrole Array of roles of user for each tasks
  216. * @param int $projectsListId List of id of project allowed to user (string separated with comma)
  217. * @param int $addordertick Add a tick to move task
  218. * @return void
  219. */
  220. function projectLinesa(&$inc, $parent, &$lines, &$level, $var, $showproject, &$taskrole, $projectsListId='', $addordertick=0)
  221. {
  222. global $user, $bc, $langs;
  223. global $projectstatic, $taskstatic;
  224. $lastprojectid=0;
  225. $projectsArrayId=explode(',',$projectsListId);
  226. $numlines=count($lines);
  227. // We declare counter as global because we want to edit them into recursive call
  228. global $total_projectlinesa_spent,$total_projectlinesa_planned,$total_projectlinesa_spent_if_planned;
  229. if ($level == 0)
  230. {
  231. $total_projectlinesa_spent=0;
  232. $total_projectlinesa_planned=0;
  233. $total_projectlinesa_spent_if_planned=0;
  234. }
  235. for ($i = 0 ; $i < $numlines ; $i++)
  236. {
  237. if ($parent == 0) $level = 0;
  238. // Process line
  239. // print "i:".$i."-".$lines[$i]->fk_project.'<br>';
  240. if ($lines[$i]->fk_parent == $parent)
  241. {
  242. // Show task line.
  243. $showline=1;
  244. $showlineingray=0;
  245. // If there is filters to use
  246. if (is_array($taskrole))
  247. {
  248. // If task not legitimate to show, search if a legitimate task exists later in tree
  249. if (! isset($taskrole[$lines[$i]->id]) && $lines[$i]->id != $lines[$i]->fk_parent)
  250. {
  251. // So search if task has a subtask legitimate to show
  252. $foundtaskforuserdeeper=0;
  253. searchTaskInChild($foundtaskforuserdeeper,$lines[$i]->id,$lines,$taskrole);
  254. //print '$foundtaskforuserpeeper='.$foundtaskforuserdeeper.'<br>';
  255. if ($foundtaskforuserdeeper > 0)
  256. {
  257. $showlineingray=1; // We will show line but in gray
  258. }
  259. else
  260. {
  261. $showline=0; // No reason to show line
  262. }
  263. }
  264. }
  265. else
  266. {
  267. // Caller did not ask to filter on tasks of a specific user (this probably means he want also tasks of all users, into public project
  268. // or into all other projects if user has permission to).
  269. if (empty($user->rights->projet->all->lire))
  270. {
  271. // User is not allowed on this project and project is not public, so we hide line
  272. if (! in_array($lines[$i]->fk_project, $projectsArrayId))
  273. {
  274. // Note that having a user assigned to a task into a project user has no permission on, should not be possible
  275. // because assignement on task can be done only on contact of project.
  276. // If assignement was done and after, was removed from contact of project, then we can hide the line.
  277. $showline=0;
  278. }
  279. }
  280. }
  281. if ($showline)
  282. {
  283. // Break on a new project
  284. if ($parent == 0 && $lines[$i]->fk_project != $lastprojectid)
  285. {
  286. $var = !$var;
  287. $lastprojectid=$lines[$i]->fk_project;
  288. }
  289. print '<tr '.$bc[$var].' id="row-'.$lines[$i]->id.'">'."\n";
  290. if ($showproject)
  291. {
  292. // Project ref
  293. print "<td>";
  294. if ($showlineingray) print '<i>';
  295. $projectstatic->id=$lines[$i]->fk_project;
  296. $projectstatic->ref=$lines[$i]->projectref;
  297. $projectstatic->public=$lines[$i]->public;
  298. if ($lines[$i]->public || in_array($lines[$i]->fk_project,$projectsArrayId)) print $projectstatic->getNomUrl(1);
  299. else print $projectstatic->getNomUrl(1,'nolink');
  300. if ($showlineingray) print '</i>';
  301. print "</td>";
  302. // Project status
  303. print '<td>';
  304. $projectstatic->statut=$lines[$i]->projectstatus;
  305. print $projectstatic->getLibStatut(2);
  306. print "</td>";
  307. }
  308. // Ref of task
  309. print '<td>';
  310. if ($showlineingray)
  311. {
  312. print '<i>'.img_object('','projecttask').' '.$lines[$i]->ref.'</i>';
  313. }
  314. else
  315. {
  316. $taskstatic->id=$lines[$i]->id;
  317. $taskstatic->ref=$lines[$i]->ref;
  318. $taskstatic->label=($taskrole[$lines[$i]->id]?$langs->trans("YourRole").': '.$taskrole[$lines[$i]->id]:'');
  319. print $taskstatic->getNomUrl(1,'withproject');
  320. }
  321. print '</td>';
  322. // Title of task
  323. print "<td>";
  324. if ($showlineingray) print '<i>';
  325. else print '<a href="'.DOL_URL_ROOT.'/projet/tasks/task.php?id='.$lines[$i]->id.($showproject?'':'&withproject=1').'">';
  326. for ($k = 0 ; $k < $level ; $k++)
  327. {
  328. print "&nbsp; &nbsp; &nbsp;";
  329. }
  330. print $lines[$i]->label;
  331. if ($showlineingray) print '</i>';
  332. else print '</a>';
  333. print "</td>\n";
  334. // Date start
  335. print '<td align="center">';
  336. print dol_print_date($lines[$i]->date_start,'dayhour');
  337. print '</td>';
  338. // Date end
  339. print '<td align="center">';
  340. print dol_print_date($lines[$i]->date_end,'dayhour');
  341. print '</td>';
  342. $plannedworkloadoutputformat='allhourmin';
  343. $timespentoutputformat='allhourmin';
  344. if (! empty($conf->global->PROJECT_PLANNED_WORKLOAD_FORMAT)) $plannedworkloadoutputformat=$conf->global->PROJECT_PLANNED_WORKLOAD_FORMAT;
  345. if (! empty($conf->global->PROJECT_TIMES_PENT_FORMAT)) $timespentoutputformat=$conf->global->PROJECT_TIME_SPENT_FORMAT;
  346. // Planned Workload (in working hours)
  347. print '<td align="right">';
  348. $fullhour=convertSecondToTime($lines[$i]->planned_workload,$plannedworkloadoutputformat);
  349. $workingdelay=convertSecondToTime($lines[$i]->planned_workload,'all',86400,7); // TODO Replace 86400 and 7 to take account working hours per day and working day per weeks
  350. if ($lines[$i]->planned_workload != '')
  351. {
  352. print $fullhour;
  353. // TODO Add delay taking account of working hours per day and working day per week
  354. //if ($workingdelay != $fullhour) print '<br>('.$workingdelay.')';
  355. }
  356. //else print '--:--';
  357. print '</td>';
  358. // Progress declared
  359. print '<td align="right">';
  360. if ($lines[$i]->progress != '')
  361. {
  362. print $lines[$i]->progress.' %';
  363. }
  364. print '</td>';
  365. // Time spent
  366. print '<td align="right">';
  367. if ($showlineingray) print '<i>';
  368. else print '<a href="'.DOL_URL_ROOT.'/projet/tasks/time.php?id='.$lines[$i]->id.($showproject?'':'&withproject=1').'">';
  369. if ($lines[$i]->duration) print convertSecondToTime($lines[$i]->duration,$timespentoutputformat);
  370. else print '--:--';
  371. if ($showlineingray) print '</i>';
  372. else print '</a>';
  373. print '</td>';
  374. // Progress calculated (Note: ->duration is time spent)
  375. print '<td align="right">';
  376. if ($lines[$i]->planned_workload || $lines[$i]->duration)
  377. {
  378. if ($lines[$i]->planned_workload) print round(100 * $lines[$i]->duration / $lines[$i]->planned_workload,2).' %';
  379. else print $langs->trans('WorkloadNotDefined');
  380. }
  381. print '</td>';
  382. // Tick to drag and drop
  383. if ($addordertick)
  384. {
  385. print '<td align="center" class="tdlineupdown hideonsmartphone">&nbsp;</td>';
  386. }
  387. print "</tr>\n";
  388. if (! $showlineingray) $inc++;
  389. $level++;
  390. if ($lines[$i]->id) projectLinesa($inc, $lines[$i]->id, $lines, $level, $var, $showproject, $taskrole, $projectsListId, $addordertick);
  391. $level--;
  392. $total_projectlinesa_spent += $lines[$i]->duration;
  393. $total_projectlinesa_planned += $lines[$i]->planned_workload;
  394. if ($lines[$i]->planned_workload) $total_projectlinesa_spent_if_planned += $lines[$i]->duration;
  395. }
  396. }
  397. else
  398. {
  399. //$level--;
  400. }
  401. }
  402. if (($total_projectlinesa_planned > 0 || $total_projectlinesa_spent > 0) && $level==0)
  403. {
  404. print '<tr class="liste_total nodrag nodrop">';
  405. print '<td class="liste_total">'.$langs->trans("Total").'</td>';
  406. if ($showproject) print '<td></td><td></td>';
  407. print '<td></td>';
  408. print '<td></td>';
  409. print '<td></td>';
  410. print '<td align="right" class="nowrap liste_total">';
  411. print convertSecondToTime($total_projectlinesa_planned, 'allhourmin');
  412. print '</td>';
  413. print '<td></td>';
  414. print '<td align="right" class="nowrap liste_total">';
  415. print convertSecondToTime($total_projectlinesa_spent, 'allhourmin');
  416. print '</td>';
  417. print '<td align="right" class="nowrap liste_total">';
  418. if ($total_projectlinesa_planned) print round(100 * $total_projectlinesa_spent / $total_projectlinesa_planned,2).' %';
  419. print '</td>';
  420. if ($addordertick) print '<td class="hideonsmartphone"></td>';
  421. print '</tr>';
  422. }
  423. return $inc;
  424. }
  425. /**
  426. * Output a task line into a pertime intput mode
  427. *
  428. * @param string $inc Line number (start to 0, then increased by recursive call)
  429. * @param string $parent Id of parent project to show (0 to show all)
  430. * @param Task[] $lines Array of lines
  431. * @param int $level Level (start to 0, then increased/decrease by recursive call)
  432. * @param string $projectsrole Array of roles user has on project
  433. * @param string $tasksrole Array of roles user has on task
  434. * @param string $mine Show only task lines I am assigned to
  435. * @param int $restricteditformytask 0=No restriction, 1=Enable add time only if task is a task i am affected to
  436. * @param int $preselectedday Preselected day
  437. * @return $inc
  438. */
  439. function projectLinesPerDay(&$inc, $parent, $lines, &$level, &$projectsrole, &$tasksrole, $mine, $restricteditformytask=1, $preselectedday='')
  440. {
  441. global $db, $user, $bc, $langs;
  442. global $form, $formother, $projectstatic, $taskstatic;
  443. $lastprojectid=0;
  444. $var=true;
  445. $numlines=count($lines);
  446. for ($i = 0 ; $i < $numlines ; $i++)
  447. {
  448. if ($parent == 0) $level = 0;
  449. if ($lines[$i]->fk_parent == $parent)
  450. {
  451. // Break on a new project
  452. if ($parent == 0 && $lines[$i]->fk_project != $lastprojectid)
  453. {
  454. $var = !$var;
  455. $lastprojectid=$lines[$i]->fk_project;
  456. if ($preselectedday)
  457. {
  458. $projectstatic->id = $lines[$i]->fk_project;
  459. $projectstatic->loadTimeSpent($preselectedday, 0, $fuser->id); // Load time spent into this->weekWorkLoad and this->weekWorkLoadPerTaks for all day of a week
  460. }
  461. }
  462. // If we want all or we have a role on task, we show it
  463. if (empty($mine) || ! empty($tasksrole[$lines[$i]->id]))
  464. {
  465. $projectstatic->id=$lines[$i]->fk_project;
  466. $projectstatic->ref=$lines[$i]->projectref;
  467. $projectstatic->title=$lines[$i]->projectlabel;
  468. $projectstatic->public=$lines[$i]->public;
  469. $taskstatic->id=$lines[$i]->id;
  470. print "<tr ".$bc[$var].">\n";
  471. // Project
  472. print "<td>";
  473. print $projectstatic->getNomUrl(1,'',0,$langs->transnoentitiesnoconv("YourRole").': '.$projectsrole[$lines[$i]->fk_project]);
  474. print "</td>";
  475. // Ref
  476. print '<td>';
  477. $taskstatic->ref=($lines[$i]->ref?$lines[$i]->ref:$lines[$i]->id);
  478. print $taskstatic->getNomUrl(1,'withproject');
  479. print '</td>';
  480. // Label task
  481. print "<td>";
  482. for ($k = 0 ; $k < $level ; $k++) print "&nbsp;&nbsp;&nbsp;";
  483. $taskstatic->id=$lines[$i]->id;
  484. $taskstatic->ref=$lines[$i]->label;
  485. $taskstatic->date_start=$lines[$i]->date_start;
  486. $taskstatic->date_end=$lines[$i]->date_end;
  487. print $taskstatic->getNomUrl(0,'withproject');
  488. //print "<br>";
  489. //for ($k = 0 ; $k < $level ; $k++) print "&nbsp;&nbsp;&nbsp;";
  490. //print get_date_range($lines[$i]->date_start,$lines[$i]->date_end,'',$langs,0);
  491. print "</td>\n";
  492. // Planned Workload
  493. print '<td align="right">';
  494. if ($lines[$i]->planned_workload) print convertSecondToTime($lines[$i]->planned_workload,'allhourmin');
  495. else print '--:--';
  496. print '</td>';
  497. // Progress declared %
  498. print '<td align="right">';
  499. print $formother->select_percent($lines[$i]->progress, $lines[$i]->id . 'progress');
  500. print '</td>';
  501. // Time spent by everybody
  502. print '<td align="right">';
  503. // $lines[$i]->duration is a denormalised field = summ of time spent by everybody for task. What we need is time consummed by user
  504. if ($lines[$i]->duration)
  505. {
  506. print '<a href="'.DOL_URL_ROOT.'/projet/tasks/time.php?id='.$lines[$i]->id.'">';
  507. print convertSecondToTime($lines[$i]->duration,'allhourmin');
  508. print '</a>';
  509. }
  510. else print '--:--';
  511. print "</td>\n";
  512. // Time spent by user
  513. print '<td align="right">';
  514. $tmptimespent=$taskstatic->getSummaryOfTimeSpent();
  515. if ($tmptimespent['total_duration']) print convertSecondToTime($tmptimespent['total_duration'],'allhourmin');
  516. else print '--:--';
  517. print "</td>\n";
  518. $disabledproject=1;$disabledtask=1;
  519. //print "x".$lines[$i]->fk_project;
  520. //var_dump($lines[$i]);
  521. //var_dump($projectsrole[$lines[$i]->fk_project]);
  522. // If at least one role for project
  523. if ($lines[$i]->public || ! empty($projectsrole[$lines[$i]->fk_project]) || $user->rights->projet->all->creer)
  524. {
  525. $disabledproject=0;
  526. $disabledtask=0;
  527. }
  528. // If $restricteditformytask is on and I have no role on task, i disable edit
  529. if ($restricteditformytask && empty($tasksrole[$lines[$i]->id]))
  530. {
  531. $disabledtask=1;
  532. }
  533. // Form to add new time
  534. print '<td class="nowrap" align="center">';
  535. $tableCell=$form->select_date($preselectedday,$lines[$i]->id,1,1,2,"addtime",0,0,1,$disabledtask);
  536. print $tableCell;
  537. print '</td><td align="right">';
  538. $dayWorkLoad = $projectstatic->weekWorkLoadPerTask[$preselectedday][$lines[$i]->id];
  539. $alreadyspent='';
  540. if ($dayWorkLoad > 0) $alreadyspent=convertSecondToTime($dayWorkLoad,'allhourmin');
  541. $tableCell='';
  542. $tableCell.='<span class="timesheetalreadyrecorded"><input type="text" class="center" size="2" disabled id="timespent['.$inc.']['.$idw.']" name="task['.$lines[$i]->id.']['.$idw.']" value="'.$alreadyspent.'"></span>';
  543. $tableCell.=' + ';
  544. //$tableCell.='&nbsp;&nbsp;&nbsp;';
  545. $tableCell.=$form->select_duration($lines[$i]->id.'duration','',$disabledtask,'text',0,1);
  546. //$tableCell.='&nbsp;<input type="submit" class="button"'.($disabledtask?' disabled':'').' value="'.$langs->trans("Add").'">';
  547. print $tableCell;
  548. print '</td>';
  549. print '<td align="right">';
  550. if ((! $lines[$i]->public) && $disabledproject) print $form->textwithpicto('',$langs->trans("YouAreNotContactOfProject"));
  551. else if ($disabledtask) print $form->textwithpicto('',$langs->trans("TaskIsNotAffectedToYou"));
  552. print '</td>';
  553. print "</tr>\n";
  554. }
  555. $inc++;
  556. $level++;
  557. if ($lines[$i]->id) projectLinesPerDay($inc, $lines[$i]->id, $lines, $level, $projectsrole, $tasksrole, $mine, $restricteditformytask, $preselectedday);
  558. $level--;
  559. }
  560. else
  561. {
  562. //$level--;
  563. }
  564. }
  565. return $inc;
  566. }
  567. /**
  568. * Output a task line into a perday intput mode
  569. *
  570. * @param string $inc Line number (start to 0, then increased by recursive call)
  571. * @param int $firstdaytoshow First day to show
  572. * @param User|null $fuser Restrict list to user if defined
  573. * @param string $parent Id of parent project to show (0 to show all)
  574. * @param Task[] $lines Array of lines
  575. * @param int $level Level (start to 0, then increased/decrease by recursive call)
  576. * @param string $projectsrole Array of roles user has on project
  577. * @param string $tasksrole Array of roles user has on task
  578. * @param string $mine Show only task lines I am assigned to
  579. * @param int $restricteditformytask 0=No restriction, 1=Enable add time only if task is a task i am affected to
  580. * @return $inc
  581. */
  582. function projectLinesPerWeek(&$inc, $firstdaytoshow, $fuser, $parent, $lines, &$level, &$projectsrole, &$tasksrole, $mine, $restricteditformytask=1)
  583. {
  584. global $db, $user, $bc, $langs;
  585. global $form, $formother, $projectstatic, $taskstatic;
  586. $lastprojectid=0;
  587. $var=true;
  588. $numlines=count($lines);
  589. for ($i = 0 ; $i < $numlines ; $i++)
  590. {
  591. if ($parent == 0) $level = 0;
  592. if ($lines[$i]->fk_parent == $parent)
  593. {
  594. // Break on a new project
  595. if ($parent == 0 && $lines[$i]->fk_project != $lastprojectid)
  596. {
  597. $var = !$var;
  598. $lastprojectid=$lines[$i]->fk_project;
  599. $projectstatic->id = $lines[$i]->fk_project;
  600. $projectstatic->loadTimeSpent($firstdaytoshow, 0, $fuser->id); // Load time spent into this->weekWorkLoad and this->weekWorkLoadPerTaks for all day of a week
  601. }
  602. // If we want all or we have a role on task, we show it
  603. if (empty($mine) || ! empty($tasksrole[$lines[$i]->id]))
  604. {
  605. print "<tr ".$bc[$var].">\n";
  606. // Project
  607. print '<td class="nowrap">';
  608. $projectstatic->id=$lines[$i]->fk_project;
  609. $projectstatic->ref=$lines[$i]->projectref;
  610. $projectstatic->title=$lines[$i]->projectlabel;
  611. $projectstatic->public=$lines[$i]->public;
  612. print $projectstatic->getNomUrl(1,'',0,$langs->transnoentitiesnoconv("YourRole").': '.$projectsrole[$lines[$i]->fk_project]);
  613. print "</td>";
  614. // Ref
  615. print '<td class="nowrap">';
  616. $taskstatic->id=$lines[$i]->id;
  617. $taskstatic->ref=($lines[$i]->ref?$lines[$i]->ref:$lines[$i]->id);
  618. print $taskstatic->getNomUrl(1, 'withproject', 'time');
  619. print '</td>';
  620. // Label task
  621. print "<td>";
  622. print '<!-- Task id = '.$lines[$i]->id.' -->';
  623. for ($k = 0 ; $k < $level ; $k++) print "&nbsp;&nbsp;&nbsp;";
  624. $taskstatic->id=$lines[$i]->id;
  625. $taskstatic->ref=$lines[$i]->label;
  626. $taskstatic->date_start=$lines[$i]->date_start;
  627. $taskstatic->date_end=$lines[$i]->date_end;
  628. print $taskstatic->getNomUrl(0, 'withproject', 'time');
  629. //print "<br>";
  630. //for ($k = 0 ; $k < $level ; $k++) print "&nbsp;&nbsp;&nbsp;";
  631. //print get_date_range($lines[$i]->date_start,$lines[$i]->date_end,'',$langs,0);
  632. print "</td>\n";
  633. // Planned Workload
  634. print '<td align="right">';
  635. if ($lines[$i]->planned_workload) print convertSecondToTime($lines[$i]->planned_workload,'allhourmin');
  636. else print '--:--';
  637. print '</td>';
  638. // Progress declared %
  639. print '<td align="right">';
  640. print $formother->select_percent($lines[$i]->progress, $lines[$i]->id . 'progress');
  641. print '</td>';
  642. // Time spent by everybody
  643. print '<td align="right">';
  644. // $lines[$i]->duration is a denormalised field = summ of time spent by everybody for task. What we need is time consummed by user
  645. if ($lines[$i]->duration)
  646. {
  647. print '<a href="'.DOL_URL_ROOT.'/projet/tasks/time.php?id='.$lines[$i]->id.'">';
  648. print convertSecondToTime($lines[$i]->duration,'allhourmin');
  649. print '</a>';
  650. }
  651. else print '--:--';
  652. print "</td>\n";
  653. // Time spent by user
  654. print '<td align="right">';
  655. $tmptimespent=$taskstatic->getSummaryOfTimeSpent();
  656. if ($tmptimespent['total_duration']) print convertSecondToTime($tmptimespent['total_duration'],'allhourmin');
  657. else print '--:--';
  658. print "</td>\n";
  659. $disabledproject=1;$disabledtask=1;
  660. //print "x".$lines[$i]->fk_project;
  661. //var_dump($lines[$i]);
  662. //var_dump($projectsrole[$lines[$i]->fk_project]);
  663. // If at least one role for project
  664. if ($lines[$i]->public || ! empty($projectsrole[$lines[$i]->fk_project]) || $user->rights->projet->all->creer)
  665. {
  666. $disabledproject=0;
  667. $disabledtask=0;
  668. }
  669. // If $restricteditformytask is on and I have no role on task, i disable edit
  670. if ($restricteditformytask && empty($tasksrole[$lines[$i]->id]))
  671. {
  672. $disabledtask=1;
  673. }
  674. //var_dump($projectstatic->weekWorkLoadPerTask);
  675. // Fields to show current time
  676. $tableCell=''; $modeinput='hours';
  677. for ($idw = 0; $idw < 7; $idw++)
  678. {
  679. $tmpday=dol_time_plus_duree($firstdaytoshow, $idw, 'd');
  680. $tmparray=dol_getdate($tmpday);
  681. $dayWorkLoad = $projectstatic->weekWorkLoadPerTask[$tmpday][$lines[$i]->id];
  682. $alreadyspent='';
  683. if ($dayWorkLoad > 0) $alreadyspent=convertSecondToTime($dayWorkLoad,'allhourmin');
  684. $tableCell ='<td align="center">';
  685. $tableCell.='<span class="timesheetalreadyrecorded"><input type="text" class="center" size="2" disabled id="timespent['.$inc.']['.$idw.']" name="task['.$lines[$i]->id.']['.$idw.']" value="'.$alreadyspent.'"></span>';
  686. //$placeholder=' placeholder="00:00"';
  687. $placeholder='';
  688. //if (! $disabledtask)
  689. //{
  690. $tableCell.='+';
  691. $tableCell.='<input type="text" alt="'.$langs->trans("AddHereTimeSpentForDay",$tmparray['day'],$tmparray['mon']).'" title="'.$langs->trans("AddHereTimeSpentForDay",$tmparray['day'],$tmparray['mon']).'" '.($disabledtask?'disabled':$placeholder).' class="center" size="2" id="timeadded['.$inc.']['.$idw.']" name="task['.$lines[$i]->id.']['.$idw.']" value="" cols="2" maxlength="5"';
  692. $tableCell.=' onkeypress="return regexEvent(this,event,\'timeChar\')"';
  693. $tableCell.= 'onblur="regexEvent(this,event,\''.$modeinput.'\'); updateTotal('.$idw.',\''.$modeinput.'\')" />';
  694. //}
  695. $tableCell.='</td>';
  696. print $tableCell;
  697. }
  698. print '<td align="right">';
  699. if ((! $lines[$i]->public) && $disabledproject) print $form->textwithpicto('',$langs->trans("YouAreNotContactOfProject"));
  700. else if ($disabledtask) print $form->textwithpicto('',$langs->trans("TaskIsNotAffectedToYou"));
  701. print '</td>';
  702. print "</tr>\n";
  703. }
  704. $inc++;
  705. $level++;
  706. if ($lines[$i]->id) projectLinesPerWeek($inc, $firstdaytoshow, $fuser, $lines[$i]->id, $lines, $level, $projectsrole, $tasksrole, $mine, $restricteditformytask);
  707. $level--;
  708. }
  709. else
  710. {
  711. //$level--;
  712. }
  713. }
  714. return $inc;
  715. }
  716. /**
  717. * Search in task lines with a particular parent if there is a task for a particular user (in taskrole)
  718. *
  719. * @param string $inc Counter that count number of lines legitimate to show (for return)
  720. * @param int $parent Id of parent task to start
  721. * @param array $lines Array of all tasks
  722. * @param string $taskrole Array of task filtered on a particular user
  723. * @return int 1 if there is
  724. */
  725. function searchTaskInChild(&$inc, $parent, &$lines, &$taskrole)
  726. {
  727. //print 'Search in line with parent id = '.$parent.'<br>';
  728. $numlines=count($lines);
  729. for ($i = 0 ; $i < $numlines ; $i++)
  730. {
  731. // Process line $lines[$i]
  732. if ($lines[$i]->fk_parent == $parent && $lines[$i]->id != $lines[$i]->fk_parent)
  733. {
  734. // If task is legitimate to show, no more need to search deeper
  735. if (isset($taskrole[$lines[$i]->id]))
  736. {
  737. //print 'Found a legitimate task id='.$lines[$i]->id.'<br>';
  738. $inc++;
  739. return $inc;
  740. }
  741. searchTaskInChild($inc, $lines[$i]->id, $lines, $taskrole);
  742. //print 'Found inc='.$inc.'<br>';
  743. if ($inc > 0) return $inc;
  744. }
  745. }
  746. return $inc;
  747. }
  748. /**
  749. * Return HTML table with list of projects and number of opened tasks
  750. *
  751. * @param DoliDB $db Database handler
  752. * @param int $socid Id thirdparty
  753. * @param int $projectsListId Id of project i have permission on
  754. * @param int $mytasks Limited to task i am contact to
  755. * @param int $statut -1=No filter on statut, 0 or 1 = Filter on status
  756. * @return void
  757. */
  758. function print_projecttasks_array($db, $socid, $projectsListId, $mytasks=0, $statut=-1)
  759. {
  760. global $langs,$conf,$user,$bc;
  761. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  762. $projectstatic=new Project($db);
  763. $sortfield='';
  764. $sortorder='';
  765. $project_year_filter=0;
  766. $title=$langs->trans("Project");
  767. if ($statut != '' && $statut >= 0) $title=$langs->trans("Project").' ('.$langs->trans($projectstatic->statuts[$statut]).')';
  768. print '<table class="noborder" width="100%">';
  769. print '<tr class="liste_titre">';
  770. print_liste_field_titre($title,"index.php","","","","",$sortfield,$sortorder);
  771. print_liste_field_titre($langs->trans("Tasks"),"","","","",'align="right"',$sortfield,$sortorder);
  772. print_liste_field_titre($langs->trans("Status"),"","","","",'align="right"',$sortfield,$sortorder);
  773. print "</tr>\n";
  774. $sql = "SELECT p.rowid as projectid, p.ref, p.title, p.fk_user_creat, p.public, p.fk_statut, COUNT(t.rowid) as nb";
  775. $sql.= " FROM ".MAIN_DB_PREFIX."projet as p";
  776. if ($mytasks)
  777. {
  778. $sql.= ", ".MAIN_DB_PREFIX."projet_task as t";
  779. $sql.= ", ".MAIN_DB_PREFIX."element_contact as ec";
  780. $sql.= ", ".MAIN_DB_PREFIX."c_type_contact as ctc";
  781. }
  782. else
  783. {
  784. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."projet_task as t ON p.rowid = t.fk_projet";
  785. }
  786. $sql.= " WHERE p.entity = ".$conf->entity;
  787. $sql.= " AND p.rowid IN (".$projectsListId.")";
  788. if ($socid) $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".$socid.")";
  789. if ($mytasks)
  790. {
  791. $sql.= " AND p.rowid = t.fk_projet";
  792. $sql.= " AND ec.element_id = t.rowid";
  793. $sql.= " AND ctc.rowid = ec.fk_c_type_contact";
  794. $sql.= " AND ctc.element = 'project_task'";
  795. $sql.= " AND ec.fk_socpeople = ".$user->id;
  796. }
  797. if ($statut >= 0)
  798. {
  799. $sql.= " AND p.fk_statut = ".$statut;
  800. }
  801. if (!empty($conf->global->PROJECT_LIMIT_YEAR_RANGE)) {
  802. $project_year_filter = GETPOST("project_year_filter");
  803. //Check if empty or invalid year. Wildcard ignores the sql check
  804. if ($project_year_filter != "*") {
  805. if (empty($project_year_filter) || !ctype_digit($project_year_filter)) { //
  806. $project_year_filter = date("Y");
  807. }
  808. $sql.= " AND (p.dateo IS NULL OR p.dateo <= ".$db->idate(dol_get_last_day($project_year_filter,12,false)).")";
  809. $sql.= " AND (p.datee IS NULL OR p.datee >= ".$db->idate(dol_get_first_day($project_year_filter,1,false)).")";
  810. }
  811. }
  812. $sql.= " GROUP BY p.rowid, p.ref, p.title, p.fk_user_creat, p.public, p.fk_statut";
  813. $sql.= " ORDER BY p.title, p.ref";
  814. $var=true;
  815. $resql = $db->query($sql);
  816. if ( $resql )
  817. {
  818. $num = $db->num_rows($resql);
  819. $i = 0;
  820. while ($i < $num)
  821. {
  822. $objp = $db->fetch_object($resql);
  823. $projectstatic->id = $objp->projectid;
  824. $projectstatic->user_author_id = $objp->fk_user_creat;
  825. $projectstatic->public = $objp->public;
  826. // Check is user has read permission on project
  827. $userAccess = $projectstatic->restrictedProjectArea($user);
  828. if ($userAccess >= 0)
  829. {
  830. $var=!$var;
  831. print "<tr ".$bc[$var].">";
  832. print '<td class="nowrap">';
  833. $projectstatic->ref=$objp->ref;
  834. print $projectstatic->getNomUrl(1);
  835. print ' - '.dol_trunc($objp->title,24).'</td>';
  836. print '<td align="right">'.$objp->nb.'</td>';
  837. $projectstatic->statut = $objp->fk_statut;
  838. print '<td align="right">'.$projectstatic->getLibStatut(3).'</td>';
  839. print "</tr>\n";
  840. }
  841. $i++;
  842. }
  843. $db->free($resql);
  844. }
  845. else
  846. {
  847. dol_print_error($db);
  848. }
  849. print "</table>";
  850. if (!empty($conf->global->PROJECT_LIMIT_YEAR_RANGE))
  851. {
  852. //Add the year filter input
  853. print '<table width="100%">';
  854. print '<tr>';
  855. print '<td>'.$langs->trans("Year").'</td>';
  856. print '<form method="get" action="'.$_SERVER["PHP_SELF"].'">';
  857. print '<td style="text-align:right"><input type="text" size="4" class="flat" name="project_year_filter" value="'.$project_year_filter.'"/>';
  858. print '</form>';
  859. print "</tr>\n";
  860. print '</table>';
  861. }
  862. }