|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package midcom.workflow |
|
4
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
5
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace midcom\workflow; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
12
|
|
|
use midcom_helper_reflector; |
|
13
|
|
|
use midcom_core_dbaobject; |
|
14
|
|
|
use midcom_response_relocate; |
|
15
|
|
|
use midcom_connection; |
|
16
|
|
|
use midcom; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Helper class for manipulating toolbars |
|
23
|
|
|
* |
|
24
|
|
|
* @package midcom.workflow |
|
25
|
|
|
*/ |
|
26
|
|
|
class delete extends dialog |
|
27
|
|
|
{ |
|
28
|
|
|
const CONFIRMED = 'confirmed'; |
|
29
|
|
|
|
|
30
|
|
|
const SUCCESS = 'success'; |
|
31
|
|
|
|
|
32
|
|
|
const FAILURE = 'failure'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The method to call for deletion (delete or delete_tree) |
|
36
|
|
|
*/ |
|
37
|
|
|
protected bool $recursive; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The URL to redirect to after successful deletion |
|
41
|
|
|
* |
|
42
|
|
|
* Defaults to topic start page |
|
43
|
|
|
*/ |
|
44
|
|
|
protected string $success_url; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Disable relocate after execute |
|
48
|
|
|
* |
|
49
|
|
|
* Returns the uimessage as JSON instead |
|
50
|
|
|
*/ |
|
51
|
|
|
protected bool $relocate; |
|
52
|
|
|
|
|
53
|
|
|
protected midcom_core_dbaobject $object; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $label; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $dialog_text; |
|
64
|
|
|
|
|
65
|
|
|
private string $form_identifier = 'confirm-delete'; |
|
66
|
|
|
|
|
67
|
|
|
private \midcom_services_i18n_l10n $l10n_midcom; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
125 |
|
public function configure(OptionsResolver $resolver) |
|
73
|
|
|
{ |
|
74
|
125 |
|
$this->l10n_midcom = midcom::get()->i18n->get_l10n('midcom'); |
|
75
|
|
|
|
|
76
|
125 |
|
$resolver |
|
77
|
125 |
|
->setDefaults([ |
|
78
|
125 |
|
'recursive' => false, |
|
79
|
125 |
|
'success_url' => '', |
|
80
|
125 |
|
'relocate' => true, |
|
81
|
125 |
|
'label' => null, |
|
82
|
125 |
|
'object' => null, |
|
83
|
125 |
|
'dialog_text' => null |
|
84
|
125 |
|
]) |
|
85
|
125 |
|
->setRequired('object') |
|
86
|
125 |
|
->setAllowedTypes('object', midcom_core_dbaobject::class) |
|
87
|
125 |
|
->setNormalizer('label', function ($options, $value) { |
|
88
|
125 |
|
return $value ?? midcom_helper_reflector::get_object_title($options['object']); |
|
89
|
125 |
|
}); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
113 |
|
public function get_button_config() : array |
|
93
|
|
|
{ |
|
94
|
113 |
|
$dialog_text = $this->dialog_text ?: '<p>' . sprintf($this->l10n_midcom->get('delete %s'), $this->label) . '</p>'; |
|
95
|
113 |
|
if ($this->recursive) { |
|
96
|
98 |
|
$dialog_text .= '<p class="warning">' . $this->l10n_midcom->get('all descendants will be deleted') . ':</p>'; |
|
97
|
98 |
|
$dialog_text .= '<div id="delete-child-list"></div>'; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
113 |
|
return [ |
|
101
|
113 |
|
MIDCOM_TOOLBAR_LABEL => $this->l10n_midcom->get('delete'), |
|
102
|
113 |
|
MIDCOM_TOOLBAR_GLYPHICON => 'trash', |
|
103
|
113 |
|
MIDCOM_TOOLBAR_ACCESSKEY => 'd', |
|
104
|
113 |
|
MIDCOM_TOOLBAR_OPTIONS => [ |
|
105
|
113 |
|
'data-dialog' => 'delete', |
|
106
|
113 |
|
'data-form-id' => $this->form_identifier, |
|
107
|
113 |
|
'data-dialog-heading' => $this->l10n_midcom->get('confirm delete'), |
|
108
|
113 |
|
'data-dialog-text' => $dialog_text, |
|
109
|
113 |
|
'data-dialog-cancel-label' => $this->l10n_midcom->get('cancel'), |
|
110
|
113 |
|
'data-recursive' => $this->recursive ? 'true' : 'false', |
|
111
|
113 |
|
'data-guid' => $this->object->guid, |
|
112
|
113 |
|
'data-relocate' => $this->relocate |
|
113
|
113 |
|
] |
|
114
|
113 |
|
]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
18 |
|
public function is_confirmed(Request $request) : bool |
|
118
|
|
|
{ |
|
119
|
18 |
|
return (bool) $request->request->get($this->form_identifier); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
17 |
|
public function run(Request $request) : Response |
|
123
|
|
|
{ |
|
124
|
17 |
|
$this->object->require_do('midgard:delete'); |
|
125
|
17 |
|
$url = $request->request->get('referrer', $this->success_url); |
|
126
|
|
|
|
|
127
|
17 |
|
if ($this->is_confirmed($request)) { |
|
128
|
1 |
|
$method = $this->recursive ? 'delete_tree' : 'delete'; |
|
129
|
1 |
|
$message = ['title' => $this->l10n_midcom->get('midcom'), 'type' => 'info']; |
|
130
|
1 |
|
if ($this->object->{$method}()) { |
|
131
|
1 |
|
$this->state = static::SUCCESS; |
|
132
|
1 |
|
$url = $this->success_url; |
|
133
|
1 |
|
$message['message'] = sprintf($this->l10n_midcom->get("%s deleted"), $this->label); |
|
134
|
|
|
} else { |
|
135
|
|
|
$this->state = static::FAILURE; |
|
136
|
|
|
$message['message'] = sprintf($this->l10n_midcom->get("failed to delete %s: %s"), $this->label, midcom_connection::get_error_string()); |
|
137
|
|
|
$message['type'] = 'error'; |
|
138
|
|
|
} |
|
139
|
1 |
|
if (!$this->relocate) { |
|
140
|
|
|
return new JsonResponse($message); |
|
141
|
|
|
} |
|
142
|
1 |
|
midcom::get()->uimessages->add($message['title'], $message['message'], $message['type']); |
|
143
|
|
|
} |
|
144
|
17 |
|
return new midcom_response_relocate($url); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|