From b220e1acbdae9daaf7dc40e0c107bbbbb82e0d21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Fali=C3=A8re?= <121813548+altairis-benjamin@users.noreply.github.com> Date: Wed, 11 Oct 2023 03:36:15 +0200 Subject: [PATCH 1/2] NEW Assign contact to a ticket message (#24735) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * NEW Assign contact to a ticket message * FIX conflict --------- Co-authored-by: Benjamin Falière Co-authored-by: Laurent Destailleur --- htdocs/ticket/class/ticket.class.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/htdocs/ticket/class/ticket.class.php b/htdocs/ticket/class/ticket.class.php index 8e461338292..74368f93910 100644 --- a/htdocs/ticket/class/ticket.class.php +++ b/htdocs/ticket/class/ticket.class.php @@ -4,6 +4,7 @@ * Copyright (C) 2019-2023 Frédéric France * Copyright (C) 2020 Laurent Destailleur * Copyright (C) 2023 Charlene Benke + * Copyright (C) 2023 Benjamin Falière * * 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 @@ -1713,9 +1714,10 @@ class Ticket extends CommonObject * @param array $mimetype_list List of MIME type of attached files * @param array $mimefilename_list List of attached file name in message * @param boolean $send_email Whether the message is sent by email + * @param boolean $public_area 1=Is the public area * @return int <0 if KO, >0 if OK */ - public function createTicketMessage($user, $notrigger = 0, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array(), $send_email = false) + public function createTicketMessage($user, $notrigger = 0, $filename_list = array(), $mimetype_list = array(), $mimefilename_list = array(), $send_email = false, $public_area = false) { global $conf, $langs; $error = 0; @@ -1758,6 +1760,20 @@ class Ticket extends CommonObject $actioncomm->fk_element = $this->id; $actioncomm->fk_project = $this->fk_project; + // add contact id from author email on public interface + if ($public_area && !empty($this->origin_email) && !empty($conf->global->TICKET_ASSIGN_CONTACT_TO_MESSAGE)) { + $contacts = $this->searchContactByEmail($this->origin_email); + if (!empty($contacts)) { + // Ensure that contact is active and select first active contact + foreach ($contacts as $contact) { + if ((int) $contact->statut == 1) { + $actioncomm->contact_id = $contact->id; + break; + } + } + } + } + $attachedfiles = array(); $attachedfiles['paths'] = $filename_list; $attachedfiles['names'] = $mimefilename_list; @@ -2560,7 +2576,7 @@ class Ticket extends CommonObject $listofnames = $resarray['listofnames']; $listofmimes = $resarray['listofmimes']; - $id = $object->createTicketMessage($user, 0, $listofpaths, $listofmimes, $listofnames, $send_email); + $id = $object->createTicketMessage($user, 0, $listofpaths, $listofmimes, $listofnames, $send_email, $public_area); if ($id <= 0) { $error++; $this->error = $object->error; From cdf4292415f257feeaa0d25b49ca6f34602cb343 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Wed, 11 Oct 2023 11:38:56 +0200 Subject: [PATCH 2/2] FIX remove DISTINCT when MULTICOMPANY_TRANSVERSE_MODE is used (#26143) * FIX (WIP) remove DISTINCT when MULTICOMPANY_TRANSVERSE_MODE is used * FIX optimize code * FIX replace with $this->db->prefix() * FIX works on uniformize MULTICOMPANY_TRANSVERSE_MODE * FIX syntax error --- htdocs/comm/action/peruser.php | 22 ++++++++++-------- htdocs/core/class/commonobject.class.php | 16 ++++--------- htdocs/core/class/html.form.class.php | 26 ++++++++++------------ htdocs/core/class/html.formother.class.php | 4 ++-- htdocs/core/class/ldap.class.php | 4 ++-- 5 files changed, 33 insertions(+), 39 deletions(-) diff --git a/htdocs/comm/action/peruser.php b/htdocs/comm/action/peruser.php index bbc78dc5ec1..a80f787e0ac 100644 --- a/htdocs/comm/action/peruser.php +++ b/htdocs/comm/action/peruser.php @@ -900,22 +900,26 @@ while ($currentdaytoshow < $lastdaytoshow) { } } else { /* Use this list to have for all users */ - $sql = "SELECT DISTINCT u.rowid, u.lastname as lastname, u.firstname, u.statut, u.login, u.admin, u.entity"; - $sql .= " FROM ".MAIN_DB_PREFIX."user as u"; - if (isModEnabled('multicompany') && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) { - $sql .= ", ".MAIN_DB_PREFIX."usergroup_user as ug"; + $sql = "SELECT u.rowid, u.lastname as lastname, u.firstname, u.statut, u.login, u.admin, u.entity"; + $sql .= " FROM ".$db->prefix()."user as u"; + if (isModEnabled('multicompany') && getDolGlobalInt('MULTICOMPANY_TRANSVERSE_MODE')) { + $sql .= " WHERE u.rowid IN ("; + $sql .= " SELECT ug.fk_user FROM ".$db->prefix()."usergroup_user as ug"; $sql .= " WHERE ug.entity IN (".getEntity('usergroup').")"; - $sql .= " AND ug.fk_user = u.rowid "; + if ($usergroup > 0) { + $sql .= " AND ug.fk_usergroup = ".((int) $usergroup); + } + $sql .= ")"; } else { if ($usergroup > 0) { - $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."usergroup_user as ug ON u.rowid = ug.fk_user"; + $sql .= " LEFT JOIN ".$db->prefix()."usergroup_user as ug ON u.rowid = ug.fk_user"; } $sql .= " WHERE u.entity IN (".getEntity('user').")"; + if ($usergroup > 0) { + $sql .= " AND ug.fk_usergroup = ".((int) $usergroup); + } } $sql .= " AND u.statut = 1"; - if ($usergroup > 0) { - $sql .= " AND ug.fk_usergroup = ".((int) $usergroup); - } if ($user->socid > 0) { // External users should see only contacts of their company $sql .= " AND u.fk_soc = ".((int) $user->socid); diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index bafb19b13f8..8a6b5c9907a 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -2136,9 +2136,6 @@ abstract class CommonObject $restrictiononfksoc = empty($this->restrictiononfksoc) ? 0 : $this->restrictiononfksoc; $sql = "SELECT MAX(te.".$fieldid.")"; $sql .= " FROM ".(empty($nodbprefix) ?$this->db->prefix():'').$this->table_element." as te"; - if ($this->element == 'user' && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) { - $sql .= ",".$this->db->prefix()."usergroup_user as ug"; - } if (isset($this->ismultientitymanaged) && !is_numeric($this->ismultientitymanaged)) { $tmparray = explode('@', $this->ismultientitymanaged); $sql .= ", ".$this->db->prefix().$tmparray[1]." as ".($tmparray[1] == 'societe' ? 's' : 'parenttable'); // If we need to link to this table to limit select to entity @@ -2174,12 +2171,11 @@ abstract class CommonObject $sql .= ' AND te.fk_soc = s.rowid'; // If we need to link to societe to limit select to socid } if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) { - if ($this->element == 'user' && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) { + if ($this->element == 'user' && getDolGlobalInt('MULTICOMPANY_TRANSVERSE_MODE')) { if (!empty($user->admin) && empty($user->entity) && $conf->entity == 1) { $sql .= " AND te.entity IS NOT NULL"; // Show all users } else { - $sql .= " AND ug.fk_user = te.rowid"; - $sql .= " AND ug.entity IN (".getEntity('usergroup').")"; + $sql .= " AND te.rowid IN (SELECT ug.fk_user FROM ".$this->db->prefix()."usergroup_user as ug WHERE ug.entity IN (".getEntity('usergroup')."))"; } } else { $sql .= ' AND te.entity IN ('.getEntity($this->element).')'; @@ -2210,9 +2206,6 @@ abstract class CommonObject $sql = "SELECT MIN(te.".$fieldid.")"; $sql .= " FROM ".(empty($nodbprefix) ?$this->db->prefix():'').$this->table_element." as te"; - if ($this->element == 'user' && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) { - $sql .= ",".$this->db->prefix()."usergroup_user as ug"; - } if (isset($this->ismultientitymanaged) && !is_numeric($this->ismultientitymanaged)) { $tmparray = explode('@', $this->ismultientitymanaged); $sql .= ", ".$this->db->prefix().$tmparray[1]." as ".($tmparray[1] == 'societe' ? 's' : 'parenttable'); // If we need to link to this table to limit select to entity @@ -2248,12 +2241,11 @@ abstract class CommonObject $sql .= ' AND te.fk_soc = s.rowid'; // If we need to link to societe to limit select to socid } if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) { - if ($this->element == 'user' && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) { + if ($this->element == 'user' && getDolGlobalInt('MULTICOMPANY_TRANSVERSE_MODE')) { if (!empty($user->admin) && empty($user->entity) && $conf->entity == 1) { $sql .= " AND te.entity IS NOT NULL"; // Show all users } else { - $sql .= " AND ug.fk_user = te.rowid"; - $sql .= " AND ug.entity IN (".getEntity('usergroup').")"; + $sql .= " AND te.rowid IN (SELECT ug.fk_user FROM ".$this->db->prefix()."usergroup_user as ug WHERE ug.entity IN (".getEntity('usergroup')."))"; } } else { $sql .= ' AND te.entity IN ('.getEntity($this->element).')'; diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index fa0236a608e..9677edc0df3 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -2009,7 +2009,7 @@ class Form * @return array|string HTML select string * @see select_dolgroups() */ - public function select_dolusers($selected = '', $htmlname = 'userid', $show_empty = 0, $exclude = null, $disabled = 0, $include = '', $enableonly = '', $force_entity = '0', $maxlength = 0, $showstatus = 0, $morefilter = '', $show_every = 0, $enableonlytext = '', $morecss = '', $notdisabled = 0, $outputmode = 0, $multiple = false, $forcecombo = 0) + public function select_dolusers($selected = '', $htmlname = 'userid', $show_empty = 0, $exclude = null, $disabled = 0, $include = '', $enableonly = '', $force_entity = 0, $maxlength = 0, $showstatus = 0, $morefilter = '', $show_every = 0, $enableonlytext = '', $morecss = '', $notdisabled = 0, $outputmode = 0, $multiple = false, $forcecombo = 0) { // phpcs:enable global $conf, $user, $langs, $hookmanager; @@ -2056,18 +2056,16 @@ class Form $sql .= " FROM " . $this->db->prefix() . "user as u"; if (isModEnabled('multicompany') && $conf->entity == 1 && $user->admin && !$user->entity) { $sql .= " LEFT JOIN " . $this->db->prefix() . "entity as e ON e.rowid = u.entity"; - if ($force_entity) { + if (!empty($force_entity)) { $sql .= " WHERE u.entity IN (0, " . $this->db->sanitize($force_entity) . ")"; } else { $sql .= " WHERE u.entity IS NOT NULL"; } } else { - if (isModEnabled('multicompany') && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) { - $sql .= " LEFT JOIN " . $this->db->prefix() . "usergroup_user as ug"; - $sql .= " ON ug.fk_user = u.rowid"; - $sql .= " WHERE ug.entity = " . (int) $conf->entity; + if (isModEnabled('multicompany') && getDolGlobalInt('MULTICOMPANY_TRANSVERSE_MODE')) { + $sql .= " WHERE u.rowid IN (SELECT ug.fk_user FROM ".$this->db->prefix()."usergroup_user as ug WHERE ug.entity IN (".getEntity('usergroup')."))"; } else { - $sql .= " WHERE u.entity IN (0, " . ((int) $conf->entity) . ")"; + $sql .= " WHERE u.entity IN (" . getEntity('user') . ")"; } } if (!empty($user->socid)) { @@ -2173,8 +2171,8 @@ class Form $moreinfohtml .= ($moreinfohtml ? ' - ' : ' (') . $langs->trans('Disabled'); } } - if (isModEnabled('multicompany') && empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && $conf->entity == 1 && $user->admin && !$user->entity) { - if (!$obj->entity) { + if (isModEnabled('multicompany') && !getDolGlobalInt('MULTICOMPANY_TRANSVERSE_MODE') && $conf->entity == 1 && !empty($user->admin) && empty($user->entity)) { + if (empty($obj->entity)) { $moreinfo .= ($moreinfo ? ' - ' : ' (') . $langs->trans("AllEntities"); $moreinfohtml .= ($moreinfohtml ? ' - ' : ' (') . $langs->trans("AllEntities"); } else { @@ -2184,9 +2182,9 @@ class Form } } } - $moreinfo .= ($moreinfo ? ')' : ''); - $moreinfohtml .= ($moreinfohtml ? ')' : ''); - if ($disableline && $disableline != '1') { + $moreinfo .= (!empty($moreinfo) ? ')' : ''); + $moreinfohtml .= (!empty($moreinfohtml) ? ')' : ''); + if (!empty($disableline) && $disableline != '1') { // Add text from $enableonlytext parameter $moreinfo .= ' - ' . $disableline; $moreinfohtml .= ' - ' . $disableline; @@ -2195,7 +2193,7 @@ class Form $labeltoshowhtml .= $moreinfohtml; $out .= '