* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) */ namespace PrestaShop\Module\AutoUpgrade\Twig\Form; use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeConfiguration; use PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator; use Twig\Environment; class FormRenderer { /** * @var UpgradeConfiguration */ private $config; /** * @var Translator */ private $translator; /** * @var Environment */ private $twig; /** * @param Environment $twig */ public function __construct( UpgradeConfiguration $configuration, $twig, Translator $translator ) { $this->config = $configuration; $this->twig = $twig; $this->translator = $translator; } /** * @param array>> $fields */ public function render(string $name, array $fields, string $tabname): string { $formFields = []; foreach ($fields as $key => $field) { $html = ''; $required = !empty($field['required']); $disabled = !empty($field['disabled']); if ($key === 'PS_DISABLE_OVERRIDES') { // values fetched from configuration in database $val = UpgradeConfiguration::isOverrideAllowed(); } else { // other conf values are fetched from config file $val = $this->config->get($key); } if ($val === null) { $val = isset($field['defaultValue']) ? $field['defaultValue'] : false; } if (!in_array($field['type'], ['image', 'radio', 'select', 'container', 'bool', 'container_end']) || isset($field['show'])) { $html .= '
' . ($field['title'] ? '' : '') . '
'; } // Display the appropriate input type for each field switch ($field['type']) { case 'disabled': $html .= $field['disabled']; break; case 'bool': $html .= $this->renderBool($field, $key, $val); break; case 'radio': $html .= $this->renderRadio($field, $key, $val, $disabled); break; case 'select': $html .= $this->renderSelect($field, $key, $val); break; case 'textarea': $html .= $this->renderTextarea($field, $key, $val, $disabled); break; case 'container': $html .= '
'; break; case 'container_end': $html .= (isset($field['content']) ? $field['content'] : '') . '
'; break; case 'text': default: $html .= $this->renderTextField($field, $key, $val, $disabled); } if ($required && !in_array($field['type'], ['image', 'radio'])) { $html .= ' *'; } if (isset($field['desc']) && !in_array($field['type'], ['bool', 'select'])) { $html .= '

'; if (!empty($field['thumb']) && $field['thumb']['pos'] == 'after') { $html .= $this->renderThumb($field); } $html .= $field['desc'] . '

'; } if (!in_array($field['type'], ['image', 'radio', 'select', 'container', 'bool', 'container_end']) || isset($field['show'])) { $html .= '
'; } $formFields[] = $html; } return $this->twig->render( '@ModuleAutoUpgrade/form.html.twig', [ 'name' => $name, 'tabName' => $tabname, 'fields' => $formFields, ] ); } /** * @param array> $field */ private function renderBool(array $field, string $key, bool $val): string { return '
' . $field['desc'] . '
'; } /** * @param array> $field */ private function renderRadio(array $field, string $key, string $val, bool $disabled): string { $html = ''; foreach ($field['choices'] as $cValue => $cKey) { $html .= '
'; } $html .= '
'; return $html; } /** * @param array> $field */ private function renderSelect(array $field, string $key, string $val): string { $html = '
' . $field['desc'] . '
'; return $html; } /** * @param array> $field */ private function renderTextarea(array $field, string $key, string $val, bool $disabled): string { return ''; } /** * @param array> $field */ private function renderTextField(array $field, string $key, string $val, bool $disabled): string { return '' . (isset($field['next']) ? ' ' . $field['next'] : ''); } /** * @param array> $field */ private function renderThumb(array $field): string { return "\"{$field['title']}\""; } }