1
|
|
|
<?php namespace Anomaly\Streams\Platform\Ui\Form\Component\Action; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Support\Parser; |
4
|
|
|
use Anomaly\Streams\Platform\Ui\Form\FormBuilder; |
5
|
|
|
use Illuminate\Contracts\Bus\SelfHandling; |
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
7
|
|
|
use Illuminate\Http\RedirectResponse; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Illuminate\Routing\Redirector; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ActionHandler |
13
|
|
|
* |
14
|
|
|
* @link http://anomaly.is/streams-platform |
15
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
16
|
|
|
* @author Ryan Thompson <[email protected]> |
17
|
|
|
* @package Anomaly\Streams\Platform\Ui\Form\Component\Action |
18
|
|
|
*/ |
19
|
|
|
class ActionHandler implements SelfHandling |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The parser utility. |
24
|
|
|
* |
25
|
|
|
* @var Parser |
26
|
|
|
*/ |
27
|
|
|
protected $parser; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The request object. |
31
|
|
|
* |
32
|
|
|
* @var Request |
33
|
|
|
*/ |
34
|
|
|
protected $request; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The redirector utility. |
38
|
|
|
* |
39
|
|
|
* @var Redirector |
40
|
|
|
*/ |
41
|
|
|
protected $redirector; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a new ActionHandler instance. |
45
|
|
|
* |
46
|
|
|
* @param Parser $parser |
47
|
|
|
* @param Request $request |
48
|
|
|
* @param Redirector $redirector |
49
|
|
|
*/ |
50
|
|
|
public function __construct(Parser $parser, Request $request, Redirector $redirector) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$this->parser = $parser; |
53
|
|
|
$this->request = $request; |
54
|
|
|
$this->redirector = $redirector; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Handle the form response. |
59
|
|
|
* |
60
|
|
|
* @param FormBuilder $builder |
61
|
|
|
*/ |
62
|
|
|
public function handle(FormBuilder $builder) |
63
|
|
|
{ |
64
|
|
|
/** |
65
|
|
|
* If the form already has a response |
66
|
|
|
* then we're being overridden. Abort! |
67
|
|
|
*/ |
68
|
|
|
if ($builder->getFormResponse()) { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$entry = $builder->getFormEntry(); |
73
|
|
|
$actions = $builder->getFormActions(); |
74
|
|
|
|
75
|
|
|
$action = $actions->active(); |
76
|
|
|
|
77
|
|
|
if ($entry && $entry instanceof Arrayable) { |
78
|
|
|
$entry = $entry->toArray(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$redirect = $action->getRedirect(); |
82
|
|
|
|
83
|
|
|
if ($redirect instanceof RedirectResponse) { |
84
|
|
|
|
85
|
|
|
$builder->setFormResponse($redirect); |
86
|
|
|
|
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($redirect === false) { |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$redirect = $this->parser->parse($redirect, compact('entry')); |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* If the redirect is null then use the current one. |
98
|
|
|
*/ |
99
|
|
|
if ($redirect === null) { |
100
|
|
|
$redirect = $this->redirector->back()->getTargetUrl(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* If the URL is a closure then call it. |
105
|
|
|
*/ |
106
|
|
|
if ($redirect instanceof \Closure) { |
107
|
|
|
$redirect = app()->call($redirect, compact('builder')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$builder->setFormResponse($this->redirector->to($redirect)); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|