* @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 Twig_Environment|\Twig\Environment */ private $twig; public function __construct( UpgradeConfiguration $configuration, $twig, Translator $translator ) { $this->config = $configuration; $this->twig = $twig; $this->translator = $translator; } public function render($name, $fields, $tabname, $size, $icon) { $required = false; $formFields = array(); foreach ($fields as $key => $field) { $html = ''; $required = !empty($field['required']); $disabled = !empty($field['disabled']); $val = $this->config->get($key); if ($val === null) { $val = isset($field['defaultValue']) ? $field['defaultValue'] : false; } if (!in_array($field['type'], array('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'], array('image', 'radio'))) { $html .= ' *'; } if (isset($field['desc']) && !in_array($field['type'], array('bool', 'select'))) { $html .= '

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

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