qnashowquestion.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 $argarray misc. arguments
  52. *
  53. * @return boolean true
  54. */
  55. function prepare($argarray)
  56. {
  57. Action::prepare($argarray);
  58. $this->id = $this->trimmed('id');
  59. $this->question = QnA_Question::getKV('id', $this->id);
  60. if (empty($this->question)) {
  61. // TRANS: Client exception thrown trying to view a non-existing question.
  62. throw new ClientException(_m('No such question.'), 404);
  63. }
  64. $this->notice = $this->question->getNotice();
  65. if (empty($this->notice)) {
  66. // Did we used to have it, and it got deleted?
  67. // TRANS: Client exception thrown trying to view a non-existing question notice.
  68. throw new ClientException(_m('No such question notice.'), 404);
  69. }
  70. $this->user = User::getKV('id', $this->question->profile_id);
  71. if (empty($this->user)) {
  72. // TRANS: Client exception thrown trying to view a question of a non-existing user.
  73. throw new ClientException(_m('No such user.'), 404);
  74. }
  75. $this->profile = $this->user->getProfile();
  76. if (empty($this->profile)) {
  77. // TRANS: Server exception thrown trying to view a question for a user for which the profile could not be loaded.
  78. throw new ServerException(_m('User without a profile.'));
  79. }
  80. try {
  81. $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
  82. } catch (Exception $e) {
  83. $this->avatar = null;
  84. }
  85. return true;
  86. }
  87. function showContent()
  88. {
  89. $this->elementStart('div', 'qna-full-question');
  90. $this->raw($this->question->asHTML());
  91. $answer = $this->question->getAnswers();
  92. $this->elementStart('div', 'qna-full-question-answers');
  93. $answerIds = array();
  94. // @fixme use a filtered stream!
  95. if (!empty($answer)) {
  96. while ($answer->fetch()) {
  97. $answerIds[] = $answer->getNotice()->id;
  98. }
  99. }
  100. if (count($answerIds) > 0) {
  101. $notice = Notice::multiGet('id', $answerIds);
  102. $nli = new NoticeList($notice, $this);
  103. $nli->show();
  104. }
  105. $user = common_current_user();
  106. if (!empty($user)) {
  107. $profile = $user->getProfile();
  108. $answer = QnA_Question::getAnswer($profile);
  109. if (empty($answer)) {
  110. $form = new QnanewanswerForm($this, $this->question, false);
  111. $form->show();
  112. }
  113. }
  114. $this->elementEnd('div');
  115. $this->elementEnd('div');
  116. }
  117. /**
  118. * Title of the page
  119. *
  120. * Used by Action class for layout.
  121. *
  122. * @return string page tile
  123. */
  124. function title()
  125. {
  126. return sprintf(
  127. // TRANS: Page title for a question.
  128. // TRANS: %1$s is the nickname of the user who asked the question, %2$s is the question.
  129. _m('%1$s\'s question: %2$s'),
  130. $this->user->nickname,
  131. $this->question->title
  132. );
  133. }
  134. /**
  135. * @fixme combine the notice time with question update time
  136. */
  137. function lastModified()
  138. {
  139. return Action::lastModified();
  140. }
  141. /**
  142. * @fixme combine the notice time with question update time
  143. */
  144. function etag()
  145. {
  146. return Action::etag();
  147. }
  148. }