qnashowanswer.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Show an answer to 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 2010 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 an answer to a question, and associated data
  37. *
  38. * @category QnA
  39. * @package StatusNet
  40. * @author Zach Copley <zach@status.net>
  41. * @copyright 2010 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 QnashowanswerAction extends ShownoticeAction
  46. {
  47. protected $answer = 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->answer = QnA_Answer::getKV('id', $this->id);
  62. if (empty($this->answer)) {
  63. // TRANS: Client exception thrown when requesting a non-existing answer.
  64. throw new ClientException(_m('No such answer.'), 404);
  65. }
  66. $this->question = $this->answer->getQuestion();
  67. if (empty($this->question)) {
  68. // TRANS: Client exception thrown when requesting an answer that has no connected question.
  69. throw new ClientException(_m('No question for this answer.'), 404);
  70. }
  71. $this->notice = Notice::getKV('uri', $this->answer->uri);
  72. if (empty($this->notice)) {
  73. // TRANS: Did we used to have it, and it got deleted?
  74. throw new ClientException(_m('No such answer.'), 404);
  75. }
  76. $this->user = User::getKV('id', $this->answer->profile_id);
  77. if (empty($this->user)) {
  78. // TRANS: Client exception thrown when requesting answer data for a non-existing user.
  79. throw new ClientException(_m('No such user.'), 404);
  80. }
  81. $this->profile = $this->user->getProfile();
  82. if (empty($this->profile)) {
  83. // TRANS: Client exception thrown when requesting answer data for a user without a profile.
  84. throw new ServerException(_m('User without a profile.'));
  85. }
  86. try {
  87. $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
  88. } catch (Exception $e) {
  89. $this->avatar = null;
  90. }
  91. return true;
  92. }
  93. /**
  94. * Title of the page
  95. *
  96. * Used by Action class for layout.
  97. *
  98. * @return string page tile
  99. */
  100. function title()
  101. {
  102. $question = $this->answer->getQuestion();
  103. return sprintf(
  104. // TRANS: Page title.
  105. // TRANS: %1$s is the user who answered a question, %2$s is the question.
  106. _m('%1$s\'s answer to "%2$s"'),
  107. $this->user->nickname,
  108. $question->title
  109. );
  110. }
  111. /**
  112. * Overload page title display to show answer link
  113. *
  114. * @return void
  115. */
  116. function showPageTitle()
  117. {
  118. $this->elementStart('h1');
  119. $this->element(
  120. 'a',
  121. array('href' => $this->answer->uri),
  122. $this->question->title
  123. );
  124. $this->elementEnd('h1');
  125. }
  126. function showContent()
  127. {
  128. $this->raw($this->answer->asHTML());
  129. }
  130. }