]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/newnotice.php
Added is_readonly() method to all Actions
[quix0rs-gnu-social.git] / actions / newnotice.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 class NewnoticeAction extends Action {
23
24         function is_readonly() {
25                 return false;
26         }
27
28         function handle($args) {
29                 parent::handle($args);
30                 # XXX: Ajax!
31
32                 if (!common_logged_in()) {
33                         common_user_error(_('Not logged in.'));
34                 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
35                         $this->save_new_notice();
36                 } else {
37                         $this->show_form();
38                 }
39         }
40
41         function save_new_notice() {
42
43                 $user = common_current_user();
44                 assert($user); # XXX: maybe an error instead...
45                 $notice = new Notice();
46                 assert($notice);
47                 $notice->profile_id = $user->id; # user id *is* profile id
48                 $notice->is_local = 1;
49                 $notice->created = DB_DataObject_Cast::dateTime();
50                 # Default theme uses 'content' for something else
51                 $notice->content = $this->trimmed('status_textarea');
52
53                 if (!$notice->content) {
54                         $this->show_form(_('No content!'));
55                         return;
56                 } else if (strlen($notice->content) > 140) {
57                         $this->show_form(_('That\'s too long. Max notice size is 140 chars.'));
58                         return;
59                 }
60
61                 $notice->rendered = common_render_content($notice->content, $notice);
62
63                 $id = $notice->insert();
64
65                 if (!$id) {
66                         common_server_error(_('Problem saving notice.'));
67                         return;
68                 }
69
70                 $orig = clone($notice);
71                 $notice->uri = common_notice_uri($notice);
72
73                 if (!$notice->update($orig)) {
74                         common_server_error(_('Problem saving notice.'));
75                         return;
76                 }
77
78         common_save_replies($notice);
79                 common_broadcast_notice($notice);
80
81                 $returnto = $this->trimmed('returnto');
82                 if ($returnto) {
83                         $url = common_local_url($returnto,
84                                                                         array('nickname' => $user->nickname));
85                 } else {
86                         $url = common_local_url('shownotice',
87                                                                         array('notice' => $id));
88                 }
89                 common_redirect($url, 303);
90         }
91
92         function show_top($content=NULL) {
93                 common_notice_form(NULL, $content);
94         }
95
96         function show_form($msg=NULL) {
97                 $content = $this->trimmed('status_textarea');
98                 if (!$content) {
99                         $replyto = $this->trimmed('replyto');
100                         $profile = Profile::staticGet('nickname', $replyto);
101                         if ($profile) {
102                                 $content = '@' . $profile->nickname . ' ';
103                         }
104                 }
105                 common_show_header(_('New notice'), NULL, $content,
106                                    array($this, 'show_top'));
107                 if ($msg) {
108                         common_element('p', 'error', $msg);
109                 }
110                 common_show_footer();
111         }
112 }