verbb / events

Craft CMS Plugin for events management and ticketing.
Other
22 stars 13 forks source link

Ticket element - overwrite rules #120

Closed anchovy closed 1 year ago

anchovy commented 1 year ago

Question

Is it possible to overwrite the ticket element rules?

vendor/verbb/events/src/elements/Ticket.php

        $rules[] = [
            ['availableFrom', 'availableTo'], function($model) {
                $event = $this->getEvent();
                $endDate = $event ? $event->endDate : null;

                if ($endDate) {
                    if ($this->availableFrom >= $endDate) {
                        $this->addError('availableFrom', Craft::t('events', 'Available From must be before the event End Date'));
                    }

                    if ($this->availableTo >= $endDate) {
                        $this->addError('availableTo', Craft::t('events', 'Available To must be before the event End Date'));
                    }
                }
            },
        ];

We have a slightly different use case where one particular ticket type is used to track attendance to events, so they are only 'purchasable' after the event has finished - so in our case we'd want something like....


       if($this->getType()->handle != 'attendance')
       {
        /* normal tickets  - must be purchased before the end date */
        $rules[] = [
            ['availableFrom', 'availableTo'], function($model) {
                $event = $this->getEvent();
                $endDate = $event ? $event->endDate : null;

                if ($endDate) {
                    if ($this->availableFrom >= $endDate) {
                        $this->addError('availableFrom', Craft::t('events', 'Available From must be before the event End Date'));
                    }

                    if ($this->availableTo >= $endDate) {
                        $this->addError('availableTo', Craft::t('events', 'Available To must be before the event End Date'));
                    }
                }
            },
        ];
       } else {
        /* attendance tickets  - must be 'purchased' after the end date */
        $rules[] = [
            ['availableFrom', 'availableTo'], function($model) {
                $event = $this->getEvent();
                $endDate = $event ? $event->endDate : null;

                if ($endDate) {
                    if ($this->availableFrom < $endDate) {
                        $this->addError('availableFrom', Craft::t('events', 'Available From must be after the event End Date'));
                    }

                    if ($this->availableTo < $endDate) {
                        $this->addError('availableTo', Craft::t('events', 'Available To must be after the event End Date'));
                    }
                }
            },
        ];
       }

So - is it possible to overwrite these rules?

Regards Ben

Additional context

No response

anchovy commented 1 year ago

Or - maybe another option is to only verify the availableTo against the availableFrom and de-couple them from the event endDate. This way users/admins have flexibility to set ticket dates that work for their use case.

engram-design commented 1 year ago

Yii rules are a little difficult to "unset", but it certainly is possible! You'll want to loop through any of the rules configured for the element and remove it, adding your own.

use craft\events\DefineRulesEvent;
use verbb\events\elements\Ticket;
use yii\base\Event;

Event::on(Ticket::class, Ticket::EVENT_DEFINE_RULES, function(DefineRulesEvent $event) {
    foreach ($event->rules as $key => $rule) {
        [$attribute, $validator] = $rule;

        if ($attribute === ['availableFrom', 'availableTo']) {
            unset($event->rules[$key]);
        }

        if ($attribute === ['availableFrom']) {
            unset($event->rules[$key]);
        }
    }
});

But you'll need to get on the latest craft-4 branch for that as well.

And yeah, I'll consider that. I was just thinking that there's no point in offering tickets for sale once the end date of the event is over, but this is purely a validation step.

anchovy commented 1 year ago

Hi, thanks for that. We've started the upgrade to craft-v4, so I'll get that finished and hook into unsetting these rules. Thanks.