clearflag.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. *
  22. * @author Evan Prodromou <evan@status.net>
  23. * @copyright 2009 StatusNet, Inc.
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. /**
  28. * Action to clear flags for a profile
  29. *
  30. * @category Action
  31. * @package GNUsocial
  32. *
  33. * @author Evan Prodromou <evan@status.net>
  34. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  35. */
  36. class ClearflagAction extends ProfileFormAction
  37. {
  38. /**
  39. * Take arguments for running
  40. *
  41. * @param array $args $_REQUEST args
  42. *
  43. * @return bool success flag
  44. */
  45. public function prepare(array $args = [])
  46. {
  47. if (!parent::prepare($args)) {
  48. return false;
  49. }
  50. $user = common_current_user();
  51. assert(!empty($user)); // checked above
  52. assert(!empty($this->profile)); // checked above
  53. return true;
  54. }
  55. /**
  56. * Handle request
  57. *
  58. * Overriding the base Action's handle() here to deal check
  59. * for Ajax and return an HXR response if necessary
  60. *
  61. * @param array $args $_REQUEST args; handled in prepare()
  62. *
  63. * @return void
  64. */
  65. public function handle()
  66. {
  67. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  68. $this->handlePost();
  69. if (!$this->boolean('ajax')) {
  70. $this->returnToPrevious();
  71. }
  72. }
  73. }
  74. /**
  75. * Handle POST
  76. *
  77. * Executes the actions; deletes all flags
  78. *
  79. * @return void
  80. */
  81. public function handlePost()
  82. {
  83. $ufp = new User_flag_profile();
  84. $result = $ufp->query(sprintf(
  85. <<<'END'
  86. UPDATE user_flag_profile
  87. SET cleared = CURRENT_TIMESTAMP, modified = cleared
  88. WHERE cleared IS NULL AND profile_id = %d
  89. END,
  90. $this->profile->getID()
  91. ));
  92. if ($result === false) {
  93. // TRANS: Server exception given when flags could not be cleared.
  94. // TRANS: %s is a profile nickname.
  95. $msg = sprintf(
  96. _m('Could not clear flags for profile "%s".'),
  97. $this->profile->nickname
  98. );
  99. throw new ServerException($msg);
  100. }
  101. $ufp->free();
  102. if ($this->boolean('ajax')) {
  103. $this->ajaxResults();
  104. }
  105. }
  106. /**
  107. * Return results in ajax form
  108. *
  109. * @return void
  110. */
  111. public function ajaxResults()
  112. {
  113. $this->startHTML('text/xml;charset=utf-8');
  114. $this->elementStart('head');
  115. // TRANS: Title for AJAX form to indicated that flags were removed.
  116. $this->element('title', null, _m('Flags cleared'));
  117. $this->elementEnd('head');
  118. $this->elementStart('body');
  119. // TRANS: Body element for "flags cleared" form.
  120. $this->element('p', 'cleared', _m('Cleared'));
  121. $this->elementEnd('body');
  122. $this->endHTML();
  123. }
  124. }