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 >
2012-12-30 15:13:49 +01:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ capnetworks . 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 >
2013-12-02 22:12:57 +01:00
* Copyright ( C ) 2013 Alexandre Spangaro < alexandre . spangaro @ gmail . com >
2014-01-30 12:20:36 +01:00
* Copyright ( C ) 2013 Juanjo Menent < jmenent @ 2 byte . es >
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
{
2011-09-20 19:19:46 +02:00
public $element = 'contact' ;
public $table_element = 'socpeople' ;
2013-04-26 12:35:31 +02:00
protected $ismultientitymanaged = 1 ; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
2009-01-19 23:41:26 +01:00
2009-08-11 14:51:33 +02:00
var $id ;
2013-11-14 17:16:27 +01:00
var $ref_ext ;
2013-03-06 17:17:34 +01:00
var $civilite_id ; // In fact we store civility_code
2011-07-10 18:50:40 +02:00
var $lastname ;
2009-08-11 14:51:33 +02:00
var $firstname ;
var $address ;
2011-03-22 11:12:03 +01:00
var $zip ;
var $town ;
2010-06-06 16:30:28 +02:00
2012-01-19 22:20:28 +01:00
var $fk_departement ; // deprecated
var $departement_code ; // deprecated
var $departement ; // deprecated
2011-12-30 14:18:19 +01:00
var $state_id ; // Id of department
var $state_code ; // Code of department
var $state ; // Label of department
2010-06-06 16:30:28 +02:00
2012-01-19 22:20:28 +01:00
var $country_id ; // Id of country
var $country_code ; // Code of country
var $country ; // Label of country
2010-06-06 16:30:28 +02:00
2013-11-27 15:19:24 +01:00
var $poste ; // Position
2009-08-11 14:51:33 +02:00
var $socid ; // fk_soc
2013-10-30 12:24:51 +01:00
var $statut ; // 0=inactif, 1=actif
2009-08-11 14:51:33 +02:00
var $code ;
var $email ;
2014-01-30 18:17:52 +01:00
var $skype ;
2013-11-14 17:16:27 +01:00
var $jabberid ;
2013-10-27 02:33:03 +02:00
var $phone_pro ;
var $phone_perso ;
var $phone_mobile ;
2013-11-14 17:16:27 +01:00
var $fax ;
var $priv ;
2013-10-27 02:33:03 +02:00
2009-08-11 14:51:33 +02:00
var $birthday ;
2010-02-21 14:08:02 +01:00
var $default_lang ;
2013-04-06 00:37:20 +02:00
var $note_public ; // Public note
2013-04-09 17:18:07 +02:00
var $note ; // deprecated
var $note_private ; // Private note
2012-08-22 22:59:10 +02:00
var $no_email ; // 1=Don't send e-mail to this contact, 0=do
2009-08-11 14:51:33 +02:00
var $ref_facturation ; // Nb de reference facture pour lequel il est contact
var $ref_contrat ; // Nb de reference contrat pour lequel il est contact
var $ref_commande ; // Nb de reference commande pour lequel il est contact
var $ref_propal ; // Nb de reference propal pour lequel il est contact
2006-02-25 00:16:35 +01:00
2007-04-27 00:01:10 +02:00
var $user_id ;
var $user_login ;
2012-01-18 00:16:02 +01:00
var $import_key ;
2007-04-27 00:01:10 +02:00
2013-04-06 03:40:55 +02:00
var $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
}
/**
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
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 " ;
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 ) . " ', " ;
2009-08-11 14:51:33 +02:00
if ( $this -> socid > 0 ) $sql .= " " . $this -> socid . " , " ;
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 ) . " ', " ;
2008-07-19 18:53:56 +02:00
$sql .= " " . ( $user -> id > 0 ? " ' " . $user -> id . " ' " : " null " ) . " , " ;
2010-11-03 17:29:24 +01:00
$sql .= " " . $this -> priv . " , " ;
2013-10-30 12:24:51 +01:00
$sql .= " " . $this -> statut . " , " ;
2012-12-03 09:58:18 +01:00
$sql .= " " . ( ! empty ( $this -> canvas ) ? " ' " . $this -> canvas . " ' " : " null " ) . " , " ;
2012-11-30 04:40:06 +01:00
$sql .= " " . $conf -> entity . " , " ;
2012-12-03 09:58:18 +01:00
$sql .= " " . ( ! empty ( $this -> import_key ) ? " ' " . $this -> import_key . " ' " : " null " );
2009-08-11 14:51:33 +02:00
$sql .= " ) " ;
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::create sql= " . $sql );
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
}
2010-11-19 22:24:48 +01:00
if ( ! $error )
{
$result = $this -> update_perso ( $this -> id , $user );
if ( $result < 0 )
{
$error ++ ;
$this -> error = $this -> db -> lasterror ();
}
}
if ( ! $error )
{
// Appel des triggers
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php' ;
2010-11-19 22:24:48 +01:00
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'CONTACT_CREATE' , $this , $user , $langs , $conf );
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
// Fin appel triggers
}
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
2011-09-16 19:57:09 +02:00
* @ return int < 0 if KO , > 0 if OK
2009-08-11 14:51:33 +02:00
*/
2012-11-10 16:08:26 +01:00
function update ( $id , $user = 0 , $notrigger = 0 , $action = 'update' )
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 ;
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 );
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, " ;
2011-02-24 19:11:12 +01:00
$sql .= " civilite=' " . $this -> db -> escape ( $this -> civilite_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 ) . " ' " ;
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 " );
2009-08-11 14:51:33 +02:00
$sql .= " , priv = ' " . $this -> priv . " ' " ;
2013-10-30 12:24:51 +01:00
$sql .= " , statut = " . $this -> statut ;
2013-05-03 00:15:02 +02:00
$sql .= " , fk_user_modif= " . ( $user -> id > 0 ? " ' " . $user -> id . " ' " : " NULL " );
$sql .= " , default_lang= " . ( $this -> default_lang ? " ' " . $this -> default_lang . " ' " : " NULL " );
2013-03-06 21:26:05 +01:00
$sql .= " , no_email= " . ( $this -> no_email ? " ' " . $this -> no_email . " ' " : " 0 " );
2009-08-11 14:51:33 +02:00
$sql .= " WHERE rowid= " . $id ;
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::update sql= " . $sql , 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 );
2012-09-19 17:53:09 +02:00
// Actions on extra fields (by external module or standard code)
$hookmanager -> initHooks ( array ( 'contactdao' ));
$parameters = array ( 'socid' => $this -> id );
$reshook = $hookmanager -> executeHooks ( 'insertExtraFields' , $parameters , $this , $action ); // Note that $action and $object may have been modified by some hooks
if ( empty ( $reshook ))
2012-09-17 01:39:30 +02:00
{
2012-11-09 19:34:05 +01:00
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) // For avoid conflicts if trigger used
2012-09-19 17:53:09 +02:00
{
2012-11-09 19:23:46 +01:00
$result = $this -> insertExtraFields ();
if ( $result < 0 )
{
$error ++ ;
}
2012-09-19 17:53:09 +02:00
}
}
else if ( $reshook < 0 ) $error ++ ;
2012-09-17 01:39:30 +02:00
2009-08-11 22:16:58 +02:00
if ( ! $error && ! $notrigger )
{
// Appel des triggers
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php' ;
2009-08-11 22:16:58 +02:00
$interface = new Interfaces ( $this -> db );
2010-04-22 00:14:11 +02:00
$result = $interface -> run_triggers ( 'CONTACT_MODIFY' , $this , $user , $langs , $conf );
2009-08-11 22:16:58 +02:00
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
// Fin appel triggers
}
if ( ! $error )
{
$this -> db -> commit ();
return 1 ;
}
else
{
2010-06-23 20:12:29 +02:00
$this -> error = join ( ',' , $this -> errors );
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 ;
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::update Error " . $this -> error , LOG_ERR );
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
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
{
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 ;
if ( $mode == 1 ) $dn = $conf -> global -> LDAP_CONTACT_DN ;
if ( $mode == 2 ) $dn = $conf -> global -> LDAP_KEY_CONTACTS . " = " . $info [ $conf -> global -> LDAP_KEY_CONTACTS ];
return $dn ;
}
2006-11-19 02:46:32 +01:00
2006-11-20 02:13:13 +01:00
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
{
global $conf , $langs ;
2007-04-13 02:35:13 +02:00
// Object classes
2009-10-20 15:14:44 +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 );
2009-08-12 02:15:40 +02:00
$info [ $conf -> global -> LDAP_CONTACT_FIELD_COMPANY ] = $soc -> nom ;
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 ;
2013-11-04 21:41:36 +01:00
if ( $this -> skype && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_SKYPE )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_SKYPE ] = $this -> skype ;
2013-04-09 17:18:07 +02:00
if ( $this -> note_private && ! empty ( $conf -> global -> LDAP_CONTACT_FIELD_DESCRIPTION )) $info [ $conf -> global -> LDAP_CONTACT_FIELD_DESCRIPTION ] = $this -> note_private ;
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
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
* @ return int < 0 if KO , >= 0 if OK
2009-08-11 14:51:33 +02:00
*/
2006-11-21 23:51:05 +01:00
function update_perso ( $id , $user = 0 )
2006-11-18 22:59:10 +01:00
{
2010-11-19 22:24:48 +01:00
$error = 0 ;
$result = false ;
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 " );
2009-08-11 14:51:33 +02:00
if ( $user ) $sql .= " , fk_user_modif= " . $user -> id ;
2012-02-11 08:03:26 +01:00
$sql .= " WHERE rowid= " . $id ;
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::update_perso this->birthday= " . $this -> birthday . " - sql= " . $sql );
2007-01-16 23:02:36 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
2006-11-21 23:51:05 +01:00
{
2010-11-19 22:24:48 +01: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
2011-11-09 12:40:33 +01:00
$sql_check = " SELECT * FROM " . MAIN_DB_PREFIX . " user_alert WHERE type=1 AND fk_contact= " . $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) " ;
$sql .= " VALUES (1, " . $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 " ;
$sql .= " WHERE type=1 AND fk_contact= " . $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
2006-11-21 23:51:05 +01:00
return $result ;
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
*
* @ param int $id id du contact
* @ param User $user Utilisateur ( abonnes aux alertes ) qui veut les alertes de ce contact
* @ return int - 1 if KO , 0 if OK but not found , 1 if OK
2009-08-11 14:51:33 +02:00
*/
function fetch ( $id , $user = 0 )
{
global $langs ;
2011-05-02 20:21:57 +02:00
2009-08-11 14:51:33 +02:00
$langs -> load ( " companies " );
2011-05-02 20:21:57 +02:00
2013-10-08 00:42:40 +02:00
$sql = " SELECT c.rowid, c.fk_soc, c.ref_ext, c.civilite as civilite_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, " ;
2013-11-04 21:41:36 +01:00
$sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email, c.jabberid, c.skype, " ;
2013-04-09 17:18:07 +02:00
$sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.no_email, c.canvas, " ;
2012-01-18 00:16:02 +01:00
$sql .= " c.import_key, " ;
2011-12-30 14:18:19 +01:00
$sql .= " p.libelle as country, p.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 " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " c_pays as p ON c.fk_pays = p.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 " ;
$sql .= " WHERE c.rowid = " . $id ;
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::fetch sql= " . $sql );
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 ;
$this -> ref = $obj -> rowid ;
2013-10-08 00:42:40 +02:00
$this -> ref_ext = $obj -> ref_ext ;
2011-03-09 11:33:35 +01:00
$this -> civilite_id = $obj -> civilite_id ;
2011-12-30 14:18:19 +01:00
$this -> lastname = $obj -> lastname ;
2011-03-09 11:33:35 +01:00
$this -> firstname = $obj -> firstname ;
$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
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 ;
2012-01-19 22:20:28 +01:00
$this -> departement_code = $obj -> state_code ; // deprecated
$this -> state_code = $obj -> state_code ;
$this -> departement = $obj -> state ; // deprecated
$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 : '' ;
$this -> country = ( $obj -> country_id > 0 ) ? $langs -> transnoentitiesnoconv ( " Country " . $obj -> country_code ) : '' ;
2011-05-02 20:21:57 +02:00
2011-03-09 11:33:35 +01:00
$this -> socid = $obj -> fk_soc ;
$this -> socname = $obj -> socname ;
$this -> poste = $obj -> poste ;
2013-07-10 12:28:28 +02:00
$this -> statut = $obj -> statut ;
2009-08-11 14:51:33 +02:00
2011-03-09 11:33:35 +01:00
$this -> phone_pro = trim ( $obj -> phone );
$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
2011-03-09 11:33:35 +01:00
$this -> email = $obj -> email ;
$this -> jabberid = $obj -> jabberid ;
2013-11-04 21:41:36 +01:00
$this -> skype = $obj -> skype ;
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 ;
2012-08-22 22:59:10 +02:00
$this -> no_email = $obj -> no_email ;
2011-03-09 11:33:35 +01:00
$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 ;
2009-08-12 01:42:21 +02:00
// Recherche le user Dolibarr lie a ce 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 ();
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::fetch " . $this -> error , LOG_ERR );
2009-08-11 14:51:33 +02:00
return - 1 ;
}
// Charge alertes du user
if ( $user )
{
$sql = " SELECT fk_user " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " user_alert " ;
$sql .= " WHERE fk_user = " . $user -> id . " AND fk_contact = " . $id ;
$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 ();
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::fetch " . $this -> error , LOG_ERR );
2009-08-11 14:51:33 +02:00
return - 1 ;
}
}
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 ();
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::fetch " . $this -> error , LOG_ERR );
2009-08-11 14:51:33 +02:00
return - 1 ;
}
}
2011-07-04 11:36:29 +02:00
/**
2011-09-16 19:57:09 +02:00
* Charge le nombre d ' elements auquel est lie ce contact
* ref_facturation
* ref_contrat
* ref_commande
* ref_propale
*
* @ return int < 0 if KO , >= 0 if OK
2009-08-11 14:51:33 +02:00
*/
function load_ref_elements ()
{
// 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 ;
$sql .= " GROUP BY tc.element " ;
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::load_ref_elements sql= " . $sql );
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 ;
if ( $obj -> element == 'contrat' ) $this -> ref_contrat = $obj -> nb ;
if ( $obj -> element == 'commande' ) $this -> ref_commande = $obj -> nb ;
if ( $obj -> element == 'propal' ) $this -> ref_propal = $obj -> nb ;
}
2006-02-25 00:16:35 +01:00
}
2009-08-11 14:51:33 +02:00
$this -> db -> free ( $resql );
return 0 ;
}
else
{
$this -> error = $this -> db -> error () . " - " . $sql ;
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::load_ref_elements Error " . $this -> error , LOG_ERR );
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
2013-02-23 15:58:26 +01:00
$this -> old_lastname = $obj -> lastname ;
2007-09-16 05:55:35 +02:00
$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' " ;
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::delete sql= " . $sql );
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 ;
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::delete sql= " . $sqldel );
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 ;
2013-09-05 21:03:03 +02:00
dol_syslog ( get_class ( $this ) . " ::delete sql= " . $sql );
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$error ++ ;
$this -> error .= $this -> db -> lasterror ();
$errorflag =- 1 ;
2013-09-09 02:37:11 +02:00
dol_syslog ( get_class ( $this ) . " ::delete error " . $errorflag . " " . $this -> error , LOG_ERR );
2013-09-05 21:03:03 +02:00
}
}
2007-09-16 05:55:35 +02:00
if ( ! $error )
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " socpeople " ;
$sql .= " WHERE rowid= " . $this -> id ;
2011-12-30 14:18:19 +01:00
dol_syslog ( get_class ( $this ) . " ::delete sql= " . $sql );
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 )
{
// Appel des triggers
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php' ;
2007-09-16 05:55:35 +02:00
$interface = new Interfaces ( $this -> db );
2010-04-22 00:14:11 +02:00
$result = $interface -> run_triggers ( 'CONTACT_DELETE' , $this , $user , $langs , $conf );
2009-08-11 14:51:33 +02:00
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
2007-09-16 05:55:35 +02:00
// Fin appel triggers
2009-05-06 17:45:50 +02:00
2009-08-11 14:51:33 +02:00
if ( $error ) $this -> error = join ( ',' , $this -> errors );
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 " ;
$sql .= " WHERE c.rowid = " . $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 -> id = $obj -> rowid ;
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 " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " mailing_cibles as mc " ;
2011-02-24 19:11:12 +01:00
$sql .= " WHERE mc.email = ' " . $this -> db -> escape ( $this -> email ) . " ' " ;
2012-07-24 14:33:27 +02:00
$sql .= " AND mc.statut NOT IN (-1,0) " ; // -1 erreur, 0 non envoye, 1 envoye avec succes
2012-09-17 01:39:30 +02:00
2012-07-24 14:33:27 +02:00
dol_syslog ( get_class ( $this ) . " ::getNbOfEMailings sql= " . $sql , LOG_DEBUG );
2012-09-17 01:39:30 +02:00
2009-08-11 14:51:33 +02:00
$resql = $this -> db -> query ( $sql );
2012-09-17 01:39:30 +02:00
2009-08-11 14:51:33 +02:00
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 )
2013-02-23 15:58:26 +01:00
* Use $this -> id , $this -> lastname , $this -> firstname , this -> civilite_id
2011-09-16 19:57:09 +02:00
*
* @ param int $withpicto Include picto with link
* @ param string $option Where the link point to
* @ param int $maxlen Max length of
* @ return string String with URL
2006-08-05 18:44:43 +02:00
*/
2007-10-09 23:42:16 +02:00
function getNomUrl ( $withpicto = 0 , $option = '' , $maxlen = 0 )
2006-08-05 18:44:43 +02:00
{
global $langs ;
2009-01-19 23:41:26 +01:00
2006-08-05 18:44:43 +02:00
$result = '' ;
2009-01-19 23:41:26 +01:00
2006-08-05 18:44:43 +02:00
$lien = '<a href="' . DOL_URL_ROOT . '/contact/fiche.php?id=' . $this -> id . '">' ;
$lienfin = '</a>' ;
if ( $option == 'xxx' )
{
$lien = '<a href="' . DOL_URL_ROOT . '/contact/fiche.php?id=' . $this -> id . '">' ;
$lienfin = '</a>' ;
}
2007-10-17 17:26:32 +02:00
if ( $withpicto ) $result .= ( $lien . img_object ( $langs -> trans ( " ShowContact " ) . ': ' . $this -> getFullName ( $langs ), 'contact' ) . $lienfin . ' ' );
2009-02-20 23:53:15 +01:00
$result .= $lien . ( $maxlen ? dol_trunc ( $this -> getFullName ( $langs ), $maxlen ) : $this -> getFullName ( $langs )) . $lienfin ;
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
2013-06-18 23:28:03 +02:00
$code = ( ! empty ( $this -> civilite_id ) ? $this -> civilite_id : ( ! empty ( $this -> civility_id ) ? $this -> civility_id : '' ));
if ( empty ( $code )) return '' ;
2013-05-12 17:09:48 +02:00
return $langs -> getLabelFromKey ( $this -> db , " Civility " . $code , " c_civilite " , " code " , " civilite " , $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
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
{
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
{
2013-12-02 22:12:57 +01:00
if ( $statut == 0 || $statut == 5 ) return img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' ) . ' ' . $langs -> trans ( 'Disabled' );
elseif ( $statut == 1 || $statut == 4 ) return img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' ) . ' ' . $langs -> trans ( 'Enabled' );
2013-10-30 12:24:51 +01:00
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
{
2013-12-02 22:12:57 +01:00
if ( $statut == 0 || $statut == 5 ) return img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' );
elseif ( $statut == 1 || $statut == 4 ) return img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' );
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
{
2013-11-05 09:54:49 +01:00
if ( $statut == 0 ) return img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' ) . ' ' . $langs -> trans ( 'StatusContactDraft' );
2013-12-02 22:12:57 +01:00
elseif ( $statut == 1 || $statut == 4 ) return img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' ) . ' ' . $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
{
2013-12-02 22:12:57 +01:00
if ( $statut == 0 || $statut == 5 ) return '<span class="hideonsmartphone">' . $langs -> trans ( 'Disabled' ) . ' </span>' . img_picto ( $langs -> trans ( 'Disabled' ), 'statut5' );
elseif ( $statut == 1 || $statut == 4 ) return '<span class="hideonsmartphone">' . $langs -> trans ( 'Enabled' ) . ' </span>' . img_picto ( $langs -> trans ( 'Enabled' ), 'statut4' );
2009-08-11 14:51:33 +02:00
}
2007-05-04 23:19:15 +02:00
}
2013-10-08 00:42:40 +02:00
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 )
{
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 ()
{
global $user , $langs ;
2009-06-07 11:36:18 +02:00
// Charge tableau des id de societe socids
2006-11-19 02:46:32 +01:00
$socids = array ();
2007-06-12 00:51:47 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " societe LIMIT 10 " ;
2006-11-19 02:46:32 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num_socs = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num_socs )
{
$i ++ ;
$row = $this -> db -> fetch_row ( $resql );
$socids [ $i ] = $row [ 0 ];
}
}
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' ;
2013-11-04 21:41:36 +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
2006-11-19 02:46:32 +01:00
$socid = rand ( 1 , $num_socs );
2006-11-19 05:56:10 +01:00
$this -> socid = $socids [ $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 );
dol_syslog ( get_class ( $this ) . " ::setstatus sql= " . $sql );
if ( $result )
{
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php' ;
$interface = new Interfaces ( $this -> db );
2013-08-22 10:06:57 +02:00
$result = $interface -> run_triggers ( 'CONTACT_ENABLEDISABLE' , $this , $user , $langs , $conf );
2013-07-10 12:28:28 +02:00
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
// Fin appel triggers
}
if ( $error )
{
$this -> db -> rollback ();
return - $error ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
2006-11-19 02:46:32 +01:00
}
2006-02-25 00:16:35 +01:00
}
?>