From 21cc737f5c7ecca5ce0774e9903fff2ec26726da Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Wed, 20 Jan 2016 16:10:10 +0100 Subject: [PATCH] Cancelling RSVPs now seems to work. --- lib/widget.php | 10 +- plugins/Event/EventPlugin.php | 16 +-- plugins/Event/actions/cancelrsvp.php | 89 ------------ .../Event/actions/{newrsvp.php => rsvp.php} | 18 ++- plugins/Event/classes/RSVP.php | 8 +- plugins/Event/forms/cancelrsvp.php | 127 ------------------ plugins/Event/forms/rsvp.php | 31 +++-- 7 files changed, 50 insertions(+), 249 deletions(-) delete mode 100644 plugins/Event/actions/cancelrsvp.php rename plugins/Event/actions/{newrsvp.php => rsvp.php} (81%) delete mode 100644 plugins/Event/forms/cancelrsvp.php diff --git a/lib/widget.php b/lib/widget.php index 1ccd1e252b..227fe0ccee 100644 --- a/lib/widget.php +++ b/lib/widget.php @@ -28,9 +28,7 @@ * @link http://status.net/ */ -if (!defined('STATUSNET') && !defined('LACONICA')) { - exit(1); -} +if (!defined('GNUSOCIAL')) { exit(1); } /** * Base class for UI widgets @@ -65,9 +63,13 @@ class Widget * @param Action $out output helper, defaults to null */ - function __construct(Action $out=null) + function __construct(Action $out=null, array $widgetOpts=array()) { $this->out = $out; + if (!array_key_exists('scoped', $widgetOpts)) { + $this->widgetOpts['scoped'] = Profile::current(); + } + $this->scoped = $this->widgetOpts['scoped']; } /** diff --git a/plugins/Event/EventPlugin.php b/plugins/Event/EventPlugin.php index 2e972b781c..d22a5e9135 100644 --- a/plugins/Event/EventPlugin.php +++ b/plugins/Event/EventPlugin.php @@ -78,9 +78,10 @@ class EventPlugin extends ActivityVerbHandlerPlugin $m->connect('main/event/new', array('action' => 'newevent')); $m->connect('main/event/rsvp', - array('action' => 'newrsvp')); - $m->connect('main/event/rsvp/cancel', - array('action' => 'cancelrsvp')); + array('action' => 'rsvp')); + $m->connect('main/event/rsvp/:rsvp', // this will probably change to include event notice id + array('action' => 'rsvp'), + array('rsvp' => '[a-z]+')); $m->connect('event/:id', array('action' => 'showevent'), array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')); @@ -433,14 +434,7 @@ class EventPlugin extends ActivityVerbHandlerPlugin $out->elementEnd('div'); if ($scoped instanceof Profile) { - $rsvp = $event->getRSVP($scoped); - - if (empty($rsvp)) { - $form = new RSVPForm($out, array('event'=>$event)); - } else { - $form = new CancelRSVPForm($out, array('rsvp'=>$rsvp)); - } - + $form = new RSVPForm($out, array('event'=>$event, 'scoped'=>$scoped)); $form->show(); } $out->elementEnd('div'); diff --git a/plugins/Event/actions/cancelrsvp.php b/plugins/Event/actions/cancelrsvp.php deleted file mode 100644 index 67e5d896ac..0000000000 --- a/plugins/Event/actions/cancelrsvp.php +++ /dev/null @@ -1,89 +0,0 @@ -. - * - * @category Event - * @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('GNUSOCIAL')) { exit(1); } - -/** - * RSVP for an event - * - * @category Event - * @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 CancelrsvpAction extends FormAction -{ - protected $form = 'CancelRSVP'; - - function title() - { - // TRANS: Title for RSVP ("please respond") action. - return _m('TITLE','Cancel RSVP'); - } - - // FIXME: Merge this action with RSVPAction and add a 'cancel' thing there... - protected function doPreparation() - { - $rsvpId = $this->trimmed('rsvp'); - if (empty($rsvpId)) { - // TRANS: Client exception thrown when referring to a non-existing RSVP ("please respond") item. - throw new ClientException(_m('No such RSVP.')); - } - - $this->rsvp = RSVP::getKV('id', $rsvpId); - if (empty($this->rsvp)) { - // TRANS: Client exception thrown when referring to a non-existing RSVP ("please respond") item. - throw new ClientException(_m('No such RSVP.')); - } - - $this->formOpts['rsvp'] = $this->rsvp; - } - - protected function doPost() - { - $this->event = Happening::getKV('uri', $this->rsvp->event_uri); - if (empty($this->event)) { - // TRANS: Client exception thrown when referring to a non-existing event. - throw new ClientException(_m('No such event.')); - } - - $notice = $this->rsvp->getNotice(); - // NB: this will delete the rsvp, too - if (!empty($notice)) { - common_log(LOG_DEBUG, "Deleting notice..."); - $notice->deleteAs($this->scoped); - } else { - common_log(LOG_DEBUG, "Deleting RSVP alone..."); - $this->rsvp->delete(); - } - } -} diff --git a/plugins/Event/actions/newrsvp.php b/plugins/Event/actions/rsvp.php similarity index 81% rename from plugins/Event/actions/newrsvp.php rename to plugins/Event/actions/rsvp.php index df58f5b62f..ec0267a8fb 100644 --- a/plugins/Event/actions/newrsvp.php +++ b/plugins/Event/actions/rsvp.php @@ -40,11 +40,11 @@ if (!defined('GNUSOCIAL')) { exit(1); } * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @link http://status.net/ */ -class NewrsvpAction extends FormAction +class RsvpAction extends FormAction { protected $form = 'RSVP'; - protected $event; + protected $event = null; function title() { @@ -61,9 +61,19 @@ class NewrsvpAction extends FormAction protected function doPost() { + if ($this->trimmed('rsvp') === 'cancel') { + $rsvp = RSVP::byEventAndActor($this->event, $this->scoped); + try { + $notice = $rsvp->getStored(); + $notice->deleteAs($this->scoped); + } catch (NoResultException $e) { + // Notice already gone + $rsvp->delete(); + } + return _m('Cancelled RSVP'); + } - $verb = RSVP::verbFor(strtolower($this->trimmed('submitvalue'))); - + $verb = RSVP::verbFor(strtolower($this->trimmed('rsvp'))); $options = array('source' => 'web'); $act = new Activity(); diff --git a/plugins/Event/classes/RSVP.php b/plugins/Event/classes/RSVP.php index dc05f5a35e..bda502b8d0 100644 --- a/plugins/Event/classes/RSVP.php +++ b/plugins/Event/classes/RSVP.php @@ -208,7 +208,7 @@ class RSVP extends Managed_DataObject return $this->event_uri; } - static function getStored() + public function getStored() { return Notice::getByKeys(['uri' => $this->getUri()]); } @@ -218,6 +218,12 @@ class RSVP extends Managed_DataObject return self::getByKeys(['uri' => $stored->getUri()]); } + static function byEventAndActor(Happening $event, Profile $actor) + { + return self::getByKeys(['event_uri' => $event->getUri(), + 'profile_id' => $actor->getID()]); + } + static function forEvent(Happening $event) { $keypart = sprintf('rsvp:for-event:%s', $event->getUri()); diff --git a/plugins/Event/forms/cancelrsvp.php b/plugins/Event/forms/cancelrsvp.php deleted file mode 100644 index 4a07d46ba6..0000000000 --- a/plugins/Event/forms/cancelrsvp.php +++ /dev/null @@ -1,127 +0,0 @@ -. - * - * @category Event - * @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')) { exit(1); } - -/** - * A form to RSVP for an event - * - * @category General - * @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 CancelRSVPForm extends Form -{ - protected $rsvp = null; - - function __construct($out=null, array $formOpts=array()) - { - parent::__construct($out); - if (!isset($formOpts['rsvp'])) { - throw new ServerException('You must set the "rsvp" form option for RSVPForm.'); - } elseif (!$formOpts['rsvp'] instanceof RSVP) { - throw new ServerException('The "rsvp" form option for RSVPForm must be an RSVP object.'); - } - $this->rsvp = $formOpts['rsvp']; - } - - /** - * ID of the form - * - * @return int ID of the form - */ - function id() - { - return 'form_event_rsvp'; - } - - /** - * class of the form - * - * @return string class of the form - */ - function formClass() - { - return 'ajax'; - } - - /** - * Action of the form - * - * @return string URL of the action - */ - function action() - { - return common_local_url('cancelrsvp'); - } - - /** - * Data elements of the form - * - * @return void - */ - function formData() - { - $this->out->elementStart('fieldset', array('id' => 'new_rsvp_data')); - - $this->out->hidden('rsvp-id', $this->rsvp->getUri(), 'rsvp'); - - switch (RSVP::verbFor($this->rsvp->response)) { - case RSVP::POSITIVE: - // TRANS: Possible status for RSVP ("please respond") item. - $this->out->text(_m('You will attend this event.')); - break; - case RSVP::NEGATIVE: - // TRANS: Possible status for RSVP ("please respond") item. - $this->out->text(_m('You will not attend this event.')); - break; - case RSVP::POSSIBLE: - // TRANS: Possible status for RSVP ("please respond") item. - $this->out->text(_m('You might attend this event.')); - break; - } - - $this->out->elementEnd('fieldset'); - } - - /** - * Action elements - * - * @return void - */ - function formActions() - { - // TRANS: Button text to cancel responding to an RSVP ("please respond") item. - $this->out->submit('rsvp-cancel', _m('BUTTON', 'Cancel')); - } -} diff --git a/plugins/Event/forms/rsvp.php b/plugins/Event/forms/rsvp.php index 81cd335f87..d4769955ab 100644 --- a/plugins/Event/forms/rsvp.php +++ b/plugins/Event/forms/rsvp.php @@ -82,7 +82,7 @@ class RSVPForm extends Form */ function action() { - return common_local_url('newrsvp'); + return common_local_url('rsvp'); } /** @@ -98,7 +98,7 @@ class RSVPForm extends Form $this->out->text(_m('RSVP:')); $this->out->hidden('event-id', $this->event->getUri(), 'event'); - $this->out->hidden('submitvalue', ''); + $this->out->hidden('rsvp', ''); $this->out->elementEnd('fieldset'); } @@ -110,26 +110,31 @@ class RSVPForm extends Form */ function formActions() { - // TRANS: Button text for RSVP ("please respond") reply to confirm attendence. - $this->submitButton('yes', _m('BUTTON', 'Yes')); - // TRANS: Button text for RSVP ("please respond") reply to deny attendence. - $this->submitButton('no', _m('BUTTON', 'No')); - // TRANS: Button text for RSVP ("please respond") reply to indicate one might attend. - $this->submitButton('maybe', _m('BUTTON', 'Maybe')); + try { + $rsvp = RSVP::byEventAndActor($this->event, $this->scoped); + $this->submitButton('cancel', _m('BUTTON', 'Cancel')); + } catch (NoResultException $e) { + // TRANS: Button text for RSVP ("please respond") reply to confirm attendence. + $this->submitButton('yes', _m('BUTTON', 'Yes')); + // TRANS: Button text for RSVP ("please respond") reply to deny attendence. + $this->submitButton('no', _m('BUTTON', 'No')); + // TRANS: Button text for RSVP ("please respond") reply to indicate one might attend. + $this->submitButton('maybe', _m('BUTTON', 'Maybe')); + } } - function submitButton($id, $label) + function submitButton($answer, $label) { $this->out->element( 'input', array( 'type' => 'submit', - 'id' => 'rsvp-submit', - 'name' => $id, + 'id' => 'rsvp-submit-'.$answer, + 'name' => $answer, 'class' => 'submit', - 'value' => $label, + 'value' => $answer, 'title' => $label, - 'onClick' => 'this.form.submitvalue.value = this.name; return true;' + 'onClick' => 'this.form.rsvp.value = this.name; return true;' ) ); } -- 2.39.5