]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/Happening.php
Kinda complete and kinda working-ish events
[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
51 class Happening extends Managed_DataObject
52 {
53     const OBJECT_TYPE = 'http://activitystrea.ms/schema/1.0/event';
54
55     public $__table = 'happening'; // table name
56     public $id;                    // varchar(36) UUID
57     public $uri;                   // varchar(255)
58     public $profile_id;            // int
59     public $start_time;            // datetime
60     public $end_time;              // datetime
61     public $title;                 // varchar(255)
62     public $location;              // varchar(255)
63     public $description;           // text
64     public $created;               // datetime
65
66     /**
67      * Get an instance by key
68      *
69      * @param string $k Key to use to lookup (usually 'id' for this class)
70      * @param mixed  $v Value to lookup
71      *
72      * @return Happening object found, or null for no hits
73      *
74      */
75     function staticGet($k, $v=null)
76     {
77         return Memcached_DataObject::staticGet('Happening', $k, $v);
78     }
79
80     /**
81      * The One True Thingy that must be defined and declared.
82      */
83     public static function schemaDef()
84     {
85         return array(
86             'description' => 'A real-world happening',
87             'fields' => array(
88                 'id' => array('type' => 'char',
89                               'length' => 36,
90                               'not null' => true,
91                               'description' => 'UUID'),
92                 'uri' => array('type' => 'varchar',
93                                'length' => 255,
94                                'not null' => true),
95                 'profile_id' => array('type' => 'int'),
96                 'start_time' => array('type' => 'datetime'),
97                 'end_time' => array('type' => 'datetime'),
98                 'title' => array('type' => 'varchar',
99                                  'length' => 255,
100                                  'not null' => true),
101                 'location' => array('type' => 'varchar',
102                                     'length' => 255,
103                                     'not null' => true),
104                 'description' => array('type' => 'text'),
105                 'created' => array('type' => 'datetime',
106                                    'not null' => true),
107             ),
108             'primary key' => array('id'),
109             'unique keys' => array(
110                 'happening_uri_key' => array('uri'),
111             ),
112         );
113     }
114
115     function saveNew($profile, $start_time, $end_time, $title, $location, $description, $options)
116     {
117         if (array_key_exists('uri', $options)) {
118             $other = Happening::staticGet('uri', $options['uri']);
119             if (!empty($other)) {
120                 throw new ClientException(_('Event already exists.'));
121             }
122         }
123
124         $ev = new Happening();
125
126         $ev->id          = UUID::gen();
127         $ev->profile_id  = $profile->id;
128         $ev->start_time  = common_sql_date($start_time);
129         $ev->end_time    = common_sql_date($end_time);
130         $ev->title       = $title;
131         $ev->location    = $location;
132         $ev->description = $description;
133
134         if (array_key_exists('created', $options)) {
135             $ev->created = $options['created'];
136         } else {
137             $ev->created = common_sql_now();
138         }
139
140         if (array_key_exists('uri', $options)) {
141             $ev->uri = $options['uri'];
142         } else {
143             $ev->uri = common_local_url('showevent',
144                                         array('id' => $ev->id));
145         }
146
147         $ev->insert();
148
149         // XXX: does this get truncated?
150
151         $content = sprintf(_('"%s" %s - %s (%s): %s'),
152                            $title,
153                            common_exact_date($start_time),
154                            common_exact_date($end_time),
155                            $location,
156                            $description);
157
158         $rendered = sprintf(_('<span class="vevent">'.
159                               '<span class="summary">%s</span> '.
160                               '<abbr class="dtstart" title="%s">%s</a> - '.
161                               '<abbr class="dtend" title="%s">%s</a> '.
162                               '(<span class="location">%s</span>): '.
163                               '<span class="description">%s</span> '.
164                               '</span>'),
165                             htmlspecialchars($title),
166                             htmlspecialchars(common_date_iso8601($start_time)),
167                             htmlspecialchars(common_exact_date($start_time)),
168                             htmlspecialchars(common_date_iso8601($end_time)),
169                             htmlspecialchars(common_exact_date($end_time)),
170                             htmlspecialchars($location),
171                             htmlspecialchars($description));
172
173         $options = array_merge(array('object_type' => Happening::OBJECT_TYPE),
174                                $options);
175
176         if (!array_key_exists('uri', $options)) {
177             $options['uri'] = $ev->uri;
178         }
179
180         $saved = Notice::saveNew($profile->id,
181                                  $content,
182                                  array_key_exists('source', $options) ?
183                                  $options['source'] : 'web',
184                                  $options);
185
186         return $saved;
187     }
188
189     function getNotice()
190     {
191         return Notice::staticGet('uri', $this->uri);
192     }
193
194     static function fromNotice()
195     {
196         return Happening::staticGet('uri', $notice->uri);
197     }
198 }