From 837afa0e6560546e9bf92e6ae0820e459a508d24 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 29 Jul 2017 04:07:46 +0200
Subject: [PATCH] Fix better way to get info for contact/thirdparty on agenda

---
 htdocs/comm/action/pertype.php | 76 ++++++++++++++++++++++++++-----
 htdocs/comm/action/peruser.php | 82 +++++++++++++++++++++++++++++-----
 2 files changed, 136 insertions(+), 22 deletions(-)

diff --git a/htdocs/comm/action/pertype.php b/htdocs/comm/action/pertype.php
index 589a2731f1b..b10ab8dc05b 100644
--- a/htdocs/comm/action/pertype.php
+++ b/htdocs/comm/action/pertype.php
@@ -30,10 +30,12 @@
 require '../../main.inc.php';
 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
+require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
 require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
 require_once DOL_DOCUMENT_ROOT.'/core/lib/agenda.lib.php';
-if (! empty($conf->projet->enabled)) require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
+require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
+require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
 
 
 if (! isset($conf->global->AGENDA_MAX_EVENTS_DAY_VIEW)) $conf->global->AGENDA_MAX_EVENTS_DAY_VIEW=3;
@@ -55,7 +57,7 @@ $sortfield = GETPOST("sortfield",'alpha');
 $sortorder = GETPOST("sortorder",'alpha');
 $page = GETPOST("page","int");
 if (empty($page) || $page == -1) { $page = 0; }     // If $page is not defined, or '' or -1
-$limit = GETPOST('limit')?GETPOST('limit','int'):$conf->liste_limit;
+$limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
 $offset = $limit * $page;
 if (! $sortorder) $sortorder="ASC";
 if (! $sortfield) $sortfield="a.datec";
@@ -93,7 +95,7 @@ if (GETPOST('actioncode','array'))
 }
 else
 {
-    $actioncode=GETPOST("actioncode","alpha",3)?GETPOST("actioncode","alpha",3):(GETPOST("actioncode")=='0'?'0':(empty($conf->global->AGENDA_DEFAULT_FILTER_TYPE)?'':$conf->global->AGENDA_DEFAULT_FILTER_TYPE));
+    $actioncode=GETPOST("actioncode","alpha",3)?GETPOST("actioncode","alpha",3):(GETPOST("actioncode","alpha")=='0'?'0':(empty($conf->global->AGENDA_DEFAULT_FILTER_TYPE)?'':$conf->global->AGENDA_DEFAULT_FILTER_TYPE));
 }
 if ($actioncode == '' && empty($actioncodearray)) $actioncode=(empty($conf->global->AGENDA_DEFAULT_FILTER_TYPE)?'':$conf->global->AGENDA_DEFAULT_FILTER_TYPE);
 
@@ -354,7 +356,7 @@ $sql.= ' a.datep2,';
 $sql.= ' a.percent,';
 $sql.= ' a.fk_user_author,a.fk_user_action,';
 $sql.= ' a.transparency, a.priority, a.fulldayevent, a.location,';
-$sql.= ' a.fk_soc, a.fk_contact, a.fk_element, a.elementtype,';
+$sql.= ' a.fk_soc, a.fk_contact, a.fk_element, a.elementtype, a.fk_project,';
 $sql.= ' ca.code, ca.color';
 $sql.= ' FROM '.MAIN_DB_PREFIX.'c_actioncomm as ca, '.MAIN_DB_PREFIX."actioncomm as a";
 if (! $user->rights->societe->client->voir && ! $socid) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON a.fk_soc = sc.fk_soc";
@@ -477,6 +479,8 @@ if ($resql)
         $event->location=$obj->location;
         $event->transparency=$obj->transparency;
 
+        $event->fk_project=$obj->fk_project;
+
         $event->socid=$obj->fk_soc;
         $event->contactid=$obj->fk_contact;
         //$event->societe->id=$obj->fk_soc;			// deprecated
@@ -887,11 +891,37 @@ function show_day_events_pertype($username, $day, $month, $year, $monthshown, $s
 			        		}
 							$cases1[$h][$event->id]['string'].=' - '.$event->label;
 							$cases1[$h][$event->id]['typecode']=$event->type_code;
-							if ($event->socid)
+							$cases1[$h][$event->id]['color']=$color;
+							if ($event->fk_project > 0)
 							{
-								//$cases1[$h][$event->id]['string'].='xxx';
+								if (empty($cache_project[$event->fk_project]))
+								{
+									$tmpproj=new Project($db);
+									$tmpproj->fetch($event->fk_project);
+									$cache_project[$event->fk_project]=$tmpproj;
+								}
+								$cases1[$h][$event->id]['string'].=', '.$langs->trans("Project").': '.$cache_project[$event->fk_project]->ref.' - '.$cache_project[$event->fk_project]->title;
+							}
+							if ($event->socid > 0)
+							{
+								if (empty($cache_thirdparty[$event->socid]))
+								{
+									$tmpthirdparty=new Societe($db);
+									$tmpthirdparty->fetch($event->socid);
+									$cache_thirdparty[$event->socid]=$tmpthirdparty;
+								}
+								$cases1[$h][$event->id]['string'].=', '.$cache_thirdparty[$event->socid]->name;
+							}
+							if ($event->contactid > 0)
+							{
+								if (empty($cache_contact[$event->contactid]))
+								{
+									$tmpcontact=new Contact($db);
+									$tmpcontact->fetch($event->contactid);
+									$cache_contact[$event->contactid]=$tmpcontact;
+								}
+								$cases1[$h][$event->id]['string'].=', '.$cache_contact[$event->contactid]->getFullName($langs);
 							}
-							$cases1[$h][$event->id]['color']=$color;
 						}
 						if ($event->date_start_in_calendar < $c && $dateendtouse > $b)
 						{
@@ -907,11 +937,37 @@ function show_day_events_pertype($username, $day, $month, $year, $monthshown, $s
 			        		}
 							$cases2[$h][$event->id]['string'].=' - '.$event->label;
 							$cases2[$h][$event->id]['typecode']=$event->type_code;
-							if ($event->socid)
+							$cases2[$h][$event->id]['color']=$color;
+							if ($event->fk_project > 0)
 							{
-								//$cases2[$h][$event->id]['string'].='xxx';
+								if (empty($cache_project[$event->fk_project]))
+								{
+									$tmpproj=new Project($db);
+									$tmpproj->fetch($event->fk_project);
+									$cache_project[$event->fk_project]=$tmpproj;
+								}
+								$cases2[$h][$event->id]['string'].=', '.$langs->trans("Project").': '.$cache_project[$event->fk_project]->ref.' - '.$cache_project[$event->fk_project]->title;
+							}
+							if ($event->socid > 0)
+							{
+								if (empty($cache_thirdparty[$event->socid]))
+								{
+									$tmpthirdparty=new Societe($db);
+									$tmpthirdparty->fetch($event->socid);
+									$cache_thirdparty[$event->socid]=$tmpthirdparty;
+								}
+								$cases2[$h][$event->id]['string'].=', '.$cache_thirdparty[$event->socid]->name;
+							}
+							if ($event->contactid > 0)
+							{
+								if (empty($cache_contact[$event->contactid]))
+								{
+									$tmpcontact=new Contact($db);
+									$tmpcontact->fetch($event->contactid);
+									$cache_contact[$event->contactid]=$tmpcontact;
+								}
+								$cases2[$h][$event->id]['string'].=', '.$cache_contact[$event->contactid]->getFullName($langs);
 							}
-							$cases2[$h][$event->id]['color']=$color;
 						}
 					}
 					else
diff --git a/htdocs/comm/action/peruser.php b/htdocs/comm/action/peruser.php
index 364e63d8a1b..792d4f9760d 100644
--- a/htdocs/comm/action/peruser.php
+++ b/htdocs/comm/action/peruser.php
@@ -30,10 +30,12 @@
 require '../../main.inc.php';
 require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
+require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
 require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
 require_once DOL_DOCUMENT_ROOT.'/core/lib/agenda.lib.php';
-if (! empty($conf->projet->enabled)) require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
+require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
+require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
 
 
 if (! isset($conf->global->AGENDA_MAX_EVENTS_DAY_VIEW)) $conf->global->AGENDA_MAX_EVENTS_DAY_VIEW=3;
@@ -55,7 +57,7 @@ $sortfield = GETPOST("sortfield",'alpha');
 $sortorder = GETPOST("sortorder",'alpha');
 $page = GETPOST("page","int");
 if (empty($page) || $page == -1) { $page = 0; }     // If $page is not defined, or '' or -1
-$limit = GETPOST('limit')?GETPOST('limit','int'):$conf->liste_limit;
+$limit = GETPOST('limit','int')?GETPOST('limit','int'):$conf->liste_limit;
 $offset = $limit * $page;
 if (! $sortorder) $sortorder="ASC";
 if (! $sortfield) $sortfield="a.datec";
@@ -93,9 +95,10 @@ if (GETPOST('actioncode','array'))
 }
 else
 {
-    $actioncode=GETPOST("actioncode","alpha",3)?GETPOST("actioncode","alpha",3):(GETPOST("actioncode")=='0'?'0':(empty($conf->global->AGENDA_DEFAULT_FILTER_TYPE)?'':$conf->global->AGENDA_DEFAULT_FILTER_TYPE));
+    $actioncode=GETPOST("actioncode","alpha",3)?GETPOST("actioncode","alpha",3):(GETPOST("actioncode","alpha")=='0'?'0':(empty($conf->global->AGENDA_DEFAULT_FILTER_TYPE)?'':$conf->global->AGENDA_DEFAULT_FILTER_TYPE));
 }
 if ($actioncode == '' && empty($actioncodearray)) $actioncode=(empty($conf->global->AGENDA_DEFAULT_FILTER_TYPE)?'':$conf->global->AGENDA_DEFAULT_FILTER_TYPE);
+
 $dateselect=dol_mktime(0, 0, 0, GETPOST('dateselectmonth','int'), GETPOST('dateselectday','int'), GETPOST('dateselectyear','int'));
 if ($dateselect > 0)
 {
@@ -361,7 +364,7 @@ $sql.= ' a.datep2,';
 $sql.= ' a.percent,';
 $sql.= ' a.fk_user_author,a.fk_user_action,';
 $sql.= ' a.transparency, a.priority, a.fulldayevent, a.location,';
-$sql.= ' a.fk_soc, a.fk_contact, a.fk_element, a.elementtype,';
+$sql.= ' a.fk_soc, a.fk_contact, a.fk_element, a.elementtype, a.fk_project,';
 $sql.= ' ca.code, ca.color';
 $sql.= ' FROM '.MAIN_DB_PREFIX.'c_actioncomm as ca, '.MAIN_DB_PREFIX."actioncomm as a";
 if (! $user->rights->societe->client->voir && ! $socid) $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON a.fk_soc = sc.fk_soc";
@@ -485,6 +488,8 @@ if ($resql)
         $event->location=$obj->location;
         $event->transparency=$obj->transparency;
 
+        $event->fk_project=$obj->fk_project;
+
         $event->socid=$obj->fk_soc;
         $event->contactid=$obj->fk_contact;
         //$event->societe->id=$obj->fk_soc;			// deprecated
@@ -873,6 +878,7 @@ function show_day_events2($username, $day, $month, $year, $monthshown, $style, &
 	global $theme_datacolor;	// Array with a list of different we can use (come from theme)
 	global $cachethirdparties, $cachecontacts, $colorindexused;
 	global $begin_h, $end_h;
+	global $cache_project, $cache_thirdparty, $cache_contact;
 
 	$cases1 = array();	// Color first half hour
 	$cases2 = array(); // Color second half hour
@@ -988,13 +994,39 @@ function show_day_events2($username, $day, $month, $year, $monthshown, $style, &
 				        		if ($tmpa['mday'] == $tmpb['mday'] && $tmpa['mon'] == $tmpb['mon'] && $tmpa['year'] == $tmpb['year']) $cases1[$h][$event->id]['string'].='-'.dol_print_date($event->date_end_in_calendar,'hour');
 				        		else $cases1[$h][$event->id]['string'].='-'.dol_print_date($event->date_end_in_calendar,'dayhour');
 			        		}
-							$cases1[$h][$event->id]['string'].=' - '.$event->label;
+							if ($event->label) $cases1[$h][$event->id]['string'].=' - '.$event->label;
 							$cases1[$h][$event->id]['typecode']=$event->type_code;
-							if ($event->socid)
+							$cases1[$h][$event->id]['color']=$color;
+							if ($event->fk_project > 0)
 							{
-								//$cases1[$h][$event->id]['string'].='xxx';
+								if (empty($cache_project[$event->fk_project]))
+								{
+									$tmpproj=new Project($db);
+									$tmpproj->fetch($event->fk_project);
+									$cache_project[$event->fk_project]=$tmpproj;
+								}
+								$cases1[$h][$event->id]['string'].=', '.$langs->trans("Project").': '.$cache_project[$event->fk_project]->ref.' - '.$cache_project[$event->fk_project]->title;
+							}
+							if ($event->socid > 0)
+							{
+								if (empty($cache_thirdparty[$event->socid]))
+								{
+									$tmpthirdparty=new Societe($db);
+									$tmpthirdparty->fetch($event->socid);
+									$cache_thirdparty[$event->socid]=$tmpthirdparty;
+								}
+								$cases1[$h][$event->id]['string'].=', '.$cache_thirdparty[$event->socid]->name;
+							}
+							if ($event->contactid > 0)
+							{
+								if (empty($cache_contact[$event->contactid]))
+								{
+									$tmpcontact=new Contact($db);
+									$tmpcontact->fetch($event->contactid);
+									$cache_contact[$event->contactid]=$tmpcontact;
+								}
+								$cases1[$h][$event->id]['string'].=', '.$cache_contact[$event->contactid]->getFullName($langs);
 							}
-							$cases1[$h][$event->id]['color']=$color;
 						}
 						if ($event->date_start_in_calendar < $c && $dateendtouse > $b)
 						{
@@ -1008,13 +1040,39 @@ function show_day_events2($username, $day, $month, $year, $monthshown, $style, &
 				        		if ($tmpa['mday'] == $tmpb['mday'] && $tmpa['mon'] == $tmpb['mon'] && $tmpa['year'] == $tmpb['year']) $cases2[$h][$event->id]['string'].='-'.dol_print_date($event->date_end_in_calendar,'hour');
 				        		else $cases2[$h][$event->id]['string'].='-'.dol_print_date($event->date_end_in_calendar,'dayhour');
 			        		}
-							$cases2[$h][$event->id]['string'].=' - '.$event->label;
+							if ($event->label) $cases2[$h][$event->id]['string'].=' - '.$event->label;
 							$cases2[$h][$event->id]['typecode']=$event->type_code;
-							if ($event->socid)
+							$cases2[$h][$event->id]['color']=$color;
+							if ($event->fk_project > 0)
 							{
-								//$cases2[$h][$event->id]['string'].='xxx';
+								if (empty($cache_project[$event->fk_project]))
+								{
+									$tmpproj=new Project($db);
+									$tmpproj->fetch($event->fk_project);
+									$cache_project[$event->fk_project]=$tmpproj;
+								}
+								$cases2[$h][$event->id]['string'].=', '.$langs->trans("Project").': '.$cache_project[$event->fk_project]->ref.' - '.$cache_project[$event->fk_project]->title;
+							}
+							if ($event->socid > 0)
+							{
+								if (empty($cache_thirdparty[$event->socid]))
+								{
+									$tmpthirdparty=new Societe($db);
+									$tmpthirdparty->fetch($event->socid);
+									$cache_thirdparty[$event->socid]=$tmpthirdparty;
+								}
+								$cases2[$h][$event->id]['string'].=', '.$cache_thirdparty[$event->socid]->name;
+							}
+							if ($event->contactid > 0)
+							{
+								if (empty($cache_contact[$event->contactid]))
+								{
+									$tmpcontact=new Contact($db);
+									$tmpcontact->fetch($event->contactid);
+									$cache_contact[$event->contactid]=$tmpcontact;
+								}
+								$cases2[$h][$event->id]['string'].=', '.$cache_contact[$event->contactid]->getFullName($langs);
 							}
-							$cases2[$h][$event->id]['color']=$color;
 						}
 					}
 					else
-- 
GitLab