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('STATUSNET')) {
35 * Data class for happenings
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.
40 * "Happening" seemed good enough.
44 * @author Evan Prodromou <evan@status.net>
45 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
46 * @link http://status.net/
48 * @see Managed_DataObject
50 class Happening extends Managed_DataObject
52 const OBJECT_TYPE = 'http://activitystrea.ms/schema/1.0/event';
54 public $__table = 'happening'; // table name
55 public $id; // varchar(36) UUID
56 public $uri; // varchar(191) not 255 because utf8mb4 takes more space
57 public $profile_id; // int
58 public $start_time; // datetime
59 public $end_time; // datetime
60 public $title; // varchar(191) not 255 because utf8mb4 takes more space
61 public $location; // varchar(191) not 255 because utf8mb4 takes more space
62 public $url; // varchar(191) not 255 because utf8mb4 takes more space
63 public $description; // text
64 public $created; // datetime
67 * The One True Thingy that must be defined and declared.
69 public static function schemaDef()
72 'description' => 'A real-world happening',
74 'id' => array('type' => 'char',
77 'description' => 'UUID'),
78 'uri' => array('type' => 'varchar',
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',
87 'location' => array('type' => 'varchar',
89 'url' => array('type' => 'varchar',
91 'description' => array('type' => 'text'),
92 'created' => array('type' => 'datetime',
95 'primary key' => array('id'),
96 'unique keys' => array(
97 'happening_uri_key' => array('uri'),
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')),
105 static function saveNew($profile, $start_time, $end_time, $title, $location, $description, $url, $options=array())
107 if (array_key_exists('uri', $options)) {
108 $other = Happening::getKV('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.'));
115 $ev = new Happening();
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);
122 $ev->location = $location;
123 $ev->description = $description;
126 if (array_key_exists('created', $options)) {
127 $ev->created = $options['created'];
129 $ev->created = common_sql_now();
132 if (array_key_exists('uri', $options)) {
133 $ev->uri = $options['uri'];
135 $ev->uri = common_local_url('showevent',
136 array('id' => $ev->id));
141 // XXX: does this get truncated?
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'),
147 common_exact_date($ev->start_time),
148 common_exact_date($ev->end_time),
152 // TRANS: Rendered microformats2 tagged event description.
153 // TRANS: %1$s is a title, %2$s is start time, %3$s is start time,
154 // TRANS: %4$s is end time, %5$s is end time, %6$s is location, %7$s is description.
155 // TRANS: Class names should not be translated.
156 $rendered = sprintf(_m('<div class="h-event">'.
157 '<p class="p-name p-summary">%1$s</p> '.
158 '<time class="dt-start" datetime="%2$s">%3$s</time> - '.
159 '<time class="dt-end" datetime="%4$s">%5$s</time> '.
160 '(<span class="p-location">%6$s</span>): '.
161 '<div class="p-description">%7$s</div> '.
163 htmlspecialchars($title),
164 htmlspecialchars(common_date_iso8601($ev->start_time)),
165 htmlspecialchars(common_exact_date($ev->start_time)),
166 htmlspecialchars(common_date_iso8601($ev->end_time)),
167 htmlspecialchars(common_exact_date($ev->end_time)),
168 htmlspecialchars($location),
169 htmlspecialchars($description));
171 $options = array_merge(array('object_type' => Happening::OBJECT_TYPE),
174 if (!array_key_exists('uri', $options)) {
175 $options['uri'] = $ev->uri;
179 $options['urls'] = array($url);
182 $saved = Notice::saveNew($profile->id,
184 array_key_exists('source', $options) ?
185 $options['source'] : 'web',
192 * Returns the profile's canonical url, not necessarily a uri/unique id
194 * @return string $url
196 public function getUrl()
198 if (empty($this->url) ||
199 !filter_var($this->url, FILTER_VALIDATE_URL)) {
200 throw new InvalidUrlException($this->url);
207 return Notice::getKV('uri', $this->uri);
210 static function fromNotice($notice)
212 return Happening::getKV('uri', $notice->uri);
217 return RSVP::forEvent($this);
220 function getRSVP($profile)
222 return RSVP::pkeyGet(array('profile_id' => $profile->id,
223 'event_id' => $this->id));