Issues (803)

lib/midcom/admin/folder/handler/edit.php (2 issues)

Labels
Severity
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
    private controller $_controller;
22
23
    private string $_handler_id;
24
25
    private string $old_name;
26
27
    private midcom_db_topic $edit_topic;
28
29 2
    private function _load_controller()
30
    {
31
        // Get the configured schemas
32 2
        $schemadbs = $this->_config->get_array('schemadbs_folder');
33
34
        // Check if a custom schema exists
35 2
        if (array_key_exists($this->_topic->component, $schemadbs)) {
36
            $schemadb = $this->_topic->component;
37
        } else {
38 2
            $schemadb = 'default';
39
        }
40
41 2
        if (!array_key_exists($schemadb, $schemadbs)) {
42
            throw new midcom_error('Configuration error. No ' . $schemadb . ' schema for topic has been defined!');
43
        }
44
45 2
        $schemadb = schemadb::from_path($schemadbs[$schemadb]);
46
47 2
        foreach ($schemadb->all() as $schema) {
48 2
            if ($schema->has_field('name')) {
49 2
                $schema->get_field('name')['required'] = $this->_handler_id === 'edit';
50
            }
51
        }
52 2
        $defaults = [];
53 2
        if ($this->_handler_id == 'create') {
54
            // Suggest to create the same type of a folder as the parent is
55
            // Unless config told us otherwise
56 1
            $defaults['component'] = $this->_config->get('default_component') ?: $this->_topic->component;
57
        }
58
59 2
        $dm = new datamanager($schemadb);
60 2
        $this->_controller = $dm
61 2
            ->set_defaults($defaults)
62 2
            ->set_storage($this->edit_topic)
63 2
            ->get_controller();
64
    }
65
66
    /**
67
     * Handler for folder editing. Checks for the permissions and folder integrity.
68
     */
69 2
    public function _handler_edit(Request $request, string $handler_id)
70
    {
71 2
        $this->_topic->require_do('midcom.admin.folder:topic_management');
72 2
        $this->_handler_id = $handler_id;
73
74 2
        if ($this->_handler_id == 'edit') {
75 1
            $this->_topic->require_do('midgard:update');
76 1
            $title = sprintf($this->_l10n->get('edit folder %s'), $this->_topic->get_label());
77 1
            $this->edit_topic = $this->_topic;
78
        } else {
79 1
            $this->_topic->require_do('midgard:create');
80 1
            $title = $this->_l10n->get('create folder');
81 1
            $this->edit_topic = new midcom_db_topic();
82 1
            $this->edit_topic->up = $this->_topic->id;
83
        }
84
85 2
        $this->_load_controller();
86
87
        // Store the old name before editing
88 2
        $this->old_name = $this->_topic->name;
89
90 2
        $this->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.admin.folder/folder.css');
91 2
        midcom::get()->head->set_pagetitle($title);
92
93 2
        $workflow = $this->get_workflow('datamanager', [
94 2
            'controller' => $this->_controller,
95 2
            'save_callback' => $this->save_callback(...)
96 2
        ]);
97 2
        return $workflow->run($request);
98
    }
99
100 1
    public function save_callback() : ?string
101
    {
102 1
        $prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX);
103 1
        if ($this->_handler_id === 'edit') {
104 1
            return $this->_update_topic($prefix, $this->old_name);
0 ignored issues
show
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

104
            return $this->_update_topic(/** @scrutinizer ignore-type */ $prefix, $this->old_name);
Loading history...
105
        }
106
        return $this->_create_topic($prefix);
0 ignored issues
show
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

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