PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/projet/tasks/time.php

https://github.com/asterix14/dolibarr
PHP | 406 lines | 286 code | 65 blank | 55 comment | 59 complexity | beeddcd756bbe1ca35ea81987e90abfe MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2006-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2010 Regis Houssin <regis@dolibarr.fr>
  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 2 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 projet
  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. // Security check
  32. $socid=0;
  33. if ($user->societe_id > 0) $socid = $user->societe_id;
  34. if (!$user->rights->projet->lire) accessforbidden();
  35. /*
  36. * Actions
  37. */
  38. if ($_POST["action"] == 'addtimespent' && $user->rights->projet->creer)
  39. {
  40. $error=0;
  41. if (empty($_POST["timespent_durationhour"]) && empty($_POST["timespent_durationmin"]))
  42. {
  43. $mesg='<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("Duration")).'</div>';
  44. $error++;
  45. }
  46. if (empty($_POST["userid"]))
  47. {
  48. $mesg='<div class="error">'.$langs->trans('ErrorUserNotAffectedToTask').'</div>';
  49. $error++;
  50. }
  51. if (! $error)
  52. {
  53. $task = new Task($db);
  54. $task->fetch($_POST["id"]);
  55. $task->timespent_note = $_POST["timespent_note"];
  56. $task->timespent_duration = $_POST["timespent_durationhour"]*60*60; // We store duration in seconds
  57. $task->timespent_duration+= $_POST["timespent_durationmin"]*60; // We store duration in seconds
  58. $task->timespent_date = dol_mktime(12,0,0,$_POST["timemonth"],$_POST["timeday"],$_POST["timeyear"]);
  59. $task->timespent_fk_user = $_POST["userid"];
  60. $result=$task->addTimeSpent($user);
  61. if ($result >= 0)
  62. {
  63. }
  64. else
  65. {
  66. $mesg='<div class="error">'.$langs->trans($task->error).'</div>';
  67. }
  68. }
  69. else
  70. {
  71. $_POST["action"]='';
  72. }
  73. }
  74. if ($_POST["action"] == 'updateline' && ! $_POST["cancel"] && $user->rights->projet->creer)
  75. {
  76. $error=0;
  77. if (empty($_POST["new_durationhour"]) && empty($_POST["new_durationmin"]))
  78. {
  79. $mesg='<div class="error">'.$langs->trans('ErrorFieldRequired',$langs->transnoentitiesnoconv("Duration")).'</div>';
  80. $error++;
  81. }
  82. if (! $error)
  83. {
  84. $task = new Task($db);
  85. $task->fetch($_POST["id"]);
  86. $task->timespent_id = $_POST["lineid"];
  87. $task->timespent_note = $_POST["timespent_note_line"];
  88. $task->timespent_old_duration = $_POST["old_duration"];
  89. $task->timespent_duration = $_POST["new_durationhour"]*60*60; // We store duration in seconds
  90. $task->timespent_duration+= $_POST["new_durationmin"]*60; // We store duration in seconds
  91. $task->timespent_date = dol_mktime(12,0,0,$_POST["timelinemonth"],$_POST["timelineday"],$_POST["timelineyear"]);
  92. $task->timespent_fk_user = $_POST["userid_line"];
  93. $result=$task->updateTimeSpent($user);
  94. if ($result >= 0)
  95. {
  96. }
  97. else
  98. {
  99. $mesg='<div class="error">'.$langs->trans($task->error).'</div>';
  100. }
  101. }
  102. else
  103. {
  104. $_POST["action"]='';
  105. }
  106. }
  107. if ($_REQUEST["action"] == 'confirm_delete' && $_REQUEST["confirm"] == "yes" && $user->rights->projet->creer)
  108. {
  109. $task = new Task($db);
  110. $task->fetchTimeSpent($_GET['lineid']);
  111. $result = $task->delTimeSpent($user);
  112. if (!$result)
  113. {
  114. $langs->load("errors");
  115. $mesg='<div class="error">'.$langs->trans($task->error).'</div>';
  116. $_POST["action"]='';
  117. }
  118. }
  119. /*
  120. * View
  121. */
  122. $project=new Project($db);
  123. llxHeader("",$langs->trans("Task"));
  124. $form = new Form($db);
  125. if ($_GET["id"] > 0)
  126. {
  127. /*
  128. * Fiche projet en mode visu
  129. */
  130. $task = new Task($db);
  131. $projectstatic = new Project($db);
  132. $userstatic = new User($db);
  133. if ($task->fetch($_GET["id"]) >= 0 )
  134. {
  135. $result=$projectstatic->fetch($task->fk_project);
  136. if (! empty($projectstatic->socid)) $projectstatic->societe->fetch($projectstatic->socid);
  137. // To get role of users
  138. //$userAccess = $projectstatic->restrictedProjectArea($user); // We allow task affected to user even if a not allowed project
  139. //$arrayofuseridoftask=$task->getListContactId('internal');
  140. $head=task_prepare_head($task);
  141. dol_fiche_head($head, 'time', $langs->trans("Task"),0,'projecttask');
  142. if ($mesg) print $mesg.'<br>';
  143. if ($_GET["action"] == 'deleteline')
  144. {
  145. $ret=$form->form_confirm($_SERVER["PHP_SELF"]."?id=".$_GET["id"].'&lineid='.$_GET["lineid"],$langs->trans("DeleteATimeSpent"),$langs->trans("ConfirmDeleteATimeSpent"),"confirm_delete",'','',1);
  146. if ($ret == 'html') print '<br>';
  147. }
  148. print '<table class="border" width="100%">';
  149. // Ref
  150. print '<tr><td width="30%">';
  151. print $langs->trans("Ref");
  152. print '</td><td colspan="3">';
  153. $projectsListId = $project->getProjectsAuthorizedForUser($user,$mine,1);
  154. $task->next_prev_filter=" fk_projet in (".$projectsListId.")";
  155. print $form->showrefnav($task,'id','',1,'rowid','ref','','');
  156. print '</td></tr>';
  157. // Label
  158. print '<tr><td>'.$langs->trans("Label").'</td><td colspan="3">'.$task->label.'</td></tr>';
  159. // Project
  160. print '<tr><td>'.$langs->trans("Project").'</td><td>';
  161. print $projectstatic->getNomUrl(1);
  162. print '</td></tr>';
  163. // Third party
  164. print '<td>'.$langs->trans("Company").'</td><td>';
  165. if ($projectstatic->societe->id) print $projectstatic->societe->getNomUrl(1);
  166. else print '&nbsp;';
  167. print '</td></tr>';
  168. print '</table>';
  169. print '</div>';
  170. /*
  171. * Add time spent
  172. */
  173. if ($user->rights->projet->creer)
  174. {
  175. print '<br>';
  176. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'?id='.$task->id.'">';
  177. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  178. print '<input type="hidden" name="action" value="addtimespent">';
  179. print '<input type="hidden" name="id" value="'.$task->id.'">';
  180. print '<table class="noborder" width="100%">';
  181. print '<tr class="liste_titre">';
  182. print '<td width="100">'.$langs->trans("Date").'</td>';
  183. print '<td>'.$langs->trans("By").'</td>';
  184. print '<td>'.$langs->trans("Note").'</td>';
  185. print '<td align="right">'.$langs->trans("Duration").'</td>';
  186. print '<td width="80">&nbsp;</td>';
  187. print "</tr>\n";
  188. print '<tr '.$bc[false].'>';
  189. // Date
  190. print '<td nowrap="nowrap">';
  191. $newdate=dol_mktime(12,0,0,$_POST["timemonth"],$_POST["timeday"],$_POST["timeyear"]);
  192. print $form->select_date($newdate,'time','','','',"timespent_date");
  193. print '</td>';
  194. // Contributor
  195. print '<td nowrap="nowrap">';
  196. $contactoftask=$task->getListContactId('internal');
  197. print img_object('','user');
  198. print $form->select_users($_POST["userid"]?$_POST["userid"]:$user->id,'userid',0,'',0,'',$contactoftask);
  199. print '</td>';
  200. // Note
  201. print '<td nowrap="nowrap">';
  202. print '<textarea name="timespent_note" cols="80" rows="'.ROWS_3.'">'.($_POST['timespent_note']?$_POST['timespent_note']:'').'</textarea>';
  203. print '</td>';
  204. // Duration
  205. print '<td nowrap="nowrap" align="right">';
  206. print $form->select_duration('timespent_duration',($_POST['timespent_duration']?$_POST['timespent_duration']:''));
  207. print '</td>';
  208. print '<td align="center">';
  209. print '<input type="submit" class="button" value="'.$langs->trans("Add").'">';
  210. print '</td></tr>';
  211. print '</table></form>';
  212. }
  213. print '<br>';
  214. /*
  215. * List of time spent
  216. */
  217. $sql = "SELECT t.rowid, t.task_date, t.task_duration, t.fk_user, t.note";
  218. $sql.= ", u.name, u.firstname";
  219. $sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as t";
  220. $sql .= " , ".MAIN_DB_PREFIX."user as u";
  221. $sql .= " WHERE t.fk_task =".$task->id;
  222. $sql .= " AND t.fk_user = u.rowid";
  223. $sql .= " ORDER BY t.task_date DESC";
  224. $var=true;
  225. $resql = $db->query($sql);
  226. if ($resql)
  227. {
  228. $num = $db->num_rows($resql);
  229. $i = 0;
  230. $tasks = array();
  231. while ($i < $num)
  232. {
  233. $row = $db->fetch_object($resql);
  234. $tasks[$i] = $row;
  235. $i++;
  236. }
  237. $db->free($resql);
  238. }
  239. else
  240. {
  241. dol_print_error($db);
  242. }
  243. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'?id='.$task->id.'">';
  244. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  245. print '<input type="hidden" name="action" value="updateline">';
  246. print '<input type="hidden" name="id" value="'.$task->id.'">';
  247. print '<table class="noborder" width="100%">';
  248. print '<tr class="liste_titre">';
  249. print '<td width="100">'.$langs->trans("Date").'</td>';
  250. print '<td>'.$langs->trans("By").'</td>';
  251. print '<td align="left">'.$langs->trans("Note").'</td>';
  252. print '<td align="right">'.$langs->trans("Duration").'</td>';
  253. print '<td>&nbsp;</td>';
  254. print "</tr>\n";
  255. $total = 0;
  256. foreach ($tasks as $task_time)
  257. {
  258. $var=!$var;
  259. print "<tr ".$bc[$var].">";
  260. // Date
  261. print '<td>';
  262. if ($_GET['action'] == 'editline' && $_GET['lineid'] == $task_time->rowid)
  263. {
  264. print $form->select_date($db->jdate($task_time->task_date),'timeline','','','',"timespent_date");
  265. }
  266. else
  267. {
  268. print dol_print_date($db->jdate($task_time->task_date),'day');
  269. }
  270. print '</td>';
  271. // User
  272. $user->id = $task_time->fk_user;
  273. print '<td>';
  274. if ($_GET['action'] == 'editline' && $_GET['lineid'] == $task_time->rowid)
  275. {
  276. print $form->select_users($user->id,'userid_line');
  277. }
  278. else
  279. {
  280. $user->nom = $task_time->name;
  281. $user->prenom = $task_time->firstname;
  282. print $user->getNomUrl(1);
  283. }
  284. print '</td>';
  285. // Note
  286. print '<td align="left">';
  287. if ($_GET['action'] == 'editline' && $_GET['lineid'] == $task_time->rowid)
  288. {
  289. print '<textarea name="timespent_note_line" cols="80" rows="'.ROWS_3.'">'.$task_time->note.'</textarea>';
  290. }
  291. else
  292. {
  293. print dol_nl2br($task_time->note);
  294. }
  295. print '</td>';
  296. // Time spent
  297. print '<td align="right">';
  298. if ($_GET['action'] == 'editline' && $_GET['lineid'] == $task_time->rowid)
  299. {
  300. print '<input type="hidden" name="old_duration" value="'.$task_time->task_duration.'">';
  301. print $form->select_duration('new_duration',$task_time->task_duration);
  302. }
  303. else
  304. {
  305. print ConvertSecondToTime($task_time->task_duration,'all');
  306. }
  307. print '</td>';
  308. // Edit and delete icon
  309. print '<td align="center" valign="middle" width="80">';
  310. if ($_GET['action'] == 'editline' && $_GET['lineid'] == $task_time->rowid)
  311. {
  312. print '<input type="hidden" name="lineid" value="'.$_GET['lineid'].'">';
  313. print '<input type="submit" class="button" name="save" value="'.$langs->trans("Save").'">';
  314. print '<br>';
  315. print '<input type="submit" class="button" name="cancel" value="'.$langs->trans('Cancel').'">';
  316. }
  317. else if ($user->rights->projet->creer)
  318. {
  319. print '&nbsp;';
  320. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$task->id.'&amp;action=editline&amp;lineid='.$task_time->rowid.'">';
  321. print img_edit();
  322. print '</a>';
  323. print '&nbsp;';
  324. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$task->id.'&amp;action=deleteline&amp;lineid='.$task_time->rowid.'">';
  325. print img_delete();
  326. print '</a>';
  327. }
  328. print '</td>';
  329. print "</tr>\n";
  330. $total += $task_time->task_duration;
  331. }
  332. print '<tr class="liste_total"><td colspan="3" class="liste_total">'.$langs->trans("Total").'</td>';
  333. print '<td align="right" nowrap="nowrap" class="liste_total">'.ConvertSecondToTime($total).'</td><td>&nbsp;</td>';
  334. print '</tr>';
  335. print "</table>";
  336. print "</form>";
  337. }
  338. }
  339. $db->close();
  340. llxFooter();
  341. ?>