diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 78a98f77c6e..f0f349bd3bd 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -4315,6 +4315,9 @@ abstract class CommonObject if (empty($fk_object_where) || empty($field_where) || empty($table_element)) { return -1; } + if (!preg_match('/^[_a-zA-Z0-9]+$/', $field_select)) { + dol_syslog('Invalid value $field_select for parameter '.$field_select.' in call to getAllItemsLinkedByObjectID(). Must be a single field name.', LOG_ERR); + } global $db; @@ -4331,6 +4334,35 @@ abstract class CommonObject return $TRes; } + /** + * Count items linked to an object id in association table + * + * @param int $fk_object_where id of object we need to get linked items + * @param string $field_where name of field of object we need to get linked items + * @param string $table_element name of association table + * @return array|int Array of record, -1 if empty + */ + public static function getCountOfItemsLinkedByObjectID($fk_object_where, $field_where, $table_element) + { + if (empty($fk_object_where) || empty($field_where) || empty($table_element)) { + return -1; + } + + global $db; + + $sql = "SELECT COUNT(*) as nb FROM ".$db->prefix().$table_element." WHERE ".$field_where." = ".((int) $fk_object_where); + $resql = $db->query($sql); + $n = 0; + if ($resql) { + $res = $db->fetch_object($resql); + if ($res) { + $n = $res->nb; + } + } + + return $n; + } + /** * Function used to remove all items linked to an object id in association table * diff --git a/htdocs/core/lib/project.lib.php b/htdocs/core/lib/project.lib.php index 43b2c136044..07519ea55d2 100644 --- a/htdocs/core/lib/project.lib.php +++ b/htdocs/core/lib/project.lib.php @@ -214,7 +214,7 @@ function project_prepare_head(Project $project, $moreparam = '') if (isModEnabled('ticket') && $user->hasRight('ticket', 'read')) { require_once DOL_DOCUMENT_ROOT.'/ticket/class/ticket.class.php'; $Tickettatic = new Ticket($db); - $nbTicket = count($Tickettatic->getAllItemsLinkedByObjectID($project->id, '*', 'fk_project', 'ticket')); + $nbTicket = $Tickettatic->getCountOfItemsLinkedByObjectID($project->id, 'fk_project', 'ticket'); $head[$h][0] = DOL_URL_ROOT.'/ticket/list.php?projectid='.((int) $project->id); $head[$h][1] = $langs->trans("Ticket"); if ($nbTicket > 0) {