diff --git a/htdocs/contact/card.php b/htdocs/contact/card.php
index 76d0e3911b5..20a61ec4e3e 100644
--- a/htdocs/contact/card.php
+++ b/htdocs/contact/card.php
@@ -371,6 +371,7 @@ if (empty($reshook))
$object->priv = GETPOST("priv", 'int');
$object->note_public = GETPOST("note_public", 'none');
$object->note_private = GETPOST("note_private", 'none');
+ $object->roles = GETPOST("roles", 'array');
// Fill array 'array_options' with data from add form
$ret = $extrafields->setOptionalsFromPost($extralabels, $object);
@@ -474,9 +475,6 @@ else
// Si edition contact deja existant
$object = new Contact($db);
$res=$object->fetch($id, $user);
- if ($res < 0) { dol_print_error($db, $object->error); exit; }
- $res=$object->fetch_optionals();
- if ($res < 0) { dol_print_error($db, $object->error); exit; }
// Show tabs
$head = contact_prepare_head($object);
@@ -724,6 +722,15 @@ else
print "";
}
+ //Role
+ if (!empty($socid)) {
+ print '
| ' . $langs->trans("Role") . ' | ';
+ print '';
+ $contactType = $object->listeTypeContacts('external', '', 1);
+ print $form->multiselectarray('roles', $contactType);
+ print ' |
';
+ }
+
// Other attributes
$parameters=array('socid' => $socid, 'objsoc' => $objsoc, 'colspan' => ' colspan="3"', 'cols' => 3);
$reshook=$hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
@@ -1040,6 +1047,14 @@ else
print "";
}
+ //Role
+ if (!empty($object->socid)) {
+ print '| ' . $langs->trans("Role") . ' | ';
+ print '';
+ print $form->showRoles("roles", $object, 'edit' ,$object->roles);
+ print ' |
';
+ }
+
// Other attributes
$parameters=array('colspan' => ' colspan="3"', 'cols'=>3);
$reshook=$hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
@@ -1238,6 +1253,13 @@ else
print '';
}
+ if (!empty($object->socid)) {
+ print '| ' . $langs->trans("Roles") . ' | ';
+ print '';
+ print $form->showRoles("roles", $object, 'view');
+ print ' |
';
+ }
+
// Other attributes
$cols = 3;
$parameters=array('socid'=>$socid);
diff --git a/htdocs/contact/class/contact.class.php b/htdocs/contact/class/contact.class.php
index db6ab29b802..d718a898f2d 100644
--- a/htdocs/contact/class/contact.class.php
+++ b/htdocs/contact/class/contact.class.php
@@ -399,6 +399,14 @@ class Contact extends CommonObject
}
}
+ if (! $error) {
+ $result=$this->updateRoles();
+ if ($result < 0)
+ {
+ $error++;
+ }
+ }
+
if (! $error && $this->user_id > 0)
{
$tmpobj = new User($this->db);
@@ -861,6 +869,11 @@ class Contact extends CommonObject
// fetch optionals attributes and labels
$this->fetch_optionals();
+ $resultRole=$this->fetchRoles();
+ if ($resultRole<0) {
+ return $resultRole;
+ }
+
return 1;
}
else
@@ -995,6 +1008,20 @@ class Contact extends CommonObject
}
}
+ if (! $error)
+ {
+ // Remove Roles
+ $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_socpeople = ".$this->id;
+ dol_syslog(get_class($this)."::delete", LOG_DEBUG);
+ $resql=$this->db->query($sql);
+ if (! $resql)
+ {
+ $error++;
+ $this->error .= $this->db->lasterror();
+ $errorflag=-1;
+ }
+ }
+
if (! $error)
{
// Remove category
@@ -1458,14 +1485,13 @@ class Contact extends CommonObject
* @return float|int
* @throws Exception
*/
- public function fetchRole()
+ public function fetchRoles()
{
-
global $langs;
$error= 0;
$num=0;
- $sql ="SELECT tc.rowid, tc.element, tc.source, tc.code, tc.libelle";
+ $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";
@@ -1482,11 +1508,13 @@ class Contact extends CommonObject
if ($num > 0) {
while ($obj = $this->db->fetch_object($resql)) {
$transkey="TypeContact_".$obj->element."_".$obj->source."_".$obj->code;
- $this->roles[$this->id]=array('id'=>$obj->rowid,'element'=>$obj->element,'source'=>$obj->source,'code'=>$obj->code,'label'=>($langs->trans($transkey)!=$transkey ? $langs->trans($transkey) : $obj->libelle));
+ $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
+ $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 {
$error++;
+ $this->error=$this->db->lasterror();
$this->errors[]=$this->db->lasterror();
}
@@ -1497,4 +1525,61 @@ class Contact extends CommonObject
}
}
+ /**
+ * Updates Roles
+ *
+ * @return float|int
+ * @throws Exception
+ */
+ public function updateRoles()
+ {
+ global $conf;
+
+ $error=0;
+
+ $this->db->begin();
+
+ $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_contacts WHERE fk_soc=".$this->socid;
+
+ dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG);
+ $result = $this->db->query($sql);
+ if (!$result) {
+ $this->errors[]=$this->db->lasterror().' sql='.$sql;
+ $error++;
+ } else {
+ if (count($this->roles)>0) {
+ foreach ($this->roles as $keyRoles => $valRoles) {
+ $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(get_class($this) . "::".__METHOD__, LOG_DEBUG);
+
+ $result = $this->db->query($sql);
+ if (!$result)
+ {
+ $this->errors[]=$this->db->lasterror().' sql='.$sql;
+ $error++;
+ }
+ }
+ }
+ }
+ if (empty($error)) {
+ $this->db->commit();
+ return 1;
+ } else {
+ $this->error=implode(' ', $this->errors);
+ $this->db->rollback();
+ return $error*-1;
+ }
+ }
+
}
diff --git a/htdocs/contact/list.php b/htdocs/contact/list.php
index b0db8b0affb..585d6db499d 100644
--- a/htdocs/contact/list.php
+++ b/htdocs/contact/list.php
@@ -84,6 +84,7 @@ $search_zip=GETPOST('search_zip', 'alpha');
$search_town=GETPOST('search_town', 'alpha');
$search_import_key=GETPOST("search_import_key", "alpha");
$search_country=GETPOST("search_country", 'intcomma');
+$search_roles=GETPOST("search_roles", 'array');
if ($search_status=='') $search_status=1; // always display active customer first
@@ -242,6 +243,7 @@ if (empty($reshook))
$search_import_key='';
$toselect='';
$search_array_options=array();
+ $search_roles=array();
}
// Mass actions
@@ -334,6 +336,9 @@ if (strlen($search_linkedin)) $sql.= natural_search('p.linkedin', $search_
if (strlen($search_email)) $sql.= natural_search('p.email', $search_email);
if (strlen($search_zip)) $sql.= natural_search("p.zip", $search_zip);
if (strlen($search_town)) $sql.= natural_search("p.town", $search_town);
+if (count($search_roles)>0) {
+ $sql .= " AND p.rowid IN (SELECT sc.fk_socpeople FROM ".MAIN_DB_PREFIX."societe_contacts as sc WHERE sc.fk_c_type_contact IN (".implode(',', $search_roles)."))";
+}
if ($search_no_email != '' && $search_no_email >= 0) $sql.= " AND p.no_email = ".$db->escape($search_no_email);
if ($search_status != '' && $search_status >= 0) $sql.= " AND p.statut = ".$db->escape($search_status);
@@ -438,6 +443,7 @@ if ($search_status != '') $param.='&search_status='.urlencode($search_status
if ($search_priv == '0' || $search_priv == '1') $param.="&search_priv=".urlencode($search_priv);
if ($search_import_key != '') $param.='&search_import_key='.urlencode($search_import_key);
if ($optioncss != '') $param.='&optioncss='.$optioncss;
+if (count($search_roles)>0) $param.=implode('&search_roles[]=', $search_roles);
// Add $param from extra fields
include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_param.tpl.php';
@@ -510,6 +516,10 @@ if (! empty($conf->categorie->enabled))
$moreforfilter.=$formother->select_categories(Categorie::TYPE_SUPPLIER, $search_categ_supplier, 'search_categ_supplier', 1);
$moreforfilter.='';
}
+ $moreforfilter.='';
+ $moreforfilter.=$langs->trans('Roles'). ': ';
+ $moreforfilter.=$form->showRoles("search_roles", $objecttmp, 'edit', $search_roles);
+ $moreforfilter.='
';
}
if ($moreforfilter)
{
diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php
index 0eea968ecd2..716b615f503 100644
--- a/htdocs/core/class/commonobject.class.php
+++ b/htdocs/core/class/commonobject.class.php
@@ -1226,9 +1226,6 @@ abstract class CommonObject
// phpcs:enable
global $langs;
- if (empty($order)) $order='position';
- if ($order == 'position') $order.=',code';
-
$tab = array();
$sql = "SELECT DISTINCT tc.rowid, tc.code, tc.libelle, tc.position, tc.element";
$sql.= " FROM ".MAIN_DB_PREFIX."c_type_contact as tc";
@@ -1262,11 +1259,11 @@ abstract class CommonObject
{
$obj = $this->db->fetch_object($resql);
- $libelle_element = $langs->trans('ContactDefault_'.ucfirst($obj->element));
+ $libelle_element = $langs->trans('ContactDefault_'.$obj->element);
$transkey="TypeContact_".$this->element."_".$source."_".$obj->code;
$libelle_type=($langs->trans($transkey)!=$transkey ? $langs->trans($transkey) : $obj->libelle);
if (empty($option)) $tab[$obj->rowid]=$libelle_element.' - '.$libelle_type;
- else $tab[$obj->code]=$libelle_element.' - '.$libelle_type;
+ else $tab[$obj->rowid]=$libelle_element.' - '.$libelle_type;
$i++;
}
return $tab;
diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php
index 5ee969a73a0..4eca1ef0f85 100644
--- a/htdocs/core/class/html.form.class.php
+++ b/htdocs/core/class/html.form.class.php
@@ -6484,6 +6484,46 @@ class Form
return 'ErrorBadValueForParameterRenderMode'; // Should not happened
}
+ /**
+ * showContactRoles on view and edit mode
+ *
+ * @param string $htmlname Html component name and id
+ * @param Contact $contact Contact Obejct
+ * @param string $rendermode view, edit
+ * @param array $selected $key=>$val $val is selected Roles for input mode
+ * @return string String with contacts roles
+ */
+ public function showRoles($htmlname = '', Contact $contact, $rendermode = 'view', $selected = array())
+ {
+ if ($rendermode === 'view') {
+ $toprint = array();
+ foreach ($contact->roles as $key => $val) {
+ $toprint[] = '' . $val['label'] . '';
+ }
+ return '' . implode(' ', $toprint) . '
';
+ }
+
+ if ($rendermode === 'edit')
+ {
+ $contactType=$contact->listeTypeContacts('external', '', 1);
+
+ if (count($selected)>0) {
+ $newselected=array();
+ foreach($selected as $key=>$val) {
+ if (is_array($val) && array_key_exists('id', $val) && in_array($val['id'], array_keys($contactType))) {
+ $newselected[]=$val['id'];
+ } else {
+ break;
+ }
+ }
+ if (count($newselected)>0) $selected=$newselected;
+ }
+ return $this->multiselectarray($htmlname, $contactType, $selected);
+ }
+
+ return 'ErrorBadValueForParameterRenderMode'; // Should not happened
+ }
+
/**
* Show linked object block.
diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php
index cfc1fcfb30f..14b20bbe46b 100644
--- a/htdocs/core/lib/company.lib.php
+++ b/htdocs/core/lib/company.lib.php
@@ -869,7 +869,7 @@ function show_contacts($conf, $langs, $db, $object, $backtopage = '')
if ($search_status=='') $search_status=1; // always display active customer first
$search_name = GETPOST("search_name", 'alpha');
$search_addressphone = GETPOST("search_addressphone", 'alpha');
- $search_role = GETPOST("search_role", 'array');
+ $search_roles = GETPOST("search_roles", 'array');
if (! $sortorder) $sortorder="ASC";
if (! $sortfield) $sortfield="t.lastname";
@@ -898,7 +898,7 @@ function show_contacts($conf, $langs, $db, $object, $backtopage = '')
't.name'=>array('label'=>"Name", 'checked'=>1, 'position'=>10),
't.poste'=>array('label'=>"PostOrFunction", 'checked'=>1, 'position'=>20),
't.address'=>array('label'=>(empty($conf->dol_optimize_smallscreen) ? $langs->trans("Address").' / '.$langs->trans("Phone").' / '.$langs->trans("Email") : $langs->trans("Address")), 'checked'=>1, 'position'=>30),
- 'sc.role'=>array('label'=>"Role", 'checked'=>1, 'position'=>40),
+ 'sc.role'=>array('label'=>"Roles", 'checked'=>1, 'position'=>40),
't.statut'=>array('label'=>"Status", 'checked'=>1, 'position'=>50, 'class'=>'center'),
);
// Extra fields
@@ -929,7 +929,7 @@ function show_contacts($conf, $langs, $db, $object, $backtopage = '')
{
$search_status = '';
$search_name = '';
- $search_role = '';
+ $search_roles = array();
$search_addressphone = '';
$search_array_options=array();
@@ -971,7 +971,7 @@ function show_contacts($conf, $langs, $db, $object, $backtopage = '')
$param="socid=".urlencode($object->id);
if ($search_status != '') $param.='&search_status='.urlencode($search_status);
- if (count($search_role)>0) $param.=implode('&search_role[]=', $search_role);
+ if (count($search_roles)>0) $param.=implode('&search_roles[]=', $search_roles);
if ($search_name != '') $param.='&search_name='.urlencode($search_name);
if ($optioncss != '') $param.='&optioncss='.urlencode($optioncss);
// Add $param from extra fields
@@ -982,14 +982,11 @@ function show_contacts($conf, $langs, $db, $object, $backtopage = '')
$sql .= " t.civility as civility_id, t.address, t.zip, t.town";
$sql .= " FROM ".MAIN_DB_PREFIX."socpeople as t";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople_extrafields as ef on (t.rowid = ef.fk_object)";
- if (count($search_role)>0) {
- $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe_contacts as sc on (sc.fk_socpeople = t.rowid)";
- }
$sql .= " WHERE t.fk_soc = ".$object->id;
if ($search_status!='' && $search_status != '-1') $sql .= " AND t.statut = ".$db->escape($search_status);
if ($search_name) $sql .= natural_search(array('t.lastname', 't.firstname'), $search_name);
- if (count($search_role)>0) {
- $sql .= ' AND sc.fk_c_type_contact IN ('.implode(',',$search_role).')';
+ if (count($search_roles)>0) {
+ $sql .= " AND t.rowid IN (SELECT sc.fk_socpeople FROM ".MAIN_DB_PREFIX."societe_contacts as sc WHERE sc.fk_c_type_contact IN (".implode(',', $search_roles)."))";
}
// Add where from extra fields
$extrafieldsobjectkey=$contactstatic->table_element;
@@ -1020,8 +1017,7 @@ function show_contacts($conf, $langs, $db, $object, $backtopage = '')
if (in_array($key, array('lastname','name'))) print '';
elseif (in_array($key, array('statut'))) print $form->selectarray('search_status', array('-1'=>'','0'=>$contactstatic->LibStatut(0, 1),'1'=>$contactstatic->LibStatut(1, 1)), $search_status);
elseif (in_array($key, array('role'))) {
- $contactType=$contactstatic->listeTypeContacts('external', '', 1);
- print $form->multiselectarray('search_role', $contactType, $search_role);
+ print $form->showRoles("search_roles", $contactstatic, 'edit', $search_roles);
}
print '';
}
@@ -1052,9 +1048,12 @@ function show_contacts($conf, $langs, $db, $object, $backtopage = '')
if (in_array($val['type'], array('timestamp'))) $align.=($align?' ':'').'nowrap';
if ($key == 'status' || $key == 'statut') $align.=($align?' ':'').'center';
if ($key == 'role') $align.=($align?' ':'').'center';
- if (! empty($arrayfields['t.'.$key]['checked']) || ! empty($arrayfields['sc.'.$key]['checked'])) {
+ if (! empty($arrayfields['t.'.$key]['checked'])) {
print getTitleFieldOfList($arrayfields['t.'.$key]['label'], 0, $_SERVER['PHP_SELF'], 't.'.$key, '', $param, ($align?'class="'.$align.'"':''), $sortfield, $sortorder, $align.' ')."\n";
}
+ if (! empty($arrayfields['sc.'.$key]['checked'])) {
+ print getTitleFieldOfList($arrayfields['sc.'.$key]['label'], 0, $_SERVER['PHP_SELF'], 'sc.'.$key, '', $param, ($align?'class="'.$align.'"':''), $sortfield, $sortorder, $align.' ')."\n";
+ }
}
// Extra fields
$extrafieldsobjectkey=$contactstatic->table_element;
@@ -1101,7 +1100,7 @@ function show_contacts($conf, $langs, $db, $object, $backtopage = '')
$contactstatic->setGenderFromCivility();
$contactstatic->fetch_optionals();
- $resultRole=$contactstatic->fetchRole();
+ $resultRole=$contactstatic->fetchRoles();
if ($resultRole<0) {
setEventMessages(null, $contactstatic->errors,'errors');
}
@@ -1153,9 +1152,7 @@ function show_contacts($conf, $langs, $db, $object, $backtopage = '')
if (! empty($arrayfields['sc.role']['checked']))
{
print '';
- foreach($contactstatic->roles as $key=>$val) {
- print ''.implode(' ', $val['label']).' ';
- }
+ print $form->showRoles("roles", $contactstatic, 'view');
print ' | ';
}