3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
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.
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.
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/>.
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/
30 if (!defined('STATUSNET')) {
31 // This check helps protect against security problems;
32 // your code file can't be executed directly from the web.
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/
46 class NewbookmarkAction extends Action
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;
57 * Returns the title of the action
59 * @return string Action title
63 // TRANS: Title for action to create a new bookmark.
64 return _m('New bookmark');
68 * For initializing members of the class.
70 * @param array $argarray misc. arguments
72 * @return boolean true
74 function prepare($argarray)
76 parent::prepare($argarray);
78 if ($this->boolean('ajax')) {
79 GNUsocial::setApi(true);
82 $this->user = common_current_user();
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.'),
90 if ($this->isPost()) {
91 $this->checkSessionToken();
94 $this->title = $this->trimmed('title');
95 $this->url = $this->trimmed('url');
96 $this->tags = preg_split('/[\s,]+/', $this->trimmed('tags'), null, PREG_SPLIT_NO_EMPTY);
97 $this->description = $this->trimmed('description');
105 * @param array $argarray is ignored since it's now passed in in prepare()
109 function handle($argarray=null)
111 parent::handle($argarray);
113 if ($this->isPost()) {
114 $this->newBookmark();
127 function newBookmark()
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.'));
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.'));
142 ToSelector::fillOptions($this, $options);
144 $saved = Bookmark::saveNew($this->user->getProfile(),
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');
164 $this->error = $ce->getMessage();
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');
181 common_redirect($saved->getUrl(), 303);
188 * Used to generate the notice code for Ajax results.
190 * @param Notice $notice Notice that was saved
194 function showNotice(Notice $notice)
196 class_exists('NoticeList'); // @fixme hack for autoloader
197 $nli = new NoticeListItem($notice, $this);
202 * Show the bookmark form
206 function showContent()
208 if (!empty($this->error)) {
209 $this->element('p', 'error', $this->error);
212 $form = new BookmarkForm($this,
224 * Return true if read only.
228 * @param array $args other arguments
230 * @return boolean is read only action?
232 function isReadOnly($args)
234 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
235 $_SERVER['REQUEST_METHOD'] == 'HEAD') {