Passed
Push — master ( f61cc5...ab3fce )
by Andreas
33:36
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
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 146
    public function get_button_config() : array
59
    {
60
        return [
61 146
            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_l10n('midcom')->get('edit'),
62 146
            MIDCOM_TOOLBAR_GLYPHICON => 'pencil',
63 146
            MIDCOM_TOOLBAR_OPTIONS => [
64 146
                'data-dialog' => 'dialog',
65 146
                '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
        if ($this->relocate) {
86 9
            $url = '';
87 9
            if (is_callable($this->save_callback)) {
88 8
                $url = call_user_func($this->save_callback, $this->controller);
89 8
                if ($url !== null) {
90 4
                    $url = $this->prepare_url($url);
91
                }
92
            }
93 9
            return 'refresh_opener(' . $url . ');';
94
        }
95 2
        $dm = $this->controller->get_datamanager();
96 2
        $object = $dm->get_storage()->get_value();
97 2
        if ($object instanceof \midcom_core_dbaobject) {
98
            // we rebuild the form so that newly created child objects are listed with their proper DB identifiers
99 2
            $dm->set_storage($object);
100 2
            $data = $dm->get_content_html();
101 2
            $data['guid'] = $object->guid;
102
        } else {
103
            $data = $dm->get_content_html();
104
        }
105 2
        return 'close(' . json_encode($data) . ');';
106
    }
107
108 3
    public function add_post_button($url, $label, array $args)
109
    {
110 3
        $this->add_dialog_js();
111 3
        midcom::get()->head->add_jscript('add_post_button(' . $this->prepare_url($url) . ', "' . $label . '", ' . json_encode($args) . ');');
112 3
    }
113
114 5
    public function add_dialog_button(dialog $dialog, $url)
115
    {
116 5
        $config = $dialog->get_button_config();
117 5
        $this->add_dialog_js();
118 5
        midcom::get()->head->add_jscript('add_dialog_button(' . $this->prepare_url($url) . ', "' . $config[MIDCOM_TOOLBAR_LABEL] . '", ' . json_encode($config[MIDCOM_TOOLBAR_OPTIONS]) . ');');
119 5
    }
120
121 12
    private function prepare_url(string $url) : string
122
    {
123 12
        if (   substr($url, 0, 1) != '/'
124 12
            && ! preg_match('|^https?://|', $url)) {
125 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

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