2006-02-25 00:16:35 +01:00
< ? php
2008-06-24 10:41:28 +02:00
/* Copyright ( C ) 2002 - 2004 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2004 Benoit Mortier < benoit . mortier @ opensides . be >
2013-05-03 00:15:02 +02:00
* Copyright ( C ) 2004 - 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
2018-10-27 14:43:12 +02:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ inodbox . com >
2008-06-24 10:41:28 +02:00
* Copyright ( C ) 2007 Franky Van Liedekerke < franky . van . liedekerker @ telenet . be >
* Copyright ( C ) 2008 Raphael Bertrand ( Resultic ) < raphael . bertrand @ resultic . fr >
2013-11-04 21:41:36 +01:00
* Copyright ( C ) 2013 Florian Henry < florian . henry @ open - concept . pro >
2015-07-15 20:30:33 +02:00
* Copyright ( C ) 2013 Alexandre Spangaro < aspangaro . dolibarr @ gmail . com >
2014-01-30 12:20:36 +01:00
* Copyright ( C ) 2013 Juanjo Menent < jmenent @ 2 byte . es >
2015-04-18 20:50:03 +02:00
* Copyright ( C ) 2015 Marcos GarcÃa < marcosgdf @ gmail . com >
2006-02-25 00:16:35 +01: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
2006-02-25 00:16:35 +01: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:54:11 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2006-02-25 00:16:35 +01:00
*/
/**
2010-06-06 16:30:28 +02:00
* \file htdocs / contact / class / contact . class . php
2009-08-12 02:15:40 +02:00
* \ingroup societe
2010-02-21 14:08:02 +01:00
* \brief File of contacts class
2009-08-11 14:51:33 +02:00
*/
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2007-11-05 23:37:41 +01:00
2006-02-25 00:16:35 +01:00
/**
2012-11-15 14:52:19 +01:00
* Class to manage contact / addresses
2009-08-11 14:51:33 +02:00
*/
2007-11-05 23:37:41 +01:00
class Contact extends CommonObject
2006-02-25 00:16:35 +01:00
{
2018-08-23 18:17:09 +02:00
/**
* @ var string ID to identify managed object
*/
2011-09-20 19:19:46 +02:00
public $element = 'contact' ;
2018-09-02 18:18:10 +02:00
2018-08-22 18:34:50 +02:00
/**
* @ var string Name of table without prefix where object is stored
*/
2011-09-20 19:19:46 +02:00
public $table_element = 'socpeople' ;
2018-09-02 18:18:10 +02:00
2018-10-06 12:11:48 +02:00
/**
* 0 = No test on entity , 1 = Test with field entity , 2 = Test with link by societe
* @ var int
*/
public $ismultientitymanaged = 1 ;
2018-09-05 12:21:13 +02:00
/**
* @ var string String with name of icon for myobject . Must be the part after the 'object_' into object_myobject . png
*/
2017-11-02 15:03:09 +01:00
public $picto = 'contact' ;
2009-01-19 23:41:26 +01:00
2018-01-31 10:51:40 +01:00
// BEGIN MODULEBUILDER PROPERTIES
/**
* @ var array Array with all fields and their property . Do not use it as a static var . It may be modified by constructor .
*/
public $fields = array (
'rowid' => array ( 'type' => 'integer' , 'label' => 'TechnicalID' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' => 1 , 'index' => 1 , 'position' => 1 , 'comment' => 'Id' ),
2018-01-31 14:01:58 +01:00
'lastname' => array ( 'type' => 'varchar(128)' , 'label' => 'Name' , 'enabled' => 1 , 'visible' => 1 , 'notnull' => 1 , 'showoncombobox' => 1 , 'index' => 1 , 'position' => 10 , 'searchall' => 1 ),
'firstname' => array ( 'type' => 'varchar(128)' , 'label' => 'Firstname' , 'enabled' => 1 , 'visible' => 1 , 'notnull' => 1 , 'showoncombobox' => 1 , 'index' => 1 , 'position' => 11 , 'searchall' => 1 ),
2018-01-31 10:51:40 +01:00
'entity' => array ( 'type' => 'integer' , 'label' => 'Entity' , 'enabled' => 1 , 'visible' => 0 , 'default' => 1 , 'notnull' => 1 , 'index' => 1 , 'position' => 20 ),
'note_public' => array ( 'type' => 'text' , 'label' => 'NotePublic' , 'enabled' => 1 , 'visible' => 0 , 'position' => 60 ),
'note_private' => array ( 'type' => 'text' , 'label' => 'NotePrivate' , 'enabled' => 1 , 'visible' => 0 , 'position' => 61 ),
'datec' => array ( 'type' => 'datetime' , 'label' => 'DateCreation' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' => 1 , 'position' => 500 ),
'tms' => array ( 'type' => 'timestamp' , 'label' => 'DateModification' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' => 1 , 'position' => 501 ),
//'date_valid' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-2, 'position'=>502),
'fk_user_creat' => array ( 'type' => 'integer' , 'label' => 'UserAuthor' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' => 1 , 'position' => 510 ),
'fk_user_modif' => array ( 'type' => 'integer' , 'label' => 'UserModif' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' =>- 1 , 'position' => 511 ),
//'fk_user_valid' =>array('type'=>'integer', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'position'=>512),
'import_key' => array ( 'type' => 'varchar(14)' , 'label' => 'ImportId' , 'enabled' => 1 , 'visible' =>- 2 , 'notnull' =>- 1 , 'index' => 1 , 'position' => 1000 ),
);
2017-02-08 21:13:54 +01:00
public $civility_id ; // In fact we store civility_code
public $civility_code ;
public $address ;
public $zip ;
public $town ;
2010-06-06 16:30:28 +02:00
2015-04-23 23:21:06 +02:00
/**
* @ deprecated
2018-11-12 15:27:59 +01:00
* @ see $state_id
2015-04-23 23:21:06 +02:00
*/
2017-02-08 21:13:54 +01:00
public $fk_departement ;
2015-04-23 23:21:06 +02:00
/**
* @ deprecated
2018-11-12 15:27:59 +01:00
* @ see $state_code
2015-04-23 23:21:06 +02:00
*/
2017-02-08 21:13:54 +01:00
public $departement_code ;
2015-04-23 23:21:06 +02:00
/**
* @ deprecated
2018-11-12 15:27:59 +01:00
* @ see $state
2015-04-23 23:21:06 +02:00
*/
2017-02-08 21:13:54 +01:00
public $departement ;
public $state_id ; // Id of department
public $state_code ; // Code of department
public $state ; // Label of department
2010-06-06 16:30:28 +02:00
2018-09-16 19:19:45 +02:00
public $poste ; // Position
2013-11-27 15:19:24 +01:00
2017-02-08 21:13:54 +01:00
public $socid ; // fk_soc
public $statut ; // 0=inactif, 1=actif
2009-08-11 14:51:33 +02:00
2017-02-08 21:13:54 +01:00
public $code ;
public $email ;
public $skype ;
public $photo ;
2018-01-11 20:39:23 +01:00
public $jabberid ;
2017-02-08 21:13:54 +01:00
public $phone_pro ;
public $phone_perso ;
public $phone_mobile ;
2018-01-11 20:39:23 +01:00
public $fax ;
2013-11-14 17:16:27 +01:00
2018-01-11 20:39:23 +01:00
public $priv ;
2013-10-27 02:33:03 +02:00
2017-02-08 21:13:54 +01:00
public $birthday ;
public $default_lang ;
2009-08-11 14:51:33 +02:00
2017-02-08 21:13:54 +01:00
public $ref_facturation ; // Reference number of invoice for which it is contact
public $ref_contrat ; // Nb de reference contrat pour lequel il est contact
public $ref_commande ; // Nb de reference commande pour lequel il est contact
public $ref_propal ; // Nb de reference propal pour lequel il est contact
2006-02-25 00:16:35 +01:00
2017-02-08 21:13:54 +01:00
public $user_id ;
public $user_login ;
2007-04-27 00:01:10 +02:00
2018-01-31 10:51:40 +01:00
// END MODULEBUILDER PROPERTIES
2017-02-08 21:13:54 +01:00
public $oldcopy ; // To contains a clone of this when we need to save old properties of object
2009-08-12 01:42:21 +02:00
2007-04-27 00:01:10 +02:00
2009-08-11 14:51:33 +02:00
/**
2011-09-11 20:35:38 +02:00
* Constructor
*
2011-12-30 14:18:19 +01:00
* @ param DoliDB $db Database handler
2009-08-11 14:51:33 +02:00
*/
2012-07-30 17:17:33 +02:00
function __construct ( $db )
2009-08-11 14:51:33 +02:00
{
2011-12-30 14:18:19 +01:00
$this -> db = $db ;
2014-01-30 18:17:52 +01:00
$this -> statut = 1 ; // By default, status is enabled
2009-08-11 14:51:33 +02:00
}
2017-07-13 23:52:02 +02:00
2018-09-02 18:18:10 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2016-04-23 13:06:46 +02:00
/**
* Load indicators into this -> nb for board
*
* @ return int < 0 if KO , > 0 if OK
*/
function load_state_board ()
{
2018-09-02 18:18:10 +02:00
// phpcs:enable
2016-04-23 13:06:46 +02:00
global $user ;
2017-07-13 23:52:02 +02:00
2016-04-23 13:06:46 +02:00
$this -> nb = array ();
$clause = " WHERE " ;
2017-07-13 23:52:02 +02:00
2016-04-23 13:06:46 +02:00
$sql = " SELECT count(sp.rowid) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " socpeople as sp " ;
if ( ! $user -> rights -> societe -> client -> voir && ! $user -> societe_id )
{
2017-02-22 14:09:19 +01:00
$sql .= " , " . MAIN_DB_PREFIX . " societe as s " ;
$sql .= " , " . MAIN_DB_PREFIX . " societe_commerciaux as sc " ;
$sql .= " WHERE sp.fk_soc = s.rowid AND s.rowid = sc.fk_soc AND sc.fk_user = " . $user -> id ;
2016-04-23 13:06:46 +02:00
$clause = " AND " ;
}
2018-01-30 15:48:09 +01:00
$sql .= ' ' . $clause . ' sp.entity IN (' . getEntity ( $this -> element ) . ')' ;
2017-01-10 00:42:28 +01:00
$sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat= " . $user -> id . " )) " ;
2016-11-06 17:20:21 +01:00
if ( $user -> societe_id > 0 ) $sql .= " AND sp.fk_soc = " . $user -> societe_id ;
2017-07-13 23:52:02 +02:00
2016-04-23 13:06:46 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
while ( $obj = $this -> db -> fetch_object ( $resql ))
{
$this -> nb [ " contacts " ] = $obj -> nb ;
}
$this -> db -> free ( $resql );
return 1 ;
}
else
{
dol_print_error ( $this -> db );
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
2009-08-11 14:51:33 +02:00
/**
2011-09-11 20:35:38 +02:00
* Add a contact into database
*
* @ param User $user Object user that create
* @ return int < 0 if KO , > 0 if OK
2009-08-11 14:51:33 +02:00
*/
function create ( $user )
{
global $conf , $langs ;
2009-01-19 23:41:26 +01:00
2010-11-19 22:24:48 +01:00
$error = 0 ;
2010-06-06 16:30:28 +02:00
$now = dol_now ();
2010-11-19 22:24:48 +01:00
$this -> db -> begin ();
2008-04-10 01:07:13 +02:00
// Clean parameters
2013-06-19 18:08:05 +02:00
$this -> lastname = $this -> lastname ? trim ( $this -> lastname ) : trim ( $this -> name );
2011-05-02 20:21:57 +02:00
$this -> firstname = trim ( $this -> firstname );
2011-12-30 14:18:19 +01:00
if ( ! empty ( $conf -> global -> MAIN_FIRST_TO_UPPER )) $this -> lastname = ucwords ( $this -> lastname );
2011-05-02 20:21:57 +02:00
if ( ! empty ( $conf -> global -> MAIN_FIRST_TO_UPPER )) $this -> firstname = ucwords ( $this -> firstname );
2014-01-30 18:17:52 +01:00
if ( empty ( $this -> socid )) $this -> socid = 0 ;
if ( empty ( $this -> priv )) $this -> priv = 0 ;
if ( empty ( $this -> statut )) $this -> statut = 0 ; // This is to convert '' into '0' to avoid bad sql request
2006-02-25 00:16:35 +01:00
2018-07-08 12:11:22 +02:00
$this -> entity = (( isset ( $this -> entity ) && is_numeric ( $this -> entity )) ? $this -> entity : $conf -> entity );
2018-01-11 20:39:23 +01:00
2011-04-17 20:14:29 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " socpeople ( " ;
$sql .= " datec " ;
$sql .= " , fk_soc " ;
2013-02-23 15:58:26 +01:00
$sql .= " , lastname " ;
2011-05-02 20:21:57 +02:00
$sql .= " , firstname " ;
$sql .= " , fk_user_creat " ;
2011-04-17 20:14:29 +02:00
$sql .= " , priv " ;
2013-10-30 12:24:51 +01:00
$sql .= " , statut " ;
2011-04-17 20:14:29 +02:00
$sql .= " , canvas " ;
$sql .= " , entity " ;
2018-01-10 12:38:12 +01:00
$sql .= " , ref_ext " ;
2012-11-30 04:40:06 +01:00
$sql .= " , import_key " ;
2011-04-17 20:14:29 +02:00
$sql .= " ) VALUES ( " ;
$sql .= " ' " . $this -> db -> idate ( $now ) . " ', " ;
2018-04-06 20:18:36 +02:00
if ( $this -> socid > 0 ) $sql .= " " . $this -> db -> escape ( $this -> socid ) . " , " ;
2009-08-11 14:51:33 +02:00
else $sql .= " null, " ;
2012-05-12 12:32:24 +02:00
$sql .= " ' " . $this -> db -> escape ( $this -> lastname ) . " ', " ;
2011-05-02 20:21:57 +02:00
$sql .= " ' " . $this -> db -> escape ( $this -> firstname ) . " ', " ;
2018-04-06 20:18:36 +02:00
$sql .= " " . ( $user -> id > 0 ? " ' " . $this -> db -> escape ( $user -> id ) . " ' " : " null " ) . " , " ;
$sql .= " " . $this -> db -> escape ( $this -> priv ) . " , " ;
$sql .= " " . $this -> db -> escape ( $this -> statut ) . " , " ;
2015-09-12 11:23:44 +02:00
$sql .= " " . ( ! empty ( $this -> canvas ) ? " ' " . $this -> db -> escape ( $this -> canvas ) . " ' " : " null " ) . " , " ;
2018-07-08 12:11:22 +02:00
$sql .= " " . $this -> db -> escape ( $this -> entity ) . " , " ;
2015-09-12 11:23:44 +02:00
$sql .= " ' " . $this -> db -> escape ( $this -> ref_ext ) . " ', " ;
2017-09-15 15:41:07 +02:00
$sql .= " " . ( ! empty ( $this -> import_key ) ? " ' " . $this -> db -> escape ( $this -> import_key ) . " ' " : " null " );
2009-08-11 14:51:33 +02:00
$sql .= " ) " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2009-08-11 14:51:33 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " socpeople " );
2010-11-19 22:24:48 +01:00
if ( ! $error )
2009-08-11 14:51:33 +02:00
{
2012-11-10 16:08:26 +01:00
$result = $this -> update ( $this -> id , $user , 1 , 'add' );
2010-11-19 22:24:48 +01:00
if ( $result < 0 )
{
$error ++ ;
$this -> error = $this -> db -> lasterror ();
}
2009-08-11 14:51:33 +02:00
}
2016-08-02 17:55:13 +02:00
if ( ! $error )
2010-11-19 22:24:48 +01:00
{
2016-08-02 17:55:13 +02:00
$result = $this -> update_perso ( $this -> id , $user , 1 ); // TODO Remove function update_perso, should be same than update
2010-11-19 22:24:48 +01:00
if ( $result < 0 )
{
$error ++ ;
$this -> error = $this -> db -> lasterror ();
}
}
2016-08-02 17:55:13 +02:00
if ( ! $error )
2010-11-19 22:24:48 +01:00
{
2014-07-04 17:10:19 +02:00
// Call trigger
$result = $this -> call_trigger ( 'CONTACT_CREATE' , $user );
2014-10-04 17:20:17 +02:00
if ( $result < 0 ) { $error ++ ; }
2014-07-04 17:10:19 +02:00
// End call triggers
2010-11-19 22:24:48 +01:00
}
if ( ! $error )
{
$this -> db -> commit ();
return $this -> id ;
}
else
{
$this -> db -> rollback ();
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::create " . $this -> error , LOG_ERR );
2010-11-19 22:24:48 +01:00
return - 2 ;
}
2009-08-11 14:51:33 +02:00
}
else
{
2010-11-19 22:24:48 +01:00
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::create " . $this -> error , LOG_ERR );
2009-08-11 14:51:33 +02:00
return - 1 ;
}
}
2010-02-21 14:08:02 +01:00
/**
2010-11-19 22:24:48 +01:00
* Update informations into database
2011-09-16 19:57:09 +02:00
*
* @ param int $id Id of contact / address to update
* @ param User $user Objet user making change
2012-11-10 16:08:26 +01:00
* @ param int $notrigger 0 = no , 1 = yes
* @ param string $action Current action for hookmanager
2017-12-19 16:16:31 +01:00
* @ param int $nosyncuser No sync linked user ( external users and contacts are linked )
2011-09-16 19:57:09 +02:00
* @ return int < 0 if KO , > 0 if OK
2009-08-11 14:51:33 +02:00
*/
2017-12-19 16:16:31 +01:00
function update ( $id , $user = null , $notrigger = 0 , $action = 'update' , $nosyncuser = 0 )
2009-08-11 14:51:33 +02:00
{
2013-01-25 19:12:54 +01:00
global $conf , $langs , $hookmanager ;
2009-08-11 14:51:33 +02:00
2009-08-11 22:16:58 +02:00
$error = 0 ;
2009-08-11 14:51:33 +02:00
$this -> id = $id ;
2018-07-08 12:11:22 +02:00
$this -> entity = (( isset ( $this -> entity ) && is_numeric ( $this -> entity )) ? $this -> entity : $conf -> entity );
2011-12-30 14:18:19 +01:00
// Clean parameters
2013-02-23 15:58:26 +01:00
$this -> lastname = trim ( $this -> lastname ) ? trim ( $this -> lastname ) : trim ( $this -> lastname );
2009-08-11 14:51:33 +02:00
$this -> firstname = trim ( $this -> firstname );
$this -> email = trim ( $this -> email );
$this -> phone_pro = trim ( $this -> phone_pro );
$this -> phone_perso = trim ( $this -> phone_perso );
$this -> phone_mobile = trim ( $this -> phone_mobile );
2013-05-03 00:15:02 +02:00
$this -> jabberid = trim ( $this -> jabberid );
2014-01-30 18:17:52 +01:00
$this -> skype = trim ( $this -> skype );
2015-10-04 17:25:46 +02:00
$this -> photo = trim ( $this -> photo );
2009-08-11 14:51:33 +02:00
$this -> fax = trim ( $this -> fax );
2013-05-03 00:15:02 +02:00
$this -> zip = ( empty ( $this -> zip ) ? '' : $this -> zip );
$this -> town = ( empty ( $this -> town ) ? '' : $this -> town );
2013-02-25 17:56:41 +01:00
$this -> country_id = ( $this -> country_id > 0 ? $this -> country_id : $this -> country_id );
2011-12-30 14:18:19 +01:00
$this -> state_id = ( $this -> state_id > 0 ? $this -> state_id : $this -> fk_departement );
2013-10-30 12:24:51 +01:00
if ( empty ( $this -> statut )) $this -> statut = 0 ;
2009-08-11 14:51:33 +02:00
2009-08-11 22:16:58 +02:00
$this -> db -> begin ();
2009-08-11 14:51:33 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " socpeople SET " ;
2011-02-24 19:11:12 +01:00
if ( $this -> socid > 0 ) $sql .= " fk_soc=' " . $this -> db -> escape ( $this -> socid ) . " ', " ;
2011-12-31 11:30:49 +01:00
else if ( $this -> socid == - 1 ) $sql .= " fk_soc=null, " ;
2014-08-12 06:03:27 +02:00
$sql .= " civility=' " . $this -> db -> escape ( $this -> civility_id ) . " ' " ;
2013-02-23 15:58:26 +01:00
$sql .= " , lastname=' " . $this -> db -> escape ( $this -> lastname ) . " ' " ;
2011-02-24 19:11:12 +01:00
$sql .= " , firstname=' " . $this -> db -> escape ( $this -> firstname ) . " ' " ;
$sql .= " , address=' " . $this -> db -> escape ( $this -> address ) . " ' " ;
2013-02-24 17:08:52 +01:00
$sql .= " , zip=' " . $this -> db -> escape ( $this -> zip ) . " ' " ;
2013-02-22 16:53:50 +01:00
$sql .= " , town=' " . $this -> db -> escape ( $this -> town ) . " ' " ;
2011-12-29 18:07:41 +01:00
$sql .= " , fk_pays= " . ( $this -> country_id > 0 ? $this -> country_id : 'NULL' );
2011-12-30 14:18:19 +01:00
$sql .= " , fk_departement= " . ( $this -> state_id > 0 ? $this -> state_id : 'NULL' );
2011-02-24 19:11:12 +01:00
$sql .= " , poste=' " . $this -> db -> escape ( $this -> poste ) . " ' " ;
$sql .= " , fax=' " . $this -> db -> escape ( $this -> fax ) . " ' " ;
$sql .= " , email=' " . $this -> db -> escape ( $this -> email ) . " ' " ;
2014-01-30 18:17:52 +01:00
$sql .= " , skype=' " . $this -> db -> escape ( $this -> skype ) . " ' " ;
2018-09-26 16:55:46 +02:00
$sql .= " , twitter=' " . $this -> db -> escape ( $this -> twitter ) . " ' " ;
$sql .= " , facebook=' " . $this -> db -> escape ( $this -> facebook ) . " ' " ;
2015-10-04 17:25:46 +02:00
$sql .= " , photo=' " . $this -> db -> escape ( $this -> photo ) . " ' " ;
2018-04-11 14:43:16 +02:00
$sql .= " , birthday= " . ( $this -> birthday ? " ' " . $this -> db -> idate ( $this -> birthday ) . " ' " : " null " );
2013-05-07 13:08:30 +02:00
$sql .= " , note_private = " . ( isset ( $this -> note_private ) ? " ' " . $this -> db -> escape ( $this -> note_private ) . " ' " : " null " );
$sql .= " , note_public = " . ( isset ( $this -> note_public ) ? " ' " . $this -> db -> escape ( $this -> note_public ) . " ' " : " null " );
2013-05-06 17:57:33 +02:00
$sql .= " , phone = " . ( isset ( $this -> phone_pro ) ? " ' " . $this -> db -> escape ( $this -> phone_pro ) . " ' " : " null " );
$sql .= " , phone_perso = " . ( isset ( $this -> phone_perso ) ? " ' " . $this -> db -> escape ( $this -> phone_perso ) . " ' " : " null " );
$sql .= " , phone_mobile = " . ( isset ( $this -> phone_mobile ) ? " ' " . $this -> db -> escape ( $this -> phone_mobile ) . " ' " : " null " );
$sql .= " , jabberid = " . ( isset ( $this -> jabberid ) ? " ' " . $this -> db -> escape ( $this -> jabberid ) . " ' " : " null " );
2017-05-12 16:55:11 +02:00
$sql .= " , priv = ' " . $this -> db -> escape ( $this -> priv ) . " ' " ;
2018-04-06 20:18:36 +02:00
$sql .= " , statut = " . $this -> db -> escape ( $this -> statut );
2017-09-15 15:41:07 +02:00
$sql .= " , fk_user_modif= " . ( $user -> id > 0 ? " ' " . $this -> db -> escape ( $user -> id ) . " ' " : " NULL " );
$sql .= " , default_lang= " . ( $this -> default_lang ? " ' " . $this -> db -> escape ( $this -> default_lang ) . " ' " : " NULL " );
2018-07-08 12:11:22 +02:00
$sql .= " , entity = " . $this -> db -> escape ( $this -> entity );
2014-05-06 00:06:54 +02:00
$sql .= " WHERE rowid= " . $this -> db -> escape ( $id );
2009-08-11 14:51:33 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
2009-08-11 14:51:33 +02:00
$result = $this -> db -> query ( $sql );
2009-08-11 22:16:58 +02:00
if ( $result )
{
2011-12-30 14:18:19 +01:00
unset ( $this -> country_code );
unset ( $this -> country );
unset ( $this -> state_code );
unset ( $this -> state );
2014-11-15 19:04:21 +01:00
$action = 'update' ;
2018-04-10 12:03:01 +02:00
// Actions on extra fields
2018-08-12 16:29:26 +02:00
if ( ! $error && empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED ))
2012-09-17 01:39:30 +02:00
{
2018-08-12 16:29:26 +02:00
$result = $this -> insertExtraFields ();
if ( $result < 0 )
2012-09-19 17:53:09 +02:00
{
2018-08-12 16:29:26 +02:00
$error ++ ;
2012-09-19 17:53:09 +02:00
}
}
2012-09-17 01:39:30 +02:00
2017-12-19 16:16:31 +01:00
if ( ! $error && $this -> user_id > 0 )
{
$tmpobj = new User ( $this -> db );
$tmpobj -> fetch ( $this -> user_id );
$usermustbemodified = 0 ;
if ( $tmpobj -> office_phone != $this -> phone_pro )
{
$tmpobj -> office_phone = $this -> phone_pro ;
$usermustbemodified ++ ;
}
if ( $tmpobj -> office_fax != $this -> fax )
{
$tmpobj -> office_fax = $this -> fax ;
$usermustbemodified ++ ;
}
if ( $tmpobj -> address != $this -> address )
{
$tmpobj -> address = $this -> address ;
$usermustbemodified ++ ;
}
if ( $tmpobj -> town != $this -> town )
{
$tmpobj -> town = $this -> town ;
$usermustbemodified ++ ;
}
if ( $tmpobj -> zip != $this -> zip )
{
$tmpobj -> zip = $this -> zip ;
$usermustbemodified ++ ;
}
if ( $tmpobj -> zip != $this -> zip )
{
$tmpobj -> state_id = $this -> state_id ;
$usermustbemodified ++ ;
}
if ( $tmpobj -> country_id != $this -> country_id )
{
$tmpobj -> country_id = $this -> country_id ;
$usermustbemodified ++ ;
}
if ( $tmpobj -> email != $this -> email )
{
$tmpobj -> email = $this -> email ;
$usermustbemodified ++ ;
}
if ( $tmpobj -> skype != $this -> skype )
{
$tmpobj -> skype = $this -> skype ;
$usermustbemodified ++ ;
}
2018-09-26 16:55:46 +02:00
if ( $tmpobj -> twitter != $this -> twitter )
{
$tmpobj -> twitter = $this -> twitter ;
$usermustbemodified ++ ;
}
if ( $tmpobj -> facebook != $this -> facebook )
{
$tmpobj -> facebook = $this -> facebook ;
$usermustbemodified ++ ;
}
2017-12-19 16:16:31 +01:00
if ( $usermustbemodified )
{
$result = $tmpobj -> update ( $user , 0 , 1 , 1 , 1 );
if ( $result < 0 ) { $error ++ ; }
}
}
2009-08-11 22:16:58 +02:00
if ( ! $error && ! $notrigger )
{
2017-12-19 16:16:31 +01:00
// Call trigger
$result = $this -> call_trigger ( 'CONTACT_MODIFY' , $user );
if ( $result < 0 ) { $error ++ ; }
// End call triggers
2009-08-11 22:16:58 +02:00
}
if ( ! $error )
{
$this -> db -> commit ();
return 1 ;
}
else
{
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::update Error " . $this -> error , LOG_ERR );
2009-08-11 22:16:58 +02:00
$this -> db -> rollback ();
return - $error ;
}
}
else
2009-08-11 14:51:33 +02:00
{
$this -> error = $this -> db -> lasterror () . ' sql=' . $sql ;
2010-11-19 22:24:48 +01:00
$this -> db -> rollback ();
2009-08-11 14:51:33 +02:00
return - 1 ;
}
}
2009-01-19 23:41:26 +01:00
2018-09-02 18:18:10 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2009-08-11 14:51:33 +02:00
/**
2011-09-16 19:57:09 +02:00
* Retourne chaine DN complete dans l 'annuaire LDAP pour l' objet
*
* @ param array $info Info string loaded by _load_ldap_info
* @ param int $mode 0 = Return full DN ( uid = qqq , ou = xxx , dc = aaa , dc = bbb )
* 1 = Return DN without key inside ( ou = xxx , dc = aaa , dc = bbb )
* 2 = Return key only ( uid = qqq )
* @ return string DN
2009-08-11 14:51:33 +02:00
*/
2006-11-21 23:51:05 +01:00
function _load_ldap_dn ( $info , $mode = 0 )
2006-11-19 02:46:32 +01:00
{
2018-09-02 18:18:10 +02:00
// phpcs:enable
2006-11-21 23:51:05 +01:00
global $conf ;
$dn = '' ;
if ( $mode == 0 ) $dn = $conf -> global -> LDAP_KEY_CONTACTS . " = " . $info [ $conf -> global -> LDAP_KEY_CONTACTS ] . " , " . $conf -> global -> LDAP_CONTACT_DN ;
2018-09-16 19:19:45 +02:00
elseif ( $mode == 1 ) $dn = $conf -> global -> LDAP_CONTACT_DN ;
elseif ( $mode == 2 ) $dn = $conf -> global -> LDAP_KEY_CONTACTS . " = " . $info [ $conf -> global -> LDAP_KEY_CONTACTS ];
2006-11-21 23:51:05 +01:00
return $dn ;
}
2006-11-19 02:46:32 +01:00
2006-11-20 02:13:13 +01:00
2018-09-02 18:18:10 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2009-08-11 14:51:33 +02:00
/**
2011-09-16 19:57:09 +02:00
* Initialise tableau info ( tableau des attributs LDAP )
*
* @ return array Tableau info des attributs
2009-08-11 14:51:33 +02:00
*/
2006-11-21 23:51:05 +01:00
function _load_ldap_info ()
2006-11-20 02:13:13 +01:00
{
2018-09-02 18:18:10 +02:00
// phpcs:enable
2018-09-16 19:19:45 +02:00
global $conf , $langs ;
2006-11-20 02:13:13 +01:00
2017-07-13 23:52:02 +02:00
$info = array ();
// Object classes
2018-09-16 19:19:45 +02:00
$info [ " objectclass " ] = explode ( ',' , $conf -> global -> LDAP_CONTACT_OBJECT_CLASS );
2009-01-19 23:41:26 +01:00
2010-07-18 12:39:07 +02:00
$this -> fullname = $this -> getFullName ( $langs );
// Fields
2012-09-15 11:21:22 +02:00
if ( $this -> fullname && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_FULLNAME )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_FULLNAME ] = $this -> fullname ;
if ( $this -> lastname && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_NAME )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_NAME ] = $this -> lastname ;
if ( $this -> firstname && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_FIRSTNAME )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_FIRSTNAME ] = $this -> firstname ;
2006-11-21 23:51:05 +01:00
2006-11-20 02:13:13 +01:00
if ( $this -> poste ) $info [ " title " ] = $this -> poste ;
if ( $this -> socid > 0 )
{
$soc = new Societe ( $this -> db );
$soc -> fetch ( $this -> socid );
2014-10-04 17:20:17 +02:00
$info [ $conf -> global -> LDAP_CONTACT_FIELD_COMPANY ] = $soc -> name ;
2006-11-20 02:13:13 +01:00
if ( $soc -> client == 1 ) $info [ " businessCategory " ] = " Customers " ;
if ( $soc -> client == 2 ) $info [ " businessCategory " ] = " Prospects " ;
if ( $soc -> fournisseur == 1 ) $info [ " businessCategory " ] = " Suppliers " ;
}
2012-09-15 11:21:22 +02:00
if ( $this -> address && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_ADDRESS )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_ADDRESS ] = $this -> address ;
2013-02-24 17:08:52 +01:00
if ( $this -> zip && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_ZIP )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_ZIP ] = $this -> zip ;
2013-02-22 16:53:50 +01:00
if ( $this -> town && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_TOWN )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_TOWN ] = $this -> town ;
2012-09-15 11:21:22 +02:00
if ( $this -> country_code && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_COUNTRY )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_COUNTRY ] = $this -> country_code ;
if ( $this -> phone_pro && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_PHONE )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_PHONE ] = $this -> phone_pro ;
if ( $this -> phone_perso && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_HOMEPHONE )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_HOMEPHONE ] = $this -> phone_perso ;
if ( $this -> phone_mobile && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_MOBILE )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_MOBILE ] = $this -> phone_mobile ;
if ( $this -> fax && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_FAX )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_FAX ] = $this -> fax ;
2015-10-04 17:25:46 +02:00
if ( $this -> skype && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_SKYPE )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_SKYPE ] = $this -> skype ;
2018-04-06 12:27:55 +02:00
if ( $this -> note_private && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_DESCRIPTION )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_DESCRIPTION ] = dol_string_nohtmltag ( $this -> note_private , 2 );
2012-09-15 11:21:22 +02:00
if ( $this -> email && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_MAIL )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_MAIL ] = $this -> email ;
2006-11-20 02:13:13 +01:00
if ( $conf -> global -> LDAP_SERVER_TYPE == 'egroupware' )
{
$info [ " objectclass " ][ 4 ] = " phpgwContact " ; // compatibilite egroupware
$info [ 'uidnumber' ] = $this -> id ;
$info [ 'phpgwTz' ] = 0 ;
$info [ 'phpgwMailType' ] = 'INTERNET' ;
$info [ 'phpgwMailHomeType' ] = 'INTERNET' ;
$info [ " phpgwContactTypeId " ] = 'n' ;
$info [ " phpgwContactCatId " ] = 0 ;
$info [ " phpgwContactAccess " ] = " public " ;
2010-08-24 16:42:18 +02:00
if ( dol_strlen ( $this -> egroupware_id ) == 0 )
2006-11-20 02:13:13 +01:00
{
$this -> egroupware_id = 1 ;
}
$info [ " phpgwContactOwner " ] = $this -> egroupware_id ;
if ( $this -> email ) $info [ " rfc822Mailbox " ] = $this -> email ;
if ( $this -> phone_mobile ) $info [ " phpgwCellTelephoneNumber " ] = $this -> phone_mobile ;
}
2009-01-19 23:41:26 +01:00
2006-11-20 02:13:13 +01:00
return $info ;
}
2009-01-19 23:41:26 +01:00
2018-09-02 18:18:10 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2010-11-19 22:24:48 +01:00
/**
2011-09-16 19:57:09 +02:00
* Update field alert birthday
*
* @ param int $id Id of contact
* @ param User $user User asking to change alert or birthday
2016-08-02 16:55:32 +02:00
* @ param int $notrigger 0 = no , 1 = yes
2011-09-16 19:57:09 +02:00
* @ return int < 0 if KO , >= 0 if OK
2009-08-11 14:51:33 +02:00
*/
2016-08-02 16:55:32 +02:00
function update_perso ( $id , $user = null , $notrigger = 0 )
2006-11-18 22:59:10 +01:00
{
2018-09-02 18:18:10 +02:00
// phpcs:enable
2010-11-19 22:24:48 +01:00
$error = 0 ;
$result = false ;
2016-08-02 16:55:32 +02:00
$this -> db -> begin ();
2017-07-13 23:52:02 +02:00
2006-11-21 23:51:05 +01:00
// Mis a jour contact
2012-02-11 08:03:26 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " socpeople SET " ;
$sql .= " birthday= " . ( $this -> birthday ? " ' " . $this -> db -> idate ( $this -> birthday ) . " ' " : " null " );
2017-09-15 15:41:07 +02:00
$sql .= " , photo = " . ( $this -> photo ? " ' " . $this -> db -> escape ( $this -> photo ) . " ' " : " null " );
2009-08-11 14:51:33 +02:00
if ( $user ) $sql .= " , fk_user_modif= " . $user -> id ;
2014-05-06 00:06:54 +02:00
$sql .= " WHERE rowid= " . $this -> db -> escape ( $id );
2012-02-11 08:03:26 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::update_perso this->birthday= " . $this -> birthday . " - " , LOG_DEBUG );
2007-01-16 23:02:36 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
2006-11-21 23:51:05 +01:00
{
2018-04-11 14:38:56 +02:00
$error ++ ;
$this -> error = $this -> db -> lasterror ();
2006-11-21 23:51:05 +01:00
}
2009-01-19 23:41:26 +01:00
2006-11-21 23:51:05 +01:00
// Mis a jour alerte birthday
if ( $this -> birthday_alert )
{
2007-01-14 15:50:35 +01:00
//check existing
2014-05-06 00:06:54 +02:00
$sql_check = " SELECT * FROM " . MAIN_DB_PREFIX . " user_alert WHERE type=1 AND fk_contact= " . $this -> db -> escape ( $id ) . " AND fk_user= " . $user -> id ;
2007-01-14 15:50:35 +01:00
$result_check = $this -> db -> query ( $sql_check );
2011-12-31 11:30:49 +01:00
if ( ! $result_check || ( $this -> db -> num_rows ( $result_check ) < 1 ))
2009-08-11 14:51:33 +02:00
{
2007-01-14 15:50:35 +01:00
//insert
2011-12-31 11:30:49 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " user_alert(type,fk_contact,fk_user) " ;
2014-05-06 00:06:54 +02:00
$sql .= " VALUES (1, " . $this -> db -> escape ( $id ) . " , " . $user -> id . " ) " ;
2007-01-14 15:50:35 +01:00
$result = $this -> db -> query ( $sql );
2011-12-31 11:30:49 +01:00
if ( ! $result )
2007-01-14 15:50:35 +01:00
{
2010-11-19 22:24:48 +01:00
$error ++ ;
$this -> error = $this -> db -> lasterror ();
2007-01-14 15:50:35 +01:00
}
2009-08-11 14:51:33 +02:00
}
else
{
$result = true ;
}
2006-11-21 23:51:05 +01:00
}
2006-11-18 22:59:10 +01:00
else
{
2011-12-31 11:30:49 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " user_alert " ;
2014-05-06 00:06:54 +02:00
$sql .= " WHERE type=1 AND fk_contact= " . $this -> db -> escape ( $id ) . " AND fk_user= " . $user -> id ;
2007-01-14 15:50:35 +01:00
$result = $this -> db -> query ( $sql );
2010-11-19 22:24:48 +01:00
if ( ! $result )
2007-01-14 15:50:35 +01:00
{
2010-11-19 22:24:48 +01:00
$error ++ ;
$this -> error = $this -> db -> lasterror ();
2007-01-14 15:50:35 +01:00
}
2006-11-18 22:59:10 +01:00
}
2010-11-19 22:24:48 +01:00
2016-08-02 16:55:32 +02:00
if ( ! $error && ! $notrigger )
{
// Call trigger
$result = $this -> call_trigger ( 'CONTACT_MODIFY' , $user );
if ( $result < 0 ) { $error ++ ; }
// End call triggers
}
2017-07-13 23:52:02 +02:00
2016-08-02 16:55:32 +02:00
if ( ! $error )
{
$this -> db -> commit ();
return 1 ;
}
else
{
dol_syslog ( get_class ( $this ) . " ::update Error " . $this -> error , LOG_ERR );
$this -> db -> rollback ();
return - $error ;
}
2006-11-18 22:59:10 +01:00
}
2006-02-25 00:16:35 +01:00
2011-09-16 19:57:09 +02:00
/**
2013-10-08 00:42:40 +02:00
* Load object contact
2011-09-16 19:57:09 +02:00
*
2018-11-12 14:49:08 +01:00
* @ param int $id id du contact
* @ param User $user Utilisateur ( abonnes aux alertes ) qui veut les alertes de ce contact
* @ param string $ref_ext External reference , not given by Dolibarr
* @ param string $email Email
* @ return int - 1 if KO , 0 if OK but not found , 1 if OK
2009-08-11 14:51:33 +02:00
*/
2018-11-12 15:27:59 +01:00
function fetch ( $id , $user = null , $ref_ext = '' , $email = '' )
2009-08-11 14:51:33 +02:00
{
global $langs ;
2011-05-02 20:21:57 +02:00
2015-08-29 13:45:23 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch id= " . $id , LOG_DEBUG );
2015-07-19 23:39:07 +02:00
if ( empty ( $id ) && empty ( $ref_ext ))
{
$this -> error = 'BadParameter' ;
return - 1 ;
}
2009-08-11 14:51:33 +02:00
$langs -> load ( " companies " );
2011-05-02 20:21:57 +02:00
2018-01-10 12:38:12 +01:00
$sql = " SELECT c.rowid, c.entity, c.fk_soc, c.ref_ext, c.civility as civility_id, c.lastname, c.firstname, " ;
2013-07-10 12:28:28 +02:00
$sql .= " c.address, c.statut, c.zip, c.town, " ;
2011-12-30 14:18:19 +01:00
$sql .= " c.fk_pays as country_id, " ;
2010-06-16 21:18:01 +02:00
$sql .= " c.fk_departement, " ;
2009-08-11 14:51:33 +02:00
$sql .= " c.birthday, " ;
2018-09-26 16:55:46 +02:00
$sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email, c.jabberid, c.skype, c.twitter, c.facebook, " ;
2015-10-04 17:25:46 +02:00
$sql .= " c.photo, " ;
2018-12-11 09:49:46 +01:00
$sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas, " ;
2012-01-18 00:16:02 +01:00
$sql .= " c.import_key, " ;
2018-08-26 18:08:10 +02:00
$sql .= " c.datec as date_creation, c.tms as date_modification, " ;
2014-08-10 13:37:39 +02:00
$sql .= " co.label as country, co.code as country_code, " ;
2012-01-19 22:20:28 +01:00
$sql .= " d.nom as state, d.code_departement as state_code, " ;
2009-08-11 14:51:33 +02:00
$sql .= " u.rowid as user_id, u.login as user_login, " ;
2013-02-26 12:41:52 +01:00
$sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang " ;
2009-08-11 14:51:33 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " socpeople as c " ;
2014-08-10 13:37:39 +02:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_country as co ON c.fk_pays = co.rowid " ;
2010-06-06 16:30:28 +02:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_departements as d ON c.fk_departement = d.rowid " ;
2009-08-11 14:51:33 +02:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " user as u ON c.rowid = u.fk_socpeople " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " societe as s ON c.fk_soc = s.rowid " ;
2015-02-05 12:05:34 +01:00
if ( $id ) $sql .= " WHERE c.rowid = " . $id ;
2018-11-12 14:49:08 +01:00
else
{
2018-04-27 10:42:10 +02:00
$sql .= " WHERE c.entity IN ( " . getEntity ( $this -> element ) . " ) " ;
2018-11-12 14:49:08 +01:00
if ( $ref_ext ) {
$sql .= " AND c.ref_ext = ' " . $this -> db -> escape ( $ref_ext ) . " ' " ;
}
if ( $email ) {
$sql .= " AND c.email = ' " . $this -> db -> escape ( $email ) . " ' " ;
}
2018-04-27 10:42:10 +02:00
}
2009-08-11 14:51:33 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( $this -> db -> num_rows ( $resql ))
{
$obj = $this -> db -> fetch_object ( $resql );
2011-03-09 11:33:35 +01:00
$this -> id = $obj -> rowid ;
2018-01-10 12:38:12 +01:00
$this -> entity = $obj -> entity ;
2011-03-09 11:33:35 +01:00
$this -> ref = $obj -> rowid ;
2013-10-08 00:42:40 +02:00
$this -> ref_ext = $obj -> ref_ext ;
2014-05-05 00:23:09 +02:00
$this -> civility_id = $obj -> civility_id ;
2018-01-10 12:38:12 +01:00
$this -> civility_code = $obj -> civility_id ;
2011-12-30 14:18:19 +01:00
$this -> lastname = $obj -> lastname ;
2018-01-10 12:38:12 +01:00
$this -> firstname = $obj -> firstname ;
2011-03-09 11:33:35 +01:00
$this -> address = $obj -> address ;
2011-12-30 14:18:19 +01:00
$this -> zip = $obj -> zip ;
$this -> town = $obj -> town ;
2011-03-09 11:33:35 +01:00
2018-08-26 18:08:10 +02:00
$this -> date_creation = $this -> db -> jdate ( $obj -> date_creation );
$this -> date_modification = $this -> db -> jdate ( $obj -> date_modification );
2018-09-02 18:18:10 +02:00
2012-01-19 22:20:28 +01:00
$this -> fk_departement = $obj -> fk_departement ; // deprecated
2011-12-29 18:07:41 +01:00
$this -> state_id = $obj -> fk_departement ;
2018-01-10 12:38:12 +01:00
$this -> departement_code = $obj -> state_code ; // deprecated
$this -> state_code = $obj -> state_code ;
2012-01-19 22:20:28 +01:00
$this -> departement = $obj -> state ; // deprecated
2018-01-10 12:38:12 +01:00
$this -> state = $obj -> state ;
2010-06-06 16:30:28 +02:00
2011-12-30 14:18:19 +01:00
$this -> country_id = $obj -> country_id ;
$this -> country_code = $obj -> country_id ? $obj -> country_code : '' ;
2015-07-02 17:00:37 +02:00
$this -> country = $obj -> country_id ? ( $langs -> trans ( 'Country' . $obj -> country_code ) != 'Country' . $obj -> country_code ? $langs -> transnoentities ( 'Country' . $obj -> country_code ) : $obj -> country ) : '' ;
2011-05-02 20:21:57 +02:00
2018-01-10 12:38:12 +01:00
$this -> socid = $obj -> fk_soc ;
2011-03-09 11:33:35 +01:00
$this -> socname = $obj -> socname ;
2018-01-10 12:38:12 +01:00
$this -> poste = $obj -> poste ;
2013-07-10 12:28:28 +02:00
$this -> statut = $obj -> statut ;
2009-08-11 14:51:33 +02:00
2018-01-10 12:38:12 +01:00
$this -> phone_pro = trim ( $obj -> phone );
2011-03-09 11:33:35 +01:00
$this -> fax = trim ( $obj -> fax );
$this -> phone_perso = trim ( $obj -> phone_perso );
$this -> phone_mobile = trim ( $obj -> phone_mobile );
2009-08-11 14:51:33 +02:00
2018-01-10 12:38:12 +01:00
$this -> email = $obj -> email ;
2011-03-09 11:33:35 +01:00
$this -> jabberid = $obj -> jabberid ;
2018-01-10 12:38:12 +01:00
$this -> skype = $obj -> skype ;
2018-09-26 16:55:46 +02:00
$this -> twitter = $obj -> twitter ;
$this -> facebook = $obj -> facebook ;
2018-01-10 12:38:12 +01:00
$this -> photo = $obj -> photo ;
2011-03-09 11:33:35 +01:00
$this -> priv = $obj -> priv ;
$this -> mail = $obj -> email ;
2009-08-11 14:51:33 +02:00
2012-01-22 02:20:28 +01:00
$this -> birthday = $this -> db -> jdate ( $obj -> birthday );
2013-04-10 15:53:40 +02:00
$this -> note = $obj -> note_private ; // deprecated
2013-04-09 17:18:07 +02:00
$this -> note_private = $obj -> note_private ;
$this -> note_public = $obj -> note_public ;
2011-03-09 11:33:35 +01:00
$this -> default_lang = $obj -> default_lang ;
$this -> user_id = $obj -> user_id ;
$this -> user_login = $obj -> user_login ;
$this -> canvas = $obj -> canvas ;
2009-08-11 14:51:33 +02:00
2012-01-18 00:16:02 +01:00
$this -> import_key = $obj -> import_key ;
2017-07-13 23:52:02 +02:00
2016-04-22 10:30:13 +02:00
// Define gender according to civility
2016-08-06 00:04:24 +02:00
$this -> setGenderFromCivility ();
2012-01-18 00:16:02 +01:00
2016-04-22 10:30:13 +02:00
// Search Dolibarr user linked to this contact
2009-08-11 14:51:33 +02:00
$sql = " SELECT u.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " user as u " ;
$sql .= " WHERE u.fk_socpeople = " . $this -> id ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( $this -> db -> num_rows ( $resql ))
{
$uobj = $this -> db -> fetch_object ( $resql );
$this -> user_id = $uobj -> rowid ;
}
$this -> db -> free ( $resql );
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
// Charge alertes du user
if ( $user )
{
$sql = " SELECT fk_user " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " user_alert " ;
2014-05-06 00:06:54 +02:00
$sql .= " WHERE fk_user = " . $user -> id . " AND fk_contact = " . $this -> db -> escape ( $id );
2009-08-11 14:51:33 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( $this -> db -> num_rows ( $resql ))
{
$obj = $this -> db -> fetch_object ( $resql );
$this -> birthday_alert = 1 ;
}
$this -> db -> free ( $resql );
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2007-09-10 02:17:28 +02:00
2018-02-20 19:38:18 +01:00
// Retreive all extrafield
// fetch optionals attributes and labels
$this -> fetch_optionals ();
2015-05-26 12:40:49 +02:00
2007-09-10 02:17:28 +02:00
return 1 ;
}
else
{
$this -> error = $langs -> trans ( " RecordNotFound " );
return 0 ;
}
2009-08-11 14:51:33 +02:00
}
else
{
2008-04-27 17:51:01 +02:00
$this -> error = $this -> db -> error ();
2009-08-11 14:51:33 +02:00
return - 1 ;
}
}
2016-08-06 00:04:24 +02:00
/**
* Set property -> gender from property -> civility_id
2017-07-13 23:52:02 +02:00
*
2016-08-06 00:04:24 +02:00
* @ return void
*/
function setGenderFromCivility ()
{
unset ( $this -> gender );
if ( in_array ( $this -> civility_id , array ( 'MR' ))) {
$this -> gender = 'man' ;
} else if ( in_array ( $this -> civility_id , array ( 'MME' , 'MLE' ))) {
$this -> gender = 'woman' ;
}
2017-07-13 23:52:02 +02:00
}
2018-09-02 18:18:10 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2011-07-04 11:36:29 +02:00
/**
2016-02-24 11:17:47 +01:00
* Load number of elements the contact is used as a link for
2011-09-16 19:57:09 +02:00
* ref_facturation
* ref_contrat
2016-02-24 11:17:47 +01:00
* ref_commande ( for order and / or shipments )
2011-09-16 19:57:09 +02:00
* ref_propale
*
* @ return int < 0 if KO , >= 0 if OK
2009-08-11 14:51:33 +02:00
*/
function load_ref_elements ()
{
2018-09-02 18:18:10 +02:00
// phpcs:enable
2009-08-11 14:51:33 +02:00
// Compte les elements pour lesquels il est contact
$sql = " SELECT tc.element, count(ec.rowid) as nb " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " element_contact as ec, " . MAIN_DB_PREFIX . " c_type_contact as tc " ;
$sql .= " WHERE ec.fk_c_type_contact = tc.rowid " ;
$sql .= " AND fk_socpeople = " . $this -> id ;
2018-10-24 10:17:21 +02:00
$sql .= " AND tc.source = 'external' " ;
2009-08-11 14:51:33 +02:00
$sql .= " GROUP BY tc.element " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::load_ref_elements " , LOG_DEBUG );
2009-08-11 14:51:33 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
while ( $obj = $this -> db -> fetch_object ( $resql ))
{
if ( $obj -> nb )
{
if ( $obj -> element == 'facture' ) $this -> ref_facturation = $obj -> nb ;
2018-09-16 19:19:45 +02:00
elseif ( $obj -> element == 'contrat' ) $this -> ref_contrat = $obj -> nb ;
elseif ( $obj -> element == 'commande' ) $this -> ref_commande = $obj -> nb ;
elseif ( $obj -> element == 'propal' ) $this -> ref_propal = $obj -> nb ;
2009-08-11 14:51:33 +02:00
}
2006-02-25 00:16:35 +01:00
}
2009-08-11 14:51:33 +02:00
$this -> db -> free ( $resql );
return 0 ;
}
else
{
2016-02-24 11:17:47 +01:00
$this -> error = $this -> db -> lasterror ();
2009-08-11 14:51:33 +02:00
return - 1 ;
}
}
2006-02-25 00:16:35 +01:00
2011-07-04 11:36:29 +02:00
/**
* Efface le contact de la base
2011-09-16 19:57:09 +02:00
*
* @ param int $notrigger Disable all trigger
* @ return int < 0 if KO , > 0 if OK
2009-08-11 14:51:33 +02:00
*/
2007-09-16 05:55:35 +02:00
function delete ( $notrigger = 0 )
2006-02-25 00:16:35 +01:00
{
2008-04-10 01:07:13 +02:00
global $conf , $langs , $user ;
2009-01-19 23:41:26 +01:00
2007-09-16 05:55:35 +02:00
$error = 0 ;
2009-01-19 23:41:26 +01:00
2018-12-01 15:45:02 +01:00
//$this->old_lastname = $obj->lastname;
//$this->old_firstname = $obj->firstname;
2009-01-19 23:41:26 +01:00
2007-09-16 05:55:35 +02:00
$this -> db -> begin ();
2009-01-19 23:41:26 +01:00
2007-09-16 05:55:35 +02:00
if ( ! $error )
2006-11-18 22:59:10 +01:00
{
2007-09-16 05:55:35 +02:00
// Get all rowid of element_contact linked to a type that is link to llx_socpeople
$sql = " SELECT ec.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " element_contact ec, " ;
$sql .= " " . MAIN_DB_PREFIX . " c_type_contact tc " ;
$sql .= " WHERE ec.fk_socpeople= " . $this -> id ;
$sql .= " AND ec.fk_c_type_contact=tc.rowid " ;
$sql .= " AND tc.source='external' " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " , LOG_DEBUG );
2007-09-16 05:55:35 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2006-11-18 22:59:10 +01:00
{
2009-08-11 14:51:33 +02:00
$num = $this -> db -> num_rows ( $resql );
2009-01-19 23:41:26 +01:00
2007-09-16 05:55:35 +02:00
$i = 0 ;
while ( $i < $num && ! $error )
{
$obj = $this -> db -> fetch_object ( $resql );
$sqldel = " DELETE FROM " . MAIN_DB_PREFIX . " element_contact " ;
$sqldel .= " WHERE rowid = " . $obj -> rowid ;
2014-06-12 13:49:05 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " , LOG_DEBUG );
2007-09-16 05:55:35 +02:00
$result = $this -> db -> query ( $sqldel );
if ( ! $result )
{
$error ++ ;
$this -> error = $this -> db -> error () . ' sql=' . $sqldel ;
}
$i ++ ;
}
}
else
{
$error ++ ;
$this -> error = $this -> db -> error () . ' sql=' . $sql ;
2006-11-18 22:59:10 +01:00
}
}
2009-01-19 23:41:26 +01:00
2013-09-05 21:03:03 +02:00
if ( ! $error )
{
// Remove category
2013-09-09 02:37:11 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " categorie_contact WHERE fk_socpeople = " . $this -> id ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " , LOG_DEBUG );
2013-09-05 21:03:03 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$error ++ ;
$this -> error .= $this -> db -> lasterror ();
$errorflag =- 1 ;
}
}
2007-09-16 05:55:35 +02:00
if ( ! $error )
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " socpeople " ;
$sql .= " WHERE rowid= " . $this -> id ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " , LOG_DEBUG );
2007-09-16 05:55:35 +02:00
$result = $this -> db -> query ( $sql );
if ( ! $result )
{
$error ++ ;
$this -> error = $this -> db -> error () . ' sql=' . $sql ;
}
}
2013-04-06 00:37:20 +02:00
2012-12-16 23:18:20 +01:00
// Removed extrafields
if (( ! $error ) && ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED ))) { // For avoid conflicts if trigger used
$result = $this -> deleteExtraFields ( $this );
if ( $result < 0 ) $error ++ ;
}
2007-09-16 05:55:35 +02:00
if ( ! $error && ! $notrigger )
{
2014-07-04 17:10:19 +02:00
// Call trigger
$result = $this -> call_trigger ( 'CONTACT_DELETE' , $user );
2014-10-04 17:20:17 +02:00
if ( $result < 0 ) { $error ++ ; }
2014-07-04 17:10:19 +02:00
// End call triggers
2007-09-16 05:55:35 +02:00
}
2009-01-19 23:41:26 +01:00
2007-09-16 05:55:35 +02:00
if ( ! $error )
2006-11-18 22:59:10 +01:00
{
2009-01-19 23:41:26 +01:00
2007-09-16 05:55:35 +02:00
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
2012-05-13 14:27:51 +02:00
dol_syslog ( " Error " . $this -> error , LOG_ERR );
2006-11-19 02:46:32 +01:00
return - 1 ;
2006-11-18 22:59:10 +01:00
}
2006-02-25 00:16:35 +01:00
}
2006-06-24 17:13:29 +02:00
2009-01-19 23:41:26 +01:00
2011-09-16 19:57:09 +02:00
/**
* Charge les informations sur le contact , depuis la base
*
* @ param int $id Id du contact a charger
* @ return void
2009-08-11 14:51:33 +02:00
*/
function info ( $id )
{
2010-01-13 19:51:19 +01:00
$sql = " SELECT c.rowid, c.datec as datec, c.fk_user_creat, " ;
$sql .= " c.tms as tms, c.fk_user_modif " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " socpeople as c " ;
2014-05-06 00:06:54 +02:00
$sql .= " WHERE c.rowid = " . $this -> db -> escape ( $id );
2009-08-11 14:51:33 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
if ( $this -> db -> num_rows ( $resql ))
{
$obj = $this -> db -> fetch_object ( $resql );
2018-09-16 19:19:45 +02:00
$this -> id = $obj -> rowid ;
2009-08-11 14:51:33 +02:00
if ( $obj -> fk_user_creat ) {
2010-04-28 09:31:34 +02:00
$cuser = new User ( $this -> db );
$cuser -> fetch ( $obj -> fk_user_creat );
2009-08-11 14:51:33 +02:00
$this -> user_creation = $cuser ;
}
if ( $obj -> fk_user_modif ) {
2010-04-28 09:31:34 +02:00
$muser = new User ( $this -> db );
$muser -> fetch ( $obj -> fk_user_modif );
2009-08-11 14:51:33 +02:00
$this -> user_modification = $muser ;
}
2010-01-13 19:51:19 +01:00
$this -> date_creation = $this -> db -> jdate ( $obj -> datec );
$this -> date_modification = $this -> db -> jdate ( $obj -> tms );
2009-08-11 14:51:33 +02:00
}
$this -> db -> free ( $resql );
}
else
{
print $this -> db -> error ();
}
}
2010-02-21 22:21:12 +01:00
/**
2011-09-16 19:57:09 +02:00
* Return number of mass Emailing received by this contacts with its email
*
* @ return int Number of EMailings
2009-08-11 14:51:33 +02:00
*/
function getNbOfEMailings ()
{
$sql = " SELECT count(mc.email) as nb " ;
2018-09-23 15:44:44 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " mailing_cibles as mc, " . MAIN_DB_PREFIX . " mailing as m " ;
$sql .= " WHERE mc.fk_mailing=m.rowid AND mc.email = ' " . $this -> db -> escape ( $this -> email ) . " ' " ;
$sql .= " AND m.entity IN ( " . getEntity ( $this -> element ) . " ) AND mc.statut NOT IN (-1,0) " ; // -1 error, 0 not sent, 1 sent with success
2012-09-17 01:39:30 +02:00
2009-08-11 14:51:33 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
$nb = $obj -> nb ;
$this -> db -> free ( $resql );
return $nb ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
2006-08-05 18:44:43 +02:00
/**
2011-09-16 19:57:09 +02:00
* Return name of contact with link ( and eventually picto )
2014-05-05 00:23:09 +02:00
* Use $this -> id , $this -> lastname , $this -> firstname , this -> civility_id
2011-09-16 19:57:09 +02:00
*
2017-10-03 16:00:52 +02:00
* @ param int $withpicto Include picto with link
* @ param string $option Where the link point to
* @ param int $maxlen Max length of
* @ param string $moreparam Add more param into URL
* @ param int $save_lastsearch_value - 1 = Auto , 0 = No save of lastsearch_values when clicking , 1 = Save lastsearch_values whenclicking
2018-03-12 19:02:30 +01:00
* @ param int $notooltip 1 = Disable tooltip
2017-10-03 16:00:52 +02:00
* @ return string String with URL
2006-08-05 18:44:43 +02:00
*/
2018-03-12 19:02:30 +01:00
function getNomUrl ( $withpicto = 0 , $option = '' , $maxlen = 0 , $moreparam = '' , $save_lastsearch_value =- 1 , $notooltip = 0 )
2006-08-05 18:44:43 +02:00
{
2016-07-16 18:00:53 +02:00
global $conf , $langs , $hookmanager ;
2009-01-19 23:41:26 +01:00
2006-08-05 18:44:43 +02:00
$result = '' ;
2017-10-03 16:00:52 +02:00
$label = '<u>' . $langs -> trans ( " ShowContact " ) . '</u>' ;
$label .= '<br><b>' . $langs -> trans ( " Name " ) . ':</b> ' . $this -> getFullName ( $langs );
//if ($this->civility_id) $label.= '<br><b>' . $langs->trans("Civility") . ':</b> '.$this->civility_id; // TODO Translate cibilty_id code
if ( ! empty ( $this -> poste )) $label .= '<br><b>' . $langs -> trans ( " Poste " ) . ':</b> ' . $this -> poste ;
$label .= '<br><b>' . $langs -> trans ( " EMail " ) . ':</b> ' . $this -> email ;
$phonelist = array ();
if ( $this -> phone_pro ) $phonelist [] = $this -> phone_pro ;
if ( $this -> phone_mobile ) $phonelist [] = $this -> phone_mobile ;
if ( $this -> phone_perso ) $phonelist [] = $this -> phone_perso ;
$label .= '<br><b>' . $langs -> trans ( " Phone " ) . ':</b> ' . join ( ', ' , $phonelist );
$label .= '<br><b>' . $langs -> trans ( " Address " ) . ':</b> ' . dol_format_address ( $this , 1 , ' ' , $langs );
$url = DOL_URL_ROOT . '/contact/card.php?id=' . $this -> id ;
if ( $option !== 'nolink' )
{
// Add param to save lastsearch_values or not
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
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' ;
}
$url .= $moreparam ;
$linkclose = " " ;
2018-03-12 19:02:30 +01:00
if ( empty ( $notooltip )) {
if ( ! empty ( $conf -> global -> MAIN_OPTIMIZEFORTEXTBROWSER ))
{
$label = $langs -> trans ( " ShowContact " );
$linkclose .= ' alt="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
}
$linkclose .= ' title="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
$linkclose .= ' class="classfortooltip"' ;
2016-07-16 18:00:53 +02:00
2018-06-04 12:55:55 +02:00
/*
$hookmanager -> initHooks ( array ( 'contactdao' ));
$parameters = array ( 'id' => $this -> id );
$reshook = $hookmanager -> executeHooks ( 'getnomurltooltip' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( $reshook > 0 ) $linkclose = $hookmanager -> resPrint ;
*/
2016-07-16 18:00:53 +02:00
}
2006-08-05 18:44:43 +02:00
2018-06-04 12:55:55 +02:00
$linkstart = '<a href="' . $url . '"' ;
$linkstart .= $linkclose . '>' ;
2016-07-16 18:00:53 +02:00
$linkend = '</a>' ;
2017-07-13 23:52:02 +02:00
2006-08-05 18:44:43 +02:00
if ( $option == 'xxx' )
{
2017-10-03 16:00:52 +02:00
$linkstart = '<a href="' . DOL_URL_ROOT . '/contact/card.php?id=' . $this -> id . $moreparam . '" title="' . dol_escape_htmltag ( $label , 1 ) . '" class="classfortooltip">' ;
2015-03-15 14:04:07 +01:00
$linkend = '</a>' ;
2006-08-05 18:44:43 +02:00
}
2017-07-13 23:52:02 +02:00
2017-11-02 15:03:09 +01:00
$result .= $linkstart ;
if ( $withpicto ) $result .= img_object (( $notooltip ? '' : $label ), ( $this -> picto ? $this -> picto : 'generic' ), ( $notooltip ? (( $withpicto != 2 ) ? 'class="paddingright"' : '' ) : 'class="' . (( $withpicto != 2 ) ? 'paddingright ' : '' ) . 'classfortooltip valigntextbottom"' ), 0 , 0 , $notooltip ? 0 : 1 );
if ( $withpicto != 2 ) $result .= ( $maxlen ? dol_trunc ( $this -> getFullName ( $langs ), $maxlen ) : $this -> getFullName ( $langs ));
$result .= $linkend ;
2018-03-15 20:46:55 +01:00
global $action ;
$hookmanager -> initHooks ( array ( 'contactdao' ));
$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 ;
2006-08-05 18:44:43 +02:00
return $result ;
2006-09-01 01:44:34 +02:00
}
2009-08-11 14:51:33 +02:00
/**
2013-05-12 17:09:48 +02:00
* Return civility label of contact
2011-09-16 19:57:09 +02:00
*
2013-05-12 17:09:48 +02:00
* @ return string Translated name of civility
2009-08-11 14:51:33 +02:00
*/
function getCivilityLabel ()
{
global $langs ;
$langs -> load ( " dict " );
2008-01-10 18:12:07 +01:00
2014-11-12 10:05:40 +01:00
$code = ( ! empty ( $this -> civility_id ) ? $this -> civility_id : ( ! empty ( $this -> civilite_id ) ? $this -> civilite_id : '' ));
2013-06-18 23:28:03 +02:00
if ( empty ( $code )) return '' ;
2014-08-12 06:03:27 +02:00
return $langs -> getLabelFromKey ( $this -> db , " Civility " . $code , " c_civility " , " code " , " label " , $code );
2009-08-11 14:51:33 +02:00
}
2008-01-10 18:12:07 +01:00
2007-05-04 23:19:15 +02:00
/**
2013-09-10 13:45:39 +02:00
* Return label of contact status
2011-09-16 19:57:09 +02:00
*
2013-09-10 13:45:39 +02:00
* @ param int $mode 0 = libelle long , 1 = libelle court , 2 = Picto + Libelle court , 3 = Picto , 4 = Picto + Libelle long , 5 = Libelle court + Picto
* @ return string Label of contact status
2007-05-04 23:19:15 +02:00
*/
function getLibStatut ( $mode )
{
2013-07-10 12:28:28 +02:00
return $this -> LibStatut ( $this -> statut , $mode );
}
2013-09-09 02:37:11 +02:00
2018-09-02 18:18:10 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2007-05-04 23:19:15 +02:00
/**
2011-09-16 19:57:09 +02:00
* Renvoi le libelle d ' un statut donne
*
* @ param int $statut Id statut
* @ param int $mode 0 = libelle long , 1 = libelle court , 2 = Picto + Libelle court , 3 = Picto , 4 = Picto + Libelle long , 5 = Libelle court + Picto
* @ return string Libelle
2007-05-04 23:19:15 +02:00
*/
2013-09-10 13:45:39 +02:00
function LibStatut ( $statut , $mode )
2007-05-04 23:19:15 +02:00
{
2018-09-02 18:18:10 +02:00
// phpcs:enable
2007-05-04 23:19:15 +02:00
global $langs ;
2009-01-19 23:41:26 +01:00
2009-08-11 14:51:33 +02:00
if ( $mode == 0 )
{
2013-12-02 22:12:57 +01:00
if ( $statut == 0 || $statut == 5 ) return $langs -> trans ( 'Disabled' );
elseif ( $statut == 1 || $statut == 4 ) return $langs -> trans ( 'Enabled' );
2009-08-11 14:51:33 +02:00
}
2011-10-29 18:28:13 +02:00
elseif ( $mode == 1 )
2009-08-11 14:51:33 +02:00
{
2013-12-02 22:12:57 +01:00
if ( $statut == 0 || $statut == 5 ) return $langs -> trans ( 'Disabled' );
elseif ( $statut == 1 || $statut == 4 ) return $langs -> trans ( 'Enabled' );
2009-08-11 14:51:33 +02:00
}
2011-10-29 18:28:13 +02:00
elseif ( $mode == 2 )
2009-08-11 14:51:33 +02:00
{
2016-09-10 19:44:55 +02:00
if ( $statut == 0 || $statut == 5 ) return img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' , 'class="pictostatus"' ) . ' ' . $langs -> trans ( 'Disabled' );
elseif ( $statut == 1 || $statut == 4 ) return img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' , 'class="pictostatus"' ) . ' ' . $langs -> trans ( 'Enabled' );
2009-08-11 14:51:33 +02:00
}
2011-10-29 18:28:13 +02:00
elseif ( $mode == 3 )
2009-08-11 14:51:33 +02:00
{
2016-09-10 19:44:55 +02:00
if ( $statut == 0 || $statut == 5 ) return img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' , 'class="pictostatus"' );
elseif ( $statut == 1 || $statut == 4 ) return img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' , 'class="pictostatus"' );
2009-08-11 14:51:33 +02:00
}
2011-10-29 18:28:13 +02:00
elseif ( $mode == 4 )
2009-08-11 14:51:33 +02:00
{
2016-09-10 19:44:55 +02:00
if ( $statut == 0 ) return img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' , 'class="pictostatus"' ) . ' ' . $langs -> trans ( 'Disabled' );
elseif ( $statut == 1 || $statut == 4 ) return img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' , 'class="pictostatus"' ) . ' ' . $langs -> trans ( 'Enabled' );
2009-08-11 14:51:33 +02:00
}
2011-10-29 18:28:13 +02:00
elseif ( $mode == 5 )
2009-08-11 14:51:33 +02:00
{
2016-09-10 19:44:55 +02:00
if ( $statut == 0 || $statut == 5 ) return '<span class="hideonsmartphone">' . $langs -> trans ( 'Disabled' ) . ' </span>' . img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' , 'class="pictostatus"' );
elseif ( $statut == 1 || $statut == 4 ) return '<span class="hideonsmartphone">' . $langs -> trans ( 'Enabled' ) . ' </span>' . img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' , 'class="pictostatus"' );
2009-08-11 14:51:33 +02:00
}
2007-05-04 23:19:15 +02:00
}
2013-10-08 00:42:40 +02:00
2018-09-02 18:18:10 +02:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
2008-03-31 00:25:39 +02:00
/**
2011-09-16 19:57:09 +02:00
* Return translated label of Public or Private
*
* @ param int $statut Type ( 0 = public , 1 = private )
* @ return string Label translated
2008-03-31 00:25:39 +02:00
*/
function LibPubPriv ( $statut )
{
2018-09-02 18:18:10 +02:00
// phpcs:enable
2008-03-31 00:25:39 +02:00
global $langs ;
2009-08-11 14:51:33 +02:00
if ( $statut == '1' ) return $langs -> trans ( 'ContactPrivate' );
else return $langs -> trans ( 'ContactPublic' );
2008-03-31 00:25:39 +02:00
}
2006-11-19 02:46:32 +01:00
/**
2011-09-20 19:19:46 +02:00
* Initialise an instance with random values .
* Used to build previews or test instances .
* id must be 0 if object instance is a specimen .
*
* @ return void
2006-11-19 02:46:32 +01:00
*/
function initAsSpecimen ()
{
2014-02-08 12:58:27 +01:00
// Get first id of existing company and save it into $socid
$socid = 0 ;
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " societe ORDER BY rowid LIMIT 1 " ;
2006-11-19 02:46:32 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2014-02-08 12:58:27 +01:00
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj ) $socid = $obj -> rowid ;
2006-11-19 02:46:32 +01:00
}
2009-08-12 02:40:16 +02:00
// Initialise parameters
2006-11-19 02:46:32 +01:00
$this -> id = 0 ;
$this -> specimen = 1 ;
2012-06-26 13:11:04 +02:00
$this -> lastname = 'DOLIBARR' ;
2011-12-30 14:18:19 +01:00
$this -> firstname = 'SPECIMEN' ;
2013-04-06 00:37:20 +02:00
$this -> address = '21 jump street' ;
$this -> zip = '99999' ;
$this -> town = 'MyTown' ;
2011-12-30 14:18:19 +01:00
$this -> country_id = 1 ;
$this -> country_code = 'FR' ;
$this -> country = 'France' ;
2006-11-19 02:46:32 +01:00
$this -> email = 'specimen@specimen.com' ;
2014-02-08 12:58:27 +01:00
$this -> skype = 'tom.hanson' ;
2013-04-06 00:37:20 +02:00
2013-06-05 16:24:32 +02:00
$this -> phone_pro = '0909090901' ;
$this -> phone_perso = '0909090902' ;
$this -> phone_mobile = '0909090903' ;
$this -> fax = '0909090909' ;
2013-04-06 03:40:55 +02:00
2013-06-05 16:24:32 +02:00
$this -> note_public = 'This is a comment (public)' ;
$this -> note_private = 'This is a comment (private)' ;
2013-04-06 00:37:20 +02:00
2014-02-08 12:58:27 +01:00
$this -> socid = $socid ;
2013-07-10 12:28:28 +02:00
$this -> statut = 1 ;
}
2013-09-09 02:37:11 +02:00
2013-07-10 12:28:28 +02:00
/**
* Change status of a user
*
* @ param int $statut Status to set
* @ return int < 0 if KO , 0 if nothing is done , > 0 if OK
*/
function setstatus ( $statut )
{
global $conf , $langs , $user ;
$error = 0 ;
// Check parameters
if ( $this -> statut == $statut ) return 0 ;
else $this -> statut = $statut ;
$this -> db -> begin ();
// Desactive utilisateur
$sql = " UPDATE " . MAIN_DB_PREFIX . " socpeople " ;
$sql .= " SET statut = " . $this -> statut ;
$sql .= " WHERE rowid = " . $this -> id ;
$result = $this -> db -> query ( $sql );
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::setstatus " , LOG_DEBUG );
2013-07-10 12:28:28 +02:00
if ( $result )
{
2014-07-04 17:10:19 +02:00
// Call trigger
$result = $this -> call_trigger ( 'CONTACT_ENABLEDISABLE' , $user );
2014-10-04 17:20:17 +02:00
if ( $result < 0 ) { $error ++ ; }
2014-07-04 17:10:19 +02:00
// End call triggers
2013-07-10 12:28:28 +02:00
}
if ( $error )
{
$this -> db -> rollback ();
return - $error ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
2006-11-19 02:46:32 +01:00
}
2015-09-30 18:46:51 +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
2018-08-14 12:23:10 +02:00
* @ return void
2015-09-30 18:46:51 +02:00
*/
public function setCategories ( $categories )
{
// 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 , Categorie :: TYPE_CONTACT , '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 ) {
2015-10-15 10:13:52 +02:00
if ( $c -> fetch ( $del ) > 0 ) {
$c -> del_type ( $this , 'contact' );
}
2015-09-30 18:46:51 +02:00
}
foreach ( $to_add as $add ) {
2015-10-15 10:13:52 +02:00
if ( $c -> fetch ( $add ) > 0 ) {
$c -> add_type ( $this , 'contact' );
}
2015-09-30 18:46:51 +02:00
}
return ;
}
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 (
'socpeople'
);
return CommonObject :: commonReplaceThirdparty ( $db , $origin_id , $dest_id , $tables );
}
2006-02-25 00:16:35 +01:00
}