deletenotice.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Class for deleting a notice
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Personal
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Sarven Capadisli <csarven@status.net>
  26. * @copyright 2008 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET') && !defined('LACONICA')) {
  31. exit(1);
  32. }
  33. // @todo FIXME: documentation needed.
  34. class DeletenoticeAction extends Action
  35. {
  36. var $error = null;
  37. var $user = null;
  38. var $notice = null;
  39. var $profile = null;
  40. var $user_profile = null;
  41. function prepare($args)
  42. {
  43. parent::prepare($args);
  44. $this->user = common_current_user();
  45. if (!$this->user) {
  46. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  47. common_user_error(_('Not logged in.'));
  48. exit;
  49. }
  50. $notice_id = $this->trimmed('notice');
  51. $this->notice = Notice::getKV($notice_id);
  52. if (!$this->notice) {
  53. // TRANS: Error message displayed trying to delete a non-existing notice.
  54. common_user_error(_('No such notice.'));
  55. exit;
  56. }
  57. $this->profile = $this->notice->getProfile();
  58. $this->user_profile = $this->user->getProfile();
  59. return true;
  60. }
  61. function handle($args)
  62. {
  63. parent::handle($args);
  64. if ($this->notice->profile_id != $this->user_profile->id &&
  65. !$this->user->hasRight(Right::DELETEOTHERSNOTICE)) {
  66. // TRANS: Error message displayed trying to delete a notice that was not made by the current user.
  67. common_user_error(_('Cannot delete this notice.'));
  68. exit;
  69. }
  70. // XXX: Ajax!
  71. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  72. $this->deleteNotice();
  73. } else if ($_SERVER['REQUEST_METHOD'] == 'GET') {
  74. $this->showForm();
  75. }
  76. }
  77. /**
  78. * Show the page notice
  79. *
  80. * Shows instructions for the page
  81. *
  82. * @return void
  83. */
  84. function showPageNotice()
  85. {
  86. $instr = $this->getInstructions();
  87. $output = common_markup_to_html($instr);
  88. $this->elementStart('div', 'instructions');
  89. $this->raw($output);
  90. $this->elementEnd('div');
  91. }
  92. function getInstructions()
  93. {
  94. // TRANS: Instructions for deleting a notice.
  95. return _('You are about to permanently delete a notice. ' .
  96. 'Once this is done, it cannot be undone.');
  97. }
  98. function title()
  99. {
  100. // TRANS: Page title when deleting a notice.
  101. return _('Delete notice');
  102. }
  103. /**
  104. * Wrapper for showing a page
  105. *
  106. * Stores an error and shows the page
  107. *
  108. * @param string $error Error, if any
  109. *
  110. * @return void
  111. */
  112. function showForm($error = null)
  113. {
  114. $this->error = $error;
  115. $this->showPage();
  116. }
  117. /**
  118. * Insert delete notice form into the content
  119. *
  120. * @return void
  121. */
  122. function showContent()
  123. {
  124. $this->elementStart('form', array('id' => 'form_notice_delete',
  125. 'class' => 'form_settings',
  126. 'method' => 'post',
  127. 'action' => common_local_url('deletenotice')));
  128. $this->elementStart('fieldset');
  129. // TRANS: Fieldset legend for the delete notice form.
  130. $this->element('legend', null, _('Delete notice'));
  131. $this->hidden('token', common_session_token());
  132. $this->hidden('notice', $this->trimmed('notice'));
  133. // TRANS: Message for the delete notice form.
  134. $this->element('p', null, _('Are you sure you want to delete this notice?'));
  135. $this->submit('form_action-no',
  136. // TRANS: Button label on the delete notice form.
  137. _m('BUTTON','No'),
  138. 'submit form_action-primary',
  139. 'no',
  140. // TRANS: Submit button title for 'No' when deleting a notice.
  141. _('Do not delete this notice.'));
  142. $this->submit('form_action-yes',
  143. // TRANS: Button label on the delete notice form.
  144. _m('BUTTON','Yes'),
  145. 'submit form_action-secondary',
  146. 'yes',
  147. // TRANS: Submit button title for 'Yes' when deleting a notice.
  148. _('Delete this notice.'));
  149. $this->elementEnd('fieldset');
  150. $this->elementEnd('form');
  151. }
  152. function deleteNotice()
  153. {
  154. // CSRF protection
  155. $token = $this->trimmed('token');
  156. if (!$token || $token != common_session_token()) {
  157. // TRANS: Client error displayed when the session token does not match or is not given.
  158. $this->showForm(_('There was a problem with your session token. ' .
  159. 'Try again, please.'));
  160. return;
  161. }
  162. if ($this->arg('yes')) {
  163. if (Event::handle('StartDeleteOwnNotice', array($this->user, $this->notice))) {
  164. $this->notice->delete();
  165. Event::handle('EndDeleteOwnNotice', array($this->user, $this->notice));
  166. }
  167. }
  168. $url = common_get_returnto();
  169. if ($url) {
  170. common_set_returnto(null);
  171. } else {
  172. $url = common_local_url('public');
  173. }
  174. common_redirect($url, 303);
  175. }
  176. }