Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
feat(install): template for installation wizard
Browse files Browse the repository at this point in the history
Functional draft of the installation wizard

see #154
  • Loading branch information
btry authored and DIOHz0r committed Jan 11, 2018
1 parent 92ac441 commit 4f71481
Show file tree
Hide file tree
Showing 11 changed files with 1,343 additions and 400 deletions.
3 changes: 1 addition & 2 deletions front/config.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@

$config = new Config();
$pluginConfig = new PluginFlyvemdmConfig();
if (isset($_POST["update"])) {
if (isset($_POST['update']) || isset($_POST['back'])) {
$config->update($_POST);
//Html::back();
Html::redirect(Toolbox::getItemTypeFormURL(PluginFlyvemdmConfig::class));
} else if (isset($_POST['addDocTypes'])) {
$pluginConfig->addDocumentTypes();
Expand Down
140 changes: 130 additions & 10 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
* @since 0.1.0
*/
class PluginFlyvemdmConfig extends CommonDBTM {

// From CommonGLPI
protected $displaylist = false;

Expand All @@ -53,6 +52,15 @@ class PluginFlyvemdmConfig extends CommonDBTM {

const PLUGIN_FLYVEMDM_MQTT_CLIENT = 'flyvemdm';

// first and last steps of the welcome pages of wizard
const WIZARD_WELCOME_BEGIN = 1;
const WIZARD_WELCOME_END = 1;

// first and last steps of the MQTT pages of wizard
const WIZARD_MQTT_BEGIN = 100;
const WIZARD_MQTT_END = 105;

const WIZARD_FINISH = -1;
static $config = [];

/**
Expand Down Expand Up @@ -123,9 +131,13 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
switch ($item->getType()) {
case __CLASS__:
$tabs = [];
$tabs[1] = __('General configuration', 'flyvemdm');
$tabs[2] = __('Message queue', 'flyvemdm');
$tabs[3] = __('Debug', 'flyvemdm');
$config = Config::getConfigurationValues('flyvemdm', ['show_wizard']);
if ($config['show_wizard'] !== '0') {
$tabs[1] = __('Installation wizard', 'flyvemdm');
}
$tabs[2] = __('General configuration', 'flyvemdm');
$tabs[3] = __('Message queue', 'flyvemdm');
$tabs[4] = __('Debug', 'flyvemdm');
return $tabs;
break;
}
Expand All @@ -134,23 +146,26 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
}

/**
* @param $item CommonGLPI object
* @param $tabnum (default 1)
* @param $withtemplate (default 0)
* @return bool|void
* @param CommonGLPI $item object
* @param integer $tabnum (default 1)
* @param integer $withtemplate (default 0)
*/
static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) {
if ($item->getType() == __CLASS__) {
switch ($tabnum) {
case 1:
$item->showFormGeneral();
$item->showFormWizard();
break;

case 2:
$item->showFormMessageQueue();
$item->showFormGeneral();
break;

case 3:
$item->showFormMessageQueue();
break;

case 4:
$item->showFormDebug();
break;
}
Expand Down Expand Up @@ -285,6 +300,12 @@ public function showFormDebug() {
-1,
['display' => false]
);
$fields['show_wizard'] = Dropdown::showYesNo(
'show_wizard',
$fields['show_wizard'],
-1,
['display' => false]
);

$data = [
'config' => $fields
Expand All @@ -296,6 +317,37 @@ public function showFormDebug() {
Html::closeForm();
}

/**
* Displays the message queue configuration form for the plugin.
*/
public function showFormWizard() {
$canedit = PluginFlyvemdmConfig::canUpdate();
if ($canedit) {
if (!isset($_SESSION['plugin_flyvemdm_wizard_step'])) {
$_SESSION['plugin_flyvemdm_wizard_step'] = static::WIZARD_WELCOME_BEGIN;
}
echo '<form name="form" id="pluginFlyvemdm-config" method="post" action="' . Toolbox::getItemTypeFormURL(__CLASS__) . '">';

$texts = [];
$data = [];
$paragraph = 1;
switch ($_SESSION['plugin_flyvemdm_wizard_step']) {
default:
// Nothing here for now
}

$data = $data + [
'texts' => $texts,
'update' => $_SESSION['plugin_flyvemdm_wizard_step'] === static::WIZARD_FINISH ? __('Finish', 'flyvemdm') : __('Next', 'flyvemdm'),
'step' => $_SESSION['plugin_flyvemdm_wizard_step'],
];
$twig = plugin_flyvemdm_getTemplateEngine();
echo $twig->render('config-wizard.html', $data);

Html::closeForm();
}
}

/**
* Initializes the instance of the item with default values
*/
Expand All @@ -312,6 +364,11 @@ public function post_getEmpty() {
* @return array
*/
public static function configUpdate($input) {
if (isset($input['back'])) {
// Going one step backwards in wizard
return static::backwardStep();
}

// process certificates update
if (isset($input['_CACertificateFile'])) {
if (isset($input['_CACertificateFile'][0])) {
Expand All @@ -329,12 +386,75 @@ public static function configUpdate($input) {
}
}

if (isset($_SESSION['plugin_flyvemdm_wizard_step'])) {
$input = static::processStep($input);
if (count($input) > 0 && $input !== false) {
static::forwardStep($input);
} else {
$input = [];
}
}

unset($input['_CACertificateFile']);
unset($input['_tag_CACertificateFile']);
unset($input['CACertificateFile']);
return $input;
}

/**
* Does an action for the step saved in session, and defines the next step to run
* @param array $input the data send in the submitted step
* @return array modified input
*/
protected static function processStep($input) {
switch ($_SESSION['plugin_flyvemdm_wizard_step']) {
case static::WIZARD_FINISH:
Config::setConfigurationValues('flyvemdm', ['show_wizard' => '0']);
break;
}
return $input;
}

/**
* Goes one step forward in the wizard
* @param array $input the data send in the submitted step
*/
protected static function forwardStep($input) {
// Choose next step depending on current step and form data
switch ($_SESSION['plugin_flyvemdm_wizard_step']) {
case static::WIZARD_WELCOME_END:
$_SESSION['plugin_flyvemdm_wizard_step'] = static::WIZARD_MQTT_BEGIN;
break;

case static::WIZARD_MQTT_END:
$_SESSION['plugin_flyvemdm_wizard_step'] = static::WIZARD_FINISH;
break;

default:
$_SESSION['plugin_flyvemdm_wizard_step']++;
}
}

/**
* Goes one step backward in the wizard
*/
protected static function backwardStep() {
switch ($_SESSION['plugin_flyvemdm_wizard_step']) {
case static::WIZARD_MQTT_BEGIN:
$_SESSION['plugin_flyvemdm_wizard_step'] = static::WIZARD_WELCOME_END;
break;

case static::WIZARD_FINISH:
$_SESSION['plugin_flyvemdm_wizard_step'] = static::WIZARD_MQTT_END;
break;

default:
$_SESSION['plugin_flyvemdm_wizard_step']--;
}

return [];
}

/**
* Remove the value from sensitive configuration entry
* @param array $fields
Expand Down
7 changes: 5 additions & 2 deletions install/installer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function install() {
$this->migration->displayWarning("Error creating tables : " . $DB->error(), true);
return false;
}

$this->createInitialConfig();
} else {
if ($this->endsWith(PLUGIN_FLYVEMDM_VERSION,
Expand Down Expand Up @@ -524,6 +525,7 @@ protected function upgradeOneStep($toVersion) {
if (function_exists($updateFunction)) {
$this->migration->addNewMessageArea("Upgrade to $toVersion");
$updateFunction($this->migration);
$this->migration->executeMigration();
$this->migration->displayMessage('Done');
}
}
Expand Down Expand Up @@ -648,6 +650,7 @@ protected function createInitialConfig() {
'computertypes_id' => '0',
'agentusercategories_id' => '0',
'invitation_deeplink' => PLUGIN_FLYVEMDM_DEEPLINK,
'show_wizard' => PluginFlyvemdmConfig::WIZARD_WELCOME_BEGIN,
];
Config::setConfigurationValues('flyvemdm', $newConfig);
$this->createBackendMqttUser(self::BACKEND_MQTT_USER, $MdmMqttPassword);
Expand Down Expand Up @@ -1437,7 +1440,7 @@ static public function getPolicies() {
'type_data' => '',
'unicity' => 1,
'plugin_flyvemdm_policycategories_id' => 'Security > Peripherals',
'comment' => __('', 'flyvemdm'),
'comment' => __('Disable the speakerphone', 'flyvemdm'),
'default_value' => '0',
'recommended_value' => '0',
'is_android_policy' => '1',
Expand All @@ -1453,7 +1456,7 @@ static public function getPolicies() {
'type_data' => '',
'unicity' => 1,
'plugin_flyvemdm_policycategories_id' => 'Security > Peripherals',
'comment' => __('', 'flyvemdm'),
'comment' => __('Disable creation of VPN profiles', 'flyvemdm'),
'default_value' => '0',
'recommended_value' => '0',
'is_android_policy' => '1',
Expand Down
1 change: 1 addition & 0 deletions install/upgrade/update_to_dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function plugin_flyvemdm_update_to_dev(Migration $migration) {

Config::setConfigurationValues('flyvemdm', [
'default_agent_url' => PLUGIN_FLYVEMDM_AGENT_DOWNLOAD_URL,
'show_wizard' => '0',
]);

// Update configuration
Expand Down
Loading

0 comments on commit 4f71481

Please sign in to comment.