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
51 class Happening extends Managed_DataObject
53 const OBJECT_TYPE = 'http://activitystrea.ms/schema/1.0/event';
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 $url; // varchar(255)
64 public $description; // text
65 public $created; // datetime
68 * Get an instance by key
70 * @param string $k Key to use to lookup (usually 'id' for this class)
71 * @param mixed $v Value to lookup
73 * @return Happening object found, or null for no hits
76 function staticGet($k, $v=null)
78 return Memcached_DataObject::staticGet('Happening', $k, $v);
82 * The One True Thingy that must be defined and declared.
84 public static function schemaDef()
87 'description' => 'A real-world happening',
89 'id' => array('type' => 'char',
92 'description' => 'UUID'),
93 'uri' => array('type' => 'varchar',
96 'profile_id' => array('type' => 'int', 'not null' => true),
97 'start_time' => array('type' => 'datetime', 'not null' => true),
98 'end_time' => array('type' => 'datetime', 'not null' => true),
99 'title' => array('type' => 'varchar',
102 'location' => array('type' => 'varchar',
104 'url' => array('type' => 'varchar',
106 'description' => array('type' => 'text'),
107 'created' => array('type' => 'datetime',
110 'primary key' => array('id'),
111 'unique keys' => array(
112 'happening_uri_key' => array('uri'),
114 'foreign keys' => array('happening_profile_id__key' => array('profile', array('profile_id' => 'id'))),
115 'indexes' => array('happening_created_idx' => array('created'),
116 'happening_start_end_idx' => array('start_time', 'end_time')),
120 function saveNew($profile, $start_time, $end_time, $title, $location, $description, $url, $options=array())
122 if (array_key_exists('uri', $options)) {
123 $other = Happening::staticGet('uri', $options['uri']);
124 if (!empty($other)) {
125 throw new ClientException(_('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 $content = sprintf(_('"%s" %s - %s (%s): %s'),
159 common_exact_date($start_time),
160 common_exact_date($end_time),
164 $rendered = sprintf(_('<span class="vevent">'.
165 '<span class="summary">%s</span> '.
166 '<abbr class="dtstart" title="%s">%s</a> - '.
167 '<abbr class="dtend" title="%s">%s</a> '.
168 '(<span class="location">%s</span>): '.
169 '<span class="description">%s</span> '.
171 htmlspecialchars($title),
172 htmlspecialchars(common_date_iso8601($start_time)),
173 htmlspecialchars(common_exact_date($start_time)),
174 htmlspecialchars(common_date_iso8601($end_time)),
175 htmlspecialchars(common_exact_date($end_time)),
176 htmlspecialchars($location),
177 htmlspecialchars($description));
179 $options = array_merge(array('object_type' => Happening::OBJECT_TYPE),
182 if (!array_key_exists('uri', $options)) {
183 $options['uri'] = $ev->uri;
187 $options['urls'] = array($url);
190 $saved = Notice::saveNew($profile->id,
192 array_key_exists('source', $options) ?
193 $options['source'] : 'web',
201 return Notice::staticGet('uri', $this->uri);
204 static function fromNotice($notice)
206 return Happening::staticGet('uri', $notice->uri);
211 return RSVP::forEvent($this);
214 function getRSVP($profile)
216 common_log(LOG_DEBUG, "Finding RSVP for " . $profile->id . ', ' . $this->id);
217 return RSVP::pkeyGet(array('profile_id' => $profile->id,
218 'event_id' => $this->id));