123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- defined('GNUSOCIAL') || die();
- class Happening extends Managed_DataObject
- {
- const OBJECT_TYPE = 'http://activitystrea.ms/schema/1.0/event';
- public $__table = 'happening';
- public $id;
- public $uri;
- public $profile_id;
- public $start_time;
- public $end_time;
- public $title;
- public $location;
- public $url;
- public $description;
- public $created;
-
- public static function schemaDef()
- {
- return array(
- 'description' => 'A real-world happening',
- 'fields' => array(
- 'id' => array('type' => 'char',
- 'length' => 36,
- 'not null' => true,
- 'description' => 'UUID'),
- 'uri' => array('type' => 'varchar',
- 'length' => 191,
- 'not null' => true),
- 'profile_id' => array('type' => 'int', 'not null' => true),
- 'start_time' => array('type' => 'datetime', 'not null' => true),
- 'end_time' => array('type' => 'datetime', 'not null' => true),
- 'title' => array('type' => 'varchar',
- 'length' => 191,
- 'not null' => true),
- 'location' => array('type' => 'varchar',
- 'length' => 191),
- 'url' => array('type' => 'varchar',
- 'length' => 191),
- 'description' => array('type' => 'text'),
- 'created' => array('type' => 'datetime',
- 'not null' => true),
- ),
- 'primary key' => array('id'),
- 'unique keys' => array(
- 'happening_uri_key' => array('uri'),
- ),
- 'foreign keys' => array(
- 'happening_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
- 'happening_uri_fkey' => array('notice', array('uri' => 'uri'))
- ),
- 'indexes' => array(
- 'happening_profile_id_idx' => array('profile_id'),
- 'happening_created_idx' => array('created'),
- 'happening_start_time_end_time_idx' => array('start_time', 'end_time'),
- ),
- );
- }
- public static function saveActivityObject(Activity $act, Notice $stored)
- {
- if (count($act->objects) !== 1) {
-
- throw new Exception(_m('Too many activity objects.'));
- }
- $actobj = $act->objects[0];
- if (!ActivityUtils::compareTypes($actobj->type, [Happening::OBJECT_TYPE])) {
-
- throw new Exception(_m('Wrong type for object.'));
- }
- try {
- $other = Happening::getByKeys(['uri' => $actobj->id]);
- throw AlreadyFulfilledException('Happening already exists.');
- } catch (NoResultException $e) {
-
- }
- $dtstart = null;
- $dtend = null;
- $location = null;
- $url = null;
- foreach ($actobj->extra as $extra) {
- switch ($extra[0]) {
- case 'dtstart':
- $dtstart = $extra[2];
- break;
- case 'dtend':
- $dtend = $extra[2];
- break;
- case 'location':
-
- $location = $extra[2];
- break;
- case 'url':
-
- $url = $extra[2];
- }
- }
- if (empty($dtstart)) {
-
- throw new Exception(_m('No start date for event.'));
- }
- if (empty($dtend)) {
-
- throw new Exception(_m('No end date for event.'));
- }
-
- $start_time = new DateTime($dtstart);
- $start_time->setTimezone(new DateTimeZone('UTC'));
- $start_time = $start_time->format('Y-m-d H:i:s');
- $end_time = new DateTime($dtend);
- $end_time->setTimezone(new DateTimeZone('UTC'));
- $end_time = $end_time->format('Y-m-d H:i:s');
- $ev = new Happening();
- $ev->id = UUID::gen();
- $ev->uri = $actobj->id;
- $ev->profile_id = $stored->getProfile()->getID();
- $ev->start_time = $start_time;
- $ev->end_time = $end_time;
- $ev->title = $actobj->title;
- $ev->location = $location;
- $ev->description = $stored->getContent();
- $ev->url = $url;
- $ev->created = $stored->getCreated();
- $ev->insert();
- return $ev;
- }
- public function insert()
- {
- $result = parent::insert();
- if ($result === false) {
- common_log_db_error($this, 'INSERT', __FILE__);
- throw new ServerException(_('Failed to insert '._ve(get_called_class()).' into database'));
- }
- return $result;
- }
-
- public function getUrl()
- {
- if (empty($this->url) ||
- !filter_var($this->url, FILTER_VALIDATE_URL)) {
- throw new InvalidUrlException($this->url);
- }
- return $this->url;
- }
- public function getUri()
- {
- return $this->uri;
- }
- public function getStored()
- {
- return Notice::getByKeys(array('uri'=>$this->getUri()));
- }
- public static function fromStored(Notice $stored)
- {
- if (!ActivityUtils::compareTypes($stored->getObjectType(), [self::OBJECT_TYPE])) {
- throw new ServerException('Notice is not of type '.self::OBJECT_TYPE);
- }
- return self::getByKeys(array('uri'=>$stored->getUri()));
- }
- public function getRSVPs()
- {
- return RSVP::forEvent($this);
- }
- public function getRSVP($profile)
- {
- return RSVP::pkeyGet([
- 'profile_id' => $profile->getID(),
- 'event_uri' => $this->getUri(),
- ]);
- }
- public static function getObjectType()
- {
- return self::OBJECT_TYPE;
- }
- public function asActivityObject()
- {
- $actobj = new ActivityObject();
- $actobj->id = $this->getUri();
- $actobj->type = self::getObjectType();
- $actobj->title = $this->title;
- $actobj->summary = $this->description;
- $actobj->extra[] = array('dtstart',
- array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
- common_date_iso8601($this->start_time));
- $actobj->extra[] = array('dtend',
- array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
- common_date_iso8601($this->end_time));
- $actobj->extra[] = array('location',
- array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
- $this->location);
- try {
- $actobj->extra[] = array('url',
- array('xmlns' => 'urn:ietf:params:xml:ns:xcal'),
- $this->getUrl());
- } catch (InvalidUrlException $e) {
-
- }
-
- $actobj->extra[] = array('startdate',
- array('xmlns' => 'http://purl.org/rss/1.0/plugins/event/'),
- common_date_iso8601($this->start_time));
- $actobj->extra[] = array('enddate',
- array('xmlns' => 'http://purl.org/rss/1.0/plugins/event/'),
- common_date_iso8601($this->end_time));
- $actobj->extra[] = array('location',
- array('xmlns' => 'http://purl.org/rss/1.0/plugins/event/'),
- $this->location);
- return $actobj;
- }
- }
|