]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/actions/qnareviseanswer.php
Merge branch '1.0.x' of git://gitorious.org/statusnet/mainline
[quix0rs-gnu-social.git] / plugins / QnA / actions / qnareviseanswer.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Revise an answer
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  * Revise an answer
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 QnareviseanswerAction extends Action
47 {
48     protected $user     = null;
49     protected $error    = null;
50     protected $question = null;
51     protected $answer   = null;
52     protected $content  = null;
53
54     /**
55      * Returns the title of the action
56      *
57      * @return string Action title
58      */
59     function title()
60     {
61         // TRANS: Page title for revising a question
62         return _m('Revise answer');
63     }
64
65     /**
66      * For initializing members of the class.
67      *
68      * @param array $argarray misc. arguments
69      *
70      * @return boolean true
71      */
72     function prepare($argarray)
73     {
74         parent::prepare($argarray);
75         if ($this->boolean('ajax')) {
76             StatusNet::setApi(true);
77         }
78
79         $this->user = common_current_user();
80
81         if (empty($this->user)) {
82             // TRANS: Client exception thrown trying to answer a question while not logged in.
83             throw new ClientException(
84                 _m("You must be logged in to answer to a question."),
85                 403
86             );
87         }
88
89         $id = substr($this->trimmed('id'), 7);
90
91         $this->answer   = QnA_Answer::staticGet('id', $id);
92         $this->question = $this->answer->getQuestion();
93
94         if (empty($this->answer) || empty($this->question)) {
95             // TRANS: Client exception thrown trying to respond to a non-existing question.
96             throw new ClientException(
97                 _m('Invalid or missing answer.'),
98                 404
99             );
100         }
101
102         $this->answerText = $this->trimmed('answer');
103
104         return true;
105     }
106
107     /**
108      * Handler method
109      *
110      * @param array $argarray is ignored since it's now passed in in prepare()
111      *
112      * @return void
113      */
114     function handle($argarray=null)
115     {
116         parent::handle($argarray);
117
118         if ($this->isPost()) {
119             $this->checkSessionToken();
120             if ($this->arg('revise')) {
121                 $this->showContent();
122                 return;
123             } else if ($this->arg('best')) {
124                 if ($this->user->id == $this->question->profile_id) {
125                     $this->markBest();
126                     return;
127                 }
128             } else {
129                 $this->reviseAnswer();
130                 return;
131             }
132         }
133
134         $this->showPage();
135     }
136
137     /**
138      * Revise the answer
139      *
140      * @return void
141      */
142     function reviseAnswer()
143     {
144         $answer = $this->answer;
145
146         try {
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();
153             $this->showPage();
154             return;
155         }
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);
167             $form->show();
168             $this->elementEnd('body');
169             $this->elementEnd('html');
170         } else {
171             common_redirect($this->answer->bestUrl(), 303);
172         }
173     }
174
175     /**
176      * Mark the answer as the "best" answer
177      *
178      * @return void
179      */
180     function markBest()
181     {
182         $question = $this->question;
183         $answer   = $this->answer;
184
185         try {
186             // close the question to further answers
187             $orig = clone($question);
188             $question->closed = 1;
189             $result = $question->update($orig);
190
191             // mark this answer an the best answer
192             $orig = clone($answer);
193             $answer->best = 1;
194             $result = $answer->update($orig);
195         } catch (ClientException $ce) {
196             $this->error = $ce->getMessage();
197             $this->showPage();
198             return;
199         }
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);
211             $form->show();
212             $this->elementEnd('body');
213             $this->elementEnd('html');
214         } else {
215             common_redirect($this->answer->bestUrl(), 303);
216         }
217     }
218
219     /**
220      * Show the revise answer form
221      *
222      * @return void
223      */
224     function showContent()
225     {
226         if (!empty($this->error)) {
227             $this->element('p', 'error', $this->error);
228         }
229
230         if ($this->boolean('ajax')) {
231             $this->showAjaxReviseForm();
232         } else {
233             $form = new QnareviseanswerForm($this->answer, $this);
234             $form->show();
235         }
236
237         return;
238     }
239
240     function showAjaxReviseForm()
241     {
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         $this->element('title', null, _m('Answer'));
247         $this->elementEnd('head');
248         $this->elementStart('body');
249         $form = new QnareviseanswerForm($this->answer, $this);
250         $form->show();
251         $this->elementEnd('body');
252         $this->elementEnd('html');
253     }
254
255     /**
256      * Return true if read only.
257      *
258      * MAY override
259      *
260      * @param array $args other arguments
261      *
262      * @return boolean is read only action?
263      */
264     function isReadOnly($args)
265     {
266         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
267             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
268             return true;
269         } else {
270             return false;
271         }
272     }
273 }