Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 0077c8e

Browse files
author
Jamie Snape
committed
Revise demo module configuration form and add install script
1 parent c416249 commit 0077c8e

File tree

8 files changed

+135
-122
lines changed

8 files changed

+135
-122
lines changed

modules/demo/configs/module.ini

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
; MIDAS Server. Copyright Kitware SAS. Licensed under the Apache License 2.0.
2+
13
[global]
2-
version = 1.0.0
3-
fullname = Demo
4-
description = "Allows demonstration mode to be enabled"
5-
category = Core
4+
fullname = "Demonstration"
5+
description = "Allow demonstration mode to be enabled"
6+
category = "Core"
7+
uuid = "5f1220d3-50ff-48ac-a6aa-b92fbdd1d801"
8+
version = "1.0.0"

modules/demo/constant/module.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
limitations under the License.
1919
=========================================================================*/
2020

21+
define('MIDAS_DEMO_ENABLED_KEY', 'enabled');
22+
define('MIDAS_DEMO_ENABLED_DEFAULT_VALUE', 1);
23+
2124
define('MIDAS_DEMO_ADMIN_EMAIL', 'admin@kitware.com');
2225
define('MIDAS_DEMO_ADMIN_PASSWORD', 'admin');
2326
define('MIDAS_DEMO_USER_EMAIL', 'user@kitware.com');

modules/demo/controllers/ConfigController.php renamed to modules/demo/controllers/AdminController.php

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
limitations under the License.
1919
=========================================================================*/
2020

21-
/** Config controller for the demo module */
22-
class Demo_ConfigController extends Demo_AppController
21+
/** Admin controller for the demo module. */
22+
class Demo_AdminController extends Demo_AppController
2323
{
24+
/** @var array */
2425
public $_models = array('Setting');
26+
27+
/** @var array */
2528
public $_moduleComponents = array('Demo');
26-
public $_moduleForms = array('Config');
2729

2830
/** Require admin privileges excluding demo admin */
2931
public function requireNonDemoAdminPrivileges()
@@ -38,12 +40,40 @@ public function requireNonDemoAdminPrivileges()
3840
public function indexAction()
3941
{
4042
$this->requireNonDemoAdminPrivileges();
41-
$configForm = $this->ModuleForm->Config->createConfigForm();
42-
$formArray = $this->getFormAsArray($configForm);
43-
$param = 'enabled';
44-
$value = $this->Setting->getValueByName($param, $this->moduleName);
45-
$formArray[$param]->setValue($value);
46-
$this->view->configForm = $formArray;
43+
44+
$this->view->pageTitle = 'Demo Module Configuration';
45+
$form = new Demo_Form_Admin();
46+
47+
if ($this->getRequest()->isPost()) {
48+
$data = $this->getRequest()->getPost();
49+
50+
if ($form->isValid($data)) {
51+
$values = $form->getValues();
52+
53+
foreach ($values as $key => $value) {
54+
$this->Setting->setConfig($key, $value, $this->moduleName);
55+
}
56+
}
57+
58+
$form->populate($data);
59+
} else {
60+
$elements = $form->getElements();
61+
62+
foreach ($elements as $element) {
63+
$name = $element->getName();
64+
65+
if ($name !== 'csrf' && $name !== 'submit') {
66+
$value = $this->Setting->getValueByName($name, $this->moduleName);
67+
68+
if (!is_null($value)) {
69+
$form->setDefault($name, $value);
70+
}
71+
}
72+
}
73+
}
74+
75+
$this->view->form = $form;
76+
session_start();
4777
}
4878

4979
/** Reset action */
@@ -53,16 +83,4 @@ public function resetAction()
5383
$this->ModuleComponent->Demo->reset();
5484
$this->_helper->Redirector->gotoSimple('index');
5585
}
56-
57-
/** Submit action */
58-
public function submitAction()
59-
{
60-
$this->requireNonDemoAdminPrivileges();
61-
$this->disableLayout();
62-
$this->disableView();
63-
$param = 'enabled';
64-
$value = $this->getParam($param);
65-
$this->Setting->setConfig($param, $value, $this->moduleName);
66-
echo JsonComponent::encode(array(true, 'Changes saved'));
67-
}
6886
}

modules/demo/controllers/forms/ConfigForm.php renamed to modules/demo/database/InstallScript.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,19 @@
1818
limitations under the License.
1919
=========================================================================*/
2020

21-
/** Config form for the demo module */
22-
class Demo_ConfigForm extends AppForm
21+
require_once BASE_PATH.'/modules/demo/constant/module.php';
22+
23+
/** Install the demo module. */
24+
class Demo_InstallScript extends MIDASModuleInstallScript
2325
{
24-
/** Create form */
25-
public function createConfigForm()
26-
{
27-
$form = new Zend_Form();
28-
$form->setAction($this->webroot.'/demo/config/submit')->setMethod('post');
29-
$enabled = new Zend_Form_Element_Checkbox('enabled');
30-
$enabled->setLabel($this->t('Enabled'));
31-
$submit = new Zend_Form_Element_Submit('submit');
32-
$submit->setLabel($this->t('Save'));
33-
$form->addElements(array($enabled, $submit));
26+
/** @var string */
27+
public $moduleName = 'demo';
3428

35-
return $form;
29+
/** Post database install. */
30+
public function postInstall()
31+
{
32+
/** @var SettingModel $settingModel */
33+
$settingModel = MidasLoader::loadModel('Setting');
34+
$settingModel->setConfig(MIDAS_DEMO_ENABLED_KEY, MIDAS_DEMO_ENABLED_DEFAULT_VALUE, $this->moduleName);
3635
}
3736
}

modules/demo/forms/Admin.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
/** Admin form for the demo module. */
22+
class Demo_Form_Admin extends Zend_Form
23+
{
24+
/** Initialize this form. */
25+
public function init()
26+
{
27+
$this->setName('demo_admin');
28+
$this->setMethod('POST');
29+
30+
$csrf = new Midas_Form_Element_Hash('csrf');
31+
$csrf->setSalt('25LPjLsarZvkFTK8t5ntWSDF');
32+
$csrf->setDecorators(array('ViewHelper'));
33+
34+
$enabled = new Zend_Form_Element_Checkbox(MIDAS_DEMO_ENABLED_KEY);
35+
$enabled->setLabel('Enabled');
36+
37+
$this->addDisplayGroup(array($enabled), 'global');
38+
39+
$submit = new Zend_Form_Element_Submit('submit');
40+
$submit->setLabel('Save');
41+
42+
$this->addElements(array($csrf, $enabled, $submit));
43+
}
44+
}

modules/demo/public/js/config/config.index.js

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
$this->declareVars('form', 'pageTitle');
22+
$this->headTitle($this->escape($this->pageTitle));
23+
?>
24+
25+
<div class="viewMain">
26+
<h1><?php echo $this->escape($this->pageTitle); ?></h1>
27+
<?php echo $this->form; ?>
28+
<p><a href="<?php echo $this->url(array('module' => 'demo', 'controller' => 'admin', 'action' => 'reset'), 'default'); ?>">Reset Demo</a></p>
29+
<p><a href="<?php echo $this->url(array('controller' => 'admin', 'action' => 'index'), 'default'); ?>#tabs-modules">&laquo; Back to Modules Administration</a></p>
30+
</div>

modules/demo/views/config/index.phtml

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)