toselector.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Widget showing a drop-down of potential addressees
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Widget
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Widget showing a drop-down of potential addressees
  37. *
  38. * @category Widget
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class ToSelector extends Widget
  46. {
  47. public $widgetOpts;
  48. public $scoped;
  49. protected $user;
  50. protected $to;
  51. protected $id;
  52. protected $name;
  53. protected $private;
  54. /**
  55. * Constructor
  56. *
  57. * @param HTMLOutputter $out output context
  58. * @param User $user Current user
  59. * @param mixed $to Default selection for addressee
  60. */
  61. function __construct($out, $user, $to, $private=false, $id='notice_to', $name='notice_to')
  62. {
  63. parent::__construct($out);
  64. $this->user = $user;
  65. $this->to = $to;
  66. $this->private = $private;
  67. $this->id = $id;
  68. $this->name = $name;
  69. }
  70. /**
  71. * Constructor
  72. *
  73. * @param HTMLOutputter $out output context
  74. * @param User $user Current user
  75. * @param mixed $to Default selection for addressee
  76. */
  77. function show()
  78. {
  79. $choices = array();
  80. $default = common_config('site', 'private') ? 'public:site' : 'public:everyone';
  81. $groups = $this->user->getGroups();
  82. while ($groups instanceof User_group && $groups->fetch()) {
  83. $value = 'group:'.$groups->getID();
  84. if (($this->to instanceof User_group) && $this->to->id == $groups->id) {
  85. $default = $value;
  86. }
  87. $choices[$value] = "!{$groups->getNickname()} [{$groups->getBestName()}]";
  88. }
  89. // Add subscribed users to dropdown menu
  90. $users = $this->user->getSubscribed();
  91. while ($users->fetch()) {
  92. $value = 'profile:'.$users->getID();
  93. try {
  94. $choices[$value] = substr($users->getAcctUri(), 5) . " [{$users->getBestName()}]";
  95. } catch (ProfileNoAcctUriException $e) {
  96. $choices[$value] = "[?@?] " . $e->profile->getBestName();
  97. }
  98. }
  99. if ($this->to instanceof Profile) {
  100. $value = 'profile:'.$this->to->getID();
  101. $default = $value;
  102. try {
  103. $choices[$value] = substr($this->to->getAcctUri(), 5) . " [{$this->to->getBestName()}]";
  104. } catch (ProfileNoAcctUriException $e) {
  105. $choices[$value] = "[?@?] " . $e->profile->getBestName();
  106. }
  107. }
  108. // alphabetical order
  109. asort($choices);
  110. // Reverse so we can add entries at the end (can't unshift with a key)
  111. $choices = array_reverse($choices);
  112. if (common_config('site', 'private')) {
  113. // TRANS: Option in drop-down of potential addressees.
  114. // TRANS: %s is a StatusNet sitename.
  115. $choices['public:site'] = sprintf(_('Everyone at %s'), common_config('site', 'name'));
  116. }
  117. if (!common_config('site', 'private')) {
  118. // TRANS: Option in drop-down of potential addressees.
  119. $choices['public:everyone'] = _m('SENDTO','Everyone');
  120. }
  121. // Return the order
  122. $choices = array_reverse($choices);
  123. $this->out->dropdown($this->id,
  124. // TRANS: Label for drop-down of potential addressees.
  125. _m('LABEL','To:'),
  126. $choices,
  127. null,
  128. false,
  129. $default);
  130. $this->out->elementStart('span', 'checkbox-wrapper');
  131. if (common_config('notice', 'allowprivate')) {
  132. $this->out->checkbox('notice_private',
  133. // TRANS: Checkbox label in widget for selecting potential addressees to mark the notice private.
  134. _('Private?'),
  135. $this->private);
  136. }
  137. $this->out->elementEnd('span');
  138. }
  139. static function fillActivity(Action $action, Activity $act, array &$options)
  140. {
  141. if (!$act->context instanceof ActivityContext) {
  142. $act->context = new ActivityContext();
  143. }
  144. self::fillOptions($action, $options);
  145. if (isset($options['groups'])) {
  146. foreach ($options['groups'] as $group_id) {
  147. $group = User_group::getByID($group_id);
  148. $act->context->attention[$group->getUri()] = $group->getObjectType();
  149. }
  150. }
  151. if (isset($options['replies'])) {
  152. foreach ($options['replies'] as $profile_uri) {
  153. $profile = Profile::fromUri($profile_uri);
  154. $act->context->attention[$profile->getUri()] = $profile->getObjectType();
  155. }
  156. }
  157. }
  158. static function fillOptions($action, &$options)
  159. {
  160. // XXX: make arg name selectable
  161. $toArg = $action->trimmed('notice_to');
  162. $private = common_config('notice', 'allowprivate') ? $action->boolean('notice_private') : false;
  163. if (empty($toArg)) {
  164. return;
  165. }
  166. list($prefix, $value) = explode(':', $toArg);
  167. switch ($prefix) {
  168. case 'group':
  169. $options['groups'] = array($value);
  170. if ($private) {
  171. $options['scope'] = Notice::GROUP_SCOPE;
  172. }
  173. break;
  174. case 'profile':
  175. $profile = Profile::getKV('id', $value);
  176. $options['replies'] = array($profile->getUri());
  177. if ($private) {
  178. $options['scope'] = Notice::ADDRESSEE_SCOPE;
  179. }
  180. break;
  181. case 'public':
  182. if ($value == 'everyone' && !common_config('site', 'private')) {
  183. $options['scope'] = 0;
  184. } else if ($value == 'site') {
  185. $options['scope'] = Notice::SITE_SCOPE;
  186. }
  187. break;
  188. default:
  189. // TRANS: Client exception thrown in widget for selecting potential addressees when an invalid fill option was received.
  190. throw new ClientException(sprintf(_('Unknown to value: "%s".'),$toArg));
  191. break;
  192. }
  193. }
  194. }