PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/kronolith/lib/Views/Month.php

https://github.com/wrobel/horde-fw3
PHP | 253 lines | 207 code | 32 blank | 14 comment | 57 complexity | fb82d2e38dc63194988c6799655949ed MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, LGPL-2.1, BSD-2-Clause
  1. <?php
  2. require_once KRONOLITH_BASE . '/lib/Day.php';
  3. /**
  4. * The Kronolith_View_Month:: class provides an API for viewing
  5. * months.
  6. *
  7. * $Horde: kronolith/lib/Views/Month.php,v 1.17.2.3 2009-11-07 14:34:57 jan Exp $
  8. *
  9. * @author Chuck Hagenbuch <chuck@horde.org>
  10. * @author Jan Schneider <jan@horde.org>
  11. * @since Kronolith 2.2
  12. * @package Kronolith
  13. */
  14. class Kronolith_View_Month {
  15. var $month;
  16. var $year;
  17. var $_events = array();
  18. var $_currentCalendars = array();
  19. var $_daysInView;
  20. var $_startOfView;
  21. var $_startday;
  22. function Kronolith_View_Month($month = null, $year = null, $events = null)
  23. {
  24. global $prefs;
  25. if ($month === null) {
  26. $month = date('n');
  27. }
  28. if ($year === null) {
  29. $year = date('Y');
  30. }
  31. $this->month = $month;
  32. $this->year = $year;
  33. // Need to calculate the start and length of the view.
  34. $this->_startday = new Horde_Date(array('mday' => 1,
  35. 'month' => $this->month,
  36. 'year' => $this->year));
  37. $this->_startday = $this->_startday->dayOfWeek();
  38. $this->_daysInView = Date_Calc::weeksInMonth($this->month, $this->year) * 7;
  39. if (!$prefs->getValue('week_start_monday')) {
  40. $this->_startOfView = 1 - $this->_startday;
  41. // We may need to adjust the number of days in the view if
  42. // we're starting weeks on Sunday.
  43. if ($this->_startday == HORDE_DATE_SUNDAY) {
  44. $this->_daysInView -= 7;
  45. }
  46. $endday = new Horde_Date(array('mday' => Horde_Date::daysInMonth($this->month, $this->year),
  47. 'month' => $this->month,
  48. 'year' => $this->year));
  49. $endday = $endday->dayOfWeek();
  50. if ($endday == HORDE_DATE_SUNDAY) {
  51. $this->_daysInView += 7;
  52. }
  53. } else {
  54. if ($this->_startday == HORDE_DATE_SUNDAY) {
  55. $this->_startOfView = -5;
  56. } else {
  57. $this->_startOfView = 2 - $this->_startday;
  58. }
  59. }
  60. if ($events !== null) {
  61. $this->_events = $events;
  62. } else {
  63. $startDate = new Horde_Date(array('year' => $this->year,
  64. 'month' => $this->month,
  65. 'mday' => $this->_startOfView));
  66. $endDate = new Horde_Date(array('year' => $this->year,
  67. 'month' => $this->month,
  68. 'mday' => $this->_startOfView + $this->_daysInView,
  69. 'hour' => 23,
  70. 'min' => 59,
  71. 'sec' => 59));
  72. $startDate->correct();
  73. $endDate->correct();
  74. $this->_events = Kronolith::listEvents($startDate, $endDate, $GLOBALS['display_calendars']);
  75. if ($prefs->getValue('show_shared_side_by_side')) {
  76. $allCalendars = Kronolith::listCalendars();
  77. $this->_currentCalendars = array();
  78. foreach ($GLOBALS['display_calendars'] as $id) {
  79. $this->_currentCalendars[$id] = &$allCalendars[$id];
  80. }
  81. } else {
  82. $this->_currentCalendars = array(true);
  83. }
  84. }
  85. if (is_a($this->_events, 'PEAR_Error')) {
  86. $GLOBALS['notification']->push($this->_events, 'horde.error');
  87. $this->_events = array();
  88. }
  89. if (!is_array($this->_events)) {
  90. $this->_events = array();
  91. }
  92. }
  93. function html()
  94. {
  95. global $prefs;
  96. $sidebyside = $prefs->getValue('show_shared_side_by_side');
  97. $twentyFour = $prefs->getValue('twentyFour');
  98. $addLinks = Kronolith::getDefaultCalendar(PERMS_EDIT) &&
  99. (!empty($GLOBALS['conf']['hooks']['permsdenied']) ||
  100. Kronolith::hasPermission('max_events') === true ||
  101. Kronolith::hasPermission('max_events') > Kronolith::countEvents());
  102. if ($sidebyside) {
  103. require KRONOLITH_TEMPLATES . '/month/head_side_by_side.inc';
  104. } else {
  105. require KRONOLITH_TEMPLATES . '/month/head.inc';
  106. }
  107. $eventCategories = array();
  108. $html = '';
  109. if (!$sidebyside && count($this->_currentCalendars)) {
  110. $html .= '<tr>';
  111. }
  112. $showLocation = Kronolith::viewShowLocation();
  113. $showTime = Kronolith::viewShowTime();
  114. $day_url = Horde::applicationUrl('day.php');
  115. $this_link = $this->link(0, true);
  116. $new_url = Util::addParameter(Horde::applicationUrl('new.php'), 'url', $this_link);
  117. $new_img = Horde::img('new_small.png', '+');
  118. foreach ($this->_currentCalendars as $id => $cal) {
  119. if ($sidebyside) {
  120. $html .= '<tr>';
  121. }
  122. $cell = 0;
  123. for ($day = $this->_startOfView; $day < $this->_startOfView + $this->_daysInView; ++$day) {
  124. $date = new Horde_Date(array('year' => $this->year, 'month' => $this->month, 'mday' => $day));
  125. $daystamp = $date->timestamp();
  126. $date->hour = $twentyFour ? 12 : 6;
  127. $timestamp = $date->timestamp();
  128. $week = $date->weekOfYear();
  129. if ($cell % 7 == 0 && $cell != 0) {
  130. if ($sidebyside) {
  131. $html .= '<td>' . htmlspecialchars($cal->get('name')) . '</td>';
  132. } else {
  133. $html .= "</tr>\n<tr>";
  134. }
  135. }
  136. if (mktime(0, 0, 0) == $daystamp) {
  137. $style = 'today';
  138. } elseif (date('n', $daystamp) != $this->month) {
  139. $style = 'othermonth';
  140. } elseif (date('w', $daystamp) == 0 || date('w', $daystamp) == 6) {
  141. $style = 'weekend';
  142. } else {
  143. $style = 'text';
  144. }
  145. $html .= '<td class="' . $style . '" height="70" width="14%" valign="top"><div>';
  146. $url = Util::addParameter($day_url, 'timestamp', $daystamp);
  147. $html .= '<a class="day" href="' . $url . '">' . date('j', $daystamp) . '</a>';
  148. if ($addLinks) {
  149. $url = Util::addParameter($new_url, 'timestamp', $timestamp);
  150. if ($sidebyside) {
  151. $url = Util::addParameter($url, 'calendar', $id);
  152. }
  153. $html .= Horde::link($url, _("Create a New Event"), 'newEvent') .
  154. $new_img . '</a>';
  155. }
  156. if ($date->dayOfWeek() == HORDE_DATE_MONDAY) {
  157. $url = Util::addParameter('week.php', 'week', $week);
  158. if ($this->month == 12 && $week == 1) {
  159. $url = Util::addParameter($url, 'year', $this->year + 1);
  160. } elseif ($this->month == 1 && $week > 51) {
  161. $url = Util::addParameter($url, 'year', $this->year - 1);
  162. } else {
  163. $url = Util::addParameter($url, 'year', $this->year);
  164. }
  165. $html .= Horde::link(Horde::applicationUrl($url), '', 'week') . sprintf(_("Week %d"), $week) . '</a>';
  166. }
  167. $html .= '</div><div class="clear">&nbsp;</div>';
  168. if (!empty($this->_events[$daystamp]) &&
  169. count($this->_events[$daystamp])) {
  170. foreach ($this->_events[$daystamp] as $event) {
  171. if (!$sidebyside || $event->getCalendar() == $id) {
  172. if ($event->hasPermission(PERMS_READ)) {
  173. $eventCategories[$event->getCategory()] = true;
  174. }
  175. $html .= '<div class="month-eventbox category' . md5($event->getCategory()) . '">'
  176. . $event->getLink($timestamp, true, $this_link);
  177. if ($showTime) {
  178. $html .= '<div class="event-time">' . htmlspecialchars($event->getTimeRange()) . '</div>';
  179. }
  180. if ($showLocation) {
  181. $html .= '<div class="event-location">' . htmlspecialchars($event->getLocation()) . '</div>';
  182. }
  183. $html .= '</div>';
  184. }
  185. }
  186. }
  187. $html .= "</td>\n";
  188. ++$cell;
  189. }
  190. if ($sidebyside) {
  191. $html .= '</tr>';
  192. }
  193. }
  194. if (!$sidebyside && count($this->_currentCalendars)) {
  195. $html .= '</tr>';
  196. }
  197. echo $html . '</tbody></table>';
  198. require KRONOLITH_TEMPLATES . '/category_legend.inc';
  199. }
  200. function getStamp($offset = 0)
  201. {
  202. return @mktime(1, 1, 1, $this->month + $offset, 1, $this->year);
  203. }
  204. function getMonth($offset = 0)
  205. {
  206. $stamp = $this->getStamp($offset);
  207. return array('month' => date('n', $stamp),
  208. 'year' => date('Y', $stamp));
  209. }
  210. function link($offset = 0, $full = false)
  211. {
  212. $month = $this->getMonth($offset);
  213. return Horde::applicationUrl(Util::addParameter('month.php',
  214. array('month' => $month['month'],
  215. 'year' => $month['year'])), $full);
  216. }
  217. function getName()
  218. {
  219. return 'Month';
  220. }
  221. }