tagprofile.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('GNUSOCIAL')) { exit(1); }
  20. require_once INSTALLDIR . '/lib/settingsaction.php';
  21. require_once INSTALLDIR . '/lib/peopletags.php';
  22. class TagprofileAction extends FormAction
  23. {
  24. var $error = null;
  25. protected $target = null;
  26. protected $form = 'TagProfile';
  27. protected function prepare(array $args=array())
  28. {
  29. parent::prepare($args);
  30. $id = $this->trimmed('id');
  31. if (!$id) {
  32. $this->target = null;
  33. } else {
  34. $this->target = Profile::getKV('id', $id);
  35. if (!$this->target instanceof Profile) {
  36. // TRANS: Client error displayed when referring to non-existing profile ID.
  37. $this->clientError(_('No profile with that ID.'));
  38. }
  39. }
  40. if ($this->target instanceof Profile && !$this->scoped->canTag($this->target)) {
  41. // TRANS: Client error displayed when trying to tag a user that cannot be tagged.
  42. $this->clientError(_('You cannot tag this user.'));
  43. }
  44. return true;
  45. }
  46. protected function handle()
  47. {
  48. if (Event::handle('StartTagProfileAction', array($this, $this->target))) {
  49. parent::handle();
  50. Event::handle('EndTagProfileAction', array($this, $this->target));
  51. }
  52. }
  53. function title()
  54. {
  55. if (!$this->target instanceof Profile) {
  56. // TRANS: Title for list form when not on a profile page.
  57. return _('List a profile');
  58. }
  59. // TRANS: Title for list form when on a profile page.
  60. // TRANS: %s is a profile nickname.
  61. return sprintf(_m('ADDTOLIST','List %s'), $this->target->getNickname());
  62. }
  63. function showContent()
  64. {
  65. $this->elementStart('div', 'entity_profile h-card');
  66. // TRANS: Header in list form.
  67. $this->element('h2', null, _('User profile'));
  68. $avatarUrl = $this->target->avatarUrl(AVATAR_PROFILE_SIZE);
  69. $this->element('img', array('src' => $avatarUrl,
  70. 'class' => 'u-photo avatar entity_depiction',
  71. 'width' => AVATAR_PROFILE_SIZE,
  72. 'height' => AVATAR_PROFILE_SIZE,
  73. 'alt' => $this->target->getBestName()));
  74. $this->element('a', array('href' => $this->target->getUrl(),
  75. 'class' => 'entity_nickname p-nickname'),
  76. $this->target->getNickname());
  77. if ($this->target->fullname) {
  78. $this->element('div', 'p-name entity_fn', $this->target->fullname);
  79. }
  80. if ($this->target->location) {
  81. $this->element('div', 'p-locality label entity_location', $this->target->location);
  82. }
  83. if ($this->target->homepage) {
  84. $this->element('a', array('href' => $this->target->homepage,
  85. 'rel' => 'me',
  86. 'class' => 'u-url entity_url'),
  87. $this->target->homepage);
  88. }
  89. if ($this->target->bio) {
  90. $this->element('div', 'p-note entity_note', $this->target->bio);
  91. }
  92. $this->elementEnd('div');
  93. if (Event::handle('StartShowTagProfileForm', array($this, $this->target))) {
  94. parent::showContent();
  95. Event::handle('EndShowTagProfileForm', array($this, $this->target));
  96. }
  97. }
  98. protected function getForm()
  99. {
  100. $class = $this->form.'Form';
  101. $form = new $class($this, $this->target);
  102. return $form;
  103. }
  104. protected function handlePost()
  105. {
  106. parent::handlePost(); // Does nothing for now
  107. $tagstring = $this->trimmed('tags');
  108. $token = $this->trimmed('token');
  109. if (Event::handle('StartSavePeopletags', array($this, $tagstring))) {
  110. $tags = array();
  111. $tag_priv = array();
  112. if (is_string($tagstring) && strlen($tagstring) > 0) {
  113. $tags = preg_split('/[\s,]+/', $tagstring);
  114. foreach ($tags as &$tag) {
  115. $private = @$tag[0] === '.';
  116. $tag = common_canonical_tag($tag);
  117. if (!common_valid_profile_tag($tag)) {
  118. // TRANS: Form validation error displayed if a given tag is invalid.
  119. // TRANS: %s is the invalid tag.
  120. $this->showForm(sprintf(_('Invalid tag: "%s".'), $tag));
  121. return;
  122. }
  123. $tag_priv[$tag] = $private;
  124. }
  125. }
  126. try {
  127. $result = Profile_tag::setTags($this->scoped->id, $this->target->id, $tags, $tag_priv);
  128. if (!$result) {
  129. throw new Exception('The tags could not be saved.');
  130. }
  131. } catch (Exception $e) {
  132. $this->showForm($e->getMessage());
  133. return false;
  134. }
  135. if ($this->boolean('ajax')) {
  136. $this->startHTML('text/xml;charset=utf-8');
  137. $this->elementStart('head');
  138. $this->element('title', null, _m('TITLE','Tags'));
  139. $this->elementEnd('head');
  140. $this->elementStart('body');
  141. if ($this->scoped->id == $this->target->id) {
  142. $widget = new SelftagsWidget($this, $this->scoped, $this->target);
  143. $widget->show();
  144. } else {
  145. $widget = new PeopletagsWidget($this, $this->scoped, $this->target);
  146. $widget->show();
  147. }
  148. $this->elementEnd('body');
  149. $this->endHTML();
  150. } else {
  151. // TRANS: Success message if lists are saved.
  152. $this->msg = _('Lists saved.');
  153. $this->showForm();
  154. }
  155. Event::handle('EndSavePeopletags', array($this, $tagstring));
  156. }
  157. }
  158. function showPageNotice()
  159. {
  160. if ($this->error) {
  161. $this->element('p', 'error', $this->error);
  162. } else {
  163. $this->elementStart('div', 'instructions');
  164. $this->element('p', null,
  165. // TRANS: Page notice.
  166. _('Use this form to add your subscribers or subscriptions to lists.'));
  167. $this->elementEnd('div');
  168. }
  169. }
  170. }