Passed
Push — master ( 9727f8...a3fb64 )
by Andreas
22:43
created

controller::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
23
    const SAVE = 'save';
24
25
    const CANCEL = 'cancel';
26
27
    const DELETE = 'delete';
28
29
    const PREVIEW = 'preview';
30
31
    const NEXT = 'next';
32
33
    const PREVIOUS = 'previous';
34
35
    /**
36
     *
37
     * @var Form
38
     */
39
    private $form;
40
41
    /**
42
     *
43
     * @var datamanager
44
     */
45
    private $dm;
46
47 104
    public function __construct(datamanager $dm, $name = null)
48
    {
49 104
        $this->dm = $dm;
50 104
        $this->form = $dm->get_form($name);
51 104
    }
52
53
    /**
54
     * Process the form
55
     *
56
     * @param Request $request
57
     * @return string The processing result
58
     */
59 104
    public function handle(Request $request) : string
60
    {
61 104
        $operation = self::EDIT;
62
63 104
        $storage = $this->dm->get_storage();
64 104
        if ($request->request->has('midcom_datamanager_unlock')) {
65
            if (!$storage->unlock()) {
66
                $l10n = midcom::get()->i18n->get_l10n('midcom.datamanager');
67
                midcom::get()->uimessages->add($l10n->get('midcom.datamanager'), sprintf($l10n->get('failed to unlock, reason %s'), midcom_connection::get_error_string()), 'error');
68
            }
69 104
        } elseif (!$this->form->isSubmitted()) {
70 104
            $this->form->handleRequest($request);
71 104
            if (   $this->form->isSubmitted()
72 104
                && $button = $this->form->getClickedButton()) {
73 16
                $operation = $button->getConfig()->getOption('operation');
74
            }
75
        }
76
77 104
        if (   in_array($operation, [self::SAVE, self::NEXT])
78 104
            && !$this->form->isValid()) {
79
            $operation = self::EDIT;
80
        }
81 104
        if ($operation == self::SAVE) {
82 15
            $storage->save();
83
        }
84
85 104
        if (in_array($operation, [self::CANCEL, self::SAVE])) {
86 16
            $storage->unlock();
87
        } else {
88 102
            $storage->lock();
89
        }
90 104
        $storage->set_last_operation($operation);
91
92 104
        return $operation;
93
    }
94
95
    /**
96
     * Process the form (request object will be created on the fly)
97
     *
98
     * @deprecated Use handle() instead
99
     */
100
    public function process() : string
101
    {
102
        return $this->handle(Request::createFromGlobals());
103
    }
104
105 22
    public function get_datamanager() : datamanager
106
    {
107 22
        return $this->dm;
108
    }
109
110 4
    public function get_errors() : array
111
    {
112 4
        $errors = [];
113 4
        foreach ($this->form as $child) {
114 4
            $messages = '';
115 4
            foreach ($child->getErrors(true) as $error) {
116
                $messages .= $error->getMessage();
117
            }
118 4
            if (!empty($messages)) {
119
                $errors[$child->getName()] = $messages;
120
            }
121
        }
122 4
        $messages = '';
123 4
        foreach ($this->form->getErrors() as $error) {
124
            $messages .= $error->getMessage();
0 ignored issues
show
Bug introduced by
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

124
            $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...
125
        }
126 4
        if (!empty($messages)) {
127
            $errors[] = $messages;
128
        }
129 4
        return $errors;
130
    }
131
132 5
    public function get_form_values()
133
    {
134 5
        if (!$this->form->isSubmitted()) {
135
            throw new midcom_error('form is not submitted');
136
        }
137 5
        return $this->form->getData();
138
    }
139
140 101
    public function display_form()
141
    {
142 101
        $storage = $this->dm->get_storage();
143 101
        if ($storage->is_locked()) {
144 1
            midcom::get()->style->data['handler'] = $this;
145 1
            midcom::get()->style->show_midcom('midcom_datamanager_unlock');
146
        } else {
147 100
            if ($storage instanceof dbacontainer && $this->form->isSubmitted()) {
148
                // This is either a save without relocate or a validation error
149
                if ($storage->get_last_operation() === self::SAVE) {
150
                    // Get GUIDs and such for new-created dependent objects
151
                    $this->dm->set_storage($storage->get_value(), $this->dm->get_schema()->get_name());
152
                } elseif (   !$this->form->isValid()
153
                          && $storage->move_uploaded_files() > 0) {
154
                    $form = $this->dm->get_form($this->form->getName(), true);
155
                    $this->copy_errors($this->form, $form);
156
                }
157
            }
158
159 100
            $renderer = $this->dm->get_renderer('form');
160 100
            echo $renderer->block($renderer->get_view(), 'form');
161
        }
162 101
    }
163
164
    private function copy_errors(FormInterface $from, FormInterface $to)
165
    {
166
        foreach ($from->getErrors() as $error) {
167
            $to->addError($error);
0 ignored issues
show
Bug introduced by
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

167
            $to->addError(/** @scrutinizer ignore-type */ $error);
Loading history...
168
        }
169
170
        foreach ($from->all() as $key => $child) {
171
            $this->copy_errors($child, $to->get($key));
172
        }
173
    }
174
175
    public function display_view()
176
    {
177
        $this->dm->display_view();
178
    }
179
}
180