From 933a9446038b7b2010321fd120f1ce945b492a11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Wed, 17 Feb 2021 22:14:21 +0100 Subject: [PATCH 1/8] count actioncomm for thirdparty tab agenda --- htdocs/core/lib/company.lib.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php index a424657e206..867180ef9b6 100644 --- a/htdocs/core/lib/company.lib.php +++ b/htdocs/core/lib/company.lib.php @@ -304,8 +304,22 @@ function societe_prepare_head(Societe $object) $head[$h][0] = DOL_URL_ROOT.'/societe/agenda.php?socid='.$object->id; $head[$h][1] = $langs->trans("Events"); if (!empty($conf->agenda->enabled) && (!empty($user->rights->agenda->myactions->read) || !empty($user->rights->agenda->allactions->read))) { + $nbNote = 0; + $sql = "SELECT COUNT(id) as nb"; + $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm"; + $sql .= " WHERE fk_soc = ".$object->id; + $resql = $db->query($sql); + if ($resql) { + $obj = $db->fetch_object($resql); + $nbNote = $obj->nb; + } else { + dol_print_error($db); + } $head[$h][1] .= '/'; $head[$h][1] .= $langs->trans("Agenda"); + if ($nbNote > 0) { + $head[$h][1] .= ''.$nbNote.''; + } } $head[$h][2] = 'agenda'; $h++; From 45e947ee2b73063cd4cb7e8e55f80367d1707eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Wed, 17 Feb 2021 22:16:07 +0100 Subject: [PATCH 2/8] count actioncomm for thirdparty tab agenda --- htdocs/core/lib/company.lib.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php index 867180ef9b6..1fd7b653925 100644 --- a/htdocs/core/lib/company.lib.php +++ b/htdocs/core/lib/company.lib.php @@ -304,21 +304,21 @@ function societe_prepare_head(Societe $object) $head[$h][0] = DOL_URL_ROOT.'/societe/agenda.php?socid='.$object->id; $head[$h][1] = $langs->trans("Events"); if (!empty($conf->agenda->enabled) && (!empty($user->rights->agenda->myactions->read) || !empty($user->rights->agenda->allactions->read))) { - $nbNote = 0; + $nbEvent = 0; $sql = "SELECT COUNT(id) as nb"; $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm"; $sql .= " WHERE fk_soc = ".$object->id; $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); - $nbNote = $obj->nb; + $nbEvent = $obj->nb; } else { dol_print_error($db); } $head[$h][1] .= '/'; $head[$h][1] .= $langs->trans("Agenda"); - if ($nbNote > 0) { - $head[$h][1] .= ''.$nbNote.''; + if ($nbEvent > 0) { + $head[$h][1] .= ''.$nbEvent.''; } } $head[$h][2] = 'agenda'; @@ -326,9 +326,9 @@ function societe_prepare_head(Societe $object) // Log /*$head[$h][0] = DOL_URL_ROOT.'/societe/info.php?socid='.$object->id; - $head[$h][1] = $langs->trans("Info"); - $head[$h][2] = 'info'; - $h++;*/ + $head[$h][1] = $langs->trans("Info"); + $head[$h][2] = 'info'; + $h++;*/ complete_head_from_modules($conf, $langs, $object, $head, $h, 'thirdparty', 'remove'); @@ -1003,7 +1003,7 @@ function show_contacts($conf, $langs, $db, $object, $backtopage = '') } elseif (in_array($key, array('role'))) { print $formcompany->showRoles("search_roles", $contactstatic, 'edit', $search_roles); } else { - print ''; + print ''; } print ''; } From c7efd68916b1dec7f4c15362aace2c87ca063737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Thu, 18 Feb 2021 18:45:04 +0100 Subject: [PATCH 3/8] fix actioncomm index --- htdocs/comm/action/index.php | 139 ++++++++++++++++------------------- 1 file changed, 63 insertions(+), 76 deletions(-) diff --git a/htdocs/comm/action/index.php b/htdocs/comm/action/index.php index ab712c89847..e66f84f9987 100644 --- a/htdocs/comm/action/index.php +++ b/htdocs/comm/action/index.php @@ -836,93 +836,80 @@ if ($showbirthday) } // HOLIDAYS CALENDAR -//if ($conf->global->AGENDA_SHOW_HOLIDAYS) -//{ - $sql = "SELECT u.rowid as uid, u.lastname, u.firstname, u.statut, x.rowid, x.date_debut as date_start, x.date_fin as date_end, x.halfday, x.statut as status"; - $sql .= " FROM ".MAIN_DB_PREFIX."holiday as x, ".MAIN_DB_PREFIX."user as u"; - $sql .= " WHERE u.rowid = x.fk_user"; - $sql .= " AND u.statut = '1'"; // Show only active users (0 = inactive user, 1 = active user) - $sql .= " AND (x.statut = '2' OR x.statut = '3')"; // Show only public leaves (2 = leave wait for approval, 3 = leave approved) +$sql = "SELECT u.rowid as uid, u.lastname, u.firstname, u.statut, x.rowid, x.date_debut as date_start, x.date_fin as date_end, x.halfday, x.statut as status"; +$sql .= " FROM ".MAIN_DB_PREFIX."holiday as x, ".MAIN_DB_PREFIX."user as u"; +$sql .= " WHERE u.rowid = x.fk_user"; +$sql .= " AND u.statut = '1'"; // Show only active users (0 = inactive user, 1 = active user) +$sql .= " AND (x.statut = '2' OR x.statut = '3')"; // Show only public leaves (2 = leave wait for approval, 3 = leave approved) - if ($action == 'show_day') - { - // Request only leaves for the current selected day - $sql .= " AND '".$db->escape($year)."-".$db->escape($month)."-".$db->escape($day)."' BETWEEN x.date_debut AND x.date_fin"; - } elseif ($action == 'show_week') - { - // TODO: Add filter to reduce database request - } elseif ($action == 'show_month') - { - // TODO: Add filter to reduce database request - } +if ($action == 'show_day') { + // Request only leaves for the current selected day + $sql .= " AND '".$db->escape($year)."-".$db->escape($month)."-".$db->escape($day)."' BETWEEN x.date_debut AND x.date_fin"; +} elseif ($action == 'show_week') { + // TODO: Add filter to reduce database request +} elseif ($action == 'show_month') { + // TODO: Add filter to reduce database request +} - $resql = $db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - $i = 0; +$resql = $db->query($sql); +if ($resql) { + $num = $db->num_rows($resql); + $i = 0; - while ($i < $num) - { - $obj = $db->fetch_object($resql); + while ($i < $num) { + $obj = $db->fetch_object($resql); - $dateStartArray = dol_getdate(dol_stringtotime($obj->date_start, 1), true); - $dateEndArray = dol_getdate(dol_stringtotime($obj->date_end, 1), true); + $dateStartArray = dol_getdate(dol_stringtotime($obj->date_start, 1), true); + $dateEndArray = dol_getdate(dol_stringtotime($obj->date_end, 1), true); - $event = new ActionComm($db); + $event = new ActionComm($db); - // Need the id of the leave object for link to it - $event->id = $obj->rowid; - $event->ref = $event->id; + // Need the id of the leave object for link to it + $event->id = $obj->rowid; + $event->ref = $event->id; - $event->type_code = 'HOLIDAY'; - $event->type_label = ''; - $event->type_color = ''; - $event->type = 'holiday'; - $event->type_picto = 'holiday'; + $event->type_code = 'HOLIDAY'; + $event->type_label = ''; + $event->type_color = ''; + $event->type = 'holiday'; + $event->type_picto = 'holiday'; - $event->datep = dol_mktime(0, 0, 0, $dateStartArray['mon'], $dateStartArray['mday'], $dateStartArray['year'], true); - $event->datef = dol_mktime(0, 0, 0, $dateEndArray['mon'], $dateEndArray['mday'], $dateEndArray['year'], true); - $event->date_start_in_calendar = $event->datep; - $event->date_end_in_calendar = $event->datef; + $event->datep = dol_mktime(0, 0, 0, $dateStartArray['mon'], $dateStartArray['mday'], $dateStartArray['year'], true); + $event->datef = dol_mktime(0, 0, 0, $dateEndArray['mon'], $dateEndArray['mday'], $dateEndArray['year'], true); + $event->date_start_in_calendar = $event->datep; + $event->date_end_in_calendar = $event->datef; - if ($obj->status == 3) - { - // Show no symbol for leave with state "leave approved" - $event->percentage = -1; - } elseif ($obj->status == 2) - { - // Show TO-DO symbol for leave with state "leave wait for approval" - $event->percentage = 0; - } - - if ($obj->halfday == 1) - { - $event->label = $obj->lastname.' ('.$langs->trans("Morning").')'; - } elseif ($obj->halfday == -1) - { - $event->label = $obj->lastname.' ('.$langs->trans("Afternoon").')'; - } else { - $event->label = $obj->lastname; - } - - $daycursor = $event->date_start_in_calendar; - $annee = dol_print_date($daycursor, '%Y'); - $mois = dol_print_date($daycursor, '%m'); - $jour = dol_print_date($daycursor, '%d'); - - $daykey = dol_mktime(0, 0, 0, $mois, $jour, $annee); - - do { - $eventarray[$daykey][] = $event; - - $daykey += 60 * 60 * 24; - } while ($daykey <= $event->date_end_in_calendar); - - $i++; + if ($obj->status == 3) { + // Show no symbol for leave with state "leave approved" + $event->percentage = -1; + } elseif ($obj->status == 2) { + // Show TO-DO symbol for leave with state "leave wait for approval" + $event->percentage = 0; } + + if ($obj->halfday == 1) { + $event->label = $obj->lastname.' ('.$langs->trans("Morning").')'; + } elseif ($obj->halfday == -1) { + $event->label = $obj->lastname.' ('.$langs->trans("Afternoon").')'; + } else { + $event->label = $obj->lastname; + } + + $daycursor = $event->date_start_in_calendar; + $annee = dol_print_date($daycursor, '%Y'); + $mois = dol_print_date($daycursor, '%m'); + $jour = dol_print_date($daycursor, '%d'); + + $daykey = dol_mktime(0, 0, 0, $mois, $jour, $annee); + + do { + $eventarray[$daykey][] = $event; + $daykey += 60 * 60 * 24; + } while ($daykey <= $event->date_end_in_calendar); + + $i++; } -//} +} // EXTERNAL CALENDAR // Complete $eventarray with external import Ical From 1e3ae8348ae15f27de0c6ba4681e114501790e2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Fri, 19 Feb 2021 23:16:56 +0100 Subject: [PATCH 4/8] use cache --- htdocs/core/lib/company.lib.php | 49 +++++++--- htdocs/core/lib/memory.lib.php | 153 +++++++++++++++++--------------- 2 files changed, 121 insertions(+), 81 deletions(-) diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php index 1fd7b653925..c4565a5660c 100644 --- a/htdocs/core/lib/company.lib.php +++ b/htdocs/core/lib/company.lib.php @@ -304,16 +304,45 @@ function societe_prepare_head(Societe $object) $head[$h][0] = DOL_URL_ROOT.'/societe/agenda.php?socid='.$object->id; $head[$h][1] = $langs->trans("Events"); if (!empty($conf->agenda->enabled) && (!empty($user->rights->agenda->myactions->read) || !empty($user->rights->agenda->allactions->read))) { - $nbEvent = 0; - $sql = "SELECT COUNT(id) as nb"; - $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm"; - $sql .= " WHERE fk_soc = ".$object->id; - $resql = $db->query($sql); - if ($resql) { - $obj = $db->fetch_object($resql); - $nbEvent = $obj->nb; - } else { - dol_print_error($db); + // Enable caching of count actioncomm + $usecachekey = ''; + $found = false; + if (!empty($conf->memcached->enabled) && !empty($conf->global->MEMCACHED_SERVER)) { + // Using a memcached/memcache server + $usecachekey = 'count_event_'.$object->id; + } elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) { + // Using cache with shmop + $usecachekey = 'count_event_'.$object->id; + } + if ($usecachekey) { + require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; + $dataretrieved = dol_getcache($usecachekey); + if (is_array($dataretrieved) && count($dataretrieved)) { + $nbEvent = $dataretrieved[$usecachekey]; + $found = true; + } + } + if (!$found) { + $nbEvent = 0; + $sql = "SELECT COUNT(id) as nb"; + $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm"; + $sql .= " WHERE fk_soc = ".$object->id; + $resql = $db->query($sql); + if ($resql) { + $obj = $db->fetch_object($resql); + $nbEvent = $obj->nb; + } else { + dol_syslog('Failed to count actioncomm '.$db->lasterror(), LOG_ERR); + } + if ($usecachekey) { + $datatocache = array(); + $datatocache[$usecachekey] = $nbEvent; + $ressetcache = dol_setcache($usecachekey, $datatocache); + if ($ressetcache < 0) { + $error = 'Failed to set cache for usecachekey='.$usecachekey.' result='.$ressetcache; + dol_syslog($error, LOG_ERR); + } + } } $head[$h][1] .= '/'; $head[$h][1] .= $langs->trans("Agenda"); diff --git a/htdocs/core/lib/memory.lib.php b/htdocs/core/lib/memory.lib.php index 7b863391b24..ccf2ce7ce12 100644 --- a/htdocs/core/lib/memory.lib.php +++ b/htdocs/core/lib/memory.lib.php @@ -1,5 +1,6 @@ + * Copyright (C) 2021 Frédéric France * * 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 @@ -23,11 +24,34 @@ global $shmkeys, $shmoffset; -$shmkeys = array('main'=>1, 'admin'=>2, 'dict'=>3, 'companies'=>4, 'suppliers'=>5, 'products'=>6, - 'commercial'=>7, 'compta'=>8, 'projects'=>9, 'cashdesk'=>10, 'agenda'=>11, 'bills'=>12, - 'propal'=>13, 'boxes'=>14, 'banks'=>15, 'other'=>16, 'errors'=>17, 'members'=>18, 'ecm'=>19, - 'orders'=>20, 'users'=>21, 'help'=>22, 'stocks'=>23, 'interventions'=>24, - 'donations'=>25, 'contracts'=>26); +$shmkeys = array( + 'main' => 1, + 'admin' => 2, + 'dict' => 3, + 'companies' => 4, + 'suppliers' => 5, + 'products' => 6, + 'commercial' => 7, + 'compta' => 8, + 'projects' => 9, + 'cashdesk' => 10, + 'agenda' => 11, + 'bills' => 12, + 'propal' => 13, + 'boxes' => 14, + 'banks' => 15, + 'other' => 16, + 'errors' => 17, + 'members' => 18, + 'ecm' => 19, + 'orders' => 20, + 'users' => 21, + 'help' => 22, + 'stocks' => 23, + 'interventions' => 24, + 'donations' => 25, + 'contracts' => 26, +); $shmoffset = 1000; // Max number of entries found into a language file. If too low, some entries will be overwritten. @@ -44,51 +68,45 @@ function dol_setcache($memoryid, $data) global $conf; $result = 0; - // Using a memcached server - if (!empty($conf->memcached->enabled) && class_exists('Memcached')) - { + if (!empty($conf->memcached->enabled) && class_exists('Memcached')) { + // Using a memcached server global $dolmemcache; - if (empty($dolmemcache) || !is_object($dolmemcache)) - { - $dolmemcache = new Memcached(); - $tmparray = explode(':', $conf->global->MEMCACHED_SERVER); - $result = $dolmemcache->addServer($tmparray[0], $tmparray[1] ? $tmparray[1] : 11211); - if (!$result) return -1; - } + if (empty($dolmemcache) || !is_object($dolmemcache)) { + $dolmemcache = new Memcached(); + $tmparray = explode(':', $conf->global->MEMCACHED_SERVER); + $result = $dolmemcache->addServer($tmparray[0], $tmparray[1] ? $tmparray[1] : 11211); + if (!$result) return -1; + } - $memoryid = session_name().'_'.$memoryid; + $memoryid = session_name() . '_' . $memoryid; //$dolmemcache->setOption(Memcached::OPT_COMPRESSION, false); $dolmemcache->add($memoryid, $data); // This fails if key already exists $rescode = $dolmemcache->getResultCode(); - if ($rescode == 0) - { + if ($rescode == 0) { return count($data); } else { return -$rescode; } - } elseif (!empty($conf->memcached->enabled) && class_exists('Memcache')) - { + } elseif (!empty($conf->memcached->enabled) && class_exists('Memcache')) { + // Using a memcache server global $dolmemcache; - if (empty($dolmemcache) || !is_object($dolmemcache)) - { - $dolmemcache = new Memcache(); - $tmparray = explode(':', $conf->global->MEMCACHED_SERVER); - $result = $dolmemcache->addServer($tmparray[0], $tmparray[1] ? $tmparray[1] : 11211); - if (!$result) return -1; - } + if (empty($dolmemcache) || !is_object($dolmemcache)) { + $dolmemcache = new Memcache(); + $tmparray = explode(':', $conf->global->MEMCACHED_SERVER); + $result = $dolmemcache->addServer($tmparray[0], $tmparray[1] ? $tmparray[1] : 11211); + if (!$result) return -1; + } - $memoryid = session_name().'_'.$memoryid; + $memoryid = session_name() . '_' . $memoryid; //$dolmemcache->setOption(Memcached::OPT_COMPRESSION, false); $result = $dolmemcache->add($memoryid, $data); // This fails if key already exists - if ($result) - { + if ($result) { return count($data); } else { return -1; } - } // Using shmop - elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) - { + } elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) { + // Using shmop $result = dol_setshmop($memoryid, $data); } @@ -106,55 +124,48 @@ function dol_getcache($memoryid) global $conf; // Using a memcached server - if (!empty($conf->memcached->enabled) && class_exists('Memcached')) - { + if (!empty($conf->memcached->enabled) && class_exists('Memcached')) { global $m; - if (empty($m) || !is_object($m)) - { + if (empty($m) || !is_object($m)) { $m = new Memcached(); - $tmparray = explode(':', $conf->global->MEMCACHED_SERVER); - $result = $m->addServer($tmparray[0], $tmparray[1] ? $tmparray[1] : 11211); - if (!$result) return -1; - } + $tmparray = explode(':', $conf->global->MEMCACHED_SERVER); + $result = $m->addServer($tmparray[0], $tmparray[1] ? $tmparray[1] : 11211); + if (!$result) return -1; + } - $memoryid = session_name().'_'.$memoryid; + $memoryid = session_name() . '_' . $memoryid; //$m->setOption(Memcached::OPT_COMPRESSION, false); //print "Get memoryid=".$memoryid; $data = $m->get($memoryid); $rescode = $m->getResultCode(); //print "memoryid=".$memoryid." - rescode=".$rescode." - data=".count($data)."\n
"; //var_dump($data); - if ($rescode == 0) - { + if ($rescode == 0) { return $data; } else { return -$rescode; } - } elseif (!empty($conf->memcached->enabled) && class_exists('Memcache')) - { + } elseif (!empty($conf->memcached->enabled) && class_exists('Memcache')) { global $m; - if (empty($m) || !is_object($m)) - { - $m = new Memcache(); - $tmparray = explode(':', $conf->global->MEMCACHED_SERVER); - $result = $m->addServer($tmparray[0], $tmparray[1] ? $tmparray[1] : 11211); - if (!$result) return -1; - } + if (empty($m) || !is_object($m)) { + $m = new Memcache(); + $tmparray = explode(':', $conf->global->MEMCACHED_SERVER); + $result = $m->addServer($tmparray[0], $tmparray[1] ? $tmparray[1] : 11211); + if (!$result) return -1; + } - $memoryid = session_name().'_'.$memoryid; + $memoryid = session_name() . '_' . $memoryid; //$m->setOption(Memcached::OPT_COMPRESSION, false); $data = $m->get($memoryid); //print "memoryid=".$memoryid." - rescode=".$rescode." - data=".count($data)."\n
"; //var_dump($data); - if ($data) - { + if ($data) { return $data; } else { return -1; } - } // Using shmop - elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) - { + } elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) { + // Using shmop $data = dol_getshmop($memoryid); return $data; } @@ -173,7 +184,9 @@ function dol_getcache($memoryid) function dol_getshmopaddress($memoryid) { global $shmkeys, $shmoffset; - if (empty($shmkeys[$memoryid])) return 0; + if (empty($shmkeys[$memoryid])) { + return 0; + } return $shmkeys[$memoryid] + $shmoffset; } @@ -187,10 +200,11 @@ function dol_listshmop() global $shmkeys, $shmoffset; $resarray = array(); - foreach ($shmkeys as $key => $val) - { + foreach ($shmkeys as $key => $val) { $result = dol_getshmop($key); - if (!is_numeric($result) || $result > 0) $resarray[$key] = $result; + if (!is_numeric($result) || $result > 0) { + $resarray[$key] = $result; + } } return $resarray; } @@ -213,18 +227,16 @@ function dol_setshmop($memoryid, $data) $size = strlen($newdata); //print 'dol_setshmop memoryid='.$memoryid." shmkey=".$shmkey." newdata=".$size."bytes
\n"; $handle = shmop_open($shmkey, 'c', 0644, 6 + $size); - if ($handle) - { + if ($handle) { $shm_bytes_written1 = shmop_write($handle, str_pad($size, 6), 0); $shm_bytes_written2 = shmop_write($handle, $newdata, 6); - if (($shm_bytes_written1 + $shm_bytes_written2) != (6 + dol_strlen($newdata))) - { - print "Couldn't write the entire length of data\n"; + if (($shm_bytes_written1 + $shm_bytes_written2) != (6 + dol_strlen($newdata))) { + print "Couldn't write the entire length of data\n"; } shmop_close($handle); return ($shm_bytes_written1 + $shm_bytes_written2); } else { - print 'Error in shmop_open for memoryid='.$memoryid.' shmkey='.$shmkey.' 6+size=6+'.$size; + print 'Error in shmop_open for memoryid=' . $memoryid . ' shmkey=' . $shmkey . ' 6+size=6+' . $size; return -1; } } @@ -243,8 +255,7 @@ function dol_getshmop($memoryid) $shmkey = dol_getshmopaddress($memoryid); //print 'dol_getshmop memoryid='.$memoryid." shmkey=".$shmkey."
\n"; $handle = @shmop_open($shmkey, 'a', 0, 0); - if ($handle) - { + if ($handle) { $size = trim(shmop_read($handle, 0, 6)); if ($size) $data = unserialize(shmop_read($handle, 6, $size)); else return -1; From fe1aedd825c627d47e47a28bc6c74d6e42d287f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Fri, 19 Feb 2021 23:23:47 +0100 Subject: [PATCH 5/8] restore file --- htdocs/comm/action/index.php | 154 +++++++++++++++++++++-------------- 1 file changed, 95 insertions(+), 59 deletions(-) diff --git a/htdocs/comm/action/index.php b/htdocs/comm/action/index.php index e66f84f9987..f9274189109 100644 --- a/htdocs/comm/action/index.php +++ b/htdocs/comm/action/index.php @@ -114,8 +114,7 @@ if ($status == '' && !GETPOSTISSET('search_status')) $status = (empty($conf->glo $defaultview = (empty($conf->global->AGENDA_DEFAULT_VIEW) ? 'show_month' : $conf->global->AGENDA_DEFAULT_VIEW); $defaultview = (empty($user->conf->AGENDA_DEFAULT_VIEW) ? $defaultview : $user->conf->AGENDA_DEFAULT_VIEW); if (empty($action) && !GETPOSTISSET('action')) $action = $defaultview; -if ($action == 'default') // When action is default, we want a calendar view and not the list -{ +if ($action == 'default') { // When action is default, we want a calendar view and not the list $action = (($defaultview != 'show_list') ? $defaultview : 'show_month'); } if (GETPOST('viewcal', 'restricthtml') && GETPOST('action', 'alpha') != 'show_day' && GETPOST('action', 'alpha') != 'show_week') { @@ -488,8 +487,8 @@ if (!empty($conf->use_javascript_ajax)) // If javascript on $s .= "\n".''."\n"; $s .= ''."\n"; } @@ -1951,7 +1988,6 @@ function sort_events_by_date($a, $b) { return -1; } - if ($b->type_code === 'HOLIDAY') { return 1; From 99fa13cbad65518977c6746e68e0fa9e5ed9159c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sat, 20 Feb 2021 15:41:57 +0100 Subject: [PATCH 6/8] enhance caching --- htdocs/core/lib/company.lib.php | 200 +++++++++++++++++++++----------- 1 file changed, 131 insertions(+), 69 deletions(-) diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php index c4565a5660c..7d1d9b9a709 100644 --- a/htdocs/core/lib/company.lib.php +++ b/htdocs/core/lib/company.lib.php @@ -7,7 +7,7 @@ * Copyright (C) 2013-2014 Juanjo Menent * Copyright (C) 2013 Christophe Battarel * Copyright (C) 2013-2018 Alexandre Spangaro - * Copyright (C) 2015-2019 Frédéric France + * Copyright (C) 2015-2021 Frédéric France * Copyright (C) 2015 Raphaël Doursenaud * Copyright (C) 2017 Rui Strecht * Copyright (C) 2018 Ferran Marcet @@ -53,20 +53,46 @@ function societe_prepare_head(Societe $object) if (empty($conf->global->MAIN_SUPPORT_SHARED_CONTACT_BETWEEN_THIRDPARTIES)) { if (empty($conf->global->MAIN_DISABLE_CONTACTS_TAB) && $user->rights->societe->contact->lire) { //$nbContact = count($object->liste_contact(-1,'internal')) + count($object->liste_contact(-1,'external')); - $nbContact = 0; // TODO - - $sql = "SELECT COUNT(p.rowid) as nb"; - $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p"; - $sql .= " WHERE p.fk_soc = ".$object->id; - $resql = $db->query($sql); - if ($resql) { - $obj = $db->fetch_object($resql); - if ($obj) $nbContact = $obj->nb; + $nbContact = 0; + if (!empty($conf->memcached->enabled) && !empty($conf->global->MEMCACHED_SERVER)) { + // Using a memcached/memcache server + $usecachekey = 'thirdparty_countcontact_'.$object->id; + } elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) { + // Using cache with shmop + $usecachekey = 'thirdparty_countcontact_'.$object->id; + } + if ($usecachekey) { + require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; + $dataretrieved = dol_getcache($usecachekey); + if (is_array($dataretrieved) && count($dataretrieved)) { + $nbContact = $dataretrieved[$usecachekey]; + $found = true; + } + } + if (!$found) { + $sql = "SELECT COUNT(p.rowid) as nb"; + $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p"; + $sql .= " WHERE p.fk_soc = ".$object->id; + $resql = $db->query($sql); + if ($resql) { + $obj = $db->fetch_object($resql); + $nbContact = $obj->nb; + } + if ($usecachekey) { + $datatocache = array(); + $datatocache[$usecachekey] = $nbContact; + $res_setcache = dol_setcache($usecachekey, $datatocache); + if ($res_setcache < 0) { + $error = 'Failed to set cache for usecachekey='.$usecachekey.' result='.$ressetcache; + dol_syslog($error, LOG_ERR); + } + } } - $head[$h][0] = DOL_URL_ROOT.'/societe/contact.php?socid='.$object->id; $head[$h][1] = $langs->trans('ContactsAddresses'); - if ($nbContact > 0) $head[$h][1] .= ''.$nbContact.''; + if ($nbContact > 0) { + $head[$h][1] .= ''.$nbContact.''; + } $head[$h][2] = 'contact'; $h++; } @@ -74,7 +100,9 @@ function societe_prepare_head(Societe $object) $head[$h][0] = DOL_URL_ROOT.'/societe/societecontact.php?socid='.$object->id; $nbContact = count($object->liste_contact(-1, 'internal')) + count($object->liste_contact(-1, 'external')); $head[$h][1] = $langs->trans("ContactsAddresses"); - if ($nbContact > 0) $head[$h][1] .= ''.$nbContact.''; + if ($nbContact > 0) { + $head[$h][1] .= ''.$nbContact.''; + } $head[$h][2] = 'contact'; $h++; } @@ -82,9 +110,15 @@ function societe_prepare_head(Societe $object) if ($object->client == 1 || $object->client == 2 || $object->client == 3) { $head[$h][0] = DOL_URL_ROOT.'/comm/card.php?socid='.$object->id; $head[$h][1] = ''; - if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && ($object->client == 2 || $object->client == 3)) $head[$h][1] .= $langs->trans("Prospect"); - if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && $object->client == 3) $head[$h][1] .= ' | '; - if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && ($object->client == 1 || $object->client == 3)) $head[$h][1] .= $langs->trans("Customer"); + if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && ($object->client == 2 || $object->client == 3)) { + $head[$h][1] .= $langs->trans("Prospect"); + } + if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && $object->client == 3) { + $head[$h][1] .= ' | '; + } + if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && ($object->client == 1 || $object->client == 3)) { + $head[$h][1] .= $langs->trans("Customer"); + } $head[$h][2] = 'customer'; $h++; @@ -98,7 +132,9 @@ function societe_prepare_head(Societe $object) } } $supplier_module_enabled = 0; - if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || !empty($conf->supplier_proposal->enabled) || !empty($conf->supplier_order->enabled) || !empty($conf->supplier_invoice->enabled)) $supplier_module_enabled = 1; + if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || !empty($conf->supplier_proposal->enabled) || !empty($conf->supplier_order->enabled) || !empty($conf->supplier_invoice->enabled)) { + $supplier_module_enabled = 1; + } if ($supplier_module_enabled == 1 && $object->fournisseur && !empty($user->rights->fournisseur->lire)) { $head[$h][0] = DOL_URL_ROOT.'/fourn/card.php?socid='.$object->id; $head[$h][1] = $langs->trans("Supplier"); @@ -107,8 +143,6 @@ function societe_prepare_head(Societe $object) } if (!empty($conf->projet->enabled) && (!empty($user->rights->projet->lire))) { - $head[$h][0] = DOL_URL_ROOT.'/societe/project.php?socid='.$object->id; - $head[$h][1] = $langs->trans("Projects"); $nbNote = 0; $sql = "SELECT COUNT(n.rowid) as nb"; $sql .= " FROM ".MAIN_DB_PREFIX."projet as n"; @@ -116,17 +150,16 @@ function societe_prepare_head(Societe $object) $sql .= " AND entity IN (".getEntity('project').")"; $resql = $db->query($sql); if ($resql) { - $num = $db->num_rows($resql); - $i = 0; - while ($i < $num) { - $obj = $db->fetch_object($resql); - $nbNote = $obj->nb; - $i++; - } + $obj = $db->fetch_object($resql); + $nbNote = $obj->nb; } else { dol_print_error($db); } - if ($nbNote > 0) $head[$h][1] .= ''.$nbNote.''; + $head[$h][0] = DOL_URL_ROOT.'/societe/project.php?socid='.$object->id; + $head[$h][1] = $langs->trans("Projects"); + if ($nbNote > 0) { + $head[$h][1] .= ''.$nbNote.''; + } $head[$h][2] = 'project'; $h++; } @@ -180,12 +213,16 @@ function societe_prepare_head(Societe $object) //$title = $langs->trans("BankAccountsAndGateways"); $servicestatus = 0; - if (!empty($conf->global->STRIPE_LIVE) && !GETPOST('forcesandbox', 'alpha')) $servicestatus = 1; + if (!empty($conf->global->STRIPE_LIVE) && !GETPOST('forcesandbox', 'alpha')) { + $servicestatus = 1; + } include_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php'; $societeaccount = new SocieteAccount($db); $stripecu = $societeaccount->getCustomerAccount($object->id, 'stripe', $servicestatus); // Get thirdparty cu_... - if ($stripecu) $foundonexternalonlinesystem++; + if ($stripecu) { + $foundonexternalonlinesystem++; + } } $sql = "SELECT COUNT(n.rowid) as nb"; @@ -199,13 +236,8 @@ function societe_prepare_head(Societe $object) $resql = $db->query($sql); if ($resql) { - $num = $db->num_rows($resql); - $i = 0; - while ($i < $num) { - $obj = $db->fetch_object($resql); - $nbBankAccount = $obj->nb; - $i++; - } + $obj = $db->fetch_object($resql); + $nbBankAccount = $obj->nb; } else { dol_print_error($db); } @@ -214,8 +246,11 @@ function societe_prepare_head(Societe $object) $head[$h][0] = DOL_URL_ROOT.'/societe/paymentmodes.php?socid='.$object->id; $head[$h][1] = $title; - if ($foundonexternalonlinesystem) $head[$h][1] .= '...'; - elseif ($nbBankAccount > 0) $head[$h][1] .= ''.$nbBankAccount.''; + if ($foundonexternalonlinesystem) { + $head[$h][1] .= '...'; + } elseif ($nbBankAccount > 0) { + $head[$h][1] .= ''.$nbBankAccount.''; + } $head[$h][2] = 'rib'; $h++; } @@ -229,17 +264,14 @@ function societe_prepare_head(Societe $object) $sql .= " WHERE fk_soc = ".$object->id.' AND fk_website > 0'; $resql = $db->query($sql); if ($resql) { - $num = $db->num_rows($resql); - $i = 0; - while ($i < $num) { - $obj = $db->fetch_object($resql); - $nbNote = $obj->nb; - $i++; - } + $obj = $db->fetch_object($resql); + $nbNote = $obj->nb; } else { dol_print_error($db); } - if ($nbNote > 0) $head[$h][1] .= ''.$nbNote.''; + if ($nbNote > 0) { + $head[$h][1] .= ''.$nbNote.''; + } $head[$h][2] = 'website'; $h++; } @@ -253,26 +285,52 @@ function societe_prepare_head(Societe $object) if ($user->socid == 0) { // Notifications if (!empty($conf->notification->enabled)) { - $nbNote = 0; - $sql = "SELECT COUNT(n.rowid) as nb"; - $sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n"; - $sql .= " WHERE fk_soc = ".$object->id; - $resql = $db->query($sql); - if ($resql) { - $num = $db->num_rows($resql); - $i = 0; - while ($i < $num) { - $obj = $db->fetch_object($resql); - $nbNote = $obj->nb; - $i++; + // Enable caching of thirdrparty count notifications + $usecachekey = ''; + $found = false; + $nbNotif = 0; + if (!empty($conf->memcached->enabled) && !empty($conf->global->MEMCACHED_SERVER)) { + // Using a memcached/memcache server + $usecachekey = 'thirdparty_countnotif_'.$object->id; + } elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) { + // Using cache with shmop + $usecachekey = 'thirdparty_countnotif_'.$object->id; + } + if ($usecachekey) { + require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; + $dataretrieved = dol_getcache($usecachekey); + if (is_array($dataretrieved) && count($dataretrieved)) { + $nbNotif = $dataretrieved[$usecachekey]; + $found = true; + } + } + if (!$found) { + $sql = "SELECT COUNT(n.rowid) as nb"; + $sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n"; + $sql .= " WHERE fk_soc = ".$object->id; + $resql = $db->query($sql); + if ($resql) { + $obj = $db->fetch_object($resql); + $nbNotif = $obj->nb; + } else { + dol_print_error($db); + } + if ($usecachekey) { + $datatocache = array(); + $datatocache[$usecachekey] = $nbNotif; + $res_setcache = dol_setcache($usecachekey, $datatocache); + if ($res_setcache < 0) { + $error = 'Failed to set cache for usecachekey='.$usecachekey.' result='.$ressetcache; + dol_syslog($error, LOG_ERR); + } } - } else { - dol_print_error($db); } $head[$h][0] = DOL_URL_ROOT.'/societe/notify/card.php?socid='.$object->id; $head[$h][1] = $langs->trans("Notifications"); - if ($nbNote > 0) $head[$h][1] .= ''.$nbNote.''; + if ($nbNotif > 0) { + $head[$h][1] .= ''.$nbNotif.''; + } $head[$h][2] = 'notify'; $h++; } @@ -283,7 +341,9 @@ function societe_prepare_head(Societe $object) if (!empty($object->note_public)) $nbNote++; $head[$h][0] = DOL_URL_ROOT.'/societe/note.php?id='.$object->id; $head[$h][1] = $langs->trans("Notes"); - if ($nbNote > 0) $head[$h][1] .= ''.$nbNote.''; + if ($nbNote > 0) { + $head[$h][1] .= ''.$nbNote.''; + } $head[$h][2] = 'note'; $h++; @@ -296,7 +356,9 @@ function societe_prepare_head(Societe $object) $head[$h][0] = DOL_URL_ROOT.'/societe/document.php?socid='.$object->id; $head[$h][1] = $langs->trans("Documents"); - if (($nbFiles + $nbLinks) > 0) $head[$h][1] .= ''.($nbFiles + $nbLinks).''; + if (($nbFiles + $nbLinks) > 0) { + $head[$h][1] .= ''.($nbFiles + $nbLinks).''; + } $head[$h][2] = 'document'; $h++; } @@ -304,15 +366,16 @@ function societe_prepare_head(Societe $object) $head[$h][0] = DOL_URL_ROOT.'/societe/agenda.php?socid='.$object->id; $head[$h][1] = $langs->trans("Events"); if (!empty($conf->agenda->enabled) && (!empty($user->rights->agenda->myactions->read) || !empty($user->rights->agenda->allactions->read))) { - // Enable caching of count actioncomm + // Enable caching of thirdrparty count actioncomm $usecachekey = ''; $found = false; + $nbEvent = 0; if (!empty($conf->memcached->enabled) && !empty($conf->global->MEMCACHED_SERVER)) { // Using a memcached/memcache server - $usecachekey = 'count_event_'.$object->id; + $usecachekey = 'thirdparty_countevent_'.$object->id; } elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) { // Using cache with shmop - $usecachekey = 'count_event_'.$object->id; + $usecachekey = 'thirdparty_countevent_'.$object->id; } if ($usecachekey) { require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; @@ -323,7 +386,6 @@ function societe_prepare_head(Societe $object) } } if (!$found) { - $nbEvent = 0; $sql = "SELECT COUNT(id) as nb"; $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm"; $sql .= " WHERE fk_soc = ".$object->id; @@ -337,8 +399,8 @@ function societe_prepare_head(Societe $object) if ($usecachekey) { $datatocache = array(); $datatocache[$usecachekey] = $nbEvent; - $ressetcache = dol_setcache($usecachekey, $datatocache); - if ($ressetcache < 0) { + $res_setcache = dol_setcache($usecachekey, $datatocache); + if ($res_setcache < 0) { $error = 'Failed to set cache for usecachekey='.$usecachekey.' result='.$ressetcache; dol_syslog($error, LOG_ERR); } From ec1dd40a8a5a5f15313573a4ce1c4661c19bb620 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sat, 20 Feb 2021 15:52:45 +0100 Subject: [PATCH 7/8] enhance caching --- htdocs/core/lib/company.lib.php | 49 +++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php index 7d1d9b9a709..2251a21e7b3 100644 --- a/htdocs/core/lib/company.lib.php +++ b/htdocs/core/lib/company.lib.php @@ -337,8 +337,12 @@ function societe_prepare_head(Societe $object) // Notes $nbNote = 0; - if (!empty($object->note_private)) $nbNote++; - if (!empty($object->note_public)) $nbNote++; + if (!empty($object->note_private)) { + $nbNote++; + } + if (!empty($object->note_public)) { + $nbNote++; + } $head[$h][0] = DOL_URL_ROOT.'/societe/note.php?id='.$object->id; $head[$h][1] = $langs->trans("Notes"); if ($nbNote > 0) { @@ -548,7 +552,9 @@ function getCountry($searchkey, $withcode = '', $dbtouse = 0, $outputlangs = '', } $dbtouse->free($resql); return $result; - } else dol_print_error($dbtouse, ''); + } else { + dol_print_error($dbtouse, ''); + } return 'Error'; } @@ -1000,8 +1006,8 @@ function show_contacts($conf, $langs, $db, $object, $backtopage = '') // Purge search criteria if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers $search_status = ''; - $search_name = ''; - $search_roles = array(); + $search_name = ''; + $search_roles = array(); $search_address = ''; $search_poste = ''; $search = array(); @@ -1365,14 +1371,27 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon = '', $noprin $sql .= " a.fk_contact,"; $sql .= " c.code as acode, c.libelle as alabel, c.picto as apicto,"; $sql .= " u.rowid as user_id, u.login as user_login, u.photo as user_photo, u.firstname as user_firstname, u.lastname as user_lastname"; - if (is_object($filterobj) && in_array(get_class($filterobj), array('Societe', 'Client', 'Fournisseur'))) $sql .= ", sp.lastname, sp.firstname"; - elseif (is_object($filterobj) && get_class($filterobj) == 'Dolresource') { /* Nothing */ } elseif (is_object($filterobj) && get_class($filterobj) == 'Project') { /* Nothing */ } elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') $sql .= ", m.lastname, m.firstname"; - elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur') $sql .= ", o.ref"; - elseif (is_object($filterobj) && get_class($filterobj) == 'Product') $sql .= ", o.ref"; - elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') $sql .= ", o.ref"; - elseif (is_object($filterobj) && get_class($filterobj) == 'BOM') $sql .= ", o.ref"; - elseif (is_object($filterobj) && get_class($filterobj) == 'Contrat') $sql .= ", o.ref"; - elseif (is_object($filterobj) && is_array($filterobj->fields) && is_array($filterobj->fields['rowid']) && is_array($filterobj->fields['ref']) && $filterobj->table_element && $filterobj->element) $sql .= ", o.ref"; + if (is_object($filterobj) && in_array(get_class($filterobj), array('Societe', 'Client', 'Fournisseur'))) { + $sql .= ", sp.lastname, sp.firstname"; + } elseif (is_object($filterobj) && get_class($filterobj) == 'Dolresource') { + /* Nothing */ + } elseif (is_object($filterobj) && get_class($filterobj) == 'Project') { + /* Nothing */ + } elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') { + $sql .= ", m.lastname, m.firstname"; + } elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur') { + $sql .= ", o.ref"; + } elseif (is_object($filterobj) && get_class($filterobj) == 'Product') { + $sql .= ", o.ref"; + } elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') { + $sql .= ", o.ref"; + } elseif (is_object($filterobj) && get_class($filterobj) == 'BOM') { + $sql .= ", o.ref"; + } elseif (is_object($filterobj) && get_class($filterobj) == 'Contrat') { + $sql .= ", o.ref"; + } elseif (is_object($filterobj) && is_array($filterobj->fields) && is_array($filterobj->fields['rowid']) && is_array($filterobj->fields['ref']) && $filterobj->table_element && $filterobj->element) { + $sql .= ", o.ref"; + } $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm as a"; $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u on u.rowid = a.fk_user_action"; @@ -1391,7 +1410,9 @@ function show_actions_done($conf, $langs, $db, $filterobj, $objcon = '', $noprin $sql .= " ON er.resource_type = 'dolresource'"; $sql .= " AND er.element_id = a.id"; $sql .= " AND er.resource_id = ".$filterobj->id; - } elseif (is_object($filterobj) && get_class($filterobj) == 'Project') { /* Nothing */ } elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') $sql .= ", ".MAIN_DB_PREFIX."adherent as m"; + } elseif (is_object($filterobj) && get_class($filterobj) == 'Project') { + /* Nothing */ + } elseif (is_object($filterobj) && get_class($filterobj) == 'Adherent') $sql .= ", ".MAIN_DB_PREFIX."adherent as m"; elseif (is_object($filterobj) && get_class($filterobj) == 'CommandeFournisseur') $sql .= ", ".MAIN_DB_PREFIX."commande_fournisseur as o"; elseif (is_object($filterobj) && get_class($filterobj) == 'Product') $sql .= ", ".MAIN_DB_PREFIX."product as o"; elseif (is_object($filterobj) && get_class($filterobj) == 'Ticket') $sql .= ", ".MAIN_DB_PREFIX."ticket as o"; From 57e15eb46ddc9c247a7bc408a87b16bc95e6c14e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20FRANCE?= Date: Sat, 20 Feb 2021 17:12:06 +0100 Subject: [PATCH 8/8] try to apply the advice --- htdocs/core/lib/company.lib.php | 171 ++++++++++++++------------------ 1 file changed, 77 insertions(+), 94 deletions(-) diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php index 2251a21e7b3..238f069c919 100644 --- a/htdocs/core/lib/company.lib.php +++ b/htdocs/core/lib/company.lib.php @@ -54,22 +54,12 @@ function societe_prepare_head(Societe $object) if (empty($conf->global->MAIN_DISABLE_CONTACTS_TAB) && $user->rights->societe->contact->lire) { //$nbContact = count($object->liste_contact(-1,'internal')) + count($object->liste_contact(-1,'external')); $nbContact = 0; - if (!empty($conf->memcached->enabled) && !empty($conf->global->MEMCACHED_SERVER)) { - // Using a memcached/memcache server - $usecachekey = 'thirdparty_countcontact_'.$object->id; - } elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) { - // Using cache with shmop - $usecachekey = 'thirdparty_countcontact_'.$object->id; - } - if ($usecachekey) { - require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; - $dataretrieved = dol_getcache($usecachekey); - if (is_array($dataretrieved) && count($dataretrieved)) { - $nbContact = $dataretrieved[$usecachekey]; - $found = true; - } - } - if (!$found) { + $cachekey = 'thirdparty_countcontact_'.$object->id; + require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; + $dataretrieved = dol_getcache($cachekey); + if (is_array($dataretrieved) && count($dataretrieved)) { + $nbContact = $dataretrieved[$cachekey]; + } else { $sql = "SELECT COUNT(p.rowid) as nb"; $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p"; $sql .= " WHERE p.fk_soc = ".$object->id; @@ -78,14 +68,12 @@ function societe_prepare_head(Societe $object) $obj = $db->fetch_object($resql); $nbContact = $obj->nb; } - if ($usecachekey) { - $datatocache = array(); - $datatocache[$usecachekey] = $nbContact; - $res_setcache = dol_setcache($usecachekey, $datatocache); - if ($res_setcache < 0) { - $error = 'Failed to set cache for usecachekey='.$usecachekey.' result='.$ressetcache; - dol_syslog($error, LOG_ERR); - } + $datatocache = array(); + $datatocache[$cachekey] = $nbContact; + $res_setcache = dol_setcache($cachekey, $datatocache); + if ($res_setcache < 0) { + $error = 'Failed to set cache for cachekey='.$cachekey.' result='.$res_setcache; + dol_syslog($error, LOG_ERR); } } $head[$h][0] = DOL_URL_ROOT.'/societe/contact.php?socid='.$object->id; @@ -285,26 +273,14 @@ function societe_prepare_head(Societe $object) if ($user->socid == 0) { // Notifications if (!empty($conf->notification->enabled)) { - // Enable caching of thirdrparty count notifications - $usecachekey = ''; - $found = false; $nbNotif = 0; - if (!empty($conf->memcached->enabled) && !empty($conf->global->MEMCACHED_SERVER)) { - // Using a memcached/memcache server - $usecachekey = 'thirdparty_countnotif_'.$object->id; - } elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) { - // Using cache with shmop - $usecachekey = 'thirdparty_countnotif_'.$object->id; - } - if ($usecachekey) { - require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; - $dataretrieved = dol_getcache($usecachekey); - if (is_array($dataretrieved) && count($dataretrieved)) { - $nbNotif = $dataretrieved[$usecachekey]; - $found = true; - } - } - if (!$found) { + // Enable caching of thirdrparty count notifications + $cachekey = 'thirdparty_countnotif_'.$object->id; + require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; + $dataretrieved = dol_getcache($cachekey); + if (is_array($dataretrieved) && count($dataretrieved)) { + $nbNotif = $dataretrieved[$cachekey]; + } else { $sql = "SELECT COUNT(n.rowid) as nb"; $sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n"; $sql .= " WHERE fk_soc = ".$object->id; @@ -315,17 +291,14 @@ function societe_prepare_head(Societe $object) } else { dol_print_error($db); } - if ($usecachekey) { - $datatocache = array(); - $datatocache[$usecachekey] = $nbNotif; - $res_setcache = dol_setcache($usecachekey, $datatocache); - if ($res_setcache < 0) { - $error = 'Failed to set cache for usecachekey='.$usecachekey.' result='.$ressetcache; - dol_syslog($error, LOG_ERR); - } + $datatocache = array(); + $datatocache[$cachekey] = $nbNotif; + $res_setcache = dol_setcache($cachekey, $datatocache); + if ($res_setcache < 0) { + $error = 'Failed to set cache for cachekey='.$cachekey.' result='.$res_setcache; + dol_syslog($error, LOG_ERR); } } - $head[$h][0] = DOL_URL_ROOT.'/societe/notify/card.php?socid='.$object->id; $head[$h][1] = $langs->trans("Notifications"); if ($nbNotif > 0) { @@ -371,25 +344,13 @@ function societe_prepare_head(Societe $object) $head[$h][1] = $langs->trans("Events"); if (!empty($conf->agenda->enabled) && (!empty($user->rights->agenda->myactions->read) || !empty($user->rights->agenda->allactions->read))) { // Enable caching of thirdrparty count actioncomm - $usecachekey = ''; - $found = false; $nbEvent = 0; - if (!empty($conf->memcached->enabled) && !empty($conf->global->MEMCACHED_SERVER)) { - // Using a memcached/memcache server - $usecachekey = 'thirdparty_countevent_'.$object->id; - } elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) { - // Using cache with shmop - $usecachekey = 'thirdparty_countevent_'.$object->id; - } - if ($usecachekey) { - require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; - $dataretrieved = dol_getcache($usecachekey); - if (is_array($dataretrieved) && count($dataretrieved)) { - $nbEvent = $dataretrieved[$usecachekey]; - $found = true; - } - } - if (!$found) { + $cachekey = 'thirdparty_countevent_'.$object->id; + require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; + $dataretrieved = dol_getcache($cachekey); + if (is_array($dataretrieved) && count($dataretrieved)) { + $nbEvent = $dataretrieved[$cachekey]; + } else { $sql = "SELECT COUNT(id) as nb"; $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm"; $sql .= " WHERE fk_soc = ".$object->id; @@ -400,14 +361,12 @@ function societe_prepare_head(Societe $object) } else { dol_syslog('Failed to count actioncomm '.$db->lasterror(), LOG_ERR); } - if ($usecachekey) { - $datatocache = array(); - $datatocache[$usecachekey] = $nbEvent; - $res_setcache = dol_setcache($usecachekey, $datatocache); - if ($res_setcache < 0) { - $error = 'Failed to set cache for usecachekey='.$usecachekey.' result='.$ressetcache; - dol_syslog($error, LOG_ERR); - } + $datatocache = array(); + $datatocache[$cachekey] = $nbEvent; + $res_setcache = dol_setcache($cachekey, $datatocache); + if ($res_setcache < 0) { + $error = 'Failed to set cache for cachekey='.$cachekey.' result='.$res_setcache; + dol_syslog($error, LOG_ERR); } } $head[$h][1] .= '/'; @@ -521,16 +480,27 @@ function getCountry($searchkey, $withcode = '', $dbtouse = 0, $outputlangs = '', // Check parameters if (empty($searchkey) && empty($searchlabel)) { - if ($withcode === 'all') return array('id'=>'', 'code'=>'', 'label'=>''); - else return ''; + if ($withcode === 'all') { + return array('id'=>'', 'code'=>'', 'label'=>''); + } else { + return ''; + } + } + if (!is_object($dbtouse)) { + $dbtouse = $db; + } + if (!is_object($outputlangs)) { + $outputlangs = $langs; } - if (!is_object($dbtouse)) $dbtouse = $db; - if (!is_object($outputlangs)) $outputlangs = $langs; $sql = "SELECT rowid, code, label FROM ".MAIN_DB_PREFIX."c_country"; - if (is_numeric($searchkey)) $sql .= " WHERE rowid=".$searchkey; - elseif (!empty($searchkey)) $sql .= " WHERE code='".$db->escape($searchkey)."'"; - else $sql .= " WHERE label='".$db->escape($searchlabel)."'"; + if (is_numeric($searchkey)) { + $sql .= " WHERE rowid=".$searchkey; + } elseif (!empty($searchkey)) { + $sql .= " WHERE code='".$db->escape($searchkey)."'"; + } else { + $sql .= " WHERE label='".$db->escape($searchlabel)."'"; + } $resql = $dbtouse->query($sql); if ($resql) { @@ -542,11 +512,17 @@ function getCountry($searchkey, $withcode = '', $dbtouse = 0, $outputlangs = '', if ($entconv) $label = ($obj->code && ($outputlangs->trans("Country".$obj->code) != "Country".$obj->code)) ? $outputlangs->trans("Country".$obj->code) : $label; else $label = ($obj->code && ($outputlangs->transnoentitiesnoconv("Country".$obj->code) != "Country".$obj->code)) ? $outputlangs->transnoentitiesnoconv("Country".$obj->code) : $label; } - if ($withcode == 1) $result = $label ? "$obj->code - $label" : "$obj->code"; - elseif ($withcode == 2) $result = $obj->code; - elseif ($withcode == 3) $result = $obj->rowid; - elseif ($withcode === 'all') $result = array('id'=>$obj->rowid, 'code'=>$obj->code, 'label'=>$label); - else $result = $label; + if ($withcode == 1) { + $result = $label ? "$obj->code - $label" : "$obj->code"; + } elseif ($withcode == 2) { + $result = $obj->code; + } elseif ($withcode == 3) { + $result = $obj->rowid; + } elseif ($withcode === 'all') { + $result = array('id'=>$obj->rowid, 'code'=>$obj->code, 'label'=>$label); + } else { + $result = $label; + } } else { $result = 'NotDefined'; } @@ -577,7 +553,9 @@ function getState($id, $withcode = '', $dbtouse = 0, $withregion = 0, $outputlan { global $db, $langs; - if (!is_object($dbtouse)) $dbtouse = $db; + if (!is_object($dbtouse)) { + $dbtouse = $db; + } $sql = "SELECT d.rowid as id, d.code_departement as code, d.nom as name, d.active, c.label as country, c.code as country_code, r.code_region as region_code, r.nom as region_name FROM"; $sql .= " ".MAIN_DB_PREFIX."c_departements as d, ".MAIN_DB_PREFIX."c_regions as r,".MAIN_DB_PREFIX."c_country as c"; @@ -640,7 +618,9 @@ function currency_name($code_iso, $withcode = '', $outputlangs = null) { global $langs, $db; - if (empty($outputlangs)) $outputlangs = $langs; + if (empty($outputlangs)) { + $outputlangs = $langs; + } $outputlangs->load("dict"); @@ -660,8 +640,11 @@ function currency_name($code_iso, $withcode = '', $outputlangs = null) if ($num) { $obj = $db->fetch_object($resql); $label = ($obj->label != '-' ? $obj->label : ''); - if ($withcode) return ($label == $code_iso) ? "$code_iso" : "$code_iso - $label"; - else return $label; + if ($withcode) { + return ($label == $code_iso) ? "$code_iso" : "$code_iso - $label"; + } else { + return $label; + } } else { return $code_iso; }