]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/RSVP.php
updates to make RSVPs work
[quix0rs-gnu-social.git] / plugins / Event / RSVP.php
1 <?php
2 /**
3  * Data class for event RSVPs
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2011, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Data class for event RSVPs
36  *
37  * @category Event
38  * @package  StatusNet
39  * @author   Evan Prodromou <evan@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41  * @link     http://status.net/
42  *
43  * @see      Managed_DataObject
44  */
45
46 class RSVP extends Managed_DataObject
47 {
48     const POSITIVE = 'http://activitystrea.ms/schema/1.0/rsvp-yes';
49     const POSSIBLE = 'http://activitystrea.ms/schema/1.0/rsvp-maybe';
50     const NEGATIVE = 'http://activitystrea.ms/schema/1.0/rsvp-no';
51
52     public $__table = 'rsvp'; // table name
53     public $id;                // varchar(36) UUID
54     public $uri;               // varchar(255)
55     public $profile_id;        // int
56     public $event_id;          // varchar(36) UUID
57     public $result;            // tinyint
58     public $created;           // datetime
59
60     /**
61      * Get an instance by key
62      *
63      * @param string $k Key to use to lookup (usually 'id' for this class)
64      * @param mixed  $v Value to lookup
65      *
66      * @return RSVP object found, or null for no hits
67      *
68      */
69     function staticGet($k, $v=null)
70     {
71         return Memcached_DataObject::staticGet('RSVP', $k, $v);
72     }
73
74     /**
75      * Get an instance by compound key
76      *
77      * @param array $kv array of key-value mappings
78      *
79      * @return Bookmark object found, or null for no hits
80      *
81      */
82
83     function pkeyGet($kv)
84     {
85         return Memcached_DataObject::pkeyGet('RSVP', $kv);
86     }
87
88     /**
89      * The One True Thingy that must be defined and declared.
90      */
91     public static function schemaDef()
92     {
93         return array(
94             'description' => 'Plan to attend event',
95             'fields' => array(
96                 'id' => array('type' => 'char',
97                               'length' => 36,
98                               'not null' => true,
99                               'description' => 'UUID'),
100                 'uri' => array('type' => 'varchar',
101                                'length' => 255,
102                                'not null' => true),
103                 'profile_id' => array('type' => 'int'),
104                 'event_id' => array('type' => 'char',
105                               'length' => 36,
106                               'not null' => true,
107                               'description' => 'UUID'),
108                 'result' => array('type' => 'tinyint',
109                                   'description' => '1, 0, or null for three-state yes, no, maybe'),
110                 'created' => array('type' => 'datetime',
111                                    'not null' => true),
112             ),
113             'primary key' => array('id'),
114             'unique keys' => array(
115                 'rsvp_uri_key' => array('uri'),
116                 'rsvp_profile_event_key' => array('profile_id', 'event_id'),
117             ),
118             'foreign keys' => array('rsvp_event_id_key' => array('event', array('event_id' => 'id')),
119                                     'rsvp_profile_id__key' => array('profile', array('profile_id' => 'id'))),
120             'indexes' => array('rsvp_created_idx' => array('created')),
121         );
122     }
123
124     function saveNew($profile, $event, $result, $options=array())
125     {
126         if (array_key_exists('uri', $options)) {
127             $other = RSVP::staticGet('uri', $options['uri']);
128             if (!empty($other)) {
129                 throw new ClientException(_('RSVP already exists.'));
130             }
131         }
132
133         $other = RSVP::pkeyGet(array('profile_id' => $profile->id,
134                                      'event_id' => $event->id));
135
136         if (!empty($other)) {
137             throw new ClientException(_('RSVP already exists.'));
138         }
139
140         $rsvp = new RSVP();
141
142         $rsvp->id          = UUID::gen();
143         $rsvp->profile_id  = $profile->id;
144         $rsvp->event_id    = $event->id;
145         $rsvp->result      = self::codeFor($result);
146
147         if (array_key_exists('created', $options)) {
148             $rsvp->created = $options['created'];
149         } else {
150             $rsvp->created = common_sql_now();
151         }
152
153         if (array_key_exists('uri', $options)) {
154             $rsvp->uri = $options['uri'];
155         } else {
156             $rsvp->uri = common_local_url('showrsvp',
157                                         array('id' => $rsvp->id));
158         }
159
160         $rsvp->insert();
161
162         // XXX: come up with something sexier
163
164         $content = sprintf(_('RSVPed %s for an event.'),
165                            ($result == RSVP::POSITIVE) ? _('positively') :
166                            ($result == RSVP::NEGATIVE) ? _('negatively') : _('possibly'));
167         
168         $rendered = $content;
169
170         $options = array_merge(array('object_type' => $result),
171                                $options);
172
173         if (!array_key_exists('uri', $options)) {
174             $options['uri'] = $rsvp->uri;
175         }
176
177         $eventNotice = $event->getNotice();
178
179         if (!empty($eventNotice)) {
180             $options['reply_to'] = $eventNotice->id;
181         }
182
183         $saved = Notice::saveNew($profile->id,
184                                  $content,
185                                  array_key_exists('source', $options) ?
186                                  $options['source'] : 'web',
187                                  $options);
188
189         return $saved;
190     }
191
192     function codeFor($verb)
193     {
194         return ($verb == RSVP::POSITIVE) ? 1 :
195             ($verb == RSVP::NEGATIVE) ? 0 : null;
196     }
197
198     static function verbFor($code)
199     {
200         return ($code == 1) ? RSVP::POSITIVE :
201             ($code == 0) ? RSVP::NEGATIVE : null;
202     }
203
204     function getNotice()
205     {
206         $notice = Notice::staticGet('uri', $this->uri);
207         if (empty($notice)) {
208             throw new ServerException("RSVP {$this->id} does not correspond to a notice in the DB.");
209         }
210         return $notice;
211     }
212
213     static function fromNotice($notice)
214     {
215         return RSVP::staticGet('uri', $notice->uri);
216     }
217
218     static function forEvent($event)
219     {
220         $rsvps = array(RSVP::POSITIVE => array(), RSVP::NEGATIVE => array(), RSVP::POSSIBLE => array());
221
222         $rsvp = new RSVP();
223
224         $rsvp->event_id = $event->id;
225
226         if ($rsvp->find()) {
227             while ($rsvp->fetch()) {
228                 $verb = self::verbFor($rsvp->result);
229                 $rsvps[$verb][] = clone($rsvp);
230             }
231         }
232
233         return $rsvps;
234     }
235
236     function delete()
237     {
238     }
239 }