2005-08-21 14:39:11 +02:00
< ? php
2009-07-28 15:37:28 +02:00
/* Copyright ( C ) 2008 - 2009 Laurent Destailleur < eldy @ users . sourceforge . net >
2010-01-22 21:42:35 +01:00
* Copyright ( C ) 2010 Regis Houssin < regis @ dolibarr . fr >
2005-08-21 14:39:11 +02:00
*
* 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
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
*/
/**
2010-01-22 21:20:41 +01:00
* \file htdocs / projet / tasks / task . class . php
2009-07-28 15:37:28 +02:00
* \ingroup project
2009-11-24 07:51:12 +01:00
* \brief This file is a CRUD class file for Task ( Create / Read / Update / Delete )
2009-07-28 15:37:28 +02:00
* \version $Id $
* \remarks Initialy built by build_class_from_table on 2008 - 09 - 10 12 : 41
*/
2005-08-21 14:39:11 +02:00
2008-09-10 13:14:45 +02:00
2005-08-21 14:39:11 +02:00
/**
2010-02-07 03:39:01 +01:00
* \class Task
* \brief Class to manage tasks
2009-07-28 15:37:28 +02:00
* \remarks Initialy built by build_class_from_table on 2008 - 09 - 10 12 : 41
*/
class Task extends CommonObject
2008-09-10 13:14:45 +02:00
{
var $db ; //!< To store db handler
var $error ; //!< To return error code (or message)
var $errors = array (); //!< To return several error codes (or messages)
2010-02-03 17:57:23 +01:00
var $element = 'project_task' ; //!< Id that identify managed objects
var $table_element = 'projet_task' ; //!< Name of table without prefix where object is stored
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
var $id ;
2009-07-28 15:37:28 +02:00
2010-01-25 23:24:40 +01:00
var $fk_project ;
2008-09-10 13:14:45 +02:00
var $fk_task_parent ;
2010-01-22 21:42:35 +01:00
var $label ;
var $description ;
2008-09-10 13:14:45 +02:00
var $duration_effective ;
2010-01-25 23:24:40 +01:00
var $date_c ;
var $date_start ;
var $date_end ;
2010-01-28 16:55:43 +01:00
var $progress ;
var $priority ;
2008-09-10 13:14:45 +02:00
var $fk_user_creat ;
2010-01-22 21:42:35 +01:00
var $fk_user_valid ;
2008-09-10 13:14:45 +02:00
var $statut ;
2010-01-22 21:42:35 +01:00
var $note_private ;
var $note_public ;
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
/**
* \brief Constructor
* \param DB Database handler
*/
2009-07-28 15:37:28 +02:00
function Task ( $DB )
2008-09-10 13:14:45 +02:00
{
$this -> db = $DB ;
return 1 ;
}
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
/**
* \brief Create in database
* \param user User that create
* \param notrigger 0 = launch triggers after , 1 = disable triggers
* \return int < 0 if KO , Id of created object if OK
*/
function create ( $user , $notrigger = 0 )
{
global $conf , $langs ;
2010-02-07 03:39:01 +01:00
2008-09-10 13:14:45 +02:00
$error = 0 ;
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
// Clean parameters
2010-01-22 21:42:35 +01:00
$this -> label = trim ( $this -> label );
$this -> description = trim ( $this -> description );
2008-09-10 13:14:45 +02:00
// Check parameters
// Put here code to add control on parameters values
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
// Insert request
2010-01-22 21:20:41 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " projet_task ( " ;
$sql .= " fk_projet " ;
$sql .= " , fk_task_parent " ;
$sql .= " , label " ;
2010-01-29 10:23:28 +01:00
$sql .= " , description " ;
2010-01-25 23:24:40 +01:00
$sql .= " , datec " ;
2010-01-22 21:20:41 +01:00
$sql .= " , fk_user_creat " ;
2010-01-29 10:23:28 +01:00
$sql .= " , dateo " ;
$sql .= " , datee " ;
$sql .= " , progress " ;
2008-09-10 13:14:45 +02:00
$sql .= " ) VALUES ( " ;
2010-01-25 23:24:40 +01:00
$sql .= $this -> fk_project ;
2010-01-22 21:20:41 +01:00
$sql .= " , " . $this -> fk_task_parent ;
$sql .= " , ' " . addslashes ( $this -> label ) . " ' " ;
2010-01-29 10:23:28 +01:00
$sql .= " , ' " . addslashes ( $this -> description ) . " ' " ;
2010-01-25 23:24:40 +01:00
$sql .= " , " . $this -> db -> idate ( $this -> date_c );
2010-01-22 21:20:41 +01:00
$sql .= " , " . $user -> id ;
2010-01-29 10:23:28 +01:00
$sql .= " , " . ( $this -> date_start != '' ? $this -> db -> idate ( $this -> date_start ) : 'null' );
$sql .= " , " . ( $this -> date_end != '' ? $this -> db -> idate ( $this -> date_end ) : 'null' );
$sql .= " , " . $this -> progress ;
2008-09-10 13:14:45 +02:00
$sql .= " ) " ;
$this -> db -> begin ();
2009-07-28 15:37:28 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( get_class ( $this ) . " ::create sql= " . $sql , LOG_DEBUG );
2008-09-10 13:14:45 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) { $error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror (); }
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
if ( ! $error )
{
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " projet_task " );
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
if ( ! $notrigger )
{
2009-11-24 07:51:12 +01:00
// Call triggers
include_once ( DOL_DOCUMENT_ROOT . " /interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'TASK_CREATE' , $this , $user , $langs , $conf );
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
// End call triggers
2008-09-10 13:14:45 +02:00
}
}
2005-08-21 14:39:11 +02:00
2008-09-10 13:14:45 +02:00
// Commit or rollback
if ( $error )
{
foreach ( $this -> errors as $errmsg )
{
2009-02-20 23:53:15 +01:00
dol_syslog ( get_class ( $this ) . " ::create " . $errmsg , LOG_ERR );
2008-09-10 13:14:45 +02:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2009-07-28 15:37:28 +02:00
}
2008-09-10 13:14:45 +02:00
$this -> db -> rollback ();
return - 1 * $error ;
}
else
{
$this -> db -> commit ();
return $this -> id ;
}
}
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
/**
* \brief Load object in memory from database
* \param id id object
* \return int < 0 if KO , > 0 if OK
*/
function fetch ( $id )
{
global $langs ;
2010-02-07 03:39:01 +01:00
2008-09-10 13:14:45 +02:00
$sql = " SELECT " ;
$sql .= " t.rowid, " ;
$sql .= " t.fk_projet, " ;
$sql .= " t.fk_task_parent, " ;
2010-01-22 21:42:35 +01:00
$sql .= " t.label, " ;
2010-01-25 23:24:40 +01:00
$sql .= " t.description, " ;
2008-09-10 13:14:45 +02:00
$sql .= " t.duration_effective, " ;
2010-01-25 23:24:40 +01:00
$sql .= " t.dateo, " ;
$sql .= " t.datee, " ;
2008-09-10 13:14:45 +02:00
$sql .= " t.fk_user_creat, " ;
2010-01-22 21:42:35 +01:00
$sql .= " t.fk_user_valid, " ;
$sql .= " t.fk_statut, " ;
2010-01-28 12:08:11 +01:00
$sql .= " t.progress, " ;
$sql .= " t.priority, " ;
2010-01-22 21:42:35 +01:00
$sql .= " t.note_private, " ;
$sql .= " t.note_public " ;
2008-09-10 13:14:45 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " projet_task as t " ;
$sql .= " WHERE t.rowid = " . $id ;
2009-07-28 15:37:28 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( get_class ( $this ) . " ::fetch sql= " . $sql , LOG_DEBUG );
2008-09-10 13:14:45 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( $this -> db -> num_rows ( $resql ))
{
$obj = $this -> db -> fetch_object ( $resql );
2009-07-28 15:37:28 +02:00
2010-01-22 21:42:35 +01:00
$this -> id = $obj -> rowid ;
$this -> ref = $obj -> rowid ;
2010-01-25 23:24:40 +01:00
$this -> fk_project = $obj -> fk_projet ;
2010-01-22 21:42:35 +01:00
$this -> fk_task_parent = $obj -> fk_task_parent ;
$this -> label = $obj -> label ;
$this -> description = $obj -> description ;
$this -> duration_effective = $obj -> duration_effective ;
2010-01-25 23:24:40 +01:00
$this -> date_c = $this -> db -> jdate ( $obj -> datec );
$this -> date_start = $this -> db -> jdate ( $obj -> dateo );
$this -> date_end = $this -> db -> jdate ( $obj -> datee );
2010-01-22 21:42:35 +01:00
$this -> fk_user_creat = $obj -> fk_user_creat ;
$this -> fk_user_valid = $obj -> fk_user_valid ;
$this -> fk_statut = $obj -> fk_statut ;
2010-01-28 12:08:11 +01:00
$this -> progress = $obj -> progress ;
$this -> priority = $obj -> priority ;
2010-01-22 21:42:35 +01:00
$this -> note_private = $obj -> note_private ;
$this -> note_public = $obj -> note_public ;
2008-09-10 13:14:45 +02:00
}
2010-02-07 03:39:01 +01:00
2008-09-10 13:14:45 +02:00
$this -> db -> free ( $resql );
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
return 1 ;
}
else
{
$this -> error = " Error " . $this -> db -> lasterror ();
2009-02-20 23:53:15 +01:00
dol_syslog ( get_class ( $this ) . " ::fetch " . $this -> error , LOG_ERR );
2008-09-10 13:14:45 +02:00
return - 1 ;
}
}
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
/**
* \brief Update database
* \param user User that modify
* \param notrigger 0 = launch triggers after , 1 = disable triggers
* \return int < 0 if KO , > 0 if OK
*/
function update ( $user = 0 , $notrigger = 0 )
{
global $conf , $langs ;
$error = 0 ;
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
// Clean parameters
2010-01-25 23:24:40 +01:00
if ( isset ( $this -> fk_project )) $this -> fk_project = trim ( $this -> fk_project );
2008-09-10 13:14:45 +02:00
if ( isset ( $this -> fk_task_parent )) $this -> fk_task_parent = trim ( $this -> fk_task_parent );
2010-01-25 23:24:40 +01:00
if ( isset ( $this -> label )) $this -> label = trim ( $this -> label );
if ( isset ( $this -> description )) $this -> description = trim ( $this -> description );
2008-09-10 13:14:45 +02:00
if ( isset ( $this -> duration_effective )) $this -> duration_effective = trim ( $this -> duration_effective );
// Check parameters
// Put here code to add control on parameters values
// Update request
$sql = " UPDATE " . MAIN_DB_PREFIX . " projet_task SET " ;
2010-01-25 23:24:40 +01:00
$sql .= " fk_projet= " . ( isset ( $this -> fk_project ) ? $this -> fk_project : " null " ) . " , " ;
2008-09-10 13:14:45 +02:00
$sql .= " fk_task_parent= " . ( isset ( $this -> fk_task_parent ) ? $this -> fk_task_parent : " null " ) . " , " ;
2010-01-25 23:24:40 +01:00
$sql .= " label= " . ( isset ( $this -> label ) ? " ' " . addslashes ( $this -> label ) . " ' " : " null " ) . " , " ;
$sql .= " description= " . ( isset ( $this -> description ) ? " ' " . addslashes ( $this -> description ) . " ' " : " null " ) . " , " ;
2008-09-10 13:14:45 +02:00
$sql .= " duration_effective= " . ( isset ( $this -> duration_effective ) ? $this -> duration_effective : " null " ) . " , " ;
2010-01-25 23:24:40 +01:00
$sql .= " dateo= " . ( $this -> date_start != '' ? $this -> db -> idate ( $this -> date_start ) : 'null' ) . " , " ;
2010-01-28 16:55:43 +01:00
$sql .= " datee= " . ( $this -> date_end != '' ? $this -> db -> idate ( $this -> date_end ) : 'null' ) . " , " ;
$sql .= " progress= " . $this -> progress ;
2008-09-10 13:14:45 +02:00
$sql .= " WHERE rowid= " . $this -> id ;
$this -> db -> begin ();
2009-07-28 15:37:28 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( get_class ( $this ) . " ::update sql= " . $sql , LOG_DEBUG );
2008-09-10 13:14:45 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) { $error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror (); }
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
if ( ! $error )
{
if ( ! $notrigger )
{
2009-11-24 07:51:12 +01:00
// Call triggers
include_once ( DOL_DOCUMENT_ROOT . " /interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'TASK_MODIFY' , $this , $user , $langs , $conf );
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
// End call triggers
2008-09-10 13:14:45 +02:00
}
}
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
// Commit or rollback
if ( $error )
{
foreach ( $this -> errors as $errmsg )
{
2009-02-20 23:53:15 +01:00
dol_syslog ( get_class ( $this ) . " ::update " . $errmsg , LOG_ERR );
2008-09-10 13:14:45 +02:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2009-07-28 15:37:28 +02:00
}
2008-09-10 13:14:45 +02:00
$this -> db -> rollback ();
return - 1 * $error ;
}
else
{
$this -> db -> commit ();
return 1 ;
2009-07-28 15:37:28 +02:00
}
2008-09-10 13:14:45 +02:00
}
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
/**
* \brief Delete object in database
* \param user User that delete
* \param notrigger 0 = launch triggers after , 1 = disable triggers
* \return int < 0 if KO , > 0 if OK
*/
function delete ( $user , $notrigger = 0 )
{
global $conf , $langs ;
$error = 0 ;
2009-07-28 15:37:28 +02:00
2009-01-12 21:26:06 +01:00
$this -> db -> begin ();
2009-07-28 15:37:28 +02:00
2009-01-12 21:26:06 +01:00
if ( $this -> hasChildren () > 0 )
{
2009-02-20 23:53:15 +01:00
dol_syslog ( get_class ( $this ) . " ::delete Can't delete record as it has some child " , LOG_WARNING );
2009-01-12 21:26:06 +01:00
$this -> error = 'ErrorRecordHasChildren' ;
$this -> db -> rollback ();
return 0 ;
}
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " projet_task " ;
$sql .= " WHERE rowid= " . $this -> id ;
2009-07-28 15:37:28 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( get_class ( $this ) . " ::delete sql= " . $sql );
2008-09-10 13:14:45 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) { $error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror (); }
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
if ( ! $error )
{
if ( ! $notrigger )
{
2009-11-24 07:51:12 +01:00
// Call triggers
include_once ( DOL_DOCUMENT_ROOT . " /interfaces.class.php " );
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'TASK_DELETE' , $this , $user , $langs , $conf );
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
// End call triggers
2009-07-28 15:37:28 +02:00
}
2008-09-10 13:14:45 +02:00
}
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
// Commit or rollback
if ( $error )
{
foreach ( $this -> errors as $errmsg )
{
2009-02-20 23:53:15 +01:00
dol_syslog ( get_class ( $this ) . " ::delete " . $errmsg , LOG_ERR );
2008-09-10 13:14:45 +02:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2009-07-28 15:37:28 +02:00
}
2008-09-10 13:14:45 +02:00
$this -> db -> rollback ();
return - 1 * $error ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
}
2009-01-12 21:26:06 +01:00
/**
* \brief Return nb of children
* \return < 0 if KO , 0 if no children , > 0 if OK
*/
function hasChildren ()
{
$ret = 0 ;
2009-07-28 15:37:28 +02:00
2009-01-12 21:26:06 +01:00
$sql = " SELECT COUNT(*) as nb FROM " . MAIN_DB_PREFIX . " projet_task " ;
$sql .= " WHERE fk_task_parent= " . $this -> id ;
2009-07-28 15:37:28 +02:00
2009-02-20 23:53:15 +01:00
dol_syslog ( get_class ( $this ) . " ::hasChildren sql= " . $sql , LOG_DEBUG );
2009-01-12 21:26:06 +01:00
$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 ;
}
2009-07-28 15:37:28 +02:00
2009-01-12 21:26:06 +01:00
if ( ! $error )
{
2009-07-28 15:37:28 +02:00
return $ret ;
2009-01-12 21:26:06 +01:00
}
else
{
return - 1 ;
}
2009-07-28 15:37:28 +02:00
}
/**
* \brief Renvoie nom clicable ( avec eventuellement le picto )
* \param withpicto 0 = Pas de picto , 1 = Inclut le picto dans le lien , 2 = Picto seul
* \param option Sur quoi pointe le lien
* \return string Chaine avec URL
*/
function getNomUrl ( $withpicto = 0 , $option = '' )
{
global $langs ;
$result = '' ;
$lien = '<a href="' . DOL_URL_ROOT . '/projet/tasks/task.php?id=' . $this -> id . '">' ;
$lienfin = '</a>' ;
$picto = 'projecttask' ;
$label = $langs -> trans ( " ShowTask " ) . ': ' . $this -> ref ;
if ( $withpicto ) $result .= ( $lien . img_object ( $label , $picto ) . $lienfin );
if ( $withpicto && $withpicto != 2 ) $result .= ' ' ;
if ( $withpicto != 2 ) $result .= $lien . $this -> ref . $lienfin ;
return $result ;
}
2008-09-10 13:14:45 +02:00
/**
* \brief Initialise object with example values
* \remarks id must be 0 if object instance is a specimen .
*/
function initAsSpecimen ()
{
$this -> id = 0 ;
2009-07-28 15:37:28 +02:00
2008-09-10 13:14:45 +02:00
$this -> fk_projet = '' ;
$this -> fk_task_parent = '' ;
$this -> title = '' ;
$this -> duration_effective = '' ;
$this -> fk_user_creat = '' ;
$this -> statut = '' ;
$this -> note = '' ;
}
2010-02-07 03:39:01 +01:00
2010-01-25 11:59:20 +01:00
/**
* Return list of task for all projects or a particular project
* Sort order is on project , TODO then of position of task , and last on title of first level task
2010-02-07 03:39:01 +01:00
* @ param usert Object user to limit task affected to a particular user
* @ param userp Object user to limit projects of a particular user and public projects
* @ param projectid Project id
* @ param socid Third party id
* @ param mode 0 = Return list of tasks and their projects , 1 = Return projects and tasks if exists
* @ return array Array of tasks
2010-01-25 11:59:20 +01:00
*/
2010-02-07 03:39:01 +01:00
function getTasksArray ( $usert = 0 , $userp = 0 , $projectid = 0 , $socid = 0 , $mode = 0 )
2010-01-25 11:59:20 +01:00
{
global $conf ;
$tasks = array ();
2010-02-07 03:39:01 +01:00
//print $usert.'-'.$userp.'-'.$projectid.'-'.$socid.'-'.$mode.'<br>';
2010-01-25 11:59:20 +01:00
// List of tasks
2010-02-07 03:39:01 +01:00
$sql = " SELECT p.rowid as projectid, p.ref, p.title as plabel, p.public, " ;
$sql .= " t.rowid, t.label, t.fk_task_parent, t.duration_effective " ;
if ( $mode == 0 )
{
$sql .= " FROM ( " . MAIN_DB_PREFIX . " projet as p, " . MAIN_DB_PREFIX . " projet_task as t) " ;
/* if ( is_object ( $userp ) && $userp -> id ) // Limit to projects affected to a user
{
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " element_contact as ec ON ec.element_id = p.rowid " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_type_contact as tc ON ec.fk_c_type_contact = tc.rowid " ;
}
if ( is_object ( $usert ) && $usert -> id ) // Limit to tasks affected to a user
{
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " element_contact as ec ON ec.element_id = t.rowid " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_type_contact as tc ON ec.fk_c_type_contact = tc.rowid " ;
} */
$sql .= " WHERE t.fk_projet = p.rowid " ;
$sql .= " AND p.entity = " . $conf -> entity ;
if ( $socid ) $sql .= " AND p.fk_soc = " . $socid ;
if ( $projectid ) $sql .= " AND p.rowid = " . $projectid ;
/* if ( is_object ( $userp )) $sql .= " AND (p.public=1 OR (ec.fk_socpeople = " . $userp -> id . " AND tc.active = 1 AND tc.source = 'internal' AND tc.element='project')) " ;
if ( is_object ( $usert )) $sql .= " AND (p.public=1 OR (ec.fk_socpeople = " . $usert -> id . " AND tc.active = 1 AND tc.source = 'internal' AND tc.element='project_task')) " ;
*/
}
if ( $mode == 1 )
{
$sql .= " FROM " . MAIN_DB_PREFIX . " projet as p " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " projet_task as t on t.fk_projet = p.rowid " ;
/* if ( is_object ( $userp ) && $userp -> id ) // Limit to projects affected to a user
{
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " element_contact as ec ON ec.element_id = p.rowid " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_type_contact as tc ON ec.fk_c_type_contact = tc.rowid " ;
}
if ( is_object ( $usert ) && $usert -> id ) // Limit to tasks affected to a user
{
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " element_contact as ec ON ec.element_id = t.rowid " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_type_contact as tc ON ec.fk_c_type_contact = tc.rowid " ;
} */
$sql .= " WHERE p.entity = " . $conf -> entity ;
if ( $socid ) $sql .= " AND p.fk_soc = " . $socid ;
if ( $projectid ) $sql .= " AND p.rowid = " . $projectid ;
/* if ( is_object ( $userp ) && $userp -> id ) $sql .= " AND (p.public=1 OR (ec.fk.socpeople = " . $userp -> id . " AND tc.active = 1 AND tc.source = 'internal' AND tc.element='project')) " ;
if ( is_object ( $usert ) && $usert -> id ) $sql .= " AND (p.public=1 OR (ec.fk.socpeople = " . $usert -> id . " AND tc.active = 1 AND tc.source = 'internal' AND tc.element='project_task')) " ;
*/
}
2010-01-25 11:59:20 +01:00
$sql .= " ORDER BY p.ref, t.label " ;
2010-01-26 17:54:40 +01:00
2010-02-07 03:39:01 +01:00
//print $sql;
dol_syslog ( " Task::getTasksArray sql= " . $sql , LOG_DEBUG );
2010-01-25 11:59:20 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2010-02-07 03:39:01 +01:00
// Loop on each record found, so each couple (project id, task id)
2010-01-25 11:59:20 +01:00
while ( $i < $num )
{
2010-02-01 10:21:52 +01:00
$error = 0 ;
2010-02-07 03:39:01 +01:00
2010-01-25 11:59:20 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2010-02-07 03:39:01 +01:00
if (( ! $obj -> public ) && ( $userp || $usert )) // If not public and we ask a filter on user
2010-02-01 10:21:52 +01:00
{
2010-02-07 03:39:01 +01:00
if ( ! $this -> getUserRolesForProjetsOrTasks ( $userp , $usert , $obj -> projectid , $obj -> rowid ))
2010-02-01 10:21:52 +01:00
{
$error ++ ;
}
}
2010-02-07 03:39:01 +01:00
if ( ! $error )
2010-02-01 10:21:52 +01:00
{
$tasks [ $i ] -> id = $obj -> rowid ;
$tasks [ $i ] -> projectid = $obj -> projectid ;
$tasks [ $i ] -> projectref = $obj -> ref ;
$tasks [ $i ] -> projectlabel = $obj -> plabel ;
$tasks [ $i ] -> label = $obj -> label ;
$tasks [ $i ] -> description = $obj -> description ;
$tasks [ $i ] -> fk_parent = $obj -> fk_task_parent ;
$tasks [ $i ] -> duration = $obj -> duration_effective ;
}
2010-02-07 03:39:01 +01:00
2010-01-25 11:59:20 +01:00
$i ++ ;
}
2010-02-01 10:21:52 +01:00
$this -> db -> free ( $resql );
2010-01-25 11:59:20 +01:00
}
else
{
dol_print_error ( $this -> db );
}
return $tasks ;
}
2010-02-07 03:39:01 +01:00
2010-01-25 12:40:48 +01:00
/**
2010-02-07 03:39:01 +01:00
* Return Array of role of user for each projects or each tasks
* @ param userp
* @ param usert
* @ param projectid
* @ param taskid
* @ return array Array of role of user for each projects or each tasks
2010-01-25 12:40:48 +01:00
*/
2010-02-07 03:39:01 +01:00
function getUserRolesForProjetsOrTasks ( $userp , $usert , $projectid = 0 , $taskid = 0 )
2010-01-25 12:40:48 +01:00
{
$tasksrole = array ();
2010-02-07 03:39:01 +01:00
// We want role of user for projet or role of user for task. Both are not possible.
if ( empty ( $userp ) && empty ( $usert ))
{
$this -> error = " CallWithWrongParameters " ;
return - 1 ;
}
2010-01-25 12:40:48 +01:00
/* Liste des taches et role sur la tache du user courant dans $tasksrole */
2010-01-26 17:54:40 +01:00
$sql = " SELECT ec.element_id, ctc.code " ;
2010-02-07 03:39:01 +01:00
if ( $userp ) $sql .= " FROM " . MAIN_DB_PREFIX . " projet as pt " ;
if ( $usert ) $sql .= " FROM " . MAIN_DB_PREFIX . " projet_task as pt " ;
2010-01-26 17:54:40 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " element_contact as ec " ;
$sql .= " , " . MAIN_DB_PREFIX . " c_type_contact as ctc " ;
$sql .= " WHERE pt.rowid = ec.element_id " ;
2010-02-07 03:39:01 +01:00
if ( $userp ) $sql .= " AND ctc.element = 'project' " ;
if ( $usert ) $sql .= " AND ctc.element = 'project_task' " ;
2010-01-26 17:54:40 +01:00
$sql .= " AND ctc.rowid = ec.fk_c_type_contact " ;
2010-02-07 03:39:01 +01:00
if ( $userp ) $sql .= " AND ec.fk_socpeople = " . $userp -> id ;
if ( $usert ) $sql .= " AND ec.fk_socpeople = " . $usert -> id ;
$sql .= " AND ec.statut = 4 " ;
if ( $projectid )
{
if ( $userp ) $sql .= " AND pt.rowid = " . $projectid ;
if ( $usert ) $sql .= " AND pt.fk_projet = " . $projectid ;
}
if ( $taskid )
{
if ( $userp ) $sql .= " ERROR SHOULD NOT HAPPEN " ;
if ( $usert ) $sql .= " AND pt.rowid = " . $projectid ;
}
2010-01-25 12:40:48 +01:00
2010-02-07 03:39:01 +01:00
dol_syslog ( " Task::getTasksForProjectOwnedByAUser sql= " . $sql );
2010-01-25 12:40:48 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num )
{
$row = $this -> db -> fetch_row ( $resql );
$tasksrole [ $row [ 0 ]] = $row [ 1 ];
$i ++ ;
}
2010-02-07 03:39:01 +01:00
$this -> db -> free ( $resql );
2010-01-25 12:40:48 +01:00
}
else
{
dol_print_error ( $this -> db );
}
return $tasksrole ;
}
2010-02-07 03:39:01 +01:00
2010-01-27 08:58:31 +01:00
/**
* \brief Add time spent
* \param user Id utilisateur qui cree
* \param time Time spent
* \param date date
*/
function addTimeSpent ( $user , $time , $date )
{
$result = 0 ;
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " projet_task_time ( " ;
$sql .= " fk_task " ;
$sql .= " , task_date " ;
$sql .= " , task_duration " ;
$sql .= " , fk_user " ;
$sql .= " ) VALUES ( " ;
$sql .= $this -> id ;
$sql .= " , ' " . $this -> db -> idate ( $date ) . " ' " ;
$sql .= " , " . $time ;
$sql .= " , " . $user -> id ;
$sql .= " ) " ;
2010-02-07 03:39:01 +01:00
dol_syslog ( " Task::addTimeSpent sql= " . $sql , LOG_DEBUG );
2010-01-27 08:58:31 +01:00
if ( $this -> db -> query ( $sql ) )
{
$task_id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " projet_task " );
$result = 0 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
2010-02-07 03:39:01 +01:00
dol_syslog ( " Task::addTimeSpent error -2 " . $this -> error , LOG_ERR );
2010-01-27 08:58:31 +01:00
$result = - 2 ;
}
if ( $result == 0 )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " projet_task " ;
$sql .= " SET duration_effective = duration_effective + ' " . price2num ( $time ) . " ' " ;
$sql .= " WHERE rowid = " . $this -> id ;
dol_syslog ( " Project::addTimeSpent sql= " . $sql , LOG_DEBUG );
if ( $this -> db -> query ( $sql ) )
{
$result = 0 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
2010-02-07 03:39:01 +01:00
dol_syslog ( " Task::addTimeSpent error -3 " . $this -> error , LOG_ERR );
2010-01-27 08:58:31 +01:00
$result = - 2 ;
}
}
return $result ;
}
2008-09-10 13:14:45 +02:00
2005-08-21 14:39:11 +02:00
}
2008-09-10 13:14:45 +02:00
?>