Skip to content
Permalink
Browse files

Convert setup helper to actual handler & improve usability

  • Loading branch information...
flack committed Oct 8, 2019
1 parent 663bcc8 commit 079a6641de07fa237ef8ea0add8cda9ae7d9e8f6
@@ -164,21 +164,16 @@ private static function initialize_root_topic() : midcom_db_topic
if ($guid = midcom::get()->config->get('midcom_root_topic_guid')) {
try {
self::$root_topic = midcom_db_topic::get_cached($guid);
return self::$root_topic;
} catch (midcom_error $e) {
if ($e instanceof midcom_error_forbidden) {
throw $e;
}
// Fall back to another topic so that admin has a chance to fix this
$setup = new midcom_core_setup("Root folder is misconfigured. Please log in as administrator");
self::$root_topic = $setup->find_topic();
}
} elseif ($component = midcom::get()->config->get('midcom_root_component')) {
self::$root_topic = new midcom_db_topic;
self::$root_topic->component = $component;
} else {
$setup = new midcom_core_setup("Root folder is not configured. Please log in as administrator");
self::$root_topic = $setup->find_topic(true);
}
$component = midcom::get()->config->get('midcom_root_component', 'midcom.core.nullcomponent');
self::$root_topic = new midcom_db_topic;
self::$root_topic->component = $component;
}
return self::$root_topic;
}
@@ -1 +1,2 @@
'enable_folder_code_execution' => false,
'schemadb_wizard' => 'file:/midcom/core/nullcomponent/config/schemadb_wizard.inc',
@@ -0,0 +1,7 @@
index:
path: /
defaults: { _controller: 'midcom_core_nullcomponent_handler_index::index' }

wizard:
path: /wizard/
defaults: { _controller: 'midcom_core_nullcomponent_handler_wizard::index' }
@@ -0,0 +1,74 @@
'default' => [
'description' => 'wizard',
'fields' => [
'existing' => [
'title' => 'topic',
'type' => 'select',
'type_config' => [
'options' => [],
'allow_other' => true,
'require_corresponding_option' => false,
],
'widget' => 'autocomplete',
'widget_config' => [
'class' => 'midcom_db_topic',
'titlefield' => 'extra',
'id_field' => 'guid',
'searchfields' => [
'title',
'extra',
'component',
'name',
],
'constraints' => [
[
'field' => 'up',
'op' => '=',
'value' => 0,
],
],
'result_headers' => [
[
'name' => 'name',
'title' => 'name',
], [
'name' => 'component',
'title' => 'component',
],
],
'orders' => [
[
'title' => 'ASC',
], [
'extra' => 'ASC',
], [
'name' => 'ASC',
],
],
],
'start_fieldset' => [
'title' => 'select existing folder'
],
'end_fieldset' => 1
],
'title' => [
'title' => 'title',
'storage' => 'extra',
'type' => 'text',
'widget' => 'text',
'start_fieldset' => [
'title' => 'create new folder'
],
],
'component' => [
'title' => 'component',
'storage' => 'component',
'type' => 'select',
'type_config' => [
'options' => midcom_admin_folder_management::list_components(midcom_core_context::get()->get_key(MIDCOM_CONTEXT_COMPONENT)),
],
'widget' => 'select',
'end_fieldset' => 1
],
],
],
@@ -23,6 +23,12 @@ public function _handler_index(array &$data)
midcom::get()->metadata->set_request_metadata($this->_topic->metadata->revised, $this->_topic->guid);
$data['node'] = $this->_topic;
if ($this->_topic->id == midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ROOTTOPIC)->id
&& !midcom::get()->config->get('midcom_root_component')) {
// if we have an ad hoc root topic, give user some options to do the initial setup
return new midcom_response_relocate($this->router->generate('wizard'));
}
return $this->show('index');
}
}
@@ -0,0 +1,90 @@
<?php
/**
* @package midcom.core.nullcomponent
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
use midcom\datamanager\controller;
use midcom\datamanager\schemadb;
use midcom\datamanager\datamanager;
use Symfony\Component\HttpFoundation\Request;
/**
* This is a URL handler class for midcom.core.nullcomponent
*
* @package midcom.core.nullcomponent
*/
class midcom_core_nullcomponent_handler_wizard extends midcom_baseclasses_components_handler
{
public function _handler_index(Request $request, array &$data)
{
if (midcom::get()->config->get('midcom_root_topic_guid')) {
$data['message'] = 'Root folder is misconfigured';
} else {
$data['message'] = 'Root folder is not configured.';
}
midcom::get()->auth->require_admin_user($data['message'] . ' Please log in as administrator');
$controller = $this->get_controller();
if ($controller->handle($request) === controller::SAVE) {
if ($response = $this->write_config($controller)) {
return $response;
}
}
$data['controller'] = $controller;
return $this->show('wizard');
}
private function get_controller() : controller
{
$schemadb = schemadb::from_path($this->_config->get('schemadb_wizard'));
$qb = midcom_db_topic::new_query_builder();
$qb->add_constraint('up', '=', 0);
$qb->add_constraint('component', '<>', '');
if ($qb->count() == 0) {
$schemadb->get_first()->get_field('existing')['hidden'] = true;
}
$dm = new datamanager($schemadb);
return $dm->get_controller();
}
/**
* Write config file
*/
private function write_config(controller $controller)
{
$values = $controller->get_form_values();
if (!empty($values['existing'])) {
$guid = $values['existing'];
} elseif (!empty($values['title'])) {
$topic = new midcom_db_topic;
$topic->title = $values['title'];
$topic->component = $values['component'];
if (!$topic->create()) {
throw new midcom_error(midcom_connection::get_error_string());
}
$guid = $topic->guid;
} else {
return;
}
$conf = '<?php' . "\n";
$conf .= "//AUTO-GENERATED on " . strftime('%x %X') . "\n";
$conf .= '$GLOBALS[\'midcom_config_local\'][\'midcom_root_topic_guid\'] = "' . $guid . '";' . "\n";
$project_dir = dirname(__DIR__, 5);
if (strpos($project_dir, '/vendor/')) {
$project_dir = dirname($project_dir, 3);
}
if (!@file_put_contents($project_dir . '/config.inc.php', $conf)) {
$this->_request_data['project_dir'] = $project_dir;
$this->_request_data['conf'] = $conf;
return $this->show('wizard-save-config');
}
return new midcom_response_relocate('');
}
}
@@ -0,0 +1,2 @@
<p>Please save the following under <code>&(data['project_dir']);/config.inc.php</code></p>
<textarea rows="5" cols="100">&(data['conf']:h);</textarea>
@@ -0,0 +1,13 @@
<p>&(data['message']);</p>
<p>Choose one of the options below:</p>
<?php $data['controller']->display_form(); ?>
<?php
$runner = new midcom_config_test;
$runner->check();
if ($runner->get_status() === midcom_config_test::ERROR) { ?>
<script>
$(document).ready(function() {
$('[href$="midcom-config-test"]').click();
});
</script>
<?php } ?>

This file was deleted.

This file was deleted.

0 comments on commit 079a664

Please sign in to comment.
You can’t perform that action at this time.