We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Happening.php 8.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
<?php
/**
 * Data class for happenings
 *
 * PHP version 5
 *
 * @category Data
 * @package  StatusNet
 * @author   Evan Prodromou <evan@status.net>
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
 * @link     http://status.net/
 *
 * StatusNet - the distributed open-source microblogging tool
 * Copyright (C) 2011, StatusNet, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

if (!defined('STATUSNET')) {
    exit(1);
}

/**
 * Data class for happenings
 *
 * There's already an Event class in lib/event.php, so we couldn't
 * call this an Event without causing a hole in space-time.
 *
 * "Happening" seemed good enough.
 *
 * @category Event
 * @package  StatusNet
 * @author   Evan Prodromou <evan@status.net>
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
 * @link     http://status.net/
 *
 * @see      Managed_DataObject
 */
class Happening extends Managed_DataObject
{
    const OBJECT_TYPE = 'http://activitystrea.ms/schema/1.0/event';

    public $__table = 'happening'; // table name
    public $id;                    // varchar(36) UUID
    public $uri;                   // varchar(255)
    public $profile_id;            // int
    public $start_time;            // datetime
    public $end_time;              // datetime
    public $title;                 // varchar(255)
    public $location;              // varchar(255)
Evan Prodromou's avatar
Evan Prodromou committed
62
    public $url;                   // varchar(255)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    public $description;           // text
    public $created;               // datetime

    /**
     * Get an instance by key
     *
     * @param string $k Key to use to lookup (usually 'id' for this class)
     * @param mixed  $v Value to lookup
     *
     * @return Happening object found, or null for no hits
     *
     */
    function staticGet($k, $v=null)
    {
        return Memcached_DataObject::staticGet('Happening', $k, $v);
    }

    /**
     * The One True Thingy that must be defined and declared.
     */
    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' => 255,
                               'not null' => true),
Evan Prodromou's avatar
Evan Prodromou committed
95 96 97
                'profile_id' => array('type' => 'int', 'not null' => true),
                'start_time' => array('type' => 'datetime', 'not null' => true),
                'end_time' => array('type' => 'datetime', 'not null' => true),
98 99 100 101
                'title' => array('type' => 'varchar',
                                 'length' => 255,
                                 'not null' => true),
                'location' => array('type' => 'varchar',
Evan Prodromou's avatar
Evan Prodromou committed
102 103 104
                                    'length' => 255),
                'url' => array('type' => 'varchar',
                               'length' => 255),
105 106 107 108 109 110 111 112
                'description' => array('type' => 'text'),
                'created' => array('type' => 'datetime',
                                   'not null' => true),
            ),
            'primary key' => array('id'),
            'unique keys' => array(
                'happening_uri_key' => array('uri'),
            ),
Evan Prodromou's avatar
Evan Prodromou committed
113 114 115
            'foreign keys' => array('happening_profile_id__key' => array('profile', array('profile_id' => 'id'))),
            'indexes' => array('happening_created_idx' => array('created'),
                               'happening_start_end_idx' => array('start_time', 'end_time')),
116 117 118
        );
    }

Evan Prodromou's avatar
Evan Prodromou committed
119
    function saveNew($profile, $start_time, $end_time, $title, $location, $description, $url, $options=array())
120 121 122 123
    {
        if (array_key_exists('uri', $options)) {
            $other = Happening::staticGet('uri', $options['uri']);
            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.'));
126 127 128 129 130 131 132 133 134 135 136 137
            }
        }

        $ev = new Happening();

        $ev->id          = UUID::gen();
        $ev->profile_id  = $profile->id;
        $ev->start_time  = common_sql_date($start_time);
        $ev->end_time    = common_sql_date($end_time);
        $ev->title       = $title;
        $ev->location    = $location;
        $ev->description = $description;
Evan Prodromou's avatar
Evan Prodromou committed
138
        $ev->url         = $url;
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

        if (array_key_exists('created', $options)) {
            $ev->created = $options['created'];
        } else {
            $ev->created = common_sql_now();
        }

        if (array_key_exists('uri', $options)) {
            $ev->uri = $options['uri'];
        } else {
            $ev->uri = common_local_url('showevent',
                                        array('id' => $ev->id));
        }

        $ev->insert();

        // XXX: does this get truncated?

157 158 159
        // TRANS: Event description. %1$s is a title, %2$s is start time, %3$s is end time,
	// TRANS: %4$s is location, %5$s is a description.
        $content = sprintf(_m('"%1$s" %2$s - %3$s (%4$s): %5$s'),
160
                           $title,
161 162
                           common_exact_date($ev->start_time),
                           common_exact_date($ev->end_time),
163 164 165
                           $location,
                           $description);

166 167 168
        // TRANS: Rendered event description. %1$s is a title, %2$s is start time, %3$s is start time,
	// TRANS: %4$s is end time, %5$s is end time, %6$s is location, %7$s is description.
	// TRANS: Class names should not be translated.
169 170 171 172 173 174
        $rendered = sprintf(_m('<span class="vevent">'.
                              '<span class="summary">%1$s</span> '.
                              '<abbr class="dtstart" title="%2$s">%3$s</a> - '.
                              '<abbr class="dtend" title="%4$s">%5$s</a> '.
                              '(<span class="location">%6$s</span>): '.
                              '<span class="description">%7$s</span> '.
175 176
                              '</span>'),
                            htmlspecialchars($title),
177 178 179 180
                            htmlspecialchars(common_date_iso8601($ev->start_time)),
                            htmlspecialchars(common_exact_date($ev->start_time)),
                            htmlspecialchars(common_date_iso8601($ev->end_time)),
                            htmlspecialchars(common_exact_date($ev->end_time)),
181 182 183 184 185 186 187 188 189 190
                            htmlspecialchars($location),
                            htmlspecialchars($description));

        $options = array_merge(array('object_type' => Happening::OBJECT_TYPE),
                               $options);

        if (!array_key_exists('uri', $options)) {
            $options['uri'] = $ev->uri;
        }

Evan Prodromou's avatar
Evan Prodromou committed
191 192 193 194
        if (!empty($url)) {
            $options['urls'] = array($url);
        }

195 196 197 198 199 200 201 202 203 204 205 206 207 208
        $saved = Notice::saveNew($profile->id,
                                 $content,
                                 array_key_exists('source', $options) ?
                                 $options['source'] : 'web',
                                 $options);

        return $saved;
    }

    function getNotice()
    {
        return Notice::staticGet('uri', $this->uri);
    }

Evan Prodromou's avatar
Evan Prodromou committed
209
    static function fromNotice($notice)
210 211 212
    {
        return Happening::staticGet('uri', $notice->uri);
    }
Evan Prodromou's avatar
Evan Prodromou committed
213 214 215 216 217

    function getRSVPs()
    {
        return RSVP::forEvent($this);
    }
Evan Prodromou's avatar
Evan Prodromou committed
218 219 220 221 222 223

    function getRSVP($profile)
    {
        return RSVP::pkeyGet(array('profile_id' => $profile->id,
                                   'event_id' => $this->id));
    }
224
}