This commit is contained in:
Laurent Destailleur 2021-01-22 13:17:27 +01:00
parent ad2f5ea7c1
commit 93ca27d808
2 changed files with 52 additions and 31 deletions

View File

@ -54,6 +54,8 @@ $mesg = ''; $error = 0; $errors = array();
$action = (GETPOST('action', 'alpha') ? GETPOST('action', 'alpha') : 'view'); $action = (GETPOST('action', 'alpha') ? GETPOST('action', 'alpha') : 'view');
$confirm = GETPOST('confirm', 'alpha'); $confirm = GETPOST('confirm', 'alpha');
$backtopage = GETPOST('backtopage', 'alpha'); $backtopage = GETPOST('backtopage', 'alpha');
$cancel = GETPOST('cancel', 'alpha');
$id = GETPOST('id', 'int'); $id = GETPOST('id', 'int');
$socid = GETPOST('socid', 'int'); $socid = GETPOST('socid', 'int');
@ -296,9 +298,9 @@ if (empty($reshook))
} }
} }
if ($action == 'update' && !$_POST["cancel"] && $user->rights->societe->contact->creer) if ($action == 'update' && empty($cancel) && $user->rights->societe->contact->creer)
{ {
if (empty($_POST["lastname"])) if (!GETPOST("lastname", 'alpha'))
{ {
$error++; $errors = array($langs->trans("ErrorFieldRequired", $langs->transnoentities("Name").' / '.$langs->transnoentities("Label"))); $error++; $errors = array($langs->trans("ErrorFieldRequired", $langs->transnoentities("Name").' / '.$langs->transnoentities("Label")));
$action = 'edit'; $action = 'edit';
@ -308,6 +310,7 @@ if (empty($reshook))
{ {
$contactid = GETPOST("contactid", 'int'); $contactid = GETPOST("contactid", 'int');
$object->fetch($contactid); $object->fetch($contactid);
$object->fetchRoles($contactid);
// Photo save // Photo save
$dir = $conf->societe->multidir_output[$object->entity]."/contact/".$object->id."/photos"; $dir = $conf->societe->multidir_output[$object->entity]."/contact/".$object->id."/photos";
@ -396,7 +399,10 @@ if (empty($reshook))
$object->priv = (string) GETPOST("priv", 'int'); $object->priv = (string) GETPOST("priv", 'int');
$object->note_public = (string) GETPOST("note_public", 'restricthtml'); $object->note_public = (string) GETPOST("note_public", 'restricthtml');
$object->note_private = (string) GETPOST("note_private", 'restricthtml'); $object->note_private = (string) GETPOST("note_private", 'restricthtml');
if (GETPOSTISSET("roles")) {
$object->roles = GETPOST("roles", 'array'); $object->roles = GETPOST("roles", 'array');
}
// Fill array 'array_options' with data from add form // Fill array 'array_options' with data from add form
$ret = $extrafields->setOptionalsFromPost(null, $object); $ret = $extrafields->setOptionalsFromPost(null, $object);

View File

@ -259,7 +259,7 @@ class Contact extends CommonObject
*/ */
public $oldcopy; // To contains a clone of this when we need to save old properties of object public $oldcopy; // To contains a clone of this when we need to save old properties of object
public $roles = array(); public $roles = null;
public $cacheprospectstatus = array(); public $cacheprospectstatus = array();
public $fk_prospectlevel; public $fk_prospectlevel;
@ -856,7 +856,7 @@ class Contact extends CommonObject
* @param User $user Load also alerts of this user (subscribing to alerts) that want alerts about this 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 $ref_ext External reference, not given by Dolibarr
* @param string $email Email * @param string $email Email
* @param int $loadalsoroles Load also roles * @param int $loadalsoroles Load also roles. Try to always 0 here and load roles with a separate call of fetchRoles().
* @return int >0 if OK, <0 if KO or if two records found for same ref or idprof, 0 if not found. * @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) public function fetch($id, $user = null, $ref_ext = '', $email = '', $loadalsoroles = 0)
@ -1634,8 +1634,8 @@ class Contact extends CommonObject
* Fetch roles (default contact of some companies) for the current contact. * Fetch roles (default contact of some companies) for the current contact.
* This load the array ->roles. * This load the array ->roles.
* *
* @return float|int * @return int <0 if KO, Nb of roles found if OK
* @throws Exception * @see updateRoles()
*/ */
public function fetchRoles() public function fetchRoles()
{ {
@ -1650,11 +1650,10 @@ class Contact extends CommonObject
$sql .= " AND sc.fk_socpeople = ".$this->id; $sql .= " AND sc.fk_socpeople = ".$this->id;
$sql .= " AND sc.entity IN (".getEntity('societe').')'; $sql .= " AND sc.entity IN (".getEntity('societe').')';
dol_syslog(__METHOD__, LOG_DEBUG);
$this->roles = array();
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {
$this->roles = array();
$num = $this->db->num_rows($resql); $num = $this->db->num_rows($resql);
if ($num > 0) { if ($num > 0) {
while ($obj = $this->db->fetch_object($resql)) { while ($obj = $this->db->fetch_object($resql)) {
@ -1726,7 +1725,7 @@ class Contact extends CommonObject
* This is called by update of contact. * This is called by update of contact.
* *
* @return float|int * @return float|int
* @throws Exception * @see fetchRoles()
*/ */
public function updateRoles() public function updateRoles()
{ {
@ -1734,11 +1733,12 @@ class Contact extends CommonObject
$error = 0; $error = 0;
if (!isset($this->roles)) return; // Avoid to loose roles when property not set
$this->db->begin(); $this->db->begin();
$sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_soc=".$this->socid." AND fk_socpeople=".$this->id; ; $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople=".$this->id." AND entity IN (".getEntity("societe_contact").")";
dol_syslog(__METHOD__, LOG_DEBUG);
$result = $this->db->query($sql); $result = $this->db->query($sql);
if (!$result) { if (!$result) {
$this->errors[] = $this->db->lasterror().' sql='.$sql; $this->errors[] = $this->db->lasterror().' sql='.$sql;
@ -1746,6 +1746,21 @@ class Contact extends CommonObject
} else { } else {
if (count($this->roles) > 0) { if (count($this->roles) > 0) {
foreach ($this->roles as $keyRoles => $valRoles) { foreach ($this->roles as $keyRoles => $valRoles) {
$idrole = 0;
if (is_array($idrole)) {
$idrole = $valRoles['id'];
} else {
$idrole = $valRoles;
}
$socid = 0;
if (is_array($idrole)) {
$socid = $valRoles['socid'];
} else {
$socid = $this->socid;
}
if ($socid > 0) {
$sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts"; $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_contacts";
$sql .= " (entity,"; $sql .= " (entity,";
$sql .= "date_creation,"; $sql .= "date_creation,";
@ -1754,11 +1769,10 @@ class Contact extends CommonObject
$sql .= "fk_socpeople) "; $sql .= "fk_socpeople) ";
$sql .= " VALUES (".$conf->entity.","; $sql .= " VALUES (".$conf->entity.",";
$sql .= "'".$this->db->idate(dol_now())."',"; $sql .= "'".$this->db->idate(dol_now())."',";
$sql .= $this->socid.", "; $sql .= $socid.", ";
$sql .= $valRoles." , "; $sql .= $idrole." , ";
$sql .= $this->id; $sql .= $this->id;
$sql .= ")"; $sql .= ")";
dol_syslog(__METHOD__, LOG_DEBUG);
$result = $this->db->query($sql); $result = $this->db->query($sql);
if (!$result) if (!$result)
@ -1769,6 +1783,7 @@ class Contact extends CommonObject
} }
} }
} }
}
if (empty($error)) { if (empty($error)) {
$this->db->commit(); $this->db->commit();
return 1; return 1;