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
|
|
|
* @var controller |
23
|
|
|
*/ |
24
|
|
|
private $_controller; |
25
|
|
|
|
26
|
|
|
private $_handler_id; |
27
|
|
|
|
28
|
|
|
private $old_name; |
29
|
|
|
|
30
|
|
|
private $edit_topic; |
31
|
|
|
|
32
|
2 |
|
private function _load_controller() |
33
|
|
|
{ |
34
|
|
|
// Get the configured schemas |
35
|
2 |
|
$schemadbs = $this->_config->get_array('schemadbs_folder'); |
36
|
|
|
|
37
|
|
|
// Check if a custom schema exists |
38
|
2 |
|
if (array_key_exists($this->_topic->component, $schemadbs)) { |
39
|
|
|
$schemadb = $this->_topic->component; |
40
|
|
|
} else { |
41
|
2 |
|
$schemadb = 'default'; |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
if (!array_key_exists($schemadb, $schemadbs)) { |
45
|
|
|
throw new midcom_error('Configuration error. No ' . $schemadb . ' schema for topic has been defined!'); |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
$schemadb = schemadb::from_path($schemadbs[$schemadb]); |
49
|
|
|
|
50
|
2 |
|
foreach ($schemadb->all() as $schema) { |
51
|
2 |
|
if ($schema->has_field('name')) { |
52
|
2 |
|
$schema->get_field('name')['required'] = $this->_handler_id === 'edit'; |
53
|
|
|
} |
54
|
|
|
} |
55
|
2 |
|
$defaults = []; |
56
|
2 |
|
if ($this->_handler_id == 'create') { |
57
|
|
|
// Suggest to create the same type of a folder as the parent is |
58
|
|
|
// Unless config told us otherwise |
59
|
1 |
|
$defaults['component'] = $this->_config->get('default_component') ?: $this->_topic->component; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
$dm = new datamanager($schemadb); |
63
|
2 |
|
$this->_controller = $dm |
64
|
2 |
|
->set_defaults($defaults) |
65
|
2 |
|
->set_storage($this->edit_topic) |
66
|
2 |
|
->get_controller(); |
67
|
2 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Handler for folder editing. Checks for the permissions and folder integrity. |
71
|
|
|
*/ |
72
|
2 |
|
public function _handler_edit(Request $request, string $handler_id) |
73
|
|
|
{ |
74
|
2 |
|
$this->_topic->require_do('midcom.admin.folder:topic_management'); |
75
|
2 |
|
$this->_handler_id = $handler_id; |
76
|
|
|
|
77
|
2 |
|
if ($this->_handler_id == 'edit') { |
78
|
1 |
|
$this->_topic->require_do('midgard:update'); |
79
|
1 |
|
$title = sprintf($this->_l10n->get('edit folder %s'), $this->_topic->get_label()); |
80
|
1 |
|
$this->edit_topic = $this->_topic; |
81
|
|
|
} else { |
82
|
1 |
|
$this->_topic->require_do('midgard:create'); |
83
|
1 |
|
$title = $this->_l10n->get('create folder'); |
84
|
1 |
|
$this->edit_topic = new midcom_db_topic(); |
85
|
1 |
|
$this->edit_topic->up = $this->_topic->id; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
$this->_load_controller(); |
89
|
|
|
|
90
|
|
|
// Store the old name before editing |
91
|
2 |
|
$this->old_name = $this->_topic->name; |
92
|
|
|
|
93
|
2 |
|
$this->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.admin.folder/folder.css'); |
94
|
2 |
|
midcom::get()->head->set_pagetitle($title); |
95
|
|
|
|
96
|
2 |
|
$workflow = $this->get_workflow('datamanager', [ |
97
|
2 |
|
'controller' => $this->_controller, |
98
|
2 |
|
'save_callback' => [$this, 'save_callback'] |
99
|
|
|
]); |
100
|
2 |
|
return $workflow->run($request); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
public function save_callback() : ?string |
104
|
|
|
{ |
105
|
1 |
|
$prefix = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ANCHORPREFIX); |
106
|
1 |
|
if ($this->_handler_id === 'edit') { |
107
|
1 |
|
return $this->_update_topic($prefix, $this->old_name); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
return $this->_create_topic($prefix); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
private function _update_topic(string $prefix, string $old_name) : ?string |
113
|
|
|
{ |
114
|
1 |
|
if (isset($this->_controller->get_form_values()['style']) && $this->_controller->get_form_values()['style'] == '__create') { |
115
|
|
|
$this->edit_topic->style = $this->_create_style($this->edit_topic->name); |
116
|
|
|
|
117
|
|
|
// Failed to create the new style template |
118
|
|
|
if ($this->edit_topic->style === '') { |
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
midcom::get()->uimessages->add($this->_l10n->get('midcom.admin.folder'), $this->_l10n->get('new style created')); |
123
|
|
|
|
124
|
|
|
if (!$this->edit_topic->update()) { |
125
|
|
|
midcom::get()->uimessages->add($this->_l10n->get('midcom.admin.folder'), sprintf($this->_l10n->get('could not save folder: %s'), midcom_connection::get_error_string()), 'error'); |
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
midcom::get()->uimessages->add($this->_l10n->get('midcom.admin.folder'), $this->_l10n->get('folder saved')); |
131
|
|
|
|
132
|
|
|
// Get the relocation url |
133
|
1 |
|
if (midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ROOTTOPIC)->id === $this->edit_topic->id) { |
134
|
1 |
|
return $prefix; |
135
|
|
|
} |
136
|
|
|
return preg_replace("/{$old_name}\/\$/", "{$this->edit_topic->name}/", $prefix); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function _create_topic(string $prefix) : string |
140
|
|
|
{ |
141
|
|
|
midcom::get()->uimessages->add($this->_l10n->get('midcom.admin.folder'), $this->_l10n->get('folder created')); |
142
|
|
|
|
143
|
|
|
// Generate name if it is missing |
144
|
|
|
if (!$this->edit_topic->name) { |
145
|
|
|
$this->edit_topic->name = midcom_helper_misc::urlize($this->edit_topic->extra); |
146
|
|
|
$this->edit_topic->update(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// Get the relocation url |
150
|
|
|
return "{$prefix}{$this->edit_topic->name}/"; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Create a new style for the topic |
155
|
|
|
* |
156
|
|
|
* @return string Style path |
157
|
|
|
*/ |
158
|
|
|
private function _create_style(string $style_name) : string |
159
|
|
|
{ |
160
|
|
|
$style = new midcom_db_style(); |
161
|
|
|
$style->name = $style_name; |
162
|
|
|
|
163
|
|
|
if ($inherited = midcom_core_context::get()->get_inherited_style()) { |
164
|
|
|
$style->up = midcom_db_style::id_from_path($inherited); |
165
|
|
|
debug_add("Style inherited from {$inherited}"); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (!$style->create()) { |
169
|
|
|
debug_print_r('Failed to create a new style due to ' . midcom_connection::get_error_string(), $style, MIDCOM_LOG_WARN); |
170
|
|
|
|
171
|
|
|
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'); |
172
|
|
|
return ''; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
debug_print_r('New style created', $style); |
176
|
|
|
|
177
|
|
|
return midcom_db_style::path_from_id($style->id); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|