Issues (808)

src/midcom/datamanager/controller.php (2 issues)

Labels
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager;
7
8
use midcom;
9
use midcom_connection;
10
use midcom_error;
11
use Symfony\Component\Form\Form;
12
use midcom\datamanager\storage\container\dbacontainer;
13
use Symfony\Component\Form\FormInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * Experimental controller class
18
 */
19
class controller
20
{
21
    const EDIT = 'edit';
22
    const SAVE = 'save';
23
    const CANCEL = 'cancel';
24
    const DELETE = 'delete';
25
    const PREVIEW = 'preview';
26
    const NEXT = 'next';
27
    const PREVIOUS = 'previous';
28
29
    private Form $form;
30
31
    private datamanager $dm;
32
33 107
    public function __construct(datamanager $dm, ?string $name = null)
34
    {
35 107
        $this->dm = $dm;
36 107
        $this->form = $dm->get_form($name);
37
    }
38
39
    /**
40
     * Process the form
41
     *
42
     * @return string The processing result
43
     */
44 107
    public function handle(Request $request) : string
45
    {
46 107
        $operation = self::EDIT;
47
48 107
        $storage = $this->dm->get_storage();
49 107
        if ($request->request->has('midcom_datamanager_unlock')) {
50
            if (!$storage->unlock()) {
51
                $l10n = midcom::get()->i18n->get_l10n('midcom.datamanager');
52
                midcom::get()->uimessages->add($l10n->get('midcom.datamanager'), sprintf($l10n->get('failed to unlock, reason %s'), midcom_connection::get_error_string()), 'error');
53
            }
54 107
        } elseif (!$this->form->isSubmitted()) {
55 107
            $this->form->handleRequest($request);
56 107
            if (   $this->form->isSubmitted()
57 107
                && $button = $this->form->getClickedButton()) {
58 17
                $operation = $button->getConfig()->getOption('operation');
59
            }
60
        }
61
62 107
        if (   in_array($operation, [self::SAVE, self::NEXT])
63 107
            && !$this->form->isValid()) {
64
            $operation = self::EDIT;
65
        }
66 107
        if ($operation == self::SAVE) {
67 16
            $storage->save();
68
        }
69
70 107
        if (in_array($operation, [self::CANCEL, self::SAVE])) {
71 17
            $storage->unlock();
72
        } else {
73 105
            $storage->lock();
74
        }
75 107
        $storage->set_last_operation($operation);
76
77 107
        return $operation;
78
    }
79
80
    /**
81
     * Process the form (request object will be created on the fly)
82
     *
83
     * @deprecated Use handle() instead
84
     */
85
    public function process() : string
86
    {
87
        return $this->handle(Request::createFromGlobals());
88
    }
89
90 77
    public function get_datamanager() : datamanager
91
    {
92 77
        return $this->dm;
93
    }
94
95 3
    public function get_errors() : array
96
    {
97 3
        $errors = [];
98 3
        foreach ($this->form as $child) {
99 3
            $messages = '';
100 3
            foreach ($child->getErrors(true) as $error) {
101
                $messages .= $error->getMessage();
102
            }
103 3
            if (!empty($messages)) {
104
                $errors[$child->getName()] = $messages;
105
            }
106
        }
107 3
        $messages = '';
108 3
        foreach ($this->form->getErrors() as $error) {
109
            $messages .= $error->getMessage();
0 ignored issues
show
The method getMessage() does not exist on Symfony\Component\Form\FormErrorIterator. ( Ignorable by Annotation )

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

109
            $messages .= $error->/** @scrutinizer ignore-call */ getMessage();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
        }
111 3
        if (!empty($messages)) {
112
            $errors[] = $messages;
113
        }
114 3
        return $errors;
115
    }
116
117 6
    public function get_form_values()
118
    {
119 6
        if (!$this->form->isSubmitted()) {
120
            throw new midcom_error('form is not submitted');
121
        }
122 6
        return $this->form->getData();
123
    }
124
125 104
    public function display_form()
126
    {
127 104
        $storage = $this->dm->get_storage();
128 104
        if ($storage->is_locked()) {
129 1
            midcom::get()->style->data['handler'] = $this;
130 1
            midcom::get()->style->show_midcom('midcom_datamanager_unlock');
131
        } else {
132 103
            if ($storage instanceof dbacontainer && $this->form->isSubmitted()) {
133
                // This is either a save without relocate or a validation error
134
                if ($storage->get_last_operation() === self::SAVE) {
135
                    // Get GUIDs and such for new-created dependent objects
136
                    $this->dm->set_storage($storage->get_value(), $this->dm->get_schema()->get_name());
137
                } elseif (   !$this->form->isValid()
138
                          && $storage->move_uploaded_files() > 0) {
139
                    $form = $this->dm->get_form($this->form->getName(), true);
140
                    $this->copy_errors($this->form, $form);
141
                }
142
            }
143
144 103
            $renderer = $this->dm->get_renderer('form');
145 103
            echo $renderer->block($renderer->get_view(), 'form');
146
        }
147
    }
148
149
    private function copy_errors(FormInterface $from, FormInterface $to)
150
    {
151
        foreach ($from->getErrors() as $error) {
152
            $to->addError($error);
0 ignored issues
show
It seems like $error can also be of type Symfony\Component\Form\FormErrorIterator; however, parameter $error of Symfony\Component\Form\FormInterface::addError() does only seem to accept Symfony\Component\Form\FormError, maybe add an additional type check? ( Ignorable by Annotation )

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

152
            $to->addError(/** @scrutinizer ignore-type */ $error);
Loading history...
153
        }
154
155
        foreach ($from->all() as $key => $child) {
156
            if (!$to->has($key)) {
157
                $config = $child->getConfig();
158
                $to->add($child->getName(), get_class($config->getType()->getInnerType()), $config->getOptions());
159
            }
160
161
            $this->copy_errors($child, $to->get($key));
162
        }
163
    }
164
165
    public function display_view()
166
    {
167
        $this->dm->display_view();
168
    }
169
}
170