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