1: <?php
2: /**
3: * Events methods for Horde_Service_Facebook
4: *
5: * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
6: *
7: * @author Michael J. Rubinsky <mrubinsk@horde.org>
8: * @category Horde
9: * @package Service_Facebook
10: */
11: class Horde_Service_Facebook_Events extends Horde_Service_Facebook_Base
12: {
13: /**
14: * Returns events according to the filters specified.
15: *
16: * @param integer $uid User associated with events. A null parameter
17: * will default to the session user.
18: * @param string $eids Filter by these comma-separated event ids. A
19: * null parameter will get all events for the
20: * user.
21: * @param integer $start_time Filter with this unix time as lower bound.
22: * A null or zero parameter indicates no lower
23: * bound.
24: * @param integer $end_time Filter with this UTC as upper bound. A null
25: * or zero parameter indicates no upper bound.
26: * @param string $rsvp_status Only show events where the given uid has this
27: * rsvp status. This only works if you have
28: * specified a value for $uid. Values are as
29: * in events.getMembers. Null indicates to
30: * ignore rsvp status when filtering.
31: *
32: * @return array The events matching the query.
33: */
34: public function &get($uid = null, $eids = null, $start_time = null,
35: $end_time = null, $rsvp_status = null)
36: {
37: if (empty($uid)) {
38: $uid = 'me()';//$this->_facebook->auth->getLoggedInUser();
39: }
40:
41: $fql = 'SELECT eid, name, tagline, nid, pic_square, pic_small, '
42: . 'pic_big, pic, host, description, event_type, event_subtype, '
43: . 'start_time, end_time, creator, update_time, location, venue, '
44: . 'privacy, hide_guest_list FROM event WHERE eid IN';
45:
46: if (!empty($rsvp_status)) {
47: $fql .= '(SELECT eid FROM event_member WHERE uid=' . $uid . ' AND rsvp_status=\'' . $rsvp_status . '\')';
48: } else {
49: $fql .= '(SELECT eid FROM event_member WHERE uid=' . $uid . ')';
50: }
51:
52: if (!empty($eids)) {
53: $fql .= ' AND eid IN (' . implode(',', $eids) . ')';
54: }
55:
56: if (!empty($start_time)) {
57: $fql .= ' AND start_time>=' . $start_time;
58: }
59: if (!empty($end_time)) {
60: $fql .= ' AND start_time<=' . $end_time;
61: }
62:
63: // Get the events
64: $events = $this->_facebook->fql->run($fql);
65:
66: // If no requested status, query to get the current statuses.
67: if (empty($rsvp_status)) {
68: $eids = array();
69: foreach ($events as $e) {
70: $eids[] = $e['eid'];
71: }
72: $fql = 'SELECT eid, rsvp_status FROM event_member WHERE uid=' . $uid
73: . 'AND eid IN (' . implode(',', $eids) . ')';
74:
75: $status = $this->_facebook->fql->run($fql);
76: foreach ($events as &$e) {
77: foreach ($status as $s) {
78: if ($s['eid'] == $e['eid']) {
79: $e['rsvp_status'] = $this->_fromDriverStatus($s['rsvp_status']);
80: }
81: }
82: }
83: } else {
84: // Otherwise, we already know the status.
85: foreach ($events as &$e) {
86: $e['rsvp_status'] = $this->_fromDriverStatus($rsvp_status);
87: }
88: }
89:
90: return $events;
91: }
92:
93: protected function _fromDriverStatus($driver_status)
94: {
95: switch ($driver_status) {
96: case 'attending':
97: return 'confirmed';
98: case 'unsure':
99: return 'tentative';
100: case 'declined':
101: case 'not_replied':
102: return 'free';
103: }
104: }
105:
106: /**
107: * Returns membership list data associated with an event.
108: *
109: * @param integer $eid event id
110: *
111: * @return array An assoc array of four membership lists, with keys
112: * 'attending', 'unsure', 'declined', and 'not_replied'
113: */
114: public function &getMembers($eid)
115: {
116: return $this->_facebook->callMethod(
117: 'facebook.events.getMembers',
118: array('eid' => $eid));
119: }
120:
121: /**
122: * RSVPs the current user to this event.
123: *
124: * @param integer $eid event id
125: * @param string $rsvp_status 'attending', 'unsure', or 'declined'
126: *
127: * @return boolean
128: */
129: public function &rsvp($eid, $rsvp_status)
130: {
131: return $this->_facebook->callMethod(
132: 'facebook.events.rsvp',
133: array('eid' => $eid,
134: 'rsvp_status' => $rsvp_status));
135: }
136:
137:
138: /**
139: * Cancels an event. Only works for events where application is the admin.
140: *
141: * @param integer $eid event id
142: * @param string $cancel_message (Optional) message to send to members of
143: * the event about why it is cancelled
144: *
145: * @return boolean
146: */
147: public function &cancel($eid, $cancel_message = '')
148: {
149: return $this->_facebook->callMethod(
150: 'facebook.events.cancel',
151: array('eid' => $eid,
152: 'cancel_message' => $cancel_message));
153: }
154:
155: /**
156: * Creates an event on behalf of the user is there is a session, otherwise on
157: * behalf of app. Successful creation guarantees app will be admin.
158: *
159: * @param array $event_info json encoded event information
160: *
161: * @return integer event id
162: */
163: public function &create(array $event_info)
164: {
165: return $this->_facebook->callMethod(
166: 'facebook.events.create',
167: array('event_info' => $event_info));
168: }
169:
170: /**
171: * Edits an existing event. Only works for events where application is admin.
172: *
173: * @param integer $eid event id
174: * @param array $event_info json encoded event information
175: *
176: * @return boolean true if successful
177: */
178: public function &edit($eid, array $event_info)
179: {
180: return $this->_facebook->callMethod(
181: 'facebook.events.edit',
182: array('eid' => $eid,
183: 'event_info' => $event_info));
184: }
185:
186: }