Do not allow to remove task with children

This commit is contained in:
Laurent Destailleur 2009-01-12 20:26:06 +00:00
parent 4d7e695d86
commit 5cf2356bcb
8 changed files with 162 additions and 88 deletions

View File

@ -44,3 +44,4 @@ ErrorLDAPMakeManualTest=A .ldif file has been generated in directory %s. Try to
ErrorCantSaveADoneUserWithZeroPercentage=Can't save an action with "statut not started" if field "done by" is also filled. ErrorCantSaveADoneUserWithZeroPercentage=Can't save an action with "statut not started" if field "done by" is also filled.
ErrorBillRefAlreadyExists=Ref used for creation already exists. ErrorBillRefAlreadyExists=Ref used for creation already exists.
ErrorPleaseTypeBankTransactionReportName=Please type bank receipt name where transaction is reported (Format YYYYMM or YYYYMMDD) ErrorPleaseTypeBankTransactionReportName=Please type bank receipt name where transaction is reported (Format YYYYMM or YYYYMMDD)
ErrorRecordHasChildren=Failed to delete records since it has some childs.

View File

@ -44,3 +44,4 @@ ErrorLDAPMakeManualTest=Un fichier .ldif a
ErrorCantSaveADoneUserWithZeroPercentage=Impossible sauver une action à l'état non commencé avec un utilisateur défini comme ayant fait l'action. ErrorCantSaveADoneUserWithZeroPercentage=Impossible sauver une action à l'état non commencé avec un utilisateur défini comme ayant fait l'action.
ErrorBillRefAlreadyExists=La référence utilisée pour la création existe déjà ErrorBillRefAlreadyExists=La référence utilisée pour la création existe déjà
ErrorPleaseTypeBankTransactionReportName=Veuiller saisir le nom de relevé bancaire sur lequel l'écriture est constaté (Format AAAAMM ou AAAMMJJ) ErrorPleaseTypeBankTransactionReportName=Veuiller saisir le nom de relevé bancaire sur lequel l'écriture est constaté (Format AAAAMM ou AAAMMJJ)
ErrorRecordHasChildren=Impossible de supprimer l'enregistrement car il possède des fils.

View File

@ -33,13 +33,19 @@ require_once(DOL_DOCUMENT_ROOT ."/commonobject.class.php");
*/ */
class Project extends CommonObject class Project extends CommonObject
{ {
var $db; //!< To store db handler
var $error; //!< To return error code (or message)
var $errors=array(); //!< To return several error codes (or messages)
var $element='project'; //!< Id that identify managed objects
var $table_element='projet'; //!< Name of table without prefix where object is stored
var $id; var $id;
var $db;
var $ref; var $ref;
var $title; var $title;
var $socid; var $socid;
var $user_resp_id; var $user_resp_id;
/** /**
* \brief Constructeur de la classe * \brief Constructeur de la classe
* \param DB handler acc<EFBFBD>s base de donn<EFBFBD>es * \param DB handler acc<EFBFBD>s base de donn<EFBFBD>es
@ -121,17 +127,20 @@ class Project extends CommonObject
} }
/* /**
* \brief Charge objet projet depuis la base * \brief Get object and lines from database
* \param rowid id du projet a charger * \param rowid id of object to load
*/ * \param ref Ref of project
function fetch($rowid) * \return int >0 if OK, <0 if KO
*/
function fetch($id,$ref='')
{ {
$sql = "SELECT rowid, ref, title, fk_soc, fk_user_creat, fk_user_resp, fk_statut, note";
$sql = "SELECT title, ref, fk_soc, fk_user_creat, fk_user_resp, fk_statut, note";
$sql.= " FROM ".MAIN_DB_PREFIX."projet"; $sql.= " FROM ".MAIN_DB_PREFIX."projet";
$sql.= " WHERE rowid=".$rowid; if ($ref) $sql.= " WHERE ref='".$ref."'";
else $sql.= " WHERE rowid=".$id;
dolibarr_syslog("Project::fetch sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{ {
@ -139,7 +148,7 @@ class Project extends CommonObject
{ {
$obj = $this->db->fetch_object($resql); $obj = $this->db->fetch_object($resql);
$this->id = $rowid; $this->id = $obj->rowid;
$this->ref = $obj->ref; $this->ref = $obj->ref;
$this->title = $obj->title; $this->title = $obj->title;
$this->titre = $obj->title; $this->titre = $obj->title;

View File

@ -1,6 +1,6 @@
<?php <?php
/* Copyright (C) 2001-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org> /* Copyright (C) 2001-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2008 Regis Houssin <regis@dolibarr.fr> * Copyright (C) 2005-2008 Regis Houssin <regis@dolibarr.fr>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -43,9 +43,10 @@ if ($conf->propal->enabled) $langs->load("propal");
// Security check // Security check
$projetid=''; $projetid='';
if ($_GET["id"]) { $projetid=$_GET["id"]; } $ref='';
if (isset($_GET["id"])) { $projetid=$_GET["id"]; }
if ($projetid == '') accessforbidden(); if (isset($_GET["ref"])) { $ref=$_GET["ref"]; }
if ($projetid == '' && $ref == '') accessforbidden();
// Security check // Security check
if ($user->societe_id) $socid=$user->societe_id; if ($user->societe_id) $socid=$user->societe_id;
@ -58,8 +59,10 @@ $result = restrictedArea($user, 'projet', $projetid);
llxHeader("",$langs->trans("Referers")); llxHeader("",$langs->trans("Referers"));
$form = new Form($db);
$projet = new Project($db); $projet = new Project($db);
$projet->fetch($_GET["id"]); $projet->fetch($_GET["id"],$_GET["ref"]);
$projet->societe->fetch($projet->societe->id); $projet->societe->fetch($projet->societe->id);
$head=project_prepare_head($projet); $head=project_prepare_head($projet);
@ -68,7 +71,9 @@ dolibarr_fiche_head($head, 'element', $langs->trans("Project"));
print '<table class="border" width="100%">'; print '<table class="border" width="100%">';
print '<tr><td width="30%">'.$langs->trans("Ref").'</td><td>'.$projet->ref.'</td></tr>'; print '<tr><td width="30%">'.$langs->trans("Ref").'</td><td>';
print $form->showrefnav($projet,'ref','',1,'ref','ref');
print '</td></tr>';
print '<tr><td>'.$langs->trans("Label").'</td><td>'.$projet->title.'</td></tr>'; print '<tr><td>'.$langs->trans("Label").'</td><td>'.$projet->title.'</td></tr>';

View File

@ -32,7 +32,9 @@ require_once(DOL_DOCUMENT_ROOT."/commande/commande.class.php");
require_once(DOL_DOCUMENT_ROOT."/lib/project.lib.php"); require_once(DOL_DOCUMENT_ROOT."/lib/project.lib.php");
$projetid=''; $projetid='';
if ($_GET["id"]) { $projetid=$_GET["id"]; } $ref='';
if (isset($_GET["id"])) { $projetid=$_GET["id"]; }
if (isset($_GET["ref"])) { $ref=$_GET["ref"]; }
// If socid provided by ajax company selector // If socid provided by ajax company selector
if (! empty($_POST['socid_id'])) if (! empty($_POST['socid_id']))
@ -41,7 +43,7 @@ if (! empty($_POST['socid_id']))
$_REQUEST['socid'] = $_REQUEST['socid_id']; $_REQUEST['socid'] = $_REQUEST['socid_id'];
} }
if ($projetid == '' && ($_GET['action'] != "create" && $_POST['action'] != "add" && $_POST["action"] != "update" && !$_POST["cancel"])) accessforbidden(); if ($projetid == '' && $ref == '' && ($_GET['action'] != "create" && $_POST['action'] != "add" && $_POST["action"] != "update" && !$_POST["cancel"])) accessforbidden();
// Security check // Security check
if ($user->societe_id) $socid=$user->societe_id; if ($user->societe_id) $socid=$user->societe_id;
@ -180,7 +182,7 @@ else
if ($mesg) print $mesg; if ($mesg) print $mesg;
$projet = new Project($db); $projet = new Project($db);
$projet->fetch($_GET["id"]); $projet->fetch($_GET["id"],$_GET["ref"]);
$projet->societe->fetch($projet->societe->id); $projet->societe->fetch($projet->societe->id);
$head=project_prepare_head($projet); $head=project_prepare_head($projet);
@ -227,7 +229,9 @@ else
print '<table class="border" width="100%">'; print '<table class="border" width="100%">';
// Ref // Ref
print '<tr><td width="30%">'.$langs->trans("Ref").'</td><td>'.$projet->ref.'</td></tr>'; print '<tr><td width="30%">'.$langs->trans("Ref").'</td><td>';
print $html->showrefnav($projet,'ref','',1,'ref','ref');
print '</td></tr>';
// Label // Label
print '<tr><td>'.$langs->trans("Label").'</td><td>'.$projet->title.'</td></tr>'; print '<tr><td>'.$langs->trans("Label").'</td><td>'.$projet->title.'</td></tr>';

View File

@ -29,7 +29,7 @@ require("./pre.inc.php");
require_once(DOL_DOCUMENT_ROOT."/lib/project.lib.php"); require_once(DOL_DOCUMENT_ROOT."/lib/project.lib.php");
$projetid=''; $projetid='';
$projetid=isset($_GET["id"])?$_GET["id"]:$_POST["projetid"]; $projetid=isset($_REQUEST["id"])?$_REQUEST["id"]:$_POST["projetid"];
// Security check // Security check
if ($user->societe_id) $socid=$user->societe_id; if ($user->societe_id) $socid=$user->societe_id;
@ -39,7 +39,7 @@ $result = restrictedArea($user, 'projet', $projetid);
* Actions * Actions
*/ */
if ($_POST["action"] == 'createtask' && $user->rights->projet->creer && empty($_POST["cancel"])) if ($_POST["action"] == 'createtask' && empty($_POST["cancel"]) && $user->rights->projet->creer)
{ {
$error=0; $error=0;
@ -66,8 +66,16 @@ if ($_POST["action"] == 'createtask' && $user->rights->projet->creer && empty($_
if (! $error) if (! $error)
{ {
Header("Location: ".DOL_URL_ROOT.'/projet/tasks/index.php'); if (empty($projetid))
exit; {
Header("Location: ".DOL_URL_ROOT.'/projet/tasks/index.php');
exit;
}
else
{
Header("Location: ".DOL_URL_ROOT.'/projet/tasks/fiche.php?id='.$projetid);
exit;
}
} }
} }
@ -111,9 +119,12 @@ $form=new Form($db);
llxHeader("",$langs->trans("Tasks"),"Tasks"); llxHeader("",$langs->trans("Tasks"),"Tasks");
$projet = new Project($db); $projet = new Project($db);
if ($_GET["id"])
$id = $_REQUEST['id'];
$ref= $_GET['ref'];
if ($id > 0 || ! empty($ref))
{ {
$projet->fetch($_GET["id"]); $projet->fetch($_REQUEST["id"],$_GET["ref"]);
$projet->societe->fetch($projet->societe->id); $projet->societe->fetch($projet->societe->id);
} }
@ -127,6 +138,7 @@ if ($_GET["action"] == 'create' && $user->rights->projet->creer)
print '<form action="fiche.php" method="post">'; print '<form action="fiche.php" method="post">';
print '<input type="hidden" name="action" value="createtask">'; print '<input type="hidden" name="action" value="createtask">';
if ($_GET['id']) print '<input type="hidden" name="id" value="'.$_GET['id'].'">';
print '<table class="border" width="100%">'; print '<table class="border" width="100%">';
@ -165,10 +177,17 @@ else
$head=project_prepare_head($projet); $head=project_prepare_head($projet);
dolibarr_fiche_head($head, 'tasks', $langs->trans("Project")); dolibarr_fiche_head($head, 'tasks', $langs->trans("Project"));
print '<form name="addtime" method="POST" action="fiche.php?id='.$projet->id.'">';
print '<table class="border" width="100%">'; print '<table class="border" width="100%">';
print '<tr><td width="30%">'.$langs->trans("Ref").'</td><td>'.$projet->ref.'</td></tr>'; // Ref
print '<tr><td width="30%">';
print $langs->trans("Ref");
print '</td><td>';
print $form->showrefnav($projet,'ref','',1,'ref','ref');
print '</td></tr>';
print '<tr><td>'.$langs->trans("Label").'</td><td>'.$projet->title.'</td></tr>'; print '<tr><td>'.$langs->trans("Label").'</td><td>'.$projet->title.'</td></tr>';
print '<td>'.$langs->trans("Company").'</td><td>'; print '<td>'.$langs->trans("Company").'</td><td>';
if (! empty($projet->societe->id)) print $projet->societe->getNomUrl(1); if (! empty($projet->societe->id)) print $projet->societe->getNomUrl(1);
@ -180,25 +199,10 @@ else
$tasksarray=$projet->getTasksArray(); $tasksarray=$projet->getTasksArray();
print '</table>'; print '</table>';
print '<br>';
print '<form name="addtime" method="POST" action="fiche.php?id='.$projet->id.'">';
print '<input type="hidden" name="action" value="addtime">'; print '<input type="hidden" name="action" value="addtime">';
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("Project").'</td>';
print '<td>'.$langs->trans("RefTask").'</td>';
print '<td>'.$langs->trans("LabelTask").'</td>';
print '<td align="right">'.$langs->trans("TimeSpent").'</td>';
print "</tr>\n";
$j=0;
PLines($j, 0, $tasksarray, $level, true);
print '</form>'; print '</form>';
print "</table>";
print '</div>'; print '</div>';
@ -213,6 +217,20 @@ else
} }
print '</div>'; print '</div>';
print '<br>';
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("Project").'</td>';
print '<td>'.$langs->trans("RefTask").'</td>';
print '<td>'.$langs->trans("LabelTask").'</td>';
print '<td align="right">'.$langs->trans("TimeSpent").'</td>';
print "</tr>\n";
$j=0;
PLines($j, 0, $tasksarray, $level, true);
print "</table>";
print '</div>';
} }
$db->close(); $db->close();

View File

@ -1,6 +1,6 @@
<?php <?php
/* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org> /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2006-2008 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2006-2009 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -45,14 +45,15 @@ if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == "yes" && $user-
$projet->societe->fetch($projet->socid); $projet->societe->fetch($projet->socid);
} }
if ($task->delete($user) >= 0) if ($task->delete($user) > 0)
{ {
Header("Location: index.php"); Header("Location: index.php");
exit; exit;
} }
else else
{ {
$mesg=$task->error; $langs->load("errors");
$mesg='<div class="error">'.$langs->trans($task->error).'</div>';
$_POST["action"]=''; $_POST["action"]='';
} }
} }
@ -81,10 +82,7 @@ if ($_GET["id"] > 0)
{ {
$projet = new Project($db); $projet = new Project($db);
$result=$projet->fetch($task->fk_projet); $result=$projet->fetch($task->fk_projet);
if (! empty($projet->socid)) if (! empty($projet->socid)) $projet->societe->fetch($projet->socid);
{
$projet->societe->fetch($projet->socid);
}
$h=0; $h=0;
$head[$h][0] = DOL_URL_ROOT.'/projet/tasks/task.php?id='.$task->id; $head[$h][0] = DOL_URL_ROOT.'/projet/tasks/task.php?id='.$task->id;
@ -94,6 +92,8 @@ if ($_GET["id"] > 0)
dolibarr_fiche_head($head, 'tasks', $langs->trans("Tasks")); dolibarr_fiche_head($head, 'tasks', $langs->trans("Tasks"));
if ($mesg) print $mesg.'<br>';
if ($_GET["action"] == 'delete') if ($_GET["action"] == 'delete')
{ {
$html->form_confirm($_SERVER["PHP_SELF"]."?id=".$_GET["id"],$langs->trans("DeleteATask"),$langs->trans("ConfirmDeleteATask"),"confirm_delete"); $html->form_confirm($_SERVER["PHP_SELF"]."?id=".$_GET["id"],$langs->trans("DeleteATask"),$langs->trans("ConfirmDeleteATask"),"confirm_delete");
@ -111,7 +111,10 @@ if ($_GET["id"] > 0)
print $projet->getNomUrl(1); print $projet->getNomUrl(1);
print '</td></tr>'; print '</td></tr>';
print '<td>'.$langs->trans("Company").'</td><td>'.$projet->societe->getNomUrl(1).'</td></tr>'; print '<td>'.$langs->trans("Company").'</td><td>';
if ($projet->societe->id) print $projet->societe->getNomUrl(1);
else print '&nbsp;';
print '</td></tr>';
/* Liste des tâches */ /* Liste des tâches */
@ -131,44 +134,21 @@ if ($_GET["id"] > 0)
$tasks = array(); $tasks = array();
while ($i < $num) while ($i < $num)
{ {
$row = $db->fetch_row($resql); $row = $db->fetch_object($resql);
$tasks[$i] = $row; $tasks[$i] = $row;
$i++; $i++;
} }
$db->free(); $db->free($resql);
} }
else else
{ {
dolibarr_print_error($db); dolibarr_print_error($db);
} }
print '</table></form>';
/* Nouvelle tâche */
print '</table></form><br />';
print '<input type="hidden" name="action" value="addtime">';
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("Date").'</td>';
print '<td align="right">'.$langs->trans("TimeSpent").'</td>';
print '<td align="right">'.$langs->trans("User").'</td>';
print "</tr>\n";
foreach ($tasks as $task_time)
{
$var=!$var;
print "<tr ".$bc[$var].">";
print '<td>'.dolibarr_print_date($task_time[0],'day').'</td>';
print '<td align="right">'.$task_time[1].'</td>';
$user->id=$task_time[4];
$user->nom=$task_time[3];
print '<td align="right">'.$user->getNomUrl(1).'</td>';
print "</tr>\n";
}
print "</table>";
print '</div>'; print '</div>';
/* /*
* Actions * Actions
*/ */
@ -180,6 +160,30 @@ if ($_GET["id"] > 0)
} }
print '</div>'; print '</div>';
print '<br>';
print '<input type="hidden" name="action" value="addtime">';
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("Date").'</td>';
print '<td align="right">'.$langs->trans("TimeSpent").'</td>';
print '<td align="right">'.$langs->trans("User").'</td>';
print "</tr>\n";
foreach ($tasks as $task_time)
{
$var=!$var;
print "<tr ".$bc[$var].">";
print '<td>'.dolibarr_print_date($db->jdate($task_time->task_date),'day').'</td>';
print '<td align="right">'.$task_time->task_duration.'</td>';
$user->id=$task_time->rowid;
$user->nom=$task_time->login;
print '<td align="right">'.$user->getNomUrl(1).'</td>';
print "</tr>\n";
}
print "</table>";
} }
} }

View File

@ -309,14 +309,19 @@ class Task // extends CommonObject
global $conf, $langs; global $conf, $langs;
$error=0; $error=0;
// TODO. Refused if there is some child. $this->db->begin();
if ($this->hasChildren() > 0)
{
dolibarr_syslog(get_class($this)."::delete Can't delete record as it has some child", LOG_WARNING);
$this->error='ErrorRecordHasChildren';
$this->db->rollback();
return 0;
}
$sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task"; $sql = "DELETE FROM ".MAIN_DB_PREFIX."projet_task";
$sql.= " WHERE rowid=".$this->id; $sql.= " WHERE rowid=".$this->id;
$this->db->begin();
dolibarr_syslog(get_class($this)."::delete sql=".$sql); dolibarr_syslog(get_class($this)."::delete sql=".$sql);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); } if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
@ -355,6 +360,35 @@ class Task // extends CommonObject
} }
} }
/**
* \brief Return nb of children
* \return <0 if KO, 0 if no children, >0 if OK
*/
function hasChildren()
{
$ret=0;
$sql = "SELECT COUNT(*) as nb FROM ".MAIN_DB_PREFIX."projet_task";
$sql.= " WHERE fk_task_parent=".$this->id;
dolibarr_syslog(get_class($this)."::hasChildren sql=".$sql, LOG_DEBUG);
$resql = $this->db->query($sql);
if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
else
{
$obj=$this->db->fetch_object($resql);
if ($obj) $ret=$obj->nb;
}
if (! $error)
{
return $ret;
}
else
{
return -1;
}
}
/** /**
* \brief Initialise object with example values * \brief Initialise object with example values
@ -371,8 +405,6 @@ class Task // extends CommonObject
$this->fk_user_creat=''; $this->fk_user_creat='';
$this->statut=''; $this->statut='';
$this->note=''; $this->note='';
} }
} }