--- /tmp/dsg/dolibarr/htdocs/contact/class/github_contact.class.php
+++ /tmp/dsg/dolibarr/htdocs/contact/class/client_contact.class.php
@@ -10,7 +10,6 @@
  * Copyright (C) 2013      Juanjo Menent	 	       <jmenent@2byte.es>

  * Copyright (C) 2015      Marcos García               <marcosgdf@gmail.com>

  * Copyright (C) 2019      Nicolas ZABOURI 	           <info@inovea-conseil.com>

- * Copyright (C) 2020      Open-Dsi  	               <support@open-dsi.fr>

  *

  * 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

@@ -60,29 +59,6 @@
 	 */

 	public $picto = 'contact';

 

-	/**

-	 *  'type' if the field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')

-	 *         Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"

-	 *  'label' the translation key.

-	 *  'enabled' is a condition when the field must be managed.

-	 *  'position' is the sort order of field.

-	 *  'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).

-	 *  'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing)

-	 *  'noteditable' says if field is not editable (1 or 0)

-	 *  'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created.

-	 *  'index' if we want an index in database.

-	 *  'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).

-	 *  'searchall' is 1 if we want to search in this field when making a search from the quick search button.

-	 *  'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).

-	 *  'css' is the CSS style to use on field. For example: 'maxwidth200'

-	 *  'help' is a string visible as a tooltip on field

-	 *  'showoncombobox' if value of the field must be visible into the label of the combobox that list record

-	 *  'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code.

-	 *  'arraykeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")

-	 *  'comment' is not used. You can store here any text of your choice. It is not used by application.

-	 *

-	 *  Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.

-	 */

 

 	// BEGIN MODULEBUILDER PROPERTIES

 	/**

@@ -92,38 +68,36 @@
 		'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>10),

 		'datec' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'position'=>15),

 		'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>20),

-		'fk_soc' =>array('type'=>'integer', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>1, 'position'=>25, 'searchall'=>1),

-		'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>3, 'notnull'=>1, 'position'=>30, 'index'=>1),

-		'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'Ref ext', 'enabled'=>1, 'visible'=>3, 'position'=>35),

-		'civility' =>array('type'=>'varchar(6)', 'label'=>'Civility', 'enabled'=>1, 'visible'=>3, 'position'=>40),

-		'lastname' =>array('type'=>'varchar(50)', 'label'=>'Lastname', 'enabled'=>1, 'visible'=>1, 'position'=>45, 'showoncombobox'=>1, 'searchall'=>1),

-		'firstname' =>array('type'=>'varchar(50)', 'label'=>'Firstname', 'enabled'=>1, 'visible'=>1, 'position'=>50, 'showoncombobox'=>1, 'searchall'=>1),

+		'fk_soc' =>array('type'=>'integer', 'label'=>'ThirdParty', 'enabled'=>1, 'visible'=>-1, 'position'=>25),

+		'entity' =>array('type'=>'integer', 'label'=>'Entity', 'default'=>1, 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'position'=>30, 'index'=>1),

+		'ref_ext' =>array('type'=>'varchar(255)', 'label'=>'Ref ext', 'enabled'=>1, 'visible'=>0, 'position'=>35),

+		'civility' =>array('type'=>'varchar(6)', 'label'=>'Civility', 'enabled'=>1, 'visible'=>-1, 'position'=>40),

+		'lastname' =>array('type'=>'varchar(50)', 'label'=>'Lastname', 'enabled'=>1, 'visible'=>-1, 'position'=>45, 'showoncombobox'=>1),

+		'firstname' =>array('type'=>'varchar(50)', 'label'=>'Firstname', 'enabled'=>1, 'visible'=>-1, 'position'=>50, 'showoncombobox'=>1),

 		'address' =>array('type'=>'varchar(255)', 'label'=>'Address', 'enabled'=>1, 'visible'=>-1, 'position'=>55),

-		'zip' =>array('type'=>'varchar(25)', 'label'=>'Zip', 'enabled'=>1, 'visible'=>1, 'position'=>60),

-		'town' =>array('type'=>'text', 'label'=>'Town', 'enabled'=>1, 'visible'=>1, 'position'=>65),

-		'fk_departement' =>array('type'=>'integer', 'label'=>'Fk departement', 'enabled'=>1, 'visible'=>3, 'position'=>70),

-		'fk_pays' =>array('type'=>'integer', 'label'=>'Fk pays', 'enabled'=>1, 'visible'=>3, 'position'=>75),

-		'birthday' =>array('type'=>'date', 'label'=>'Birthday', 'enabled'=>1, 'visible'=>3, 'position'=>80),

+		'zip' =>array('type'=>'varchar(25)', 'label'=>'Zip', 'enabled'=>1, 'visible'=>-1, 'position'=>60),

+		'town' =>array('type'=>'text', 'label'=>'Town', 'enabled'=>1, 'visible'=>-1, 'position'=>65),

+		'fk_departement' =>array('type'=>'integer', 'label'=>'Fk departement', 'enabled'=>1, 'visible'=>-1, 'position'=>70),

+		'fk_pays' =>array('type'=>'integer', 'label'=>'Fk pays', 'enabled'=>1, 'visible'=>-1, 'position'=>75),

+		'birthday' =>array('type'=>'date', 'label'=>'Birthday', 'enabled'=>1, 'visible'=>-1, 'position'=>80),

 		'poste' =>array('type'=>'varchar(80)', 'label'=>'PostOrFunction', 'enabled'=>1, 'visible'=>-1, 'position'=>85),

-		'phone' =>array('type'=>'varchar(30)', 'label'=>'Phone', 'enabled'=>1, 'visible'=>1, 'position'=>90, 'searchall'=>1),

-		'phone_perso' =>array('type'=>'varchar(30)', 'label'=>'PhonePerso', 'enabled'=>1, 'visible'=>1, 'position'=>95, 'searchall'=>1),

-		'phone_mobile' =>array('type'=>'varchar(30)', 'label'=>'PhoneMobile', 'enabled'=>1, 'visible'=>1, 'position'=>100, 'searchall'=>1),

-		'fax' =>array('type'=>'varchar(30)', 'label'=>'Fax', 'enabled'=>1, 'visible'=>1, 'position'=>105, 'searchall'=>1),

-		'email' =>array('type'=>'varchar(255)', 'label'=>'Email', 'enabled'=>1, 'visible'=>1, 'position'=>110, 'searchall'=>1),

-		'socialnetworks' =>array('type'=>'text', 'label'=>'SocialNetworks', 'enabled'=>1, 'visible'=>3, 'position'=>115),

-		'photo' =>array('type'=>'varchar(255)', 'label'=>'Photo', 'enabled'=>1, 'visible'=>3, 'position'=>170),

-		'priv' =>array('type'=>'smallint(6)', 'label'=>'ContactVisibility', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>175),

-		'fk_stcommcontact' =>array('type'=>'integer', 'label'=>'Fk stcommcontact', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>220),

-		'fk_prospectlevel' =>array('type'=>'varchar(12)', 'label'=>'ProspectLevel', 'enabled'=>1, 'visible'=>-1, 'position'=>255),

-		'no_email' =>array('type'=>'smallint(6)', 'label'=>'No_Email', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>180),

-		'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>3, 'position'=>185),

-		'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>3, 'position'=>190),

-		'note_private' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>3, 'position'=>195, 'searchall'=>1),

-		'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>3, 'position'=>200, 'searchall'=>1),

-		'default_lang' =>array('type'=>'varchar(6)', 'label'=>'Default lang', 'enabled'=>1, 'visible'=>3, 'position'=>205),

-		'canvas' =>array('type'=>'varchar(32)', 'label'=>'Canvas', 'enabled'=>1, 'visible'=>3, 'position'=>210),

-		'statut' =>array('type'=>'tinyint(4)', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>500),

-		'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-1, 'position'=>1000),

+		'phone' =>array('type'=>'varchar(30)', 'label'=>'Phone', 'enabled'=>1, 'visible'=>-1, 'position'=>90),

+		'phone_perso' =>array('type'=>'varchar(30)', 'label'=>'Phone perso', 'enabled'=>1, 'visible'=>-1, 'position'=>95),

+		'phone_mobile' =>array('type'=>'varchar(30)', 'label'=>'Phone mobile', 'enabled'=>1, 'visible'=>-1, 'position'=>100),

+		'fax' =>array('type'=>'varchar(30)', 'label'=>'Fax', 'enabled'=>1, 'visible'=>-1, 'position'=>105),

+		'email' =>array('type'=>'varchar(255)', 'label'=>'Email', 'enabled'=>1, 'visible'=>-1, 'position'=>110),

+		'socialnetworks' =>array('type'=>'text', 'label'=>'SocialNetworks', 'enabled'=>1, 'visible'=>-1, 'position'=>115),

+		'photo' =>array('type'=>'varchar(255)', 'label'=>'Photo', 'enabled'=>1, 'visible'=>-1, 'position'=>170),

+		'priv' =>array('type'=>'smallint(6)', 'label'=>'Private', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>175),

+		'no_email' =>array('type'=>'smallint(6)', 'label'=>'No email', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>180),

+		'fk_user_creat' =>array('type'=>'integer', 'label'=>'UserAuthor', 'enabled'=>1, 'visible'=>-1, 'position'=>185),

+		'fk_user_modif' =>array('type'=>'integer', 'label'=>'UserModif', 'enabled'=>1, 'visible'=>-1, 'position'=>190),

+		'note_private' =>array('type'=>'text', 'label'=>'NotePrivate', 'enabled'=>1, 'visible'=>0, 'position'=>195),

+		'note_public' =>array('type'=>'text', 'label'=>'NotePublic', 'enabled'=>1, 'visible'=>0, 'position'=>200),

+		'default_lang' =>array('type'=>'varchar(6)', 'label'=>'Default lang', 'enabled'=>1, 'visible'=>-1, 'position'=>205),

+		'canvas' =>array('type'=>'varchar(32)', 'label'=>'Canvas', 'enabled'=>1, 'visible'=>-1, 'position'=>210),

+		'statut' =>array('type'=>'tinyint(4)', 'label'=>'Statut', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500),

+		'import_key' =>array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>1, 'visible'=>-2, 'position'=>1000),

 	);

 

 	public $civility_id; // In fact we store civility_code

@@ -131,106 +105,80 @@
 	public $civility;

 

 	/**

-	 * @var string Address

+	 * Address

+	 * @var string

 	 */

 	public $address;

-

-	/**

-	 * @var string zip code

-	 */

 	public $zip;

-

-	/**

-	 * @var string Town

-	 */

 	public $town;

 

 	public $state_id; // Id of department

 	public $state_code; // Code of department

 	public $state; // Label of department

 

-	public $poste; // Position

+    public $poste; // Position

 

 	public $socid; // fk_soc

 	public $statut; // 0=inactif, 1=actif

 

 	public $code;

 

-	/**

-	 * Email

-	 * @var string

-	 */

+    /**

+     * Email

+     * @var string

+     */

 	public $email;

 

-	/**

-	 * Unsuscribe all : 1 = contact has globaly unsubscribe of all mass emailings

+    /**

+     * Unsuscribe all : 1 = contact has globaly unsubscribe of all mass emailings

 	 * @var int

-	 */

+     */

 	public $no_email;

 

-	/**

-	 * @var array array of socialnetworks

-	 */

-	public $socialnetworks;

-

-	/**

-	 * Skype username

-	 * @var string

-	 * @deprecated

-	 */

+    /**

+     * @var array array of socialnetworks

+     */

+    public $socialnetworks;

+

+    /**

+     * Skype username

+     * @var string

+     * @deprecated

+     */

 	public $skype;

 

 	/**

-	 * Twitter username

-	 * @var string

-	 * @deprecated

-	 */

+     * Twitter username

+     * @var string

+     * @deprecated

+     */

 	public $twitter;

 

 	 /**

-	  * Facebook username

-	  * @var string

-	  * @deprecated

-	  */

+     * Facebook username

+     * @var string

+     * @deprecated

+     */

 	public $facebook;

 

 	 /**

-	  * Linkedin username

-	  * @var string

-	  * @deprecated

-	  */

-	public $linkedin;

-

-	/**

-	 * Jabber username

-	 * @var string

-	 * @deprecated

-	 */

+     * Linkedin username

+     * @var string

+     * @deprecated

+     */

+    public $linkedin;

+

+    /**

+     * Jabber username

+     * @var string

+     * @deprecated

+     */

 	public $jabberid;

 

-	/**

-	 * @var string filename for photo

-	 */

 	public $photo;

-

-	/**

-	 * @var string phone pro

-	 */

 	public $phone_pro;

-

-	/**

-	 * @var string phone perso

-	 */

 	public $phone_perso;

-

-	/**

-	 * @var string phone mobile

-	 */

 	public $phone_mobile;

-

-	/**

-	 * @var string fax

-	 */

 	public $fax;

 

 	/**

@@ -253,19 +201,14 @@
 	// END MODULEBUILDER PROPERTIES

 

 

-	/**

-	 * Old copy

-	 * @var Contact

-	 */

+    /**

+     * Old copy

+     * @var Contact

+     */

 	public $oldcopy; // To contains a clone of this when we need to save old properties of object

 

-	public $roles = null;

-

-	public $cacheprospectstatus = array();

-	public $fk_prospectlevel;

-	public $stcomm_id;

-	public $statut_commercial;

-	public $stcomm_picto;

+	public $roles = array();

+

 

 	/**

 	 *	Constructor

@@ -277,28 +220,17 @@
 		global $conf, $langs;

 

 		$this->db = $db;

+

+		if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID) && isset($this->fields['rowid'])) $this->fields['rowid']['visible'] = 0;

+		if (empty($conf->mailing->enabled)) $this->fields['no_email']['enabled'] = 0;

+		if (!empty($conf->global->SOCIETE_DISABLE_CONTACTS)) $this->fields['thirdparty']['enabled'] = 0;

 		$this->statut = 1; // By default, status is enabled

 

-		if (empty($conf->global->MAIN_SHOW_TECHNICAL_ID)) {

-			$this->fields['rowid']['visible'] = 0;

-		}

-		if (empty($conf->mailing->enabled)) {

-			$this->fields['no_email']['enabled'] = 0;

-		}

-		// typical ['s.nom'] is used for third-parties

-		if (empty($conf->global->SOCIETE_DISABLE_CONTACTS)) {

-			$this->fields['fk_soc']['enabled'] = 0;

-			$this->fields['fk_soc']['searchall'] = 0;

-		}

-

-		if (empty($conf->global->THIRDPARTY_ENABLE_PROSPECTION_ON_ALTERNATIVE_ADRESSES)) {	// Default behaviour

-			$this->field['fk_stcommcontact']['enabled'] = 0;

-			$this->field['fk_prospectcontactlevel']['enabled'] = 0;

-		}

-

 		// Unset fields that are disabled

-		foreach ($this->fields as $key => $val) {

-			if (isset($val['enabled']) && empty($val['enabled'])) {

+		foreach ($this->fields as $key => $val)

+		{

+			if (isset($val['enabled']) && empty($val['enabled']))

+			{

 				unset($this->fields[$key]);

 			}

 		}

@@ -308,7 +240,7 @@
 		{

 			foreach($this->fields as $key => $val)

 			{

-				if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval']))

+				if (is_array($val['arrayofkeyval']))

 				{

 					foreach($val['arrayofkeyval'] as $key2 => $val2)

 					{

@@ -319,7 +251,7 @@
 		}*/

 	}

 

-	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

+    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

 	/**

 	 *  Load indicators into this->nb for board

 	 *

@@ -327,7 +259,7 @@
 	 */

 	public function load_state_board()

 	{

-		// phpcs:enable

+        // phpcs:enable

 		global $user;

 

 		$this->nb = array();

@@ -337,14 +269,14 @@
 		$sql .= " FROM ".MAIN_DB_PREFIX."socpeople as sp";

 		if (!$user->rights->societe->client->voir && !$user->socid)

 		{

-			$sql .= ", ".MAIN_DB_PREFIX."societe as s";

-			$sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";

+		    $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;

 			$clause = "AND";

 		}

 		$sql .= ' '.$clause.' sp.entity IN ('.getEntity($this->element).')';

 		$sql .= " AND (sp.priv='0' OR (sp.priv='1' AND sp.fk_user_creat=".$user->id."))";

-		if ($user->socid > 0) $sql .= " AND sp.fk_soc = ".$user->socid;

+        if ($user->socid > 0) $sql .= " AND sp.fk_soc = ".$user->socid;

 

 		$resql = $this->db->query($sql);

 		if ($resql)

@@ -355,7 +287,9 @@
 			}

 			$this->db->free($resql);

 			return 1;

-		} else {

+		}

+		else

+		{

 			dol_print_error($this->db);

 			$this->error = $this->db->lasterror();

 			return -1;

@@ -379,9 +313,11 @@
 

 		// Clean parameters

 		$this->lastname = $this->lastname ?trim($this->lastname) : trim($this->name);

-		$this->firstname = trim($this->firstname);

-		$this->setUpperOrLowerCase();

-		if (empty($this->socid)) $this->socid = 0;

+        $this->firstname = trim($this->firstname);

+        if (!empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->lastname = ucwords(strtolower($this->lastname));

+		if (!empty($conf->global->MAIN_ALL_TO_UPPER)) $this->lastname = strtoupper($this->lastname);

+        if (!empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->firstname = ucwords(strtolower($this->firstname));

+        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

 

@@ -390,11 +326,10 @@
 		$sql = "INSERT INTO ".MAIN_DB_PREFIX."socpeople (";

 		$sql .= " datec";

 		$sql .= ", fk_soc";

-		$sql .= ", lastname";

-		$sql .= ", firstname";

-		$sql .= ", fk_user_creat";

+        $sql .= ", lastname";

+        $sql .= ", firstname";

+        $sql .= ", fk_user_creat";

 		$sql .= ", priv";

-		$sql .= ", fk_stcommcontact";

 		$sql .= ", statut";

 		$sql .= ", canvas";

 		$sql .= ", entity";

@@ -405,15 +340,14 @@
 		if ($this->socid > 0) $sql .= " ".$this->db->escape($this->socid).",";

 		else $sql .= "null,";

 		$sql .= "'".$this->db->escape($this->lastname)."',";

-		$sql .= "'".$this->db->escape($this->firstname)."',";

-		$sql .= " ".($user->id > 0 ? "'".$this->db->escape($user->id)."'" : "null").",";

+        $sql .= "'".$this->db->escape($this->firstname)."',";

+        $sql .= " ".($user->id > 0 ? "'".$this->db->escape($user->id)."'" : "null").",";

 		$sql .= " ".$this->db->escape($this->priv).",";

-		$sql .= " 0,";

 		$sql .= " ".$this->db->escape($this->statut).",";

-		$sql .= " ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null").",";

-		$sql .= " ".$this->db->escape($this->entity).",";

-		$sql .= "'".$this->db->escape($this->ref_ext)."',";

-		$sql .= " ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");

+        $sql .= " ".(!empty($this->canvas) ? "'".$this->db->escape($this->canvas)."'" : "null").",";

+        $sql .= " ".$this->db->escape($this->entity).",";

+        $sql .= "'".$this->db->escape($this->ref_ext)."',";

+        $sql .= " ".(!empty($this->import_key) ? "'".$this->db->escape($this->import_key)."'" : "null");

 		$sql .= ")";

 

 		dol_syslog(get_class($this)."::create", LOG_DEBUG);

@@ -424,42 +358,46 @@
 

 			if (!$error)

 			{

-				$result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...

-				if ($result < 0)

-				{

-					$error++;

-					$this->error = $this->db->lasterror();

-				}

+                $result = $this->update($this->id, $user, 1, 'add'); // This include updateRoles(), ...

+                if ($result < 0)

+                {

+                    $error++;

+				    $this->error = $this->db->lasterror();

+                }

 			}

 

 			if (!$error)

-			{

-				$result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update

-				if ($result < 0)

-				{

-					$error++;

-					$this->error = $this->db->lasterror();

-				}

-			}

-

-			if (!$error)

-			{

-				// Call trigger

-				$result = $this->call_trigger('CONTACT_CREATE', $user);

-				if ($result < 0) { $error++; }

-				// End call triggers

-			}

-

-			if (!$error)

-			{

-				$this->db->commit();

-				return $this->id;

-			} else {

-				$this->db->rollback();

-				dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);

-				return -2;

-			}

-		} else {

+            {

+                $result = $this->update_perso($this->id, $user, 1); // TODO Remove function update_perso, should be same than update

+                if ($result < 0)

+                {

+                    $error++;

+                    $this->error = $this->db->lasterror();

+                }

+            }

+

+            if (!$error)

+            {

+                // Call trigger

+                $result = $this->call_trigger('CONTACT_CREATE', $user);

+                if ($result < 0) { $error++; }

+                // End call triggers

+            }

+

+            if (!$error)

+            {

+                $this->db->commit();

+                return $this->id;

+            }

+            else

+            {

+                $this->db->rollback();

+                dol_syslog(get_class($this)."::create ".$this->error, LOG_ERR);

+                return -2;

+            }

+		}

+		else

+		{

 			$this->error = $this->db->lasterror();

 

 			$this->db->rollback();

@@ -489,6 +427,10 @@
 		$this->entity = ((isset($this->entity) && is_numeric($this->entity)) ? $this->entity : $conf->entity);

 

 		// Clean parameters

+		if (!empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->lastname = ucwords(strtolower($this->lastname));

+		if (!empty($conf->global->MAIN_ALL_TO_UPPER)) $this->lastname = strtoupper($this->lastname);

+        if (!empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->firstname = ucwords(strtolower($this->firstname));

+

 		$this->lastname = trim($this->lastname) ?trim($this->lastname) : trim($this->lastname);

 		$this->firstname = trim($this->firstname);

 		$this->email = trim($this->email);

@@ -499,9 +441,8 @@
 		$this->skype = trim($this->skype);

 		$this->photo = trim($this->photo);

 		$this->fax = trim($this->fax);

-		$this->zip = (empty($this->zip) ? '' : trim($this->zip));

-		$this->town = (empty($this->town) ? '' : trim($this->town));

-		$this->setUpperOrLowerCase();

+		$this->zip = (empty($this->zip) ? '' : $this->zip);

+		$this->town = (empty($this->town) ? '' : $this->town);

 		$this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);

 		if (empty($this->statut)) $this->statut = 0;

 		if (empty($this->civility_code) && !is_numeric($this->civility_id)) $this->civility_code = $this->civility_id; // For backward compatibility

@@ -521,7 +462,7 @@
 		$sql .= ", poste='".$this->db->escape($this->poste)."'";

 		$sql .= ", fax='".$this->db->escape($this->fax)."'";

 		$sql .= ", email='".$this->db->escape($this->email)."'";

-		$sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";

+        $sql .= ", socialnetworks = '".$this->db->escape(json_encode($this->socialnetworks))."'";

 		$sql .= ", photo='".$this->db->escape($this->photo)."'";

 		$sql .= ", birthday=".($this->birthday ? "'".$this->db->idate($this->birthday)."'" : "null");

 		$sql .= ", note_private = ".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null");

@@ -530,11 +471,6 @@
 		$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 .= ", priv = '".$this->db->escape($this->priv)."'";

-		$sql .= ", fk_prospectcontactlevel = '".$this->db->escape($this->fk_prospectlevel)."'";

-		if (isset($this->stcomm_id))

-		{

-			$sql .= ", fk_stcommcontact = ".($this->stcomm_id > 0 || $this->stcomm_id == -1 ? $this->stcomm_id : "0");

-		}

 		$sql .= ", statut = ".$this->db->escape($this->statut);

 		$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");

@@ -545,22 +481,22 @@
 		$result = $this->db->query($sql);

 		if ($result)

 		{

-			unset($this->country_code);

-			unset($this->country);

-			unset($this->state_code);

-			unset($this->state);

-

-			$action = 'update';

-

-			// Actions on extra fields

-			if (!$error)

-			{

-				$result = $this->insertExtraFields();

-				if ($result < 0)

-				{

-					$error++;

-				}

-			}

+		    unset($this->country_code);

+		    unset($this->country);

+		    unset($this->state_code);

+		    unset($this->state);

+

+		    $action = 'update';

+

+		    // Actions on extra fields

+		    if (!$error)

+		    {

+		    	$result = $this->insertExtraFields();

+		    	if ($result < 0)

+		    	{

+		    		$error++;

+		    	}

+		    }

 

 			if (!$error) {

 				$result = $this->updateRoles();

@@ -660,21 +596,25 @@
 			{

 				$this->db->commit();

 				return 1;

-			} else {

+			}

+			else

+			{

 				dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);

 				$this->db->rollback();

 				return -$error;

 			}

-		} else {

+		}

+		else

+		{

 			$this->error = $this->db->lasterror().' sql='.$sql;

-			$this->db->rollback();

+            $this->db->rollback();

 			return -1;

 		}

 	}

 

 

-	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

-	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore

+    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

+    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore

 	/**

 	 *	Retourne chaine DN complete dans l'annuaire LDAP pour l'objet

 	 *

@@ -686,7 +626,7 @@
 	 */

 	public function _load_ldap_dn($info, $mode = 0)

 	{

-		// phpcs:enable

+        // phpcs:enable

 		global $conf;

 		$dn = '';

 		if ($mode == 0) $dn = $conf->global->LDAP_KEY_CONTACTS."=".$info[$conf->global->LDAP_KEY_CONTACTS].",".$conf->global->LDAP_CONTACT_DN;

@@ -696,8 +636,8 @@
 	}

 

 

-	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

-	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore

+    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

+    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore

 	/**

 	 *	Initialise tableau info (tableau des attributs LDAP)

 	 *

@@ -705,12 +645,12 @@
 	 */

 	public function _load_ldap_info()

 	{

-		// phpcs:enable

+        // phpcs:enable

 		global $conf, $langs;

 

-		$info = array();

-

-		// Object classes

+        $info = array();

+

+        // Object classes

 		$info["objectclass"] = explode(',', $conf->global->LDAP_CONTACT_OBJECT_CLASS);

 

 		$this->fullname = $this->getFullName($langs);

@@ -739,8 +679,8 @@
 		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;

-		if ($this->skype && !empty($conf->global->LDAP_CONTACT_FIELD_SKYPE))	    $info[$conf->global->LDAP_CONTACT_FIELD_SKYPE] = $this->skype;

-		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);

+        if ($this->skype && !empty($conf->global->LDAP_CONTACT_FIELD_SKYPE))	    $info[$conf->global->LDAP_CONTACT_FIELD_SKYPE] = $this->skype;

+        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);

 		if ($this->email && !empty($conf->global->LDAP_CONTACT_FIELD_MAIL))     $info[$conf->global->LDAP_CONTACT_FIELD_MAIL] = $this->email;

 

 		if ($conf->global->LDAP_SERVER_TYPE == 'egroupware')

@@ -772,22 +712,22 @@
 	}

 

 

-	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

+    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

 	/**

 	 *  Update field alert birthday

 	 *

 	 *  @param      int			$id         Id of contact

 	 *  @param      User		$user		User asking to change alert or birthday

 	 *  @param      int		    $notrigger	0=no, 1=yes

-	 *  @return     int         			<0 if KO, >=0 if OK

+     *  @return     int         			<0 if KO, >=0 if OK

 	 */

 	public function update_perso($id, $user = null, $notrigger = 0)

 	{

-		// phpcs:enable

-		$error = 0;

-		$result = false;

-

-		$this->db->begin();

+        // phpcs:enable

+	    $error = 0;

+	    $result = false;

+

+	    $this->db->begin();

 

 		// Mis a jour contact

 		$sql = "UPDATE ".MAIN_DB_PREFIX."socpeople SET";

@@ -805,46 +745,58 @@
 		}

 

 		// Mis a jour alerte birthday

-		if (!empty($this->birthday_alert)) {

+		if ($this->birthday_alert)

+		{

 			//check existing

 			$sql_check = "SELECT rowid FROM ".MAIN_DB_PREFIX."user_alert WHERE type=1 AND fk_contact=".$this->db->escape($id)." AND fk_user=".$user->id;

 			$result_check = $this->db->query($sql_check);

-			if (!$result_check || ($this->db->num_rows($result_check) < 1)) {

+			if (!$result_check || ($this->db->num_rows($result_check) < 1))

+			{

 				//insert

 				$sql = "INSERT INTO ".MAIN_DB_PREFIX."user_alert(type,fk_contact,fk_user) ";

 				$sql .= "VALUES (1,".$this->db->escape($id).",".$user->id.")";

 				$result = $this->db->query($sql);

-				if (!$result) {

-					$error++;

-					$this->error = $this->db->lasterror();

-				}

-			} else {

+				if (!$result)

+				{

+                    $error++;

+                    $this->error = $this->db->lasterror();

+				}

+			}

+			else

+			{

 				$result = true;

 			}

-		} else {

+		}

+		else

+		{

 			$sql = "DELETE FROM ".MAIN_DB_PREFIX."user_alert ";

 			$sql .= "WHERE type=1 AND fk_contact=".$this->db->escape($id)." AND fk_user=".$user->id;

 			$result = $this->db->query($sql);

-			if (!$result) {

-				$error++;

-				$this->error = $this->db->lasterror();

-			}

-		}

-

-		if (!$error && !$notrigger) {

-			// Call trigger

-			$result = $this->call_trigger('CONTACT_MODIFY', $user);

-			if ($result < 0) { $error++; }

-			// End call triggers

-		}

-

-		if (!$error) {

-			$this->db->commit();

-			return 1;

-		} else {

-			dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);

-			$this->db->rollback();

-			return -$error;

+			if (!$result)

+			{

+                $error++;

+                $this->error = $this->db->lasterror();

+			}

+		}

+

+		if (!$error && !$notrigger)

+		{

+		    // Call trigger

+		    $result = $this->call_trigger('CONTACT_MODIFY', $user);

+		    if ($result < 0) { $error++; }

+		    // End call triggers

+		}

+

+		if (!$error)

+		{

+		    $this->db->commit();

+		    return 1;

+		}

+		else

+		{

+		    dol_syslog(get_class($this)."::update Error ".$this->error, LOG_ERR);

+		    $this->db->rollback();

+		    return -$error;

 		}

 	}

 

@@ -854,9 +806,9 @@
 	 *

 	 *  @param      int		$id         	Id of contact

 	 *  @param      User	$user       	Load also alerts of this user (subscribing to alerts) that want alerts about this contact

-	 *  @param      string  $ref_ext    	External reference, not given by Dolibarr

-	 *  @param		string	$email			Email

-	 *  @param		int		$loadalsoroles	Load also roles. Try to always 0 here and load roles with a separate call of fetchRoles().

+     *  @param      string  $ref_ext    	External reference, not given by Dolibarr

+     *  @param		string	$email			Email

+     *  @param		int		$loadalsoroles	Load also roles

 	 *  @return     int     		    	>0 if OK, <0 if KO or if two records found for same ref or idprof, 0 if not found.

 	 */

 	public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0)

@@ -880,9 +832,8 @@
 		$sql .= " c.birthday,";

 		$sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";

 		$sql .= " c.socialnetworks,";

-		$sql .= " c.photo,";

+        $sql .= " c.photo,";

 		$sql .= " c.priv, c.note_private, c.note_public, c.default_lang, c.canvas,";

-		$sql .= " c.fk_prospectcontactlevel, c.fk_stcommcontact, st.libelle as stcomm, st.picto as stcomm_picto,";

 		$sql .= " c.import_key,";

 		$sql .= " c.datec as date_creation, c.tms as date_modification,";

 		$sql .= " co.label as country, co.code as country_code,";

@@ -894,9 +845,9 @@
 		$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";

 		$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 .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_stcommcontact as st ON c.fk_stcommcontact = st.id';

-		if ($id) $sql .= " WHERE c.rowid = ".((int) $id);

-		else {

+		if ($id) $sql .= " WHERE c.rowid = ".$id;

+		else

+		{

 			$sql .= " WHERE c.entity IN (".getEntity($this->element).")";

 			if ($ref_ext) {

 				$sql .= " AND c.ref_ext = '".$this->db->escape($ref_ext)."'";

@@ -916,7 +867,8 @@
 				dol_syslog($this->error, LOG_ERR);

 

 				return 2;

-			} elseif ($num)   // $num = 1

+			}

+			elseif ($num)   // $num = 1

 			{

 				$obj = $this->db->fetch_object($resql);

 

@@ -928,7 +880,7 @@
 				$this->civility_code    = $obj->civility_code;

 				$this->civility	        = $obj->civility_code ? ($langs->trans("Civility".$obj->civility_code) != ("Civility".$obj->civility_code) ? $langs->trans("Civility".$obj->civility_code) : $obj->civility_code) : '';

 

-				$this->lastname = $obj->lastname;

+                $this->lastname = $obj->lastname;

 				$this->firstname = $obj->firstname;

 				$this->address = $obj->address;

 				$this->zip = $obj->zip;

@@ -949,14 +901,6 @@
 				$this->socname			= $obj->socname;

 				$this->poste			= $obj->poste;

 				$this->statut = $obj->statut;

-

-				$this->fk_prospectlevel = $obj->fk_prospectcontactlevel;

-

-				$transcode = $langs->trans('StatusProspect'.$obj->fk_stcommcontact);

-				$libelle = ($transcode != 'StatusProspect'.$obj->fk_stcommcontact ? $transcode : $obj->stcomm);

-				$this->stcomm_id = $obj->fk_stcommcontact; // id statut commercial

-				$this->statut_commercial = $libelle; // libelle statut commercial

-				$this->stcomm_picto = $obj->stcomm_picto; // Picto statut commercial

 

 				$this->phone_pro = trim($obj->phone);

 				$this->fax = trim($obj->fax);

@@ -998,12 +942,14 @@
 						$this->user_id = $uobj->rowid;

 					}

 					$this->db->free($resql);

-				} else {

+				}

+				else

+				{

 					$this->error = $this->db->error();

 					return -1;

 				}

 

-				// Retrieve all extrafield

+				// Retreive all extrafield

 				// fetch optionals attributes and labels

 				$this->fetch_optionals();

 

@@ -1024,7 +970,9 @@
 							$this->birthday_alert = 1;

 						}

 						$this->db->free($resql);

-					} else {

+					}

+					else

+					{

 						$this->error = $this->db->error();

 						return -1;

 					}

@@ -1039,11 +987,15 @@
 				}

 

 				return 1;

-			} else {

+			}

+			else

+			{

 				$this->error = $langs->trans("RecordNotFound");

 				return 0;

 			}

-		} else {

+		}

+		else

+		{

 			$this->error = $this->db->error();

 			return -1;

 		}

@@ -1064,13 +1016,14 @@
 		if (in_array($this->civility_id, array('MR')) || in_array($this->civility_code, array('MR')))

 		{

 			$this->gender = 'man';

-		} elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE')))

+		}

+		elseif (in_array($this->civility_id, array('MME', 'MLE')) || in_array($this->civility_code, array('MME', 'MLE')))

 		{

 			$this->gender = 'woman';

 		}

 	}

 

-	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

+    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

 	/**

 	 *  Load number of elements the contact is used as a link for

 	 *  ref_facturation

@@ -1078,11 +1031,11 @@
 	 *  ref_commande (for order and/or shipments)

 	 *  ref_propale

 	 *

-	 *  @return     int             					<0 if KO, >=0 if OK

+     *  @return     int             					<0 if KO, >=0 if OK

 	 */

 	public function load_ref_elements()

 	{

-		// phpcs:enable

+        // phpcs:enable

 		// 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";

@@ -1108,7 +1061,9 @@
 			}

 			$this->db->free($resql);

 			return 0;

-		} else {

+		}

+		else

+		{

 			$this->error = $this->db->lasterror();

 			return -1;

 		}

@@ -1160,7 +1115,9 @@
 

 					$i++;

 				}

-			} else {

+			}

+			else

+			{

 				$error++;

 				$this->error = $this->db->error().' sql='.$sql;

 			}

@@ -1208,25 +1165,27 @@
 		}

 

 		// Removed extrafields

-		if (!$error) {

-			// For avoid conflicts if trigger used

+        if (!$error) {

+            // For avoid conflicts if trigger used

 			$result = $this->deleteExtraFields();

 			if ($result < 0) $error++;

 		}

 

 		if (!$error && !$notrigger)

 		{

-			// Call trigger

-			$result = $this->call_trigger('CONTACT_DELETE', $user);

-			if ($result < 0) { $error++; }

-			// End call triggers

+            // Call trigger

+            $result = $this->call_trigger('CONTACT_DELETE', $user);

+            if ($result < 0) { $error++; }

+            // End call triggers

 		}

 

 		if (!$error)

 		{

 			$this->db->commit();

 			return 1;

-		} else {

+		}

+		else

+		{

 			$this->db->rollback();

 			dol_syslog("Error ".$this->error, LOG_ERR);

 			return -1;

@@ -1273,7 +1232,9 @@
 			}

 

 			$this->db->free($resql);

-		} else {

+		}

+		else

+		{

 			print $this->db->error();

 		}

 	}

@@ -1298,7 +1259,9 @@
 

 			$this->db->free($resql);

 			return $nb;

-		} else {

+		}

+		else

+		{

 			$this->error = $this->db->error();

 			return -1;

 		}

@@ -1312,7 +1275,7 @@
 	 *	@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

+     *  @param      int     	$save_lastsearch_value		-1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking

 	 *	@param		int			$notooltip					1=Disable tooltip

 	 *	@return		string									String with URL

 	 */

@@ -1329,43 +1292,41 @@
 			$label .= '</div><div style="clear: both;"></div>';

 		}

 

-		$label .= img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Contact").'</u>';

-		$label .= ' '.$this->getLibStatut(4);

-		$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();

-		$country_code = empty($this->country_code) ? '': $this->country_code;

-		if ($this->phone_pro) $phonelist[] = dol_print_phone($this->phone_pro, $country_code, $this->id, 0, '', '&nbsp;', 'phone');

-		if ($this->phone_mobile) $phonelist[] = dol_print_phone($this->phone_mobile, $country_code, $this->id, 0, '', '&nbsp;', 'mobile');

-		if ($this->phone_perso) $phonelist[] = dol_print_phone($this->phone_perso, $country_code, $this->id, 0, '', '&nbsp;', 'phone');

-		$label .= '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $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 = "";

+        $label .= '<u>'.$langs->trans("Contact").'</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[] = dol_print_phone($this->phone_pro, $this->country_code, $this->id, 0, '', '&nbsp;', 'phone');

+        if ($this->phone_mobile) $phonelist[] = dol_print_phone($this->phone_mobile, $this->country_code, $this->id, 0, '', '&nbsp;', 'mobile');

+        if ($this->phone_perso) $phonelist[] = dol_print_phone($this->phone_perso, $this->country_code, $this->id, 0, '', '&nbsp;', 'phone');

+        $label .= '<br><b>'.$langs->trans("Phone").':</b> '.implode('&nbsp;', $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 = "";

 		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"';

-

-		   	/*

+	    	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"';

+

+    	   	/*

     	   	 $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

@@ -1425,7 +1386,7 @@
 		return $this->LibStatut($this->statut, $mode);

 	}

 

-	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

+    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

 	/**

 	 *	Renvoi le libelle d'un statut donne

 	 *

@@ -1435,7 +1396,7 @@
 	 */

 	public function LibStatut($status, $mode)

 	{

-		// phpcs:enable

+        // phpcs:enable

 		global $langs;

 

 		$labelStatus = array(

@@ -1461,7 +1422,7 @@
 	}

 

 

-	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

+    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

 	/**

 	 *	Return translated label of Public or Private

 	 *

@@ -1470,7 +1431,7 @@
 	 */

 	public function LibPubPriv($status)

 	{

-		// phpcs:enable

+        // phpcs:enable

 		global $langs;

 		if ($status == '1') return $langs->trans('ContactPrivate');

 		else return $langs->trans('ContactPublic');

@@ -1478,11 +1439,11 @@
 

 

 	/**

-	 *  Initialise an instance with random values.

-	 *  Used to build previews or test instances.

-	 *	id must be 0 if object instance is a specimen.

-	 *

-	 *  @return	int >0 if ok

+     *  Initialise an instance with random values.

+     *  Used to build previews or test instances.

+     *	id must be 0 if object instance is a specimen.

+     *

+     *  @return	int >0 if ok

 	 */

 	public function initAsSpecimen()

 	{

@@ -1497,7 +1458,6 @@
 

 		// Initialise parameters

 		$this->id = 0;

-		$this->entity = 1;

 		$this->specimen = 1;

 		$this->lastname = 'DOLIBARR';

 		$this->firstname = 'SPECIMEN';

@@ -1508,6 +1468,7 @@
 		$this->country_code = 'FR';

 		$this->country = 'France';

 		$this->email = 'specimen@specimen.com';

+		$this->skype = 'tom.hanson';

 		$this->socialnetworks = array(

 			'skype' => 'tom.hanson',

 		);

@@ -1551,17 +1512,19 @@
 		dol_syslog(get_class($this)."::setstatus", LOG_DEBUG);

 		if ($result)

 		{

-			// Call trigger

-			$result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);

-			if ($result < 0) { $error++; }

-			// End call triggers

+            // Call trigger

+            $result = $this->call_trigger('CONTACT_ENABLEDISABLE', $user);

+            if ($result < 0) { $error++; }

+            // End call triggers

 		}

 

 		if ($error)

 		{

 			$this->db->rollback();

 			return -$error;

-		} else {

+		}

+		else

+		{

 			$this->db->commit();

 			return 1;

 		}

@@ -1575,7 +1538,7 @@
 	 * Existing categories are left untouch.

 	 *

 	 * @param int[]|int $categories Category or categories IDs

-	 * @return void

+     * @return void

 	 */

 	public function setCategories($categories)

 	{

@@ -1631,11 +1594,10 @@
 	}

 

 	/**

-	 * Fetch roles (default contact of some companies) for the current contact.

-	 * This load the array ->roles.

-	 *

-	 * @return 	int			<0 if KO, Nb of roles found if OK

-	 * @see updateRoles()

+	 * Fetch Roles for a contact

+	 *

+	 * @return float|int

+	 * @throws Exception

 	 */

 	public function fetchRoles()

 	{

@@ -1643,23 +1605,25 @@
 		$error = 0;

 		$num = 0;

 

-		$sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle as label, sc.rowid as contactroleid, sc.fk_soc as socid";

-		$sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc, ".MAIN_DB_PREFIX."c_type_contact as tc";

-		$sql .= " WHERE tc.rowid = sc.fk_c_type_contact";

+		$sql = "SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle, sc.rowid as contactroleid";

+		$sql .= " FROM ".MAIN_DB_PREFIX."societe_contacts as sc";

+		$sql .= " INNER JOIN ".MAIN_DB_PREFIX."c_type_contact as tc";

+		$sql .= " ON tc.rowid = sc.fk_c_type_contact";

+		$sql .= " AND sc.fk_socpeople = ".$this->id;

 		$sql .= " AND tc.source = 'external' AND tc.active=1";

-		$sql .= " AND sc.fk_socpeople = ".$this->id;

 		$sql .= " AND sc.entity IN (".getEntity('societe').')';

 

+		dol_syslog(__METHOD__, LOG_DEBUG);

+

+		$this->roles = array();

 		$resql = $this->db->query($sql);

 		if ($resql) {

-			$this->roles = array();

-

 			$num = $this->db->num_rows($resql);

 			if ($num > 0) {

 				while ($obj = $this->db->fetch_object($resql)) {

 					$transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code;

 					$libelle_element = $langs->trans('ContactDefault_'.$obj->element);

-					$this->roles[$obj->contactroleid] = array('id'=>$obj->rowid, 'socid'=>$obj->socid, 'element'=>$obj->element, 'source'=>$obj->source, 'code'=>$obj->code, 'label'=>$libelle_element.' - '.($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->label));

+					$this->roles[$obj->contactroleid] = array('id'=>$obj->rowid, 'element'=>$obj->element, 'source'=>$obj->source, 'code'=>$obj->code, 'label'=>$libelle_element.' - '.($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->libelle));

 				}

 			}

 		} else {

@@ -1695,7 +1659,7 @@
 		$sql .= ", ".MAIN_DB_PREFIX."societe_contacts sc";

 		$sql .= " WHERE sc.fk_soc =".$this->socid;

 		$sql .= " AND sc.fk_c_type_contact=tc.rowid";

-		$sql .= " AND tc.element='".$this->db->escape($element)."'";

+		$sql .= " AND tc.element='".$element."'";

 		$sql .= " AND tc.active=1";

 

 		dol_syslog(__METHOD__, LOG_DEBUG);

@@ -1713,7 +1677,9 @@
 			}

 

 			return $tab;

-		} else {

+		}

+		else

+		{

 			$this->error = $this->db->error();

 			dol_print_error($this->db);

 			return -1;

@@ -1721,11 +1687,10 @@
 	}

 

 	/**

-	 * Updates all roles (default contact for companies) according to values inside the ->roles array.

-	 * This is called by update of contact.

+	 * Updates Roles

 	 *

 	 * @return float|int

-	 * @see fetchRoles()

+	 * @throws Exception

 	 */

 	public function updateRoles()

 	{

@@ -1733,12 +1698,11 @@
 

 		$error = 0;

 

-		if (!isset($this->roles)) return;	// Avoid to loose roles when property not set

-

 		$this->db->begin();

 

-		$sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".((int) $this->id)." AND entity IN (".getEntity("societe_contact").")";

-

+		$sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_soc=".$this->socid." AND fk_socpeople=".$this->id; ;

+

+		dol_syslog(__METHOD__, LOG_DEBUG);

 		$result = $this->db->query($sql);

 		if (!$result) {

 			$this->errors[] = $this->db->lasterror().' sql='.$sql;

@@ -1746,40 +1710,25 @@
 		} else {

 			if (count($this->roles) > 0) {

 				foreach ($this->roles as $keyRoles => $valRoles) {

-					$idrole = 0;

-					if (is_array($valRoles)) {

-						$idrole = $valRoles['id'];

-					} else {

-						$idrole = $valRoles;

-					}

-

-					$socid = 0;

-					if (is_array($valRoles)) {

-						$socid = $valRoles['socid'];

-					} else {

-						$socid = $this->socid;

-					}

-

-					if ($socid > 0) {

-						$sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";

-						$sql .= " (entity,";

-						$sql .= "date_creation,";

-						$sql .= "fk_soc,";

-						$sql .= "fk_c_type_contact,";

-						$sql .= "fk_socpeople) ";

-						$sql .= " VALUES (".$conf->entity.",";

-						$sql .= "'".$this->db->idate(dol_now())."',";

-						$sql .= $socid.", ";

-						$sql .= $idrole." , ";

-						$sql .= $this->id;

-						$sql .= ")";

-

-						$result = $this->db->query($sql);

-						if (!$result)

-						{

-							$this->errors[] = $this->db->lasterror().' sql='.$sql;

-							$error++;

-						}

+					$sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";

+					$sql .= " (entity,";

+					$sql .= "date_creation,";

+					$sql .= "fk_soc,";

+					$sql .= "fk_c_type_contact,";

+					$sql .= "fk_socpeople) ";

+					$sql .= " VALUES (".$conf->entity.",";

+					$sql .= "'".$this->db->idate(dol_now())."',";

+					$sql .= $this->socid.", ";

+					$sql .= $valRoles." , ";

+					$sql .= $this->id;

+					$sql .= ")";

+					dol_syslog(__METHOD__, LOG_DEBUG);

+

+					$result = $this->db->query($sql);

+					if (!$result)

+					{

+						$this->errors[] = $this->db->lasterror().' sql='.$sql;

+						$error++;

 					}

 				}

 			}

@@ -1793,137 +1742,4 @@
 			return $error * -1;

 		}

 	}

-

-	/**

-	 *  Load array of prospect status

-	 *

-	 *  @param	int		$active     1=Active only, 0=Not active only, -1=All

-	 *  @return int					<0 if KO, >0 if OK

-	 */

-	public function loadCacheOfProspStatus($active = 1)

-	{

-		global $langs;

-

-		$sql = "SELECT id, code, libelle as label, picto FROM ".MAIN_DB_PREFIX."c_stcommcontact";

-		if ($active >= 0) $sql .= " WHERE active = ".$active;

-		$resql = $this->db->query($sql);

-		$num = $this->db->num_rows($resql);

-		$i = 0;

-		while ($i < $num)

-		{

-			$obj = $this->db->fetch_object($resql);

-			$this->cacheprospectstatus[$obj->id] = array('id' => $obj->id, 'code' => $obj->code, 'label' => ($langs->trans("ST_".strtoupper($obj->code)) == "ST_".strtoupper($obj->code)) ? $obj->label : $langs->trans("ST_".strtoupper($obj->code)), 'picto' => $obj->picto);

-			$i++;

-		}

-		return 1;

-	}

-

-	/**

-	 *	Return prostect level

-	 *

-	 *  @return     string        Libelle

-	 */

-	public function getLibProspLevel()

-	{

-		return $this->libProspLevel($this->fk_prospectlevel);

-	}

-

-	/**

-	 *  Return label of prospect level

-	 *

-	 *  @param	int		$fk_prospectlevel   	Prospect level

-	 *  @return string        					label of level

-	 */

-	public function libProspLevel($fk_prospectlevel)

-	{

-		global $langs;

-

-		$lib = $langs->trans("ProspectLevel".$fk_prospectlevel);

-		// If lib not found in language file, we get label from cache/databse

-		if ($lib == $langs->trans("ProspectLevel".$fk_prospectlevel))

-		{

-			$lib = $langs->getLabelFromKey($this->db, $fk_prospectlevel, 'c_prospectlevel', 'code', 'label');

-		}

-		return $lib;

-	}

-

-

-	/**

-	 *  Set prospect level

-	 *

-	 *  @param  User	$user		Utilisateur qui definie la remise

-	 *	@return	int					<0 if KO, >0 if OK

-	 * @deprecated Use update function instead

-	 */

-	public function setProspectLevel(User $user)

-	{

-		return $this->update($this->id, $user);

-	}

-

-	/**

-	 *  Return status of prospect

-	 *

-	 *  @param	int		$mode       0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long

-	 *  @param	string	$label		Label to use for status for added status

-	 *  @return string        		Libelle

-	 */

-	public function getLibProspCommStatut($mode = 0, $label = '')

-	{

-		return $this->libProspCommStatut($this->stcomm_id, $mode, $label, $this->stcomm_picto);

-	}

-

-	/**

-	 *  Return label of a given status

-	 *

-	 *  @param	int|string	$statut        	Id or code for prospection status

-	 *  @param  int			$mode          	0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto

-	 *  @param	string		$label			Label to use for status for added status

-	 *	@param 	string		$picto      	Name of image file to show ('filenew', ...)

-	 *                                      If no extension provided, we use '.png'. Image must be stored into theme/xxx/img directory.

-	 *                                      Example: picto.png                  if picto.png is stored into htdocs/theme/mytheme/img

-	 *                                      Example: picto.png@mymodule         if picto.png is stored into htdocs/mymodule/img

-	 *                                      Example: /mydir/mysubdir/picto.png  if picto.png is stored into htdocs/mydir/mysubdir (pictoisfullpath must be set to 1)

-	 *  @return string       	 			Libelle du statut

-	 */

-	public function libProspCommStatut($statut, $mode = 0, $label = '', $picto = '')

-	{

-		global $langs;

-		$langs->load('customers');

-

-		if ($mode == 2)

-		{

-			if ($statut == '-1' || $statut == 'ST_NO') return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");

-			elseif ($statut == '0' || $statut == 'ST_NEVER') return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");

-			elseif ($statut == '1' || $statut == 'ST_TODO') return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");

-			elseif ($statut == '2' || $statut == 'ST_PEND') return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");

-			elseif ($statut == '3' || $statut == 'ST_DONE') return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");

-			else {

-				return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0, $picto).' '.(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label);

-			}

-		}

-		if ($mode == 3)

-		{

-			if ($statut == '-1' || $statut == 'ST_NO') return img_action($langs->trans("StatusProspect-1"), -1, $picto);

-			elseif ($statut == '0' || $statut == 'ST_NEVER') return img_action($langs->trans("StatusProspect0"), 0, $picto);

-			elseif ($statut == '1' || $statut == 'ST_TODO') return img_action($langs->trans("StatusProspect1"), 1, $picto);

-			elseif ($statut == '2' || $statut == 'ST_PEND') return img_action($langs->trans("StatusProspect2"), 2, $picto);

-			elseif ($statut == '3' || $statut == 'ST_DONE') return img_action($langs->trans("StatusProspect3"), 3, $picto);

-			else {

-				return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0, $picto);

-			}

-		}

-		if ($mode == 4)

-		{

-			if ($statut == '-1' || $statut == 'ST_NO') return img_action($langs->trans("StatusProspect-1"), -1, $picto).' '.$langs->trans("StatusProspect-1");

-			elseif ($statut == '0' || $statut == 'ST_NEVER') return img_action($langs->trans("StatusProspect0"), 0, $picto).' '.$langs->trans("StatusProspect0");

-			elseif ($statut == '1' || $statut == 'ST_TODO') return img_action($langs->trans("StatusProspect1"), 1, $picto).' '.$langs->trans("StatusProspect1");

-			elseif ($statut == '2' || $statut == 'ST_PEND') return img_action($langs->trans("StatusProspect2"), 2, $picto).' '.$langs->trans("StatusProspect2");

-			elseif ($statut == '3' || $statut == 'ST_DONE') return img_action($langs->trans("StatusProspect3"), 3, $picto).' '.$langs->trans("StatusProspect3");

-			else {

-				return img_action(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label, 0, $picto).' '.(($langs->trans("StatusProspect".$statut) != "StatusProspect".$statut) ? $langs->trans("StatusProspect".$statut) : $label);

-			}

-		}

-

-		return "Error, mode/status not found";

-	}

 }