From 954bfa02e2368e18d4c038855816f064793dceda Mon Sep 17 00:00:00 2001 From: "vincent@cubedesigners.com" Date: Wed, 20 Jan 2021 16:39:16 +0000 Subject: [PATCH] wip #4216 @1 --- inc/ws/Metier/class.ws.parametres.php | 107 +++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/inc/ws/Metier/class.ws.parametres.php b/inc/ws/Metier/class.ws.parametres.php index f4ffd94e5..bea7edc55 100644 --- a/inc/ws/Metier/class.ws.parametres.php +++ b/inc/ws/Metier/class.ws.parametres.php @@ -247,7 +247,7 @@ class wsParametres extends cubeMetier implements Iterator { $res = []; foreach ($this as $k => $v) { - $res[$k]=$v; + $res[$k] = $v; } return $res; } @@ -292,4 +292,109 @@ class wsParametres extends cubeMetier implements Iterator return $v; } + public function generateV3Form($tab = false) + { + ob_end_clean(); + header('Content-type: text/plain'); + + + echo ' public function setSettingsFields() + { + '; + foreach ($this->forms as $formname => $form) { + if ($tab !== 'form') { + echo $this->generateV3Field(['name' => 'section_' . $formname, 'type' => 'FormSection', 'label' => $form['label']]); + $_tab = $tab; + } else { + $_tab = $form['label']; + } + foreach ($form['fieldsnames'] as $fieldsname) { + if ($fieldsname === '|') { + echo $this->generateV3Field(['type' => 'FormSeparator'], $_tab); + continue; + } + $f = $this->fields[$fieldsname]; + echo $this->_generateV3Field($fieldsname, $f, $_tab); + } + } + + echo "\n}"; + exit; + } + + public function generateV3Field($attributes, $tab = false) + { + if ($tab && !isset($attributes['tab'])) { + $attributes['tab'] = '§!§' . $tab . '!§!'; + } + $res = " "; + $res .= '$this->addField(' . $this->_varexport($attributes, true) . ');'; + $res .= "\n"; + + $res = str_replace("'§!§", '__(\'', $res); + $res = str_replace("!§!'", '\')', $res); + + return $res; + } + + public function _generateV3Field($name, $f, $tab = false) + { + $typeMap = ['combo' => 'SelectFromArray', 'boolean' => 'Checkbox', 'couleur' => 'Color', 'couleurAlpha' => 'Color', 'integer' => 'Integer', 'float' => 'Number', 'text' => 'Text', 'textarea' => 'Textarea', 'file' => 'Images', 'freefile' => 'FilesOrURL']; + $copyAttrs = ['min' => 'min', 'max' => 'max', 'default' => 'default', 'allows_alpha' => 'allows_alpha']; + $transAttrs = ['label' => 'label', 'hint' => 'hint']; + + if ($name === 'iconSet') { + $f['type'] = 'combo'; + $f['default'] = 15; + $f['datas'] = [__('Jeu d\'icônes par défaut') => 15]; + } + + if ($f['type'] === 'couleur') { + $f['allows_alpha'] = false; + } else if ($f['type'] === 'couleurAlpha') { + $f['allows_alpha'] = false; + } + + $attrs = ['name' => $name, 'type' => isset($typeMap[$f['type']]) ? $typeMap[$f['type']] : $f['type']]; + + if ($attrs['type'] === 'SelectFromArray') { + $attrs['options'] = array_flip($f['datas']); + } + + if ($attrs['type'] === 'Color' && isset($f['default'])) { + $f['default'] = CubeIT_Graphics_Color::colorToCSS($f['default']); + } + if ($attrs['type'] === 'Images') { + unset($f['default']); + } + + foreach ($transAttrs as $former => $new) { + if (isset($f[$former])) { + $attrs[$new] = '§!§' . $f[$former] . '!§!'; + } + } + foreach ($copyAttrs as $former => $new) { + if (isset($f[$former])) { + $attrs[$new] = $f[$former]; + } + } + + + $attrs['fake'] = true; + $attrs['store_in'] = 'settings'; + $attrs['translatable'] = false; + + return $this->generateV3Field($attrs, $tab); + } + + protected function _varexport($expression, $return = FALSE) + { + $export = var_export($expression, TRUE); + $export = preg_replace("/^([ ]*)(.*)/m", '$1$1$2', $export); + $array = preg_split("/\r\n|\n|\r/", $export); + $array = preg_replace(["/\s*array\s\($/", "/\)(,)?$/", "/\s=>\s$/"], [NULL, ']$1', ' => ['], $array); + $export = implode(PHP_EOL, array_filter(["["] + $array)); + if ((bool)$return) return $export; else echo $export; + } + } \ No newline at end of file -- 2.39.5