Completed
Push — master ( a04700...b64531 )
by Andreas
31:59 queued 10:47
created

datamanager::get_button_config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
ccs 6
cts 6
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
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 = null;
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 ($url === null) {
122 5
            return 'undefined';
123
        }
124
125 12
        if (   !str_starts_with($url, '/')
126 12
            && ! preg_match('|^https?://|', $url)) {
127 3
            $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

127
            $url = /** @scrutinizer ignore-type */ midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX) . $url;
Loading history...
128
        }
129 12
        return '"' . $url . '"';
130
    }
131
}
132