123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- <?php
- if (!defined('STATUSNET')) {
-
-
- exit(1);
- }
- class QnanewanswerAction extends Action
- {
- protected $user = null;
- protected $error = null;
- protected $complete = null;
- public $question = null;
- protected $content = null;
-
- function title()
- {
-
- return _m('Answer');
- }
-
- function prepare($argarray)
- {
- parent::prepare($argarray);
- if ($this->boolean('ajax')) {
- GNUsocial::setApi(true);
- }
- common_debug("in qnanewanswer");
- $this->user = common_current_user();
- if (empty($this->user)) {
- throw new ClientException(
-
- _m("You must be logged in to answer to a question."),
- 403
- );
- }
- if ($this->isPost()) {
- $this->checkSessionToken();
- }
- $id = substr($this->trimmed('id'), 9);
- $this->question = QnA_Question::getKV('id', $id);
- if (empty($this->question)) {
- throw new ClientException(
-
- _m('Invalid or missing question.'),
- 404
- );
- }
- $this->answerText = $this->trimmed('answer');
- return true;
- }
-
- function handle($argarray=null)
- {
- parent::handle($argarray);
- if ($this->isPost()) {
- $this->newAnswer();
- } else {
- $this->showForm();
- }
- return;
- }
-
- function newAnswer()
- {
- $profile = $this->user->getProfile();
- try {
- $notice = QnA_Answer::saveNew(
- $profile,
- $this->question,
- $this->answerText
- );
- } catch (ClientException $ce) {
- $this->error = $ce->getMessage();
- $this->showForm($this->error);
- return;
- }
- if ($this->boolean('ajax')) {
- common_debug("ajaxy part");
- $answer = $this->question->getAnswer($profile);
- $this->startHTML('text/xml;charset=utf-8');
- $this->elementStart('head');
-
- $this->element('title', null, _m('Answers'));
- $this->elementEnd('head');
- $this->elementStart('body');
- $nli = new NoticeAnswerListItem($notice, $this, $this->question, $answer);
- $nli->show();
- $this->elementEnd('body');
- $this->endHTML();
- } else {
- common_debug("not ajax");
- common_redirect($this->question->getUrl(), 303);
- }
- }
-
- function showContent()
- {
- if (!empty($this->error)) {
- $this->element('p', 'error', $this->error);
- }
- $form = new QnanewanswerForm($this->question, $this);
- $form->show();
- return;
- }
-
- function isReadOnly($args)
- {
- if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
- $_SERVER['REQUEST_METHOD'] == 'HEAD') {
- return true;
- } else {
- return false;
- }
- }
-
- function ajaxErrorMsg($msg)
- {
- $this->startHTML('text/xml;charset=utf-8', true);
- $this->elementStart('head');
-
- $this->element('title', null, _m('Ajax Error'));
- $this->elementEnd('head');
- $this->elementStart('body');
- $this->element('p', array('id' => 'error'), $msg);
- $this->elementEnd('body');
- $this->endHTML();
- }
-
- function ajaxShowForm()
- {
- common_debug('ajaxShowForm()');
- $this->startHTML('text/xml;charset=utf-8', true);
- $this->elementStart('head');
-
- $this->element('title', null, _m('TITLE','Your answer'));
- $this->elementEnd('head');
- $this->elementStart('body');
- $form = new QnanewanswerForm($this, $this->question);
- $form->show();
- $this->elementEnd('body');
- $this->endHTML();
- }
-
- function showForm($msg = null)
- {
- common_debug("show form - msg = $msg");
- if ($this->boolean('ajax')) {
- if ($msg) {
- $this->ajaxErrorMsg($msg);
- } else {
- $this->ajaxShowForm();
- }
- return;
- }
- $this->msg = $msg;
- $this->showPage();
- }
- }
- class NoticeAnswerListItem extends NoticeListItem
- {
- protected $question;
- protected $answer;
-
- function __construct($notice, $out=null, $question, $answer)
- {
- parent::__construct($notice, $out);
- $this->question = $question;
- $this->answer = $answer;
- }
- function show()
- {
- if (empty($this->notice)) {
- common_log(LOG_WARNING, "Trying to show missing notice; skipping.");
- return;
- } else if (empty($this->profile)) {
- common_log(LOG_WARNING, "Trying to show missing profile (" . $this->notice->profile_id . "); skipping.");
- return;
- }
- $this->showStart();
- $this->showNotice();
- $this->showNoticeInfo();
- $notice = $this->question->getNotice();
- $this->out->hidden('inreplyto', $notice->id);
- $this->showEnd();
- }
-
- function showContent()
- {
- $this->out->elementStart('p', array('class' => 'e-content answer-content'));
- $this->out->raw($this->notice->getRendered());
- if (!empty($this->answer)) {
- $form = new QnashowanswerForm($this->out, $this->answer);
- $form->show();
- } else {
-
- $out->text(_m('Answer data is missing.'));
- }
- $this->out->elementEnd('p');
- }
- }
|