qnashowquestion.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Show 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. * Show a question
  37. *
  38. * @category QnA
  39. * @package StatusNet
  40. * @author Zach Copley <zach@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class QnashowquestionAction extends ShownoticeAction
  46. {
  47. protected $question = null;
  48. /**
  49. * For initializing members of the class.
  50. *
  51. * @param array $args misc. arguments
  52. *
  53. * @return boolean true
  54. * @throws ClientException
  55. * @throws ServerException
  56. */
  57. function prepare(array $args = [])
  58. {
  59. Action::prepare($args);
  60. $this->id = $this->trimmed('id');
  61. $this->question = QnA_Question::getKV('id', $this->id);
  62. if (empty($this->question)) {
  63. // TRANS: Client exception thrown trying to view a non-existing question.
  64. throw new ClientException(_m('No such question.'), 404);
  65. }
  66. $this->notice = $this->question->getNotice();
  67. if (empty($this->notice)) {
  68. // Did we used to have it, and it got deleted?
  69. // TRANS: Client exception thrown trying to view a non-existing question notice.
  70. throw new ClientException(_m('No such question notice.'), 404);
  71. }
  72. $this->user = User::getKV('id', $this->question->profile_id);
  73. if (empty($this->user)) {
  74. // TRANS: Client exception thrown trying to view a question of a non-existing user.
  75. throw new ClientException(_m('No such user.'), 404);
  76. }
  77. $this->profile = $this->user->getProfile();
  78. if (empty($this->profile)) {
  79. // TRANS: Server exception thrown trying to view a question for a user for which the profile could not be loaded.
  80. throw new ServerException(_m('User without a profile.'));
  81. }
  82. try {
  83. $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
  84. } catch (Exception $e) {
  85. $this->avatar = null;
  86. }
  87. return true;
  88. }
  89. function showContent()
  90. {
  91. $this->elementStart('div', 'qna-full-question');
  92. $this->raw($this->question->asHTML());
  93. $answer = $this->question->getAnswers();
  94. $this->elementStart('div', 'qna-full-question-answers');
  95. $answerIds = array();
  96. // @fixme use a filtered stream!
  97. if (!empty($answer)) {
  98. while ($answer->fetch()) {
  99. $answerIds[] = $answer->getNotice()->id;
  100. }
  101. }
  102. if (count($answerIds) > 0) {
  103. $notice = Notice::multiGet('id', $answerIds);
  104. $nli = new NoticeList($notice, $this);
  105. $nli->show();
  106. }
  107. $user = common_current_user();
  108. if (!empty($user)) {
  109. $profile = $user->getProfile();
  110. $answer = QnA_Question::getAnswer($profile);
  111. if (empty($answer)) {
  112. $form = new QnanewanswerForm($this, $this->question, false);
  113. $form->show();
  114. }
  115. }
  116. $this->elementEnd('div');
  117. $this->elementEnd('div');
  118. }
  119. /**
  120. * Title of the page
  121. *
  122. * Used by Action class for layout.
  123. *
  124. * @return string page tile
  125. */
  126. function title()
  127. {
  128. return sprintf(
  129. // TRANS: Page title for a question.
  130. // TRANS: %1$s is the nickname of the user who asked the question, %2$s is the question.
  131. _m('%1$s\'s question: %2$s'),
  132. $this->user->nickname,
  133. $this->question->title
  134. );
  135. }
  136. /**
  137. * @fixme combine the notice time with question update time
  138. */
  139. function lastModified()
  140. {
  141. return Action::lastModified();
  142. }
  143. /**
  144. * @fixme combine the notice time with question update time
  145. */
  146. function etag()
  147. {
  148. return Action::etag();
  149. }
  150. }