index.php 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  1. <?php
  2. /* Copyright (C) 2001-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2003 Eric Seigne <erics@rycks.com>
  4. * Copyright (C) 2004-2018 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2014 Cedric GROSS <c.gross@kreiz-it.fr>
  8. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  9. * Copyright (C) 2017 Open-DSI <support@open-dsi.fr>
  10. * Copyright (C) 2018-2021 Frédéric France <frederic.france@netlogic.fr>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  24. */
  25. /**
  26. * \file htdocs/comm/action/index.php
  27. * \ingroup agenda
  28. * \brief Home page of calendar events
  29. */
  30. require '../../main.inc.php';
  31. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  33. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/actioncomm.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/lib/agenda.lib.php';
  36. if (!empty($conf->projet->enabled)) {
  37. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php';
  38. }
  39. if (!isset($conf->global->AGENDA_MAX_EVENTS_DAY_VIEW)) {
  40. $conf->global->AGENDA_MAX_EVENTS_DAY_VIEW = 3;
  41. }
  42. if (empty($conf->global->AGENDA_EXT_NB)) {
  43. $conf->global->AGENDA_EXT_NB = 5;
  44. }
  45. $MAXAGENDA = $conf->global->AGENDA_EXT_NB;
  46. $filter = GETPOST("search_filter", 'alpha', 3) ?GETPOST("search_filter", 'alpha', 3) : GETPOST("filter", 'alpha', 3);
  47. $filtert = GETPOST("search_filtert", "int", 3) ?GETPOST("search_filtert", "int", 3) : GETPOST("filtert", "int", 3);
  48. $usergroup = GETPOST("search_usergroup", "int", 3) ?GETPOST("search_usergroup", "int", 3) : GETPOST("usergroup", "int", 3);
  49. $showbirthday = empty($conf->use_javascript_ajax) ?GETPOST("showbirthday", "int") : 1;
  50. // If not choice done on calendar owner (like on left menu link "Agenda"), we filter on user.
  51. if (empty($filtert) && empty($conf->global->AGENDA_ALL_CALENDARS)) {
  52. $filtert = $user->id;
  53. }
  54. $newparam = '';
  55. $sortfield = GETPOST('sortfield', 'aZ09comma');
  56. $sortorder = GETPOST('sortorder', 'aZ09comma');
  57. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  58. if (empty($page) || $page == -1) {
  59. $page = 0;
  60. } // If $page is not defined, or '' or -1
  61. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
  62. $offset = $limit * $page;
  63. if (!$sortorder) {
  64. $sortorder = "ASC";
  65. }
  66. if (!$sortfield) {
  67. $sortfield = "a.datec";
  68. }
  69. // Security check
  70. $socid = GETPOST("search_socid", "int") ?GETPOST("search_socid", "int") : GETPOST("socid", "int");
  71. if ($user->socid) {
  72. $socid = $user->socid;
  73. }
  74. if ($socid < 0) {
  75. $socid = '';
  76. }
  77. $canedit = 1;
  78. if (empty($user->rights->agenda->myactions->read)) {
  79. accessforbidden();
  80. }
  81. if (empty($user->rights->agenda->allactions->read)) {
  82. $canedit = 0;
  83. }
  84. if (empty($user->rights->agenda->allactions->read) || $filter == 'mine') { // If no permission to see all, we show only affected to me
  85. $filtert = $user->id;
  86. }
  87. $action = GETPOST('action', 'aZ09');
  88. $mode = GETPOST('mode', 'aZ09');
  89. if (empty($mode) && preg_match('/show_/', $action)) {
  90. $mode = $action; // For backward compatibility
  91. }
  92. $resourceid = GETPOST("search_resourceid", "int");
  93. $year = GETPOST("year", "int") ?GETPOST("year", "int") : date("Y");
  94. $month = GETPOST("month", "int") ?GETPOST("month", "int") : date("m");
  95. $week = GETPOST("week", "int") ?GETPOST("week", "int") : date("W");
  96. $day = GETPOST("day", "int") ?GETPOST("day", "int") : date("d");
  97. $pid = GETPOST("search_projectid", "int", 3) ?GETPOST("search_projectid", "int", 3) : GETPOST("projectid", "int", 3);
  98. $status = GETPOST("search_status", 'aZ09') ?GETPOST("search_status", 'aZ09') : GETPOST("status", 'aZ09'); // status may be 0, 50, 100, 'todo'
  99. $type = GETPOST("search_type", 'aZ09') ?GETPOST("search_type", 'aZ09') : GETPOST("type", 'aZ09');
  100. $maxprint = (isset($_GET["maxprint"]) ?GETPOST("maxprint") : $conf->global->AGENDA_MAX_EVENTS_DAY_VIEW);
  101. $optioncss = GETPOST('optioncss', 'aZ'); // Option for the css output (always '' except when 'print')
  102. $dateselect = dol_mktime(0, 0, 0, GETPOST('dateselectmonth', 'int'), GETPOST('dateselectday', 'int'), GETPOST('dateselectyear', 'int'));
  103. if ($dateselect > 0) {
  104. $day = GETPOST('dateselectday', 'int');
  105. $month = GETPOST('dateselectmonth', 'int');
  106. $year = GETPOST('dateselectyear', 'int');
  107. }
  108. // Set actioncode (this code must be same for setting actioncode into peruser, listacton and index)
  109. if (GETPOST('search_actioncode', 'array:aZ09')) {
  110. $actioncode = GETPOST('search_actioncode', 'array:aZ09', 3);
  111. if (!count($actioncode)) {
  112. $actioncode = '0';
  113. }
  114. } else {
  115. $actioncode = GETPOST("search_actioncode", "alpha", 3) ?GETPOST("search_actioncode", "alpha", 3) : (GETPOST("search_actioncode") == '0' ? '0' : (empty($conf->global->AGENDA_DEFAULT_FILTER_TYPE) ? '' : $conf->global->AGENDA_DEFAULT_FILTER_TYPE));
  116. }
  117. if ($actioncode == '' && empty($actioncodearray)) {
  118. $actioncode = (empty($conf->global->AGENDA_DEFAULT_FILTER_TYPE) ? '' : $conf->global->AGENDA_DEFAULT_FILTER_TYPE);
  119. }
  120. if ($status == '' && !GETPOSTISSET('search_status')) {
  121. $status = (empty($conf->global->AGENDA_DEFAULT_FILTER_STATUS) ? '' : $conf->global->AGENDA_DEFAULT_FILTER_STATUS);
  122. }
  123. $defaultview = (empty($conf->global->AGENDA_DEFAULT_VIEW) ? 'show_month' : $conf->global->AGENDA_DEFAULT_VIEW);
  124. $defaultview = (empty($user->conf->AGENDA_DEFAULT_VIEW) ? $defaultview : $user->conf->AGENDA_DEFAULT_VIEW);
  125. if (empty($mode) && !GETPOSTISSET('mode')) {
  126. $mode = $defaultview;
  127. }
  128. if ($mode == 'default') { // When action is default, we want a calendar view and not the list
  129. $mode = (($defaultview != 'show_list') ? $defaultview : 'show_month');
  130. }
  131. if (GETPOST('viewcal', 'int') && GETPOST('mode', 'alpha') != 'show_day' && GETPOST('mode', 'alpha') != 'show_week') {
  132. $mode = 'show_month'; $day = '';
  133. } // View by month
  134. if (GETPOST('viewweek', 'int') || GETPOST('mode', 'alpha') == 'show_week') {
  135. $mode = 'show_week'; $week = ($week ? $week : date("W")); $day = ($day ? $day : date("d"));
  136. } // View by week
  137. if (GETPOST('viewday', 'int') || GETPOST('mode', 'alpha') == 'show_day') {
  138. $mode = 'show_day'; $day = ($day ? $day : date("d"));
  139. } // View by day
  140. $object = new ActionComm($db);
  141. // Load translation files required by the page
  142. $langs->loadLangs(array('agenda', 'other', 'commercial'));
  143. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  144. $hookmanager->initHooks(array('agenda'));
  145. $result = restrictedArea($user, 'agenda', 0, 'actioncomm&societe', 'myactions|allactions', 'fk_soc', 'id');
  146. if ($user->socid && $socid) {
  147. $result = restrictedArea($user, 'societe', $socid);
  148. }
  149. /*
  150. * Actions
  151. */
  152. if (GETPOST("viewlist", 'alpha') || $mode == 'show_list') {
  153. $param = '';
  154. if (is_array($_POST)) {
  155. foreach ($_POST as $key => $val) {
  156. if ($key == 'token') {
  157. continue;
  158. }
  159. $param .= '&'.$key.'='.urlencode($val);
  160. }
  161. }
  162. if (!preg_match('/action=/', $param)) {
  163. $param .= ($param ? '&' : '').'mode=show_list';
  164. }
  165. //print $param;
  166. header("Location: ".DOL_URL_ROOT.'/comm/action/list.php?'.$param);
  167. exit;
  168. }
  169. if (GETPOST("viewperuser", 'alpha') || $mode == 'show_peruser') {
  170. $param = '';
  171. if (is_array($_POST)) {
  172. foreach ($_POST as $key => $val) {
  173. if ($key == 'token') {
  174. continue;
  175. }
  176. $param .= '&'.$key.'='.urlencode($val);
  177. }
  178. }
  179. //print $param;
  180. header("Location: ".DOL_URL_ROOT.'/comm/action/peruser.php?'.$param);
  181. exit;
  182. }
  183. /*
  184. if ($action == 'delete_action' && $user->rights->agenda->delete) {
  185. $event = new ActionComm($db);
  186. $event->fetch($actionid);
  187. $event->fetch_optionals();
  188. $event->fetch_userassigned();
  189. $event->oldcopy = clone $event;
  190. $result = $event->delete();
  191. }
  192. */
  193. /*
  194. * View
  195. */
  196. $parameters = array(
  197. 'socid' => $socid,
  198. 'status' => $status,
  199. 'year' => $year,
  200. 'month' => $month,
  201. 'day' => $day,
  202. 'type' => $type,
  203. 'maxprint' => $maxprint,
  204. 'filter' => $filter,
  205. 'filtert' => $filtert,
  206. 'showbirthday' => $showbirthday,
  207. 'canedit' => $canedit,
  208. 'optioncss' => $optioncss,
  209. 'actioncode' => $actioncode,
  210. 'pid' => $pid,
  211. 'resourceid' => $resourceid,
  212. 'usergroup' => $usergroup,
  213. );
  214. $reshook = $hookmanager->executeHooks('beforeAgenda', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  215. if ($reshook < 0) {
  216. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  217. }
  218. $help_url = 'EN:Module_Agenda_En|FR:Module_Agenda|ES:M&oacute;dulo_Agenda';
  219. llxHeader('', $langs->trans("Agenda"), $help_url);
  220. $form = new Form($db);
  221. $companystatic = new Societe($db);
  222. $contactstatic = new Contact($db);
  223. $userstatic = new User($db);
  224. $now = dol_now();
  225. $nowarray = dol_getdate($now);
  226. $nowyear = $nowarray['year'];
  227. $nowmonth = $nowarray['mon'];
  228. $nowday = $nowarray['mday'];
  229. $listofextcals = array();
  230. // Define list of external calendars (global admin setup)
  231. if (empty($conf->global->AGENDA_DISABLE_EXT)) {
  232. $i = 0;
  233. while ($i < $MAXAGENDA) {
  234. $i++;
  235. $source = 'AGENDA_EXT_SRC'.$i;
  236. $name = 'AGENDA_EXT_NAME'.$i;
  237. $offsettz = 'AGENDA_EXT_OFFSETTZ'.$i;
  238. $color = 'AGENDA_EXT_COLOR'.$i;
  239. $default = 'AGENDA_EXT_ACTIVEBYDEFAULT'.$i;
  240. $buggedfile = 'AGENDA_EXT_BUGGEDFILE'.$i;
  241. if (!empty($conf->global->$source) && !empty($conf->global->$name)) {
  242. // Note: $conf->global->buggedfile can be empty or 'uselocalandtznodaylight' or 'uselocalandtzdaylight'
  243. $listofextcals[] = array(
  244. 'src'=>$conf->global->$source,
  245. 'name'=>$conf->global->$name,
  246. 'offsettz' => (!empty($conf->global->$offsettz) ? $conf->global->$offsettz : 0),
  247. 'color'=>$conf->global->$color,
  248. 'default'=>$conf->global->$default,
  249. 'buggedfile'=>(isset($conf->global->buggedfile) ? $conf->global->buggedfile : 0)
  250. );
  251. }
  252. }
  253. }
  254. // Define list of external calendars (user setup)
  255. if (empty($user->conf->AGENDA_DISABLE_EXT)) {
  256. $i = 0;
  257. while ($i < $MAXAGENDA) {
  258. $i++;
  259. $source = 'AGENDA_EXT_SRC_'.$user->id.'_'.$i;
  260. $name = 'AGENDA_EXT_NAME_'.$user->id.'_'.$i;
  261. $offsettz = 'AGENDA_EXT_OFFSETTZ_'.$user->id.'_'.$i;
  262. $color = 'AGENDA_EXT_COLOR_'.$user->id.'_'.$i;
  263. $enabled = 'AGENDA_EXT_ENABLED_'.$user->id.'_'.$i;
  264. $default = 'AGENDA_EXT_ACTIVEBYDEFAULT_'.$user->id.'_'.$i;
  265. $buggedfile = 'AGENDA_EXT_BUGGEDFILE_'.$user->id.'_'.$i;
  266. if (!empty($user->conf->$source) && !empty($user->conf->$name)) {
  267. // Note: $conf->global->buggedfile can be empty or 'uselocalandtznodaylight' or 'uselocalandtzdaylight'
  268. $listofextcals[] = array(
  269. 'src'=>$user->conf->$source,
  270. 'name'=>$user->conf->$name,
  271. 'offsettz' => (!empty($user->conf->$offsettz) ? $user->conf->$offsettz : 0),
  272. 'color'=>$user->conf->$color,
  273. 'default'=>$user->conf->$default,
  274. 'buggedfile'=>(isset($user->conf->buggedfile) ? $user->conf->buggedfile : 0)
  275. );
  276. }
  277. }
  278. }
  279. if (empty($mode) || $mode == 'show_month') {
  280. $prev = dol_get_prev_month($month, $year);
  281. $prev_year = $prev['year'];
  282. $prev_month = $prev['month'];
  283. $next = dol_get_next_month($month, $year);
  284. $next_year = $next['year'];
  285. $next_month = $next['month'];
  286. $max_day_in_prev_month = date("t", dol_mktime(0, 0, 0, $prev_month, 1, $prev_year, 'gmt')); // Nb of days in previous month
  287. $max_day_in_month = date("t", dol_mktime(0, 0, 0, $month, 1, $year)); // Nb of days in next month
  288. // tmpday is a negative or null cursor to know how many days before the 1st to show on month view (if tmpday=0, 1st is monday)
  289. $tmpday = -date("w", dol_mktime(12, 0, 0, $month, 1, $year, 'gmt')) + 2; // date('w') is 0 fo sunday
  290. $tmpday += ((isset($conf->global->MAIN_START_WEEK) ? $conf->global->MAIN_START_WEEK : 1) - 1);
  291. if ($tmpday >= 1) {
  292. $tmpday -= 7; // If tmpday is 0 we start with sunday, if -6, we start with monday of previous week.
  293. }
  294. // Define firstdaytoshow and lastdaytoshow (warning: lastdaytoshow is last second to show + 1)
  295. $firstdaytoshow = dol_mktime(0, 0, 0, $prev_month, $max_day_in_prev_month + $tmpday, $prev_year, 'tzuserrel');
  296. $next_day = 7 - ($max_day_in_month + 1 - $tmpday) % 7;
  297. if ($next_day < 6) {
  298. $next_day += 7;
  299. }
  300. $lastdaytoshow = dol_mktime(0, 0, 0, $next_month, $next_day, $next_year, 'tzuserrel');
  301. }
  302. if ($mode == 'show_week') {
  303. $prev = dol_get_first_day_week($day, $month, $year);
  304. $prev_year = $prev['prev_year'];
  305. $prev_month = $prev['prev_month'];
  306. $prev_day = $prev['prev_day'];
  307. $first_day = $prev['first_day'];
  308. $first_month = $prev['first_month'];
  309. $first_year = $prev['first_year'];
  310. $week = $prev['week'];
  311. $day = (int) $day;
  312. $next = dol_get_next_week($first_day, $week, $first_month, $first_year);
  313. $next_year = $next['year'];
  314. $next_month = $next['month'];
  315. $next_day = $next['day'];
  316. // Define firstdaytoshow and lastdaytoshow (warning: lastdaytoshow is last second to show + 1)
  317. $firstdaytoshow = dol_mktime(0, 0, 0, $first_month, $first_day, $first_year, 'tzuserrel');
  318. $lastdaytoshow = dol_time_plus_duree($firstdaytoshow, 7, 'd');
  319. $max_day_in_month = date("t", dol_mktime(0, 0, 0, $month, 1, $year, 'gmt'));
  320. $tmpday = $first_day;
  321. }
  322. if ($mode == 'show_day') {
  323. $prev = dol_get_prev_day($day, $month, $year);
  324. $prev_year = $prev['year'];
  325. $prev_month = $prev['month'];
  326. $prev_day = $prev['day'];
  327. $next = dol_get_next_day($day, $month, $year);
  328. $next_year = $next['year'];
  329. $next_month = $next['month'];
  330. $next_day = $next['day'];
  331. // Define firstdaytoshow and lastdaytoshow (warning: lastdaytoshow is last second to show + 1)
  332. $firstdaytoshow = dol_mktime(0, 0, 0, $prev_month, $prev_day, $prev_year, 'tzuserrel');
  333. $lastdaytoshow = dol_mktime(0, 0, 0, $next_month, $next_day, $next_year, 'tzuserrel');
  334. }
  335. //print 'xx'.$prev_year.'-'.$prev_month.'-'.$prev_day;
  336. //print 'xx'.$next_year.'-'.$next_month.'-'.$next_day;
  337. //print dol_print_date($firstdaytoshow,'dayhour').' '.dol_print_date($lastdaytoshow,'dayhour');
  338. /*$title = $langs->trans("DoneAndToDoActions");
  339. if ($status == 'done') $title = $langs->trans("DoneActions");
  340. if ($status == 'todo') $title = $langs->trans("ToDoActions");
  341. */
  342. $param = '';
  343. if ($actioncode || GETPOSTISSET('search_actioncode')) {
  344. if (is_array($actioncode)) {
  345. foreach ($actioncode as $str_action) {
  346. $param .= "&search_actioncode[]=".urlencode($str_action);
  347. }
  348. } else {
  349. $param .= "&search_actioncode=".urlencode($actioncode);
  350. }
  351. }
  352. if ($resourceid > 0) {
  353. $param .= "&search_resourceid=".urlencode($resourceid);
  354. }
  355. if ($status || GETPOSTISSET('status')) {
  356. $param .= "&search_status=".urlencode($status);
  357. }
  358. if ($filter) {
  359. $param .= "&search_filter=".urlencode($filter);
  360. }
  361. if ($filtert) {
  362. $param .= "&search_filtert=".urlencode($filtert);
  363. }
  364. if ($usergroup > 0) {
  365. $param .= "&search_usergroup=".urlencode($usergroup);
  366. }
  367. if ($socid > 0) {
  368. $param .= "&search_socid=".urlencode($socid);
  369. }
  370. if ($showbirthday) {
  371. $param .= "&search_showbirthday=1";
  372. }
  373. if ($pid) {
  374. $param .= "&search_projectid=".urlencode($pid);
  375. }
  376. if ($type) {
  377. $param .= "&search_type=".urlencode($type);
  378. }
  379. $param .= "&maxprint=".urlencode($maxprint);
  380. if ($mode == 'show_day' || $mode == 'show_week' || $mode == 'show_month') {
  381. $param .= '&mode='.urlencode($mode);
  382. }
  383. // Show navigation bar
  384. if (empty($mode) || $mode == 'show_month') {
  385. $nav = "<a href=\"?year=".$prev_year."&month=".$prev_month.$param."\"><i class=\"fa fa-chevron-left\"></i></a> &nbsp;\n";
  386. $nav .= " <span id=\"month_name\">".dol_print_date(dol_mktime(0, 0, 0, $month, 1, $year), "%b %Y");
  387. $nav .= " </span>\n";
  388. $nav .= " &nbsp; <a href=\"?year=".$next_year."&month=".$next_month.$param."\"><i class=\"fa fa-chevron-right\"></i></a>\n";
  389. if (empty($conf->dol_optimize_smallscreen)) {
  390. $nav .= " &nbsp; <a href=\"?year=".$nowyear."&month=".$nowmonth.$param."\">".$langs->trans("Today")."</a> ";
  391. }
  392. $picto = 'calendar';
  393. }
  394. if ($mode == 'show_week') {
  395. $nav = "<a href=\"?year=".$prev_year."&month=".$prev_month."&day=".$prev_day.$param."\"><i class=\"fa fa-chevron-left\" title=\"".dol_escape_htmltag($langs->trans("Previous"))."\"></i></a> &nbsp;\n";
  396. $nav .= " <span id=\"month_name\">".dol_print_date(dol_mktime(0, 0, 0, $first_month, $first_day, $first_year), "%Y").", ".$langs->trans("Week")." ".$week;
  397. $nav .= " </span>\n";
  398. $nav .= " &nbsp; <a href=\"?year=".$next_year."&month=".$next_month."&day=".$next_day.$param."\"><i class=\"fa fa-chevron-right\" title=\"".dol_escape_htmltag($langs->trans("Next"))."\"></i></a>\n";
  399. if (empty($conf->dol_optimize_smallscreen)) {
  400. $nav .= " &nbsp; <a href=\"?year=".$nowyear."&month=".$nowmonth."&day=".$nowday.$param."\">".$langs->trans("Today")."</a> ";
  401. }
  402. $picto = 'calendarweek';
  403. }
  404. if ($mode == 'show_day') {
  405. $nav = "<a href=\"?year=".$prev_year."&month=".$prev_month."&day=".$prev_day.$param."\"><i class=\"fa fa-chevron-left\"></i></a> &nbsp;\n";
  406. $nav .= " <span id=\"month_name\">".dol_print_date(dol_mktime(0, 0, 0, $month, $day, $year), "daytextshort");
  407. $nav .= " </span>\n";
  408. $nav .= " &nbsp; <a href=\"?year=".$next_year."&month=".$next_month."&day=".$next_day.$param."\"><i class=\"fa fa-chevron-right\"></i></a>\n";
  409. if (empty($conf->dol_optimize_smallscreen)) {
  410. $nav .= " &nbsp; <a href=\"?year=".$nowyear."&amp;month=".$nowmonth."&amp;day=".$nowday.$param."\">".$langs->trans("Today")."</a> ";
  411. }
  412. $picto = 'calendarday';
  413. }
  414. $nav .= $form->selectDate($dateselect, 'dateselect', 0, 0, 1, '', 1, 0);
  415. //$nav .= ' <input type="submit" class="button button-save" name="submitdateselect" value="'.$langs->trans("Refresh").'">';
  416. $nav .= '<button type="submit" class="liste_titre button_search valignmiddle" name="button_search_x" value="x"><span class="fa fa-search"></span></button>';
  417. // Must be after the nav definition
  418. $paramnodate = $param;
  419. $param .= '&year='.$year.'&month='.$month.($day ? '&day='.$day : '');
  420. //print 'x'.$param;
  421. /*$tabactive = '';
  422. if ($mode == 'show_month') $tabactive = 'cardmonth';
  423. if ($mode == 'show_week') $tabactive = 'cardweek';
  424. if ($mode == 'show_day') $tabactive = 'cardday';
  425. if ($mode == 'show_list') $tabactive = 'cardlist';
  426. if ($mode == 'show_pertuser') $tabactive = 'cardperuser';
  427. if ($mode == 'show_pertype') $tabactive = 'cardpertype';
  428. */
  429. $paramnoaction = preg_replace('/mode=[a-z_]+/', '', preg_replace('/action=[a-z_]+/', '', $param));
  430. $paramnoactionodate = preg_replace('/mode=[a-z_]+/', '', preg_replace('/action=[a-z_]+/', '', $paramnodate));
  431. $head = calendars_prepare_head($paramnoaction);
  432. print '<form method="POST" id="searchFormList" class="listactionsfilter" action="'.$_SERVER["PHP_SELF"].'">'."\n";
  433. if ($optioncss != '') {
  434. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  435. }
  436. print '<input type="hidden" name="token" value="'.newToken().'">';
  437. print '<input type="hidden" name="mode" value="'.$mode.'">';
  438. //print dol_get_fiche_head($head, $tabactive, $langs->trans('Agenda'), 0, 'action');
  439. //print_actions_filter($form, $canedit, $status, $year, $month, $day, $showbirthday, 0, $filtert, 0, $pid, $socid, $action, $listofextcals, $actioncode, $usergroup, '', $resourceid);
  440. //print dol_get_fiche_end();
  441. $viewmode = '';
  442. $viewmode .= '<a class="btnTitle reposition" href="'.DOL_URL_ROOT.'/comm/action/list.php?mode=show_list&restore_lastsearch_values=1'.$paramnoactionodate.'">';
  443. //$viewmode .= '<span class="fa paddingleft imgforviewmode valignmiddle btnTitle-icon">';
  444. $viewmode .= img_picto($langs->trans("List"), 'object_list', 'class="imgforviewmode pictoactionview block"');
  445. //$viewmode .= '</span>';
  446. $viewmode .= '<span class="valignmiddle text-plus-circle btnTitle-label hideonsmartphone">'.$langs->trans("ViewList").'</span></a>';
  447. $viewmode .= '<a class="btnTitle'.($mode == 'show_month' ? ' btnTitleSelected' : '').' reposition" href="'.DOL_URL_ROOT.'/comm/action/index.php?mode=show_month&year='.(isset($object->datep) ? dol_print_date($object->datep, '%Y') : $year).'&month='.(isset($object->datep) ? dol_print_date($object->datep, '%m') : $month).'&day='.(isset($object->datep) ? dol_print_date($object->datep, '%d') : $day).$paramnoactionodate.'">';
  448. //$viewmode .= '<span class="fa paddingleft imgforviewmode valignmiddle btnTitle-icon">';
  449. $viewmode .= img_picto($langs->trans("ViewCal"), 'object_calendarmonth', 'class="pictoactionview block"');
  450. //$viewmode .= '</span>';
  451. $viewmode .= '<span class="valignmiddle text-plus-circle btnTitle-label hideonsmartphone">'.$langs->trans("ViewCal").'</span></a>';
  452. $viewmode .= '<a class="btnTitle'.($mode == 'show_week' ? ' btnTitleSelected' : '').' reposition" href="'.DOL_URL_ROOT.'/comm/action/index.php?mode=show_week&year='.(isset($object->datep) ? dol_print_date($object->datep, '%Y') : $year).'&month='.(isset($object->datep) ? dol_print_date($object->datep, '%m') : $month).'&day='.(isset($object->datep) ? dol_print_date($object->datep, '%d') : $day).$paramnoactionodate.'">';
  453. //$viewmode .= '<span class="fa paddingleft imgforviewmode valignmiddle btnTitle-icon">';
  454. $viewmode .= img_picto($langs->trans("ViewWeek"), 'object_calendarweek', 'class="pictoactionview block"');
  455. //$viewmode .= '</span>';
  456. $viewmode .= '<span class="valignmiddle text-plus-circle btnTitle-label hideonsmartphone">'.$langs->trans("ViewWeek").'</span></a>';
  457. $viewmode .= '<a class="btnTitle'.($mode == 'show_day' ? ' btnTitleSelected' : '').' reposition" href="'.DOL_URL_ROOT.'/comm/action/index.php?mode=show_day&year='.(isset($object->datep) ? dol_print_date($object->datep, '%Y') : $year).'&month='.(isset($object->datep) ? dol_print_date($object->datep, '%m') : $month).'&day='.(isset($object->datep) ? dol_print_date($object->datep, '%d') : $day).$paramnoactionodate.'">';
  458. //$viewmode .= '<span class="fa paddingleft imgforviewmode valignmiddle btnTitle-icon">';
  459. $viewmode .= img_picto($langs->trans("ViewDay"), 'object_calendarday', 'class="pictoactionview block"');
  460. //$viewmode .= '</span>';
  461. $viewmode .= '<span class="valignmiddle text-plus-circle btnTitle-label hideonsmartphone">'.$langs->trans("ViewDay").'</span></a>';
  462. $viewmode .= '<a class="btnTitle reposition" href="'.DOL_URL_ROOT.'/comm/action/peruser.php?mode=show_peruser&year='.(isset($object->datep) ? dol_print_date($object->datep, '%Y') : $year).'&month='.(isset($object->datep) ? dol_print_date($object->datep, '%m') : $month).'&day='.(isset($object->datep) ? dol_print_date($object->datep, '%d') : $day).$paramnoactionodate.'">';
  463. //$viewmode .= '<span class="fa paddingleft imgforviewmode valignmiddle btnTitle-icon">';
  464. $viewmode .= img_picto($langs->trans("ViewPerUser"), 'object_calendarperuser', 'class="pictoactionview block"');
  465. //$viewmode .= '</span>';
  466. $viewmode .= '<span class="valignmiddle text-plus-circle btnTitle-label hideonsmartphone">'.$langs->trans("ViewPerUser").'</span></a>';
  467. // Add more views from hooks
  468. $parameters = array(); $object = null;
  469. $reshook = $hookmanager->executeHooks('addCalendarView', $parameters, $object, $action);
  470. if (empty($reshook)) {
  471. $viewmode .= $hookmanager->resPrint;
  472. } elseif ($reshook > 1) {
  473. $viewmode = $hookmanager->resPrint;
  474. }
  475. $viewmode .= '<span class="marginrightonly"></span>'; // To add a space before the navigation tools
  476. $newcardbutton = '';
  477. $newparam = '';
  478. if ($user->rights->agenda->myactions->create || $user->rights->agenda->allactions->create) {
  479. $tmpforcreatebutton = dol_getdate(dol_now(), true);
  480. $newparam .= '&month='.((int) $month).'&year='.((int) $tmpforcreatebutton['year']).'&mode='.urlencode($mode);
  481. //$param='month='.$monthshown.'&year='.$year;
  482. $hourminsec = '100000';
  483. $newcardbutton .= dolGetButtonTitle($langs->trans("AddAction"), '', 'fa fa-plus-circle', DOL_URL_ROOT.'/comm/action/card.php?action=create&datep='.sprintf("%04d%02d%02d", $tmpforcreatebutton['year'], $tmpforcreatebutton['mon'], $tmpforcreatebutton['mday']).$hourminsec.'&backtopage='.urlencode($_SERVER["PHP_SELF"].($newparam ? '?'.$newparam : '')));
  484. }
  485. // Define the legend/list of calendard to show
  486. $s = ''; $link = '';
  487. $showextcals = $listofextcals;
  488. if (!empty($conf->use_javascript_ajax)) { // If javascript on
  489. $s .= "\n".'<!-- Div to calendars selectors -->'."\n";
  490. $s .= '<script type="text/javascript">'."\n";
  491. $s .= 'jQuery(document).ready(function () {'."\n";
  492. $s .= 'jQuery(".check_birthday").click(function() { console.log("Toggle birthdays"); jQuery(".family_birthday").toggle(); });'."\n";
  493. $s .= 'jQuery(".check_holiday").click(function() { console.log("Toggle holidays"); jQuery(".family_holiday").toggle(); });'."\n";
  494. if ($mode == "show_week" || $mode == "show_month" || empty($mode)) {
  495. // Code to enable drag and drop
  496. $s .= 'jQuery( "div.sortable" ).sortable({connectWith: ".sortable", placeholder: "ui-state-highlight", items: "div.movable", receive: function( event, ui ) {'."\n";
  497. // Code to submit form
  498. $s .= 'console.log("submit form to record new event");'."\n";
  499. //$s.='console.log(event.target);';
  500. $s .= 'var newval = jQuery(event.target).closest("div.dayevent").attr("id");'."\n";
  501. $s .= 'console.log("found parent div.dayevent with id = "+newval);'."\n";
  502. $s .= 'var frm=jQuery("#searchFormList");'."\n";
  503. $s .= 'var newurl = ui.item.find("a.cal_event").attr("href");'."\n";
  504. $s .= 'console.log(newurl);'."\n";
  505. $s .= 'frm.attr("action", newurl).children("#newdate").val(newval);frm.submit();}'."\n";
  506. $s .= '});'."\n";
  507. }
  508. $s .= '});'."\n";
  509. $s .= '</script>'."\n";
  510. // Local calendar
  511. $s .= '<div class="nowrap inline-block minheight30"><input type="checkbox" id="check_mytasks" name="check_mytasks" checked disabled> '.$langs->trans("LocalAgenda").' &nbsp; </div>';
  512. // Holiday calendar
  513. $s .= '<div class="nowrap inline-block minheight30"><input type="checkbox" id="check_holiday" name="check_holiday" class="check_holiday"><label for="check_holiday"> <span class="check_holiday_text">'.$langs->trans("Holidays").'</span></label> &nbsp; </div>';
  514. // External calendars
  515. if (is_array($showextcals) && count($showextcals) > 0) {
  516. $s .= '<script type="text/javascript">'."\n";
  517. $s .= 'jQuery(document).ready(function () {
  518. jQuery("div input[name^=\"check_ext\"]").each(function(index, elem) {
  519. var name = jQuery(elem).attr("name");
  520. if (jQuery(elem).is(":checked")) {
  521. jQuery(".family_ext" + name.replace("check_ext", "")).show();
  522. } else {
  523. jQuery(".family_ext" + name.replace("check_ext", "")).hide();
  524. }
  525. });
  526. jQuery("div input[name^=\"check_ext\"]").click(function() {
  527. var name = $(this).attr("name");
  528. jQuery(".family_ext" + name.replace("check_ext", "")).toggle();
  529. });
  530. });' . "\n";
  531. $s .= '</script>'."\n";
  532. foreach ($showextcals as $val) {
  533. $htmlname = md5($val['name']);
  534. if (!empty($val['default'])) {
  535. $default = "checked";
  536. } else {
  537. $default = '';
  538. }
  539. $s .= '<div class="nowrap inline-block minheight30"><input type="checkbox" id="check_ext'.$htmlname.'" name="check_ext'.$htmlname.'" '.$default.'> <label for="check_ext'.$htmlname.'">'.$val['name'].'</label> &nbsp; </div>';
  540. }
  541. }
  542. // Birthdays
  543. $s .= '<div class="nowrap inline-block minheight30"><input type="checkbox" id="check_birthday" name="check_birthday" class="check_birthday"><label for="check_birthday"> <span class="check_birthday_text">'.$langs->trans("AgendaShowBirthdayEvents").'</span></label> &nbsp; </div>';
  544. // Calendars from hooks
  545. $parameters = array(); $object = null;
  546. $reshook = $hookmanager->executeHooks('addCalendarChoice', $parameters, $object, $action);
  547. if (empty($reshook)) {
  548. $s .= $hookmanager->resPrint;
  549. } elseif ($reshook > 1) {
  550. $s = $hookmanager->resPrint;
  551. }
  552. $s .= "\n".'<!-- End div to calendars selectors -->'."\n";
  553. } else { // If javascript off
  554. $newparam = $param; // newparam is for birthday links
  555. $newparam = preg_replace('/showbirthday=[0-1]/i', 'showbirthday='.(empty($showbirthday) ? 1 : 0), $newparam);
  556. if (!preg_match('/showbirthday=/i', $newparam)) {
  557. $newparam .= '&showbirthday=1';
  558. }
  559. $link = '<a href="'.dol_escape_htmltag($_SERVER['PHP_SELF']);
  560. $link .= '?'.dol_escape_htmltag($newparam);
  561. $link .= '">';
  562. if (empty($showbirthday)) {
  563. $link .= $langs->trans("AgendaShowBirthdayEvents");
  564. } else {
  565. $link .= $langs->trans("AgendaHideBirthdayEvents");
  566. }
  567. $link .= '</a>';
  568. }
  569. // Load events from database into $eventarray
  570. $eventarray = array();
  571. // DEFAULT CALENDAR + AUTOEVENT CALENDAR + CONFERENCEBOOTH CALENDAR
  572. $sql = 'SELECT ';
  573. if ($usergroup > 0) {
  574. $sql .= " DISTINCT";
  575. }
  576. $sql .= ' a.id, a.label,';
  577. $sql .= ' a.datep,';
  578. $sql .= ' a.datep2,';
  579. $sql .= ' a.percent,';
  580. $sql .= ' a.fk_user_author,a.fk_user_action,';
  581. $sql .= ' a.transparency, a.priority, a.fulldayevent, a.location,';
  582. $sql .= ' a.fk_soc, a.fk_contact, a.fk_project,';
  583. $sql .= ' a.fk_element, a.elementtype,';
  584. $sql .= ' ca.code as type_code, ca.libelle as type_label, ca.color as type_color, ca.type as type_type, ca.picto as type_picto';
  585. $sql .= ' FROM '.MAIN_DB_PREFIX.'c_actioncomm as ca, '.MAIN_DB_PREFIX."actioncomm as a";
  586. if (empty($user->rights->societe->client->voir) && !$socid) {
  587. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON a.fk_soc = sc.fk_soc";
  588. }
  589. // We must filter on resource table
  590. if ($resourceid > 0) {
  591. $sql .= ", ".MAIN_DB_PREFIX."element_resources as r";
  592. }
  593. // We must filter on assignement table
  594. if ($filtert > 0 || $usergroup > 0) {
  595. $sql .= ", ".MAIN_DB_PREFIX."actioncomm_resources as ar";
  596. }
  597. if ($usergroup > 0) {
  598. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."usergroup_user as ugu ON ugu.fk_user = ar.fk_element";
  599. }
  600. $sql .= ' WHERE a.fk_action = ca.id';
  601. $sql .= ' AND a.entity IN ('.getEntity('agenda').')';
  602. // Condition on actioncode
  603. if (!empty($actioncode)) {
  604. if (empty($conf->global->AGENDA_USE_EVENT_TYPE)) {
  605. if ($actioncode == 'AC_NON_AUTO') {
  606. $sql .= " AND ca.type != 'systemauto'";
  607. } elseif ($actioncode == 'AC_ALL_AUTO') {
  608. $sql .= " AND ca.type = 'systemauto'";
  609. } else {
  610. if ($actioncode == 'AC_OTH') {
  611. $sql .= " AND ca.type != 'systemauto'";
  612. }
  613. if ($actioncode == 'AC_OTH_AUTO') {
  614. $sql .= " AND ca.type = 'systemauto'";
  615. }
  616. }
  617. } else {
  618. if ($actioncode == 'AC_NON_AUTO') {
  619. $sql .= " AND ca.type != 'systemauto'";
  620. } elseif ($actioncode == 'AC_ALL_AUTO') {
  621. $sql .= " AND ca.type = 'systemauto'";
  622. } else {
  623. if (is_array($actioncode)) {
  624. $sql .= " AND ca.code IN (".$db->sanitize("'".implode("','", $actioncode)."'", 1).")";
  625. } else {
  626. $sql .= " AND ca.code IN (".$db->sanitize("'".implode("','", explode(',', $actioncode))."'", 1).")";
  627. }
  628. }
  629. }
  630. }
  631. if ($resourceid > 0) {
  632. $sql .= " AND r.element_type = 'action' AND r.element_id = a.id AND r.resource_id = ".((int) $resourceid);
  633. }
  634. if ($pid) {
  635. $sql .= " AND a.fk_project=".((int) $pid);
  636. }
  637. if (empty($user->rights->societe->client->voir) && !$socid) {
  638. $sql .= " AND (a.fk_soc IS NULL OR sc.fk_user = ".((int) $user->id).")";
  639. }
  640. if ($socid > 0) {
  641. $sql .= " AND a.fk_soc = ".((int) $socid);
  642. }
  643. // We must filter on assignement table
  644. if ($filtert > 0 || $usergroup > 0) {
  645. $sql .= " AND ar.fk_actioncomm = a.id AND ar.element_type='user'";
  646. }
  647. //var_dump($day.' '.$month.' '.$year);
  648. if ($mode == 'show_day') {
  649. $sql .= " AND (";
  650. $sql .= " (a.datep BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month, $day, $year, 'tzuserrel'))."'";
  651. $sql .= " AND '".$db->idate(dol_mktime(23, 59, 59, $month, $day, $year, 'tzuserrel'))."')";
  652. $sql .= " OR ";
  653. $sql .= " (a.datep2 BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month, $day, $year, 'tzuserrel'))."'";
  654. $sql .= " AND '".$db->idate(dol_mktime(23, 59, 59, $month, $day, $year, 'tzuserrel'))."')";
  655. $sql .= " OR ";
  656. $sql .= " (a.datep < '".$db->idate(dol_mktime(0, 0, 0, $month, $day, $year, 'tzuserrel'))."'";
  657. $sql .= " AND a.datep2 > '".$db->idate(dol_mktime(23, 59, 59, $month, $day, $year, 'tzuserrel'))."')";
  658. $sql .= ')';
  659. } else {
  660. // To limit array
  661. $sql .= " AND (";
  662. $sql .= " (a.datep BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month, 1, $year) - (60 * 60 * 24 * 7))."'"; // Start 7 days before
  663. $sql .= " AND '".$db->idate(dol_mktime(23, 59, 59, $month, 28, $year) + (60 * 60 * 24 * 10))."')"; // End 7 days after + 3 to go from 28 to 31
  664. $sql .= " OR ";
  665. $sql .= " (a.datep2 BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $month, 1, $year) - (60 * 60 * 24 * 7))."'";
  666. $sql .= " AND '".$db->idate(dol_mktime(23, 59, 59, $month, 28, $year) + (60 * 60 * 24 * 10))."')";
  667. $sql .= " OR ";
  668. $sql .= " (a.datep < '".$db->idate(dol_mktime(0, 0, 0, $month, 1, $year) - (60 * 60 * 24 * 7))."'";
  669. $sql .= " AND a.datep2 > '".$db->idate(dol_mktime(23, 59, 59, $month, 28, $year) + (60 * 60 * 24 * 10))."')";
  670. $sql .= ')';
  671. }
  672. if ($type) {
  673. $sql .= " AND ca.id = ".((int) $type);
  674. }
  675. if ($status == '0') {
  676. $sql .= " AND a.percent = 0";
  677. }
  678. if ($status == '-1') {
  679. $sql .= " AND a.percent = -1";
  680. } // Not applicable
  681. if ($status == '50') {
  682. $sql .= " AND (a.percent > 0 AND a.percent < 100)";
  683. } // Running already started
  684. if ($status == 'done' || $status == '100') {
  685. $sql .= " AND (a.percent = 100)";
  686. }
  687. if ($status == 'todo') {
  688. $sql .= " AND (a.percent >= 0 AND a.percent < 100)";
  689. }
  690. // We must filter on assignement table
  691. if ($filtert > 0 || $usergroup > 0) {
  692. $sql .= " AND (";
  693. if ($filtert > 0) {
  694. $sql .= "ar.fk_element = ".((int) $filtert);
  695. }
  696. if ($usergroup > 0) {
  697. $sql .= ($filtert > 0 ? " OR " : "")." ugu.fk_usergroup = ".((int) $usergroup);
  698. }
  699. $sql .= ")";
  700. }
  701. // Sort on date
  702. $sql .= ' ORDER BY datep';
  703. //print $sql;
  704. dol_syslog("comm/action/index.php", LOG_DEBUG);
  705. $resql = $db->query($sql);
  706. if ($resql) {
  707. $num = $db->num_rows($resql);
  708. $MAXONSAMEPAGE = 10000; // Useless to have more. Protection to avoid memory overload when high number of event (for example after a mass import)
  709. $i = 0;
  710. while ($i < $num && $i < $MAXONSAMEPAGE) {
  711. $obj = $db->fetch_object($resql);
  712. // Discard auto action if option is on
  713. if (!empty($conf->global->AGENDA_ALWAYS_HIDE_AUTO) && $obj->type_code == 'AC_OTH_AUTO') {
  714. $i++;
  715. continue;
  716. }
  717. // Create a new object action
  718. $event = new ActionComm($db);
  719. $event->id = $obj->id;
  720. $event->ref = $event->id;
  721. $event->datep = $db->jdate($obj->datep); // datep and datef are GMT date. Example: 1970-01-01 01:00:00, jdate will return 0 if TZ of PHP server is Europe/Berlin
  722. $event->datef = $db->jdate($obj->datep2);
  723. //$event->datep_formated_gmt = dol_print_date($event->datep, 'dayhour', 'gmt');
  724. //var_dump($obj->datep);
  725. //var_dump($event->datep);
  726. $event->type_code = $obj->type_code;
  727. $event->type_label = $obj->type_label;
  728. $event->type_color = $obj->type_color;
  729. $event->type = $obj->type_type;
  730. $event->type_picto = $obj->type_picto;
  731. $event->libelle = $obj->label; // deprecated
  732. $event->label = $obj->label;
  733. $event->percentage = $obj->percent;
  734. $event->authorid = $obj->fk_user_author; // user id of creator
  735. $event->userownerid = $obj->fk_user_action; // user id of owner
  736. $event->fetch_userassigned(); // This load $event->userassigned
  737. $event->priority = $obj->priority;
  738. $event->fulldayevent = $obj->fulldayevent;
  739. $event->location = $obj->location;
  740. $event->transparency = $obj->transparency;
  741. $event->fk_element = $obj->fk_element;
  742. $event->elementtype = $obj->elementtype;
  743. $event->fk_project = $obj->fk_project;
  744. $event->thirdparty_id = $obj->fk_soc;
  745. $event->contact_id = $obj->fk_contact;
  746. // Defined date_start_in_calendar and date_end_in_calendar property
  747. // They are date start and end of action but modified to not be outside calendar view.
  748. $event->date_start_in_calendar = $event->datep;
  749. if ($event->datef != '' && $event->datef >= $event->datep) {
  750. $event->date_end_in_calendar = $event->datef;
  751. } else {
  752. $event->date_end_in_calendar = $event->datep;
  753. }
  754. // Define ponctual property
  755. if ($event->date_start_in_calendar == $event->date_end_in_calendar) {
  756. $event->ponctuel = 1;
  757. }
  758. // Check values
  759. if ($event->date_end_in_calendar < $firstdaytoshow || $event->date_start_in_calendar >= $lastdaytoshow) {
  760. // This record is out of visible range
  761. } else {
  762. if ($event->date_start_in_calendar < $firstdaytoshow) {
  763. $event->date_start_in_calendar = $firstdaytoshow;
  764. }
  765. if ($event->date_end_in_calendar >= $lastdaytoshow) {
  766. $event->date_end_in_calendar = ($lastdaytoshow - 1);
  767. }
  768. // Add an entry in actionarray for each day
  769. $daycursor = $event->date_start_in_calendar;
  770. $annee = dol_print_date($daycursor, '%Y', 'tzuserrel');
  771. $mois = dol_print_date($daycursor, '%m', 'tzuserrel');
  772. $jour = dol_print_date($daycursor, '%d', 'tzuserrel');
  773. //var_dump(dol_print_date($event->date_start_in_calendar, 'dayhour', 'gmt')); // Hour at greenwich
  774. //var_dump($annee.'-'.$mois.'-'.$jour);
  775. // Loop on each day covered by action to prepare an index to show on calendar
  776. $loop = true; $j = 0;
  777. $daykey = dol_mktime(0, 0, 0, $mois, $jour, $annee, 'gmt');
  778. do {
  779. //if ($event->id==408)
  780. //print 'daykey='.$daykey.' '.dol_print_date($daykey, 'dayhour', 'gmt').' '.$event->datep.' '.$event->datef.'<br>';
  781. $eventarray[$daykey][] = $event;
  782. $j++;
  783. $daykey += 60 * 60 * 24;
  784. if ($daykey > $event->date_end_in_calendar) {
  785. $loop = false;
  786. }
  787. } while ($loop);
  788. //print 'Event '.$i.' id='.$event->id.' (start='.dol_print_date($event->datep).'-end='.dol_print_date($event->datef);
  789. //print ' startincalendar='.dol_print_date($event->date_start_in_calendar).'-endincalendar='.dol_print_date($event->date_end_in_calendar).') was added in '.$j.' different index key of array<br>';
  790. }
  791. $i++;
  792. }
  793. } else {
  794. dol_print_error($db);
  795. }
  796. //var_dump($eventarray);
  797. // BIRTHDATES CALENDAR
  798. // Complete $eventarray with birthdates
  799. if ($showbirthday) {
  800. // Add events in array
  801. $sql = 'SELECT sp.rowid, sp.lastname, sp.firstname, sp.birthday';
  802. $sql .= ' FROM '.MAIN_DB_PREFIX.'socpeople as sp';
  803. $sql .= ' WHERE (priv=0 OR (priv=1 AND fk_user_creat='.((int) $user->id).'))';
  804. $sql .= " AND sp.entity IN (".getEntity('socpeople').")";
  805. if ($mode == 'show_day') {
  806. $sql .= ' AND MONTH(birthday) = '.((int) $month);
  807. $sql .= ' AND DAY(birthday) = '.((int) $day);
  808. } else {
  809. $sql .= ' AND MONTH(birthday) = '.((int) $month);
  810. }
  811. $sql .= ' ORDER BY birthday';
  812. dol_syslog("comm/action/index.php", LOG_DEBUG);
  813. $resql = $db->query($sql);
  814. if ($resql) {
  815. $num = $db->num_rows($resql);
  816. $i = 0;
  817. while ($i < $num) {
  818. $obj = $db->fetch_object($resql);
  819. $event = new ActionComm($db);
  820. $event->id = $obj->rowid; // We put contact id in action id for birthdays events
  821. $event->ref = $event->id;
  822. $datebirth = dol_stringtotime($obj->birthday, 1);
  823. //print 'ee'.$obj->birthday.'-'.$datebirth;
  824. $datearray = dol_getdate($datebirth, true);
  825. $event->datep = dol_mktime(0, 0, 0, $datearray['mon'], $datearray['mday'], $year, true); // For full day events, date are also GMT but they wont but converted during output
  826. $event->datef = $event->datep;
  827. $event->type_code = 'BIRTHDAY';
  828. $event->type_label = '';
  829. $event->type_color = '';
  830. $event->type = 'birthdate';
  831. $event->type_picto = 'birthdate';
  832. $event->label = $langs->trans("Birthday").' '.dolGetFirstLastname($obj->firstname, $obj->lastname);
  833. $event->percentage = 100;
  834. $event->fulldayevent = 1;
  835. $event->date_start_in_calendar = $db->jdate($event->datep);
  836. $event->date_end_in_calendar = $db->jdate($event->datef);
  837. $event->ponctuel = 0;
  838. // Add an entry in eventarray for each day
  839. $daycursor = $event->datep;
  840. $annee = dol_print_date($daycursor, '%Y', 'tzuserrel');
  841. $mois = dol_print_date($daycursor, '%m', 'tzuserrel');
  842. $jour = dol_print_date($daycursor, '%d', 'tzuserrel');
  843. $daykey = dol_mktime(0, 0, 0, $mois, $jour, $annee, 'gmt');
  844. $eventarray[$daykey][] = $event;
  845. /*$loop = true;
  846. $daykey = dol_mktime(0, 0, 0, $mois, $jour, $annee);
  847. do {
  848. $eventarray[$daykey][] = $event;
  849. $daykey += 60 * 60 * 24;
  850. if ($daykey > $event->date_end_in_calendar) $loop = false;
  851. } while ($loop);
  852. */
  853. $i++;
  854. }
  855. } else {
  856. dol_print_error($db);
  857. }
  858. }
  859. // LEAVE CALENDAR
  860. $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";
  861. $sql .= " FROM ".MAIN_DB_PREFIX."holiday as x, ".MAIN_DB_PREFIX."user as u";
  862. $sql .= " WHERE u.rowid = x.fk_user";
  863. $sql .= " AND u.statut = '1'"; // Show only active users (0 = inactive user, 1 = active user)
  864. $sql .= " AND (x.statut = '2' OR x.statut = '3')"; // Show only public leaves (2 = leave wait for approval, 3 = leave approved)
  865. if ($mode == 'show_day') {
  866. // Request only leaves for the current selected day
  867. $sql .= " AND '".$db->escape($year)."-".$db->escape($month)."-".$db->escape($day)."' BETWEEN x.date_debut AND x.date_fin"; // date_debut and date_fin are date without time
  868. } elseif ($mode == 'show_week') {
  869. // TODO: Add filter to reduce database request
  870. } elseif ($mode == 'show_month') {
  871. // TODO: Add filter to reduce database request
  872. }
  873. $resql = $db->query($sql);
  874. if ($resql) {
  875. $num = $db->num_rows($resql);
  876. $i = 0;
  877. while ($i < $num) {
  878. $obj = $db->fetch_object($resql);
  879. $event = new ActionComm($db);
  880. // Need the id of the leave object for link to it
  881. $event->id = $obj->rowid;
  882. $event->ref = $event->id;
  883. $event->type_code = 'HOLIDAY';
  884. $event->type_label = '';
  885. $event->type_color = '';
  886. $event->type = 'holiday';
  887. $event->type_picto = 'holiday';
  888. $event->datep = $db->jdate($obj->date_start);
  889. $event->datef = $db->jdate($obj->date_end);
  890. $event->date_start_in_calendar = $event->datep;
  891. $event->date_end_in_calendar = $event->datef;
  892. if ($obj->status == 3) {
  893. // Show no symbol for leave with state "leave approved"
  894. $event->percentage = -1;
  895. } elseif ($obj->status == 2) {
  896. // Show TO-DO symbol for leave with state "leave wait for approval"
  897. $event->percentage = 0;
  898. }
  899. if ($obj->halfday == 1) {
  900. $event->label = $obj->lastname.' ('.$langs->trans("Morning").')';
  901. } elseif ($obj->halfday == -1) {
  902. $event->label = $obj->lastname.' ('.$langs->trans("Afternoon").')';
  903. } else {
  904. $event->label = $obj->lastname;
  905. }
  906. $daycursor = $event->date_start_in_calendar;
  907. $annee = dol_print_date($daycursor, '%Y', 'tzuserrel');
  908. $mois = dol_print_date($daycursor, '%m', 'tzuserrel');
  909. $jour = dol_print_date($daycursor, '%d', 'tzuserrel');
  910. $daykey = dol_mktime(0, 0, 0, $mois, $jour, $annee, 'gmt');
  911. do {
  912. $eventarray[$daykey][] = $event;
  913. $daykey += 60 * 60 * 24;
  914. } while ($daykey <= $event->date_end_in_calendar);
  915. $i++;
  916. }
  917. }
  918. // EXTERNAL CALENDAR
  919. // Complete $eventarray with external import Ical
  920. if (count($listofextcals)) {
  921. require_once DOL_DOCUMENT_ROOT.'/comm/action/class/ical.class.php';
  922. foreach ($listofextcals as $extcal) {
  923. $url = $extcal['src']; // Example: https://www.google.com/calendar/ical/eldy10%40gmail.com/private-cde92aa7d7e0ef6110010a821a2aaeb/basic.ics
  924. $namecal = $extcal['name'];
  925. $offsettz = $extcal['offsettz'];
  926. $colorcal = $extcal['color'];
  927. $buggedfile = $extcal['buggedfile'];
  928. $ical = new ICal();
  929. $ical->parse($url);
  930. // After this $ical->cal['VEVENT'] contains array of events, $ical->cal['DAYLIGHT'] contains daylight info, $ical->cal['STANDARD'] contains non daylight info, ...
  931. //var_dump($ical->cal); exit;
  932. $icalevents = array();
  933. if (is_array($ical->get_event_list())) {
  934. $icalevents = array_merge($icalevents, $ical->get_event_list()); // Add $ical->cal['VEVENT']
  935. }
  936. if (is_array($ical->get_freebusy_list())) {
  937. $icalevents = array_merge($icalevents, $ical->get_freebusy_list()); // Add $ical->cal['VFREEBUSY']
  938. }
  939. if (count($icalevents) > 0) {
  940. // Duplicate all repeatable events into new entries
  941. $moreicalevents = array();
  942. foreach ($icalevents as $icalevent) {
  943. if (isset($icalevent['RRULE']) && is_array($icalevent['RRULE'])) { //repeatable event
  944. //if ($event->date_start_in_calendar < $firstdaytoshow) $event->date_start_in_calendar=$firstdaytoshow;
  945. //if ($event->date_end_in_calendar > $lastdaytoshow) $event->date_end_in_calendar=($lastdaytoshow-1);
  946. if ($icalevent['DTSTART;VALUE=DATE']) { //fullday event
  947. $datecurstart = dol_stringtotime($icalevent['DTSTART;VALUE=DATE'], 1);
  948. $datecurend = dol_stringtotime($icalevent['DTEND;VALUE=DATE'], 1) - 1; // We remove one second to get last second of day
  949. } elseif (is_array($icalevent['DTSTART']) && !empty($icalevent['DTSTART']['unixtime'])) {
  950. $datecurstart = $icalevent['DTSTART']['unixtime'];
  951. $datecurend = $icalevent['DTEND']['unixtime'];
  952. if (!empty($ical->cal['DAYLIGHT']['DTSTART']) && $datecurstart) {
  953. //var_dump($ical->cal);
  954. $tmpcurstart = $datecurstart;
  955. $tmpcurend = $datecurend;
  956. $tmpdaylightstart = dol_mktime(0, 0, 0, 1, 1, 1970, 1) + (int) $ical->cal['DAYLIGHT']['DTSTART'];
  957. $tmpdaylightend = dol_mktime(0, 0, 0, 1, 1, 1970, 1) + (int) $ical->cal['STANDARD']['DTSTART'];
  958. //var_dump($tmpcurstart);var_dump($tmpcurend); var_dump($ical->cal['DAYLIGHT']['DTSTART']);var_dump($ical->cal['STANDARD']['DTSTART']);
  959. // Edit datecurstart and datecurend
  960. if ($tmpcurstart >= $tmpdaylightstart && $tmpcurstart < $tmpdaylightend) {
  961. $datecurstart -= ((int) $ical->cal['DAYLIGHT']['TZOFFSETTO']) * 36;
  962. } else {
  963. $datecurstart -= ((int) $ical->cal['STANDARD']['TZOFFSETTO']) * 36;
  964. }
  965. if ($tmpcurend >= $tmpdaylightstart && $tmpcurstart < $tmpdaylightend) {
  966. $datecurend -= ((int) $ical->cal['DAYLIGHT']['TZOFFSETTO']) * 36;
  967. } else {
  968. $datecurend -= ((int) $ical->cal['STANDARD']['TZOFFSETTO']) * 36;
  969. }
  970. }
  971. // datecurstart and datecurend are now GMT date
  972. //var_dump($datecurstart); var_dump($datecurend); exit;
  973. } else {
  974. // Not a recongized record
  975. dol_syslog("Found a not recognized repeatable record with unknown date start", LOG_ERR);
  976. continue;
  977. }
  978. //print 'xx'.$datecurstart;exit;
  979. $interval = (empty($icalevent['RRULE']['INTERVAL']) ? 1 : $icalevent['RRULE']['INTERVAL']);
  980. $until = empty($icalevent['RRULE']['UNTIL']) ? 0 : dol_stringtotime($icalevent['RRULE']['UNTIL'], 1);
  981. $maxrepeat = empty($icalevent['RRULE']['COUNT']) ? 0 : $icalevent['RRULE']['COUNT'];
  982. if ($until && ($until + ($datecurend - $datecurstart)) < $firstdaytoshow) {
  983. continue; // We discard repeatable event that end before start date to show
  984. }
  985. if ($datecurstart >= $lastdaytoshow) {
  986. continue; // We discard repeatable event that start after end date to show
  987. }
  988. $numofevent = 0;
  989. while (($datecurstart < $lastdaytoshow) && (empty($maxrepeat) || ($numofevent < $maxrepeat))) {
  990. if ($datecurend >= $firstdaytoshow) { // We add event
  991. $newevent = $icalevent;
  992. unset($newevent['RRULE']);
  993. if ($icalevent['DTSTART;VALUE=DATE']) {
  994. $newevent['DTSTART;VALUE=DATE'] = dol_print_date($datecurstart, '%Y%m%d');
  995. $newevent['DTEND;VALUE=DATE'] = dol_print_date($datecurend + 1, '%Y%m%d');
  996. } else {
  997. $newevent['DTSTART'] = $datecurstart;
  998. $newevent['DTEND'] = $datecurend;
  999. }
  1000. $moreicalevents[] = $newevent;
  1001. }
  1002. // Jump on next occurence
  1003. $numofevent++;
  1004. $savdatecurstart = $datecurstart;
  1005. if ($icalevent['RRULE']['FREQ'] == 'DAILY') {
  1006. $datecurstart = dol_time_plus_duree($datecurstart, $interval, 'd');
  1007. $datecurend = dol_time_plus_duree($datecurend, $interval, 'd');
  1008. }
  1009. if ($icalevent['RRULE']['FREQ'] == 'WEEKLY') {
  1010. $datecurstart = dol_time_plus_duree($datecurstart, $interval, 'w');
  1011. $datecurend = dol_time_plus_duree($datecurend, $interval, 'w');
  1012. } elseif ($icalevent['RRULE']['FREQ'] == 'MONTHLY') {
  1013. $datecurstart = dol_time_plus_duree($datecurstart, $interval, 'm');
  1014. $datecurend = dol_time_plus_duree($datecurend, $interval, 'm');
  1015. } elseif ($icalevent['RRULE']['FREQ'] == 'YEARLY') {
  1016. $datecurstart = dol_time_plus_duree($datecurstart, $interval, 'y');
  1017. $datecurend = dol_time_plus_duree($datecurend, $interval, 'y');
  1018. }
  1019. // Test to avoid infinite loop ($datecurstart must increase)
  1020. if ($savdatecurstart >= $datecurstart) {
  1021. dol_syslog("Found a rule freq ".$icalevent['RRULE']['FREQ']." not managed by dolibarr code. Assume 1 week frequency.", LOG_ERR);
  1022. $datecurstart += 3600 * 24 * 7;
  1023. $datecurend += 3600 * 24 * 7;
  1024. }
  1025. }
  1026. }
  1027. }
  1028. $icalevents = array_merge($icalevents, $moreicalevents);
  1029. // Loop on each entry into cal file to know if entry is qualified and add an ActionComm into $eventarray
  1030. foreach ($icalevents as $icalevent) {
  1031. //var_dump($icalevent);
  1032. //print $icalevent['SUMMARY'].'->'.var_dump($icalevent).'<br>';exit;
  1033. if (!empty($icalevent['RRULE'])) {
  1034. continue; // We found a repeatable event. It was already split into unitary events, so we discard general rule.
  1035. }
  1036. // Create a new object action
  1037. $event = new ActionComm($db);
  1038. $addevent = false;
  1039. if (isset($icalevent['DTSTART;VALUE=DATE'])) { // fullday event
  1040. // For full day events, date are also GMT but they wont but converted using tz during output
  1041. $datestart = dol_stringtotime($icalevent['DTSTART;VALUE=DATE'], 1);
  1042. if (empty($icalevent['DTEND;VALUE=DATE'])) {
  1043. $dateend = $datestart + 86400 - 1;
  1044. } else {
  1045. $dateend = dol_stringtotime($icalevent['DTEND;VALUE=DATE'], 1) - 1; // We remove one second to get last second of day
  1046. }
  1047. //print 'x'.$datestart.'-'.$dateend;exit;
  1048. //print dol_print_date($dateend,'dayhour','gmt');
  1049. $event->fulldayevent = 1;
  1050. $addevent = true;
  1051. } elseif (!is_array($icalevent['DTSTART'])) { // not fullday event (DTSTART is not array. It is a value like '19700101T000000Z' for 00:00 in greenwitch)
  1052. $datestart = $icalevent['DTSTART'];
  1053. $dateend = $icalevent['DTEND'];
  1054. $datestart += +($offsettz * 3600);
  1055. $dateend += +($offsettz * 3600);
  1056. $addevent = true;
  1057. //var_dump($offsettz);
  1058. //var_dump(dol_print_date($datestart, 'dayhour', 'gmt'));
  1059. } elseif (isset($icalevent['DTSTART']['unixtime'])) { // File contains a local timezone + a TZ (for example when using bluemind)
  1060. $datestart = $icalevent['DTSTART']['unixtime'];
  1061. $dateend = $icalevent['DTEND']['unixtime'];
  1062. $datestart += +($offsettz * 3600);
  1063. $dateend += +($offsettz * 3600);
  1064. // $buggedfile is set to uselocalandtznodaylight if conf->global->AGENDA_EXT_BUGGEDFILEx = 'uselocalandtznodaylight'
  1065. if ($buggedfile === 'uselocalandtznodaylight') { // unixtime is a local date that does not take daylight into account, TZID is +1 for example for 'Europe/Paris' in summer instead of 2
  1066. // TODO
  1067. }
  1068. // $buggedfile is set to uselocalandtzdaylight if conf->global->AGENDA_EXT_BUGGEDFILEx = 'uselocalandtzdaylight' (for example with bluemind)
  1069. if ($buggedfile === 'uselocalandtzdaylight') { // unixtime is a local date that does take daylight into account, TZID is +2 for example for 'Europe/Paris' in summer
  1070. $localtzs = new DateTimeZone(preg_replace('/"/', '', $icalevent['DTSTART']['TZID']));
  1071. $localtze = new DateTimeZone(preg_replace('/"/', '', $icalevent['DTEND']['TZID']));
  1072. $localdts = new DateTime(dol_print_date($datestart, 'dayrfc', 'gmt'), $localtzs);
  1073. $localdte = new DateTime(dol_print_date($dateend, 'dayrfc', 'gmt'), $localtze);
  1074. $tmps = -1 * $localtzs->getOffset($localdts);
  1075. $tmpe = -1 * $localtze->getOffset($localdte);
  1076. $datestart += $tmps;
  1077. $dateend += $tmpe;
  1078. //var_dump($datestart);
  1079. }
  1080. $addevent = true;
  1081. }
  1082. if ($addevent) {
  1083. $event->id = $icalevent['UID'];
  1084. $event->ref = $event->id;
  1085. $userId = $userstatic->findUserIdByEmail($namecal);
  1086. if (!empty($userId) && $userId > 0) {
  1087. $event->userassigned[$userId] = $userId;
  1088. $event->percentage = -1;
  1089. }
  1090. $event->type_code = "ICALEVENT";
  1091. $event->type_label = $namecal;
  1092. $event->type_color = $colorcal;
  1093. $event->type = 'icalevent';
  1094. $event->type_picto = 'rss';
  1095. $event->icalname = $namecal;
  1096. $event->icalcolor = $colorcal;
  1097. $usertime = 0; // We dont modify date because we want to have date into memory datep and datef stored as GMT date. Compensation will be done during output.
  1098. $event->datep = $datestart + $usertime;
  1099. $event->datef = $dateend + $usertime;
  1100. if ($icalevent['SUMMARY']) {
  1101. $event->label = $icalevent['SUMMARY'];
  1102. } elseif ($icalevent['DESCRIPTION']) {
  1103. $event->label = dol_nl2br($icalevent['DESCRIPTION'], 1);
  1104. } else {
  1105. $event->label = $langs->trans("ExtSiteNoLabel");
  1106. }
  1107. // Priority (see https://www.kanzaki.com/docs/ical/priority.html)
  1108. // LOW = 0 to 4
  1109. // MEDIUM = 5
  1110. // HIGH = 6 to 9
  1111. if (!empty($icalevent['PRIORITY'])) {
  1112. $event->priority = $icalevent['PRIORITY'];
  1113. }
  1114. // Transparency (see https://www.kanzaki.com/docs/ical/transp.html)
  1115. if ($icalevent['TRANSP']) {
  1116. if ($icalevent['TRANSP'] == "TRANSPARENT") {
  1117. $event->transparency = 0; // 0 = available / free
  1118. }
  1119. if ($icalevent['TRANSP'] == "OPAQUE") {
  1120. $event->transparency = 1; // 1 = busy
  1121. }
  1122. // TODO: MS outlook states
  1123. // X-MICROSOFT-CDO-BUSYSTATUS:FREE + TRANSP:TRANSPARENT => Available / Free
  1124. // X-MICROSOFT-CDO-BUSYSTATUS:FREE + TRANSP:OPAQUE => Work another place
  1125. // X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE + TRANSP:OPAQUE => With reservations
  1126. // X-MICROSOFT-CDO-BUSYSTATUS:BUSY + TRANSP:OPAQUE => Busy
  1127. // X-MICROSOFT-CDO-BUSYSTATUS:OOF + TRANSP:OPAQUE => Away from the office / off-site
  1128. }
  1129. if (!empty($icalevent['LOCATION'])) {
  1130. $event->location = $icalevent['LOCATION'];
  1131. }
  1132. $event->date_start_in_calendar = $event->datep;
  1133. if ($event->datef != '' && $event->datef >= $event->datep) {
  1134. $event->date_end_in_calendar = $event->datef;
  1135. } else {
  1136. $event->date_end_in_calendar = $event->datep;
  1137. }
  1138. // Define ponctual property
  1139. if ($event->date_start_in_calendar == $event->date_end_in_calendar) {
  1140. $event->ponctuel = 1;
  1141. //print 'x'.$datestart.'-'.$dateend;exit;
  1142. }
  1143. // Add event into $eventarray if date range are ok.
  1144. if ($event->date_end_in_calendar < $firstdaytoshow || $event->date_start_in_calendar >= $lastdaytoshow) {
  1145. //print 'x'.$datestart.'-'.$dateend;exit;
  1146. //print 'x'.$datestart.'-'.$dateend;exit;
  1147. //print 'x'.$datestart.'-'.$dateend;exit;
  1148. // This record is out of visible range
  1149. } else {
  1150. if ($event->date_start_in_calendar < $firstdaytoshow) {
  1151. $event->date_start_in_calendar = $firstdaytoshow;
  1152. }
  1153. if ($event->date_end_in_calendar >= $lastdaytoshow) {
  1154. $event->date_end_in_calendar = ($lastdaytoshow - 1);
  1155. }
  1156. // Add an entry in actionarray for each day
  1157. $daycursor = $event->date_start_in_calendar;
  1158. $annee = dol_print_date($daycursor, '%Y', 'tzuserrel');
  1159. $mois = dol_print_date($daycursor, '%m', 'tzuserrel');
  1160. $jour = dol_print_date($daycursor, '%d', 'tzuserrel');
  1161. // Loop on each day covered by action to prepare an index to show on calendar
  1162. $loop = true; $j = 0;
  1163. // daykey must be date that represent day box in calendar so must be a user time
  1164. $daykey = dol_mktime(0, 0, 0, $mois, $jour, $annee, 'gmt');
  1165. $daykeygmt = dol_mktime(0, 0, 0, $mois, $jour, $annee, 'gmt');
  1166. do {
  1167. //if ($event->fulldayevent) print dol_print_date($daykeygmt,'dayhour','gmt').'-'.dol_print_date($daykey,'dayhour','gmt').'-'.dol_print_date($event->date_end_in_calendar,'dayhour','gmt').' ';
  1168. $eventarray[$daykey][] = $event;
  1169. $daykey += 60 * 60 * 24; $daykeygmt += 60 * 60 * 24; // Add one day
  1170. if (($event->fulldayevent ? $daykeygmt : $daykey) > $event->date_end_in_calendar) {
  1171. $loop = false;
  1172. }
  1173. } while ($loop);
  1174. }
  1175. }
  1176. }
  1177. }
  1178. }
  1179. }
  1180. // Complete $eventarray with events coming from external module
  1181. $parameters = array(); $object = null;
  1182. $reshook = $hookmanager->executeHooks('getCalendarEvents', $parameters, $object, $action);
  1183. if (!empty($hookmanager->resArray['eventarray'])) {
  1184. foreach ($hookmanager->resArray['eventarray'] as $keyDate => $events) {
  1185. if (!isset($eventarray[$keyDate])) {
  1186. $eventarray[$keyDate] = array();
  1187. }
  1188. $eventarray[$keyDate] = array_merge($eventarray[$keyDate], $events);
  1189. }
  1190. }
  1191. // Sort events
  1192. foreach ($eventarray as $keyDate => &$dateeventarray) {
  1193. usort($dateeventarray, 'sort_events_by_date');
  1194. }
  1195. $maxnbofchar = 0;
  1196. $cachethirdparties = array();
  1197. $cachecontacts = array();
  1198. $cacheusers = array();
  1199. // Define theme_datacolor array
  1200. $color_file = DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/theme_vars.inc.php";
  1201. if (is_readable($color_file)) {
  1202. include $color_file;
  1203. }
  1204. if (!is_array($theme_datacolor)) {
  1205. $theme_datacolor = array(array(120, 130, 150), array(200, 160, 180), array(190, 190, 220));
  1206. }
  1207. $massactionbutton ='';
  1208. print_barre_liste($langs->trans("Agenda"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, -1, 'object_action', 0, $nav.'<span class="marginleftonly"></span>'.$newcardbutton, '', $limit, 1, 0, 1, $viewmode);
  1209. // Show div with list of calendars
  1210. print $s;
  1211. if (empty($mode) || $mode == 'show_month') { // View by month
  1212. $newparam = $param; // newparam is for birthday links
  1213. $newparam = preg_replace('/showbirthday=/i', 'showbirthday_=', $newparam); // To avoid replacement when replace day= is done
  1214. $newparam = preg_replace('/mode=show_month&?/i', '', $newparam);
  1215. $newparam = preg_replace('/mode=show_week&?/i', '', $newparam);
  1216. $newparam = preg_replace('/day=[0-9]+&?/i', '', $newparam);
  1217. $newparam = preg_replace('/month=[0-9]+&?/i', '', $newparam);
  1218. $newparam = preg_replace('/year=[0-9]+&?/i', '', $newparam);
  1219. $newparam = preg_replace('/viewcal=[0-9]+&?/i', '', $newparam);
  1220. $newparam = preg_replace('/showbirthday_=/i', 'showbirthday=', $newparam); // Restore correct parameter
  1221. $newparam .= '&viewcal=1';
  1222. print '<div class="liste_titre liste_titre_bydiv centpercent">';
  1223. print_actions_filter($form, $canedit, $status, $year, $month, $day, $showbirthday, 0, $filtert, 0, $pid, $socid, $action, -1, $actioncode, $usergroup, '', $resourceid);
  1224. print '</div>';
  1225. print '<div class="div-table-responsive-no-min sectioncalendarbymonth maxscreenheightless300">';
  1226. print '<table width="100%" class="noborder nocellnopadd cal_pannel cal_month">';
  1227. print ' <tr class="liste_titre">';
  1228. // Column title of weeks numbers
  1229. echo ' <td class="center">#</td>';
  1230. $i = 0;
  1231. while ($i < 7) {
  1232. print ' <td class="center bold uppercase tdfordaytitle'.($i == 0 ? ' borderleft' : '').'">';
  1233. $numdayinweek = (($i + (isset($conf->global->MAIN_START_WEEK) ? $conf->global->MAIN_START_WEEK : 1)) % 7);
  1234. if (!empty($conf->dol_optimize_smallscreen)) {
  1235. $labelshort = array(0=>'SundayMin', 1=>'MondayMin', 2=>'TuesdayMin', 3=>'WednesdayMin', 4=>'ThursdayMin', 5=>'FridayMin', 6=>'SaturdayMin');
  1236. print $langs->trans($labelshort[$numdayinweek]);
  1237. } else {
  1238. print $langs->trans("Day".$numdayinweek);
  1239. }
  1240. print ' </td>'."\n";
  1241. $i++;
  1242. }
  1243. echo ' </tr>'."\n";
  1244. $todayarray = dol_getdate($now, 'fast');
  1245. $todaytms = dol_mktime(0, 0, 0, $todayarray['mon'], $todayarray['mday'], $todayarray['year']);
  1246. // In loops, tmpday contains day nb in current month (can be zero or negative for days of previous month)
  1247. //var_dump($eventarray);
  1248. for ($iter_week = 0; $iter_week < 6; $iter_week++) {
  1249. echo " <tr>\n";
  1250. // Get date of the current day, format 'yyyy-mm-dd'
  1251. if ($tmpday <= 0) { // If number of the current day is in previous month
  1252. $currdate0 = sprintf("%04d", $prev_year).sprintf("%02d", $prev_month).sprintf("%02d", $max_day_in_prev_month + $tmpday);
  1253. } elseif ($tmpday <= $max_day_in_month) { // If number of the current day is in current month
  1254. $currdate0 = sprintf("%04d", $year).sprintf("%02d", $month).sprintf("%02d", $tmpday);
  1255. } else // If number of the current day is in next month
  1256. {
  1257. $currdate0 = sprintf("%04d", $next_year).sprintf("%02d", $next_month).sprintf("%02d", $tmpday - $max_day_in_month);
  1258. }
  1259. // Get week number for the targeted date '$currdate0'
  1260. $numweek0 = date("W", strtotime(date($currdate0)));
  1261. // Show the week number, and define column width
  1262. echo ' <td class="center weeknumber opacitymedium" width="2%">'.$numweek0.'</td>';
  1263. for ($iter_day = 0; $iter_day < 7; $iter_day++) {
  1264. if ($tmpday <= 0) {
  1265. /* Show days before the beginning of the current month (previous month) */
  1266. $style = 'cal_other_month cal_past';
  1267. if ($iter_day == 6) {
  1268. $style .= ' cal_other_month_right';
  1269. }
  1270. echo ' <td class="'.$style.' nowrap tdtop" width="14%">';
  1271. show_day_events($db, $max_day_in_prev_month + $tmpday, $prev_month, $prev_year, $month, $style, $eventarray, $maxprint, $maxnbofchar, $newparam);
  1272. echo " </td>\n";
  1273. } elseif ($tmpday <= $max_day_in_month) {
  1274. /* Show days of the current month */
  1275. $curtime = dol_mktime(0, 0, 0, $month, $tmpday, $year);
  1276. $style = 'cal_current_month';
  1277. if ($iter_day == 6) {
  1278. $style .= ' cal_current_month_right';
  1279. }
  1280. $today = 0;
  1281. if ($todayarray['mday'] == $tmpday && $todayarray['mon'] == $month && $todayarray['year'] == $year) {
  1282. $today = 1;
  1283. }
  1284. if ($today) {
  1285. $style = 'cal_today';
  1286. }
  1287. if ($curtime < $todaytms) {
  1288. $style .= ' cal_past';
  1289. }
  1290. //var_dump($todayarray['mday']."==".$tmpday." && ".$todayarray['mon']."==".$month." && ".$todayarray['year']."==".$year.' -> '.$style);
  1291. echo ' <td class="'.$style.' nowrap tdtop" width="14%">';
  1292. show_day_events($db, $tmpday, $month, $year, $month, $style, $eventarray, $maxprint, $maxnbofchar, $newparam);
  1293. echo "</td>\n";
  1294. } else {
  1295. /* Show days after the current month (next month) */
  1296. $style = 'cal_other_month';
  1297. if ($iter_day == 6) {
  1298. $style .= ' cal_other_month_right';
  1299. }
  1300. echo ' <td class="'.$style.' nowrap tdtop" width="14%">';
  1301. show_day_events($db, $tmpday - $max_day_in_month, $next_month, $next_year, $month, $style, $eventarray, $maxprint, $maxnbofchar, $newparam);
  1302. echo "</td>\n";
  1303. }
  1304. $tmpday++;
  1305. }
  1306. echo " </tr>\n";
  1307. }
  1308. print "</table>\n";
  1309. print '</div>';
  1310. print '<input type="hidden" name="actionmove" value="mupdate">';
  1311. print '<input type="hidden" name="backtopage" value="'.dol_escape_htmltag($_SERVER['PHP_SELF']).'?'.dol_escape_htmltag($_SERVER['QUERY_STRING']).'">';
  1312. print '<input type="hidden" name="newdate" id="newdate">';
  1313. } elseif ($mode == 'show_week') {
  1314. // View by week
  1315. $newparam = $param; // newparam is for birthday links
  1316. $newparam = preg_replace('/showbirthday=/i', 'showbirthday_=', $newparam); // To avoid replacement when replace day= is done
  1317. $newparam = preg_replace('/mode=show_month&?/i', '', $newparam);
  1318. $newparam = preg_replace('/mode=show_week&?/i', '', $newparam);
  1319. $newparam = preg_replace('/day=[0-9]+&?/i', '', $newparam);
  1320. $newparam = preg_replace('/month=[0-9]+&?/i', '', $newparam);
  1321. $newparam = preg_replace('/year=[0-9]+&?/i', '', $newparam);
  1322. $newparam = preg_replace('/viewweek=[0-9]+&?/i', '', $newparam);
  1323. $newparam = preg_replace('/showbirthday_=/i', 'showbirthday=', $newparam); // Restore correct parameter
  1324. $newparam .= '&viewweek=1';
  1325. print '<div class="liste_titre liste_titre_bydiv centpercent"><div class="divsearchfield">';
  1326. print_actions_filter($form, $canedit, $status, $year, $month, $day, $showbirthday, 0, $filtert, 0, $pid, $socid, $action, -1, $actioncode, $usergroup, '', $resourceid);
  1327. print '</div></div>';
  1328. print '<div class="div-table-responsive-no-min sectioncalendarbyweek maxscreenheightless300">';
  1329. print '<table width="100%" class="noborder nocellnopadd cal_pannel cal_month">';
  1330. print ' <tr class="liste_titre">';
  1331. $i = 0;
  1332. while ($i < 7) {
  1333. echo ' <td class="center bold uppercase tdfordaytitle">'.$langs->trans("Day".(($i + (isset($conf->global->MAIN_START_WEEK) ? $conf->global->MAIN_START_WEEK : 1)) % 7))."</td>\n";
  1334. $i++;
  1335. }
  1336. echo " </tr>\n";
  1337. echo " <tr>\n";
  1338. for ($iter_day = 0; $iter_day < 7; $iter_day++) {
  1339. // Show days of the current week
  1340. $curtime = dol_time_plus_duree($firstdaytoshow, $iter_day, 'd'); // $firstdaytoshow is in timezone of server
  1341. $tmpday = dol_print_date($curtime, '%d', 'tzuserrel');
  1342. $tmpmonth = dol_print_date($curtime, '%m', 'tzuserrel');
  1343. $tmpyear = dol_print_date($curtime, '%Y', 'tzuserrel');
  1344. $style = 'cal_current_month';
  1345. if ($iter_day == 6) {
  1346. $style .= ' cal_other_month_right';
  1347. }
  1348. $today = 0;
  1349. $todayarray = dol_getdate($now, 'fast');
  1350. if ($todayarray['mday'] == $tmpday && $todayarray['mon'] == $tmpmonth && $todayarray['year'] == $tmpyear) {
  1351. $today = 1;
  1352. }
  1353. if ($today) {
  1354. $style = 'cal_today';
  1355. }
  1356. echo ' <td class="'.$style.'" width="14%" valign="top">';
  1357. show_day_events($db, $tmpday, $tmpmonth, $tmpyear, $month, $style, $eventarray, 0, $maxnbofchar, $newparam, 1, 300);
  1358. echo " </td>\n";
  1359. }
  1360. echo " </tr>\n";
  1361. print "</table>\n";
  1362. print '</div>';
  1363. echo '<input type="hidden" name="actionmove" value="mupdate">';
  1364. echo '<input type="hidden" name="backtopage" value="'.dol_escape_htmltag($_SERVER['PHP_SELF']).'?'.dol_escape_htmltag($_SERVER['QUERY_STRING']).'">';
  1365. echo '<input type="hidden" name="newdate" id="newdate">';
  1366. } else // View by day
  1367. {
  1368. $newparam = $param; // newparam is for birthday links
  1369. $newparam = preg_replace('/mode=show_month&?/i', '', $newparam);
  1370. $newparam = preg_replace('/mode=show_week&?/i', '', $newparam);
  1371. $newparam = preg_replace('/viewday=[0-9]+&?/i', '', $newparam);
  1372. $newparam .= '&viewday=1';
  1373. // Code to show just one day
  1374. $style = 'cal_current_month cal_current_month_oneday';
  1375. $today = 0;
  1376. $todayarray = dol_getdate($now, 'fast');
  1377. if ($todayarray['mday'] == $day && $todayarray['mon'] == $month && $todayarray['year'] == $year) {
  1378. $today = 1;
  1379. }
  1380. //if ($today) $style='cal_today';
  1381. $timestamp = dol_mktime(12, 0, 0, $month, $day, $year);
  1382. $arraytimestamp = dol_getdate($timestamp);
  1383. print '<div class="liste_titre liste_titre_bydiv centpercent"><div class="divsearchfield">';
  1384. print_actions_filter($form, $canedit, $status, $year, $month, $day, $showbirthday, 0, $filtert, 0, $pid, $socid, $action, -1, $actioncode, $usergroup, '', $resourceid);
  1385. print '</div></div>';
  1386. print '<div class="div-table-responsive-no-min sectioncalendarbyday maxscreenheightless300">';
  1387. echo '<table class="tagtable centpercent noborder nocellnopadd cal_pannel cal_month noborderbottom" style="margin-bottom: 5px !important;">';
  1388. echo ' <tr class="tagtr liste_titre">';
  1389. echo ' <td class="tagtd center bold uppercase">'.$langs->trans("Day".$arraytimestamp['wday'])."</td>\n";
  1390. echo " </td>\n";
  1391. /*
  1392. echo ' <div class="tagtr">';
  1393. echo ' <div class="tagtd width100"></div>';
  1394. echo ' <div class="tagtd center">';
  1395. echo show_day_events($db, $day, $month, $year, $month, $style, $eventarray, 0, $maxnbofchar, $newparam, 1, 300, -1);
  1396. echo ' </div>'."\n";
  1397. echo " </div>\n";
  1398. */
  1399. echo '</table>';
  1400. print '</div>';
  1401. /* WIP View per hour */
  1402. $useviewhour = 0;
  1403. if ($useviewhour) {
  1404. print '<div class="div-table-responsive-no-min borderbottom">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  1405. $maxheightwin = (isset($_SESSION["dol_screenheight"]) && $_SESSION["dol_screenheight"] > 500) ? ($_SESSION["dol_screenheight"] - 200) : 660; // Also into index.php file
  1406. echo '<div style="max-height: '.$maxheightwin.'px;">';
  1407. echo '<div class="tagtable centpercent calendarviewcontainer">';
  1408. $maxnbofchar = 80;
  1409. $tmp = explode('-', $conf->global->MAIN_DEFAULT_WORKING_HOURS);
  1410. $minhour = round($tmp[0], 0);
  1411. $maxhour = round($tmp[1], 0);
  1412. if ($minhour > 23) {
  1413. $minhour = 23;
  1414. }
  1415. if ($maxhour < 1) {
  1416. $maxhour = 1;
  1417. }
  1418. if ($maxhour <= $minhour) {
  1419. $maxhour = $minhour + 1;
  1420. }
  1421. $i = 0;
  1422. $j = 0;
  1423. while ($i < 24) {
  1424. echo ' <div class="tagtr calendarviewcontainertr">'."\n";
  1425. echo ' <div class="tagtd width100 tdtop">'.dol_print_date($i * 3600, 'hour', 'gmt').'</div>';
  1426. echo ' <div class="tagtd '.$style.' tdtop"></div>'."\n";
  1427. echo ' </div>'."\n";
  1428. $i++;
  1429. $j++;
  1430. }
  1431. echo '</div></div>';
  1432. show_day_events($db, $day, $month, $year, $month, $style, $eventarray, 0, $maxnbofchar, $newparam, 1, 300, 1);
  1433. print '</div>';
  1434. } else {
  1435. print '<div class="div-table-responsive-no-min">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  1436. show_day_events($db, $day, $month, $year, $month, $style, $eventarray, 0, $maxnbofchar, $newparam, 1, 300, 0);
  1437. print '</div>';
  1438. }
  1439. }
  1440. print "\n".'</form>';
  1441. // End of page
  1442. llxFooter();
  1443. $db->close();
  1444. /**
  1445. * Show event of a particular day
  1446. *
  1447. * @param DoliDB $db Database handler
  1448. * @param int $day Day
  1449. * @param int $month Month
  1450. * @param int $year Year
  1451. * @param int $monthshown Current month shown in calendar view
  1452. * @param string $style Style to use for this day
  1453. * @param array $eventarray Array of events
  1454. * @param int $maxprint Nb of actions to show each day on month view (0 means no limit)
  1455. * @param int $maxnbofchar Nb of characters to show for event line
  1456. * @param string $newparam Parameters on current URL
  1457. * @param int $showinfo Add extended information (used by day and week view)
  1458. * @param int $minheight Minimum height for each event. 60px by default.
  1459. * @param string $nonew 0=Add "new entry button", 1=No "new entry button", -1=Only "new entry button"
  1460. * @return void
  1461. */
  1462. function show_day_events($db, $day, $month, $year, $monthshown, $style, &$eventarray, $maxprint = 0, $maxnbofchar = 16, $newparam = '', $showinfo = 0, $minheight = 60, $nonew = 0)
  1463. {
  1464. global $user, $conf, $langs;
  1465. global $action, $mode, $filter, $filtert, $status, $actioncode, $usergroup; // Filters used into search form
  1466. global $theme_datacolor;
  1467. global $cachethirdparties, $cachecontacts, $cacheusers, $colorindexused;
  1468. if ($conf->use_javascript_ajax) { // Enable the "Show more button..."
  1469. $conf->global->MAIN_JS_SWITCH_AGENDA = 1;
  1470. }
  1471. $dateint = sprintf("%04d", $year).sprintf("%02d", $month).sprintf("%02d", $day);
  1472. //print 'show_day_events day='.$day.' month='.$month.' year='.$year.' dateint='.$dateint;
  1473. print "\n";
  1474. $curtime = dol_mktime(0, 0, 0, $month, $day, $year);
  1475. $urltoshow = DOL_URL_ROOT.'/comm/action/index.php?mode=show_day&day='.str_pad($day, 2, "0", STR_PAD_LEFT).'&month='.str_pad($month, 2, "0", STR_PAD_LEFT).'&year='.$year.$newparam;
  1476. $urltocreate = '';
  1477. if ($user->rights->agenda->myactions->create || $user->rights->agenda->allactions->create) {
  1478. $newparam .= '&month='.str_pad($month, 2, "0", STR_PAD_LEFT).'&year='.$year;
  1479. $hourminsec = '100000';
  1480. $urltocreate = DOL_URL_ROOT.'/comm/action/card.php?action=create&datep='.sprintf("%04d%02d%02d", $year, $month, $day).$hourminsec.'&backtopage='.urlencode($_SERVER["PHP_SELF"].($newparam ? '?'.$newparam : ''));
  1481. }
  1482. // Line with title of day
  1483. print '<div id="dayevent_'.$dateint.'" class="dayevent tagtable centpercent nobordernopadding">'."\n";
  1484. if ($nonew <= 0) {
  1485. print '<div class="tagtr cursorpointer" onclick="window.location=\''.$urltocreate.'\';"><div class="nowrap tagtd"><div class="left inline-block">';
  1486. print '<a class="dayevent-aday" style="color: #666" href="'.$urltoshow.'">';
  1487. if ($showinfo) {
  1488. print dol_print_date($curtime, 'daytextshort');
  1489. } else {
  1490. print dol_print_date($curtime, '%d');
  1491. }
  1492. print '</a>';
  1493. print '</div><div class="nowrap floatright inline-block marginrightonly">';
  1494. if ($user->rights->agenda->myactions->create || $user->rights->agenda->allactions->create) {
  1495. print '<a class="cursoradd" href="'.$urltocreate.'">'; // Explicit link, usefull for nojs interfaces
  1496. print img_picto($langs->trans("NewAction"), 'edit_add.png');
  1497. print '</a>';
  1498. }
  1499. print '</div></div></div>'."\n";
  1500. }
  1501. if ($nonew < 0) {
  1502. print '</div>';
  1503. return;
  1504. }
  1505. // Line with td contains all div of each events
  1506. print '<div class="tagtr">';
  1507. print '<div class="tagtd centpercent agendacell sortable">';
  1508. //$curtime = dol_mktime (0, 0, 0, $month, $day, $year);
  1509. $i = 0; $ireallyshown = 0; $itoshow = 0; $numother = 0; $numbirthday = 0; $numical = 0; $numicals = array();
  1510. $ymd = sprintf("%04d", $year).sprintf("%02d", $month).sprintf("%02d", $day);
  1511. $colorindexused[$user->id] = 0; // Color index for current user (user->id) is always 0
  1512. $nextindextouse = is_array($colorindexused) ?count($colorindexused) : 0; // At first run this is 0, so fist user has 0, next 1, ...
  1513. //var_dump($colorindexused);
  1514. include_once DOL_DOCUMENT_ROOT.'/holiday/class/holiday.class.php';
  1515. $tmpholiday = new Holiday($db);
  1516. foreach ($eventarray as $daykey => $notused) { // daykey is the 'YYYYMMDD' to show according to user
  1517. $annee = dol_print_date($daykey, '%Y', 'gmt'); // We use gmt because we want the value represented by string 'YYYYMMDD'
  1518. $mois = dol_print_date($daykey, '%m', 'gmt'); // We use gmt because we want the value represented by string 'YYYYMMDD'
  1519. $jour = dol_print_date($daykey, '%d', 'gmt'); // We use gmt because we want the value represented by string 'YYYYMMDD'
  1520. //print 'event daykey='.$daykey.' dol_print_date(daykey)='.dol_print_date($daykey, 'dayhour', 'gmt').' jour='.$jour.' mois='.$mois.' annee='.$annee."<br>\n";
  1521. if ($day == $jour && $month == $mois && $year == $annee) {
  1522. foreach ($eventarray[$daykey] as $index => $event) {
  1523. if ($i < $maxprint || $maxprint == 0 || !empty($conf->global->MAIN_JS_SWITCH_AGENDA)) {
  1524. $keysofuserassigned = array_keys($event->userassigned);
  1525. $ponct = ($event->date_start_in_calendar == $event->date_end_in_calendar);
  1526. // Define $color (Hex string like '0088FF') and $cssclass of event
  1527. $color = -1; $cssclass = ''; $colorindex = -1;
  1528. if (in_array($user->id, $keysofuserassigned)) {
  1529. $cssclass = 'family_mytasks';
  1530. if (empty($cacheusers[$event->userownerid])) {
  1531. $newuser = new User($db);
  1532. $newuser->fetch($event->userownerid);
  1533. $cacheusers[$event->userownerid] = $newuser;
  1534. }
  1535. //var_dump($cacheusers[$event->userownerid]->color);
  1536. // We decide to choose color of owner of event (event->userownerid is user id of owner, event->userassigned contains all users assigned to event)
  1537. if (!empty($cacheusers[$event->userownerid]->color)) {
  1538. $color = $cacheusers[$event->userownerid]->color;
  1539. }
  1540. } elseif ($event->type_code == 'ICALEVENT') { // Event come from external ical file
  1541. $numical++;
  1542. if (!empty($event->icalname)) {
  1543. if (!isset($numicals[dol_string_nospecial($event->icalname)])) {
  1544. $numicals[dol_string_nospecial($event->icalname)] = 0;
  1545. }
  1546. $numicals[dol_string_nospecial($event->icalname)]++;
  1547. }
  1548. $color = ($event->icalcolor ? $event->icalcolor : -1);
  1549. $cssclass = (!empty($event->icalname) ? 'family_ext'.md5($event->icalname) : 'family_other');
  1550. } elseif ($event->type_code == 'BIRTHDAY') {
  1551. $numbirthday++; $colorindex = 2; $cssclass = 'family_birthday '; $color = sprintf("%02x%02x%02x", $theme_datacolor[$colorindex][0], $theme_datacolor[$colorindex][1], $theme_datacolor[$colorindex][2]);
  1552. } else {
  1553. $numother++;
  1554. $color = ($event->icalcolor ? $event->icalcolor : -1);
  1555. $cssclass = (!empty($event->icalname) ? 'family_ext'.md5($event->icalname) : 'family_other');
  1556. if (empty($cacheusers[$event->userownerid])) {
  1557. $newuser = new User($db);
  1558. $newuser->fetch($event->userownerid);
  1559. $cacheusers[$event->userownerid] = $newuser;
  1560. }
  1561. //var_dump($cacheusers[$event->userownerid]->color);
  1562. // We decide to choose color of owner of event (event->userownerid is user id of owner, event->userassigned contains all users assigned to event)
  1563. if (!empty($cacheusers[$event->userownerid]->color)) {
  1564. $color = $cacheusers[$event->userownerid]->color;
  1565. }
  1566. }
  1567. if ($color < 0) { // Color was not set on user card. Set color according to color index.
  1568. // Define color index if not yet defined
  1569. $idusertouse = ($event->userownerid ? $event->userownerid : 0);
  1570. if (isset($colorindexused[$idusertouse])) {
  1571. $colorindex = $colorindexused[$idusertouse]; // Color already assigned to this user
  1572. } else {
  1573. $colorindex = $nextindextouse;
  1574. $colorindexused[$idusertouse] = $colorindex;
  1575. if (!empty($theme_datacolor[$nextindextouse + 1])) {
  1576. $nextindextouse++; // Prepare to use next color
  1577. }
  1578. }
  1579. //print '|'.($color).'='.($idusertouse?$idusertouse:0).'='.$colorindex.'<br>';
  1580. // Define color
  1581. $color = sprintf("%02x%02x%02x", $theme_datacolor[$colorindex][0], $theme_datacolor[$colorindex][1], $theme_datacolor[$colorindex][2]);
  1582. }
  1583. $cssclass = $cssclass.' eventday_'.$ymd;
  1584. // Defined style to disable drag and drop feature
  1585. if ($event->type_code == 'AC_OTH_AUTO') {
  1586. $cssclass .= " unmovable";
  1587. } elseif ($event->type_code == 'HOLIDAY') {
  1588. $cssclass .= " unmovable";
  1589. } elseif ($event->type_code == 'BIRTHDAY') {
  1590. $cssclass .= " unmovable";
  1591. } elseif ($event->type_code == 'ICALEVENT') {
  1592. $cssclass .= " unmovable";
  1593. } elseif ($event->date_end_in_calendar && date('Ymd', $event->date_start_in_calendar) != date('Ymd', $event->date_end_in_calendar)) {
  1594. $tmpyearend = date('Y', $event->date_end_in_calendar);
  1595. $tmpmonthend = date('m', $event->date_end_in_calendar);
  1596. $tmpdayend = date('d', $event->date_end_in_calendar);
  1597. if ($tmpyearend != $annee || $tmpmonthend != $mois || $tmpdayend != $jour) {
  1598. $cssclass .= " unmovable";
  1599. }
  1600. } else {
  1601. if ($user->rights->agenda->allactions->create ||
  1602. (($event->authorid == $user->id || $event->userownerid == $user->id) && $user->rights->agenda->myactions->create)) {
  1603. $cssclass .= " movable cursormove";
  1604. } else {
  1605. $cssclass .= " unmovable";
  1606. }
  1607. }
  1608. $h = ''; $nowrapontd = 1;
  1609. if ($mode == 'show_day') {
  1610. $h = 'height: 100%; '; $nowrapontd = 0;
  1611. }
  1612. if ($mode == 'show_week') {
  1613. $h = 'height: 100%; '; $nowrapontd = 0;
  1614. }
  1615. // Show event box
  1616. print "\n";
  1617. print '<!-- start event '.$i.' -->'."\n";
  1618. $morecss = '';
  1619. if ($maxprint && $ireallyshown >= $maxprint) {
  1620. $morecss = 'showifmore';
  1621. }
  1622. if ($event->type == 'birthdate' && !GETPOST('check_birthday')) {
  1623. $morecss = 'hidden';
  1624. }
  1625. if ($event->type == 'holiday' && !GETPOST('check_holiday')) {
  1626. $morecss = 'hidden';
  1627. }
  1628. if ($morecss != 'hidden') {
  1629. $itoshow++;
  1630. }
  1631. if ($morecss != 'showifmore' && $morecss != 'hidden') {
  1632. $ireallyshown++;
  1633. }
  1634. //var_dump($event->type.' - '.$morecss.' - '.$cssclass.' - '.$i.' - '.$ireallyshown.' - '.$itoshow);
  1635. print '<div id="event_'.$ymd.'_'.$i.'" class="event family_'.$event->type.' '.$cssclass.($morecss ? ' '.$morecss : '').'"';
  1636. //print ' style="height: 100px;';
  1637. //print ' position: absolute; top: 40px; width: 50%;';
  1638. //print '"';
  1639. print '>';
  1640. //var_dump($event->userassigned);
  1641. //var_dump($event->transparency);
  1642. print '<table class="centpercent cal_event';
  1643. print (empty($event->transparency) ? ' cal_event_notbusy' : ' cal_event_busy');
  1644. //if (empty($event->transparency) && empty($conf->global->AGENDA_NO_TRANSPARENT_ON_NOT_BUSY)) print ' opacitymedium'; // Not busy
  1645. print '" style="'.$h;
  1646. $colortouse = $color;
  1647. // If colortouse is similar than background, we force to change it.
  1648. if (empty($event->transparency) && empty($conf->global->AGENDA_NO_TRANSPARENT_ON_NOT_BUSY)) {
  1649. print 'background: #f0f0f0;';
  1650. print 'border-left: 5px solid #'.$colortouse.';';
  1651. } else {
  1652. print 'background: #f0f0f0;';
  1653. print 'border-left: 5px solid #'.dol_color_minus($colortouse, -3).';';
  1654. //print 'background: -webkit-gradient(linear, left top, left bottom, from(#'.dol_color_minus($colortouse, -3).'), to(#'.dol_color_minus($colortouse, -1).'));';
  1655. }
  1656. //print 'background: #'.$colortouse.';';
  1657. //print 'background: -webkit-gradient(linear, left top, left bottom, from(#'.dol_color_minus($color, -3).'), to(#'.dol_color_minus($color, -1).'));';
  1658. //if (! empty($event->transparency)) print 'background: #'.$color.'; background: -webkit-gradient(linear, left top, left bottom, from(#'.$color.'), to(#'.dol_color_minus($color,1).'));';
  1659. //else print 'background-color: transparent !important; background: none; border: 1px solid #bbb;';
  1660. //print ' -moz-border-radius:4px;"';
  1661. //print 'border: 1px solid #ccc" width="100%"';
  1662. print '">';
  1663. print '<tr>';
  1664. print '<td class="tdoverflow nobottom centpercent '.($nowrapontd ? 'nowrap ' : '').'cal_event'.($event->type_code == 'BIRTHDAY' ? ' cal_event_birthday' : '').'">';
  1665. $daterange = '';
  1666. if ($event->type_code == 'BIRTHDAY') { // It's birthday calendar
  1667. print $event->getNomUrl(1, $maxnbofchar, 'cal_event', 'birthday', 'contact');
  1668. } elseif ($event->type_code == 'HOLIDAY') { // It's holiday calendar
  1669. $tmpholiday->fetch($event->id);
  1670. print $tmpholiday->getNomUrl(1);
  1671. $tmpid = $tmpholiday->fk_user;
  1672. if (empty($cacheusers[$tmpid])) {
  1673. $newuser = new User($db);
  1674. $newuser->fetch($tmpid);
  1675. $cacheusers[$tmpid] = $newuser;
  1676. }
  1677. $listofusertoshow = '';
  1678. $listofusertoshow .= '<br>'.$cacheusers[$tmpid]->getNomUrl(-1, '', 0, 0, 0, 0, '', 'paddingright valigntextbottom');
  1679. print $listofusertoshow;
  1680. } else { // Other calendar
  1681. // Picto
  1682. if (empty($event->fulldayevent)) {
  1683. //print $event->getNomUrl(2).' ';
  1684. }
  1685. // Date
  1686. if (empty($event->fulldayevent)) {
  1687. // Show hours (start ... end)
  1688. $tmpyearstart = dol_print_date($event->date_start_in_calendar, '%Y', 'tzuserrel');
  1689. $tmpmonthstart = dol_print_date($event->date_start_in_calendar, '%m', 'tzuserrel');
  1690. $tmpdaystart = dol_print_date($event->date_start_in_calendar, '%d', 'tzuserrel');
  1691. $tmpyearend = dol_print_date($event->date_end_in_calendar, '%Y', 'tzuserrel');
  1692. $tmpmonthend = dol_print_date($event->date_end_in_calendar, '%m', 'tzuserrel');
  1693. $tmpdayend = dol_print_date($event->date_end_in_calendar, '%d', 'tzuserrel');
  1694. // Hour start
  1695. if ($tmpyearstart == $annee && $tmpmonthstart == $mois && $tmpdaystart == $jour) {
  1696. $daterange .= dol_print_date($event->date_start_in_calendar, 'hour', 'tzuserrel');
  1697. if ($event->date_end_in_calendar && $event->date_start_in_calendar != $event->date_end_in_calendar) {
  1698. if ($tmpyearstart == $tmpyearend && $tmpmonthstart == $tmpmonthend && $tmpdaystart == $tmpdayend) {
  1699. $daterange .= '-';
  1700. }
  1701. //else
  1702. //print '...';
  1703. }
  1704. }
  1705. if ($event->date_end_in_calendar && $event->date_start_in_calendar != $event->date_end_in_calendar) {
  1706. if ($tmpyearstart != $tmpyearend || $tmpmonthstart != $tmpmonthend || $tmpdaystart != $tmpdayend) {
  1707. $daterange .= '...';
  1708. }
  1709. }
  1710. // Hour end
  1711. if ($event->date_end_in_calendar && $event->date_start_in_calendar != $event->date_end_in_calendar) {
  1712. if ($tmpyearend == $annee && $tmpmonthend == $mois && $tmpdayend == $jour) {
  1713. $daterange .= dol_print_date($event->date_end_in_calendar, 'hour', 'tzuserrel');
  1714. }
  1715. }
  1716. } else {
  1717. if ($showinfo) {
  1718. print $langs->trans("EventOnFullDay")."<br>\n";
  1719. }
  1720. }
  1721. // Show title
  1722. $titletoshow = $daterange;
  1723. $titletoshow .= ($titletoshow ? ' ' : '').($event->label ? $event->label : $event->libelle);
  1724. if ($event->type_code != 'ICALEVENT') {
  1725. $savlabel = $event->label ? $event->label : $event->libelle;
  1726. $event->label = $titletoshow;
  1727. $event->libelle = $titletoshow;
  1728. // Note: List of users are inside $event->userassigned. Link may be clickable depending on permissions of user.
  1729. $titletoshow = (($event->type_picto || $event->type_code) ? $event->getTypePicto() : '');
  1730. $titletoshow .= $event->getNomUrl(0, $maxnbofchar, 'cal_event cal_event_title', '', 0, 0);
  1731. $event->label = $savlabel;
  1732. $event->libelle = $savlabel;
  1733. }
  1734. // Loop on each assigned user
  1735. $listofusertoshow = '';
  1736. $posuserassigned = 0;
  1737. foreach ($event->userassigned as $tmpid => $tmpdata) {
  1738. if (!$posuserassigned && $titletoshow) {
  1739. $listofusertoshow .= '<br>';
  1740. }
  1741. $posuserassigned++;
  1742. if (empty($cacheusers[$tmpid])) {
  1743. $newuser = new User($db);
  1744. $newuser->fetch($tmpid);
  1745. $cacheusers[$tmpid] = $newuser;
  1746. }
  1747. $listofusertoshow .= $cacheusers[$tmpid]->getNomUrl(-3, '', 0, 0, 0, 0, '', 'paddingright valigntextbottom');
  1748. }
  1749. print $titletoshow;
  1750. print $listofusertoshow;
  1751. if ($event->type_code == 'ICALEVENT') {
  1752. print '<br>('.dol_trunc($event->icalname, $maxnbofchar).')';
  1753. }
  1754. $thirdparty_id = ($event->thirdparty_id > 0 ? $event->thirdparty_id : ((is_object($event->societe) && $event->societe->id > 0) ? $event->societe->id : 0));
  1755. $contact_id = ($event->contact_id > 0 ? $event->contact_id : ((is_object($event->contact) && $event->contact->id > 0) ? $event->contact->id : 0));
  1756. // If action related to company / contact
  1757. $linerelatedto = '';
  1758. if ($thirdparty_id > 0) {
  1759. if (!isset($cachethirdparties[$thirdparty_id]) || !is_object($cachethirdparties[$thirdparty_id])) {
  1760. $thirdparty = new Societe($db);
  1761. $thirdparty->fetch($thirdparty_id);
  1762. $cachethirdparties[$thirdparty_id] = $thirdparty;
  1763. } else {
  1764. $thirdparty = $cachethirdparties[$thirdparty_id];
  1765. }
  1766. if (!empty($thirdparty->id)) {
  1767. $linerelatedto .= $thirdparty->getNomUrl(1, '', 0);
  1768. }
  1769. }
  1770. if (!empty($contact_id) && $contact_id > 0) {
  1771. if (!is_object($cachecontacts[$contact_id])) {
  1772. $contact = new Contact($db);
  1773. $contact->fetch($contact_id);
  1774. $cachecontacts[$contact_id] = $contact;
  1775. } else {
  1776. $contact = $cachecontacts[$contact_id];
  1777. }
  1778. if ($linerelatedto) {
  1779. $linerelatedto .= '&nbsp;';
  1780. }
  1781. if (!empty($contact->id)) {
  1782. $linerelatedto .= $contact->getNomUrl(1, '', 0);
  1783. }
  1784. }
  1785. if (!empty($event->fk_element) && $event->fk_element > 0 && !empty($event->elementtype) && !empty($conf->global->AGENDA_SHOW_LINKED_OBJECT)) {
  1786. include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  1787. if ($linerelatedto) {
  1788. $linerelatedto .= '<br>';
  1789. }
  1790. $linerelatedto .= dolGetElementUrl($event->fk_element, $event->elementtype, 1);
  1791. }
  1792. if ($linerelatedto) {
  1793. print '<br>'.$linerelatedto;
  1794. }
  1795. }
  1796. // Show location
  1797. if ($showinfo) {
  1798. if ($event->location) {
  1799. print '<br>';
  1800. print $langs->trans("Location").': '.$event->location;
  1801. }
  1802. }
  1803. print '</td>';
  1804. // Status - Percent
  1805. $withstatus = 0;
  1806. if ($event->type_code != 'BIRTHDAY' && $event->type_code != 'ICALEVENT') {
  1807. $withstatus = 1;
  1808. if ($event->percentage >= 0) {
  1809. $withstatus = 2;
  1810. }
  1811. }
  1812. print '<td class="nobottom right nowrap cal_event_right'.($withstatus >= 2 ? ' cal_event_right_status' : '').'">';
  1813. if ($withstatus) {
  1814. print $event->getLibStatut(3, 1);
  1815. } else {
  1816. print '&nbsp;';
  1817. }
  1818. print '</td></tr></table>';
  1819. print '</div><!-- end event '.$i.' -->'."\n";
  1820. $i++;
  1821. } else {
  1822. print '<a href="'.DOL_URL_ROOT.'/comm/action/index.php?mode='.$mode.'&maxprint=0&month='.((int) $monthshown).'&year='.((int) $year);
  1823. print ($status ? '&status='.$status : '').($filter ? '&filter='.urlencode($filter) : '');
  1824. print ($filtert ? '&search_filtert='.urlencode($filtert) : '');
  1825. print ($usergroup ? '&search_usergroup='.urlencode($usergroup) : '');
  1826. print ($actioncode != '' ? '&search_actioncode='.urlencode($actioncode) : '');
  1827. print '">'.img_picto("all", "1downarrow_selected.png").' ...';
  1828. print ' +'.(count($eventarray[$daykey]) - $maxprint);
  1829. print '</a>';
  1830. break;
  1831. //$ok=false; // To avoid to show twice the link
  1832. }
  1833. }
  1834. break;
  1835. }
  1836. }
  1837. if (!$i) { // No events
  1838. print '&nbsp;';
  1839. }
  1840. if (!empty($conf->global->MAIN_JS_SWITCH_AGENDA) && $itoshow > $ireallyshown && $maxprint) {
  1841. print '<div class="center cursorpointer" id="more_'.$ymd.'">'.img_picto("All", "angle-double-down", 'class="warning"').' +'.($itoshow - $ireallyshown).'</div>';
  1842. //print ' +'.(count($eventarray[$daykey])-$maxprint);
  1843. print '<script type="text/javascript">'."\n";
  1844. print 'jQuery(document).ready(function () {'."\n";
  1845. print ' var open=0;'."\n";
  1846. print ' jQuery("#more_'.$ymd.'").click(function() { console.log("Click on showmore for '.$ymd.'"); reinit_day_'.$ymd.'(); event.stopImmediatePropagation(); });'."\n";
  1847. print ' function reinit_day_'.$ymd.'() {'."\n";
  1848. print ' jQuery(".eventday_'.$ymd.'.showifmore").toggle();'."\n";
  1849. print ' open = open + 1; if (open > 1) { open = 0; }'."\n";
  1850. print ' if (open) { ';
  1851. print ' jQuery("#more_'.$ymd.'").html(\''.img_picto("All", "angle-double-up", 'class="warning"').'\');'."\n";
  1852. print ' } else { ';
  1853. print ' jQuery("#more_'.$ymd.'").html(\''.img_picto("All", "angle-double-down", 'class="warning"').' +'.($itoshow - $ireallyshown).'\');'."\n";
  1854. print ' }'."\n";
  1855. print ' }'."\n";
  1856. print '});'."\n";
  1857. print '</script>'."\n";
  1858. }
  1859. print '</div></div>'; // td tr
  1860. print '</div>'; // table
  1861. print "\n";
  1862. }
  1863. /**
  1864. * Change color with a delta
  1865. *
  1866. * @param string $color Color
  1867. * @param int $minus Delta (1 = 16 unit). Positive value = darker color, Negative value = brighter color.
  1868. * @param int $minusunit Minus unit
  1869. * @return string New color
  1870. */
  1871. function dol_color_minus($color, $minus, $minusunit = 16)
  1872. {
  1873. $newcolor = $color;
  1874. if ($minusunit == 16) {
  1875. $newcolor[0] = dechex(max(min(hexdec($newcolor[0]) - $minus, 15), 0));
  1876. $newcolor[2] = dechex(max(min(hexdec($newcolor[2]) - $minus, 15), 0));
  1877. $newcolor[4] = dechex(max(min(hexdec($newcolor[4]) - $minus, 15), 0));
  1878. } else {
  1879. // Not yet implemented
  1880. }
  1881. return $newcolor;
  1882. }
  1883. /**
  1884. * Sort events by date
  1885. *
  1886. * @param object $a Event A
  1887. * @param object $b Event B
  1888. * @return int < 0 if event A should be before event B, > 0 otherwise, 0 if they have the exact same time slot
  1889. */
  1890. function sort_events_by_date($a, $b)
  1891. {
  1892. // Sort holidays at first
  1893. if ($a->type_code === 'HOLIDAY') {
  1894. return -1;
  1895. }
  1896. if ($b->type_code === 'HOLIDAY') {
  1897. return 1;
  1898. }
  1899. // datep => Event start time
  1900. // datef => Event end time
  1901. // Events have different start time
  1902. if ($a->datep !== $b->datep) {
  1903. return $a->datep - $b->datep;
  1904. }
  1905. // Events have same start time and no end time
  1906. if ((!is_numeric($b->datef)) || (!is_numeric($a->datef))) {
  1907. return sort_events_by_percentage($a, $b);
  1908. }
  1909. // Events have the same start time and same end time
  1910. if ($b->datef === $a->datef) {
  1911. return sort_events_by_percentage($a, $b);
  1912. }
  1913. // Events have the same start time, but have different end time -> longest event first
  1914. return $b->datef - $a->datef;
  1915. }
  1916. /**
  1917. * Sort events by percentage
  1918. *
  1919. * @param object $a Event A
  1920. * @param object $b Event B
  1921. * @return int < 0 if event A should be before event B, > 0 otherwise, 0 if they have the exact same percentage
  1922. */
  1923. function sort_events_by_percentage($a, $b)
  1924. {
  1925. // Sort events with no percentage before each other
  1926. // (usefull to sort holidays, sick days or similar on the top)
  1927. if ($a->percentage < 0) {
  1928. return -1;
  1929. }
  1930. if ($b->percentage < 0) {
  1931. return 1;
  1932. }
  1933. return $b->percentage - $a->percentage;
  1934. }