favor.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Favor action.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Action
  8. * @package GNUsocial
  9. * @author Evan Prodromou <evan@status.net>
  10. * @author Robin Millette <millette@status.net>
  11. * @author Mikael Nordfeldth <mmn@hethane.se>
  12. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  13. * @link http://www.gnu.org/software/social/
  14. *
  15. * StatusNet - the distributed open-source microblogging tool
  16. * Copyright (C) 2008, 2009, StatusNet, Inc.
  17. *
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License as published by
  20. * the Free Software Foundation, either version 3 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. */
  31. if (!defined('GNUSOCIAL')) { exit(1); }
  32. /**
  33. * FavorAction class.
  34. *
  35. * @category Action
  36. * @package GNUsocial
  37. * @author Evan Prodromou <evan@status.net>
  38. * @author Robin Millette <millette@status.net>
  39. * @author Mikael Nordfeldth <mmn@hethane.se>
  40. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  41. * @link http://www.gnu.org/software/social/
  42. */
  43. class FavorAction extends FormAction
  44. {
  45. protected $needPost = true;
  46. protected $object = null;
  47. protected function prepare(array $args=array())
  48. {
  49. parent::prepare($args);
  50. $this->target = Notice::getKV($this->trimmed('notice'));
  51. if (!$this->target instanceof Notice) {
  52. throw new ServerException(_m('No such notice.'));
  53. }
  54. if (!$this->target->inScope($this->scoped)) {
  55. // TRANS: Client error displayed when trying to reply to a notice a the target has no access to.
  56. // TRANS: %1$s is a user nickname, %2$d is a notice ID (number).
  57. throw new ClientException(sprintf(_m('%1$s has no right to reply to notice %2$d.'), $this->scoped->getNickname(), $this->target->id), 403);
  58. }
  59. return true;
  60. }
  61. protected function handlePost()
  62. {
  63. parent::handlePost();
  64. if (Fave::existsForProfile($this->target, $this->scoped)) {
  65. // TRANS: Client error displayed when trying to mark a notice as favorite that already is a favorite.
  66. throw new AlreadyFulfilledException(_('You have already favorited this!'));
  67. }
  68. // throws exception on failure
  69. $stored = Fave::addNew($this->scoped, $this->target);
  70. return _('Favorited the notice');
  71. }
  72. protected function showContent()
  73. {
  74. if ($this->target instanceof Notice) {
  75. $disfavor = new DisfavorForm($this, $this->target);
  76. $disfavor->show();
  77. }
  78. }
  79. }