]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Bookmark/newbookmark.php
8c60fc159681b9da422343aacc71e166956d62fd
[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         $this->user = common_current_user();
79
80         if (empty($this->user)) {
81             // TRANS: Client exception thrown when trying to create a new bookmark while not logged in.
82             throw new ClientException(_m('Must be logged in to post a bookmark.'),
83                                       403);
84         }
85
86         if ($this->isPost()) {
87             $this->checkSessionToken();
88         }
89
90         $this->title       = $this->trimmed('bookmark-title');
91         $this->url         = $this->trimmed('bookmark-url');
92         $this->tags        = $this->trimmed('bookmark-tags');
93         $this->description = $this->trimmed('bookmark-description');
94
95         return true;
96     }
97
98     /**
99      * Handler method
100      *
101      * @param array $argarray is ignored since it's now passed in in prepare()
102      *
103      * @return void
104      */
105     function handle($argarray=null)
106     {
107         parent::handle($argarray);
108
109         if ($this->isPost()) {
110             $this->newBookmark();
111         } else {
112             $this->showPage();
113         }
114
115         return;
116     }
117
118     /**
119      * Add a new bookmark
120      *
121      * @return void
122      */
123     function newBookmark()
124     {
125         if ($this->boolean('ajax')) {
126             StatusNet::setApi(true);
127         }
128         try {
129             if (empty($this->title)) {
130                 // TRANS: Client exception thrown when trying to create a new bookmark without a title.
131                 throw new ClientException(_m('Bookmark must have a title.'));
132             }
133
134             if (empty($this->url)) {
135                 // TRANS: Client exception thrown when trying to create a new bookmark without a URL.
136                 throw new ClientException(_m('Bookmark must have an URL.'));
137             }
138
139             $options = array();
140
141             ToSelector::fillOptions($this, $options);
142
143             $saved = Bookmark::saveNew($this->user->getProfile(),
144                                        $this->title,
145                                        $this->url,
146                                        $this->tags,
147                                        $this->description,
148                                        $options);
149
150         } catch (ClientException $ce) {
151             $this->error = $ce->getMessage();
152             $this->showPage();
153             return;
154         }
155
156         if ($this->boolean('ajax')) {
157             header('Content-Type: text/xml;charset=utf-8');
158             $this->xw->startDocument('1.0', 'UTF-8');
159             $this->elementStart('html');
160             $this->elementStart('head');
161             // TRANS: Page title after posting a bookmark.
162             $this->element('title', null, _m('Bookmark posted'));
163             $this->elementEnd('head');
164             $this->elementStart('body');
165             $this->showNotice($saved);
166             $this->elementEnd('body');
167             $this->elementEnd('html');
168         } else {
169             common_redirect($saved->bestUrl(), 303);
170         }
171     }
172
173     /**
174      * Output a notice
175      *
176      * Used to generate the notice code for Ajax results.
177      *
178      * @param Notice $notice Notice that was saved
179      *
180      * @return void
181      */
182     function showNotice($notice)
183     {
184         class_exists('NoticeList'); // @fixme hack for autoloader
185         $nli = new NoticeListItem($notice, $this);
186         $nli->show();
187     }
188
189     /**
190      * Show the bookmark form
191      *
192      * @return void
193      */
194     function showContent()
195     {
196         if (!empty($this->error)) {
197             $this->element('p', 'error', $this->error);
198         }
199
200         $form = new BookmarkForm($this,
201                                  $this->title,
202                                  $this->url,
203                                  $this->tags,
204                                  $this->description);
205
206         $form->show();
207
208         return;
209     }
210
211     /**
212      * Return true if read only.
213      *
214      * MAY override
215      *
216      * @param array $args other arguments
217      *
218      * @return boolean is read only action?
219      */
220     function isReadOnly($args)
221     {
222         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
223             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
224             return true;
225         } else {
226             return false;
227         }
228     }
229 }