PageRenderTime 83ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 1ms

/modules/scheduler/class.scheduler_model.inc

https://github.com/klaaspeter/achievo
PHP | 1154 lines | 817 code | 143 blank | 194 comment | 129 complexity | be3d4a6d4c823d7e49587167fd398af1 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * This file is part of the Achievo distribution.
  4. * Detailed copyright and licensing information can be found
  5. * in the doc/COPYRIGHT and doc/LICENSE files which should be
  6. * included in the distribution.
  7. *
  8. * @package achievo
  9. * @subpackage scheduler
  10. *
  11. * @copyright (c)2008 Sandy Pleyte
  12. * @copyright (c)2008 Ibuildings B.V.
  13. * @license http://www.achievo.org/licensing Achievo Open Source License
  14. *
  15. * @version $Revision$
  16. * $Id$
  17. */
  18. atkimport("modules.scheduler.utils.schedulertools");
  19. /**
  20. * Scheduler model used for the views
  21. * @author Sandy Pleyte <sandy@achievo.org>
  22. * @package achievo
  23. * @subpackage scheduler
  24. * @since 1.3.0
  25. */
  26. class scheduler_model
  27. {
  28. /**
  29. * @var int $m_day Day
  30. */
  31. var $m_day = 0;
  32. /**
  33. * @var int $m_day Day
  34. */
  35. var $m_month = 0;
  36. /**
  37. * @var int $m_year Year
  38. */
  39. var $m_year = 0;
  40. /**
  41. * @var string $m_viewdate Viewdate in yyyy-mm-dd
  42. */
  43. var $m_viewdate = "";
  44. /**
  45. * @var string $m_viewdate Viewdate in yyyy-mm-dd
  46. */
  47. var $m_small_viewdate = "";
  48. /**
  49. * @var int $m_year Year
  50. */
  51. var $m_calendarStartDay = 1;
  52. /**
  53. * @var string $m_view Current view
  54. */
  55. var $m_view = "";
  56. /**
  57. * @var string $m_user Current user
  58. */
  59. var $m_user = "";
  60. /**
  61. * @var string $m_viewUsers Current userview
  62. */
  63. var $m_viewUsers = "";
  64. /**
  65. * @var string $m_viewTitle View Title
  66. */
  67. var $m_viewTitle = "";
  68. /**
  69. * @var array $m_allDayItems All allday calendar items
  70. */
  71. var $m_allDayItems = array();
  72. /**
  73. * @var array $m_appointmentItems All appointment calendar items
  74. */
  75. var $m_appointmentItems = array();
  76. /**
  77. * @var string $m_legend Legend of a calendar
  78. */
  79. var $m_attendees = array();
  80. var $m_users = array();
  81. var $m_showTodo = 0;
  82. var $m_employeeLookup = array();
  83. function scheduler_model()
  84. {
  85. }
  86. function getInstance()
  87. {
  88. static $s_in = null;
  89. if (is_null($s_in))
  90. {
  91. $s_in = new scheduler_model();
  92. }
  93. return $s_in;
  94. }
  95. /**
  96. * Set view user
  97. * @param string $user Current user
  98. */
  99. function setViewUser($user)
  100. {
  101. // empty string => all users
  102. if (empty($user))
  103. {
  104. $user = array();
  105. }
  106. // other string => only that user
  107. if (!is_array($user))
  108. {
  109. $user = array((string) $user);
  110. }
  111. $this->m_viewUsers = $user;
  112. }
  113. function setView($view)
  114. {
  115. $this->m_view = $view;
  116. }
  117. /**
  118. * Set viewdate
  119. * @param int $day Day
  120. * @param int $month Month
  121. * @param int $year Year
  122. */
  123. function setViewDate($date)
  124. {
  125. if($date=="")
  126. {
  127. $date = date("Y-m-d");
  128. }
  129. $day = substr($date, 8,2);
  130. $month = substr($date, 5,2);
  131. $year = substr($date, 0,4);
  132. $this->m_day = ($day == 0) ? date("d") : $day;
  133. $this->m_month = ($month == 0) ? date("m") : $month;
  134. $this->m_year = ($year == 0) ? date("Y") : $year;
  135. $this->m_viewdate = sprintf('%04d-%02d-%02d',$this->m_year,$this->m_month,$this->m_day);
  136. $this->m_weeknumber = $this->getWeekNumber($this->m_day,$this->m_month,$this->m_year);
  137. }
  138. function getViewDate()
  139. {
  140. return $this->m_viewdate;
  141. }
  142. function getDay()
  143. {
  144. return $this->m_day;
  145. }
  146. function getMonth()
  147. {
  148. return $this->m_month;
  149. }
  150. function getYear()
  151. {
  152. return $this->m_year;
  153. }
  154. /**
  155. * Set the viewdate for the small calendar
  156. *
  157. * @param int $day Day
  158. * @param int $month Month
  159. * @param int $year Year
  160. */
  161. function setSmallViewDate($date)
  162. {
  163. if($date=="")
  164. {
  165. $this->m_small_viewdate = $this->m_viewdate;
  166. }
  167. else
  168. {
  169. $day = substr($date, 8,2);
  170. $month = substr($date, 5,2);
  171. $year = substr($date, 0,4);
  172. $this->m_small_viewdate = sprintf('%04d-%02d-%02d',$year,$month,$day);
  173. }
  174. }
  175. /**
  176. * Set the calendar start day
  177. * @param int $day Startday 0=Sunday, 1=Monday
  178. */
  179. function setCalendarStartDay($day)
  180. {
  181. $this->m_calendarStartDay = $day;
  182. }
  183. /**
  184. * Get weekNumber
  185. *
  186. * @param int $day Day
  187. * @param int $month Month
  188. * @param int $year Year
  189. * @return int Weeknumber
  190. */
  191. function getWeekNumber($day,$month,$year)
  192. {
  193. atkImport("modules.utils.dateutil");
  194. return dateutil::getWeeknumber($day,$month,$year);
  195. }
  196. /**
  197. * Get the legend of the calendar
  198. * @return string html table with the calendar legend
  199. */
  200. function getLegend()
  201. {
  202. static $rows = null;
  203. if(is_null($rows))
  204. {
  205. $g_db = &atkgetdb();
  206. // get the defined colors for each scheduler-type
  207. $query = "SELECT * FROM scheduler_category ORDER BY id";
  208. $rows = $g_db->getRows($query);
  209. }
  210. return $rows;
  211. }
  212. function getLegendColors()
  213. {
  214. static $itemColors = null;
  215. if(is_null($itemColors))
  216. {
  217. $nrows = $this->getLegend();
  218. for ($i=0,$_i=count($nrows);$i<$_i;$i++)
  219. {
  220. $itemColors[$nrows[$i]["id"]]["desc"] = $nrows[$i]["description"];
  221. $itemColors[$nrows[$i]["id"]]["fore"] = $nrows[$i]["fgcolor"];
  222. $itemColors[$nrows[$i]["id"]]["back"] = $nrows[$i]["bgcolor"];
  223. }
  224. }
  225. return $itemColors;
  226. }
  227. function renderLegend()
  228. {
  229. $nrows = $this->getLegend();
  230. if(count($nrows)==0) return "";
  231. $legend = '<div style="border: 1px solid black; background: white none repeat scroll 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; font-size: 1em;">
  232. <dl><b>'.atktext("legend","scheduler").':</b></dl>
  233. <div style="margin-left: 2em;">';
  234. for ($i=0,$_i=count($nrows);$i<$_i;$i++)
  235. {
  236. $legend.='<p style="margin: 0px;">
  237. <span style="border: medium none ; background-color: '.$nrows[$i]["bgcolor"].'; color: '.$nrows[$i]["bgcolor"].';">&nbsp;&nbsp;&nbsp;</span>
  238. '.htmlspecialchars($nrows[$i]["description"]).'
  239. </p>';
  240. }
  241. $legend.='</div><br></div>';
  242. return $legend;
  243. }
  244. /**
  245. * Get Items for the small scheduler
  246. *
  247. * @param string $startdate Startdate
  248. * @param string $enddate Enddate
  249. * @return array Array with dates that have an Event
  250. */
  251. function getItemsCompact($startdate,$enddate,$user=array())
  252. {
  253. if(count($user)==0)
  254. {
  255. $user =$this->getSchedulerUsers();
  256. }
  257. schedulertools::updateRecurringEvents($startdate,$enddate);
  258. $name = "atk".atkconfig("database")."query";
  259. $query = atknew($name);
  260. $query->addTable('scheduler_scheduler');
  261. $query->addJoin('scheduler_attendees', '', 'scheduler_attendees.scheduler_id=scheduler_scheduler.id', TRUE);
  262. $query->addJoin('person', '', 'person.id=scheduler_attendees.person_id', TRUE);
  263. $query->addJoin('person', 'owner', 'owner.id=scheduler_scheduler.owner', TRUE);
  264. $query->addJoin('scheduler_dates','','scheduler_dates.scheduler_id=scheduler_scheduler.id',TRUE);
  265. $query->addField('startdate',' ','scheduler_dates');
  266. $query->addField('enddate',' ','scheduler_dates');
  267. $tmp_enddate = mktime(0,0,0,substr($startdate,5,2),substr($startdate,8,2),substr($startdate,0,4));
  268. $tmp_startdate = mktime(23,59,59,substr($enddate,5,2),substr($enddate,8,2),substr($enddate,0,4));
  269. $condition = "$tmp_enddate < scheduler_dates.enddate AND scheduler_dates.startdate < $tmp_startdate";
  270. if(count($user)>0 && !in_array('all', $user))
  271. {
  272. $useridcondition = (count($user)>0) ? " OR scheduler_scheduler.owner IN ('".implode("','", $user)."') OR scheduler_attendees.person_id IN ('".implode("','", $user)."')" : "";
  273. $condition.=" AND (scheduler_scheduler.all_users = '1' $useridcondition)";
  274. }
  275. $query->addCondition($condition);
  276. $querystring = $query->buildSelect(TRUE);
  277. $db = &atkGetDb();
  278. $nrows = $db->getrows($querystring);
  279. $dates = array();
  280. for($i=0,$_i=count($nrows);$i<$_i;$i++)
  281. {
  282. $loop_enddate = date("Ymd",$nrows[$i]["enddate"]);
  283. for($j=0;date("Ymd",($nrows[$i]["startdate"]+($j*86400)))<=$loop_enddate;$j++)
  284. {
  285. $dates[date("Y-m-d",($nrows[$i]["startdate"]+($j*86400)))]=true;
  286. }
  287. }
  288. //todo: Should we add holidays to the small scheduler ?
  289. //$this->addHolidays(true,$dates,$startdate,$enddate);
  290. $userprefs = $this->getUserSchedulerPrefs();
  291. if($userprefs["showemployeebirthdays"])
  292. $this->addEmployeeBirthdays(true,$dates,date("n",$tmp_startdate),date("n",$tmp_enddate));
  293. if($userprefs["showtodo"])
  294. $this->addTodos(true,$dates,$startdate,$enddate);
  295. return $dates;
  296. }
  297. function getSchedulerUsers()
  298. {
  299. $users = $this->m_viewUsers;
  300. $realUsers=array();
  301. // Check for groups
  302. for($i=0,$_i=count($users);$i<$_i;$i++)
  303. {
  304. if(substr($users[$i],0,2)=="g_")
  305. {
  306. $group_id = intval(str_replace('g_','',$users[$i]));
  307. $groupUsers = $this->groupToUsers($group_id);
  308. array_merge($realUsers,$groupUsers);
  309. }
  310. else
  311. {
  312. $realUsers[] = $users[$i];
  313. }
  314. }
  315. return $realUsers;
  316. }
  317. /**
  318. * Get scheduler items
  319. *
  320. * @param string $startdate Startdate
  321. * @param string $enddate Enddate
  322. * @param array $user Array with user id's
  323. */
  324. function getItems($startdate="",$enddate="",$user=array())
  325. {
  326. $db = &atkGetDb();
  327. if(count($user)==0)
  328. {
  329. $user = $this->getSchedulerUsers();
  330. }
  331. $this->m_alldayItems = array();
  332. $this->m_appointmentItems = array();
  333. if($startdate=="")
  334. {
  335. // Set startdate and enddate with viewdate since it's not
  336. // possible to call this function with only an enddate.
  337. $startdate=$this->m_viewdate;
  338. $enddate=$this->m_viewdate;
  339. }
  340. schedulertools::updateRecurringEvents($startdate,$enddate);
  341. $tmp_array=array();
  342. $this->addHolidays(false,$tmp_array,$startdate,$enddate);
  343. $query = &$db->createQuery();
  344. $query->addTable('scheduler_scheduler');
  345. $query->addJoin('scheduler_attendees', '', 'scheduler_attendees.scheduler_id=scheduler_scheduler.id', TRUE);
  346. $query->addJoin('person', '', 'person.id=scheduler_attendees.person_id', TRUE);
  347. $query->addJoin('person', 'owner', 'owner.id=scheduler_scheduler.owner', TRUE);
  348. $query->addJoin('scheduler_dates','','scheduler_dates.scheduler_id=scheduler_scheduler.id',TRUE);
  349. $query->addField('id', ' ', 'scheduler_scheduler');
  350. $query->addField('startdate as itemstartdate', '', 'scheduler_scheduler');
  351. $query->addField('enddate as itemenddate', '', 'scheduler_scheduler');
  352. $query->addField('starttime', ' ', 'scheduler_scheduler');
  353. $query->addField('endtime', ' ', 'scheduler_scheduler');
  354. $query->addField('title', ' ', 'scheduler_scheduler');
  355. $query->addField('description', ' ', 'scheduler_scheduler');
  356. $query->addField('location', ' ', 'scheduler_scheduler');
  357. $query->addField('allday', ' ', 'scheduler_scheduler');
  358. $query->addField('private', ' ', 'scheduler_scheduler');
  359. $query->addField('owner', ' ', 'scheduler_scheduler');
  360. $query->addField('all_users', ' ', 'scheduler_scheduler');
  361. $query->addField('category', ' ', 'scheduler_scheduler');
  362. $query->addField('recur', ' ', 'scheduler_scheduler');
  363. $query->addField('startdate',' ','scheduler_dates');
  364. $query->addField('enddate',' ','scheduler_dates');
  365. $tmp_enddate = mktime(0,0,0,substr($startdate,5,2),substr($startdate,8,2),substr($startdate,0,4));
  366. $tmp_startdate = mktime(23,59,59,substr($enddate,5,2),substr($enddate,8,2),substr($enddate,0,4));
  367. $condition = "$tmp_enddate < scheduler_dates.enddate AND scheduler_dates.startdate < $tmp_startdate";
  368. if(count($user)>0 && !in_array('all', $user))
  369. {
  370. $useridcondition = (count($user)>0) ? " OR scheduler_scheduler.owner IN ('".implode("','", $user)."') OR scheduler_attendees.person_id IN ('".implode("','", $user)."')" : "";
  371. $condition.=" AND (scheduler_scheduler.all_users = '1' $useridcondition)";
  372. }
  373. $query->addCondition($condition);
  374. $query->addOrderBy('starttime');
  375. $querystring = $query->buildSelect(TRUE);
  376. $nrows = $db->getrows($querystring);
  377. $items = array();
  378. for($i=0,$_i=count($nrows);$i<$_i;$i++)
  379. {
  380. $items[] = $nrows[$i]["id"];
  381. $check_startdate = date("Ymd",$nrows[$i]["startdate"]);
  382. $check_enddate = date("Ymd",$nrows[$i]["enddate"]);
  383. for($j=0;date("Ymd",($nrows[$i]["startdate"]+($j*DAY)))<=$check_enddate;$j++)
  384. {
  385. $currenttime = $nrows[$i]["startdate"]+($j*DAY);
  386. /** @todo Optimize for week and month */
  387. if($this->m_view=="day" && date("Y-m-d",$currenttime)!=$this->m_viewdate) continue;
  388. $currentdate = date("Ymd",$currenttime);
  389. $tmp_nrow = $nrows[$i];
  390. if($nrows[$i]["allday"]==1 || ($check_startdate!=$currentdate && $check_enddate!=$currentdate))
  391. {
  392. if($tmp_nrow["allday"]==0) $tmp_nrow["allday"]=1;
  393. $this->m_allDayItems[date("Y-m-d",$currenttime)][] = $tmp_nrow;
  394. }
  395. else
  396. {
  397. if($check_startdate==$currentdate && $check_startdate!=$check_enddate)
  398. {
  399. $tmp_nrow["enddate"] = mktime(23,59,00,date("m",$currenttime),date("d",$currenttime),date("Y",$currenttime));
  400. $tmp_nrow["endtime"] = "23:59:00";
  401. }
  402. elseif($check_enddate==$currentdate && $check_startdate!=$check_enddate)
  403. {
  404. $tmp_nrow["startdate"] = mktime(0,0,0,date("m",$currenttime),date("d",$currenttime),date("Y",$currenttime));
  405. $tmp_nrow["starttime"] = "00:00:00";
  406. }
  407. $this->m_appointmentItems[date("Y-m-d",$currenttime)][] = $tmp_nrow;
  408. }
  409. }
  410. }
  411. $this->getAttendees($items);
  412. $userprefs = $this->getUserSchedulerPrefs();
  413. if($userprefs["showemployeebirthdays"])
  414. {
  415. $tmp_array = array();
  416. $this->addEmployeeBirthdays(false,$tmp_array);
  417. }
  418. if($userprefs["showtodo"])
  419. {
  420. $tmp_array=array();
  421. $this->addTodos(false,$tmp_array,$startdate,$enddate);
  422. }
  423. }
  424. function getAllDayItems($date="")
  425. {
  426. if($date=="")
  427. {
  428. return $this->m_allDayItems;
  429. }
  430. elseif(array_key_exists($date,$this->m_allDayItems))
  431. {
  432. return $this->m_allDayItems[$date];
  433. }
  434. return array();
  435. }
  436. function getAppointmentItems($date="")
  437. {
  438. if($date=="")
  439. {
  440. return $this->m_appointmentItems;
  441. }
  442. elseif(array_key_exists($date,$this->m_appointmentItems))
  443. {
  444. return $this->m_appointmentItems[$date];
  445. }
  446. return array();
  447. }
  448. /**
  449. * Add holidays to scheduler items
  450. * @param boolean $compact Compact mode or not (for the small calendar)
  451. * @param array $dates Array with the small calendar dates
  452. * @param integer $startmonth Start month of the small calendar
  453. * @param integer $endmonth End month of the small calendar
  454. */
  455. function addHolidays($compact=false,&$dates,$startdate="",$enddate="")
  456. {
  457. $holiday = &getNode("scheduler.scheduler_holidays");
  458. if($startdate=="") { $startdate=time(); } else { $startdate = str_replace("-","",$startdate); }
  459. if($enddate=="") { $enddate=date("Ymd"); } else { $enddate = str_replace("-","",$enddate); }
  460. $startdate = mktime(12,0,0,intval(substr($startdate,4,2)),intval(substr($startdate,6,2)),intval(substr($startdate,0,4)));
  461. for($i=0;date("Ymd",($startdate+($i*86400)))<=$enddate;$i++)
  462. {
  463. $date = date("Y-m-d",($startdate+($i*86400)));
  464. if($holiday->isHoliday($date))
  465. {
  466. if($compact)
  467. {
  468. $dates[$date]=true;
  469. }
  470. else
  471. {
  472. $holidaysInfo = $holiday->getHolidayInfo($date);
  473. foreach($holidaysInfo as $info)
  474. {
  475. $this->m_allDayItems[$date][] = array("id"=>NULL,
  476. "itemstartdate"=>$date,
  477. "itemenddate"=>$date,
  478. "starttime"=>"00:00:00",
  479. "endtime"=>"23:59:59",
  480. "allday"=>1,
  481. "all_users"=>1,
  482. "recur"=>"once",
  483. "holiday"=>true,
  484. "startdate"=>mktime(0,0,0,$this->m_month,$this->m_day,$this->m_year),
  485. "enddate"=>mktime(23,59,59,$this->m_month,$this->m_day,$this->m_year),
  486. "title"=>$info["name"],
  487. "category"=>$info["schedulercategory"]);
  488. }
  489. }
  490. }
  491. }
  492. }
  493. /**
  494. * Add employee birthdates to scheduler items
  495. *
  496. * @param boolean $compact Compact mode or not (for the small calendar)
  497. * @param array $dates Array with the small calendar dates
  498. * @param integer $startmonth Start month of the small calendar
  499. * @param integer $endmonth End month of the small calendar
  500. */
  501. function addEmployeeBirthdays($compact=false,&$dates,$startmonth=1,$endmonth=12)
  502. {
  503. $employees = &atkGetNode("employee.employee");
  504. $birthdays = $employees->getBirthdates();
  505. if(!$compact)
  506. {
  507. $startmonth=intval($this->m_month);
  508. $endmonth=intval($this->m_month);
  509. }
  510. for($i=$startmonth;$i<=$endmonth;$i++)
  511. {
  512. if(isset($birthdays[$i]) && is_array($birthdays[$i]))
  513. {
  514. foreach($birthdays[$i] as $day => $names)
  515. {
  516. foreach($names as $name)
  517. {
  518. $date = date("Y-m-d",mktime(0,0,0,$i,$day,$this->m_year));
  519. if($compact)
  520. {
  521. $dates[$date]=true;
  522. }
  523. else
  524. {
  525. $this->m_allDayItems[$date][] = array("id"=>NULL,
  526. "itemstartdate"=>$date,
  527. "itemenddate"=>$date,
  528. "starttime"=>"00:00:00",
  529. "endtime"=>"23:59:59",
  530. "allday"=>1,
  531. "all_users"=>false,
  532. "recur"=>"once",
  533. "birthday"=>true,
  534. "startdate"=>mktime(0,0,0,$this->m_month,$this->m_day,$this->m_year),
  535. "enddate"=>mktime(23,59,59,$this->m_month,$this->m_day,$this->m_year),
  536. "title"=>$name["name"]." ".atktext("birthday"));
  537. }
  538. }
  539. }
  540. }
  541. }
  542. }
  543. /**
  544. * Add todo's to the events list
  545. *
  546. * @param bool $compact Small calendor or not
  547. * @param array $dates Array with dates for the compact mode
  548. * @param string $startdate Start date
  549. * @param string $enddate End date
  550. */
  551. function addTodos($compact=false,&$dates,$startdate,$enddate)
  552. {
  553. $todo_node = &atkGetNode("todo.todo");
  554. $filter="duedate >= '".$startdate."' AND duedate<='".$enddate."' ";
  555. $filter.='AND todo.status NOT IN (5,2)';
  556. $todos = $todo_node->selectDb($filter,"","","",array("id","title","duedate"));
  557. foreach($todos as $todo)
  558. {
  559. $date = date("Y-m-d",mktime(0,0,0,$todo["duedate"]["month"],$todo["duedate"]["day"],$todo["duedate"]["year"]));
  560. if($compact)
  561. {
  562. $dates[$date]=true;
  563. }
  564. else
  565. {
  566. $this->m_allDayItems[$date][] = array("id"=>NULL,
  567. "itemstartdate"=>$date,
  568. "itemenddate"=>$date,
  569. "starttime"=>"00:00:00",
  570. "endtime"=>"23:59:59",
  571. "allday"=>1,
  572. "all_user"=>false,
  573. "recur"=>"once",
  574. "todo"=>true,
  575. "startdate"=>mktime(0,0,0,$this->m_month,$this->m_day,$this->m_year),
  576. "enddate"=>mktime(23,59,59,$this->m_month,$this->m_day,$this->m_year),
  577. "title"=>atktext("todo_duedate")." ".$todo["title"]);
  578. }
  579. }
  580. }
  581. /**
  582. * Get attendees of a schedule item
  583. * @param int $scheduleid Schedule id
  584. * @return array Array with attendees of the schedule item
  585. */
  586. function getAttendees($items)
  587. {
  588. if(count($items)==0) return false;
  589. $db = &atkgetdb();
  590. $name = "atk".atkconfig("database")."query";
  591. $query = new $name();
  592. $query->addTable('scheduler_attendees');
  593. $query->addJoin('person', '', 'person.id=scheduler_attendees.person_id', TRUE);
  594. $query->addField('id', ' ', 'person');
  595. $query->addField('scheduler_id', ' ', 'scheduler_attendees');
  596. $query->addField('lastname', ' ', 'person');
  597. $query->addField('firstname', ' ', 'person');
  598. $query->addField('role', ' ', 'person');
  599. $query->addField('initials', ' ', 'person');
  600. $query->addCondition("scheduler_attendees.scheduler_id IN (".implode(",",$items).")");
  601. $querystring = $query->buildSelect(TRUE);
  602. $nrows = $db->getRows($querystring);
  603. for($i=0,$_i=count($nrows);$i<$_i;$i++)
  604. {
  605. $this->m_attendees[$nrows[$i]["scheduler_id"]][] = $nrows[$i]["id"];
  606. if(!array_key_exists($nrows[$i]["id"],$this->m_users))
  607. {
  608. $this->m_users[$nrows[$i]["id"]] = array("firstname"=>$nrows[$i]["firstname"],
  609. "lastname"=>$nrows[$i]["lastname"],
  610. "role"=>$nrows[$i]["role"],
  611. "initials"=>$nrows[$i]["initials"]
  612. );
  613. }
  614. }
  615. unset($nrows);
  616. return true;
  617. }
  618. /**
  619. * Corrects times to the timeschedule
  620. * @param string $time Time to be corrected
  621. * @params int $scheduletime Scheduler scale
  622. * @param string $round Round the time up or down
  623. * @return string Corrected time
  624. */
  625. function correct_time($time,$scheduletime=30,$round="down")
  626. {
  627. $tmp_min = substr($time,3,2);
  628. if($round=="down")
  629. {
  630. $minutes = sprintf("%02d",floor($tmp_min/$scheduletime)*$scheduletime);
  631. }
  632. else
  633. {
  634. $minutes = sprintf("%02d",ceil($tmp_min/$scheduletime)*$scheduletime);
  635. }
  636. if($minutes==60)
  637. {
  638. return sprintf("%02d",substr($time,0,2)+1).":00";
  639. }
  640. else
  641. {
  642. return substr($time,0,2).":".$minutes;
  643. }
  644. }
  645. /**
  646. * Creates a html form with a date/user selector
  647. * @return string html form with a date/user selector
  648. */
  649. function getSelector()
  650. {
  651. $form = '<form method="get" action="'.atkSelf().'">'."\n";
  652. $form .= session_form();
  653. $form .= '<input type="hidden" name="view" value="'.$this->m_view.'">';
  654. $dummy_rec = array("viewdate"=>$this->_getInitialDate());
  655. $datebox = new atkDateAttribute("viewdate","F d Y","d F Y",0,0,AF_OBLIGATORY);
  656. $form .=$datebox->edit($dummy_rec);
  657. $form .= '&nbsp;<input type="submit" value="'.atkText("refresh","scheduler").'">'."\n";
  658. $form .= '</form>'."\n";
  659. return $form;
  660. }
  661. /**
  662. * Return the initalDate for a view
  663. * @return array Initial Date
  664. */
  665. function _getInitialDate()
  666. {
  667. if ($this->m_viewdate!="")
  668. {
  669. $initial_date = array("year"=>$this->m_year,
  670. "month"=>$this->m_month,
  671. "day"=>$this->m_day);
  672. }
  673. else
  674. {
  675. $initial_date = array("year"=>date("Y"),
  676. "month"=>date("m"),
  677. "day"=>date("d"));
  678. }
  679. return $initial_date;
  680. }
  681. /**
  682. * Gets employees. Used in getEmployeesInSelect and getEmployeesInCheckboxes
  683. *
  684. * @return array employees
  685. */
  686. function _getEmployees()
  687. {
  688. $g_db = &atkgetdb();
  689. $sql = "SELECT lastname,firstname,id
  690. FROM person
  691. WHERE status='active' AND role='employee'
  692. ORDER BY lastname
  693. ";
  694. $records = $g_db->getrows($sql);
  695. return $records;
  696. }
  697. /**
  698. * Gets the employee selector as a form with checkboxes
  699. *
  700. * @return string html form
  701. */
  702. function _getEmployeesCheckboxesForm()
  703. {
  704. $buff = '<form method="get" action="'.atkSelf().'">'."\n";
  705. $buff.= session_form();
  706. $buff.= '<input type="hidden" name="view" value="'.$this->m_view.'">';
  707. $buff.= '<div class="employees_checklistheader">'.atkText("employeeview","scheduler").'</div>';
  708. $buff.= $this->_getEmployeesCheckboxes();
  709. $buff.= '<input type="submit" value="'.atkText("refresh","scheduler").'">'."\n";
  710. $buff.= '<br>';
  711. $buff.= '</form>';
  712. return $buff;
  713. }
  714. /**
  715. * Get Employee selector input elements as checkboxes
  716. * @param array $default wich items should be checked?
  717. * @param bool $show_groups show the user groups?
  718. * @param bool $show_all show the 'all employees' option?
  719. * @return string html of the input elements
  720. */
  721. function _getEmployeesCheckboxes($default=array(), $show_groups=true, $show_all=true)
  722. {
  723. // get the records
  724. $records = $this->_getEmployees();
  725. if(count($default)==0)
  726. {
  727. $default = $this->m_viewUsers;
  728. }
  729. $employee_code = '<ul class="employees_checklist">';
  730. if($show_groups || $show_all)
  731. {
  732. array_unshift($records, array(
  733. 'id' => 'spacer',
  734. 'firstname' => '',
  735. 'lastname' => '',
  736. ));
  737. }
  738. // Add groups
  739. if($show_groups)
  740. {
  741. $userprefs = $this->getUserSchedulerPrefs();
  742. $groups = $userprefs["groups"];
  743. $total_groups = count($groups);
  744. if($total_groups>0)
  745. {
  746. for($i=0;$i<$total_groups;$i++)
  747. {
  748. array_unshift($records, array(
  749. 'id' => 'g_'.$groups[$i]["id"],
  750. 'firstname' => '',
  751. 'lastname' => '<strong>'.htmlspecialchars($groups[$i]["name"]).'</strong>',
  752. ));
  753. }
  754. }
  755. }
  756. // add the 'all' option to top of records
  757. if($show_all)
  758. {
  759. array_unshift($records, array(
  760. 'id' => 'all',
  761. 'firstname' => '',
  762. 'lastname' => '<strong>'.atkText("allusers","scheduler").'</strong>',
  763. ));
  764. }
  765. // build up the html
  766. for($i=0;$i<count($records);$i++)
  767. {
  768. if($records[$i]["id"]=="spacer")
  769. {
  770. $employee_code.="<hr>";
  771. continue;
  772. }
  773. $sel='';
  774. if(in_array($records[$i]["id"],$default))
  775. {
  776. $sel="checked";
  777. }
  778. // name format
  779. $id = $records[$i]["id"];
  780. $name = $records[$i]["lastname"];
  781. if (!empty($records[$i]["firstname"])) {
  782. $name .= ', '.$records[$i]["firstname"];
  783. }
  784. // checkboxes
  785. $input = "<input id=\"user_{$id}\" type=\"checkbox\" value=\"{$id}\" name=\"user[]\" {$sel} />";
  786. $label = "<label for=\"user_{$id}\">{$input}{$name}</label>";
  787. $employee_code .= "<li>{$label}</li>";
  788. }
  789. $employee_code .= '</ul>';
  790. return $employee_code;
  791. }
  792. function groupToUsers($group_id)
  793. {
  794. $userprefs = $this->getUserSchedulerPrefs();
  795. $groups = $userprefs["groups"];
  796. $total_groups = count($groups);
  797. if($total_groups>0)
  798. {
  799. $members = array();
  800. for($i=0;$i<$total_groups;$i++)
  801. {
  802. if($groups[$i]["id"]==$group_id)
  803. {
  804. foreach($groups[$i]["members"] as $member)
  805. {
  806. $member[] = $member["userid"];
  807. }
  808. }
  809. }
  810. return $members;
  811. }
  812. else
  813. {
  814. return array();
  815. }
  816. }
  817. /**
  818. * Creates a small calendar with month navigation
  819. *
  820. * @param int $day Day
  821. * @param int $month Month
  822. * @param int $year Year
  823. * @return string a small html calendar
  824. */
  825. function _getSmallCalendar($day="",$month="",$year="",$monthNav=true,$showdaysnextmonth=false)
  826. {
  827. if($day=="" && $month=="" && $year=="")
  828. {
  829. $day = $this->m_day;
  830. $month = $this->m_month;
  831. $year = $this->m_year;
  832. }
  833. $userprefs = $this->getUserSchedulerPrefs();
  834. $weekstartday=0;
  835. $weekendday=0;
  836. if($this->m_view=="week")
  837. {
  838. atkimport("modules.timereg.timeregutils");
  839. $weekdays = TimeregUtils::getWeekdates($this->m_viewdate,false);
  840. $weekstartday = $weekdays[1]['date'];
  841. $weekendday = $weekdays[7]['date'];
  842. }
  843. atkimport("modules.utils.dateutil");
  844. $days = dateutil::short_daylist();
  845. for($i=0;$i<=6;$i++)
  846. $days[$i] = atk_substr(atkText($days[$i],"atk"), 0, 2);
  847. $months = dateutil::monthlist();
  848. for($i=1; $i<=12; $i++)
  849. $months[$i] = atkText($months[$i], "atk");
  850. $days_in_month = mktime (0,0,0,$month+1,0,$year);
  851. $days_in_month = (strftime ("%d",$days_in_month));
  852. $first_day_of_month = date("w", mktime(0,0,0,$month,1,$year));
  853. $prevTime = date("Y-m-d", mktime(0,0,0,$month-1, 1, $year));
  854. $prevUrl = atkSelf()."?view=".$this->m_view."&viewdate=".$this->m_viewdate."&small_viewdate=$prevTime";
  855. $nextTime = date("Y-m-d", mktime(0,0,0,$month+1, 1, $year));
  856. $nextUrl = atkSelf()."?view=".$this->m_view."&viewdate=".$this->m_viewdate."&small_viewdate=$nextTime";
  857. $currentTime = date("Y-m-d", mktime(0,0,0,$month, 1, $year));
  858. $currentUrl = atkSelf()."?view=month&viewdate=".$currentTime."&small_viewdate=$currentTime";
  859. $tmp ='<table border="0" cellpadding="1" cellspacing="0">';
  860. $tmp.='<tr style="background-color: #dddddd">';
  861. $tmp.='<td align="left">';
  862. if($monthNav) $tmp.= href($prevUrl,"&lt&lt");
  863. $tmp.='</td>';
  864. $tmp.='<td align="center" colspan="'.(dateutil::isISO8601()?6:5).'">';
  865. $tmp.= href($currentUrl,$months[intval($month)].' '.$year);
  866. $tmp.='</td>';
  867. $tmp.='<td align="right">';
  868. if($monthNav) $tmp.= href($nextUrl,"&gt;&gt;");
  869. $tmp.='</td>';
  870. $tmp.='</tr><tr>';
  871. if(dateutil::isISO8601()) $tmp.='<td>&nbsp;</td>';
  872. $day_number = $this->m_calendarStartDay;
  873. atkdebug("<h1>$day_number</h1>");
  874. for ($i=0;$i<7;$i++)
  875. {
  876. if ($day_number == 7) $day_number = 0;
  877. $tmp.='<td align="center" style="border-bottom: 1px solid #C0C0C0;">'.$days[$day_number].'</td>';
  878. $day_number++;
  879. }
  880. $tmp.='</tr>';
  881. $cellcount=$first_day_of_month-$this->m_calendarStartDay;
  882. if ($cellcount < 0) $cellcount = 7 + $cellcount;
  883. $tmp.='<tr>';
  884. $week_number = $this->getWeekNumber(1,$month,$year);
  885. $weekTime = date("Y-m-d",mktime(12,0,0,$month,1,$year));
  886. $weekUrl = atkSelf()."?view=week&viewdate=$weekTime";
  887. if(dateutil::isISO8601()) $tmp.='<td style="border-right: 1px solid #C0C0C0;"><i>'.href($weekUrl,$week_number).'</i>&nbsp;</td>';
  888. for ($i=1;$i<=$cellcount;$i++)
  889. {
  890. $tmp.='<td>&nbsp;</td>';
  891. }
  892. $startdate = date("Y-m-d",mktime(0,0,0,$month,1,$year));
  893. $enddate = date("Y-m-d",mktime(23,59,59,$month,$days_in_month,$year));
  894. $itemdates = $this->getItemsCompact($startdate,$enddate);
  895. for ($i=1;$i<=$days_in_month;$i++)
  896. {
  897. if ($cellcount == 7)
  898. {
  899. $tmp.="</tr><tr>\n";
  900. $week_number++;
  901. $weekTime = date("Y-m-d",mktime(12,0,0,$month,$i,$year));
  902. switch ($this->m_calendarStartDay)
  903. {
  904. case 0: $tmp_i = $i+1;
  905. case 1: $tmp_i = $i;
  906. case 3: $tmp_i = $i-2;
  907. case 6: $tmp_i = $i+2;
  908. }
  909. $week_number = $this->getWeekNumber($tmp_i,$month,$year);
  910. $weekUrl = atkSelf()."?view=week&viewdate=$weekTime";
  911. if(dateutil::isISO8601()) $tmp.='<td style="border-right: 1px solid #C0C0C0;"><i>'.href($weekUrl,$week_number).'</i>&nbsp;</td>';
  912. $cellcount=0;
  913. }
  914. $cellcount++;
  915. $dayTime = date("Y-m-d",mktime(12,0,0,$month,$i,$year));
  916. if (($i == date("d")) && ($year == date("Y")) && ($month==date("m")) && (isset($itemdates[$dayTime]) && $itemdates[$dayTime]===true))
  917. {
  918. $class="calendarItemToday";
  919. }
  920. elseif(($i == date("d")) && ($year == date("Y")) && ($month==date("m")))
  921. {
  922. $class="calendarToday";
  923. }
  924. elseif(isset($itemdates[$dayTime]) && $itemdates[$dayTime]===true)
  925. {
  926. $class="calendarItem";
  927. }
  928. else
  929. {
  930. $class="calendarNormal";
  931. }
  932. $dayUrl = atkSelf()."?view=day&viewdate=$dayTime";
  933. $daynumber = ($i>9?$i:"&nbsp;".$i);
  934. $date = sprintf('%4d-%02d-%02d',$year,$month,$i);
  935. atkdebug("Date: $date | start: $weekstartday | end: $weekendday");
  936. if(($this->m_view=="day" && $i == $this->m_day && $month == $this->m_month && $year == $this->m_year) ||
  937. ($this->m_view=="week" && $date>=$weekstartday && $date<=$weekendday) ||
  938. ($this->m_view=="month" && $this->m_month==$month))
  939. {
  940. $bgcolor="#FFDD00";
  941. }
  942. else
  943. {
  944. $bgcolor="#FFFFFF";
  945. }
  946. $tmp.='<td align="right" bgcolor="'.$bgcolor.'">'.href($dayUrl,$daynumber,'','','class="'.$class.'"').'</td>';
  947. }
  948. $remaining_cells = 7-$cellcount;
  949. for ($i=0;$i<$remaining_cells;$i++)
  950. {
  951. $tmp.='<td style="color: #C0C0C0">'.($showdaysnextmonth?($i+1):'&nbsp;').'</td>';
  952. }
  953. $tmp.='</tr>';
  954. $tmp.='</table>';
  955. return $tmp;
  956. }
  957. function getContractInfo()
  958. {
  959. static $contractInfo = null;
  960. if(is_null($contractInfo))
  961. {
  962. $user = &atkGetUser();
  963. // These can be found in the current user contract so get the current valid contract
  964. $sql = "SELECT workstarttime,workendtime,WorkingDays FROM usercontract
  965. WHERE userid = ".($user["id"]!=""?$user["id"]:"-1")."
  966. AND startdate <= '".$this->m_viewdate."'
  967. AND (enddate >= '".$this->m_viewdate."'
  968. OR enddate IS NULL)";
  969. $db = &atkGetDb();
  970. $row = $db->getrows($sql);
  971. $contractInfo = array();
  972. if(count($row)==1)
  973. {
  974. if($row[0]["workstarttime"]!="00:00:00") $contractInfo["workstarttime"] = substr($row[0]["workstarttime"],0,5);
  975. if($row[0]["workendtime"]!="00:00:00")$contractInfo["workendtime"] = substr($row[0]["workendtime"],0,5);
  976. if($row[0]["WorkingDays"]!="")
  977. {
  978. $contractInfo["workdays"]=0;
  979. $days = explode("|",$row[0]["WorkingDays"]);
  980. foreach($days as $day)
  981. {
  982. $contractInfo["workdays"]|= pow(2,$day);
  983. }
  984. }
  985. }
  986. else
  987. {
  988. $contractInfo["workstarttime"]="08:30";
  989. $contractInfo["workendtime"]="17:00";
  990. $contractInfo["workdays"]=0;
  991. }
  992. }
  993. return $contractInfo;
  994. }
  995. function getUserSchedulerPrefs()
  996. {
  997. $user = getUser();
  998. $schedulerNode = &getNode("scheduler.scheduler");
  999. $userprefs = $schedulerNode->getUserSchedulerPrefs($user["id"]);
  1000. // Set some defaults incase they aren't set
  1001. if($userprefs["timeschedule"]=="") $userprefs["timeschedule"]=30;
  1002. if($userprefs["scheduler_emptycolor"]=="") $userprefs["scheduler_emptycolor"]='#DDDDDD';
  1003. if($userprefs["scheduler_emptyworkhourscolor"]=="") $userprefs["scheduler_emptyworkhourscolor"]='#FFFFFF';
  1004. if($userprefs["showweeknumbers"]=="") $userprefs["showweeknumbers"]=false;
  1005. if($userprefs["showlunarphases"]=="") $userprefs["showlunarphases"]=false;
  1006. if($userprefs["showtodo"]=="") $userprefs["showtodo"]=false;
  1007. if($userprefs["showemployeebirthdays"]=="") $userprefs["showemployeebirthdays"]=false;
  1008. return $userprefs;
  1009. }
  1010. }
  1011. ?>