3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, 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 Zach Copley <zach@status.net>
26 * @copyright 2011 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 Zach Copley <zach@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 QnareviseanswerAction extends Action
48 protected $user = null;
49 protected $error = null;
50 protected $question = null;
51 protected $answer = null;
52 protected $content = null;
55 * Returns the title of the action
57 * @return string Action title
61 // TRANS: Page title for revising a question
62 return _m('Revise answer');
66 * For initializing members of the class.
68 * @param array $argarray misc. arguments
70 * @return boolean true
72 function prepare($argarray)
74 parent::prepare($argarray);
75 if ($this->boolean('ajax')) {
76 StatusNet::setApi(true);
79 $this->user = common_current_user();
81 if (empty($this->user)) {
82 throw new ClientException(
83 // TRANS: Client exception thrown trying to answer a question while not logged in.
84 _m("You must be logged in to answer to a question."),
89 $id = substr($this->trimmed('id'), 7);
91 $this->answer = QnA_Answer::staticGet('id', $id);
92 $this->question = $this->answer->getQuestion();
94 if (empty($this->answer) || empty($this->question)) {
95 throw new ClientException(
96 // TRANS: Client exception thrown trying to respond to a non-existing question.
97 _m('Invalid or missing answer.'),
102 $this->answerText = $this->trimmed('answer');
110 * @param array $argarray is ignored since it's now passed in in prepare()
114 function handle($argarray=null)
116 parent::handle($argarray);
118 if ($this->isPost()) {
119 $this->checkSessionToken();
120 if ($this->arg('revise')) {
121 $this->showContent();
123 } else if ($this->arg('best')) {
124 if ($this->user->id == $this->question->profile_id) {
129 $this->reviseAnswer();
142 function reviseAnswer()
144 $answer = $this->answer;
147 $orig = clone($answer);
148 $answer->content = $this->answerText;
149 $answer->revisions++;
150 $result = $answer->update($orig);
151 } catch (ClientException $ce) {
152 $this->error = $ce->getMessage();
156 if ($this->boolean('ajax')) {
157 common_debug("ajaxy part");
158 header('Content-Type: text/xml;charset=utf-8');
159 $this->xw->startDocument('1.0', 'UTF-8');
160 $this->elementStart('html');
161 $this->elementStart('head');
162 // TRANS: Page title after sending an answer.
163 $this->element('title', null, _m('Answer'));
164 $this->elementEnd('head');
165 $this->elementStart('body');
166 $form = new QnashowanswerForm($this, $answer);
168 $this->elementEnd('body');
169 $this->elementEnd('html');
171 common_redirect($this->answer->bestUrl(), 303);
176 * Mark the answer as the "best" answer
182 $question = $this->question;
183 $answer = $this->answer;
186 // close the question to further answers
187 $orig = clone($question);
188 $question->closed = 1;
189 $result = $question->update($orig);
191 // mark this answer an the best answer
192 $orig = clone($answer);
194 $result = $answer->update($orig);
195 } catch (ClientException $ce) {
196 $this->error = $ce->getMessage();
200 if ($this->boolean('ajax')) {
201 common_debug("ajaxy part");
202 header('Content-Type: text/xml;charset=utf-8');
203 $this->xw->startDocument('1.0', 'UTF-8');
204 $this->elementStart('html');
205 $this->elementStart('head');
206 // TRANS: Page title after sending an answer.
207 $this->element('title', null, _m('Answer'));
208 $this->elementEnd('head');
209 $this->elementStart('body');
210 $form = new QnashowanswerForm($this, $answer);
212 $this->elementEnd('body');
213 $this->elementEnd('html');
215 common_redirect($this->answer->bestUrl(), 303);
220 * Show the revise answer form
224 function showContent()
226 if (!empty($this->error)) {
227 $this->element('p', 'error', $this->error);
230 if ($this->boolean('ajax')) {
231 $this->showAjaxReviseForm();
233 $form = new QnareviseanswerForm($this->answer, $this);
240 function showAjaxReviseForm()
242 header('Content-Type: text/xml;charset=utf-8');
243 $this->xw->startDocument('1.0', 'UTF-8');
244 $this->elementStart('html');
245 $this->elementStart('head');
246 // TRANS: Form title for sending an answer.
247 $this->element('title', null, _m('TITLE','Answer'));
248 $this->elementEnd('head');
249 $this->elementStart('body');
250 $form = new QnareviseanswerForm($this->answer, $this);
252 $this->elementEnd('body');
253 $this->elementEnd('html');
257 * Return true if read only.
261 * @param array $args other arguments
263 * @return boolean is read only action?
265 function isReadOnly($args)
267 if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
268 $_SERVER['REQUEST_METHOD'] == 'HEAD') {