diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php index a424657e206..238f069c919 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,34 @@ 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; + $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; + $resql = $db->query($sql); + if ($resql) { + $obj = $db->fetch_object($resql); + $nbContact = $obj->nb; + } + $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; $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 +88,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 +98,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 +120,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 +131,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 +138,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 +201,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 +224,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 +234,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 +252,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,37 +273,54 @@ 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++; - } + $nbNotif = 0; + // 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 { - dol_print_error($db); + $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); + } + $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 ($nbNote > 0) $head[$h][1] .= ''.$nbNote.''; + if ($nbNotif > 0) { + $head[$h][1] .= ''.$nbNotif.''; + } $head[$h][2] = 'notify'; $h++; } // 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) $head[$h][1] .= ''.$nbNote.''; + if ($nbNote > 0) { + $head[$h][1] .= ''.$nbNote.''; + } $head[$h][2] = 'note'; $h++; @@ -296,7 +333,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,17 +343,46 @@ 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 thirdrparty count actioncomm + $nbEvent = 0; + $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; + $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); + } + $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] .= '/'; $head[$h][1] .= $langs->trans("Agenda"); + if ($nbEvent > 0) { + $head[$h][1] .= ''.$nbEvent.''; + } } $head[$h][2] = 'agenda'; $h++; // 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'); @@ -412,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) { @@ -433,17 +512,25 @@ 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'; } $dbtouse->free($resql); return $result; - } else dol_print_error($dbtouse, ''); + } else { + dol_print_error($dbtouse, ''); + } return 'Error'; } @@ -466,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"; @@ -529,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"); @@ -549,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; } @@ -895,8 +989,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(); @@ -989,7 +1083,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 ''; } @@ -1260,14 +1354,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"; @@ -1286,7 +1393,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"; diff --git a/htdocs/core/lib/memory.lib.php b/htdocs/core/lib/memory.lib.php index 95e9aff52d3..11fa6082b05 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. @@ -45,51 +69,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); } @@ -112,12 +130,12 @@ function dol_getcache($memoryid) global $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); @@ -132,13 +150,13 @@ function dol_getcache($memoryid) } 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; - } + $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
"; @@ -168,7 +186,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; } @@ -182,10 +202,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; } @@ -208,18 +229,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; } } @@ -238,8 +257,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;