From 5147404ea2cd5b41d5cad8f3c3769cbde0a2e2a9 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 28 Mar 2011 11:02:20 -0400 Subject: [PATCH] ToSelector widget to send private notices A new widget, ToSelector (Sorry, couldn't think of anything better) that lets you select an addressee for a notice and whether it's private. --- actions/newnotice.php | 4 ++ lib/noticeform.php | 30 +++++++- lib/toselector.php | 156 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 lib/toselector.php diff --git a/actions/newnotice.php b/actions/newnotice.php index 7f697e23f3..f48134c3b2 100644 --- a/actions/newnotice.php +++ b/actions/newnotice.php @@ -209,6 +209,10 @@ class NewnoticeAction extends Action $author_id = $user->id; $text = $content_shortened; + // Does the heavy-lifting for getting "To:" information + + ToSelector::fillOptions($this, $options); + if (Event::handle('StartNoticeSaveWeb', array($this, &$author_id, &$text, &$options))) { $notice = Notice::saveNew($user->id, $content_shortened, 'web', $options); diff --git a/lib/noticeform.php b/lib/noticeform.php index 2cbacc9280..01a462aad1 100644 --- a/lib/noticeform.php +++ b/lib/noticeform.php @@ -79,6 +79,15 @@ class NoticeForm extends Form var $location_id; var $location_ns; + /** select this group from the drop-down by default. */ + var $to_group; + + /** select this user from the drop-down by default. */ + var $to_user; + + /** Pre-click the private checkbox. */ + var $private; + /** * Constructor * @@ -109,7 +118,8 @@ class NoticeForm extends Form $this->actionName = $action->trimmed('action'); $prefill = array('content', 'inreplyto', 'lat', - 'lon', 'location_id', 'location_ns'); + 'lon', 'location_id', 'location_ns', + 'to_group', 'to_profile', 'private'); foreach ($prefill as $fieldName) { if (array_key_exists($fieldName, $options)) { @@ -117,6 +127,16 @@ class NoticeForm extends Form } } + // Prefill the profile if we're replying + + if (empty($this->to_profile) && + !empty($this->inreplyto)) { + $notice = Notice::staticGet('id', $this->inreplyto); + if (!empty($notice)) { + $this->to_profile = $notice->getProfile(); + } + } + if (array_key_exists('user', $options)) { $this->user = $options['user']; } else { @@ -218,6 +238,14 @@ class NoticeForm extends Form } $this->out->hidden('notice_in-reply-to', $this->inreplyto, 'inreplyto'); + $this->out->elementStart('div'); + $toWidget = new ToSelector($this->out, + $this->user, + (!empty($this->to_group) ? $this->to_group : $this->to_user)); + + $toWidget->show(); + $this->out->elementEnd('div'); + if ($this->user->shareLocation()) { $this->out->hidden('notice_data-lat', empty($this->lat) ? (empty($this->profile->lat) ? null : $this->profile->lat) : $this->lat, 'lat'); $this->out->hidden('notice_data-lon', empty($this->lon) ? (empty($this->profile->lon) ? null : $this->profile->lon) : $this->lon, 'lon'); diff --git a/lib/toselector.php b/lib/toselector.php new file mode 100644 index 0000000000..6dd4b5c9f4 --- /dev/null +++ b/lib/toselector.php @@ -0,0 +1,156 @@ +. + * + * @category Widget + * @package StatusNet + * @author Evan Prodromou + * @copyright 2011 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + // This check helps protect against security problems; + // your code file can't be executed directly from the web. + exit(1); +} + +/** + * Widget showing a drop-down of potential addressees + * + * @category Widget + * @package StatusNet + * @author Evan Prodromou + * @copyright 2011 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 + * @link http://status.net/ + */ + +class ToSelector extends Widget +{ + protected $user; + protected $to; + protected $id; + protected $name; + protected $private; + + /** + * Constructor + * + * @param HTMLOutputter $out output context + * @param User $user Current user + * @param mixed $to Default selection for addressee + */ + function __construct($out, $user, $to, $private=false, $id='notice_to', $name='notice_to') + { + parent::__construct($out); + + $this->user = $user; + $this->to = $to; + $this->private = $private; + $this->id = $id; + $this->name = $name; + } + + /** + * Constructor + * + * @param HTMLOutputter $out output context + * @param User $user Current user + * @param mixed $to Default selection for addressee + */ + function show() + { + $choices = array(); + $default = 'public:site'; + + if (!common_config('site', 'private')) { + $choices['public:everyone'] = _('Everyone'); + $default = 'public:everyone'; + } + // XXX: better name...? + $choices['public:site'] = sprintf(_('My colleagues at %s'), common_config('site', 'name')); + + $groups = $this->user->getGroups(); + + while ($groups->fetch()) { + $value = 'group:'.$groups->id; + if (($this->to instanceof User_group) && $this->to->id == $groups->id) { + $default = $value; + } + $choices[$value] = $groups->getBestName(); + } + + // XXX: add users...? + + if ($this->to instanceof Profile) { + $value = 'profile:'.$this->to->id; + $default = $value; + $choices[$value] = $this->to->getBestName(); + } + + $this->out->dropdown($this->id, + _('To:'), + $choices, + null, + false, + $default); + + $this->out->checkbox('notice_private', + _('Private'), + $this->private); + } + + static function fillOptions($action, &$options) + { + // XXX: make arg name selectable + $toArg = $action->trimmed('notice_to'); + $private = $action->boolean('notice_private'); + + list($prefix, $value) = explode(':', $toArg); + switch ($prefix) { + case 'group': + $options['groups'] = array($value); + if ($private) { + $options['scope'] = Notice::GROUP_SCOPE; + } + break; + case 'profile': + $profile = Profile::staticGet('id', $value); + $options['replies'] = $profile->getUri(); + if ($private) { + $options['scope'] = Notice::ADDRESSEE_SCOPE; + } + break; + case 'public': + if ($value == 'everyone' && !common_config('site', 'private')) { + $options['scope'] = 0; + } else if ($value == 'site') { + $options['scope'] = Notice::SITE_SCOPE; + } + break; + default: + throw new ClientException('Unknown to value: ' . toArg); + break; + } + } +} -- 2.39.5