3 * Data class for happenings
9 * @author Evan Prodromou <evan@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2011, StatusNet, Inc.
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.
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.
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/>.
30 if (!defined('GNUSOCIAL')) { exit(1); }
33 * Data class for happenings
35 * There's already an Event class in lib/event.php, so we couldn't
36 * call this an Event without causing a hole in space-time.
38 * "Happening" seemed good enough.
42 * @author Evan Prodromou <evan@status.net>
43 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
44 * @link http://status.net/
46 * @see Managed_DataObject
48 class Happening extends Managed_DataObject
50 const OBJECT_TYPE = 'http://activitystrea.ms/schema/1.0/event';
52 public $__table = 'happening'; // table name
53 public $id; // varchar(36) UUID
54 public $uri; // varchar(191) not 255 because utf8mb4 takes more space
55 public $profile_id; // int
56 public $start_time; // datetime
57 public $end_time; // datetime
58 public $title; // varchar(191) not 255 because utf8mb4 takes more space
59 public $location; // varchar(191) not 255 because utf8mb4 takes more space
60 public $url; // varchar(191) not 255 because utf8mb4 takes more space
61 public $description; // text
62 public $created; // datetime
65 * The One True Thingy that must be defined and declared.
67 public static function schemaDef()
70 'description' => 'A real-world happening',
72 'id' => array('type' => 'char',
75 'description' => 'UUID'),
76 'uri' => array('type' => 'varchar',
79 'profile_id' => array('type' => 'int', 'not null' => true),
80 'start_time' => array('type' => 'datetime', 'not null' => true),
81 'end_time' => array('type' => 'datetime', 'not null' => true),
82 'title' => array('type' => 'varchar',
85 'location' => array('type' => 'varchar',
87 'url' => array('type' => 'varchar',
89 'description' => array('type' => 'text'),
90 'created' => array('type' => 'datetime',
93 'primary key' => array('id'),
94 'unique keys' => array(
95 'happening_uri_key' => array('uri'),
97 'foreign keys' => array('happening_profile_id__key' => array('profile', array('profile_id' => 'id'))),
98 'indexes' => array('happening_created_idx' => array('created'),
99 'happening_start_end_idx' => array('start_time', 'end_time')),
103 static function saveNew($profile, $start_time, $end_time, $title, $location, $description, $url, $options=array())
105 if (array_key_exists('uri', $options)) {
106 $other = Happening::getKV('uri', $options['uri']);
107 if (!empty($other)) {
108 // TRANS: Client exception thrown when trying to create an event that already exists.
109 throw new ClientException(_m('Event already exists.'));
113 $ev = new Happening();
115 $ev->id = UUID::gen();
116 $ev->profile_id = $profile->id;
117 $ev->start_time = $start_time;
118 $ev->end_time = $end_time;
120 $ev->location = $location;
121 $ev->description = $description;
124 if (array_key_exists('created', $options)) {
125 $ev->created = $options['created'];
127 $ev->created = common_sql_now();
130 if (array_key_exists('uri', $options)) {
131 $ev->uri = $options['uri'];
133 $ev->uri = common_local_url('showevent',
134 array('id' => $ev->id));
139 // XXX: does this get truncated?
141 // TRANS: Event description. %1$s is a title, %2$s is start time, %3$s is end time,
142 // TRANS: %4$s is location, %5$s is a description.
143 $content = sprintf(_m('"%1$s" %2$s - %3$s (%4$s): %5$s'),
145 common_exact_date($ev->start_time),
146 common_exact_date($ev->end_time),
150 // TRANS: Rendered microformats2 tagged event description.
151 // TRANS: %1$s is a title, %2$s is start time, %3$s is start time,
152 // TRANS: %4$s is end time, %5$s is end time, %6$s is location, %7$s is description.
153 // TRANS: Class names should not be translated.
154 $rendered = sprintf(_m('<div class="h-event">'.
155 '<p class="p-name p-summary">%1$s</p> '.
156 '<time class="dt-start" datetime="%2$s">%3$s</time> - '.
157 '<time class="dt-end" datetime="%4$s">%5$s</time> '.
158 '(<span class="p-location">%6$s</span>): '.
159 '<div class="p-description">%7$s</div> '.
161 htmlspecialchars($title),
162 htmlspecialchars(common_date_iso8601($ev->start_time)),
163 htmlspecialchars(common_exact_date($ev->start_time)),
164 htmlspecialchars(common_date_iso8601($ev->end_time)),
165 htmlspecialchars(common_exact_date($ev->end_time)),
166 htmlspecialchars($location),
167 htmlspecialchars($description));
169 $options = array_merge(array('object_type' => Happening::OBJECT_TYPE),
172 if (!array_key_exists('uri', $options)) {
173 $options['uri'] = $ev->getUri();
177 $options['urls'] = array($url);
180 $saved = Notice::saveNew($profile->getID(),
182 array_key_exists('source', $options) ?
183 $options['source'] : 'web',
190 * Returns the profile's canonical url, not necessarily a uri/unique id
192 * @return string $url
194 public function getUrl()
196 if (empty($this->url) ||
197 !filter_var($this->url, FILTER_VALIDATE_URL)) {
198 throw new InvalidUrlException($this->url);
203 public function getUri()
210 return Notice::getKV('uri', $this->getUri());
213 static function fromNotice($notice)
215 return Happening::getKV('uri', $notice->getUri());
220 return RSVP::forEvent($this);
223 function getRSVP($profile)
225 return RSVP::pkeyGet(array('profile_id' => $profile->getID(),
226 'event_uri' => $this->getUri()));