]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/newbookmark.php
57be783b3805003ad8d4ba8941ded6ae75bc77c2
[quix0rs-gnu-social.git] / plugins / Bookmark / 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             StatusNet::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                 header('Content-Type: text/xml;charset=utf-8');
154                 $this->xw->startDocument('1.0', 'UTF-8');
155                 $this->elementStart('html');
156                 $this->elementStart('head');
157                 // TRANS: Page title after an AJAX error occurs
158                 $this->element('title', null, _('Ajax Error'));
159                 $this->elementEnd('head');
160                 $this->elementStart('body');
161                 $this->element('p', array('id' => 'error'), $ce->getMessage());
162                 $this->elementEnd('body');
163                 $this->elementEnd('html');
164                 return;
165             } else {
166                 $this->error = $ce->getMessage();
167                 $this->showPage();
168                 return;
169             }
170         }
171
172         if ($this->boolean('ajax')) {
173             header('Content-Type: text/xml;charset=utf-8');
174             $this->xw->startDocument('1.0', 'UTF-8');
175             $this->elementStart('html');
176             $this->elementStart('head');
177             // TRANS: Page title after posting a bookmark.
178             $this->element('title', null, _m('Bookmark posted'));
179             $this->elementEnd('head');
180             $this->elementStart('body');
181             $this->showNotice($saved);
182             $this->elementEnd('body');
183             $this->elementEnd('html');
184         } else {
185             common_redirect($saved->bestUrl(), 303);
186         }
187     }
188
189     /**
190      * Output a notice
191      *
192      * Used to generate the notice code for Ajax results.
193      *
194      * @param Notice $notice Notice that was saved
195      *
196      * @return void
197      */
198     function showNotice($notice)
199     {
200         class_exists('NoticeList'); // @fixme hack for autoloader
201         $nli = new NoticeListItem($notice, $this);
202         $nli->show();
203     }
204
205     /**
206      * Show the bookmark form
207      *
208      * @return void
209      */
210     function showContent()
211     {
212         if (!empty($this->error)) {
213             $this->element('p', 'error', $this->error);
214         }
215
216         $form = new BookmarkForm($this,
217                                  $this->title,
218                                  $this->url,
219                                  $this->tags,
220                                  $this->description);
221
222         $form->show();
223
224         return;
225     }
226
227     /**
228      * Return true if read only.
229      *
230      * MAY override
231      *
232      * @param array $args other arguments
233      *
234      * @return boolean is read only action?
235      */
236     function isReadOnly($args)
237     {
238         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
239             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
240             return true;
241         } else {
242             return false;
243         }
244     }
245 }