From ba98db934d879c4f6c0ea87ee691758a9053239b Mon Sep 17 00:00:00 2001 From: "Sekan, Tobias" Date: Fri, 21 Aug 2020 08:18:26 +0200 Subject: [PATCH 1/6] Show user on external calender events --- htdocs/comm/action/index.php | 13 ++++++++-- htdocs/user/class/user.class.php | 43 +++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/htdocs/comm/action/index.php b/htdocs/comm/action/index.php index 97d732f6908..1af04366651 100644 --- a/htdocs/comm/action/index.php +++ b/htdocs/comm/action/index.php @@ -1061,12 +1061,21 @@ if (count($listofextcals)) $event->id = $icalevent['UID']; $event->ref = $event->id; - $event->icalname = $namecal; + $userId = $userstatic->findUserIdByEmail($namecal); + if(!empty($userId)) + { + $event->userassigned[$userId] = $userId; + } + else + { + $event->icalname = $namecal; + $event->type_code = "ICALEVENT"; + } + $event->icalcolor = $colorcal; $usertime = 0; // We dont modify date because we want to have date into memory datep and datef stored as GMT date. Compensation will be done during output. $event->datep = $datestart + $usertime; $event->datef = $dateend + $usertime; - $event->type_code = "ICALEVENT"; if ($icalevent['SUMMARY']) $event->label = $icalevent['SUMMARY']; elseif ($icalevent['DESCRIPTION']) $event->label = dol_nl2br($icalevent['DESCRIPTION'], 1); diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php index fdda769c5f4..6d278e635fa 100644 --- a/htdocs/user/class/user.class.php +++ b/htdocs/user/class/user.class.php @@ -3236,5 +3236,46 @@ class User extends CommonObject $this->errors[] = $this->db->lasterror(); return -1; } - } + } + + /** + * Cache the SQL results of the function "findUserIdByEmail($email)" + * + * @var array + */ + private $findUserIdByEmailCache; + + /** + * Find a user by the given e-mail or part of a e-mail and return its user id when found + * + * @param string $email The full e-mail or a part of a e-mail + * @return int|null The id of the user when found, otherwise null + */ + public function findUserIdByEmail($email) + { + if($this->findUserIdByEmailCache[$email]) + { + return $this->findUserIdByEmailCache[$email]; + } + + $sql = 'SELECT rowid'; + $sql .= ' FROM '.MAIN_DB_PREFIX.'user'; + $sql .= ' WHERE email LIKE "%'.$email.'%"'; + + $resql = $this->db->query($sql); + if (!$resql) + { + return 0; + } + + $obj = $this->db->fetch_object($resql); + if (!$obj) + { + return 0; + } + + $this->findUserIdByEmailCache[$email] = (int)$obj->rowid; + + return (int)$obj->rowid; + } } From bb1452917b20b1f61225fa1b07be389e891a2612 Mon Sep 17 00:00:00 2001 From: "Sekan, Tobias" Date: Fri, 21 Aug 2020 08:33:19 +0200 Subject: [PATCH 2/6] fix wrong event percentage --- htdocs/comm/action/index.php | 1 + 1 file changed, 1 insertion(+) diff --git a/htdocs/comm/action/index.php b/htdocs/comm/action/index.php index 1af04366651..f75913bb144 100644 --- a/htdocs/comm/action/index.php +++ b/htdocs/comm/action/index.php @@ -1065,6 +1065,7 @@ if (count($listofextcals)) if(!empty($userId)) { $event->userassigned[$userId] = $userId; + $event->percentage = -1; } else { From 22ff5a320fdbed3e4d70dbc758aecad2bf4dd4af Mon Sep 17 00:00:00 2001 From: "Sekan, Tobias" Date: Fri, 21 Aug 2020 08:40:23 +0200 Subject: [PATCH 3/6] fix not working external calender on/off --- htdocs/comm/action/index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/comm/action/index.php b/htdocs/comm/action/index.php index f75913bb144..e5ab1c02902 100644 --- a/htdocs/comm/action/index.php +++ b/htdocs/comm/action/index.php @@ -1069,10 +1069,10 @@ if (count($listofextcals)) } else { - $event->icalname = $namecal; $event->type_code = "ICALEVENT"; } + $event->icalname = $namecal; $event->icalcolor = $colorcal; $usertime = 0; // We dont modify date because we want to have date into memory datep and datef stored as GMT date. Compensation will be done during output. $event->datep = $datestart + $usertime; From 61cce2e700684e39e5e9ccdacf4b885547238a9b Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Fri, 21 Aug 2020 06:46:00 +0000 Subject: [PATCH 4/6] Fixing style errors. --- htdocs/comm/action/index.php | 5 ++--- htdocs/user/class/user.class.php | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/htdocs/comm/action/index.php b/htdocs/comm/action/index.php index e5ab1c02902..dafd9c4aecf 100644 --- a/htdocs/comm/action/index.php +++ b/htdocs/comm/action/index.php @@ -1062,13 +1062,12 @@ if (count($listofextcals)) $event->ref = $event->id; $userId = $userstatic->findUserIdByEmail($namecal); - if(!empty($userId)) + if (!empty($userId)) { $event->userassigned[$userId] = $userId; $event->percentage = -1; } - else - { + else { $event->type_code = "ICALEVENT"; } diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php index 6d278e635fa..4d250c02fb7 100644 --- a/htdocs/user/class/user.class.php +++ b/htdocs/user/class/user.class.php @@ -3248,12 +3248,12 @@ class User extends CommonObject /** * Find a user by the given e-mail or part of a e-mail and return its user id when found * - * @param string $email The full e-mail or a part of a e-mail + * @param string $email The full e-mail or a part of a e-mail * @return int|null The id of the user when found, otherwise null */ public function findUserIdByEmail($email) { - if($this->findUserIdByEmailCache[$email]) + if ($this->findUserIdByEmailCache[$email]) { return $this->findUserIdByEmailCache[$email]; } @@ -3274,8 +3274,8 @@ class User extends CommonObject return 0; } - $this->findUserIdByEmailCache[$email] = (int)$obj->rowid; + $this->findUserIdByEmailCache[$email] = (int) $obj->rowid; - return (int)$obj->rowid; + return (int) $obj->rowid; } } From 4c851884e54e82c3bdcf6dce87d357003153d077 Mon Sep 17 00:00:00 2001 From: "Sekan, Tobias" Date: Tue, 25 Aug 2020 12:16:44 +0200 Subject: [PATCH 5/6] Address feedback --- htdocs/comm/action/index.php | 2 +- htdocs/user/class/user.class.php | 40 ++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/htdocs/comm/action/index.php b/htdocs/comm/action/index.php index e5ab1c02902..8327aedf9ce 100644 --- a/htdocs/comm/action/index.php +++ b/htdocs/comm/action/index.php @@ -1062,7 +1062,7 @@ if (count($listofextcals)) $event->ref = $event->id; $userId = $userstatic->findUserIdByEmail($namecal); - if(!empty($userId)) + if (!empty($userId) && $userId > 0) { $event->userassigned[$userId] = $userId; $event->percentage = -1; diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php index 6d278e635fa..42d2f8705fb 100644 --- a/htdocs/user/class/user.class.php +++ b/htdocs/user/class/user.class.php @@ -3240,42 +3240,62 @@ class User extends CommonObject /** * Cache the SQL results of the function "findUserIdByEmail($email)" - * + * + * NOTE: findUserIdByEmailCache[...] === -1 means not found in database + * * @var array */ private $findUserIdByEmailCache; /** - * Find a user by the given e-mail or part of a e-mail and return its user id when found + * Find a user by the given e-mail and return it's user id when found * - * @param string $email The full e-mail or a part of a e-mail - * @return int|null The id of the user when found, otherwise null + * NOTE: + * Use AGENDA_DISABLE_EXACT_USER_EMAIL_COMPARE_FOR_EXTERNAL_CALENDAR + * to disable exact e-mail search + * + * @param string $email The full e-mail (or a part of a e-mail) + * @return int <0 = user was not found, >0 = The id of the user */ public function findUserIdByEmail($email) { - if($this->findUserIdByEmailCache[$email]) + if ($this->findUserIdByEmailCache[$email]) { return $this->findUserIdByEmailCache[$email]; } + $this->findUserIdByEmailCache[$email] = -1; + + global $conf; + $sql = 'SELECT rowid'; $sql .= ' FROM '.MAIN_DB_PREFIX.'user'; - $sql .= ' WHERE email LIKE "%'.$email.'%"'; + + if (!empty($conf->global->AGENDA_DISABLE_EXACT_USER_EMAIL_COMPARE_FOR_EXTERNAL_CALENDAR)) + { + $sql .= ' WHERE email LIKE "%'.$email.'%"'; + } + else + { + $sql .= ' WHERE email = "'.$email.'"'; + } + + $sql .= ' LIMIT 1'; $resql = $this->db->query($sql); if (!$resql) { - return 0; + return -1; } $obj = $this->db->fetch_object($resql); if (!$obj) { - return 0; + return -1; } - $this->findUserIdByEmailCache[$email] = (int)$obj->rowid; + $this->findUserIdByEmailCache[$email] = (int) $obj->rowid; - return (int)$obj->rowid; + return $this->findUserIdByEmailCache[$email]; } } From 2cbb4081fe88a565c1a4d559e10229059c300f24 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Tue, 25 Aug 2020 10:20:48 +0000 Subject: [PATCH 6/6] Fixing style errors. --- htdocs/user/class/user.class.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php index 42d2f8705fb..30300ff7d91 100644 --- a/htdocs/user/class/user.class.php +++ b/htdocs/user/class/user.class.php @@ -3240,9 +3240,9 @@ class User extends CommonObject /** * Cache the SQL results of the function "findUserIdByEmail($email)" - * + * * NOTE: findUserIdByEmailCache[...] === -1 means not found in database - * + * * @var array */ private $findUserIdByEmailCache; @@ -3270,13 +3270,12 @@ class User extends CommonObject $sql = 'SELECT rowid'; $sql .= ' FROM '.MAIN_DB_PREFIX.'user'; - + if (!empty($conf->global->AGENDA_DISABLE_EXACT_USER_EMAIL_COMPARE_FOR_EXTERNAL_CALENDAR)) { $sql .= ' WHERE email LIKE "%'.$email.'%"'; } - else - { + else { $sql .= ' WHERE email = "'.$email.'"'; }