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 public static function saveActivityObject(ActivityObject $actobj, Notice $stored)
105 $other = Happening::getKV('uri', $actobj->id);
106 if ($other instanceof Happening) {
107 // TRANS: Client exception thrown when trying to create an event that already exists.
108 throw new ClientException(_m('Event already exists.'));
116 foreach ($actobj->extra as $extra) {
119 $dtstart = $extra[2];
124 // location is optional
125 $location = $extra[2];
132 if(empty($dtstart)) {
133 // TRANS: Exception thrown when has no start date
134 throw new Exception(_m('No start date for event.'));
137 // TRANS: Exception thrown when has no end date
138 throw new Exception(_m('No end date for event.'));
141 // convert RFC3339 dates delivered in Activity Stream to MySQL DATETIME date format
142 $start_time = new DateTime($dtstart);
143 $start_time->setTimezone(new DateTimeZone('UTC'));
144 $start_time = $start_time->format('Y-m-d H:i:s');
145 $end_time = new DateTime($dtend);
146 $end_time->setTimezone(new DateTimeZone('UTC'));
147 $end_time = $end_time->format('Y-m-d H:i:s');
149 $ev = new Happening();
151 $ev->id = UUID::gen();
152 $ev->uri = $actobj->id;
153 $ev->profile_id = $stored->getProfile()->getID();
154 $ev->start_time = $start_time;
155 $ev->end_time = $end_time;
156 $ev->title = $actobj->title;
157 $ev->location = $location;
158 $ev->description = $stored->getContent();
160 $ev->created = $stored->getCreated();
166 public function insert()
168 $result = parent::insert();
169 if ($result === false) {
170 common_log_db_error($this, 'INSERT', __FILE__);
171 throw new ServerException(_('Failed to insert '._ve(get_called_class()).' into database'));
177 * Returns the profile's canonical url, not necessarily a uri/unique id
179 * @return string $url
181 public function getUrl()
183 if (empty($this->url) ||
184 !filter_var($this->url, FILTER_VALIDATE_URL)) {
185 throw new InvalidUrlException($this->url);
190 public function getUri()
197 return Notice::getKV('uri', $this->getUri());
200 static function fromNotice($notice)
202 return Happening::getKV('uri', $notice->getUri());
207 return RSVP::forEvent($this);
210 function getRSVP($profile)
212 return RSVP::pkeyGet(array('profile_id' => $profile->getID(),
213 'event_uri' => $this->getUri()));