PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/projet/tasks/time.php

https://bitbucket.org/speedealing/speedealing
PHP | 484 lines | 347 code | 79 blank | 58 comment | 73 complexity | be5d61eda2a0ef19f5e3b272717618a4 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2010-2012 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/projet/tasks/time.php
  22. * \ingroup project
  23. * \brief Page to add new time spent on a task
  24. */
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/project.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  30. $langs->load('projects');
  31. $id=GETPOST('id','int');
  32. $ref=GETPOST('ref','alpha');
  33. $action=GETPOST('action','alpha');
  34. $confirm=GETPOST('confirm','alpha');
  35. $withproject=GETPOST('withproject','int');
  36. $project_ref=GETPOST('project_ref','alpha');
  37. // Security check
  38. $socid=0;
  39. if ($user->societe_id > 0) $socid = $user->societe_id;
  40. if (!$user->rights->projet->lire) accessforbidden();
  41. $object = new Task($db);
  42. $projectstatic = new Project($db);
  43. /*
  44. * Actions
  45. */
  46. if ($action == 'addtimespent' && $user->rights->projet->creer)
  47. {
  48. $error=0;
  49. if (empty($_POST["timespent_durationhour"]) && empty($_POST["timespent_durationmin"]))
  50. {
  51. $mesg='<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("Duration")).'</div>';
  52. $error++;
  53. }
  54. if (empty($_POST["userid"]))
  55. {
  56. $mesg='<div class="error">'.$langs->trans('ErrorUserNotAffectedToTask').'</div>';
  57. $error++;
  58. }
  59. if (! $error)
  60. {
  61. $object->fetch($id);
  62. $object->timespent_note = $_POST["timespent_note"];
  63. $object->timespent_duration = $_POST["timespent_durationhour"]*60*60; // We store duration in seconds
  64. $object->timespent_duration+= $_POST["timespent_durationmin"]*60; // We store duration in seconds
  65. $object->timespent_date = dol_mktime(12,0,0,$_POST["timemonth"],$_POST["timeday"],$_POST["timeyear"]);
  66. $object->timespent_fk_user = $_POST["userid"];
  67. $result=$object->addTimeSpent($user);
  68. if ($result >= 0)
  69. {
  70. }
  71. else
  72. {
  73. $mesg='<div class="error">'.$langs->trans($object->error).'</div>';
  74. }
  75. }
  76. else
  77. {
  78. $action='';
  79. }
  80. }
  81. if ($action == 'updateline' && ! $_POST["cancel"] && $user->rights->projet->creer)
  82. {
  83. $error=0;
  84. if (empty($_POST["new_durationhour"]) && empty($_POST["new_durationmin"]))
  85. {
  86. $mesg='<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("Duration")).'</div>';
  87. $error++;
  88. }
  89. if (! $error)
  90. {
  91. $object->fetch($id);
  92. $object->timespent_id = $_POST["lineid"];
  93. $object->timespent_note = $_POST["timespent_note_line"];
  94. $object->timespent_old_duration = $_POST["old_duration"];
  95. $object->timespent_duration = $_POST["new_durationhour"]*60*60; // We store duration in seconds
  96. $object->timespent_duration+= $_POST["new_durationmin"]*60; // We store duration in seconds
  97. $object->timespent_date = dol_mktime(12,0,0,$_POST["timelinemonth"],$_POST["timelineday"],$_POST["timelineyear"]);
  98. $object->timespent_fk_user = $_POST["userid_line"];
  99. $result=$object->updateTimeSpent($user);
  100. if ($result >= 0)
  101. {
  102. }
  103. else
  104. {
  105. $mesg='<div class="error">'.$langs->trans($object->error).'</div>';
  106. }
  107. }
  108. else
  109. {
  110. $action='';
  111. }
  112. }
  113. if ($action == 'confirm_delete' && $confirm == "yes" && $user->rights->projet->creer)
  114. {
  115. $object->fetchTimeSpent($_GET['lineid']);
  116. $result = $object->delTimeSpent($user);
  117. if (!$result)
  118. {
  119. $langs->load("errors");
  120. $mesg='<div class="error">'.$langs->trans($object->error).'</div>';
  121. $action='';
  122. }
  123. }
  124. // Retreive First Task ID of Project if withprojet is on to allow project prev next to work
  125. if (! empty($project_ref) && ! empty($withproject))
  126. {
  127. if ($projectstatic->fetch(0,$project_ref) > 0)
  128. {
  129. $tasksarray=$object->getTasksArray(0, 0, $projectstatic->id, $socid, 0);
  130. if (count($tasksarray) > 0)
  131. {
  132. $id=$tasksarray[0]->id;
  133. }
  134. else
  135. {
  136. header("Location: ".DOL_URL_ROOT.'/projet/tasks.php?id='.$projectstatic->id.($withproject?'&withproject=1':'').(empty($mode)?'':'&mode='.$mode));
  137. exit;
  138. }
  139. }
  140. }
  141. /*
  142. * View
  143. */
  144. llxHeader("",$langs->trans("Task"));
  145. $form = new Form($db);
  146. $userstatic = new User($db);
  147. if ($id > 0 || ! empty($ref))
  148. {
  149. /*
  150. * Fiche projet en mode visu
  151. */
  152. if ($object->fetch($id) >= 0)
  153. {
  154. $result=$projectstatic->fetch($object->fk_project);
  155. if (! empty($projectstatic->socid)) $projectstatic->societe->fetch($projectstatic->socid);
  156. $userWrite = $projectstatic->restrictedProjectArea($user,'write');
  157. if ($withproject)
  158. {
  159. // Tabs for project
  160. $tab='tasks';
  161. $head=project_prepare_head($projectstatic);
  162. dol_fiche_head($head, $tab, $langs->trans("Project"),0,($projectstatic->public?'projectpub':'project'));
  163. $param=($mode=='mine'?'&mode=mine':'');
  164. print '<table class="border" width="100%">';
  165. // Ref
  166. print '<tr><td width="30%">';
  167. print $langs->trans("Ref");
  168. print '</td><td>';
  169. // Define a complementary filter for search of next/prev ref.
  170. if (! $user->rights->projet->all->lire)
  171. {
  172. $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user,$mine,0);
  173. $projectstatic->next_prev_filter=" rowid in (".(count($projectsListId)?join(',',array_keys($projectsListId)):'0').")";
  174. }
  175. print $form->showrefnav($projectstatic,'project_ref','',1,'ref','ref','',$param.'&withproject=1');
  176. print '</td></tr>';
  177. print '<tr><td>'.$langs->trans("Label").'</td><td>'.$projectstatic->title.'</td></tr>';
  178. print '<tr><td>'.$langs->trans("Company").'</td><td>';
  179. if (! empty($projectstatic->societe->id)) print $projectstatic->societe->getNomUrl(1);
  180. else print '&nbsp;';
  181. print '</td>';
  182. print '</tr>';
  183. // Visibility
  184. print '<tr><td>'.$langs->trans("Visibility").'</td><td>';
  185. if ($projectstatic->public) print $langs->trans('SharedProject');
  186. else print $langs->trans('PrivateProject');
  187. print '</td></tr>';
  188. // Statut
  189. print '<tr><td>'.$langs->trans("Status").'</td><td>'.$projectstatic->getLibStatut(4).'</td></tr>';
  190. print '</table>';
  191. dol_fiche_end();
  192. print '<br>';
  193. }
  194. $head=task_prepare_head($object);
  195. dol_fiche_head($head, 'task_time', $langs->trans("Task"),0,'projecttask');
  196. dol_htmloutput_mesg($mesg);
  197. if ($action == 'deleteline')
  198. {
  199. $ret=$form->form_confirm($_SERVER["PHP_SELF"]."?id=".$object->id.'&lineid='.$_GET["lineid"].($withproject?'&withproject=1':''),$langs->trans("DeleteATimeSpent"),$langs->trans("ConfirmDeleteATimeSpent"),"confirm_delete",'','',1);
  200. if ($ret == 'html') print '<br>';
  201. }
  202. print '<table class="border" width="100%">';
  203. $param=($withproject?'&withproject=1':'');
  204. $linkback=$withproject?'<a href="'.DOL_URL_ROOT.'/projet/tasks.php?id='.$projectstatic->id.'">'.$langs->trans("BackToList").'</a>':'';
  205. // Ref
  206. print '<tr><td width="30%">';
  207. print $langs->trans("Ref");
  208. print '</td><td colspan="3">';
  209. if (! GETPOST('withproject') || empty($projectstatic->id))
  210. {
  211. $projectsListId = $projectstatic->getProjectsAuthorizedForUser($user,$mine,1);
  212. $object->next_prev_filter=" fk_projet in (".$projectsListId.")";
  213. }
  214. else $object->next_prev_filter=" fk_projet = ".$projectstatic->id;
  215. print $form->showrefnav($object,'id',$linkback,1,'rowid','ref','',$param);
  216. print '</td></tr>';
  217. // Label
  218. print '<tr><td>'.$langs->trans("Label").'</td><td colspan="3">'.$object->label.'</td></tr>';
  219. // Project
  220. if (empty($withproject))
  221. {
  222. print '<tr><td>'.$langs->trans("Project").'</td><td>';
  223. print $projectstatic->getNomUrl(1);
  224. print '</td></tr>';
  225. // Third party
  226. print '<td>'.$langs->trans("Company").'</td><td>';
  227. if ($projectstatic->societe->id) print $projectstatic->societe->getNomUrl(1);
  228. else print '&nbsp;';
  229. print '</td></tr>';
  230. }
  231. print '</table>';
  232. dol_fiche_end();
  233. /*
  234. * Add time spent
  235. */
  236. if ($user->rights->projet->creer)
  237. {
  238. print '<br>';
  239. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'">';
  240. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  241. print '<input type="hidden" name="action" value="addtimespent">';
  242. print '<input type="hidden" name="id" value="'.$object->id.'">';
  243. print '<input type="hidden" name="withproject" value="'.$withproject.'">';
  244. print '<table class="noborder" width="100%">';
  245. print '<tr class="liste_titre">';
  246. print '<td width="100">'.$langs->trans("Date").'</td>';
  247. print '<td>'.$langs->trans("By").'</td>';
  248. print '<td>'.$langs->trans("Note").'</td>';
  249. print '<td align="right">'.$langs->trans("Duration").'</td>';
  250. print '<td width="80">&nbsp;</td>';
  251. print "</tr>\n";
  252. print '<tr '.$bc[false].'>';
  253. // Date
  254. print '<td nowrap="nowrap">';
  255. $newdate=dol_mktime(12,0,0,$_POST["timemonth"],$_POST["timeday"],$_POST["timeyear"]);
  256. print $form->select_date($newdate,'time','','','',"timespent_date");
  257. print '</td>';
  258. // Contributor
  259. print '<td nowrap="nowrap">';
  260. $contactoftask=$object->getListContactId('internal');
  261. print img_object('','user');
  262. print $form->select_users($_POST["userid"]?$_POST["userid"]:$user->id,'userid',0,'',0,'',$contactoftask);
  263. print '</td>';
  264. // Note
  265. print '<td nowrap="nowrap">';
  266. print '<textarea name="timespent_note" cols="80" rows="'.ROWS_3.'">'.($_POST['timespent_note']?$_POST['timespent_note']:'').'</textarea>';
  267. print '</td>';
  268. // Duration
  269. print '<td nowrap="nowrap" align="right">';
  270. print $form->select_duration('timespent_duration',($_POST['timespent_duration']?$_POST['timespent_duration']:''));
  271. print '</td>';
  272. print '<td align="center">';
  273. print '<input type="submit" class="button" value="'.$langs->trans("Add").'">';
  274. print '</td></tr>';
  275. print '</table></form>';
  276. }
  277. print '<br>';
  278. /*
  279. * List of time spent
  280. */
  281. $sql = "SELECT t.rowid, t.task_date, t.task_duration, t.fk_user, t.note";
  282. $sql.= ", u.name, u.firstname";
  283. $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as t";
  284. $sql .= " , ".MAIN_DB_PREFIX."user as u";
  285. $sql .= " WHERE t.fk_task =".$object->id;
  286. $sql .= " AND t.fk_user = u.rowid";
  287. $sql .= " ORDER BY t.task_date DESC";
  288. $var=true;
  289. $resql = $db->query($sql);
  290. if ($resql)
  291. {
  292. $num = $db->num_rows($resql);
  293. $i = 0;
  294. $tasks = array();
  295. while ($i < $num)
  296. {
  297. $row = $db->fetch_object($resql);
  298. $tasks[$i] = $row;
  299. $i++;
  300. }
  301. $db->free($resql);
  302. }
  303. else
  304. {
  305. dol_print_error($db);
  306. }
  307. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'">';
  308. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  309. print '<input type="hidden" name="action" value="updateline">';
  310. print '<input type="hidden" name="id" value="'.$object->id.'">';
  311. print '<table class="noborder" width="100%">';
  312. print '<tr class="liste_titre">';
  313. print '<td width="100">'.$langs->trans("Date").'</td>';
  314. print '<td>'.$langs->trans("By").'</td>';
  315. print '<td align="left">'.$langs->trans("Note").'</td>';
  316. print '<td align="right">'.$langs->trans("Duration").'</td>';
  317. print '<td>&nbsp;</td>';
  318. print "</tr>\n";
  319. $total = 0;
  320. foreach ($tasks as $task_time)
  321. {
  322. $var=!$var;
  323. print "<tr ".$bc[$var].">";
  324. // Date
  325. print '<td>';
  326. if ($_GET['action'] == 'editline' && $_GET['lineid'] == $task_time->rowid)
  327. {
  328. print $form->select_date($db->jdate($task_time->task_date),'timeline','','','',"timespent_date");
  329. }
  330. else
  331. {
  332. print dol_print_date($db->jdate($task_time->task_date),'day');
  333. }
  334. print '</td>';
  335. // User
  336. print '<td>';
  337. if ($_GET['action'] == 'editline' && $_GET['lineid'] == $task_time->rowid)
  338. {
  339. print $form->select_users($task_time->fk_user,'userid_line');
  340. }
  341. else
  342. {
  343. $userstatic->id = $task_time->fk_user;
  344. $userstatic->nom = $task_time->name;
  345. $userstatic->prenom = $task_time->firstname;
  346. print $userstatic->getNomUrl(1);
  347. }
  348. print '</td>';
  349. // Note
  350. print '<td align="left">';
  351. if ($_GET['action'] == 'editline' && $_GET['lineid'] == $task_time->rowid)
  352. {
  353. print '<textarea name="timespent_note_line" cols="80" rows="'.ROWS_3.'">'.$task_time->note.'</textarea>';
  354. }
  355. else
  356. {
  357. print dol_nl2br($task_time->note);
  358. }
  359. print '</td>';
  360. // Time spent
  361. print '<td align="right">';
  362. if ($_GET['action'] == 'editline' && $_GET['lineid'] == $task_time->rowid)
  363. {
  364. print '<input type="hidden" name="old_duration" value="'.$task_time->task_duration.'">';
  365. print $form->select_duration('new_duration',$task_time->task_duration);
  366. }
  367. else
  368. {
  369. print convertSecondToTime($task_time->task_duration,'all');
  370. }
  371. print '</td>';
  372. // Edit and delete icon
  373. print '<td align="center" valign="middle" width="80">';
  374. if ($action == 'editline' && $_GET['lineid'] == $task_time->rowid)
  375. {
  376. print '<input type="hidden" name="lineid" value="'.$_GET['lineid'].'">';
  377. print '<input type="submit" class="button" name="save" value="'.$langs->trans("Save").'">';
  378. print '<br>';
  379. print '<input type="submit" class="button" name="cancel" value="'.$langs->trans('Cancel').'">';
  380. }
  381. else if ($user->rights->projet->creer)
  382. {
  383. print '&nbsp;';
  384. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=editline&amp;lineid='.$task_time->rowid.($withproject?'&amp;withproject=1':'').'">';
  385. print img_edit();
  386. print '</a>';
  387. print '&nbsp;';
  388. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=deleteline&amp;lineid='.$task_time->rowid.($withproject?'&amp;withproject=1':'').'">';
  389. print img_delete();
  390. print '</a>';
  391. }
  392. print '</td>';
  393. print "</tr>\n";
  394. $total += $task_time->task_duration;
  395. }
  396. print '<tr class="liste_total"><td colspan="3" class="liste_total">'.$langs->trans("Total").'</td>';
  397. print '<td align="right" nowrap="nowrap" class="liste_total">'.convertSecondToTime($total).'</td><td>&nbsp;</td>';
  398. print '</tr>';
  399. print "</table>";
  400. print "</form>";
  401. }
  402. }
  403. llxFooter();
  404. $db->close();
  405. ?>