Passed
Push — master ( c9d642...884122 )
by Andreas
13:19 queued 03:13
created

datamanager::add_post_button()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
use midcom\datamanager\storage\container\dbacontainer;
18
19
/**
20
 * @package midcom.workflow
21
 */
22
class datamanager extends dialog
23
{
24
    /**
25
     * @var controller
26
     */
27
    protected $controller;
28
29
    /**
30
     * @var callable
31
     */
32
    protected $save_callback;
33
34
    /**
35
     * Disable relocate after execute
36
     *
37
     * Returns the uimessage as JSON instead
38
     */
39
    protected bool $relocate;
40
41
    protected ?string $style = null;
42
43
    protected string $label;
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 218
    public function configure(OptionsResolver $resolver)
49
    {
50 218
        $resolver
51 218
            ->setDefaults([
52 218
                'controller' => null,
53 218
                'save_callback' => null,
54 218
                'relocate' => true,
55 218
                'style' => null,
56 218
                'label' => midcom::get()->i18n->get_string('edit', 'midcom')
57 218
            ])
58 218
            ->setAllowedTypes('controller', ['null', controller::class]);
59
    }
60
61 147
    public function get_button_config() : array
62
    {
63 147
        return [
64 147
            MIDCOM_TOOLBAR_LABEL => $this->label,
65 147
            MIDCOM_TOOLBAR_GLYPHICON => 'pencil',
66 147
            MIDCOM_TOOLBAR_OPTIONS => [
67 147
                'data-dialog' => 'dialog',
68 147
                'data-dialog-cancel-label' => midcom::get()->i18n->get_string('cancel', 'midcom')
69 147
            ]
70 147
        ];
71
    }
72
73 73
    public function run(Request $request) : Response
74
    {
75 73
        $this->state = $this->controller->handle($request);
76
77 73
        if ($this->state == controller::SAVE) {
78 13
            $script = $this->handle_save();
79 13
            return $this->js_response($script);
80
        }
81 73
        $context = midcom_core_context::get();
82 73
        if ($style = $this->style) {
83
            $callback = function() use ($style) {
84
                midcom::get()->style->show($style);
85
            };
86
        } else {
87 73
            $storage = $this->controller->get_datamanager()->get_storage();
88 73
            if ($storage instanceof dbacontainer) {
89 68
                midcom::get()->head->add_jscript('var DM_OBJECT_GUID = "' . $storage->get_value()->guid . '";');
90
            }
91 73
            $callback = [$this->controller, 'display_form'];
92
        }
93 73
        $context->set_key(MIDCOM_CONTEXT_SHOWCALLBACK, $callback);
94 73
        return $this->response($context);
95
    }
96
97 12
    protected function handle_save() : string
98
    {
99 12
        $dm = $this->controller->get_datamanager();
100 12
        $object = $dm->get_storage()->get_value();
101 12
        if ($object instanceof \midcom_core_dbaobject) {
102
            // we rebuild the form so that newly created child objects are listed with their proper DB identifiers
103 12
            $dm->set_storage($object, $dm->get_schema()->get_name());
104 12
            $data = $dm->get_content_html();
105 12
            $data['guid'] = $object->guid;
106
        } else {
107
            $data = $dm->get_content_html();
108
        }
109 12
        if ($this->relocate) {
110 10
            $url = null;
111 10
            if (is_callable($this->save_callback)) {
112 9
                $url = call_user_func($this->save_callback, $this->controller);
113
            }
114
115 10
            return 'refresh_opener(' . $this->prepare_url($url) . ', ' . json_encode($data) . ');';
116
        }
117 2
        return 'close(' . json_encode($data) . ');';
118
    }
119
120 3
    public function add_post_button(string $url, string $label, array $args)
121
    {
122 3
        $this->add_dialog_js();
123 3
        midcom::get()->head->add_jscript('add_post_button(' . $this->prepare_url($url) . ', "' . $label . '", ' . json_encode($args) . ');');
124
    }
125
126 7
    public function add_dialog_button(dialog $dialog, string $url)
127
    {
128 7
        $config = $dialog->get_button_config();
129 7
        $this->add_dialog_js();
130 7
        midcom::get()->head->add_jscript('add_dialog_button(' . $this->prepare_url($url) . ', "' . $config[MIDCOM_TOOLBAR_LABEL] . '", ' . json_encode($config[MIDCOM_TOOLBAR_OPTIONS]) . ');');
131
    }
132
133 20
    private function prepare_url(?string $url) : string
134
    {
135 20
        if ($url === null) {
136 5
            return 'undefined';
137
        }
138
139 15
        if (   !str_starts_with($url, '/')
140 15
            && ! preg_match('|^https?://|', $url)) {
141 2
            $url = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . $url;
0 ignored issues
show
Bug introduced by
Are you sure midcom_core_context::get...M_CONTEXT_ANCHORPREFIX) of type false|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
            $url = /** @scrutinizer ignore-type */ midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . $url;
Loading history...
142
        }
143 15
        return '"' . $url . '"';
144
    }
145
}
146