]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/RSVP.php
69cae4b7dc5972556c96ff0eac540ef13acf027e
[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      * The One True Thingy that must be defined and declared.
76      */
77     public static function schemaDef()
78     {
79         return array(
80             'description' => 'A real-world event',
81             'fields' => array(
82                 'id' => array('type' => 'char',
83                               'length' => 36,
84                               'not null' => true,
85                               'description' => 'UUID'),
86                 'uri' => array('type' => 'varchar',
87                                'length' => 255,
88                                'not null' => true),
89                 'profile_id' => array('type' => 'int'),
90                 'event_id' => array('type' => 'char',
91                               'length' => 36,
92                               'not null' => true,
93                               'description' => 'UUID'),
94                 'result' => array('type' => 'tinyint',
95                                   'description' => '1, 0, or null for three-state yes, no, maybe'),
96                 'created' => array('type' => 'datetime',
97                                    'not null' => true),
98             ),
99             'primary key' => array('id'),
100             'unique keys' => array(
101                 'rsvp_uri_key' => array('uri'),
102                 'rsvp_profile_event_key' => array('profile_id', 'event_id'),
103             ),
104             'foreign keys' => array('rsvp_event_id_key' => array('event', array('event_id' => 'id')),
105                                     'rsvp_profile_id__key' => array('profile', array('profile_id' => 'id'))),
106             'indexes' => array('rsvp_created_idx' => array('created')),
107         );
108     }
109
110     function saveNew($profile, $event, $result, $options)
111     {
112         if (array_key_exists('uri', $options)) {
113             $other = RSVP::staticGet('uri', $options['uri']);
114             if (!empty($other)) {
115                 throw new ClientException(_('RSVP already exists.'));
116             }
117         }
118
119         $other = RSVP::pkeyGet(array('profile_id' => $profile->id,
120                                      'event_id' => $event->id));
121
122         if (!empty($other)) {
123             throw new ClientException(_('RSVP already exists.'));
124         }
125
126         $rsvp = new RSVP();
127
128         $rsvp->id          = UUID::gen();
129         $rsvp->profile_id  = $profile->id;
130         $rsvp->event_id    = $event->id;
131         $rsvp->result      = self::codeFor($result);
132
133         if (array_key_exists('created', $options)) {
134             $rsvp->created = $options['created'];
135         } else {
136             $rsvp->created = common_sql_now();
137         }
138
139         if (array_key_exists('uri', $options)) {
140             $rsvp->uri = $options['uri'];
141         } else {
142             $rsvp->uri = common_local_url('showrsvp',
143                                         array('id' => $rsvp->id));
144         }
145
146         $rsvp->insert();
147
148         // XXX: come up with something sexier
149
150         $content = sprintf(_('RSVPed %s for an event.'),
151                            ($result == RSVP::POSITIVE) ? _('positively') :
152                            ($result == RSVP::NEGATIVE) ? _('negatively') : _('possibly'));
153         
154         $rendered = $content;
155
156         $options = array_merge(array('object_type' => $result),
157                                $options);
158
159         if (!array_key_exists('uri', $options)) {
160             $options['uri'] = $rsvp->uri;
161         }
162
163         $eventNotice = $event->getNotice();
164
165         if (!empty($eventNotice)) {
166             $options['reply_to'] = $eventNotice->id;
167         }
168
169         $saved = Notice::saveNew($profile->id,
170                                  $content,
171                                  array_key_exists('source', $options) ?
172                                  $options['source'] : 'web',
173                                  $options);
174
175         return $saved;
176     }
177
178     function codeFor($verb)
179     {
180         return ($verb == RSVP::POSITIVE) ? 1 :
181             ($verb == RSVP::NEGATIVE) ? 0 : null;
182     }
183
184     function verbFor($code)
185     {
186         return ($code == 1) ? RSVP::POSITIVE :
187             ($code == 0) ? RSVP::NEGATIVE : null;
188     }
189
190     function getNotice()
191     {
192         return Notice::staticGet('uri', $this->uri);
193     }
194
195     static function fromNotice()
196     {
197         return RSVP::staticGet('uri', $notice->uri);
198     }
199 }