Merge pull request #16399 from frederic34/count_project_head

add cache on project head
This commit is contained in:
Laurent Destailleur 2021-02-26 13:04:39 +01:00 committed by GitHub
commit 3705e45dfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,7 @@
/* Copyright (C) 2006-2015 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2006-2015 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2010 Regis Houssin <regis.houssin@inodbox.com> * Copyright (C) 2010 Regis Houssin <regis.houssin@inodbox.com>
* Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es> * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr> * Copyright (C) 2018-2021 Frédéric France <frederic.france@netlogic.fr>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -44,24 +44,44 @@ function project_prepare_head(Project $project)
$head[$h][1] = $langs->trans("Project"); $head[$h][1] = $langs->trans("Project");
$head[$h][2] = 'project'; $head[$h][2] = 'project';
$h++; $h++;
$nbContacts = 0;
// Enable caching of project count Contacts
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
$cachekey = 'count_contacts_project_'.$object->id;
$dataretrieved = dol_getcache($cachekey);
$nbContact = count($project->liste_contact(-1, 'internal')) + count($project->liste_contact(-1, 'external')); if (!is_null($dataretrieved)) {
$nbContacts = $dataretrieved;
} else {
$nbContacts = count($project->liste_contact(-1, 'internal')) + count($project->liste_contact(-1, 'external'));
dol_setcache($cachekey, $nbContact, 120); // If setting cache fails, this is not a problem, so we do not test result.
}
$head[$h][0] = DOL_URL_ROOT.'/projet/contact.php?id='.$project->id; $head[$h][0] = DOL_URL_ROOT.'/projet/contact.php?id='.$project->id;
$head[$h][1] = $langs->trans("ProjectContact"); $head[$h][1] = $langs->trans("ProjectContact");
if ($nbContact > 0) { if ($nbContacts > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContact.'</span>'; $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContacts.'</span>';
} }
$head[$h][2] = 'contact'; $head[$h][2] = 'contact';
$h++; $h++;
if (empty($conf->global->PROJECT_HIDE_TASKS)) { if (empty($conf->global->PROJECT_HIDE_TASKS)) {
// Then tab for sub level of projet, i mean tasks // Then tab for sub level of projet, i mean tasks
$head[$h][0] = DOL_URL_ROOT.'/projet/tasks.php?id='.$project->id; $nbTasks = 0;
$head[$h][1] = $langs->trans("Tasks"); // Enable caching of project count Tasks
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
$cachekey = 'count_tasks_project_'.$object->id;
$dataretrieved = dol_getcache($cachekey);
if (!is_null($dataretrieved)) {
$nbTasks = $dataretrieved;
} else {
require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php'; require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
$taskstatic = new Task($db); $taskstatic = new Task($db);
$nbTasks = count($taskstatic->getTasksArray(0, 0, $project->id, 0, 0)); $nbTasks = count($taskstatic->getTasksArray(0, 0, $project->id, 0, 0));
dol_setcache($cachekey, $nbTasks, 120); // If setting cache fails, this is not a problem, so we do not test result.
}
$head[$h][0] = DOL_URL_ROOT.'/projet/tasks.php?id='.$project->id;
$head[$h][1] = $langs->trans("Tasks");
if ($nbTasks > 0) { if ($nbTasks > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbTasks).'</span>'; $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbTasks).'</span>';
} }
@ -69,6 +89,12 @@ function project_prepare_head(Project $project)
$h++; $h++;
$nbTimeSpent = 0; $nbTimeSpent = 0;
// Enable caching of project count Timespent
$cachekey = 'count_timespent_project_'.$object->id;
$dataretrieved = dol_getcache($cachekey);
if (!is_null($dataretrieved)) {
$nbTimeSpent = $dataretrieved;
} else {
$sql = "SELECT t.rowid"; $sql = "SELECT t.rowid";
//$sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as t, ".MAIN_DB_PREFIX."projet_task as pt, ".MAIN_DB_PREFIX."user as u"; //$sql .= " FROM ".MAIN_DB_PREFIX."projet_task_time as t, ".MAIN_DB_PREFIX."projet_task as pt, ".MAIN_DB_PREFIX."user as u";
//$sql .= " WHERE t.fk_user = u.rowid AND t.fk_task = pt.rowid"; //$sql .= " WHERE t.fk_user = u.rowid AND t.fk_task = pt.rowid";
@ -80,10 +106,12 @@ function project_prepare_head(Project $project)
$obj = $db->fetch_object($resql); $obj = $db->fetch_object($resql);
if ($obj) { if ($obj) {
$nbTimeSpent = 1; $nbTimeSpent = 1;
dol_setcache($cachekey, $nbTimeSpent, 120); // If setting cache fails, this is not a problem, so we do not test result.
} }
} else { } else {
dol_print_error($db); dol_print_error($db);
} }
}
$head[$h][0] = DOL_URL_ROOT.'/projet/tasks/time.php?withproject=1&projectid='.$project->id; $head[$h][0] = DOL_URL_ROOT.'/projet/tasks/time.php?withproject=1&projectid='.$project->id;
$head[$h][1] = $langs->trans("TimeSpent"); $head[$h][1] = $langs->trans("TimeSpent");
@ -98,8 +126,13 @@ function project_prepare_head(Project $project)
|| !empty($conf->propal->enabled) || !empty($conf->commande->enabled) || !empty($conf->propal->enabled) || !empty($conf->commande->enabled)
|| !empty($conf->facture->enabled) || !empty($conf->contrat->enabled) || !empty($conf->facture->enabled) || !empty($conf->contrat->enabled)
|| !empty($conf->ficheinter->enabled) || !empty($conf->agenda->enabled) || !empty($conf->deplacement->enabled)) { || !empty($conf->ficheinter->enabled) || !empty($conf->agenda->enabled) || !empty($conf->deplacement->enabled)) {
$count = 0; $nbElements = 0;
// Enable caching of thirdrparty count Contacts
$cachekey = 'count_elements_project_'.$object->id;
$dataretrieved = dol_getcache($cachekey);
if (!is_null($dataretrieved)) {
$nbElements = $dataretrieved;
} else {
if (!empty($conf->propal->enabled)) { if (!empty($conf->propal->enabled)) {
$count += $project->getElementCount('propal', 'propal'); $count += $project->getElementCount('propal', 'propal');
} }
@ -160,11 +193,11 @@ function project_prepare_head(Project $project)
if (!empty($conf->banque->enabled)) { if (!empty($conf->banque->enabled)) {
$count += $project->getElementCount('variouspayment', 'payment_various'); $count += $project->getElementCount('variouspayment', 'payment_various');
} }
}
$head[$h][0] = DOL_URL_ROOT.'/projet/element.php?id='.$project->id; $head[$h][0] = DOL_URL_ROOT.'/projet/element.php?id='.$project->id;
$head[$h][1] = $langs->trans("ProjectOverview"); $head[$h][1] = $langs->trans("ProjectOverview");
if ($count > 0) { if ($nbElements > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.$count.'</span>'; $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbElements.'</span>';
} }
$head[$h][2] = 'element'; $head[$h][2] = 'element';
$h++; $h++;
@ -207,22 +240,44 @@ function project_prepare_head(Project $project)
$h++; $h++;
} }
// Attached files and Links
$totalAttached = 0;
// Enable caching of thirdrparty count attached files and links
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
$cachekey = 'count_attached_project_'.$object->id;
$dataretrieved = dol_getcache($cachekey);
if (!is_null($dataretrieved)) {
$totalAttached = $dataretrieved;
} else {
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
$upload_dir = $conf->projet->dir_output."/".dol_sanitizeFileName($project->ref); $upload_dir = $conf->projet->dir_output."/".dol_sanitizeFileName($project->ref);
$nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$')); $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
$nbLinks = Link::count($db, $project->element, $project->id); $nbLinks = Link::count($db, $project->element, $project->id);
$totalAttached = $nbFiles + $nbLinks;
dol_setcache($cachekey, $totalAttached, 120); // If setting cache fails, this is not a problem, so we do not test result.
}
$head[$h][0] = DOL_URL_ROOT.'/projet/document.php?id='.$project->id; $head[$h][0] = DOL_URL_ROOT.'/projet/document.php?id='.$project->id;
$head[$h][1] = $langs->trans('Documents'); $head[$h][1] = $langs->trans('Documents');
if (($nbFiles + $nbLinks) > 0) { if (($totalAttached) > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>'; $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($totalAttached).'</span>';
} }
$head[$h][2] = 'document'; $head[$h][2] = 'document';
$h++; $h++;
// Manage discussion // Manage discussion
if (!empty($conf->global->PROJECT_ALLOW_COMMENT_ON_PROJECT)) { if (!empty($conf->global->PROJECT_ALLOW_COMMENT_ON_PROJECT)) {
$nbComments = 0;
// Enable caching of thirdrparty count attached files and links
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
$cachekey = 'count_attached_project_'.$object->id;
$dataretrieved = dol_getcache($cachekey);
if (!is_null($dataretrieved)) {
$nbComments = $dataretrieved;
} else {
$nbComments = $project->getNbComments(); $nbComments = $project->getNbComments();
dol_setcache($cachekey, $nbComments, 120); // If setting cache fails, this is not a problem, so we do not test result.
}
$head[$h][0] = DOL_URL_ROOT.'/projet/comment.php?id='.$project->id; $head[$h][0] = DOL_URL_ROOT.'/projet/comment.php?id='.$project->id;
$head[$h][1] = $langs->trans("CommentLink"); $head[$h][1] = $langs->trans("CommentLink");
if ($nbComments > 0) { if ($nbComments > 0) {