1: <?php
2: /**
3: * Base class for Kronolith resources. Partially presents a Horde_Share_Object
4: * interface.
5: *
6: * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (GPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/gpl.
10: *
11: * @author Michael J. Rubinsky <mrubinsk@horde.org>
12: * @package Kronolith
13: */
14: abstract class Kronolith_Resource_Base
15: {
16: /**
17: * Instance copy of parameters
18: *
19: * name - Display name of resource.
20: * calendar - The calendar associated with this resource.
21: * description -
22: * email -
23: * response_type - a RESPONSETYPE_* constant
24: *
25: * @var array
26: */
27: protected $_params = array();
28:
29: /**
30: * Resource's internal id
31: *
32: * @var integer
33: */
34: protected $_id = '';
35:
36: /**
37: * Const'r
38: *
39: * @param array $params
40: *
41: * @return Kronolith_Resource object
42: */
43: public function __construct($params = array())
44: {
45: if (!empty($params['id'])) {
46: // Existing resource
47: $this->_id = $params['id'];
48: }
49:
50: // Names are required.
51: if (empty($params['name'])) {
52: throw new Horde_Exception('Required \'name\' attribute missing from resource calendar');
53: }
54: $this->_params = array_merge(
55: array('description' => '',
56: 'response_type' => Kronolith_Resource::RESPONSETYPE_MANUAL,
57: 'members' => '',
58: 'calendar' => '',
59: 'email' => ''
60: ),
61: $params
62: );
63: }
64:
65: /**
66: * Obtain the resource's internal identifier.
67: *
68: * @return mixed The id.
69: */
70: public function getId()
71: {
72: return $this->_id;
73: }
74:
75: /**
76: * Allow setting of properties
77: *
78: * @param string $property The property to set
79: * @param mixed $value The value to set to
80: *
81: * @return void
82: */
83: public function set($property, $value)
84: {
85: $this->_params[$property] = $value;
86: }
87:
88: /**
89: * @TODO: need to fine tune this
90: *
91: * @param $user
92: * @param $permission
93: * @param $restrict
94: * @return unknown_type
95: */
96: public function hasPermission($user, $permission = Horde_Perms::READ, $restrict = null)
97: {
98: if (($permission & (Horde_Perms::EDIT | Horde_Perms::DELETE)) &&
99: !$GLOBALS['registry']->isAdmin()) {
100: return false;
101: }
102:
103: return true;
104: }
105:
106: /**
107: * Implemented to stand in as a share object.
108: *
109: * @param $property
110: * @return unknown_type
111: */
112: public function get($property)
113: {
114: $property = str_replace('resource_', '', $property);
115: if ($property == 'type' && empty($this->_params['type'])) {
116: return ($this instanceof Kronolith_Resource_Single) ? 'Single' : 'Group';
117: }
118: if (!array_key_exists($property, $this->_params)) {
119: throw new Horde_Exception(sprintf('The property \'%s\' does not exist', $property));
120: }
121: return $this->_params[$property];
122: }
123:
124: /**
125: * Save resource to storage.
126: *
127: * @return Kronolith_Resource object
128: * @throws Kronolith_Exception
129: */
130: public function save()
131: {
132: return $this->getDriver()->save($this);
133: }
134:
135: /**
136: * Get a storage driver instance for the resource. For now, just instantiate
137: * it here, in future, probably inject it in the const'r.
138: *
139: * @return Kronolith_Driver_Resource
140: */
141: public function getDriver()
142: {
143: if (!$this->get('calendar')) {
144: return Kronolith::getDriver('Resource');
145: } else {
146: return Kronolith::getDriver('Resource', $this->get('calendar'));
147: }
148: }
149:
150: /**
151: * Check availability and return an appropriate Kronolith response code.
152: *
153: * @param Kronolith_Event $event The event to check on
154: *
155: * @return integer Kronolith::RESPONSE* constant
156: */
157: public function getResponse($event)
158: {
159: switch($this->getResponseType()) {
160: case Kronolith_Resource::RESPONSETYPE_ALWAYS_ACCEPT:
161: return Kronolith::RESPONSE_ACCEPTED;
162: case Kronolith_Resource::RESPONSETYPE_AUTO:
163: if ($this->isFree($event)) {
164: return Kronolith::RESPONSE_ACCEPTED;
165: } else {
166: return Kronolith::RESPONSE_DECLINED;
167: }
168: case Kronolith_Resource::RESPONSETYPE_ALWAYS_DECLINE:
169: return Kronolith::RESPONSE_DECLINED;
170: case Kronolith_Resource::RESPONSETYPE_NONE:
171: case Kronolith_Resource::RESPONSETYPE_MANUAL:
172: return Kronolith::RESPONSE_NONE;
173: }
174: }
175:
176: /**
177: * Determine if event is free for specified time
178: *
179: * @param $startTime
180: * @param $endTime
181: * @return unknown_type
182: */
183: abstract public function isFree($event);
184:
185: /**
186: * Adds $event to this resource's calendar - thus blocking the time
187: * for any other event.
188: *
189: * @param $event
190: * @return unknown_type
191: */
192: abstract public function addEvent($event);
193:
194: /**
195: * Remove this event from resource's calendar
196: *
197: * @param $event
198: * @return unknown_type
199: */
200: abstract public function removeEvent($event);
201:
202: /**
203: * Obtain the freebusy information for this resource. Takes into account
204: * if this is a group of resources or not. (Returns the cumulative FB info
205: * for all the resources in the group.
206: * @return unknown_type
207: */
208: abstract public function getFreeBusy();
209:
210: /**
211: * Sets the current resource's id. Must not be an existing resource.
212: *
213: * @param int $id The id for this resource
214: *
215: * @return unknown_type
216: */
217: abstract public function setId($id);
218:
219: /**
220: * Get ResponseType for this resource.
221: * @return unknown_type
222: */
223: abstract public function getResponseType();
224:
225: }
226: