NEW : new param on load_board() function in ActionComm class to avoid duplicate code

This commit is contained in:
gauthier 2017-09-27 10:09:21 +02:00
parent d270a8e009
commit 1694737e0b
2 changed files with 27 additions and 62 deletions

View File

@ -911,19 +911,25 @@ class ActionComm extends CommonObject
/**
* Load indicators for dashboard (this->nbtodo and this->nbtodolate)
*
* @param User $user Objet user
* @param User $user Objet user
* @param int $load_state_board Charge indicateurs this->nb de tableau de bord
* @return WorkboardResponse|int <0 if KO, WorkboardResponse if OK
*/
function load_board($user)
function load_board($user, $load_state_board=0)
{
global $conf, $langs;
$sql = "SELECT a.id, a.datep as dp";
if(empty($load_state_board)) $sql = "SELECT a.id, a.datep as dp";
else {
$this->nb=array();
$sql = "SELECT count(a.id) as nb";
}
$sql.= " FROM (".MAIN_DB_PREFIX."actioncomm as a";
$sql.= ")";
if (! $user->rights->societe->client->voir && ! $user->societe_id) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON a.fk_soc = sc.fk_soc";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
$sql.= " WHERE a.percent >= 0 AND a.percent < 100";
$sql.= " WHERE 1";
if(empty($load_state_board)) $sql.= " AND a.percent >= 0 AND a.percent < 100";
$sql.= " AND a.entity IN (".getEntity('agenda').")";
if (! $user->rights->societe->client->voir && ! $user->societe_id) $sql.= " AND (a.fk_soc IS NULL OR sc.fk_user = " .$user->id . ")";
if ($user->societe_id) $sql.=" AND a.fk_soc = ".$user->societe_id;
@ -932,69 +938,28 @@ class ActionComm extends CommonObject
$resql=$this->db->query($sql);
if ($resql)
{
$agenda_static = new ActionComm($this->db);
$response = new WorkboardResponse();
$response->warning_delay = $conf->agenda->warning_delay/60/60/24;
$response->label = $langs->trans("ActionsToDo");
$response->url = DOL_URL_ROOT.'/comm/action/listactions.php?status=todo&amp;mainmenu=agenda';
if ($user->rights->agenda->allactions->read) $response->url.='&amp;filtert=-1';
$response->img = img_object('',"action",'class="inline-block valigntextmiddle"');
if(empty($load_state_board)) {
$agenda_static = new ActionComm($this->db);
$response = new WorkboardResponse();
$response->warning_delay = $conf->agenda->warning_delay/60/60/24;
$response->label = $langs->trans("ActionsToDo");
$response->url = DOL_URL_ROOT.'/comm/action/listactions.php?status=todo&amp;mainmenu=agenda';
if ($user->rights->agenda->allactions->read) $response->url.='&amp;filtert=-1';
$response->img = img_object('',"action",'class="inline-block valigntextmiddle"');
}
// This assignment in condition is not a bug. It allows walking the results.
while ($obj=$this->db->fetch_object($resql))
{
$response->nbtodo++;
$agenda_static->datep = $this->db->jdate($obj->dp);
if ($agenda_static->hasDelay()) {
$response->nbtodolate++;
}
}
return $response;
}
else
{
$this->error=$this->db->error();
return -1;
}
}
/**
* Charge indicateurs this->nb de tableau de bord
*
* @return int <0 if ko, >0 if ok
*/
function load_state_board()
{
global $user;
$this->nb=array();
$sql = "SELECT count(a.id) as nb";
$sql.= " FROM (".MAIN_DB_PREFIX."actioncomm as a";
$sql.= ")";
if (! $user->rights->societe->client->voir && ! $user->societe_id) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON a.fk_soc = sc.fk_soc";
$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON a.fk_soc = s.rowid";
$sql.= " WHERE a.entity IN (".getEntity('agenda').")";
if (! $user->rights->societe->client->voir && ! $user->societe_id) $sql.= " AND (a.fk_soc IS NULL OR sc.fk_user = " .$user->id . ")";
if ($user->societe_id) $sql.=" AND a.fk_soc = ".$user->societe_id;
if (! $user->rights->agenda->allactions->read) $sql.= " AND (a.fk_user_author = ".$user->id . " OR a.fk_user_action = ".$user->id . " OR a.fk_user_done = ".$user->id . ")";
$resql=$this->db->query($sql);
if ($resql)
{
$agenda_static = new ActionComm($this->db);
while ($obj=$this->db->fetch_object($resql))
{
$this->nb["actionscomm"]=$obj->nb;
if(empty($load_state_board)) {
$response->nbtodo++;
$agenda_static->datep = $this->db->jdate($obj->dp);
if ($agenda_static->hasDelay()) $response->nbtodolate++;
} else $this->nb["actionscomm"]=$obj->nb;
}
$this->db->free($resql);
return 1;
if(empty($load_state_board)) return $response;
else return 1;
}
else
{

View File

@ -377,7 +377,7 @@ if (! empty($conf->agenda->enabled) && $user->rights->agenda->myactions->read)
$board=new ActionComm($db);
$wb_res = $board->load_board($user);
if(!empty($conf->global->MAIN_USE_METEO_WITH_PERCENTAGE)) {
$board->load_state_board();
$board->load_board($user, 1);
$wb_res->nbtotal = (int)$board->nb['actionscomm'];
}
$dashboardlines[] = $wb_res;