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(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
67 * Get an instance by key
69 * @param string $k Key to use to lookup (usually 'id' for this class)
70 * @param mixed $v Value to lookup
72 * @return Happening object found, or null for no hits
75 function staticGet($k, $v=null)
77 return Memcached_DataObject::staticGet('Happening', $k, $v);
81 * The One True Thingy that must be defined and declared.
83 public static function schemaDef()
86 'description' => 'A real-world happening',
88 'id' => array('type' => 'char',
91 'description' => 'UUID'),
92 'uri' => array('type' => 'varchar',
95 'profile_id' => array('type' => 'int', 'not null' => true),
96 'start_time' => array('type' => 'datetime', 'not null' => true),
97 'end_time' => array('type' => 'datetime', 'not null' => true),
98 'title' => array('type' => 'varchar',
101 'location' => array('type' => 'varchar',
103 'url' => array('type' => 'varchar',
105 'description' => array('type' => 'text'),
106 'created' => array('type' => 'datetime',
109 'primary key' => array('id'),
110 'unique keys' => array(
111 'happening_uri_key' => array('uri'),
113 'foreign keys' => array('happening_profile_id__key' => array('profile', array('profile_id' => 'id'))),
114 'indexes' => array('happening_created_idx' => array('created'),
115 'happening_start_end_idx' => array('start_time', 'end_time')),
119 function saveNew($profile, $start_time, $end_time, $title, $location, $description, $url, $options=array())
121 if (array_key_exists('uri', $options)) {
122 $other = Happening::staticGet('uri', $options['uri']);
123 if (!empty($other)) {
124 // TRANS: Client exception thrown when trying to create an event that already exists.
125 throw new ClientException(_m('Event already exists.'));
129 $ev = new Happening();
131 $ev->id = UUID::gen();
132 $ev->profile_id = $profile->id;
133 $ev->start_time = common_sql_date($start_time);
134 $ev->end_time = common_sql_date($end_time);
136 $ev->location = $location;
137 $ev->description = $description;
140 if (array_key_exists('created', $options)) {
141 $ev->created = $options['created'];
143 $ev->created = common_sql_now();
146 if (array_key_exists('uri', $options)) {
147 $ev->uri = $options['uri'];
149 $ev->uri = common_local_url('showevent',
150 array('id' => $ev->id));
155 // XXX: does this get truncated?
157 // TRANS: Event description. %1$s is a title, %2$s is start time, %3$s is end time,
158 // TRANS: %4$s is location, %5$s is a description.
159 $content = sprintf(_m('"%1$s" %2$s - %3$s (%4$s): %5$s'),
161 common_exact_date($ev->start_time),
162 common_exact_date($ev->end_time),
166 // TRANS: Rendered event description. %1$s is a title, %2$s is start time, %3$s is start time,
167 // TRANS: %4$s is end time, %5$s is end time, %6$s is location, %7$s is description.
168 // TRANS: Class names should not be translated.
169 $rendered = sprintf(_m('<span class="vevent">'.
170 '<span class="summary">%1$s</span> '.
171 '<abbr class="dtstart" title="%2$s">%3$s</a> - '.
172 '<abbr class="dtend" title="%4$s">%5$s</a> '.
173 '(<span class="location">%6$s</span>): '.
174 '<span class="description">%7$s</span> '.
176 htmlspecialchars($title),
177 htmlspecialchars(common_date_iso8601($ev->start_time)),
178 htmlspecialchars(common_exact_date($ev->start_time)),
179 htmlspecialchars(common_date_iso8601($ev->end_time)),
180 htmlspecialchars(common_exact_date($ev->end_time)),
181 htmlspecialchars($location),
182 htmlspecialchars($description));
184 $options = array_merge(array('object_type' => Happening::OBJECT_TYPE),
187 if (!array_key_exists('uri', $options)) {
188 $options['uri'] = $ev->uri;
192 $options['urls'] = array($url);
195 $saved = Notice::saveNew($profile->id,
197 array_key_exists('source', $options) ?
198 $options['source'] : 'web',
206 return Notice::staticGet('uri', $this->uri);
209 static function fromNotice($notice)
211 return Happening::staticGet('uri', $notice->uri);
216 return RSVP::forEvent($this);
219 function getRSVP($profile)
221 return RSVP::pkeyGet(array('profile_id' => $profile->id,
222 'event_id' => $this->id));