Passed
Push — master ( e6f39d...2b19c5 )
by Andreas
28:29
created

midcom_admin_folder_handler_edit::_handler_edit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 19
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 29
ccs 19
cts 19
cp 1
crap 2
rs 9.6333
1
<?php
2
/**
3
 * @package midcom.admin.folder
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
use midcom\datamanager\datamanager;
10
use midcom\datamanager\schemadb;
11
use midcom\datamanager\controller;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * Handle the folder editing requests
16
 *
17
 * @package midcom.admin.folder
18
 */
19
class midcom_admin_folder_handler_edit extends midcom_baseclasses_components_handler
20
{
21
    /**
22
     * Controller instance
23
     *
24
     * @var controller
25
     */
26
    private $_controller;
27
28
    /**
29
     * ID of the handler
30
     */
31
    private $_handler_id;
32
33
    private $old_name;
34
35
    private $edit_topic;
36
37 2
    private function _load_controller()
38
    {
39
        // Get the configured schemas
40 2
        $schemadbs = $this->_config->get('schemadbs_folder');
41
42
        // Check if a custom schema exists
43 2
        if (array_key_exists($this->_topic->component, $schemadbs)) {
0 ignored issues
show
Bug introduced by
It seems like $schemadbs can also be of type false; however, parameter $array of array_key_exists() does only seem to accept ArrayObject|array, 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

43
        if (array_key_exists($this->_topic->component, /** @scrutinizer ignore-type */ $schemadbs)) {
Loading history...
44
            $schemadb = $this->_topic->component;
45
        } else {
46 2
            $schemadb = 'default';
47
        }
48
49 2
        if (!array_key_exists($schemadb, $schemadbs)) {
50
            throw new midcom_error('Configuration error. No ' . $schemadb . ' schema for topic has been defined!');
51
        }
52
53 2
        $schemadb = schemadb::from_path($schemadbs[$schemadb]);
54
55 2
        foreach ($schemadb->all() as $schema) {
56 2
            if ($schema->has_field('name')) {
57 2
                $schema->get_field('name')['required'] = $this->_handler_id === 'edit';
58
            }
59
        }
60 2
        $defaults = [];
61 2
        if ($this->_handler_id == 'create') {
62
            // Suggest to create the same type of a folder as the parent is
63 1
            $component_suggestion = $this->_topic->component;
64
65
            //Unless config told us otherwise
66 1
            if ($this->_config->get('default_component')) {
67
                $component_suggestion = $this->_config->get('default_component');
68
            }
69
70 1
            $defaults['component'] = $component_suggestion;
71
        }
72
73 2
        $dm = new datamanager($schemadb);
74 2
        $this->_controller = $dm
75 2
            ->set_defaults($defaults)
76 2
            ->set_storage($this->edit_topic)
77 2
            ->get_controller();
78 2
    }
79
80
    /**
81
     * Handler for folder editing. Checks for the permissions and folder integrity.
82
     */
83 2
    public function _handler_edit(Request $request, string $handler_id)
84
    {
85 2
        $this->_topic->require_do('midcom.admin.folder:topic_management');
86 2
        $this->_handler_id = $handler_id;
87
88 2
        if ($this->_handler_id == 'edit') {
89 1
            $this->_topic->require_do('midgard:update');
90 1
            $title = sprintf($this->_l10n->get('edit folder %s'), $this->_topic->get_label());
91 1
            $this->edit_topic = $this->_topic;
92
        } else {
93 1
            $this->_topic->require_do('midgard:create');
94 1
            $title = $this->_l10n->get('create folder');
95 1
            $this->edit_topic = new midcom_db_topic();
96 1
            $this->edit_topic->up = $this->_topic->id;
97
        }
98
99 2
        $this->_load_controller();
100
101
        // Store the old name before editing
102 2
        $this->old_name = $this->_topic->name;
103
104 2
        $this->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.admin.folder/folder.css');
105 2
        midcom::get()->head->set_pagetitle($title);
106
107 2
        $workflow = $this->get_workflow('datamanager', [
108 2
            'controller' => $this->_controller,
109 2
            'save_callback' => [$this, 'save_callback']
110
        ]);
111 2
        return $workflow->run($request);
112
    }
113
114
    public function save_callback() : ?string
115
    {
116
        $prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
117
        if ($this->_handler_id === 'edit') {
118
            return $this->_update_topic($prefix, $this->old_name);
0 ignored issues
show
Bug introduced by
It seems like $prefix can also be of type false; however, parameter $prefix of midcom_admin_folder_handler_edit::_update_topic() does only seem to accept string, 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

118
            return $this->_update_topic(/** @scrutinizer ignore-type */ $prefix, $this->old_name);
Loading history...
119
        }
120
        return $this->_create_topic($prefix);
0 ignored issues
show
Bug introduced by
It seems like $prefix can also be of type false; however, parameter $prefix of midcom_admin_folder_handler_edit::_create_topic() does only seem to accept string, 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

120
        return $this->_create_topic(/** @scrutinizer ignore-type */ $prefix);
Loading history...
121
    }
122
123
    private function _update_topic(string $prefix, string $old_name) : ?string
124
    {
125
        if (isset($this->_controller->get_form_values()['style']) && $this->_controller->get_form_values()['style'] == '__create') {
126
            $this->edit_topic->style = $this->_create_style($this->edit_topic->name);
127
128
            // Failed to create the new style template
129
            if ($this->edit_topic->style === '') {
130
                return null;
131
            }
132
133
            midcom::get()->uimessages->add($this->_l10n->get('midcom.admin.folder'), $this->_l10n->get('new style created'));
134
135
            if (!$this->edit_topic->update()) {
136
                midcom::get()->uimessages->add($this->_l10n->get('midcom.admin.folder'), sprintf($this->_l10n->get('could not save folder: %s'), midcom_connection::get_error_string()));
137
                return null;
138
            }
139
        }
140
141
        midcom::get()->uimessages->add($this->_l10n->get('midcom.admin.folder'), $this->_l10n->get('folder saved'));
142
143
        // Get the relocation url
144
        if (midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ROOTTOPIC)->id === $this->edit_topic->id) {
145
            return $prefix;
146
        }
147
        return preg_replace("/{$old_name}\/\$/", "{$this->edit_topic->name}/", $prefix);
148
    }
149
150
    private function _create_topic(string $prefix) : string
151
    {
152
        midcom::get()->uimessages->add($this->_l10n->get('midcom.admin.folder'), $this->_l10n->get('folder created'));
153
154
        // Generate name if it is missing
155
        if (!$this->edit_topic->name) {
156
            $this->edit_topic->name = midcom_helper_misc::urlize($this->edit_topic->extra);
157
            $this->edit_topic->update();
158
        }
159
160
        // Get the relocation url
161
        return "{$prefix}{$this->edit_topic->name}/";
162
    }
163
164
    /**
165
     * Create a new style for the topic
166
     *
167
     * @param string $style_name Name of the style
168
     * @return string Style path
169
     */
170
    private function _create_style(string $style_name) : string
171
    {
172
        $style = new midcom_db_style();
173
        $style->name = $style_name;
174
175
        if ($inherited = midcom_core_context::get()->get_inherited_style()) {
176
            $style->up = midcom_db_style::id_from_path($inherited);
177
            debug_add("Style inherited from {$inherited}");
178
        }
179
180
        if (!$style->create()) {
181
            debug_print_r('Failed to create a new style due to ' . midcom_connection::get_error_string(), $style, MIDCOM_LOG_WARN);
182
183
            midcom::get()->uimessages->add($this->_l10n->get('edit folder'), sprintf($this->_l10n->get('failed to create a new style template: %s'), midcom_connection::get_error_string()), 'error');
184
            return '';
185
        }
186
187
        debug_print_r('New style created', $style);
188
189
        return midcom_db_style::path_from_id($style->id);
190
    }
191
}
192