verbb / events

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

Error on ticket pdf download #169

Open mihob opened 1 day ago

mihob commented 1 day ago

Describe the bug

When I try to download a ticket in the frontend I get the following error:

Error: Call to a member function can() on null in /app/vendor/verbb/events/src/elements/PurchasedTicket.php:543
Stack trace:
#0 /app/vendor/craftcms/cms/src/base/Element.php(3649): verbb\events\elements\PurchasedTicket->cpEditUrl()
#1 /app/vendor/yiisoft/yii2/base/Component.php(139): craft\base\Element->getCpEditUrl()
#2 /app/vendor/craftcms/cms/src/base/Element.php(2497): yii\base\Component->__get('cpEditUrl')
#3 /app/vendor/yiisoft/yii2/base/ArrayableTrait.php(126): craft\base\Element->__get('cpEditUrl')
#4 /app/vendor/craftcms/cms/src/base/Element.php(2653): craft\base\Element->traitToArray(Array, Array, false)
#5 /app/vendor/craftcms/cms/src/web/View.php(713): craft\base\Element->toArray(Array, Array, false)
#6 /app/vendor/verbb/events/src/elements/PurchasedTicket.php(233): craft\web\View->renderObjectTemplate('{{ (_variables....', Object(verbb\events\elements\PurchasedTicket))
#7 /app/vendor/yiisoft/yii2/base/BaseObject.php(109): verbb\events\elements\PurchasedTicket->init()
#8 /app/vendor/craftcms/cms/src/base/Model.php(87): yii\base\BaseObject->__construct()
#9 /app/vendor/craftcms/cms/src/base/Element.php(2417): craft\base\Model->__construct(Array)
#10 /app/vendor/craftcms/cms/src/elements/db/ElementQuery.php(2376): craft\base\Element->__construct(Array)
#11 /app/vendor/craftcms/cms/src/elements/db/ElementQuery.php(3561): craft\elements\db\ElementQuery->createElement(Array)
#12 /app/vendor/craftcms/cms/src/elements/db/ElementQuery.php(1780): craft\elements\db\ElementQuery->_createElements(Array)
#13 /app/vendor/yiisoft/yii2/db/Query.php(251): craft\elements\db\ElementQuery->populate(Array)
#14 /app/vendor/craftcms/cms/src/db/Query.php(292): yii\db\Query->all(NULL)
#15 /app/vendor/craftcms/cms/src/elements/db/ElementQuery.php(1853): craft\db\Query->all(NULL)
#16 /app/vendor/verbb/events/src/controllers/DownloadsController.php(80): craft\elements\db\ElementQuery->all()
#17 [internal function]: verbb\events\controllers\DownloadsController->actionPdf()
#18 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#19 /app/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#20 /app/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('pdf', Array)
#21 /app/vendor/craftcms/cms/src/web/Application.php(350): yii\base\Module->runAction('events/download...', Array)
#22 /app/vendor/craftcms/cms/src/web/Application.php(649): craft\web\Application->runAction('events/download...', Array)
#23 /app/vendor/craftcms/cms/src/web/Application.php(312): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#24 /app/vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#25 /app/web/index.php(13): yii\base\Application->run()
#26 {main}

The url for the download was generated with the following function: craft.events.getOrderPdfUrl(order)

Steps to reproduce

Craft CMS version

5.5.2

Plugin version

3.0.3

Multi-site?

YES

Additional context

No response

engram-design commented 1 day ago

Fixed for the next release. To get this early, run composer require verbb/events:"dev-craft-5 as 3.0.3".

mihob commented 1 day ago

Thanks!

mihob commented 1 day ago

@engram-design There is another problem, the link is generated incorrectly in the e-mail.

We have a link to download the tickets in our order confirmation.

If the email is sent in the frontend via the queue worker, the link is incorrect: https://example.test/de/index.php?p=actions/events/downloads/pdf&number=xxxx&site=german

It does not work because the language slug is added.

However, if I trigger the email in the CP or use the function in the frontend, the link is correct.

Update: If I trigger the email in the CP, it uses the CP base url, which is also incorrect. We use a subdomain for the control panel.

engram-design commented 1 day ago

So whether the link has a site param shouldn't matter, as the actionUrl looks correct to me. And it shouldn't matter whether it's a CP or Site URL, as this is an action URL, it should resolve to any Craft endpoint, and the p param is going to trigger a controller action. I'll test on my end just in case.

mihob commented 1 day ago

Hmm, but I shouldn't use the CP base url when I send the e-mail from the CP. After all, the e-mail is sent to the customer, who has no access to the CP and should not know the URL.

It should always use a correct frontend url, or at least let me force this.

engram-design commented 1 day ago

We generate the link using UrlHelper::actionUrl which is going to resolve to an action endpoint to hit our controller. Typically, this looks like:

https://example.test/actions/events/downloads/pdf?number=xxxx

I can see from the format of your URL that you're not using pathInfo so that's added as a query string.

From the looks of things if this is triggered in the context of a CP request, it's going to use the prependCpTrigger and the CP base URL for that.

I'm not sure how you're triggering emails on the site and CP?

mihob commented 22 hours ago

You can resend e-mails in the CP via an order:

grafik

I get three different links, depending on the “location” of the mailing:

Regularly via the frontend store (e-mail is sent via the queue, should be a console request): https://example.test/de/index.php/actions/events/downloads/pdf?number=xxx&site=german

Output in the frontend template (should be a site request): https://example.test/de/index.php/?p=actions/events/downloads/pdf?number=xxx&site=german

Sending the email again via the control panel (should be a cp request): https://cms.example.test/de/index.php?p=/actions/events/downloads/pdf?number=xxx&site=german

Is there a better way to generate a frontend url of the download?