clearflag.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Clear all flags for a profile
  18. *
  19. * @category Action
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2009 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Action to clear flags for a profile
  28. *
  29. * @category Action
  30. * @package GNUsocial
  31. * @author Evan Prodromou <evan@status.net>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class ClearflagAction extends ProfileFormAction
  35. {
  36. /**
  37. * Take arguments for running
  38. *
  39. * @param array $args $_REQUEST args
  40. *
  41. * @return boolean success flag
  42. */
  43. public function prepare(array $args = [])
  44. {
  45. if (!parent::prepare($args)) {
  46. return false;
  47. }
  48. $user = common_current_user();
  49. assert(!empty($user)); // checked above
  50. assert(!empty($this->profile)); // checked above
  51. return true;
  52. }
  53. /**
  54. * Handle request
  55. *
  56. * Overriding the base Action's handle() here to deal check
  57. * for Ajax and return an HXR response if necessary
  58. *
  59. * @param array $args $_REQUEST args; handled in prepare()
  60. *
  61. * @return void
  62. */
  63. public function handle()
  64. {
  65. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  66. $this->handlePost();
  67. if (!$this->boolean('ajax')) {
  68. $this->returnToPrevious();
  69. }
  70. }
  71. }
  72. /**
  73. * Handle POST
  74. *
  75. * Executes the actions; deletes all flags
  76. *
  77. * @return void
  78. */
  79. public function handlePost()
  80. {
  81. $ufp = new User_flag_profile();
  82. $result = $ufp->query('UPDATE user_flag_profile ' .
  83. 'SET cleared = CURRENT_TIMESTAMP ' .
  84. 'WHERE cleared IS NULL ' .
  85. 'AND profile_id = ' . $this->profile->id);
  86. if ($result === false) {
  87. // TRANS: Server exception given when flags could not be cleared.
  88. // TRANS: %s is a profile nickname.
  89. $msg = sprintf(
  90. _m('Could not clear flags for profile "%s".'),
  91. $this->profile->nickname
  92. );
  93. throw new ServerException($msg);
  94. }
  95. $ufp->free();
  96. if ($this->boolean('ajax')) {
  97. $this->ajaxResults();
  98. }
  99. }
  100. /**
  101. * Return results in ajax form
  102. *
  103. * @return void
  104. */
  105. public function ajaxResults()
  106. {
  107. $this->startHTML('text/xml;charset=utf-8');
  108. $this->elementStart('head');
  109. // TRANS: Title for AJAX form to indicated that flags were removed.
  110. $this->element('title', null, _m('Flags cleared'));
  111. $this->elementEnd('head');
  112. $this->elementStart('body');
  113. // TRANS: Body element for "flags cleared" form.
  114. $this->element('p', 'cleared', _m('Cleared'));
  115. $this->elementEnd('body');
  116. $this->endHTML();
  117. }
  118. }