toselector.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. protected $user;
  48. protected $to;
  49. protected $id;
  50. protected $name;
  51. protected $private;
  52. /**
  53. * Constructor
  54. *
  55. * @param HTMLOutputter $out output context
  56. * @param User $user Current user
  57. * @param mixed $to Default selection for addressee
  58. */
  59. function __construct($out, $user, $to, $private=false, $id='notice_to', $name='notice_to')
  60. {
  61. parent::__construct($out);
  62. $this->user = $user;
  63. $this->to = $to;
  64. $this->private = $private;
  65. $this->id = $id;
  66. $this->name = $name;
  67. }
  68. /**
  69. * Constructor
  70. *
  71. * @param HTMLOutputter $out output context
  72. * @param User $user Current user
  73. * @param mixed $to Default selection for addressee
  74. */
  75. function show()
  76. {
  77. $choices = array();
  78. $default = common_config('site', 'private') ? 'public:site' : 'public:everyone';
  79. $groups = $this->user->getGroups();
  80. while ($groups instanceof User_group && $groups->fetch()) {
  81. $value = 'group:'.$groups->getID();
  82. if (($this->to instanceof User_group) && $this->to->id == $groups->id) {
  83. $default = $value;
  84. }
  85. $choices[$value] = "!{$groups->getNickname()} [{$groups->getBestName()}]";
  86. }
  87. // Add subscribed users to dropdown menu
  88. $users = $this->user->getSubscribed();
  89. while ($users->fetch()) {
  90. $value = 'profile:'.$users->getID();
  91. try {
  92. $choices[$value] = substr($users->getAcctUri(), 5) . " [{$users->getBestName()}]";
  93. } catch (ProfileNoAcctUriException $e) {
  94. $choices[$value] = "[?@?] " . $e->profile->getBestName();
  95. }
  96. }
  97. if ($this->to instanceof Profile) {
  98. $value = 'profile:'.$this->to->getID();
  99. $default = $value;
  100. try {
  101. $choices[$value] = substr($this->to->getAcctUri(), 5) . " [{$this->to->getBestName()}]";
  102. } catch (ProfileNoAcctUriException $e) {
  103. $choices[$value] = "[?@?] " . $e->profile->getBestName();
  104. }
  105. }
  106. // alphabetical order
  107. asort($choices);
  108. // Reverse so we can add entries at the end (can't unshift with a key)
  109. $choices = array_reverse($choices);
  110. if (common_config('notice', 'allowprivate')) {
  111. // TRANS: Option in drop-down of potential addressees.
  112. // TRANS: %s is a StatusNet sitename.
  113. $choices['public:site'] = sprintf(_('Everyone at %s'), common_config('site', 'name'));
  114. }
  115. if (!common_config('site', 'private')) {
  116. // TRANS: Option in drop-down of potential addressees.
  117. $choices['public:everyone'] = _m('SENDTO','Everyone');
  118. }
  119. // Return the order
  120. $choices = array_reverse($choices);
  121. $this->out->dropdown($this->id,
  122. // TRANS: Label for drop-down of potential addressees.
  123. _m('LABEL','To:'),
  124. $choices,
  125. null,
  126. false,
  127. $default);
  128. $this->out->elementStart('span', 'checkbox-wrapper');
  129. if (common_config('notice', 'allowprivate')) {
  130. $this->out->checkbox('notice_private',
  131. // TRANS: Checkbox label in widget for selecting potential addressees to mark the notice private.
  132. _('Private?'),
  133. $this->private);
  134. }
  135. $this->out->elementEnd('span');
  136. }
  137. static function fillActivity(Action $action, Activity $act, array &$options)
  138. {
  139. if (!$act->context instanceof ActivityContext) {
  140. $act->context = new ActivityContext();
  141. }
  142. self::fillOptions($action, $options);
  143. if (isset($options['groups'])) {
  144. foreach ($options['groups'] as $group_id) {
  145. $group = User_group::getByID($group_id);
  146. $act->context->attention[$group->getUri()] = $group->getObjectType();
  147. }
  148. }
  149. if (isset($options['replies'])) {
  150. foreach ($options['replies'] as $profile_uri) {
  151. $profile = Profile::fromUri($profile_uri);
  152. $act->context->attention[$profile->getUri()] = $profile->getObjectType();
  153. }
  154. }
  155. }
  156. static function fillOptions($action, &$options)
  157. {
  158. // XXX: make arg name selectable
  159. $toArg = $action->trimmed('notice_to');
  160. $private = common_config('notice', 'allowprivate') ? $action->boolean('notice_private') : false;
  161. if (empty($toArg)) {
  162. return;
  163. }
  164. list($prefix, $value) = explode(':', $toArg);
  165. switch ($prefix) {
  166. case 'group':
  167. $options['groups'] = array($value);
  168. if ($private) {
  169. $options['scope'] = Notice::GROUP_SCOPE;
  170. }
  171. break;
  172. case 'profile':
  173. $profile = Profile::getKV('id', $value);
  174. $options['replies'] = array($profile->getUri());
  175. if ($private) {
  176. $options['scope'] = Notice::ADDRESSEE_SCOPE;
  177. }
  178. break;
  179. case 'public':
  180. if ($value == 'everyone' && !common_config('site', 'private')) {
  181. $options['scope'] = 0;
  182. } else if ($value == 'site') {
  183. $options['scope'] = Notice::SITE_SCOPE;
  184. }
  185. break;
  186. default:
  187. // TRANS: Client exception thrown in widget for selecting potential addressees when an invalid fill option was received.
  188. throw new ClientException(sprintf(_('Unknown to value: "%s".'),$toArg));
  189. break;
  190. }
  191. }
  192. }