]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/Happening.php
52aa206435e23f484442ff862c6bb98e0d7863c4
[quix0rs-gnu-social.git] / plugins / Event / Happening.php
1 <?php
2 /**
3  * Data class for happenings
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 happenings
36  *
37  * There's already an Event class in lib/event.php, so we couldn't
38  * call this an Event without causing a hole in space-time.
39  *
40  * "Happening" seemed good enough.
41  *
42  * @category Event
43  * @package  StatusNet
44  * @author   Evan Prodromou <evan@status.net>
45  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
46  * @link     http://status.net/
47  *
48  * @see      Managed_DataObject
49  */
50 class Happening extends Managed_DataObject
51 {
52     const OBJECT_TYPE = 'http://activitystrea.ms/schema/1.0/event';
53
54     public $__table = 'happening'; // table name
55     public $id;                    // varchar(36) UUID
56     public $uri;                   // varchar(255)
57     public $profile_id;            // int
58     public $start_time;            // datetime
59     public $end_time;              // datetime
60     public $title;                 // varchar(255)
61     public $location;              // varchar(255)
62     public $url;                   // varchar(255)
63     public $description;           // text
64     public $created;               // datetime
65
66     /**
67      * The One True Thingy that must be defined and declared.
68      */
69     public static function schemaDef()
70     {
71         return array(
72             'description' => 'A real-world happening',
73             'fields' => array(
74                 'id' => array('type' => 'char',
75                               'length' => 36,
76                               'not null' => true,
77                               'description' => 'UUID'),
78                 'uri' => array('type' => 'varchar',
79                                'length' => 255,
80                                'not null' => true),
81                 'profile_id' => array('type' => 'int', 'not null' => true),
82                 'start_time' => array('type' => 'datetime', 'not null' => true),
83                 'end_time' => array('type' => 'datetime', 'not null' => true),
84                 'title' => array('type' => 'varchar',
85                                  'length' => 255,
86                                  'not null' => true),
87                 'location' => array('type' => 'varchar',
88                                     'length' => 255),
89                 'url' => array('type' => 'varchar',
90                                'length' => 255),
91                 'description' => array('type' => 'text'),
92                 'created' => array('type' => 'datetime',
93                                    'not null' => true),
94             ),
95             'primary key' => array('id'),
96             'unique keys' => array(
97                 'happening_uri_key' => array('uri'),
98             ),
99             'foreign keys' => array('happening_profile_id__key' => array('profile', array('profile_id' => 'id'))),
100             'indexes' => array('happening_created_idx' => array('created'),
101                                'happening_start_end_idx' => array('start_time', 'end_time')),
102         );
103     }
104
105     static function saveNew($profile, $start_time, $end_time, $title, $location, $description, $url, $options=array())
106     {
107         if (array_key_exists('uri', $options)) {
108             $other = Happening::staticGet('uri', $options['uri']);
109             if (!empty($other)) {
110                 // TRANS: Client exception thrown when trying to create an event that already exists.
111                 throw new ClientException(_m('Event already exists.'));
112             }
113         }
114
115         $ev = new Happening();
116
117         $ev->id          = UUID::gen();
118         $ev->profile_id  = $profile->id;
119         $ev->start_time  = common_sql_date($start_time);
120         $ev->end_time    = common_sql_date($end_time);
121         $ev->title       = $title;
122         $ev->location    = $location;
123         $ev->description = $description;
124         $ev->url         = $url;
125
126         if (array_key_exists('created', $options)) {
127             $ev->created = $options['created'];
128         } else {
129             $ev->created = common_sql_now();
130         }
131
132         if (array_key_exists('uri', $options)) {
133             $ev->uri = $options['uri'];
134         } else {
135             $ev->uri = common_local_url('showevent',
136                                         array('id' => $ev->id));
137         }
138
139         $ev->insert();
140
141         // XXX: does this get truncated?
142
143         // TRANS: Event description. %1$s is a title, %2$s is start time, %3$s is end time,
144         // TRANS: %4$s is location, %5$s is a description.
145         $content = sprintf(_m('"%1$s" %2$s - %3$s (%4$s): %5$s'),
146                            $title,
147                            common_exact_date($ev->start_time),
148                            common_exact_date($ev->end_time),
149                            $location,
150                            $description);
151
152         // TRANS: Rendered event description. %1$s is a title, %2$s is start time, %3$s is start time,
153         // TRANS: %4$s is end time, %5$s is end time, %6$s is location, %7$s is description.
154         // TRANS: Class names should not be translated.
155         $rendered = sprintf(_m('<span class="vevent">'.
156                               '<span class="summary">%1$s</span> '.
157                               '<abbr class="dtstart" title="%2$s">%3$s</a> - '.
158                               '<abbr class="dtend" title="%4$s">%5$s</a> '.
159                               '(<span class="location">%6$s</span>): '.
160                               '<span class="description">%7$s</span> '.
161                               '</span>'),
162                             htmlspecialchars($title),
163                             htmlspecialchars(common_date_iso8601($ev->start_time)),
164                             htmlspecialchars(common_exact_date($ev->start_time)),
165                             htmlspecialchars(common_date_iso8601($ev->end_time)),
166                             htmlspecialchars(common_exact_date($ev->end_time)),
167                             htmlspecialchars($location),
168                             htmlspecialchars($description));
169
170         $options = array_merge(array('object_type' => Happening::OBJECT_TYPE),
171                                $options);
172
173         if (!array_key_exists('uri', $options)) {
174             $options['uri'] = $ev->uri;
175         }
176
177         if (!empty($url)) {
178             $options['urls'] = array($url);
179         }
180
181         $saved = Notice::saveNew($profile->id,
182                                  $content,
183                                  array_key_exists('source', $options) ?
184                                  $options['source'] : 'web',
185                                  $options);
186
187         return $saved;
188     }
189
190     function getNotice()
191     {
192         return Notice::staticGet('uri', $this->uri);
193     }
194
195     static function fromNotice($notice)
196     {
197         return Happening::staticGet('uri', $notice->uri);
198     }
199
200     function getRSVPs()
201     {
202         return RSVP::forEvent($this);
203     }
204
205     function getRSVP($profile)
206     {
207         return RSVP::pkeyGet(array('profile_id' => $profile->id,
208                                    'event_id' => $this->id));
209     }
210 }