|
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_core_context; |
|
13
|
|
|
use midcom; |
|
14
|
|
|
use midcom\datamanager\controller; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @package midcom.workflow |
|
20
|
|
|
*/ |
|
21
|
|
|
class datamanager extends dialog |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
* @var \midcom\datamanager\controller |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $controller; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* |
|
31
|
|
|
* @var callable |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $save_callback; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Disable relocate after execute |
|
37
|
|
|
* |
|
38
|
|
|
* Returns the uimessage as JSON instead |
|
39
|
|
|
* |
|
40
|
|
|
* @var boolean |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $relocate; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
209 |
|
public function configure(OptionsResolver $resolver) |
|
48
|
|
|
{ |
|
49
|
|
|
$resolver |
|
50
|
209 |
|
->setDefaults([ |
|
51
|
209 |
|
'controller' => null, |
|
52
|
|
|
'save_callback' => null, |
|
53
|
|
|
'relocate' => true |
|
54
|
|
|
]) |
|
55
|
209 |
|
->setAllowedTypes('controller', ['null', controller::class]); |
|
56
|
209 |
|
} |
|
57
|
|
|
|
|
58
|
145 |
|
public function get_button_config() : array |
|
59
|
|
|
{ |
|
60
|
|
|
return [ |
|
61
|
145 |
|
MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_l10n('midcom')->get('edit'), |
|
62
|
145 |
|
MIDCOM_TOOLBAR_GLYPHICON => 'pencil', |
|
63
|
145 |
|
MIDCOM_TOOLBAR_OPTIONS => [ |
|
64
|
145 |
|
'data-dialog' => 'dialog', |
|
65
|
145 |
|
'data-dialog-cancel-label' => midcom::get()->i18n->get_l10n('midcom')->get('cancel') |
|
66
|
|
|
] |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
69 |
|
public function run(Request $request) : Response |
|
71
|
|
|
{ |
|
72
|
69 |
|
$this->state = $this->controller->handle($request); |
|
73
|
|
|
|
|
74
|
69 |
|
if ($this->state == controller::SAVE) { |
|
75
|
12 |
|
$script = $this->handle_save(); |
|
76
|
12 |
|
return $this->js_response($script); |
|
77
|
|
|
} |
|
78
|
69 |
|
$context = midcom_core_context::get(); |
|
79
|
69 |
|
$context->set_key(MIDCOM_CONTEXT_SHOWCALLBACK, [$this->controller, 'display_form']); |
|
80
|
69 |
|
return $this->response($context); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
11 |
|
protected function handle_save() : string |
|
84
|
|
|
{ |
|
85
|
11 |
|
$dm = $this->controller->get_datamanager(); |
|
86
|
11 |
|
$object = $dm->get_storage()->get_value(); |
|
87
|
11 |
|
if ($object instanceof \midcom_core_dbaobject) { |
|
88
|
|
|
// we rebuild the form so that newly created child objects are listed with their proper DB identifiers |
|
89
|
11 |
|
$dm->set_storage($object); |
|
90
|
11 |
|
$data = $dm->get_content_html(); |
|
91
|
11 |
|
$data['guid'] = $object->guid; |
|
92
|
|
|
} else { |
|
93
|
|
|
$data = $dm->get_content_html(); |
|
94
|
|
|
} |
|
95
|
11 |
|
if ($this->relocate) { |
|
96
|
9 |
|
$url = ''; |
|
97
|
9 |
|
if (is_callable($this->save_callback)) { |
|
98
|
8 |
|
$url = call_user_func($this->save_callback, $this->controller) ?? ''; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
9 |
|
return 'refresh_opener(' . $this->prepare_url($url) . ', ' . json_encode($data) . ');'; |
|
102
|
|
|
} |
|
103
|
2 |
|
return 'close(' . json_encode($data) . ');'; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
3 |
|
public function add_post_button(string $url, string $label, array $args) |
|
107
|
|
|
{ |
|
108
|
3 |
|
$this->add_dialog_js(); |
|
109
|
3 |
|
midcom::get()->head->add_jscript('add_post_button(' . $this->prepare_url($url) . ', "' . $label . '", ' . json_encode($args) . ');'); |
|
110
|
3 |
|
} |
|
111
|
|
|
|
|
112
|
5 |
|
public function add_dialog_button(dialog $dialog, string $url) |
|
113
|
|
|
{ |
|
114
|
5 |
|
$config = $dialog->get_button_config(); |
|
115
|
5 |
|
$this->add_dialog_js(); |
|
116
|
5 |
|
midcom::get()->head->add_jscript('add_dialog_button(' . $this->prepare_url($url) . ', "' . $config[MIDCOM_TOOLBAR_LABEL] . '", ' . json_encode($config[MIDCOM_TOOLBAR_OPTIONS]) . ');'); |
|
117
|
5 |
|
} |
|
118
|
|
|
|
|
119
|
17 |
|
private function prepare_url(string $url) : string |
|
120
|
|
|
{ |
|
121
|
17 |
|
if ( !str_starts_with($url, '/') |
|
122
|
17 |
|
&& ! preg_match('|^https?://|', $url)) { |
|
123
|
8 |
|
$url = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . $url; |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
17 |
|
return '"' . $url . '"'; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|