From b7ad64152d434c6a6188c7c75a2d538432c44371 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 21 Apr 2017 12:31:25 +0200 Subject: [locationinfo] Generate credentials in server-settings server side There's no need to generate parameters server side that get passed to the template engine which generates javascript code that generates html snippets in the browser. We now generate everything server side right away. --- .../locationinfo/inc/coursebackend.inc.php | 39 ++++ modules-available/locationinfo/page.inc.php | 94 +++------- .../locationinfo/templates/location-info.html | 1 - .../locationinfo/templates/server-prop-bool.html | 16 ++ .../templates/server-prop-dropdown.html | 19 ++ .../templates/server-prop-generic.html | 16 ++ .../locationinfo/templates/server-settings.html | 208 +++++++-------------- 7 files changed, 181 insertions(+), 212 deletions(-) create mode 100644 modules-available/locationinfo/templates/server-prop-bool.html create mode 100644 modules-available/locationinfo/templates/server-prop-dropdown.html create mode 100644 modules-available/locationinfo/templates/server-prop-generic.html diff --git a/modules-available/locationinfo/inc/coursebackend.inc.php b/modules-available/locationinfo/inc/coursebackend.inc.php index 3e95a170..447a5598 100644 --- a/modules-available/locationinfo/inc/coursebackend.inc.php +++ b/modules-available/locationinfo/inc/coursebackend.inc.php @@ -323,4 +323,43 @@ class BackendProperty { $this->type = $type; $this->default = $default; } + + /** + * Initialize additional fields of this class that are only required + * for rendering the server configuration dialog. + * + * @param string $backendId target backend id + * @param mixed $current current value of this property. + */ + public function initForRender($current = null) { + if (is_array($this->type)) { + $this->template = 'dropdown'; + $this->select_list = []; + foreach ($this->type as $item) { + $this->select_list[] = [ + 'option' => $item, + 'active' => $item == $current, + ]; + } + } elseif ($this->type === 'bool') { + $this->template = $this->type; + } else { + $this->template = 'generic'; + } + if ($this->type === 'string') { + $this->inputtype = 'text'; + } elseif ($this->type === 'int') { + $this->inputtype = 'number'; + } elseif ($this->type === 'password') { + $this->inputtype = Property::getPasswordFieldType(); + } + $this->currentvalue = $current === null ? $this->default : $current; + } + public $inputtype; + public $template; + public $title; + public $helptext; + public $currentvalue; + public $select_list; + public $credentialsHtml; } diff --git a/modules-available/locationinfo/page.inc.php b/modules-available/locationinfo/page.inc.php index 209de473..eb50193e 100644 --- a/modules-available/locationinfo/page.inc.php +++ b/modules-available/locationinfo/page.inc.php @@ -123,10 +123,8 @@ class Page_LocationInfo extends Page $tmptypeArray = $backend->getCredentialDefinitions(); $credentialsJson = array(); - $counter = 0; foreach ($tmptypeArray as $cred) { - $credentialsJson[$cred->property] = Request::post($counter); - $counter++; + $credentialsJson[$cred->property] = Request::post('prop-' . $cred->property); } $params = array( 'name' => $servername, @@ -443,89 +441,43 @@ class Page_LocationInfo extends Page */ private function ajaxServerSettings($id) { - $dbresult = Database::queryFirst('SELECT servername, servertype, credentials + $oldConfig = Database::queryFirst('SELECT servername, servertype, credentials FROM `locationinfo_coursebackend` WHERE serverid = :id', array('id' => $id)); // Credentials stuff. - $dbcredentials = json_decode($dbresult['credentials'], true); + if ($oldConfig !== false) { + $oldCredentials = json_decode($oldConfig['credentials'], true); + } else { + $oldCredentials = array(); + } // Get a list of all the backend types. $serverBackends = array(); $s_list = CourseBackend::getList(); foreach ($s_list as $s) { - $backend['typ'] = $s; $backendInstance = CourseBackend::getInstance($s); - $backend['display'] = $backendInstance->getDisplayName(); - - if ($backend['typ'] == $dbresult['servertype']) { - $backend['active'] = true; - } else { - $backend['active'] = false; - } - - $credentials = $backendInstance->getCredentialDefinitions(); - $backend['credentials'] = array(); - - $counter = 0; - foreach ($credentials as $cred) { - $credential['uid'] = $counter; - $credential['name'] = Dictionary::translateFile($s, $cred->property, true); - $credential['type'] = $cred->type; - $credential['title'] = Dictionary::translateFile($s, $cred->property . "_title"); - - if (Property::getPasswordFieldType() === 'text') { - $credential['mask'] = false; - } else { - if ($cred->type === "password") { - $credential['mask'] = true; - } - } - - if ($backend['typ'] == $dbresult['servertype'] && isset($dbcredentials[$cred->property])) { - $credential['value'] = $dbcredentials[$cred->property]; + $backend = array( + 'backendtype' => $s, + 'display' => $backendInstance->getDisplayName(), + 'active' => ($oldConfig !== false && $s === $oldConfig['servertype']), + ); + $backend['credentials'] = $backendInstance->getCredentialDefinitions(); + foreach ($backend['credentials'] as $cred) { + if ($backend['active'] && isset($oldCredentials[$cred->property])) { + $cred->initForRender($oldCredentials[$cred->property]); } else { - $credential['value'] = $cred->default; + $cred->initForRender(); } - - $selection = array(); - - if (is_array($cred->type)) { - - $selfirst = true; - foreach ($cred->type as $opt) { - $option['option'] = $opt; - if (isset($credential['value'])) { - if ($opt == $credential['value']) { - $option['active'] = true; - } else { - $option['active'] = false; - } - } else { - if ($selfirst) { - $option['active'] = true; - $selfirst = false; - } else { - $option['active'] = false; - } - } - - $selection[] = $option; - } - $credential['type'] = "array"; - $credential['array'] = $selection; - } - - $backend['credentials'][] = $credential; - - $counter++; + $cred->title = Dictionary::translateFile('backend-' . $s, $cred->property, true); + $cred->helptext = Dictionary::translateFile('backend-' . $s, $cred->property . "_helptext"); + $cred->credentialsHtml = Render::parse('server-prop-' . $cred->template, (array)$cred); } $serverBackends[] = $backend; } - echo Render::parse('server-settings', array('id' => $id, - 'name' => $dbresult['servername'], - 'servertype' => $dbresult['servertype'], - 'backendList' => array_values($serverBackends))); + 'name' => $oldConfig['servername'], + 'currentbackend' => $oldConfig['servertype'], + 'backendList' => $serverBackends)); } /** diff --git a/modules-available/locationinfo/templates/location-info.html b/modules-available/locationinfo/templates/location-info.html index c2242241..8af29df0 100644 --- a/modules-available/locationinfo/templates/location-info.html +++ b/modules-available/locationinfo/templates/location-info.html @@ -178,7 +178,6 @@ */ function loadServerSettingsModal(serverid) { $('#myModalHeader').text("{{lang_locationSettings}}").css("font-weight", "Bold"); - $('#myModalSubmitButton').attr("form", "settingsForm"); $('#myModal .modal-dialog').css('width', ''); $('#myModal').modal('show'); $('#myModalBody').load("?do=locationinfo&action=serverSettings&id=" + serverid); diff --git a/modules-available/locationinfo/templates/server-prop-bool.html b/modules-available/locationinfo/templates/server-prop-bool.html new file mode 100644 index 00000000..f430d02c --- /dev/null +++ b/modules-available/locationinfo/templates/server-prop-bool.html @@ -0,0 +1,16 @@ +
+
+
+
+ +
+
+ {{#helptext}} + + + + {{/helptext}} +
+
+
\ No newline at end of file diff --git a/modules-available/locationinfo/templates/server-prop-dropdown.html b/modules-available/locationinfo/templates/server-prop-dropdown.html new file mode 100644 index 00000000..80667766 --- /dev/null +++ b/modules-available/locationinfo/templates/server-prop-dropdown.html @@ -0,0 +1,19 @@ +
+
+
+
+ +
+
+ {{#helptext}} + + + + {{/helptext}} +
+
+
\ No newline at end of file diff --git a/modules-available/locationinfo/templates/server-prop-generic.html b/modules-available/locationinfo/templates/server-prop-generic.html new file mode 100644 index 00000000..9e94c23d --- /dev/null +++ b/modules-available/locationinfo/templates/server-prop-generic.html @@ -0,0 +1,16 @@ +
+
+
+
+ +
+
+ {{#helptext}} + + + + {{/helptext}} +
+
+
\ No newline at end of file diff --git a/modules-available/locationinfo/templates/server-settings.html b/modules-available/locationinfo/templates/server-settings.html index 09b3870c..c9b713e6 100644 --- a/modules-available/locationinfo/templates/server-settings.html +++ b/modules-available/locationinfo/templates/server-settings.html @@ -1,163 +1,91 @@ -
-
+
+
{{lang_general}}
+
+
+
+
+
+ +
+
+ +
+
+ + + +
+
+
+
+
+
+ +
+
+ +
+
+ + + +
+
+
+
+
+
+ +{{#backendList}} +
+ +
-
{{lang_general}}
+
{{lang_credentials}}
- -
-
-
- -
-
- -
-
- - - -
-
-
- -
-
-
- -
-
- -
-
- - - -
-
-
- + {{#credentials}} + {{{credentialsHtml}}} + {{/credentials}}
- -
-
{{lang_credentials}}
-
-
-
-
-
+{{/backendList}} -- cgit v1.2.3-55-g7522