]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/actions/newbookmark.php
StatusNet class renamed GNUsocial
[quix0rs-gnu-social.git] / plugins / Bookmark / actions / newbookmark.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Add a new bookmark
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Bookmark
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30 if (!defined('STATUSNET')) {
31     // This check helps protect against security problems;
32     // your code file can't be executed directly from the web.
33     exit(1);
34 }
35
36 /**
37  * Add a new bookmark
38  *
39  * @category  Bookmark
40  * @package   StatusNet
41  * @author    Evan Prodromou <evan@status.net>
42  * @copyright 2010 StatusNet, Inc.
43  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
44  * @link      http://status.net/
45  */
46 class NewbookmarkAction extends Action
47 {
48     protected $user        = null;
49     protected $error       = null;
50     protected $complete    = null;
51     protected $title       = null;
52     protected $url         = null;
53     protected $tags        = null;
54     protected $description = null;
55
56     /**
57      * Returns the title of the action
58      *
59      * @return string Action title
60      */
61     function title()
62     {
63         // TRANS: Title for action to create a new bookmark.
64         return _m('New bookmark');
65     }
66
67     /**
68      * For initializing members of the class.
69      *
70      * @param array $argarray misc. arguments
71      *
72      * @return boolean true
73      */
74     function prepare($argarray)
75     {
76         parent::prepare($argarray);
77
78         if ($this->boolean('ajax')) {
79             GNUsocial::setApi(true);
80         }
81
82         $this->user = common_current_user();
83
84         if (empty($this->user)) {
85             // TRANS: Client exception thrown when trying to create a new bookmark while not logged in.
86             throw new ClientException(_m('Must be logged in to post a bookmark.'),
87                                       403);
88         }
89
90         if ($this->isPost()) {
91             $this->checkSessionToken();
92         }
93
94         $this->title       = $this->trimmed('title');
95         $this->url         = $this->trimmed('url');
96         $this->tags        = $this->trimmed('tags');
97         $this->description = $this->trimmed('description');
98
99         return true;
100     }
101
102     /**
103      * Handler method
104      *
105      * @param array $argarray is ignored since it's now passed in in prepare()
106      *
107      * @return void
108      */
109     function handle($argarray=null)
110     {
111         parent::handle($argarray);
112
113         if ($this->isPost()) {
114             $this->newBookmark();
115         } else {
116             $this->showPage();
117         }
118
119         return;
120     }
121
122     /**
123      * Add a new bookmark
124      *
125      * @return void
126      */
127     function newBookmark()
128     {
129         try {
130             if (empty($this->title)) {
131                 // TRANS: Client exception thrown when trying to create a new bookmark without a title.
132                 throw new ClientException(_m('Bookmark must have a title.'));
133             }
134
135             if (empty($this->url)) {
136                 // TRANS: Client exception thrown when trying to create a new bookmark without a URL.
137                 throw new ClientException(_m('Bookmark must have an URL.'));
138             }
139
140             $options = array();
141
142             ToSelector::fillOptions($this, $options);
143
144             $saved = Bookmark::saveNew($this->user->getProfile(),
145                                        $this->title,
146                                        $this->url,
147                                        $this->tags,
148                                        $this->description,
149                                        $options);
150
151         } catch (ClientException $ce) {
152             if ($this->boolean('ajax')) {
153                 $this->startHTML('text/xml;charset=utf-8');
154                 $this->elementStart('head');
155                 // TRANS: Page title after an AJAX error occurs
156                 $this->element('title', null, _('Ajax Error'));
157                 $this->elementEnd('head');
158                 $this->elementStart('body');
159                 $this->element('p', array('id' => 'error'), $ce->getMessage());
160                 $this->elementEnd('body');
161                 $this->endHTML();
162                 return;
163             } else {
164                 $this->error = $ce->getMessage();
165                 $this->showPage();
166                 return;
167             }
168         }
169
170         if ($this->boolean('ajax')) {
171             $this->startHTML('text/xml;charset=utf-8');
172             $this->elementStart('head');
173             // TRANS: Page title after posting a bookmark.
174             $this->element('title', null, _m('Bookmark posted'));
175             $this->elementEnd('head');
176             $this->elementStart('body');
177             $this->showNotice($saved);
178             $this->elementEnd('body');
179             $this->endHTML();
180         } else {
181             common_redirect($saved->getUrl(), 303);
182         }
183     }
184
185     /**
186      * Output a notice
187      *
188      * Used to generate the notice code for Ajax results.
189      *
190      * @param Notice $notice Notice that was saved
191      *
192      * @return void
193      */
194     function showNotice(Notice $notice)
195     {
196         class_exists('NoticeList'); // @fixme hack for autoloader
197         $nli = new NoticeListItem($notice, $this);
198         $nli->show();
199     }
200
201     /**
202      * Show the bookmark form
203      *
204      * @return void
205      */
206     function showContent()
207     {
208         if (!empty($this->error)) {
209             $this->element('p', 'error', $this->error);
210         }
211
212         $form = new BookmarkForm($this,
213                                  $this->title,
214                                  $this->url,
215                                  $this->tags,
216                                  $this->description);
217
218         $form->show();
219
220         return;
221     }
222
223     /**
224      * Return true if read only.
225      *
226      * MAY override
227      *
228      * @param array $args other arguments
229      *
230      * @return boolean is read only action?
231      */
232     function isReadOnly($args)
233     {
234         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
235             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
236             return true;
237         } else {
238             return false;
239         }
240     }
241 }