1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Kronolith_Driver_Holidays extends Kronolith_Driver
16: {
17: public function listAlarms($date, $fullevent = false)
18: {
19: return array();
20: }
21:
22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40:
41: public function listEvents($startDate = null, $endDate = null,
42: $showRecurrence = false, $hasAlarm = false,
43: $json = false, $coverDates = true)
44: {
45: if (!class_exists('Date_Holidays')) {
46: Horde::logMessage('Support for Date_Holidays has been enabled but the package seems to be missing.', 'ERR');
47: return array();
48: }
49:
50: if (is_null($startDate) && !is_null($endDate)) {
51: $startDate = clone $endDate;
52: $startDate->year--;
53: }
54: if (is_null($endDate) && !is_null($startDate)) {
55: $endDate = clone $startDate;
56: $endDate->year++;
57: }
58: if ($hasAlarm || is_null($startDate) || is_null($endDate)) {
59: return array();
60: }
61:
62: $startDate = clone $startDate;
63: $startDate->hour = $startDate->min = $startDate->sec = 0;
64: $endDate = clone $endDate;
65: $endDate->hour = 23;
66: $endDate->min = $endDate->sec = 59;
67:
68: Date_Holidays::staticSetProperty('DIE_ON_MISSING_LOCALE', false);
69:
70: $results = array();
71: for ($year = $startDate->year; $year <= $endDate->year; $year++) {
72: $dh = Date_Holidays::factory($this->calendar, $year, $this->_params['language']);
73: if (Date_Holidays::isError($dh)) {
74: Horde::logMessage(sprintf('Factory was unable to produce driver object for driver %s in year %s with locale %s',
75: $this->calendar, $year, $this->_params['language']), 'ERR');
76: continue;
77: }
78: $dh->addTranslation($this->_params['language']);
79: $events = $this->_getEvents($dh, $startDate, $endDate);
80: foreach ($events as $event) {
81: Kronolith::addEvents($results, $event, $startDate, $endDate,
82: $showRecurrence, $json, $coverDates);
83: }
84: }
85:
86: return $results;
87: }
88:
89: 90: 91: 92:
93: public function getEvent($eventId = null)
94: {
95: if (!$eventId) {
96: $date = new Date();
97: return new Kronolith_Event_Holidays($this, new Date_Holidays_Holiday(null, null, $date, null));
98: }
99:
100: list($id, $date) = explode('-', $eventId, 2);
101: $year = substr($date, 0, 4);
102:
103: $dh = Date_Holidays::factory($this->calendar, $year, $this->_params['language']);
104: if (Date_Holidays::isError($dh)) {
105: Horde::logMessage(sprintf('Factory was unable to produce driver object for driver %s in year %s with locale %s',
106: $this->calendar, $year, $this->_params['language']), 'ERR');
107: return false;
108: }
109: $dh->addTranslation($this->_params['language']);
110:
111: $event = $dh->getHoliday($id);
112: if ($event instanceof PEAR_Error) {
113: throw new Horde_Exception_NotFound($event);
114: }
115:
116: return new Kronolith_Event_Holidays($this, $event);
117: }
118:
119: private function _getEvents($dh, $startDate, $endDate)
120: {
121: $events = array();
122: for ($date = new Horde_Date($startDate);
123: $date->compareDate($endDate) <= 0;
124: $date->mday++) {
125: $holidays = $dh->getHolidayForDate($date->format('Y-m-d'), null, true);
126: if (Date_Holidays::isError($holidays)) {
127: Horde::logMessage(sprintf('Unable to retrieve list of holidays from %s to %s',
128: (string)$startDate, (string)$endDate), __FILE__, __LINE__);
129: continue;
130: }
131:
132: if (is_null($holidays)) {
133: continue;
134: }
135:
136: foreach ($holidays as $holiday) {
137: $event = new Kronolith_Event_Holidays($this, $holiday);
138: $events[] = $event;
139: }
140: }
141: return $events;
142: }
143:
144: private function _getTranslationFile($driver)
145: {
146: static $data_dir;
147: if (!isset($data_dir)) {
148: $pear_config = new PEAR_Config();
149: $data_dir = $pear_config->get('data_dir');
150: }
151: if (empty($data_dir)) {
152: return;
153: }
154:
155: foreach (array('', '_' . $driver) as $pkg_ext) {
156: foreach (array('ser', 'xml') as $format) {
157: $location = $data_dir . '/Date_Holidays' . $pkg_ext . '/lang/'
158: . $driver . '/' . $this->_params['language'] . '.' . $format;
159: if (file_exists($location)) {
160: return array($format, $location);
161: }
162: }
163: }
164:
165: return array(null, null);
166: }
167:
168: }
169: