qnashowanswer.php 4.2 KB

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