diff --git a/htdocs/contact/card.php b/htdocs/contact/card.php index 20a61ec4e3e..5861cc71d38 100644 --- a/htdocs/contact/card.php +++ b/htdocs/contact/card.php @@ -1051,7 +1051,7 @@ else if (!empty($object->socid)) { print '' . $langs->trans("Role") . ''; print ''; - print $form->showRoles("roles", $object, 'edit' ,$object->roles); + print $formcompany->showRoles("roles", $object, 'edit' ,$object->roles); print ''; } @@ -1256,7 +1256,7 @@ else if (!empty($object->socid)) { print '' . $langs->trans("Roles") . ''; print ''; - print $form->showRoles("roles", $object, 'view'); + print $formcompany->showRoles("roles", $object, 'view'); print ''; } diff --git a/htdocs/contact/class/contact.class.php b/htdocs/contact/class/contact.class.php index d718a898f2d..6b46e32f532 100644 --- a/htdocs/contact/class/contact.class.php +++ b/htdocs/contact/class/contact.class.php @@ -1525,6 +1525,49 @@ class Contact extends CommonObject } } + /** + * Get Contact roles for a thirdparty + * + * @param string $element element type + * @return array|int + * @throws Exception + */ + public function getContactRoles($element = '') + { + $tab=array(); + + $sql = "SELECT sc.fk_socpeople as id, sc.fk_c_type_contact"; + $sql.= " FROM ".MAIN_DB_PREFIX."c_type_contact tc"; + $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='".$element."'"; + $sql.= " AND tc.active=1"; + + dol_syslog(get_class($this)."::liste_contact", LOG_DEBUG); + $resql=$this->db->query($sql); + if ($resql) + { + $num=$this->db->num_rows($resql); + $i=0; + while ($i < $num) + { + $obj = $this->db->fetch_object($resql); + $tab[]=array('fk_socpeople'=>$obj->id, 'type_contact'=>$obj->fk_c_type_contact); + + $i++; + } + + return $tab; + } + else + { + $this->error=$this->db->error(); + dol_print_error($this->db); + return -1; + } + } + /** * Updates Roles * diff --git a/htdocs/contact/list.php b/htdocs/contact/list.php index 585d6db499d..6b595a05860 100644 --- a/htdocs/contact/list.php +++ b/htdocs/contact/list.php @@ -36,6 +36,7 @@ require '../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'; // Load translation files required by the page $langs->loadLangs(array("companies", "suppliers", "categories")); @@ -264,6 +265,7 @@ if ($search_priv < 0) $search_priv=''; $form=new Form($db); $formother=new FormOther($db); +$formcompany=new FormCompany($db); $contactstatic=new Contact($db); $title = (! empty($conf->global->SOCIETE_ADDRESSES_MANAGEMENT) ? $langs->trans("Contacts") : $langs->trans("ContactsAddresses")); @@ -518,7 +520,7 @@ if (! empty($conf->categorie->enabled)) } $moreforfilter.='
'; $moreforfilter.=$langs->trans('Roles'). ': '; - $moreforfilter.=$form->showRoles("search_roles", $objecttmp, 'edit', $search_roles); + $moreforfilter.=$formcompany->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 716b615f503..1a1b5c597ce 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -1224,9 +1224,10 @@ abstract class CommonObject public function listeTypeContacts($source = 'internal', $option = 0, $activeonly = 0, $code = '', $element = '') { // phpcs:enable - global $langs; + global $langs, $conf; $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"; @@ -1251,27 +1252,36 @@ abstract class CommonObject dol_syslog(get_class($this)."::".__METHOD__, LOG_DEBUG); $resql=$this->db->query($sql); - if ($resql) - { - $num=$this->db->num_rows($resql); - $i=0; - while ($i < $num) - { - $obj = $this->db->fetch_object($resql); - - $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->rowid]=$libelle_element.' - '.$libelle_type; - $i++; + if ($resql) { + $num = $this->db->num_rows($resql); + if ($num > 0) { + while ($obj = $this->db->fetch_object($resql)) { + if (strpos($obj->element, 'project')!==false) { + $element='projet'; + } elseif($obj->element=='contrat') { + $element='contract'; + } elseif(strpos($obj->element, 'supplier')!==false && $obj->element!='supplier_proposal') { + $element='fournisseur'; + } elseif(strpos($obj->element, 'supplier')!==false && $obj->element!='supplier_proposal') { + $element='fournisseur'; + } else { + $element=$obj->element; + } + if ($conf->{$element}->enabled) { + $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->rowid] = $libelle_element . ' - ' . $libelle_type; + } + } } return $tab; } else { $this->error=$this->db->lasterror(); - //dol_print_error($this->db); return null; } } diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 4eca1ef0f85..f351a361d14 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -6484,47 +6484,6 @@ 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 '
    '; - } - - 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/class/html.formcompany.class.php b/htdocs/core/class/html.formcompany.class.php index 2207ab8ab2b..81b87b6f340 100644 --- a/htdocs/core/class/html.formcompany.class.php +++ b/htdocs/core/class/html.formcompany.class.php @@ -29,28 +29,11 @@ * Class to build HTML component for third parties management * Only common components are here. */ -class FormCompany + +require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; + +class FormCompany extends Form { - /** - * @var DoliDB Database handler. - */ - public $db; - - /** - * @var string Error code (or message) - */ - public $error=''; - - /** - * Constructor - * - * @param DoliDB $db Database handler - */ - public function __construct($db) - { - $this->db = $db; - } - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** @@ -763,6 +746,45 @@ class FormCompany } } + /** + * 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 '
    '; + } + + 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 + } + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Return a select list with zip codes and their town diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php index 14b20bbe46b..331b03bacc4 100644 --- a/htdocs/core/lib/company.lib.php +++ b/htdocs/core/lib/company.lib.php @@ -859,6 +859,8 @@ function show_contacts($conf, $langs, $db, $object, $backtopage = '') global $user,$conf,$extrafields,$hookmanager; global $contextpage; + require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'; + $formcompany = new FormCompany($db); $form = new Form($db); $optioncss = GETPOST('optioncss', 'alpha'); @@ -1017,7 +1019,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'))) { - print $form->showRoles("search_roles", $contactstatic, 'edit', $search_roles); + print $formcompany->showRoles("search_roles", $contactstatic, 'edit', $search_roles); } print ''; } @@ -1152,7 +1154,7 @@ function show_contacts($conf, $langs, $db, $object, $backtopage = '') if (! empty($arrayfields['sc.role']['checked'])) { print ''; - print $form->showRoles("roles", $contactstatic, 'view'); + print $formcompany->showRoles("roles", $contactstatic, 'view'); print ''; } diff --git a/htdocs/core/triggers/interface_90_modSociete_ContactRoles.class.php b/htdocs/core/triggers/interface_90_modSociete_ContactRoles.class.php new file mode 100644 index 00000000000..8fba51e7a69 --- /dev/null +++ b/htdocs/core/triggers/interface_90_modSociete_ContactRoles.class.php @@ -0,0 +1,120 @@ + + * Copyright (C) 2009-2017 Regis Houssin + * Copyright (C) 2011-2014 Juanjo Menent + * Copyright (C) 2013 Cedric GROSS + * Copyright (C) 2014 Marcos García + * Copyright (C) 2015 Bahfir Abbes + * + * 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 + * the Free Software Foundation; either version 3 of the License, or + * (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 + * along with this program. If not, see . + */ + +/** + * \file htdocs/core/triggers/interface_50_modAgenda_ActionsAuto.class.php + * \ingroup agenda + * \brief Trigger file for agenda module + */ + +require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php'; + + +/** + * Class of triggered functions for agenda module + */ +class InterfaceContactRoles extends DolibarrTriggers +{ + public $family = 'agenda'; + public $description = "Triggers of this module add actions in agenda according to setup made in agenda setup."; + + /** + * Version of the trigger + * @var string + */ + public $version = self::VERSION_DOLIBARR; + + /** + * @var string Image of the trigger + */ + public $picto = 'action'; + + /** + * Function called when a Dolibarrr business event is done. + * All functions "runTrigger" are triggered if file is inside directory htdocs/core/triggers or htdocs/module/code/triggers (and declared) + * + * Following properties may be set before calling trigger. The may be completed by this trigger to be used for writing the event into database: + * $object->actiontypecode (translation action code: AC_OTH, ...) + * $object->actionmsg (note, long text) + * $object->actionmsg2 (label, short text) + * $object->sendtoid (id of contact or array of ids) + * $object->socid (id of thirdparty) + * $object->fk_project + * $object->fk_element + * $object->elementtype + * + * @param string $action Event action code + * @param Object $object Object + * @param User $user Object user + * @param Translate $langs Object langs + * @param conf $conf Object conf + * @return int <0 if KO, 0 if no triggered ran, >0 if OK + */ + public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf) + { + // Lors de la création d'un document, récupération des contacts et rôle associés à la société et association avec le document + if ($action === 'PROPAL_CREATE' || $action === 'ORDER_CREATE' || $action === 'BILL_CREATE' || $action === 'ORDER_SUPPLIER_CREATE' || $action === 'BILL_SUPPLIER_CREATE' + || $action === 'CONTRACT_CREATE' || $action === 'FICHINTER_CREATE' || $action === 'PROJECT_CREATE' || $action === 'TICKET_CREATE' || $action === 'ACTION_CREATE') { + + if(!empty($object->socid) && $object->socid != '-1') { + global $db, $langs; + + $contactdefault = new Contact($this->db); + $contactdefault->socid=$object->socid; + $TContact = $contactdefault->getContactRoles(); + + // Le trigger est appelé avant que le core n'ajoute lui-même des contacts (contact propale, clone), il ne faut pas les associer avant sinon bug + $TContactAlreadyLinked = array(); + if ($object->id > 0) + { + $class = get_class($object); + $cloneFrom = new $class($db); + $r = $cloneFrom->fetch($object->id); + + if (!empty($cloneFrom->id)) $TContactAlreadyLinked = array_merge($cloneFrom->liste_contact(-1,'external'), $cloneFrom->liste_contact(-1,'internal')); + } + + foreach($TContact as $i => $infos) { + // Gestion du cas du clone + foreach ($TContactAlreadyLinked as $contactData) { + if($contactData['id'] == $infos['fk_socpeople'] && $contactData['fk_c_type_contact'] == $infos['type_contact']) unset($TContact[$i]); + } + } + + $nb = 0; + foreach($TContact as $infos) { + // Gestion du cas spécifique de la création de propale avec sélection du contact, cela créé un bug si le contact est ajouté par le module contactdefault + $res = $object->add_contact($infos['fk_socpeople'], $infos['type_contact']); + if($res > 0) $nb++; + } + + if($nb > 0) { + setEventMessage($langs->trans('ContactAddedAutomatically', $nb)); + } + } + + dol_syslog( + "Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id + ); + } + } +}