Passed
Push — master ( 38457e...3f9740 )
by Andreas
37:47
created

midcom_admin_folder_handler_edit::save_callback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
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_array('schemadbs_folder');
41
42
        // Check if a custom schema exists
43 2
        if (array_key_exists($this->_topic->component, $schemadbs)) {
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
            // Unless config told us otherwise
64 1
            $defaults['component'] = $this->_config->get('default_component') ?: $this->_topic->component;
65
        }
66
67 2
        $dm = new datamanager($schemadb);
68 2
        $this->_controller = $dm
69 2
            ->set_defaults($defaults)
70 2
            ->set_storage($this->edit_topic)
71 2
            ->get_controller();
72 2
    }
73
74
    /**
75
     * Handler for folder editing. Checks for the permissions and folder integrity.
76
     */
77 2
    public function _handler_edit(Request $request, string $handler_id)
78
    {
79 2
        $this->_topic->require_do('midcom.admin.folder:topic_management');
80 2
        $this->_handler_id = $handler_id;
81
82 2
        if ($this->_handler_id == 'edit') {
83 1
            $this->_topic->require_do('midgard:update');
84 1
            $title = sprintf($this->_l10n->get('edit folder %s'), $this->_topic->get_label());
85 1
            $this->edit_topic = $this->_topic;
86
        } else {
87 1
            $this->_topic->require_do('midgard:create');
88 1
            $title = $this->_l10n->get('create folder');
89 1
            $this->edit_topic = new midcom_db_topic();
90 1
            $this->edit_topic->up = $this->_topic->id;
91
        }
92
93 2
        $this->_load_controller();
94
95
        // Store the old name before editing
96 2
        $this->old_name = $this->_topic->name;
97
98 2
        $this->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.admin.folder/folder.css');
99 2
        midcom::get()->head->set_pagetitle($title);
100
101 2
        $workflow = $this->get_workflow('datamanager', [
102 2
            'controller' => $this->_controller,
103 2
            'save_callback' => [$this, 'save_callback']
104
        ]);
105 2
        return $workflow->run($request);
106
    }
107
108
    public function save_callback() : ?string
109
    {
110
        $prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
111
        if ($this->_handler_id === 'edit') {
112
            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

112
            return $this->_update_topic(/** @scrutinizer ignore-type */ $prefix, $this->old_name);
Loading history...
113
        }
114
        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

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