1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14: 
 15: 
 16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34: 
 35: class Horde_Kolab_FreeBusy_Export_Freebusy_Base
 36: implements Horde_Kolab_FreeBusy_Export_Freebusy
 37: {
 38:      39:  40:  41:  42: 
 43:     private $_resource;
 44: 
 45:      46:  47:  48:  49: 
 50:     private $_backend;
 51: 
 52:      53:  54:  55:  56: 
 57:     private $_params;
 58: 
 59:      60:  61:  62:  63: 
 64:     private $_status_map;
 65: 
 66:      67:  68:  69:  70:  71:  72: 
 73:     public function __construct(
 74:         Horde_Kolab_FreeBusy_Export_Freebusy_Backend $backend,
 75:         Horde_Kolab_FreeBusy_Resource $resource,
 76:         $params
 77:     ) {
 78:         if (!isset($params['future_days'])) {
 79:             $params['future_days'] = 60;
 80:         }
 81:         if (!isset($params['past_days'])) {
 82:             $params['past_days'] = 0;
 83:         }
 84:         if (!isset($params['request_time'])) {
 85:             $params['request_time'] = (string) new Horde_Date();
 86:         }
 87:         if (!isset($params['status_map'])) {
 88:             $this->_status_map = new Horde_Kolab_FreeBusy_Helper_Freebusy_StatusMap_Default();
 89:         } else {
 90:             $this->_status_map = $params['status_map'];
 91:         }
 92:         $this->_resource = $resource;
 93:         $this->_backend  = $backend;
 94:         $this->_params   = $params;
 95:     }
 96: 
 97:      98:  99: 100: 101: 
102:     private function _today()
103:     {
104:         return new Horde_Date(
105:             array(
106:                 'year' => date('Y'),
107:                 'month' => date('n'),
108:                 'mday' => date('j')
109:             )
110:         );
111:     }
112: 
113:     114: 115: 116: 117: 
118:     public function getStart()
119:     {
120:         try {
121:             $past = $this->_resource->getOwner()->getFreeBusyPast();
122:         } catch (Horde_Kolab_FreeBusy_Exception $e) {
123:             $past = $this->_params['past_days'];
124:         }
125:         $start = $this->_today();
126:         $start->mday = $start->mday - $past;
127:         return $start;
128:     }
129: 
130:     131: 132: 133: 134: 
135:     public function getEnd()
136:     {
137:         try {
138:             $future = $this->_resource->getOwner()->getFreeBusyFuture();
139:         } catch (Horde_Kolab_FreeBusy_Exception $e) {
140:             $future = $this->_params['future_days'];
141:         }
142:         $end = $this->_today();
143:         $end->mday = $end->mday + $future;
144:         return $end;
145:     }
146: 
147:     148: 149: 150: 151: 
152:     public function getResourceName()
153:     {
154:         return $this->_resource->getName();
155:     }
156: 
157:     158: 159: 160: 161: 
162:     public function getOrganizerMail()
163:     {
164:         return 'MAILTO:' . $this->_resource->getOwner()->getMail();
165:     }
166: 
167:     168: 169: 170: 171: 
172:     public function getOrganizerName()
173:     {
174:         $params = array();
175:         $name = $this->_resource->getOwner()->getName();
176:         if (!empty($name)) {
177:             $params['cn'] = $name;
178:         }
179:         return $params;
180:     }
181: 
182:     183: 184: 185: 186: 
187:     public function getDateStamp()
188:     {
189:         if (isset($this->_params['request_time'])) {
190:             return $this->_params['request_time'];
191:         } else {
192:             return (string) new Horde_Date();
193:         }
194:     }
195: 
196:     197: 198: 199: 200: 
201:     public function export()
202:     {
203:         
204:         $vCal = new Horde_iCalendar();
205:         $vCal->setAttribute('PRODID', $this->_backend->getProductId());
206:         $vCal->setAttribute('METHOD', 'PUBLISH');
207: 
208:         
209:         $vFb = &Horde_iCalendar::newComponent('vfreebusy', $vCal);
210: 
211:         $vFb->setAttribute(
212:             'ORGANIZER', $this->getOrganizerMail(), $this->getOrganizerName()
213:         );
214:         $vFb->setAttribute('DTSTAMP', $this->getDateStamp());
215:         $vFb->setAttribute('DTSTART', $this->getStart()->timestamp());
216:         $vFb->setAttribute('DTEND', $this->getEnd()->timestamp());
217:         $url = $this->_backend->getUrl();
218:         if (!empty($url)) {
219:             $vFb->setAttribute('URL', $this->getUrl());
220:         }
221: 
222:         
223:         foreach (
224:             $this->_resource->listEvents($this->getStart(), $this->getEnd())
225:             as $event
226:         ) {
227:             $status = $this->_status_map->map($event->getStatus());
228:             $duration = $event->duration();
229:             $extra = $event->getEncodedInformation();
230:             foreach (
231:                 $event->getBusyTimes($this->getStart(), $this->getEnd())
232:                 as $busy
233:             ) {
234:                 $vFb->addBusyPeriod($status, $busy, null, $duration, $extra);
235:             }
236:         }
237: 
238:         
239:         $vFb->simplify();
240: 
241:         
242:         $vCal->addComponent($vFb);
243:         return $vCal;
244:     }
245: }
246: