AJAX submit actions for tag subscribe/unsubscribe

This commit is contained in:
Brion Vibber 2011-03-15 13:17:06 -07:00
parent 119885d964
commit 0bb2f14704
6 changed files with 550 additions and 4 deletions

View File

@ -106,4 +106,35 @@ class TagSub extends Managed_DataObject
); );
} }
/**
* Start a tag subscription!
*
* @param profile $profile subscriber
* @param string $tag subscribee
* @return TagSub
*/
static function start(Profile $profile, $tag)
{
$ts = new TagSub();
$ts->tag = $tag;
$ts->profile_id = $profile->id;
$ts->created = common_sql_now();
$ts->insert();
return $ts;
}
/**
* End a tag subscription!
*
* @param profile $profile subscriber
* @param string $tag subscribee
*/
static function cancel(Profile $profile, $tag)
{
$ts = TagSub::pkeyGet(array('tag' => $tag,
'profile_id' => $profile->id));
if ($ts) {
$ts->delete();
}
}
} }

View File

@ -73,13 +73,11 @@ class TagSubPlugin extends Plugin
switch ($cls) switch ($cls)
{ {
case 'TagsubAction':
case 'TagunsubAction':
include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
return false;
case 'TagSub': case 'TagSub':
include_once $dir.'/'.$cls.'.php'; include_once $dir.'/'.$cls.'.php';
return false; return false;
case 'TagsubAction':
case 'TagunsubAction':
case 'TagSubForm': case 'TagSubForm':
case 'TagUnsubForm': case 'TagUnsubForm':
include_once $dir.'/'.strtolower($cls).'.php'; include_once $dir.'/'.strtolower($cls).'.php';
@ -153,4 +151,32 @@ class TagSubPlugin extends Plugin
} }
return true; return true;
} }
/**
*
* @param TagAction $action
* @return boolean hook result
*/
function onStartTagShowContent(TagAction $action)
{
$user = common_current_user();
if ($user) {
$tag = $action->trimmed('tag');
$tagsub = TagSub::pkeyGet(array('tag' => $tag,
'profile_id' => $user->id));
if ($tagsub) {
$form = new TagUnsubForm($action, $tag);
} else {
$form = new TagSubForm($action, $tag);
}
$action->elementStart('div', 'entity_actions');
$action->elementStart('ul');
$action->elementStart('li', 'entity_subscribe');
$form->show();
$action->elementEnd('li');
$action->elementEnd('ul');
$action->elementEnd('div');
}
return true;
}
} }

View File

@ -0,0 +1,149 @@
<?php
/**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2008-2011, StatusNet, Inc.
*
* Tag subscription action.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* PHP version 5
*
* @category Action
* @package StatusNet
* @author Brion Vibber <brion@status.net>
* @author Evan Prodromou <evan@status.net>
* @copyright 2008-2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
/**
* Tag subscription action
*
* Takes parameters:
*
* - token: session token to prevent CSRF attacks
* - ajax: boolean; whether to return Ajax or full-browser results
*
* Only works if the current user is logged in.
*
* @category Action
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Brion Vibber <brion@status.net>
* @copyright 2008-2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
* @link http://status.net/
*/
class TagsubAction extends Action
{
var $user;
var $tag;
/**
* Check pre-requisites and instantiate attributes
*
* @param Array $args array of arguments (URL, GET, POST)
*
* @return boolean success flag
*/
function prepare($args)
{
parent::prepare($args);
if ($this->boolean('ajax')) {
StatusNet::setApi(true);
}
// Only allow POST requests
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
// TRANS: Client error displayed trying to perform any request method other than POST.
// TRANS: Do not translate POST.
$this->clientError(_('This action only accepts POST requests.'));
return false;
}
// CSRF protection
$token = $this->trimmed('token');
if (!$token || $token != common_session_token()) {
// TRANS: Client error displayed when the session token is not okay.
$this->clientError(_('There was a problem with your session token.'.
' Try again, please.'));
return false;
}
// Only for logged-in users
$this->user = common_current_user();
if (empty($this->user)) {
// TRANS: Client error displayed trying to subscribe when not logged in.
$this->clientError(_('Not logged in.'));
return false;
}
// Profile to subscribe to
$this->tag = $this->arg('tag');
if (empty($this->tag)) {
// TRANS: Client error displayed trying to subscribe to a non-existing profile.
$this->clientError(_('No such profile.'));
return false;
}
return true;
}
/**
* Handle request
*
* Does the subscription and returns results.
*
* @param Array $args unused.
*
* @return void
*/
function handle($args)
{
// Throws exception on error
TagSub::start($this->user->getProfile(),
$this->tag);
if ($this->boolean('ajax')) {
$this->startHTML('text/xml;charset=utf-8');
$this->elementStart('head');
// TRANS: Page title when tag subscription succeeded.
$this->element('title', null, _m('Subscribed'));
$this->elementEnd('head');
$this->elementStart('body');
$unsubscribe = new TagUnsubForm($this, $this->tag);
$unsubscribe->show();
$this->elementEnd('body');
$this->elementEnd('html');
} else {
$url = common_local_url('tag',
array('tag' => $this->tag));
common_redirect($url, 303);
}
}
}

View File

@ -0,0 +1,142 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Form for subscribing to a tag
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category TagSubPlugin
* @package StatusNet
* @author Brion Vibber <brion@status.net>
* @author Evan Prodromou <evan@status.net>
* @author Sarven Capadisli <csarven@status.net>
* @copyright 2009-2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1);
}
/**
* Form for subscribing to a user
*
* @category TagSubPlugin
* @package StatusNet
* @author Brion Vibber <brion@status.net>
* @author Evan Prodromou <evan@status.net>
* @author Sarven Capadisli <csarven@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*
* @see UnsubscribeForm
*/
class TagSubForm extends Form
{
/**
* Name of tag to subscribe to
*/
var $tag = '';
/**
* Constructor
*
* @param HTMLOutputter $out output channel
* @param string $tag name of tag to subscribe to
*/
function __construct($out=null, $tag=null)
{
parent::__construct($out);
$this->tag = $tag;
}
/**
* ID of the form
*
* @return int ID of the form
*/
function id()
{
return 'tag-subscribe-' . $this->tag;
}
/**
* class of the form
*
* @return string of the form class
*/
function formClass()
{
// class to match existing styles...
return 'form_user_subscribe ajax';
}
/**
* Action of the form
*
* @return string URL of the action
*/
function action()
{
return common_local_url('tagsub', array('tag' => $this->tag));
}
/**
* Legend of the Form
*
* @return void
*/
function formLegend()
{
$this->out->element('legend', null, _m('Subscribe to this tag'));
}
/**
* Data elements of the form
*
* @return void
*/
function formData()
{
$this->out->hidden('subscribeto-' . $this->tag,
$this->tag,
'subscribeto');
}
/**
* Action elements
*
* @return void
*/
function formActions()
{
$this->out->submit('submit', _('Subscribe'), 'submit', null, _m('Subscribe to this tag'));
}
}

View File

@ -0,0 +1,89 @@
<?php
/**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2008-2011, StatusNet, Inc.
*
* Tag subscription action.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* PHP version 5
*
* @category Action
* @package StatusNet
* @author Brion Vibber <brion@status.net>
* @author Evan Prodromou <evan@status.net>
* @copyright 2008-2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
/**
* Tag unsubscription action
*
* Takes parameters:
*
* - token: session token to prevent CSRF attacks
* - ajax: boolean; whether to return Ajax or full-browser results
*
* Only works if the current user is logged in.
*
* @category Action
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @author Brion Vibber <brion@status.net>
* @copyright 2008-2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
* @link http://status.net/
*/
class TagunsubAction extends TagsubAction
{
/**
* Handle request
*
* Does the subscription and returns results.
*
* @param Array $args unused.
*
* @return void
*/
function handle($args)
{
// Throws exception on error
TagSub::cancel($this->user->getProfile(),
$this->tag);
if ($this->boolean('ajax')) {
$this->startHTML('text/xml;charset=utf-8');
$this->elementStart('head');
// TRANS: Page title when tag unsubscription succeeded.
$this->element('title', null, _m('Unsubscribed'));
$this->elementEnd('head');
$this->elementStart('body');
$subscribe = new TagSubForm($this, $this->tag);
$subscribe->show();
$this->elementEnd('body');
$this->elementEnd('html');
} else {
$url = common_local_url('tag',
array('tag' => $this->tag));
common_redirect($url, 303);
}
}
}

View File

@ -0,0 +1,109 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Form for subscribing to a tag
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category TagSubPlugin
* @package StatusNet
* @author Brion Vibber <brion@status.net>
* @author Evan Prodromou <evan@status.net>
* @author Sarven Capadisli <csarven@status.net>
* @copyright 2009-2011 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1);
}
/**
* Form for subscribing to a user
*
* @category TagSubPlugin
* @package StatusNet
* @author Brion Vibber <brion@status.net>
* @author Evan Prodromou <evan@status.net>
* @author Sarven Capadisli <csarven@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*
* @see UnsubscribeForm
*/
class TagUnsubForm extends TagSubForm
{
/**
* ID of the form
*
* @return int ID of the form
*/
function id()
{
return 'tag-unsubscribe-' . $this->tag;
}
/**
* class of the form
*
* @return string of the form class
*/
function formClass()
{
// class to match existing styles...
return 'form_user_unsubscribe ajax';
}
/**
* Action of the form
*
* @return string URL of the action
*/
function action()
{
return common_local_url('tagunsub', array('tag' => $this->tag));
}
/**
* Legend of the Form
*
* @return void
*/
function formLegend()
{
$this->out->element('legend', null, _m('Unsubscribe from this tag'));
}
/**
* Action elements
*
* @return void
*/
function formActions()
{
$this->out->submit('submit', _('Unsubscribe'), 'submit', null, _m('Unsubscribe from this tag'));
}
}