newnotice.php 9.11 KB
Newer Older
Evan Prodromou's avatar
Evan Prodromou committed
1
<?php
Evan Prodromou's avatar
Evan Prodromou committed
2
/**
3
 * StatusNet, the distributed open-source microblogging tool
Evan Prodromou's avatar
Evan Prodromou committed
4
 *
Evan Prodromou's avatar
Evan Prodromou committed
5 6 7 8 9
 * Handler for posting new notices
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
10 11 12
 * 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.
Evan Prodromou's avatar
Evan Prodromou committed
13
 *
14 15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Evan Prodromou's avatar
Evan Prodromou committed
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
Evan Prodromou's avatar
Evan Prodromou committed
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
Evan Prodromou's avatar
Evan Prodromou committed
20 21 22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Personal
23
 * @package   StatusNet
24 25 26
 * @author    Evan Prodromou <evan@status.net>
 * @author    Zach Copley <zach@status.net>
 * @author    Sarven Capadisli <csarven@status.net>
27
 * @copyright 2008-2009 StatusNet, Inc.
28
 * @copyright 2013 Free Software Foundation, Inc.
Evan Prodromou's avatar
Evan Prodromou committed
29
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
30
 * @link      http://status.net/
31 32
 */

33
if (!defined('GNUSOCIAL')) { exit(1); }
Evan Prodromou's avatar
Evan Prodromou committed
34 35 36 37 38

/**
 * Action for posting new notices
 *
 * @category Personal
39
 * @package  StatusNet
40 41 42
 * @author   Evan Prodromou <evan@status.net>
 * @author   Zach Copley <zach@status.net>
 * @author   Sarven Capadisli <csarven@status.net>
Evan Prodromou's avatar
Evan Prodromou committed
43
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44
 * @link     http://status.net/
Evan Prodromou's avatar
Evan Prodromou committed
45
 */
46
class NewnoticeAction extends FormAction
47
{
48 49
    protected $form = 'Notice';

Evan Prodromou's avatar
Evan Prodromou committed
50 51 52
    /**
     * Title of the page
     *
53
     * Note that this usually doesn't get called unless something went wrong
Evan Prodromou's avatar
Evan Prodromou committed
54 55 56 57 58
     *
     * @return string page title
     */
    function title()
    {
59 60 61 62
        if ($this->getInfo() && $this->stored instanceof Notice) {
            // TRANS: Page title after sending a notice.
            return _('Notice posted');
        }
63 64 65
        if ($this->int('inreplyto')) {
            return _m('TITLE', 'New reply');
        }
66
        // TRANS: Page title for sending a new notice.
67
        return _m('TITLE','New notice');
Evan Prodromou's avatar
Evan Prodromou committed
68 69
    }

70 71 72
    protected function doPreparation()
    {
        foreach(array('inreplyto') as $opt) {
73
            if ($this->trimmed($opt)) {
74 75 76
                $this->formOpts[$opt] = $this->trimmed($opt);
            }
        }
77

78 79 80
        // Backwards compatibility for "share this" widget things.
        // If no 'content', use 'status_textarea'
        $this->formOpts['content'] = $this->trimmed('content') ?: $this->trimmed('status_textarea');
81 82
    }

Evan Prodromou's avatar
Evan Prodromou committed
83
    /**
84
     * This doPost saves a new notice, based on arguments
Evan Prodromou's avatar
Evan Prodromou committed
85 86 87 88 89 90 91 92 93
     *
     * If successful, will show the notice, or return an Ajax-y result.
     * If not, it will show an error message -- possibly Ajax-y.
     *
     * Also, if the notice input looks like a command, it will run the
     * command and show the results -- again, possibly ajaxy.
     *
     * @return void
     */
94
    protected function doPost()
Evan Prodromou's avatar
Evan Prodromou committed
95
    {
96
        assert($this->scoped instanceof Profile); // XXX: maybe an error instead...
97
        $user = $this->scoped->getUser();
98
        $content = $this->formOpts['content'];
99
        $options = array('source' => 'web');
100
        Event::handle('StartSaveNewNoticeWeb', array($this, $user, &$content, &$options));
101

102 103 104 105 106
        $upload = null;
        try {
            // throws exception on failure
            $upload = MediaFile::fromUpload('attach', $this->scoped);
            if (Event::handle('StartSaveNewNoticeAppendAttachment', array($this, $upload, &$content, &$options))) {
107
                $content .= ($content==='' ? '' : ' ') . $upload->shortUrl();
108 109 110 111 112 113 114 115 116 117 118 119
            }
            Event::handle('EndSaveNewNoticeAppendAttachment', array($this, $upload, &$content, &$options));

            // We could check content length here if the URL was added, but I'll just let it slide for now...

            $act->enclosures[] = $upload->getEnclosure();
        } catch (NoUploadedMediaException $e) {
            // simply no attached media to the new notice
            if (empty($content)) {
                // TRANS: Client error displayed trying to send a notice without content.
                $this->clientError(_('No content!'));
            }
120 121 122 123
        }

        $inter = new CommandInterpreter();

124
        $cmd = $inter->handle_command($user, $content);
125 126

        if ($cmd) {
mmn's avatar
mmn committed
127
            if (GNUsocial::isAjax()) {
128
                $cmd->execute(new AjaxWebChannel($this));
129
            } else {
130
                $cmd->execute(new WebChannel($this));
131 132 133 134
            }
            return;
        }

135 136 137 138 139
        if ($this->int('inreplyto')) {
            // Throws exception if the inreplyto Notice is given but not found.
            $parent = Notice::getByID($this->int('inreplyto'));
        } else {
            $parent = null;
140 141
        }

142 143 144 145 146
        $act = new Activity();
        $act->verb = ActivityVerb::POST;
        $act->time = time();
        $act->actor = $this->scoped->asActivityObject();

147 148 149 150 151 152 153 154 155 156 157
        // Reject notice if it is too long (without the HTML)
        // This is done after MediaFile::fromUpload etc. just to act the same as the ApiStatusesUpdateAction
        if (Notice::contentTooLong($content)) {
            // TRANS: Client error displayed when the parameter "status" is missing.
            // TRANS: %d is the maximum number of character for a notice.
            throw new ClientException(sprintf(_m('That\'s too long. Maximum notice size is %d character.',
                                                 'That\'s too long. Maximum notice size is %d characters.',
                                                 Notice::maxContent()),
                                              Notice::maxContent()));
        }

158 159 160 161 162 163
        $act->context = new ActivityContext();

        if ($parent instanceof Notice) {
            $act->context->replyToID = $parent->getUri();
            $act->context->replyToUrl = $parent->getUrl(true);  // maybe we don't have to send true here to force a URL?
        }
mmn's avatar
mmn committed
164

165
        if ($this->scoped->shareLocation()) {
166 167 168 169 170 171
            // use browser data if checked; otherwise profile data
            if ($this->arg('notice_data-geo')) {
                $locOptions = Notice::locationOptions($this->trimmed('lat'),
                                                      $this->trimmed('lon'),
                                                      $this->trimmed('location_id'),
                                                      $this->trimmed('location_ns'),
172
                                                      $this->scoped);
173 174 175 176 177
            } else {
                $locOptions = Notice::locationOptions(null,
                                                      null,
                                                      null,
                                                      null,
178
                                                      $this->scoped);
179
            }
180

181
            $act->context->location = Location::fromOptions($locOptions);
182 183
        }

184 185 186 187
        $content = $this->scoped->shortenLinks($content);

        // FIXME: Make sure NoticeTitle plugin gets a change to add the title to our activityobject!
        if (Event::handle('StartNoticeSaveWeb', array($this, $this->scoped, &$content, &$options))) {
188

189 190
            // FIXME: We should be able to get the attentions from common_render_content!
            // and maybe even directly save whether they're local or not!
191
            $act->context->attention = common_get_attentions($content, $this->scoped, $parent);
192

mmn's avatar
mmn committed
193 194 195
            // $options gets filled with possible scoping settings
            ToSelector::fillActivity($this, $act, $options);

196 197 198
            $actobj = new ActivityObject();
            $actobj->type = ActivityObject::NOTE;
            $actobj->content = common_render_content($content, $this->scoped, $parent);
199

200 201
            // Finally add the activity object to our activity
            $act->objects[] = $actobj;
202

203
            $this->stored = Notice::saveActivity($act, $this->scoped, $options);
204

mmn's avatar
mmn committed
205
            if ($upload instanceof MediaFile) {
206
                $upload->attachToNotice($this->stored);
207 208
            }

209
            Event::handle('EndNoticeSaveWeb', array($this, $this->stored));
210
        }
211

212
        Event::handle('EndSaveNewNoticeWeb', array($this, $user, &$content, &$options));
213

mmn's avatar
mmn committed
214
        if (!GNUsocial::isAjax()) {
215
            $url = common_local_url('shownotice', array('notice' => $this->stored->id));
216 217
            common_redirect($url, 303);
        }
218

219
        return _('Saved the notice!');
220 221
    }

222
    protected function showContent()
223
    {
224 225 226 227
        if ($this->getInfo() && $this->stored instanceof Notice) {
            $this->showNotice($this->stored);
        } elseif (!$this->getError()) {
            parent::showContent();
228
        }
Evan Prodromou's avatar
Evan Prodromou committed
229 230 231 232 233 234 235 236 237 238 239
    }

    /**
     * Output a notice
     *
     * Used to generate the notice code for Ajax results.
     *
     * @param Notice $notice Notice that was saved
     *
     * @return void
     */
240
    function showNotice(Notice $notice)
241
    {
Evan Prodromou's avatar
Evan Prodromou committed
242
        $nli = new NoticeListItem($notice, $this);
243
        $nli->show();
244
    }
245 246 247 248 249

    public function showNoticeForm()
    {
        // pass
    }
250
}