/
HandleSubmitListener.php
86 lines (77 loc) · 2.94 KB
/
HandleSubmitListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* This file is part of contao-community-alliance/dc-general-contao-frontend.
*
* (c) 2015 Contao Community Alliance.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package contao-community-alliance/dc-general-contao-frontend
* @author Christian Schiffler <c.schiffler@cyberspectrum.de>
* @copyright 2015 Contao Community Alliance.
* @license https://github.com/contao-community-alliance/dc-general-contao-frontend/blob/master/LICENSE LGPL-3.0
* @filesource
*/
namespace ContaoCommunityAlliance\DcGeneral\ContaoFrontend\Listener;
use Contao\Environment;
use ContaoCommunityAlliance\Contao\Bindings\ContaoEvents;
use ContaoCommunityAlliance\Contao\Bindings\Events\Controller\RedirectEvent;
use ContaoCommunityAlliance\DcGeneral\ContaoFrontend\Event\HandleSubmitEvent;
use ContaoCommunityAlliance\DcGeneral\Data\ModelId;
use ContaoCommunityAlliance\UrlBuilder\UrlBuilder;
/**
* This class handles the submit buttons in the frontend for "save" and "save and create".
*/
class HandleSubmitListener
{
/**
* The Contao environment.
*
* @var Environment
*/
private $environment;
/**
* Create a new instance.
*
* @param Environment $environment The contao Environment in use.
*/
public function __construct(Environment $environment)
{
$this->environment = $environment;
}
/**
* Handle the event.
*
* @param HandleSubmitEvent $event The event.
*
* @return void
*/
public function handleEvent(HandleSubmitEvent $event)
{
$dispatcher = func_get_arg(2);
$currentUrl = $this->environment->get('uri');
switch ($event->getButtonName()) {
case 'save':
// Could be a create action, always redirect to current page with edit action and id properly set.
$url = UrlBuilder::fromUrl($currentUrl)
->setQueryParameter('act', 'edit')
->setQueryParameter('id', ModelId::fromModel($event->getModel())->getSerialized());
$dispatcher->dispatch(ContaoEvents::CONTROLLER_REDIRECT, new RedirectEvent($url->getUrl()));
break;
case 'saveNcreate':
// We want to create a new model, set create action and pass the current id as "after" to keep sorting.
$after = ModelId::fromModel($event->getModel());
$url = UrlBuilder::fromUrl($currentUrl)
->unsetQueryParameter('id')
->setQueryParameter('act', 'create')
->setQueryParameter('after', $after->getSerialized());
$dispatcher->dispatch(ContaoEvents::CONTROLLER_REDIRECT, new RedirectEvent($url->getUrl()));
break;
default:
// Do nothing.
}
}
}