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