1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symbiote\AdvancedWorkflow\FormFields; |
4
|
|
|
|
5
|
|
|
use ReflectionClass; |
6
|
|
|
use SilverStripe\Control\Controller; |
7
|
|
|
use SilverStripe\Control\RequestHandler; |
8
|
|
|
use Symbiote\AdvancedWorkflow\DataObjects\WorkflowAction; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Handles requests for creating or editing actions. |
12
|
|
|
* |
13
|
|
|
* @package silverstripe-advancedworkflow |
14
|
|
|
*/ |
15
|
|
|
class WorkflowFieldActionController extends RequestHandler |
16
|
|
|
{ |
17
|
|
|
private static $url_handlers = array( |
|
|
|
|
18
|
|
|
'new/$Class' => 'handleAdd', |
19
|
|
|
'item/$ID' => 'handleItem' |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
private static $allowed_actions = array( |
|
|
|
|
23
|
|
|
'handleAdd', |
24
|
|
|
'handleItem' |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
protected $parent; |
28
|
|
|
protected $name; |
29
|
|
|
|
30
|
|
|
public function __construct($parent, $name) |
31
|
|
|
{ |
32
|
|
|
$this->parent = $parent; |
33
|
|
|
$this->name = $name; |
34
|
|
|
|
35
|
|
|
parent::__construct(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function handleAdd() |
39
|
|
|
{ |
40
|
|
|
$class = $this->unsanitiseClassName($this->request->param('Class')); |
41
|
|
|
|
42
|
|
|
if (!class_exists($class) || !is_subclass_of($class, WorkflowAction::class)) { |
|
|
|
|
43
|
|
|
$this->httpError(400); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$reflector = new ReflectionClass($class); |
47
|
|
|
|
48
|
|
|
if ($reflector->isAbstract() || !singleton($class)->canCreate()) { |
49
|
|
|
$this->httpError(400); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$record = new $class(); |
53
|
|
|
$record->WorkflowDefID = $this->parent->Definition()->ID; |
54
|
|
|
|
55
|
|
|
return new WorkflowFieldItemController( |
56
|
|
|
$this, |
57
|
|
|
Controller::join_links('new', $this->sanitiseClassName($class)), |
58
|
|
|
$record |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
public function handleItem() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$id = $this->request->param('ID'); |
65
|
|
|
$defn = $this->parent->Definition(); |
66
|
|
|
$action = $defn->Actions()->byID($id); |
67
|
|
|
|
68
|
|
|
if (!$action) { |
69
|
|
|
$this->httpError(404); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (!$action->canEdit()) { |
73
|
|
|
$this->httpError(403); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return new WorkflowFieldItemController($this, "item/$id", $action); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function RootField() |
80
|
|
|
{ |
81
|
|
|
return $this->parent; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function Link($action = null) |
85
|
|
|
{ |
86
|
|
|
return Controller::join_links($this->parent->Link(), $this->name, $action); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sanitise a model class' name for inclusion in a link |
91
|
|
|
* |
92
|
|
|
* @param string $class |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function sanitiseClassName($class) |
96
|
|
|
{ |
97
|
|
|
return str_replace('\\', '-', $class); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Unsanitise a model class' name from a URL param |
102
|
|
|
* |
103
|
|
|
* @param string $class |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
protected function unsanitiseClassName($class) |
107
|
|
|
{ |
108
|
|
|
return str_replace('-', '\\', $class); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|