toselector.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 = 'public:site';
  79. if (!common_config('site', 'private')) {
  80. // TRANS: Option in drop-down of potential addressees.
  81. $choices['public:everyone'] = _m('SENDTO','Everyone');
  82. $default = 'public:everyone';
  83. }
  84. // XXX: better name...?
  85. // TRANS: Option in drop-down of potential addressees.
  86. // TRANS: %s is a StatusNet sitename.
  87. $choices['public:site'] = sprintf(_('My colleagues at %s'), common_config('site', 'name'));
  88. $groups = $this->user->getGroups();
  89. while ($groups instanceof User_group && $groups->fetch()) {
  90. $value = 'group:'.$groups->id;
  91. if (($this->to instanceof User_group) && $this->to->id == $groups->id) {
  92. $default = $value;
  93. }
  94. $choices[$value] = $groups->getBestName();
  95. }
  96. // XXX: add users...?
  97. if ($this->to instanceof Profile) {
  98. $value = 'profile:'.$this->to->id;
  99. $default = $value;
  100. $choices[$value] = $this->to->getBestName();
  101. }
  102. $this->out->dropdown($this->id,
  103. // TRANS: Label for drop-down of potential addressees.
  104. _m('LABEL','To:'),
  105. $choices,
  106. null,
  107. false,
  108. $default);
  109. $this->out->elementStart('span', 'checkbox-wrapper');
  110. $this->out->checkbox('notice_private',
  111. // TRANS: Checkbox label in widget for selecting potential addressees to mark the notice private.
  112. _('Private?'),
  113. $this->private);
  114. $this->out->elementEnd('span');
  115. }
  116. static function fillOptions($action, &$options)
  117. {
  118. // XXX: make arg name selectable
  119. $toArg = $action->trimmed('notice_to');
  120. $private = $action->boolean('notice_private');
  121. if (empty($toArg)) {
  122. return;
  123. }
  124. list($prefix, $value) = explode(':', $toArg);
  125. switch ($prefix) {
  126. case 'group':
  127. $options['groups'] = array($value);
  128. if ($private) {
  129. $options['scope'] = Notice::GROUP_SCOPE;
  130. }
  131. break;
  132. case 'profile':
  133. $profile = Profile::getKV('id', $value);
  134. $options['replies'] = array($profile->getUri());
  135. if ($private) {
  136. $options['scope'] = Notice::ADDRESSEE_SCOPE;
  137. }
  138. break;
  139. case 'public':
  140. if ($value == 'everyone' && !common_config('site', 'private')) {
  141. $options['scope'] = 0;
  142. } else if ($value == 'site') {
  143. $options['scope'] = Notice::SITE_SCOPE;
  144. }
  145. break;
  146. default:
  147. // TRANS: Client exception thrown in widget for selecting potential addressees when an invalid fill option was received.
  148. throw new ClientException(sprintf(_('Unknown to value: "%s".'),$toArg));
  149. break;
  150. }
  151. }
  152. }