]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/actions/qnanewanswer.php
Merge branch 'master' into testing
[quix0rs-gnu-social.git] / plugins / QnA / actions / qnanewanswer.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Answer a question
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  QnA
24  * @package   StatusNet
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/
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  * Answer a question
38  *
39  * @category  QnA
40  * @package   StatusNet
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/
45  */
46 class QnanewanswerAction extends Action
47 {
48     protected $user     = null;
49     protected $error    = null;
50     protected $complete = null;
51
52     public    $question = null;
53     protected $content  = null;
54
55     /**
56      * Returns the title of the action
57      *
58      * @return string Action title
59      */
60     function title()
61     {
62         // TRANS: Page title for and answer to a question.
63         return _m('Answer');
64     }
65
66     /**
67      * For initializing members of the class.
68      *
69      * @param array $argarray misc. arguments
70      *
71      * @return boolean true
72      */
73     function prepare($argarray)
74     {
75         parent::prepare($argarray);
76         if ($this->boolean('ajax')) {
77             StatusNet::setApi(true);
78         }
79         common_debug("in qnanewanswer");
80         $this->user = common_current_user();
81
82         if (empty($this->user)) {
83             throw new ClientException(
84                 // TRANS: Client exception thrown trying to answer a question while not logged in.
85                 _m("You must be logged in to answer to a question."),
86                 403
87             );
88         }
89
90         if ($this->isPost()) {
91             $this->checkSessionToken();
92         }
93
94         $id = substr($this->trimmed('id'), 9);
95
96         $this->question = QnA_Question::staticGet('id', $id);
97
98         if (empty($this->question)) {
99             throw new ClientException(
100                 // TRANS: Client exception thrown trying to respond to a non-existing question.
101                 _m('Invalid or missing question.'),
102                 404
103             );
104         }
105
106         $this->answerText = $this->trimmed('answer');
107
108         return true;
109     }
110
111     /**
112      * Handler method
113      *
114      * @param array $argarray is ignored since it's now passed in in prepare()
115      *
116      * @return void
117      */
118     function handle($argarray=null)
119     {
120         parent::handle($argarray);
121
122         if ($this->isPost()) {
123             $this->newAnswer();
124         } else {
125             $this->showForm();
126         }
127
128         return;
129     }
130
131     /**
132      * Add a new answer
133      *
134      * @return void
135      */
136     function newAnswer()
137     {
138         $profile = $this->user->getProfile();
139
140         try {
141             $notice = QnA_Answer::saveNew(
142                 $profile,
143                 $this->question,
144                 $this->answerText
145             );
146         } catch (ClientException $ce) {
147             $this->error = $ce->getMessage();
148             $this->showForm($this->error);
149             return;
150         }
151         if ($this->boolean('ajax')) {
152             common_debug("ajaxy part");
153             $answer = $this->question->getAnswer($profile);
154             header('Content-Type: text/xml;charset=utf-8');
155             $this->xw->startDocument('1.0', 'UTF-8');
156
157             $this->elementStart('html');
158             $this->elementStart('head');
159             // TRANS: Page title after sending an answer.
160             $this->element('title', null, _m('Answers'));
161             $this->elementEnd('head');
162
163             $this->elementStart('body');
164
165             $nli = new NoticeAnswerListItem($notice, $this, $this->question, $answer);
166             $nli->show();
167
168             $this->elementEnd('body');
169             $this->elementEnd('html');
170         } else {
171             common_debug("not ajax");
172             common_redirect($this->question->bestUrl(), 303);
173         }
174     }
175
176     /**
177      * Show the Answer form
178      *
179      * @return void
180      */
181     function showContent()
182     {
183         if (!empty($this->error)) {
184             $this->element('p', 'error', $this->error);
185         }
186
187         $form = new QnanewanswerForm($this->question, $this);
188         $form->show();
189
190         return;
191     }
192
193     /**
194      * Return true if read only.
195      *
196      * MAY override
197      *
198      * @param array $args other arguments
199      *
200      * @return boolean is read only action?
201      */
202     function isReadOnly($args)
203     {
204         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
205             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
206             return true;
207         } else {
208             return false;
209         }
210     }
211
212     /**
213      * Show an Ajax-y error message
214      *
215      * Goes back to the browser, where it's shown in a popup.
216      *
217      * @param string $msg Message to show
218      *
219      * @return void
220      */
221     function ajaxErrorMsg($msg)
222     {
223         $this->startHTML('text/xml;charset=utf-8', true);
224         $this->elementStart('head');
225         // TRANS: Page title after an AJAX error occurs on the post answer page.
226         $this->element('title', null, _m('Ajax Error'));
227         $this->elementEnd('head');
228         $this->elementStart('body');
229         $this->element('p', array('id' => 'error'), $msg);
230         $this->elementEnd('body');
231         $this->elementEnd('html');
232     }
233
234     /**
235      * Show an Ajax-y answer form
236      *
237      * Goes back to the browser, where it's shown in a popup.
238      *
239      * @param string $msg Message to show
240      *
241      * @return void
242      */
243     function ajaxShowForm()
244     {
245         common_debug('ajaxShowForm()');
246         $this->startHTML('text/xml;charset=utf-8', true);
247         $this->elementStart('head');
248         // TRANS: Title for form to send answer to a question.
249         $this->element('title', null, _m('TITLE','Your answer'));
250         $this->elementEnd('head');
251         $this->elementStart('body');
252
253         $form = new QnanewanswerForm($this, $this->question);
254         $form->show();
255
256         $this->elementEnd('body');
257         $this->elementEnd('html');
258     }
259
260     /**
261      * @param string $msg An error message, if any
262      *
263      * @return void
264      */
265     function showForm($msg = null)
266     {
267         common_debug("show form - msg = $msg");
268         if ($this->boolean('ajax')) {
269             if ($msg) {
270                 $this->ajaxErrorMsg($msg);
271             } else {
272                 $this->ajaxShowForm();
273             }
274             return;
275         }
276
277         $this->msg = $msg;
278         $this->showPage();
279     }
280 }
281
282 class NoticeAnswerListItem extends NoticeListItem
283 {
284     protected $question;
285     protected $answer;
286
287     /**
288      * constructor
289      *
290      * Also initializes the profile attribute.
291      *
292      * @param Notice $notice The notice we'll display
293      */
294     function __construct($notice, $out=null, $question, $answer)
295     {
296         parent::__construct($notice, $out);
297         $this->question = $question;
298         $this->answer   = $answer;
299
300     }
301
302     function show()
303     {
304         if (empty($this->notice)) {
305             common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
306             return;
307         } else if (empty($this->profile)) {
308             common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
309             return;
310         }
311
312         $this->showStart();
313         $this->showNotice();
314         $this->showNoticeInfo();
315         $notice = $this->question->getNotice();
316         $this->out->hidden('inreplyto', $notice->id);
317         $this->showEnd();
318     }
319
320     /**
321      * show the content of the notice
322      *
323      * Shows the content of the notice. This is pre-rendered for efficiency
324      * at save time. Some very old notices might not be pre-rendered, so
325      * they're rendered on the spot.
326      *
327      * @return void
328      */
329     function showContent()
330     {
331         $this->out->elementStart('p', array('class' => 'entry-content answer-content'));
332         if ($this->notice->rendered) {
333             $this->out->raw($this->notice->rendered);
334         } else {
335             // XXX: may be some uncooked notices in the DB,
336             // we cook them right now. This should probably disappear in future
337             // versions (>> 0.4.x)
338             $this->out->raw(common_render_content($this->notice->content, $this->notice));
339         }
340
341         if (!empty($this->answer)) {
342             $form = new QnashowanswerForm($this->out, $this->answer);
343             $form->show();
344         } else {
345             // TRANS: Error message displayed when an answer has no content.
346             $out->text(_m('Answer data is missing.'));
347         }
348
349         $this->out->elementEnd('p');
350     }
351 }