2004-10-20 23:06:45 +02:00
< ? php
2005-04-05 13:52:30 +02:00
/* Copyright ( C ) 2002 - 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2016-01-10 19:48:59 +01:00
* Copyright ( C ) 2005 - 2016 Laurent Destailleur < eldy @ users . sourceforge . net >
2012-12-30 15:13:49 +01:00
* Copyright ( C ) 2005 - 2010 Regis Houssin < regis . houssin @ capnetworks . com >
2013-03-25 19:31:17 +01:00
* Copyright ( C ) 2013 Florian Henry < florian . henry @ open - concept . pro >
2017-03-12 15:31:39 +01:00
* Copyright ( C ) 2014 - 2017 Marcos GarcÃa < marcosgdf @ gmail . com >
2017-03-03 12:57:52 +01:00
* Copyright ( C ) 2017 Ferran Marcet < fmarcet @ 2 byte . es >
2002-05-11 20:53:13 +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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2002-05-11 20:53:13 +02:00
* ( 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
2011-08-01 01:19:04 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2002-05-11 20:53:13 +02:00
*/
2005-08-14 03:38:12 +02:00
/**
2011-11-02 22:41:34 +01:00
* \file htdocs / projet / class / project . class . php
* \ingroup projet
2012-09-15 15:33:00 +02:00
* \brief File of class to manage projects
2009-01-15 00:36:51 +01:00
*/
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2007-05-28 13:30:44 +02:00
2005-08-14 03:38:12 +02:00
/**
2012-09-15 15:33:00 +02:00
* Class to manage projects
2009-01-15 00:36:51 +01:00
*/
2007-05-28 13:30:44 +02:00
class Project extends CommonObject
2006-06-23 21:40:28 +02:00
{
2011-11-02 22:41:34 +01:00
public $element = 'project' ; //!< Id that identify managed objects
public $table_element = 'projet' ; //!< Name of table without prefix where object is stored
2012-09-15 15:33:00 +02:00
public $table_element_line = 'projet_task' ;
public $fk_element = 'fk_projet' ;
2017-10-25 22:02:07 +02:00
public $ismultientitymanaged = 1 ; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
2016-10-17 18:18:06 +02:00
public $picto = 'projectpub' ;
2017-06-07 16:44:04 +02:00
2014-12-25 21:47:39 +01:00
/**
* { @ inheritdoc }
*/
protected $table_ref_field = 'ref' ;
2011-11-02 22:41:34 +01:00
var $description ;
2015-04-23 23:21:06 +02:00
/**
* @ var string
* @ deprecated
* @ see title
*/
public $titre ;
2011-11-02 22:41:34 +01:00
var $title ;
var $date_start ;
var $date_end ;
2015-03-04 16:28:29 +01:00
var $date_close ;
2016-08-29 10:32:00 +02:00
2016-07-20 18:02:33 +02:00
var $socid ; // To store id of thirdparty
var $thirdparty_name ; // To store name of thirdparty (defined only in some cases)
2016-08-29 10:32:00 +02:00
2011-11-02 22:41:34 +01:00
var $user_author_id ; //!< Id of project creator. Not defined if shared project.
2015-03-04 16:28:29 +01:00
var $user_close_id ;
2011-11-02 22:41:34 +01:00
var $public ; //!< Tell if this is a public or private project
2015-02-16 10:40:09 +01:00
var $budget_amount ;
2018-02-26 14:28:51 +01:00
var $bill_time ; // Is the time spent on project must be invoiced or not
2015-02-16 10:40:09 +01:00
2011-11-02 22:41:34 +01:00
var $statuts_short ;
2015-06-29 16:57:32 +02:00
var $statuts_long ;
2015-02-16 10:40:09 +01:00
2015-06-30 01:34:17 +02:00
var $statut ; // 0=draft, 1=opened, 2=closed
2015-06-29 16:57:32 +02:00
var $opp_status ; // opportunity status, into table llx_c_lead_status
2016-01-10 19:48:59 +01:00
var $opp_percent ; // opportunity probability
2015-06-29 16:57:32 +02:00
2012-06-09 18:37:54 +02:00
var $oldcopy ;
2013-07-07 13:14:32 +02:00
2015-03-18 11:04:50 +01:00
var $weekWorkLoad ; // Used to store workload details of a projet
var $weekWorkLoadPerTask ; // Used to store workload details of tasks of a projet
2015-04-23 23:21:06 +02:00
/**
* @ var int Creation date
* @ deprecated
* @ see date_c
*/
public $datec ;
/**
* @ var int Creation date
*/
public $date_c ;
/**
* @ var int Modification date
* @ deprecated
* @ see date_m
*/
public $datem ;
/**
* @ var int Modification date
*/
public $date_m ;
2016-11-19 16:08:27 +01:00
/**
* @ var Task []
*/
public $lines ;
2017-06-07 16:44:04 +02:00
2017-06-08 12:21:41 +02:00
/**
* Draft status
*/
const STATUS_DRAFT = 0 ;
/**
* Open / Validated status
*/
const STATUS_VALIDATED = 1 ;
/**
* Closed status
*/
const STATUS_CLOSED = 2 ;
2014-08-28 12:26:43 +02:00
2011-11-02 22:41:34 +01:00
/**
* Constructor
*
2012-05-06 00:15:03 +02:00
* @ param DoliDB $db Database handler
2011-11-02 22:41:34 +01:00
*/
2012-04-15 18:33:14 +02:00
function __construct ( $db )
2011-11-02 22:41:34 +01:00
{
2012-04-15 18:33:14 +02:00
$this -> db = $db ;
2011-11-02 22:41:34 +01:00
2014-03-11 09:43:26 +01:00
$this -> statuts_short = array ( 0 => 'Draft' , 1 => 'Opened' , 2 => 'Closed' );
2015-06-29 16:57:32 +02:00
$this -> statuts_long = array ( 0 => 'Draft' , 1 => 'Opened' , 2 => 'Closed' );
2011-11-02 22:41:34 +01:00
}
/**
* Create a project into database
*
* @ param User $user User making creation
* @ param int $notrigger Disable triggers
* @ return int < 0 if KO , id of created project if OK
*/
function create ( $user , $notrigger = 0 )
{
2011-12-05 19:41:38 +01:00
global $conf , $langs ;
2011-11-02 22:41:34 +01:00
$error = 0 ;
$ret = 0 ;
2014-06-14 13:41:15 +02:00
$now = dol_now ();
2011-11-02 22:41:34 +01:00
// Check parameters
if ( ! trim ( $this -> ref ))
{
$this -> error = 'ErrorFieldsRequired' ;
2012-08-31 12:55:10 +02:00
dol_syslog ( get_class ( $this ) . " ::create error -1 ref null " , LOG_ERR );
2011-11-02 22:41:34 +01:00
return - 1 ;
}
2017-02-15 18:09:45 +01:00
if ( ! empty ( $conf -> global -> PROJECT_THIRDPARTY_REQUIRED ) && ! $this -> socid > 0 )
{
$this -> error = 'ErrorFieldsRequired' ;
dol_syslog ( get_class ( $this ) . " ::create error -1 ref null " , LOG_ERR );
return - 1 ;
}
2017-06-07 16:44:04 +02:00
2011-11-02 22:41:34 +01:00
$this -> db -> begin ();
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " projet ( " ;
$sql .= " ref " ;
$sql .= " , title " ;
$sql .= " , description " ;
$sql .= " , fk_soc " ;
$sql .= " , fk_user_creat " ;
$sql .= " , fk_statut " ;
2015-06-29 16:57:32 +02:00
$sql .= " , fk_opp_status " ;
2016-01-10 19:48:59 +01:00
$sql .= " , opp_percent " ;
2011-11-02 22:41:34 +01:00
$sql .= " , public " ;
$sql .= " , datec " ;
$sql .= " , dateo " ;
$sql .= " , datee " ;
2015-06-29 16:57:32 +02:00
$sql .= " , opp_amount " ;
2015-02-16 10:40:09 +01:00
$sql .= " , budget_amount " ;
2018-02-26 14:28:51 +01:00
$sql .= " , bill_time " ;
2012-02-14 14:19:00 +01:00
$sql .= " , entity " ;
2011-11-02 22:41:34 +01:00
$sql .= " ) VALUES ( " ;
$sql .= " ' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> title ) . " ' " ;
$sql .= " , ' " . $this -> db -> escape ( $this -> description ) . " ' " ;
$sql .= " , " . ( $this -> socid > 0 ? $this -> socid : " null " );
$sql .= " , " . $user -> id ;
2015-06-30 01:34:17 +02:00
$sql .= " , " . ( is_numeric ( $this -> statut ) ? $this -> statut : '0' );
2015-06-29 16:57:32 +02:00
$sql .= " , " . ( is_numeric ( $this -> opp_status ) ? $this -> opp_status : 'NULL' );
2016-01-10 19:48:59 +01:00
$sql .= " , " . ( is_numeric ( $this -> opp_percent ) ? $this -> opp_percent : 'NULL' );
2011-11-02 22:41:34 +01:00
$sql .= " , " . ( $this -> public ? 1 : 0 );
2014-06-14 13:41:15 +02:00
$sql .= " , ' " . $this -> db -> idate ( $now ) . " ' " ;
$sql .= " , " . ( $this -> date_start != '' ? " ' " . $this -> db -> idate ( $this -> date_start ) . " ' " : 'null' );
$sql .= " , " . ( $this -> date_end != '' ? " ' " . $this -> db -> idate ( $this -> date_end ) . " ' " : 'null' );
2015-06-29 16:57:32 +02:00
$sql .= " , " . ( strcmp ( $this -> opp_amount , '' ) ? price2num ( $this -> opp_amount ) : 'null' );
$sql .= " , " . ( strcmp ( $this -> budget_amount , '' ) ? price2num ( $this -> budget_amount ) : 'null' );
2018-02-27 12:44:03 +01:00
$sql .= " , " . ( $this -> bill_time ? 1 : 0 );
2012-02-14 14:19:00 +01:00
$sql .= " , " . $conf -> entity ;
2011-11-02 22:41:34 +01:00
$sql .= " ) " ;
2014-06-13 01:46:27 +02:00
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2011-11-02 22:41:34 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " projet " );
$ret = $this -> id ;
if ( ! $notrigger )
{
2014-07-03 17:15:42 +02:00
// Call trigger
$result = $this -> call_trigger ( 'PROJECT_CREATE' , $user );
2014-07-04 13:43:50 +02:00
if ( $result < 0 ) { $error ++ ; }
2011-11-02 22:41:34 +01:00
// End call triggers
}
}
else
{
$this -> error = $this -> db -> lasterror ();
$this -> errno = $this -> db -> lasterrno ();
$error ++ ;
}
2013-07-07 13:14:32 +02:00
2015-02-21 12:57:17 +01:00
// Update extrafield
2018-04-10 12:03:01 +02:00
if ( ! $error ) {
2013-04-12 11:09:53 +02:00
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) // For avoid conflicts if trigger used
{
$result = $this -> insertExtraFields ();
if ( $result < 0 )
{
$error ++ ;
}
}
}
2011-11-02 22:41:34 +01:00
2018-04-10 12:03:01 +02:00
if ( ! $error && ! empty ( $conf -> global -> MAIN_DISABLEDRAFTSTATUS ))
2011-11-02 22:41:34 +01:00
{
$res = $this -> setValid ( $user );
2011-02-09 15:45:42 +01:00
if ( $res < 0 ) $error ++ ;
2011-11-02 22:41:34 +01:00
}
2006-09-23 02:27:47 +02:00
2018-04-10 12:03:01 +02:00
if ( ! $error )
2011-11-02 22:41:34 +01:00
{
2011-02-09 15:45:42 +01:00
$this -> db -> commit ();
return $ret ;
2011-11-02 22:41:34 +01:00
}
else
{
2011-02-09 15:45:42 +01:00
$this -> db -> rollback ();
return - 1 ;
2011-11-02 22:41:34 +01:00
}
}
/**
* Update a project
*
* @ param User $user User object of making update
* @ param int $notrigger 1 = Disable all triggers
2016-11-19 16:08:27 +01:00
* @ return int <= 0 if KO , > 0 if OK
2011-11-02 22:41:34 +01:00
*/
function update ( $user , $notrigger = 0 )
{
global $langs , $conf ;
2012-02-04 18:34:52 +01:00
2011-12-05 19:41:38 +01:00
$error = 0 ;
2016-08-29 10:32:00 +02:00
2011-11-02 22:41:34 +01:00
// Clean parameters
$this -> title = trim ( $this -> title );
$this -> description = trim ( $this -> description );
2015-06-30 01:34:17 +02:00
if ( $this -> opp_amount < 0 ) $this -> opp_amount = '' ;
2016-01-28 20:04:02 +01:00
if ( $this -> opp_percent < 0 ) $this -> opp_percent = '' ;
2016-11-09 17:46:30 +01:00
if ( $this -> date_end && $this -> date_end < $this -> date_start )
{
$this -> error = $langs -> trans ( " ErrorDateEndLowerThanDateStart " );
$this -> errors [] = $this -> error ;
$this -> db -> rollback ();
dol_syslog ( get_class ( $this ) . " ::update error -3 " . $this -> error , LOG_ERR );
return - 3 ;
}
2017-06-07 16:44:04 +02:00
2011-11-02 22:41:34 +01:00
if ( dol_strlen ( trim ( $this -> ref )) > 0 )
{
2014-07-03 17:15:42 +02:00
$this -> db -> begin ();
2014-07-04 13:43:50 +02:00
2011-11-02 22:41:34 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " projet SET " ;
2014-02-21 22:39:45 +01:00
$sql .= " ref=' " . $this -> db -> escape ( $this -> ref ) . " ' " ;
2011-11-02 22:41:34 +01:00
$sql .= " , title = ' " . $this -> db -> escape ( $this -> title ) . " ' " ;
$sql .= " , description = ' " . $this -> db -> escape ( $this -> description ) . " ' " ;
$sql .= " , fk_soc = " . ( $this -> socid > 0 ? $this -> socid : " null " );
$sql .= " , fk_statut = " . $this -> statut ;
2016-07-08 10:59:13 +02:00
$sql .= " , fk_opp_status = " . (( is_numeric ( $this -> opp_status ) && $this -> opp_status > 0 ) ? $this -> opp_status : 'null' );
2016-01-10 19:48:59 +01:00
$sql .= " , opp_percent = " . (( is_numeric ( $this -> opp_percent ) && $this -> opp_percent != '' ) ? $this -> opp_percent : 'null' );
2011-11-02 22:41:34 +01:00
$sql .= " , public = " . ( $this -> public ? 1 : 0 );
2014-08-08 01:02:59 +02:00
$sql .= " , datec= " . ( $this -> date_c != '' ? " ' " . $this -> db -> idate ( $this -> date_c ) . " ' " : 'null' );
$sql .= " , dateo= " . ( $this -> date_start != '' ? " ' " . $this -> db -> idate ( $this -> date_start ) . " ' " : 'null' );
$sql .= " , datee= " . ( $this -> date_end != '' ? " ' " . $this -> db -> idate ( $this -> date_end ) . " ' " : 'null' );
2015-03-04 16:28:29 +01:00
$sql .= " , date_close= " . ( $this -> date_close != '' ? " ' " . $this -> db -> idate ( $this -> date_close ) . " ' " : 'null' );
$sql .= " , fk_user_close= " . ( $this -> fk_user_close > 0 ? $this -> fk_user_close : " null " );
2015-06-29 16:57:32 +02:00
$sql .= " , opp_amount = " . ( strcmp ( $this -> opp_amount , '' ) ? price2num ( $this -> opp_amount ) : " null " );
$sql .= " , budget_amount = " . ( strcmp ( $this -> budget_amount , '' ) ? price2num ( $this -> budget_amount ) : " null " );
2017-10-04 12:58:13 +02:00
$sql .= " , fk_user_modif = " . $user -> id ;
2018-02-26 14:28:51 +01:00
$sql .= " , bill_time = " . ( $this -> bill_time ? 1 : 0 );
2011-11-02 22:41:34 +01:00
$sql .= " WHERE rowid = " . $this -> id ;
2015-06-30 01:34:17 +02:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
2014-07-04 13:43:50 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2011-11-02 22:41:34 +01:00
{
2018-02-01 19:03:21 +01:00
// Update extrafield
if ( ! $error )
2011-11-02 22:41:34 +01:00
{
2013-04-12 11:09:53 +02:00
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) // For avoid conflicts if trigger used
{
$result = $this -> insertExtraFields ();
if ( $result < 0 )
{
$error ++ ;
}
}
}
2012-09-15 15:33:00 +02:00
2018-04-10 12:03:01 +02:00
if ( ! $error && ! $notrigger )
2018-02-01 19:03:21 +01:00
{
// Call trigger
$result = $this -> call_trigger ( 'PROJECT_MODIFY' , $user );
if ( $result < 0 ) { $error ++ ; }
// End call triggers
}
2017-03-03 12:57:52 +01:00
if ( ! $error && ( is_object ( $this -> oldcopy ) && $this -> oldcopy -> ref !== $this -> ref ))
2012-07-02 19:30:37 +02:00
{
// We remove directory
if ( $conf -> projet -> dir_output )
{
$olddir = $conf -> projet -> dir_output . " / " . dol_sanitizeFileName ( $this -> oldcopy -> ref );
$newdir = $conf -> projet -> dir_output . " / " . dol_sanitizeFileName ( $this -> ref );
if ( file_exists ( $olddir ))
{
2014-06-21 15:45:53 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
$res = dol_move ( $olddir , $newdir );
if ( ! $res )
2012-07-02 19:30:37 +02:00
{
2016-05-17 21:55:39 +02:00
$langs -> load ( " errors " );
$this -> error = $langs -> trans ( 'ErrorFailToRenameDir' , $olddir , $newdir );
2012-07-02 19:30:37 +02:00
$error ++ ;
}
}
}
2012-06-09 18:37:54 +02:00
}
2014-07-03 17:15:42 +02:00
if ( ! $error )
{
$this -> db -> commit ();
$result = 1 ;
}
else
2014-08-28 12:26:43 +02:00
{
2014-07-03 17:15:42 +02:00
$this -> db -> rollback ();
$result = - 1 ;
}
2011-11-02 22:41:34 +01:00
}
else
2014-07-04 13:43:50 +02:00
{
2017-03-02 20:06:10 +01:00
$this -> error = $this -> db -> lasterror ();
$this -> errors [] = $this -> error ;
$this -> db -> rollback ();
if ( $this -> db -> lasterrno () == 'DB_ERROR_RECORD_ALREADY_EXISTS' )
{
$result = - 4 ;
}
else
{
$result = - 2 ;
}
dol_syslog ( get_class ( $this ) . " ::update error " . $result . " " . $this -> error , LOG_ERR );
}
2011-11-02 22:41:34 +01:00
}
else
{
2015-06-30 01:34:17 +02:00
dol_syslog ( get_class ( $this ) . " ::update ref null " );
2011-11-02 22:41:34 +01:00
$result = - 1 ;
}
return $result ;
}
/**
2016-11-19 16:08:27 +01:00
* Get object from database
2011-11-02 22:41:34 +01:00
*
* @ param int $id Id of object to load
* @ param string $ref Ref of project
* @ return int > 0 if OK , 0 if not found , < 0 if KO
*/
function fetch ( $id , $ref = '' )
{
2017-12-15 20:07:35 +01:00
global $conf ;
2011-11-02 22:41:34 +01:00
if ( empty ( $id ) && empty ( $ref )) return - 1 ;
2015-06-29 16:57:32 +02:00
$sql = " SELECT rowid, ref, title, description, public, datec, opp_amount, budget_amount, " ;
2018-02-26 14:28:51 +01:00
$sql .= " tms, dateo, datee, date_close, fk_soc, fk_user_creat, fk_user_modif, fk_user_close, fk_statut, fk_opp_status, opp_percent, " ;
$sql .= " note_private, note_public, model_pdf, bill_time " ;
2011-11-02 22:41:34 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " projet " ;
2012-06-09 18:37:54 +02:00
if ( ! empty ( $id ))
{
$sql .= " WHERE rowid= " . $id ;
}
else if ( ! empty ( $ref ))
2012-05-23 13:09:36 +02:00
{
2014-09-13 19:04:25 +02:00
$sql .= " WHERE ref=' " . $this -> db -> escape ( $ref ) . " ' " ;
2017-05-30 18:50:54 +02:00
$sql .= " AND entity IN ( " . getEntity ( 'project' ) . " ) " ;
2012-05-23 13:09:36 +02:00
}
2011-11-02 22:41:34 +01:00
2014-06-13 01:46:27 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2011-11-02 22:41:34 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2017-06-07 16:44:04 +02:00
$num_rows = $this -> db -> num_rows ( $resql );
if ( $num_rows )
2011-11-02 22:41:34 +01:00
{
$obj = $this -> db -> fetch_object ( $resql );
$this -> id = $obj -> rowid ;
$this -> ref = $obj -> ref ;
$this -> title = $obj -> title ;
$this -> titre = $obj -> title ; // TODO deprecated
$this -> description = $obj -> description ;
$this -> date_c = $this -> db -> jdate ( $obj -> datec );
$this -> datec = $this -> db -> jdate ( $obj -> datec ); // TODO deprecated
$this -> date_m = $this -> db -> jdate ( $obj -> tms );
$this -> datem = $this -> db -> jdate ( $obj -> tms ); // TODO deprecated
$this -> date_start = $this -> db -> jdate ( $obj -> dateo );
$this -> date_end = $this -> db -> jdate ( $obj -> datee );
2015-03-04 16:28:29 +01:00
$this -> date_close = $this -> db -> jdate ( $obj -> date_close );
2011-11-02 22:41:34 +01:00
$this -> note_private = $obj -> note_private ;
$this -> note_public = $obj -> note_public ;
$this -> socid = $obj -> fk_soc ;
$this -> user_author_id = $obj -> fk_user_creat ;
2017-10-04 12:58:13 +02:00
$this -> user_modification_id = $obj -> fk_user_modif ;
2015-03-04 16:28:29 +01:00
$this -> user_close_id = $obj -> fk_user_close ;
2011-11-02 22:41:34 +01:00
$this -> public = $obj -> public ;
$this -> statut = $obj -> fk_statut ;
2015-06-29 16:57:32 +02:00
$this -> opp_status = $obj -> fk_opp_status ;
$this -> opp_amount = $obj -> opp_amount ;
2016-01-10 19:48:59 +01:00
$this -> opp_percent = $obj -> opp_percent ;
2015-02-16 10:40:09 +01:00
$this -> budget_amount = $obj -> budget_amount ;
2013-03-25 19:28:34 +01:00
$this -> modelpdf = $obj -> model_pdf ;
2018-02-26 14:28:51 +01:00
$this -> bill_time = ( int ) $obj -> bill_time ;
2011-11-02 22:41:34 +01:00
$this -> db -> free ( $resql );
2018-02-20 19:38:18 +01:00
// Retreive all extrafield
// fetch optionals attributes and labels
2017-06-07 16:44:04 +02:00
$this -> fetch_optionals ();
2017-12-15 20:07:35 +01:00
2011-11-02 22:41:34 +01:00
return 1 ;
}
2017-06-07 16:44:04 +02:00
$this -> db -> free ( $resql );
2018-05-22 16:03:03 +02:00
return 0 ;
2011-11-02 22:41:34 +01:00
}
else
{
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
/**
* Return list of projects
*
* @ param int $socid To filter on a particular third party
* @ return array List of projects
*/
function liste_array ( $socid = '' )
{
global $conf ;
$projects = array ();
$sql = " SELECT rowid, title " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " projet " ;
$sql .= " WHERE entity = " . $conf -> entity ;
if ( ! empty ( $socid )) $sql .= " AND fk_soc = " . $socid ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$nump = $this -> db -> num_rows ( $resql );
if ( $nump )
{
$i = 0 ;
while ( $i < $nump )
{
$obj = $this -> db -> fetch_object ( $resql );
$projects [ $obj -> rowid ] = $obj -> title ;
$i ++ ;
}
}
return $projects ;
}
else
{
print $this -> db -> lasterror ();
}
}
/**
2018-02-01 19:15:50 +01:00
* Return list of elements for type , linked to a project
2011-11-02 22:41:34 +01:00
*
2015-06-03 21:01:50 +02:00
* @ param string $type 'propal' , 'order' , 'invoice' , 'order_supplier' , 'invoice_supplier' , ...
2014-10-29 20:21:24 +01:00
* @ param string $tablename name of table associated of the type
2015-06-03 21:01:50 +02:00
* @ param string $datefieldname name of date field for filter
2018-02-01 19:15:50 +01:00
* @ param int $dates Start date
* @ param int $datee End date
2015-02-02 21:12:43 +01:00
* @ return mixed Array list of object ids linked to project , < 0 or string if error
2011-11-02 22:41:34 +01:00
*/
2014-10-29 20:21:24 +01:00
function get_element_list ( $type , $tablename , $datefieldname = '' , $dates = '' , $datee = '' )
2011-11-02 22:41:34 +01:00
{
$elements = array ();
2016-08-29 10:32:00 +02:00
2015-11-07 12:36:34 +01:00
if ( $this -> id <= 0 ) return $elements ;
2016-08-29 10:32:00 +02:00
2018-02-01 19:15:50 +01:00
$ids = $this -> id ;
2015-06-03 21:01:50 +02:00
if ( $type == 'agenda' )
2014-10-29 20:21:24 +01:00
{
2018-02-01 19:15:50 +01:00
$sql = " SELECT id as rowid FROM " . MAIN_DB_PREFIX . " actioncomm WHERE fk_project IN ( " . $ids . " ) " ;
2014-10-29 20:21:24 +01:00
}
2015-06-03 21:01:50 +02:00
elseif ( $type == 'expensereport' )
2014-10-29 20:21:24 +01:00
{
2018-02-01 19:15:50 +01:00
$sql = " SELECT ed.rowid FROM " . MAIN_DB_PREFIX . " expensereport as e, " . MAIN_DB_PREFIX . " expensereport_det as ed WHERE e.rowid = ed.fk_expensereport AND ed.fk_projet IN ( " . $ids . " ) " ;
2014-10-29 20:21:24 +01:00
}
2015-06-03 21:01:50 +02:00
elseif ( $type == 'project_task' )
2015-02-16 10:40:09 +01:00
{
2018-02-01 19:15:50 +01:00
$sql = " SELECT DISTINCT pt.rowid FROM " . MAIN_DB_PREFIX . " projet_task as pt, " . MAIN_DB_PREFIX . " projet_task_time as ptt WHERE pt.rowid = ptt.fk_task AND pt.fk_projet IN ( " . $ids . " ) " ;
2015-06-03 21:01:50 +02:00
}
elseif ( $type == 'project_task_time' ) // Case we want to duplicate line foreach user
{
2018-02-01 19:15:50 +01:00
$sql = " SELECT DISTINCT pt.rowid, ptt.fk_user FROM " . MAIN_DB_PREFIX . " projet_task as pt, " . MAIN_DB_PREFIX . " projet_task_time as ptt WHERE pt.rowid = ptt.fk_task AND pt.fk_projet IN ( " . $ids . " ) " ;
2015-02-16 10:40:09 +01:00
}
2017-01-29 13:36:33 +01:00
elseif ( $type == 'stock_mouvement' )
{
2018-02-01 19:15:50 +01:00
$sql = 'SELECT ms.rowid, ms.fk_user_author as fk_user FROM ' . MAIN_DB_PREFIX . " stock_mouvement as ms WHERE ms.origintype = 'project' AND ms.fk_origin IN ( " . $ids . " ) AND ms.type_mouvement = 1 " ;
2017-01-29 13:36:33 +01:00
}
2015-06-03 21:01:50 +02:00
else
{
2018-02-01 19:15:50 +01:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . $tablename . " WHERE fk_projet IN ( " . $ids . " ) " ;
2015-06-03 21:01:50 +02:00
}
2014-10-29 20:21:24 +01:00
if ( $dates > 0 )
{
if ( empty ( $datefieldname ) && ! empty ( $this -> table_element_date )) $datefieldname = $this -> table_element_date ;
if ( empty ( $datefieldname )) return 'Error this object has no date field defined' ;
2015-01-22 20:09:57 +01:00
$sql .= " AND ( " . $datefieldname . " >= ' " . $this -> db -> idate ( $dates ) . " ' OR " . $datefieldname . " IS NULL) " ;
2014-10-29 20:21:24 +01:00
}
if ( $datee > 0 )
{
if ( empty ( $datefieldname ) && ! empty ( $this -> table_element_date )) $datefieldname = $this -> table_element_date ;
if ( empty ( $datefieldname )) return 'Error this object has no date field defined' ;
2015-01-22 20:09:57 +01:00
$sql .= " AND ( " . $datefieldname . " <= ' " . $this -> db -> idate ( $datee ) . " ' OR " . $datefieldname . " IS NULL) " ;
2014-10-29 20:21:24 +01:00
}
if ( ! $sql ) return - 1 ;
2011-11-02 22:41:34 +01:00
//print $sql;
2014-06-13 01:46:27 +02:00
dol_syslog ( get_class ( $this ) . " ::get_element_list " , LOG_DEBUG );
2011-11-02 22:41:34 +01:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
$nump = $this -> db -> num_rows ( $result );
if ( $nump )
{
$i = 0 ;
while ( $i < $nump )
{
$obj = $this -> db -> fetch_object ( $result );
2015-06-03 21:01:50 +02:00
$elements [ $i ] = $obj -> rowid . ( empty ( $obj -> fk_user ) ? '' : '_' . $obj -> fk_user );
2011-11-02 22:41:34 +01:00
$i ++ ;
}
$this -> db -> free ( $result );
}
2018-02-22 13:22:31 +01:00
/* Return array even if empty*/
return $elements ;
2011-11-02 22:41:34 +01:00
}
else
{
dol_print_error ( $this -> db );
}
}
/**
* Delete a project from database
*
* @ param User $user User
* @ param int $notrigger Disable triggers
2011-09-20 18:20:22 +02:00
* @ return int < 0 if KO , 0 if not possible , > 0 if OK
2011-11-02 22:41:34 +01:00
*/
function delete ( $user , $notrigger = 0 )
{
global $langs , $conf ;
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2011-11-02 22:41:34 +01:00
$error = 0 ;
$this -> db -> begin ();
if ( ! $error )
{
// Delete linked contacts
$res = $this -> delete_linked_contact ();
if ( $res < 0 )
{
$this -> error = 'ErrorFailToDeleteLinkedContact' ;
//$error++;
$this -> db -> rollback ();
return 0 ;
}
}
2015-02-21 12:57:17 +01:00
// Set fk_projet into elements to null
$listoftables = array (
2017-04-11 02:48:16 +02:00
'facture' => 'fk_projet' , 'propal' => 'fk_projet' , 'commande' => 'fk_projet' ,
'facture_fourn' => 'fk_projet' , 'commande_fournisseur' => 'fk_projet' , 'supplier_proposal' => 'fk_projet' ,
2015-06-05 15:41:27 +02:00
'expensereport_det' => 'fk_projet' , 'contrat' => 'fk_projet' , 'fichinter' => 'fk_projet' , 'don' => 'fk_projet'
2015-02-21 12:57:17 +01:00
);
foreach ( $listoftables as $key => $value )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . $key . " SET " . $value . " = NULL where " . $value . " = " . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> errors [] = $this -> db -> lasterror ();
$error ++ ;
break ;
}
}
2015-02-21 15:18:05 +01:00
2018-03-14 11:38:42 +01:00
// Fetch tasks
$this -> getLinesArray ( $user );
2014-12-14 15:09:30 +01:00
2018-03-14 11:38:42 +01:00
// Delete tasks
2018-08-14 09:32:43 +02:00
$ret = $this -> deleteTasks ( $user );
if ( $ret < 0 ) $error ++ ;
2013-07-07 13:14:32 +02:00
2014-12-14 15:09:30 +01:00
// Delete project
if ( ! $error )
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " projet " ;
$sql .= " WHERE rowid= " . $this -> id ;
2013-07-07 13:14:32 +02:00
2014-12-14 15:09:30 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
2015-07-19 14:56:52 +02:00
$this -> errors [] = $langs -> trans ( " CantRemoveProject " );
2014-12-14 15:09:30 +01:00
$error ++ ;
}
}
2013-07-07 13:14:32 +02:00
2014-12-14 15:09:30 +01:00
if ( ! $error )
2014-05-07 15:11:46 +02:00
{
2014-12-14 15:09:30 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " projet_extrafields " ;
$sql .= " WHERE fk_object= " . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> errors [] = $this -> db -> lasterror ();
$error ++ ;
}
2014-05-07 15:11:46 +02:00
}
2013-07-07 13:14:32 +02:00
2014-05-07 15:11:46 +02:00
if ( empty ( $error ))
2011-11-02 22:41:34 +01:00
{
// We remove directory
$projectref = dol_sanitizeFileName ( $this -> ref );
if ( $conf -> projet -> dir_output )
{
$dir = $conf -> projet -> dir_output . " / " . $projectref ;
if ( file_exists ( $dir ))
{
2012-04-15 11:47:57 +02:00
$res = @ dol_delete_dir_recursive ( $dir );
2011-11-02 22:41:34 +01:00
if ( ! $res )
{
2014-05-07 15:11:46 +02:00
$this -> errors [] = 'ErrorFailToDeleteDir' ;
$error ++ ;
2011-11-02 22:41:34 +01:00
}
}
}
if ( ! $notrigger )
{
2014-07-03 17:15:42 +02:00
// Call trigger
$result = $this -> call_trigger ( 'PROJECT_DELETE' , $user );
2014-12-25 22:59:57 +01:00
if ( $result < 0 ) {
2011-11-02 22:41:34 +01:00
$error ++ ;
}
// End call triggers
}
2014-05-07 15:11:46 +02:00
}
2011-11-02 22:41:34 +01:00
2014-05-07 19:30:19 +02:00
if ( empty ( $error ))
{
2011-11-02 22:41:34 +01:00
$this -> db -> commit ();
return 1 ;
}
else
2014-05-07 19:30:19 +02:00
{
foreach ( $this -> errors as $errmsg )
{
2014-05-07 15:11:46 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " . $errmsg , LOG_ERR );
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
}
2012-04-16 11:39:51 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " . $this -> error , LOG_ERR );
2011-11-02 22:41:34 +01:00
$this -> db -> rollback ();
return - 1 ;
}
}
2018-08-09 12:13:06 +02:00
/**
2018-08-21 13:09:06 +02:00
* Delete tasks with no children first , then task with children recursively
2018-08-09 12:13:06 +02:00
*
2018-08-21 13:10:20 +02:00
* @ param User $user User
* @ return int < 0 if KO , 1 if OK
2018-08-09 12:13:06 +02:00
*/
2018-08-14 09:32:43 +02:00
function deleteTasks ( $user )
2018-08-09 12:13:06 +02:00
{
2018-08-14 09:32:43 +02:00
$countTasks = count ( $this -> lines );
$deleted = false ;
2018-08-21 13:09:06 +02:00
if ( $countTasks )
2018-08-21 13:10:20 +02:00
{
2018-08-14 09:32:43 +02:00
foreach ( $this -> lines as $task )
{
2018-08-21 13:09:06 +02:00
if ( $task -> hasChildren () <= 0 ) { // If there is no children (or error to detect them)
2018-08-14 09:32:43 +02:00
$deleted = true ;
$ret = $task -> delete ( $user );
2018-08-21 13:09:06 +02:00
if ( $ret <= 0 )
2018-08-14 09:32:43 +02:00
{
$this -> errors [] = $this -> db -> lasterror ();
return - 1 ;
}
}
}
2018-08-09 12:13:06 +02:00
}
2018-08-09 12:21:48 +02:00
$this -> getLinesArray ( $user );
2018-08-21 13:09:06 +02:00
if ( $deleted && count ( $this -> lines ) < $countTasks )
2018-08-14 09:32:43 +02:00
{
if ( count ( $this -> lines )) $this -> deleteTasks ( $this -> lines );
}
return 1 ;
2018-08-09 12:13:06 +02:00
}
2011-05-18 14:20:34 +02:00
2011-11-02 22:41:34 +01:00
/**
* Validate a project
*
2016-11-09 22:54:51 +01:00
* @ param User $user User that validate
* @ param int $notrigger 1 = Disable triggers
* @ return int < 0 if KO , > 0 if OK
2011-11-02 22:41:34 +01:00
*/
2016-11-09 22:54:51 +01:00
function setValid ( $user , $notrigger = 0 )
2011-11-02 22:41:34 +01:00
{
global $langs , $conf ;
2011-12-05 19:41:38 +01:00
$error = 0 ;
2012-02-04 18:34:52 +01:00
2011-11-02 22:41:34 +01:00
if ( $this -> statut != 1 )
{
2016-10-16 12:12:45 +02:00
// Check parameters
if ( preg_match ( '/^' . preg_quote ( $langs -> trans ( " CopyOf " ) . ' ' ) . '/' , $this -> title ))
{
$this -> error = $langs -> trans ( " ErrorFieldFormat " , $langs -> transnoentities ( " Label " )) . '. ' . $langs -> trans ( 'RemoveString' , $langs -> transnoentitiesnoconv ( " CopyOf " ));
return - 1 ;
}
2017-06-07 16:44:04 +02:00
2011-11-02 22:41:34 +01:00
$this -> db -> begin ();
$sql = " UPDATE " . MAIN_DB_PREFIX . " projet " ;
$sql .= " SET fk_statut = 1 " ;
$sql .= " WHERE rowid = " . $this -> id ;
$sql .= " AND entity = " . $conf -> entity ;
2014-06-13 01:46:27 +02:00
dol_syslog ( get_class ( $this ) . " ::setValid " , LOG_DEBUG );
2011-11-02 22:41:34 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2014-07-03 17:15:42 +02:00
// Call trigger
2016-11-09 22:54:51 +01:00
if ( empty ( $notrigger ))
{
$result = $this -> call_trigger ( 'PROJECT_VALIDATE' , $user );
if ( $result < 0 ) { $error ++ ; }
// End call triggers
}
2017-06-07 16:44:04 +02:00
2011-11-02 22:41:34 +01:00
if ( ! $error )
{
2012-08-03 19:05:20 +02:00
$this -> statut = 1 ;
$this -> db -> commit ();
2011-11-02 22:41:34 +01:00
return 1 ;
}
else
{
$this -> db -> rollback ();
$this -> error = join ( ',' , $this -> errors );
2012-04-27 11:03:01 +02:00
dol_syslog ( get_class ( $this ) . " ::setValid " . $this -> error , LOG_ERR );
2011-11-02 22:41:34 +01:00
return - 1 ;
}
}
else
{
$this -> db -> rollback ();
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
}
/**
* Close a project
*
2015-03-04 16:28:29 +01:00
* @ param User $user User that close project
2017-02-01 13:25:16 +01:00
* @ return int < 0 if KO , 0 if already closed , > 0 if OK
2011-11-02 22:41:34 +01:00
*/
function setClose ( $user )
{
global $langs , $conf ;
2015-03-04 16:28:29 +01:00
$now = dol_now ();
2011-12-05 19:41:38 +01:00
$error = 0 ;
2012-02-04 18:34:52 +01:00
2011-11-02 22:41:34 +01:00
if ( $this -> statut != 2 )
{
$this -> db -> begin ();
$sql = " UPDATE " . MAIN_DB_PREFIX . " projet " ;
2015-03-04 16:28:29 +01:00
$sql .= " SET fk_statut = 2, fk_user_close = " . $user -> id . " , date_close = ' " . $this -> db -> idate ( $now ) . " ' " ;
2011-11-02 22:41:34 +01:00
$sql .= " WHERE rowid = " . $this -> id ;
$sql .= " AND entity = " . $conf -> entity ;
$sql .= " AND fk_statut = 1 " ;
2015-06-29 16:57:32 +02:00
if ( ! empty ( $conf -> global -> PROJECT_USE_OPPORTUNITIES ))
{
2016-09-30 11:45:36 +02:00
// TODO What to do if fk_opp_status is not code 'WON' or 'LOST'
2015-06-29 16:57:32 +02:00
}
2014-06-13 01:46:27 +02:00
dol_syslog ( get_class ( $this ) . " ::setClose " , LOG_DEBUG );
2011-11-02 22:41:34 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2014-07-03 17:15:42 +02:00
// Call trigger
$result = $this -> call_trigger ( 'PROJECT_CLOSE' , $user );
2014-07-04 13:43:50 +02:00
if ( $result < 0 ) { $error ++ ; }
2014-07-03 17:15:42 +02:00
// End call triggers
2011-11-02 22:41:34 +01:00
if ( ! $error )
{
2012-08-18 05:57:25 +02:00
$this -> statut = 2 ;
2011-11-02 22:41:34 +01:00
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
$this -> error = join ( ',' , $this -> errors );
2012-04-27 11:03:01 +02:00
dol_syslog ( get_class ( $this ) . " ::setClose " . $this -> error , LOG_ERR );
2011-11-02 22:41:34 +01:00
return - 1 ;
}
}
else
{
$this -> db -> rollback ();
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
2017-06-07 16:44:04 +02:00
2017-02-01 13:25:16 +01:00
return 0 ;
2011-11-02 22:41:34 +01:00
}
/**
* Return status label of object
*
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto
* @ return string Label
*/
function getLibStatut ( $mode = 0 )
{
return $this -> LibStatut ( $this -> statut , $mode );
}
/**
* Renvoi status label for a status
*
* @ param int $statut id statut
* @ param int $mode 0 = long label , 1 = short label , 2 = Picto + short label , 3 = Picto , 4 = Picto + long label , 5 = Short label + Picto
* @ return string Label
*/
function LibStatut ( $statut , $mode = 0 )
{
global $langs ;
if ( $mode == 0 )
{
2015-06-29 16:57:32 +02:00
return $langs -> trans ( $this -> statuts_long [ $statut ]);
2011-11-02 22:41:34 +01:00
}
if ( $mode == 1 )
{
return $langs -> trans ( $this -> statuts_short [ $statut ]);
}
if ( $mode == 2 )
{
if ( $statut == 0 )
2015-06-29 16:57:32 +02:00
return img_picto ( $langs -> trans ( $this -> statuts_long [ $statut ]), 'statut0' ) . ' ' . $langs -> trans ( $this -> statuts_short [ $statut ]);
2011-11-02 22:41:34 +01:00
if ( $statut == 1 )
2015-06-29 16:57:32 +02:00
return img_picto ( $langs -> trans ( $this -> statuts_long [ $statut ]), 'statut4' ) . ' ' . $langs -> trans ( $this -> statuts_short [ $statut ]);
2011-11-02 22:41:34 +01:00
if ( $statut == 2 )
2015-06-29 16:57:32 +02:00
return img_picto ( $langs -> trans ( $this -> statuts_long [ $statut ]), 'statut6' ) . ' ' . $langs -> trans ( $this -> statuts_short [ $statut ]);
2011-11-02 22:41:34 +01:00
}
if ( $mode == 3 )
{
if ( $statut == 0 )
2015-06-29 16:57:32 +02:00
return img_picto ( $langs -> trans ( $this -> statuts_long [ $statut ]), 'statut0' );
2011-11-02 22:41:34 +01:00
if ( $statut == 1 )
2015-06-29 16:57:32 +02:00
return img_picto ( $langs -> trans ( $this -> statuts_long [ $statut ]), 'statut4' );
2011-11-02 22:41:34 +01:00
if ( $statut == 2 )
2015-06-29 16:57:32 +02:00
return img_picto ( $langs -> trans ( $this -> statuts_long [ $statut ]), 'statut6' );
2011-11-02 22:41:34 +01:00
}
if ( $mode == 4 )
{
if ( $statut == 0 )
2015-06-29 16:57:32 +02:00
return img_picto ( $langs -> trans ( $this -> statuts_long [ $statut ]), 'statut0' ) . ' ' . $langs -> trans ( $this -> statuts_long [ $statut ]);
2011-11-02 22:41:34 +01:00
if ( $statut == 1 )
2015-06-29 16:57:32 +02:00
return img_picto ( $langs -> trans ( $this -> statuts_long [ $statut ]), 'statut4' ) . ' ' . $langs -> trans ( $this -> statuts_long [ $statut ]);
2011-11-02 22:41:34 +01:00
if ( $statut == 2 )
2015-06-29 16:57:32 +02:00
return img_picto ( $langs -> trans ( $this -> statuts_long [ $statut ]), 'statut6' ) . ' ' . $langs -> trans ( $this -> statuts_long [ $statut ]);
2011-11-02 22:41:34 +01:00
}
if ( $mode == 5 )
{
if ( $statut == 0 )
2015-06-29 16:57:32 +02:00
return $langs -> trans ( $this -> statuts_short [ $statut ]) . ' ' . img_picto ( $langs -> trans ( $this -> statuts_long [ $statut ]), 'statut0' );
2011-11-02 22:41:34 +01:00
if ( $statut == 1 )
2015-06-29 16:57:32 +02:00
return $langs -> trans ( $this -> statuts_short [ $statut ]) . ' ' . img_picto ( $langs -> trans ( $this -> statuts_long [ $statut ]), 'statut4' );
2011-11-02 22:41:34 +01:00
if ( $statut == 2 )
2015-06-29 16:57:32 +02:00
return $langs -> trans ( $this -> statuts_short [ $statut ]) . ' ' . img_picto ( $langs -> trans ( $this -> statuts_long [ $statut ]), 'statut6' );
2011-11-02 22:41:34 +01:00
}
}
/**
2015-03-15 14:04:07 +01:00
* Return clicable name ( with picto eventually )
2011-11-02 22:41:34 +01:00
*
2017-05-16 23:38:23 +02:00
* @ param int $withpicto 0 = No picto , 1 = Include picto into link , 2 = Only picto
* @ param string $option Variant ( '' , 'nolink' )
* @ param int $addlabel 0 = Default , 1 = Add label into string , > 1 = Add first chars into string
* @ param string $moreinpopup Text to add into popup
* @ param string $sep Separator between ref and label if option addlabel is set
* @ param int $notooltip 1 = Disable tooltip
* @ param int $save_lastsearch_value - 1 = Auto , 0 = No save of lastsearch_values when clicking , 1 = Save lastsearch_values whenclicking
* @ return string String with URL
2011-11-02 22:41:34 +01:00
*/
2017-05-16 23:38:23 +02:00
function getNomUrl ( $withpicto = 0 , $option = '' , $addlabel = 0 , $moreinpopup = '' , $sep = ' - ' , $notooltip = 0 , $save_lastsearch_value =- 1 )
2011-11-02 22:41:34 +01:00
{
2018-06-04 12:55:55 +02:00
global $conf , $langs , $user , $hookmanager ;
2011-11-02 22:41:34 +01:00
2016-10-25 10:53:25 +02:00
if ( ! empty ( $conf -> dol_no_mouse_hover )) $notooltip = 1 ; // Force disable tooltips
2017-06-07 16:44:04 +02:00
2011-11-02 22:41:34 +01:00
$result = '' ;
2017-06-07 16:44:04 +02:00
2015-10-06 00:46:26 +02:00
$label = '' ;
if ( $option != 'nolink' ) $label = '<u>' . $langs -> trans ( " ShowProject " ) . '</u>' ;
2017-05-16 23:38:23 +02:00
$label .= ( $label ? '<br>' : '' ) . '<b>' . $langs -> trans ( 'Ref' ) . ': </b>' . $this -> ref ; // The space must be after the : to not being explode when showing the title in img_picto
$label .= ( $label ? '<br>' : '' ) . '<b>' . $langs -> trans ( 'Label' ) . ': </b>' . $this -> title ; // The space must be after the : to not being explode when showing the title in img_picto
2016-07-20 18:02:33 +02:00
if ( ! empty ( $this -> thirdparty_name ))
$label .= ( $label ? '<br>' : '' ) . '<b>' . $langs -> trans ( 'ThirdParty' ) . ': </b>' . $this -> thirdparty_name ; // The space must be after the : to not being explode when showing the title in img_picto
2016-01-18 15:56:51 +01:00
if ( ! empty ( $this -> dateo ))
$label .= ( $label ? '<br>' : '' ) . '<b>' . $langs -> trans ( 'DateStart' ) . ': </b>' . dol_print_date ( $this -> dateo , 'day' ); // The space must be after the : to not being explode when showing the title in img_picto
if ( ! empty ( $this -> datee ))
$label .= ( $label ? '<br>' : '' ) . '<b>' . $langs -> trans ( 'DateEnd' ) . ': </b>' . dol_print_date ( $this -> datee , 'day' ); // The space must be after the : to not being explode when showing the title in img_picto
2015-06-03 16:59:48 +02:00
if ( $moreinpopup ) $label .= '<br>' . $moreinpopup ;
2015-01-20 20:45:38 +01:00
2017-07-25 12:44:55 +02:00
$url = '' ;
2016-08-29 10:32:00 +02:00
if ( $option != 'nolink' )
2015-10-06 00:46:26 +02:00
{
2015-01-20 20:45:38 +01:00
if ( preg_match ( '/\.php$/' , $option )) {
2016-10-25 10:53:25 +02:00
$url = dol_buildpath ( $option , 1 ) . '?id=' . $this -> id ;
2016-01-21 15:15:26 +01:00
}
else if ( $option == 'task' )
{
2016-10-25 10:53:25 +02:00
$url = DOL_URL_ROOT . '/projet/tasks.php?id=' . $this -> id ;
2016-01-21 15:15:26 +01:00
}
else
{
2016-10-25 10:53:25 +02:00
$url = DOL_URL_ROOT . '/projet/card.php?id=' . $this -> id ;
}
2017-05-16 23:38:23 +02:00
// Add param to save lastsearch_values or not
2017-06-07 16:44:04 +02:00
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
2017-05-16 23:38:23 +02:00
if ( $save_lastsearch_value == - 1 && preg_match ( '/list\.php/' , $_SERVER [ " PHP_SELF " ])) $add_save_lastsearch_values = 1 ;
if ( $add_save_lastsearch_values ) $url .= '&save_lastsearch_values=1' ;
2016-10-25 10:53:25 +02:00
}
2017-06-07 16:44:04 +02:00
2016-10-25 10:53:25 +02:00
$linkclose = '' ;
2017-07-07 20:28:43 +02:00
if ( empty ( $notooltip ) && $user -> rights -> projet -> lire )
2016-10-25 10:53:25 +02:00
{
if ( ! empty ( $conf -> global -> MAIN_OPTIMIZEFORTEXTBROWSER ))
{
$label = $langs -> trans ( " ShowProject " );
$linkclose .= ' alt="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
2015-01-20 20:45:38 +01:00
}
2016-10-25 10:53:25 +02:00
$linkclose .= ' title="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
$linkclose .= ' class="classfortooltip"' ;
2017-07-25 12:44:55 +02:00
2018-06-04 12:55:55 +02:00
/*
2018-03-15 20:46:55 +01:00
$hookmanager -> initHooks ( array ( 'projectdao' ));
$parameters = array ( 'id' => $this -> id );
// Note that $action and $object may have been modified by some hooks
$reshook = $hookmanager -> executeHooks ( 'getnomurltooltip' , $parameters , $this , $action );
if ( $reshook > 0 )
$linkclose = $hookmanager -> resPrint ;
*/
2017-07-06 15:48:22 +02:00
}
2011-11-02 22:41:34 +01:00
$picto = 'projectpub' ;
2017-11-02 15:03:09 +01:00
if ( ! $this -> public ) $picto = 'project' ;
2011-11-02 22:41:34 +01:00
2016-10-25 10:53:25 +02:00
$linkstart = '<a href="' . $url . '"' ;
$linkstart .= $linkclose . '>' ;
$linkend = '</a>' ;
2017-06-07 16:44:04 +02:00
2017-11-02 15:03:09 +01:00
$result .= $linkstart ;
if ( $withpicto ) $result .= img_object (( $notooltip ? '' : $label ), $picto , ( $notooltip ? (( $withpicto != 2 ) ? 'class="paddingright"' : '' ) : 'class="' . (( $withpicto != 2 ) ? 'paddingright ' : '' ) . 'classfortooltip"' ), 0 , 0 , $notooltip ? 0 : 1 );
if ( $withpicto != 2 ) $result .= $this -> ref ;
$result .= $linkend ;
if ( $withpicto != 2 ) $result .= (( $addlabel && $this -> title ) ? $sep . dol_trunc ( $this -> title , ( $addlabel > 1 ? $addlabel : 0 )) : '' );
2018-03-15 20:46:55 +01:00
global $action ;
$hookmanager -> initHooks ( array ( 'projectdao' ));
$parameters = array ( 'id' => $this -> id , 'getnomurl' => $result );
$reshook = $hookmanager -> executeHooks ( 'getNomUrl' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( $reshook > 0 ) $result = $hookmanager -> resPrint ;
else $result .= $hookmanager -> resPrint ;
2011-11-02 22:41:34 +01:00
return $result ;
}
/**
2011-09-20 19:19:46 +02:00
* Initialise an instance with random values .
* Used to build previews or test instances .
2011-11-02 22:41:34 +01:00
* id must be 0 if object instance is a specimen .
2011-09-20 19:19:46 +02:00
*
* @ return void
2011-11-02 22:41:34 +01:00
*/
function initAsSpecimen ()
{
global $user , $langs , $conf ;
2012-04-30 15:01:25 +02:00
$now = dol_now ();
2011-11-02 22:41:34 +01:00
2014-05-01 19:17:45 +02:00
// Initialise parameters
2011-11-02 22:41:34 +01:00
$this -> id = 0 ;
$this -> ref = 'SPECIMEN' ;
$this -> specimen = 1 ;
2012-03-21 12:33:51 +01:00
$this -> socid = 1 ;
2011-11-02 22:41:34 +01:00
$this -> date_c = $now ;
$this -> date_m = $now ;
$this -> date_start = $now ;
2015-11-07 12:36:34 +01:00
$this -> date_end = $now + ( 3600 * 24 * 365 );
2011-11-02 22:41:34 +01:00
$this -> note_public = 'SPECIMEN' ;
2015-06-29 16:57:32 +02:00
$this -> fk_ele = 20000 ;
$this -> opp_amount = 20000 ;
2015-02-16 10:40:09 +01:00
$this -> budget_amount = 10000 ;
2014-05-01 19:17:45 +02:00
/*
2016-03-05 15:01:25 +01:00
$nbp = mt_rand ( 1 , 9 );
2011-11-02 22:41:34 +01:00
$xnbp = 0 ;
while ( $xnbp < $nbp )
{
$line = new Task ( $this -> db );
2014-05-01 19:17:45 +02:00
$line -> fk_project = 0 ;
$line -> label = $langs -> trans ( " Label " ) . " " . $xnbp ;
$line -> description = $langs -> trans ( " Description " ) . " " . $xnbp ;
$this -> lines [] = $line ;
2011-11-02 22:41:34 +01:00
$xnbp ++ ;
}
2014-05-01 19:17:45 +02:00
*/
2011-11-02 22:41:34 +01:00
}
/**
2012-02-04 18:34:52 +01:00
* Check if user has permission on current project
2011-11-02 22:41:34 +01:00
*
* @ param User $user Object user to evaluate
2012-02-04 18:34:52 +01:00
* @ param string $mode Type of permission we want to know : 'read' , 'write'
* @ return int > 0 if user has permission , < 0 if user has no permission
2011-11-02 22:41:34 +01:00
*/
2012-02-04 18:34:52 +01:00
function restrictedProjectArea ( $user , $mode = 'read' )
2011-11-02 22:41:34 +01:00
{
// To verify role of users
$userAccess = 0 ;
2013-04-26 19:13:39 +02:00
if (( $mode == 'read' && ! empty ( $user -> rights -> projet -> all -> lire )) || ( $mode == 'write' && ! empty ( $user -> rights -> projet -> all -> creer )) || ( $mode == 'delete' && ! empty ( $user -> rights -> projet -> all -> supprimer )))
2011-11-02 22:41:34 +01:00
{
$userAccess = 1 ;
}
2013-04-26 19:13:39 +02:00
else if ( $this -> public && (( $mode == 'read' && ! empty ( $user -> rights -> projet -> lire )) || ( $mode == 'write' && ! empty ( $user -> rights -> projet -> creer )) || ( $mode == 'delete' && ! empty ( $user -> rights -> projet -> supprimer ))))
2011-11-02 22:41:34 +01:00
{
$userAccess = 1 ;
}
else
2013-12-01 16:01:33 +01:00
{
2011-11-02 22:41:34 +01:00
foreach ( array ( 'internal' , 'external' ) as $source )
{
$userRole = $this -> liste_contact ( 4 , $source );
$num = count ( $userRole );
$nblinks = 0 ;
while ( $nblinks < $num )
{
2013-12-01 16:01:33 +01:00
if ( $source == 'internal' && preg_match ( '/^PROJECT/' , $userRole [ $nblinks ][ 'code' ]) && $user -> id == $userRole [ $nblinks ][ 'id' ])
2012-05-23 13:09:36 +02:00
{
if ( $mode == 'read' && $user -> rights -> projet -> lire ) $userAccess ++ ;
if ( $mode == 'write' && $user -> rights -> projet -> creer ) $userAccess ++ ;
if ( $mode == 'delete' && $user -> rights -> projet -> supprimer ) $userAccess ++ ;
}
2011-11-02 22:41:34 +01:00
$nblinks ++ ;
}
}
//if (empty($nblinks)) // If nobody has permission, we grant creator
//{
// if ((!empty($this->user_author_id) && $this->user_author_id == $user->id))
// {
// $userAccess = 1;
// }
//}
}
2012-02-04 18:34:52 +01:00
return ( $userAccess ? $userAccess :- 1 );
2011-11-02 22:41:34 +01:00
}
/**
* Return array of projects a user has permission on , is affected to , or all projects
*
2012-03-19 14:44:27 +01:00
* @ param User $user User object
2016-01-21 14:12:17 +01:00
* @ param int $mode 0 = All project I have permission on ( assigned to me and public ), 1 = Projects assigned to me only , 2 = Will return list of all projects with no test on contacts
2018-02-26 11:57:29 +01:00
* @ param int $list 0 = Return array , 1 = Return string list
2012-03-19 14:44:27 +01:00
* @ param int $socid 0 = No filter on third party , id of third party
2018-02-26 11:57:29 +01:00
* @ param string $filter additionnal filter on project ( statut , ref , ... )
* @ return array or string Array of projects id , or string with projects id separated with " , " if list is 1
2011-11-02 22:41:34 +01:00
*/
2017-06-06 08:29:41 +02:00
function getProjectsAuthorizedForUser ( $user , $mode = 0 , $list = 0 , $socid = 0 , $filter = '' )
2011-11-02 22:41:34 +01:00
{
$projects = array ();
$temp = array ();
2014-08-15 02:11:01 +02:00
$sql = " SELECT " . (( $mode == 0 || $mode == 1 ) ? " DISTINCT " : " " ) . " p.rowid, p.ref " ;
2011-11-02 22:41:34 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " projet as p " ;
if ( $mode == 0 || $mode == 1 )
{
$sql .= " , " . MAIN_DB_PREFIX . " element_contact as ec " ;
}
2017-05-30 18:50:54 +02:00
$sql .= " WHERE p.entity IN ( " . getEntity ( 'project' ) . " ) " ;
2010-09-18 15:38:43 +02:00
// Internal users must see project he is contact to even if project linked to a third party he can't see.
2011-11-02 22:41:34 +01:00
//if ($socid || ! $user->rights->societe->client->voir) $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".$socid.")";
2012-03-19 14:44:27 +01:00
if ( $socid > 0 ) $sql .= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = " . $socid . " ) " ;
2010-09-18 15:38:43 +02:00
2016-01-21 14:12:17 +01:00
// Get id of types of contacts for projects (This list never contains a lot of elements)
$listofprojectcontacttype = array ();
$sql2 = " SELECT ctc.rowid, ctc.code FROM " . MAIN_DB_PREFIX . " c_type_contact as ctc " ;
2017-05-12 16:55:11 +02:00
$sql2 .= " WHERE ctc.element = ' " . $this -> db -> escape ( $this -> element ) . " ' " ;
2016-01-21 14:12:17 +01:00
$sql2 .= " AND ctc.source = 'internal' " ;
$resql = $this -> db -> query ( $sql2 );
if ( $resql )
{
while ( $obj = $this -> db -> fetch_object ( $resql ))
{
$listofprojectcontacttype [ $obj -> rowid ] = $obj -> code ;
}
}
else dol_print_error ( $this -> db );
if ( count ( $listofprojectcontacttype ) == 0 ) $listofprojectcontacttype [ 0 ] = '0' ; // To avoid syntax error if not found
2016-08-29 10:32:00 +02:00
2010-09-18 15:38:43 +02:00
if ( $mode == 0 )
2011-11-02 22:41:34 +01:00
{
2012-05-23 14:34:44 +02:00
$sql .= " AND ec.element_id = p.rowid " ;
$sql .= " AND ( p.public = 1 " ;
2016-01-21 14:12:17 +01:00
$sql .= " OR ( ec.fk_c_type_contact IN ( " . join ( ',' , array_keys ( $listofprojectcontacttype )) . " ) " ;
$sql .= " AND ec.fk_socpeople = " . $user -> id . " ) " ;
2012-05-23 14:34:44 +02:00
$sql .= " ) " ;
2011-11-02 22:41:34 +01:00
}
2010-09-18 15:38:43 +02:00
if ( $mode == 1 )
{
$sql .= " AND ec.element_id = p.rowid " ;
2016-01-21 14:12:17 +01:00
$sql .= " AND ( " ;
$sql .= " ( ec.fk_c_type_contact IN ( " . join ( ',' , array_keys ( $listofprojectcontacttype )) . " ) " ;
$sql .= " AND ec.fk_socpeople = " . $user -> id . " ) " ;
2012-05-23 14:34:44 +02:00
$sql .= " ) " ;
2010-09-18 15:38:43 +02:00
}
if ( $mode == 2 )
{
// No filter. Use this if user has permission to see all project
}
2017-06-07 16:44:04 +02:00
2017-06-06 08:29:41 +02:00
$sql .= $filter ;
2010-11-11 13:16:55 +01:00
//print $sql;
2010-02-04 17:47:30 +01:00
2011-11-02 22:41:34 +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 );
$projects [ $row [ 0 ]] = $row [ 1 ];
$temp [] = $row [ 0 ];
$i ++ ;
}
$this -> db -> free ( $resql );
if ( $list )
{
2013-01-02 15:12:46 +01:00
if ( empty ( $temp )) return '0' ;
2011-11-02 22:41:34 +01:00
$result = implode ( ',' , $temp );
return $result ;
}
}
else
{
dol_print_error ( $this -> db );
}
return $projects ;
}
2012-05-06 00:15:03 +02:00
2012-05-07 17:04:35 +02:00
/**
* Load an object from its id and create a new one in database
*
* @ param int $fromid Id of object to clone
2016-07-21 12:47:56 +02:00
* @ param bool $clone_contact Clone contact of project
* @ param bool $clone_task Clone task of project
* @ param bool $clone_project_file Clone file of project
* @ param bool $clone_task_file Clone file of task ( if task are copied )
* @ param bool $clone_note Clone note of project
* @ param bool $move_date Move task date on clone
* @ param integer $notrigger No trigger flag
* @ param int $newthirdpartyid New thirdparty id
2012-05-07 17:04:35 +02:00
* @ return int New id of clone
*/
2016-07-21 12:47:56 +02:00
function createFromClone ( $fromid , $clone_contact = false , $clone_task = true , $clone_project_file = false , $clone_task_file = false , $clone_note = true , $move_date = true , $notrigger = 0 , $newthirdpartyid = 0 )
2012-04-27 11:03:01 +02:00
{
global $user , $langs , $conf ;
$error = 0 ;
2012-05-06 00:15:03 +02:00
2015-01-18 12:06:05 +01:00
dol_syslog ( " createFromClone clone_contact= " . $clone_contact . " clone_task= " . $clone_task . " clone_project_file= " . $clone_project_file . " clone_note= " . $clone_note . " move_date= " . $move_date , LOG_DEBUG );
2012-05-07 17:04:35 +02:00
2012-04-27 11:03:01 +02:00
$now = dol_mktime ( 0 , 0 , 0 , idate ( 'm' , dol_now ()), idate ( 'd' , dol_now ()), idate ( 'Y' , dol_now ()));
$clone_project = new Project ( $this -> db );
2015-02-26 13:03:17 +01:00
$clone_project -> context [ 'createfromclone' ] = 'createfromclone' ;
2012-04-27 11:03:01 +02:00
$this -> db -> begin ();
// Load source object
$clone_project -> fetch ( $fromid );
2016-06-24 15:27:47 +02:00
$clone_project -> fetch_optionals ();
2016-07-21 12:47:56 +02:00
if ( $newthirdpartyid > 0 ) $clone_project -> socid = $newthirdpartyid ;
2014-08-21 23:43:11 +02:00
$clone_project -> fetch_thirdparty ();
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
$orign_dt_start = $clone_project -> date_start ;
$orign_project_ref = $clone_project -> ref ;
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
$clone_project -> id = 0 ;
2014-12-05 21:40:55 +01:00
if ( $move_date ) {
$clone_project -> date_start = $now ;
if ( ! ( empty ( $clone_project -> date_end )))
{
$clone_project -> date_end = $clone_project -> date_end + ( $now - $orign_dt_start );
}
}
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
$clone_project -> datec = $now ;
2012-05-07 17:04:35 +02:00
if ( ! $clone_note )
2012-04-27 11:03:01 +02:00
{
$clone_project -> note_private = '' ;
$clone_project -> note_public = '' ;
}
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
//Generate next ref
$defaultref = '' ;
$obj = empty ( $conf -> global -> PROJECT_ADDON ) ? 'mod_project_simple' : $conf -> global -> PROJECT_ADDON ;
2016-03-21 11:47:32 +01:00
// Search template files
$file = '' ; $classname = '' ; $filefound = 0 ;
$dirmodels = array_merge ( array ( '/' ),( array ) $conf -> modules_parts [ 'models' ]);
foreach ( $dirmodels as $reldir )
2012-04-27 11:03:01 +02:00
{
2016-03-21 11:47:32 +01:00
$file = dol_buildpath ( $reldir . " core/modules/project/ " . $obj . '.php' , 0 );
if ( file_exists ( $file ))
{
$filefound = 1 ;
dol_include_once ( $reldir . " core/modules/project/ " . $obj . '.php' );
$modProject = new $obj ;
$defaultref = $modProject -> getNextValue ( is_object ( $clone_project -> thirdparty ) ? $clone_project -> thirdparty : null , $clone_project );
break ;
}
2012-04-27 11:03:01 +02:00
}
if ( is_numeric ( $defaultref ) && $defaultref <= 0 ) $defaultref = '' ;
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
$clone_project -> ref = $defaultref ;
2016-10-16 12:12:45 +02:00
$clone_project -> title = $langs -> trans ( " CopyOf " ) . ' ' . $clone_project -> title ;
2012-04-27 11:03:01 +02:00
// Create clone
2013-10-19 17:51:21 +02:00
$result = $clone_project -> create ( $user , $notrigger );
2012-04-27 11:03:01 +02:00
// Other options
if ( $result < 0 )
{
$this -> error .= $clone_project -> error ;
$error ++ ;
}
if ( ! $error )
{
//Get the new project id
$clone_project_id = $clone_project -> id ;
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
//Note Update
if ( ! $clone_note )
{
$clone_project -> note_private = '' ;
$clone_project -> note_public = '' ;
}
else
{
$this -> db -> begin ();
2013-04-10 10:58:38 +02:00
$res = $clone_project -> update_note ( dol_html_entity_decode ( $clone_project -> note_public , ENT_QUOTES ), '_public' );
2012-04-27 11:03:01 +02:00
if ( $res < 0 )
{
$this -> error .= $clone_project -> error ;
$error ++ ;
$this -> db -> rollback ();
}
else
{
$this -> db -> commit ();
}
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
$this -> db -> begin ();
2013-04-20 05:53:18 +02:00
$res = $clone_project -> update_note ( dol_html_entity_decode ( $clone_project -> note_private , ENT_QUOTES ), '_private' );
2012-04-27 11:03:01 +02:00
if ( $res < 0 )
{
$this -> error .= $clone_project -> error ;
$error ++ ;
$this -> db -> rollback ();
}
else
{
$this -> db -> commit ();
}
}
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
//Duplicate contact
if ( $clone_contact )
2012-05-06 00:15:03 +02:00
{
2012-04-27 11:03:01 +02:00
$origin_project = new Project ( $this -> db );
$origin_project -> fetch ( $fromid );
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
foreach ( array ( 'internal' , 'external' ) as $source )
{
$tab = $origin_project -> liste_contact ( - 1 , $source );
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
foreach ( $tab as $contacttoadd )
{
2013-10-19 17:51:21 +02:00
$clone_project -> add_contact ( $contacttoadd [ 'id' ], $contacttoadd [ 'code' ], $contacttoadd [ 'source' ], $notrigger );
2012-04-27 11:03:01 +02:00
if ( $clone_project -> error == 'DB_ERROR_RECORD_ALREADY_EXISTS' )
{
$langs -> load ( " errors " );
$this -> error .= $langs -> trans ( " ErrorThisContactIsAlreadyDefinedAsThisType " );
$error ++ ;
}
else
2012-05-06 00:15:03 +02:00
{
2012-04-27 11:03:01 +02:00
if ( $clone_project -> error != '' )
{
$this -> error .= $clone_project -> error ;
$error ++ ;
}
}
}
}
}
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
//Duplicate file
2013-10-19 17:51:21 +02:00
if ( $clone_project_file )
2012-05-06 00:15:03 +02:00
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
$clone_project_dir = $conf -> projet -> dir_output . " / " . dol_sanitizeFileName ( $defaultref );
$ori_project_dir = $conf -> projet -> dir_output . " / " . dol_sanitizeFileName ( $orign_project_ref );
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
if ( dol_mkdir ( $clone_project_dir ) >= 0 )
{
2017-04-12 11:30:33 +02:00
$filearray = dol_dir_list ( $ori_project_dir , " files " , 0 , '' , '(\.meta|_preview.*\.png)$' , '' , SORT_ASC , 1 );
2012-04-27 11:03:01 +02:00
foreach ( $filearray as $key => $file )
{
$rescopy = dol_copy ( $ori_project_dir . '/' . $file [ 'name' ], $clone_project_dir . '/' . $file [ 'name' ], 0 , 1 );
if ( is_numeric ( $rescopy ) && $rescopy < 0 )
{
$this -> error .= $langs -> trans ( " ErrorFailToCopyFile " , $ori_project_dir . '/' . $file [ 'name' ], $clone_project_dir . '/' . $file [ 'name' ]);
$error ++ ;
}
}
}
else
{
$this -> error .= $langs -> trans ( 'ErrorInternalErrorDetected' ) . ':dol_mkdir' ;
$error ++ ;
}
}
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
//Duplicate task
if ( $clone_task )
2012-05-06 00:15:03 +02:00
{
2014-09-08 16:13:29 +02:00
require_once DOL_DOCUMENT_ROOT . '/projet/class/task.class.php' ;
2012-04-27 11:03:01 +02:00
$taskstatic = new Task ( $this -> db );
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
// Security check
$socid = 0 ;
if ( $user -> societe_id > 0 ) $socid = $user -> societe_id ;
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
$tasksarray = $taskstatic -> getTasksArray ( 0 , 0 , $fromid , $socid , 0 );
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
$tab_conv_child_parent = array ();
2016-10-16 18:28:09 +02:00
// Loop on each task, to clone it
2012-04-27 11:03:01 +02:00
foreach ( $tasksarray as $tasktoclone )
{
2014-12-05 21:40:55 +01:00
$result_clone = $taskstatic -> createFromClone ( $tasktoclone -> id , $clone_project_id , $tasktoclone -> fk_parent , $move_date , true , false , $clone_task_file , true , false );
2012-04-27 11:03:01 +02:00
if ( $result_clone <= 0 )
{
$this -> error .= $result_clone -> error ;
$error ++ ;
}
else
{
$new_task_id = $result_clone ;
$taskstatic -> fetch ( $tasktoclone -> id );
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
//manage new parent clone task id
// if the current task has child we store the original task id and the equivalent clone task id
if (( $taskstatic -> hasChildren ()) && ! array_key_exists ( $tasktoclone -> id , $tab_conv_child_parent ))
{
$tab_conv_child_parent [ $tasktoclone -> id ] = $new_task_id ;
}
}
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
}
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
//Parse all clone node to be sure to update new parent
2012-05-06 00:15:03 +02:00
$tasksarray = $taskstatic -> getTasksArray ( 0 , 0 , $clone_project_id , $socid , 0 );
2012-04-27 11:03:01 +02:00
foreach ( $tasksarray as $task_cloned )
{
$taskstatic -> fetch ( $task_cloned -> id );
if ( $taskstatic -> fk_task_parent != 0 )
{
$taskstatic -> fk_task_parent = $tab_conv_child_parent [ $taskstatic -> fk_task_parent ];
}
2013-10-19 17:51:21 +02:00
$res = $taskstatic -> update ( $user , $notrigger );
2012-04-27 11:03:01 +02:00
if ( $result_clone <= 0 )
{
$this -> error .= $taskstatic -> error ;
$error ++ ;
}
}
}
2015-02-26 13:03:17 +01:00
}
2012-05-06 00:15:03 +02:00
2015-02-26 13:03:17 +01:00
unset ( $clone_project -> context [ 'createfromclone' ]);
2012-05-06 00:15:03 +02:00
2015-02-26 13:03:17 +01:00
if ( ! $error )
{
$this -> db -> commit ();
return $clone_project_id ;
2012-04-27 11:03:01 +02:00
}
else
{
$this -> db -> rollback ();
2015-02-26 13:03:17 +01:00
dol_syslog ( get_class ( $this ) . " ::createFromClone nbError: " . $error . " error : " . $this -> error , LOG_ERR );
2012-04-27 11:03:01 +02:00
return - 1 ;
}
}
2012-05-06 00:15:03 +02:00
2012-05-07 17:04:35 +02:00
/**
* Shift project task date from current date to delta
*
* @ param timestamp $old_project_dt_start old project start date
* @ return int 1 if OK or < 0 if KO
*/
2012-04-27 11:03:01 +02:00
function shiftTaskDate ( $old_project_dt_start )
{
global $user , $langs , $conf ;
$error = 0 ;
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
$taskstatic = new Task ( $this -> db );
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
// Security check
$socid = 0 ;
if ( $user -> societe_id > 0 ) $socid = $user -> societe_id ;
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
$tasksarray = $taskstatic -> getTasksArray ( 0 , 0 , $this -> id , $socid , 0 );
foreach ( $tasksarray as $tasktoshiftdate )
{
$to_update = false ;
// Fetch only if update of date will be made
if (( ! empty ( $tasktoshiftdate -> date_start )) || ( ! empty ( $tasktoshiftdate -> date_end )))
{
//dol_syslog(get_class($this)."::shiftTaskDate to_update", LOG_DEBUG);
$to_update = true ;
$task = new Task ( $this -> db );
$result = $task -> fetch ( $tasktoshiftdate -> id );
if ( ! $result )
{
$error ++ ;
$this -> error .= $task -> error ;
}
}
2012-08-31 14:31:08 +02:00
//print "$this->date_start + $tasktoshiftdate->date_start - $old_project_dt_start";exit;
2012-09-15 15:33:00 +02:00
2012-04-27 11:03:01 +02:00
//Calcultate new task start date with difference between old proj start date and origin task start date
if ( ! empty ( $tasktoshiftdate -> date_start ))
2012-05-06 00:15:03 +02:00
{
2012-08-31 14:31:08 +02:00
$task -> date_start = $this -> date_start + ( $tasktoshiftdate -> date_start - $old_project_dt_start );
2012-04-27 11:03:01 +02:00
}
2012-05-06 00:15:03 +02:00
2012-04-27 11:03:01 +02:00
//Calcultate new task end date with difference between origin proj end date and origin task end date
if ( ! empty ( $tasktoshiftdate -> date_end ))
{
2012-08-31 14:31:08 +02:00
$task -> date_end = $this -> date_start + ( $tasktoshiftdate -> date_end - $old_project_dt_start );
2012-04-27 11:03:01 +02:00
}
if ( $to_update )
{
$result = $task -> update ( $user );
if ( ! $result )
{
$error ++ ;
$this -> error .= $task -> error ;
}
}
}
if ( $error != 0 )
{
2012-05-06 00:15:03 +02:00
return - 1 ;
2012-04-27 11:03:01 +02:00
}
return $result ;
2012-05-06 00:15:03 +02:00
}
2013-07-07 13:14:32 +02:00
2013-04-29 12:02:58 +02:00
/**
* Associate element to a project
*
2014-09-20 10:14:26 +02:00
* @ param string $tableName Table of the element to update
* @ param int $elementSelectId Key - rowid of the line of the element to update
2013-07-07 13:14:32 +02:00
* @ return int 1 if OK or < 0 if KO
2013-04-29 12:02:58 +02:00
*/
2014-09-20 10:14:26 +02:00
function update_element ( $tableName , $elementSelectId )
2013-04-29 12:02:58 +02:00
{
2014-09-20 10:14:26 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $tableName ;
2013-07-07 13:14:32 +02:00
2017-01-02 12:34:23 +01:00
if ( $tableName == " actioncomm " )
2013-04-29 12:02:58 +02:00
{
$sql .= " SET fk_project= " . $this -> id ;
2014-09-20 10:14:26 +02:00
$sql .= " WHERE id= " . $elementSelectId ;
2013-04-29 12:02:58 +02:00
}
else
{
$sql .= " SET fk_projet= " . $this -> id ;
2014-09-20 10:14:26 +02:00
$sql .= " WHERE rowid= " . $elementSelectId ;
2013-04-29 12:02:58 +02:00
}
2013-07-07 13:14:32 +02:00
2014-06-13 01:46:27 +02:00
dol_syslog ( get_class ( $this ) . " ::update_element " , LOG_DEBUG );
2013-06-13 18:11:23 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
2013-07-07 13:14:32 +02:00
$this -> error = $this -> db -> lasterror ();
2013-06-13 18:11:23 +02:00
return - 1 ;
} else {
return 1 ;
}
2013-07-07 13:14:32 +02:00
2013-04-29 12:02:58 +02:00
}
2014-09-23 00:02:19 +02:00
2014-09-20 10:14:26 +02:00
/**
* Associate element to a project
*
* @ param string $tableName Table of the element to update
* @ param int $elementSelectId Key - rowid of the line of the element to update
* @ return int 1 if OK or < 0 if KO
*/
function remove_element ( $tableName , $elementSelectId )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . $tableName ;
2014-09-23 00:02:19 +02:00
2014-09-20 10:14:26 +02:00
if ( $TableName == " actioncomm " )
{
$sql .= " SET fk_project=NULL " ;
$sql .= " WHERE id= " . $elementSelectId ;
}
else
{
$sql .= " SET fk_projet=NULL " ;
$sql .= " WHERE rowid= " . $elementSelectId ;
}
2014-09-23 00:02:19 +02:00
2014-09-20 10:14:26 +02:00
dol_syslog ( get_class ( $this ) . " ::remove_element " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) {
$this -> error = $this -> db -> lasterror ();
return - 1 ;
} else {
return 1 ;
}
2013-07-07 13:14:32 +02:00
2013-04-29 12:02:58 +02:00
}
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
/**
* Create an intervention document on disk using template defined into PROJECT_ADDON_PDF
*
2015-08-28 18:20:51 +02:00
* @ param string $modele Force template to use ( '' by default )
* @ param Translate $outputlangs Objet lang to use for translation
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
* @ param int $hidedetails Hide details of lines
* @ param int $hidedesc Hide description
* @ param int $hideref Hide ref
* @ return int 0 if KO , 1 if OK
*/
public function generateDocument ( $modele , $outputlangs , $hidedetails = 0 , $hidedesc = 0 , $hideref = 0 )
{
global $conf , $langs ;
$langs -> load ( " projects " );
2017-01-16 21:16:05 +01:00
if ( ! dol_strlen ( $modele )) {
$modele = 'baleine' ;
if ( $this -> modelpdf ) {
$modele = $this -> modelpdf ;
} elseif ( ! empty ( $conf -> global -> PROJECT_ADDON_PDF )) {
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
$modele = $conf -> global -> PROJECT_ADDON_PDF ;
}
}
2014-09-29 11:08:50 +02:00
$modelpath = " core/modules/project/doc/ " ;
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
2014-09-21 18:16:14 +02:00
return $this -> commonGenerateDocument ( $modelpath , $modele , $outputlangs , $hidedetails , $hidedesc , $hideref );
2014-09-20 10:14:26 +02:00
}
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
2015-03-04 00:53:35 +01:00
/**
2017-11-16 22:55:04 +01:00
* Load time spent into this -> weekWorkLoad and this -> weekWorkLoadPerTask for all day of a week of project .
* Note : array weekWorkLoad and weekWorkLoadPerTask are reset and filled at each call .
2015-03-04 00:53:35 +01:00
*
* @ param int $datestart First day of week ( use dol_get_first_day to find this date )
2015-03-18 11:04:50 +01:00
* @ param int $taskid Filter on a task id
* @ param int $userid Time spent by a particular user
2015-03-04 00:53:35 +01:00
* @ return int < 0 if OK , > 0 if KO
*/
2017-11-16 19:41:31 +01:00
public function loadTimeSpent ( $datestart , $taskid = 0 , $userid = 0 )
2015-03-04 00:53:35 +01:00
{
$error = 0 ;
2017-11-16 22:55:04 +01:00
$this -> weekWorkLoad = array ();
$this -> weekWorkLoadPerTask = array ();
2015-03-18 11:04:50 +01:00
if ( empty ( $datestart )) dol_print_error ( '' , 'Error datestart parameter is empty' );
2017-11-16 22:55:04 +01:00
$sql = " SELECT ptt.rowid as taskid, ptt.task_duration, ptt.task_date, ptt.task_datehour, ptt.fk_task " ;
2015-03-18 11:04:50 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " projet_task_time AS ptt, " . MAIN_DB_PREFIX . " projet_task as pt " ;
$sql .= " WHERE ptt.fk_task = pt.rowid " ;
$sql .= " AND pt.fk_projet = " . $this -> id ;
$sql .= " AND (ptt.task_date >= ' " . $this -> db -> idate ( $datestart ) . " ' " ;
2017-11-16 19:41:31 +01:00
$sql .= " AND ptt.task_date <= ' " . $this -> db -> idate ( dol_time_plus_duree ( $datestart , 1 , 'w' ) - 1 ) . " ') " ;
2015-03-18 11:04:50 +01:00
if ( $task_id ) $sql .= " AND ptt.fk_task= " . $taskid ;
if ( is_numeric ( $userid )) $sql .= " AND ptt.fk_user= " . $userid ;
//print $sql;
2015-03-04 00:53:35 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2017-11-16 19:41:31 +01:00
$daylareadyfound = array ();
2015-03-04 00:53:35 +01:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
// Loop on each record found, so each couple (project id, task id)
2017-11-16 19:41:31 +01:00
while ( $i < $num )
2015-03-04 00:53:35 +01:00
{
$obj = $this -> db -> fetch_object ( $resql );
2017-11-16 22:55:04 +01:00
$day = $this -> db -> jdate ( $obj -> task_date ); // task_date is date without hours
2017-11-16 19:41:31 +01:00
if ( empty ( $daylareadyfound [ $day ]))
{
$this -> weekWorkLoad [ $day ] = $obj -> task_duration ;
$this -> weekWorkLoadPerTask [ $day ][ $obj -> fk_task ] = $obj -> task_duration ;
}
else
{
$this -> weekWorkLoad [ $day ] += $obj -> task_duration ;
$this -> weekWorkLoadPerTask [ $day ][ $obj -> fk_task ] += $obj -> task_duration ;
}
$daylareadyfound [ $day ] = 1 ;
2015-03-04 00:53:35 +01:00
$i ++ ;
}
$this -> db -> free ( $resql );
return 1 ;
}
else
{
$this -> error = " Error " . $this -> db -> lasterror ();
dol_syslog ( get_class ( $this ) . " ::fetch " . $this -> error , LOG_ERR );
return - 1 ;
}
}
2016-01-18 15:56:51 +01:00
/**
* Load indicators for dashboard ( this -> nbtodo and this -> nbtodolate )
*
* @ param User $user Objet user
* @ return WorkboardResponse | int < 0 if KO , WorkboardResponse if OK
*/
function load_board ( $user )
{
global $conf , $langs ;
2016-08-29 10:32:00 +02:00
2018-05-02 19:29:18 +02:00
// For external user, no check is done on company because readability is managed by public status of project and assignement.
//$socid=$user->societe_id;
2016-08-29 10:32:00 +02:00
2018-05-02 19:29:18 +02:00
if ( ! $user -> rights -> projet -> all -> lire ) $projectsListId = $this -> getProjectsAuthorizedForUser ( $user , 0 , 1 , $socid );
2018-06-04 12:55:55 +02:00
2016-01-18 15:56:51 +01:00
$sql = " SELECT p.rowid, p.fk_statut as status, p.fk_opp_status, p.datee as datee " ;
$sql .= " FROM ( " . MAIN_DB_PREFIX . " projet as p " ;
$sql .= " ) " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s on p.fk_soc = s.rowid " ;
2018-05-02 19:29:18 +02:00
// For external user, no check is done on company permission because readability is managed by public status of project and assignement.
//if (! $user->rights->societe->client->voir && ! $socid) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON sc.fk_soc = s.rowid";
2016-01-18 15:56:51 +01:00
$sql .= " WHERE p.fk_statut = 1 " ;
2018-06-27 17:26:04 +02:00
$sql .= " AND p.entity IN ( " . getEntity ( 'project' ) . ')' ;
2018-05-02 19:29:18 +02:00
if ( ! $user -> rights -> projet -> all -> lire ) $sql .= " AND p.rowid IN ( " . $projectsListId . " ) " ;
2016-01-18 15:56:51 +01:00
// No need to check company, as filtering of projects must be done by getProjectsAuthorizedForUser
//if ($socid || ! $user->rights->societe->client->voir) $sql.= " AND (p.fk_soc IS NULL OR p.fk_soc = 0 OR p.fk_soc = ".$socid.")";
2018-05-02 19:29:18 +02:00
// For external user, no check is done on company permission because readability is managed by public status of project and assignement.
//if (! $user->rights->societe->client->voir && ! $socid) $sql.= " AND ((s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id.") OR (s.rowid IS NULL))";
2018-06-04 12:55:55 +02:00
2018-05-02 19:29:18 +02:00
//print $sql;
2016-01-18 15:56:51 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$project_static = new Project ( $this -> db );
2016-08-29 10:32:00 +02:00
2016-01-18 15:56:51 +01:00
$response = new WorkboardResponse ();
$response -> warning_delay = $conf -> projet -> warning_delay / 60 / 60 / 24 ;
$response -> label = $langs -> trans ( " OpenedProjects " );
2016-06-06 15:48:41 +02:00
if ( $user -> rights -> projet -> all -> lire ) $response -> url = DOL_URL_ROOT . '/projet/list.php?search_status=1&mainmenu=project' ;
2017-03-21 13:39:42 +01:00
else $response -> url = DOL_URL_ROOT . '/projet/list.php?search_project_user=-1&search_status=1&mainmenu=project' ;
2017-03-28 10:44:52 +02:00
$response -> img = img_object ( '' , " projectpub " );
2016-08-29 10:32:00 +02:00
2016-01-18 15:56:51 +01:00
// This assignment in condition is not a bug. It allows walking the results.
while ( $obj = $this -> db -> fetch_object ( $resql ))
{
$response -> nbtodo ++ ;
2016-08-29 10:32:00 +02:00
2016-01-18 15:56:51 +01:00
$project_static -> statut = $obj -> status ;
$project_static -> opp_status = $obj -> opp_status ;
$project_static -> datee = $this -> db -> jdate ( $obj -> datee );
2016-08-29 10:32:00 +02:00
2016-01-18 15:56:51 +01:00
if ( $project_static -> hasDelay ()) {
$response -> nbtodolate ++ ;
}
}
2016-08-29 10:32:00 +02:00
2016-01-18 15:56:51 +01:00
return $response ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2016-08-29 10:32:00 +02:00
2015-04-18 18:11:01 +02:00
/**
* Function used to replace a thirdparty id with another one .
*
* @ param DoliDB $db Database handler
* @ param int $origin_id Old thirdparty id
* @ param int $dest_id New thirdparty id
* @ return bool
*/
public static function replaceThirdparty ( DoliDB $db , $origin_id , $dest_id )
{
$tables = array (
'projet'
);
return CommonObject :: commonReplaceThirdparty ( $db , $origin_id , $dest_id , $tables );
}
2016-08-29 10:32:00 +02:00
2016-02-28 00:19:12 +01:00
/**
* Charge indicateurs this -> nb pour le tableau de bord
*
* @ return int < 0 if KO , > 0 if OK
*/
function load_state_board ()
{
2017-03-12 15:31:39 +01:00
global $user ;
2016-08-29 10:32:00 +02:00
2016-02-28 00:19:12 +01:00
$this -> nb = array ();
2016-08-29 10:32:00 +02:00
2017-03-21 19:20:14 +01:00
$sql = " SELECT count(p.rowid) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " projet as p " ;
2016-02-28 00:19:12 +01:00
$sql .= " WHERE " ;
2018-06-27 17:13:50 +02:00
$sql .= " p.entity IN ( " . getEntity ( 'project' ) . " ) " ;
2017-06-07 16:44:04 +02:00
if ( ! $user -> rights -> projet -> all -> lire )
2017-03-21 19:20:14 +01:00
{
2017-03-12 15:31:39 +01:00
$projectsListId = $this -> getProjectsAuthorizedForUser ( $user , 0 , 1 );
$sql .= " AND p.rowid IN ( " . $projectsListId . " ) " ;
}
2016-08-29 10:32:00 +02:00
2016-02-28 00:19:12 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
while ( $obj = $this -> db -> fetch_object ( $resql ))
{
$this -> nb [ " projects " ] = $obj -> nb ;
}
$this -> db -> free ( $resql );
return 1 ;
}
else
{
dol_print_error ( $this -> db );
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2016-08-29 10:32:00 +02:00
2016-01-18 15:56:51 +01:00
/**
2016-06-15 13:37:50 +02:00
* Is the project delayed ?
2016-01-18 15:56:51 +01:00
*
* @ return bool
*/
public function hasDelay ()
{
global $conf ;
2016-08-29 10:32:00 +02:00
2016-06-06 15:48:41 +02:00
if ( ! ( $this -> statut == 1 )) return false ;
2016-11-09 17:46:30 +01:00
if ( ! $this -> datee && ! $this -> date_end ) return false ;
2016-01-18 15:56:51 +01:00
$now = dol_now ();
2016-11-09 17:46:30 +01:00
return ( $this -> datee ? $this -> datee : $this -> date_end ) < ( $now - $conf -> projet -> warning_delay );
2017-06-07 16:44:04 +02:00
}
2016-08-29 10:32:00 +02:00
2015-04-18 18:11:01 +02:00
2016-07-09 15:30:42 +02:00
/**
* Charge les informations d 'ordre info dans l' objet commande
*
* @ param int $id Id of order
* @ return void
*/
function info ( $id )
{
$sql = 'SELECT c.rowid, datec as datec, tms as datem,' ;
$sql .= ' date_close as datecloture,' ;
$sql .= ' fk_user_creat as fk_user_author, fk_user_close as fk_use_cloture' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'projet as c' ;
$sql .= ' WHERE c.rowid = ' . $id ;
$result = $this -> db -> query ( $sql );
if ( $result )
{
if ( $this -> db -> num_rows ( $result ))
{
$obj = $this -> db -> fetch_object ( $result );
$this -> id = $obj -> rowid ;
if ( $obj -> fk_user_author )
{
$cuser = new User ( $this -> db );
$cuser -> fetch ( $obj -> fk_user_author );
$this -> user_creation = $cuser ;
}
2016-08-29 10:32:00 +02:00
2016-07-09 15:30:42 +02:00
if ( $obj -> fk_user_cloture )
{
$cluser = new User ( $this -> db );
$cluser -> fetch ( $obj -> fk_user_cloture );
$this -> user_cloture = $cluser ;
}
2016-08-29 10:32:00 +02:00
2016-07-09 15:30:42 +02:00
$this -> date_creation = $this -> db -> jdate ( $obj -> datec );
$this -> date_modification = $this -> db -> jdate ( $obj -> datem );
$this -> date_cloture = $this -> db -> jdate ( $obj -> datecloture );
}
2016-08-29 10:32:00 +02:00
2016-07-09 15:30:42 +02:00
$this -> db -> free ( $result );
2016-08-29 10:32:00 +02:00
2016-07-09 15:30:42 +02:00
}
else
{
dol_print_error ( $this -> db );
}
}
2016-08-29 10:32:00 +02:00
/**
* Sets object to supplied categories .
*
* Deletes object from existing categories not supplied .
* Adds it to non existing supplied categories .
* Existing categories are left untouch .
*
* @ param int [] | int $categories Category or categories IDs
*/
public function setCategories ( $categories )
{
// Decode type
$type_id = Categorie :: TYPE_PROJECT ;
$type_text = 'project' ;
// Handle single category
if ( ! is_array ( $categories )) {
$categories = array ( $categories );
}
// Get current categories
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
$c = new Categorie ( $this -> db );
$existing = $c -> containing ( $this -> id , $type_id , 'id' );
// Diff
if ( is_array ( $existing )) {
$to_del = array_diff ( $existing , $categories );
$to_add = array_diff ( $categories , $existing );
} else {
$to_del = array (); // Nothing to delete
$to_add = $categories ;
}
// Process
foreach ( $to_del as $del ) {
if ( $c -> fetch ( $del ) > 0 ) {
$result = $c -> del_type ( $this , $type_text );
if ( $result < 0 ) {
$this -> errors = $c -> errors ;
$this -> error = $c -> error ;
return - 1 ;
}
}
}
foreach ( $to_add as $add ) {
if ( $c -> fetch ( $add ) > 0 ) {
$result = $c -> add_type ( $this , $type_text );
if ( $result < 0 ) {
$this -> errors = $c -> errors ;
$this -> error = $c -> error ;
return - 1 ;
}
}
}
return 1 ;
}
2017-06-07 16:44:04 +02:00
2016-11-09 22:54:51 +01:00
/**
* Create an array of tasks of current project
2017-06-07 16:44:04 +02:00
*
2016-11-09 22:54:51 +01:00
* @ param User $user Object user we want project allowed to
* @ return int > 0 if OK , < 0 if KO
*/
function getLinesArray ( $user )
{
require_once DOL_DOCUMENT_ROOT . '/projet/class/task.class.php' ;
$taskstatic = new Task ( $this -> db );
$this -> lines = $taskstatic -> getTasksArray ( 0 , $user , $this -> id , 0 , 0 );
}
2017-06-07 16:44:04 +02:00
2002-05-11 20:53:13 +02:00
}
2011-11-02 22:41:34 +01:00