Issues (801)

lib/midcom/admin/folder/handler/move.php (5 issues)

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 Symfony\Component\HttpFoundation\Request;
10
11
/**
12
 * Move handler.
13
 *
14
 * @package midcom.admin.folder
15
 */
16
class midcom_admin_folder_handler_move extends midcom_baseclasses_components_handler
17
{
18
    private midcom_core_dbaobject $_object;
19
20
    private midcom_db_topic $current_folder;
21
22
    /**
23
     * Handler for folder move. Checks for updating permissions, initializes
24
     * the move and the content topic itself. Handles also the sent form.
25
     */
26 1
    public function _handler_move(Request $request, string $guid, array &$data)
27
    {
28 1
        $this->_object = midcom::get()->dbfactory->get_object_by_guid($guid);
29
30 1
        if (   !($this->_object instanceof midcom_db_topic)
31 1
            && !($this->_object instanceof midcom_db_article)) {
32
            throw new midcom_error_notfound("Moving only topics and articles is supported.");
33
        }
34
35 1
        $this->_object->require_do('midgard:update');
36
37 1
        if ($request->request->has('move_to')) {
38
            try {
39
                $target = new midcom_db_topic($request->request->getInt('move_to'));
40
                $this->_move_object($target);
41
                midcom::get()->uimessages->add($this->_l10n->get($this->_component), sprintf($this->_l10n->get('moved %s to %s'), $this->_topic->get_label(), $target->get_label()));
42
            } catch (midcom_error $e) {
43
                midcom::get()->uimessages->add($this->_l10n->get($this->_component), $e->getMessage(), 'error');
44
            }
45
        }
46
47 1
        $object_label = midcom_helper_reflector::get($this->_object)->get_object_label($this->_object);
48
49 1
        if ($this->_object instanceof midcom_db_topic) {
50
            // This is a topic
51 1
            $this->_object->require_do('midcom.admin.folder:topic_management');
52 1
            $this->current_folder = new midcom_db_topic($this->_object->up);
0 ignored issues
show
Bug Best Practice introduced by
The property up does not exist on midcom_core_dbaobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
53
        } else {
54
            // This is a regular object
55
            $this->current_folder = new midcom_db_topic($this->_object->topic);
0 ignored issues
show
Bug Best Practice introduced by
The property topic does not exist on midcom_core_dbaobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
56
        }
57 1
        $data['handler'] = $this;
58
59 1
        midcom::get()->head->set_pagetitle(sprintf($this->_l10n->get('move %s'), $object_label));
60
61 1
        $this->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.admin.folder/folder.css');
62 1
        return $this->get_workflow('viewer')->run($request);
63
    }
64
65 1
    public function show_tree(?midcom_db_topic $folder = null, bool $tree_disabled = false)
66
    {
67 1
        $folder ??= midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ROOTTOPIC);
68
69 1
        if (   $this->_object instanceof midcom_db_topic
70 1
            && $folder->up == $this->_object->id) {
71
            $tree_disabled = true;
72
        }
73
74 1
        $class = '';
75 1
        $selected = '';
76 1
        $disabled = '';
77 1
        if ($folder->guid == $this->current_folder->guid) {
78
            $class = 'current';
79
            $selected = ' checked="checked"';
80
        }
81
82 1
        if (   !($this->_object instanceof midcom_db_topic)
83 1
            && $folder->component !== $this->current_folder->component) {
84
            // Non-topic objects may only be moved under folders of same component
85
            $class = 'wrong_component';
86
            $disabled = ' disabled="disabled"';
87
        }
88
89 1
        if ($tree_disabled) {
90
            $class = 'child';
91
            $disabled = ' disabled="disabled"';
92
        }
93
94 1
        if ($folder->guid == $this->_object->guid) {
95
            $class = 'self';
96
            $disabled = ' disabled="disabled"';
97
        }
98 1
        echo "<ul>\n";
99 1
        echo "<li class=\"{$class}\"><label><input{$selected}{$disabled} type=\"radio\" name=\"move_to\" value=\"{$folder->id}\" /> " . $folder->get_label() . "</label>\n";
100
101 1
        $qb = midcom_db_topic::new_query_builder();
102 1
        $qb->add_constraint('up', '=', $folder->id);
103 1
        $qb->add_constraint('component', '<>', '');
104
105 1
        foreach ($qb->execute() as $child) {
106
            $this->show_tree($child, $tree_disabled);
107
        }
108 1
        echo "</li>\n";
109 1
        echo "</ul>\n";
110
    }
111
112
    private function _move_object(midcom_db_topic $target)
113
    {
114
        $target->require_do('midgard:create');
115
116
        if ($this->_object instanceof midcom_db_topic) {
117
            $name = $this->_object->name;
118
            $this->_object->name = ''; // Prevents problematic location to break the site, we set this back below...
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
119
            $this->_object->up = $target->id;
0 ignored issues
show
Bug Best Practice introduced by
The property up does not exist on midcom_core_dbaobject. Since you implemented __set, consider adding a @property annotation.
Loading history...
120
            if (!$this->_object->update()) {
121
                throw new midcom_error('Failed to move the topic, reason ' . midcom_connection::get_error_string());
122
            }
123
            // It was ok, so set name back now
124
            $this->_object->name = $name;
125
            $this->_object->update();
126
        } else {
127
            $this->_object->topic = $target->id;
0 ignored issues
show
Bug Best Practice introduced by
The property topic does not exist on midcom_core_dbaobject. Since you implemented __set, consider adding a @property annotation.
Loading history...
128
            if (!$this->_object->update()) {
129
                throw new midcom_error('Failed to move the article, reason ' . midcom_connection::get_error_string());
130
            }
131
        }
132
    }
133
134
    /**
135
     * Output the style element for move editing
136
     */
137 1
    public function _show_move(string $handler_id, array &$data)
138
    {
139 1
        midcom_show_style('midcom-admin-show-folder-move');
140
    }
141
}
142