1<?php 2/** 3 * Copyright since 2007 PrestaShop SA and Contributors 4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA 5 * 6 * NOTICE OF LICENSE 7 * 8 * This source file is subject to the Open Software License (OSL 3.0) 9 * that is bundled with this package in the file LICENSE.md. 10 * It is also available through the world-wide-web at this URL: 11 * https://opensource.org/licenses/OSL-3.0 12 * If you did not receive a copy of the license and are unable to 13 * obtain it through the world-wide-web, please send an email 14 * to license@prestashop.com so we can send you a copy immediately. 15 * 16 * DISCLAIMER 17 * 18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer 19 * versions in the future. If you wish to customize PrestaShop for your 20 * needs please refer to https://devdocs.prestashop.com/ for more information. 21 * 22 * @author PrestaShop SA and Contributors <contact@prestashop.com> 23 * @copyright Since 2007 PrestaShop SA and Contributors 24 * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) 25 */ 26 27namespace PrestaShopBundle\Form\Admin\Type\Material; 28 29use Symfony\Component\Form\AbstractType; 30use Symfony\Component\Form\CallbackTransformer; 31use Symfony\Component\Form\Extension\Core\Type\ChoiceType; 32use Symfony\Component\Form\FormBuilderInterface; 33use Symfony\Component\Form\FormInterface; 34use Symfony\Component\Form\FormView; 35use Symfony\Component\OptionsResolver\OptionsResolver; 36 37class MaterialMultipleChoiceTableType extends AbstractType 38{ 39 /** 40 * {@inheritdoc} 41 */ 42 public function buildForm(FormBuilderInterface $builder, array $options) 43 { 44 foreach ($options['multiple_choices'] as $choices) { 45 $builder->add($choices['name'], ChoiceType::class, [ 46 'label' => $choices['label'], 47 'choices' => $choices['choices'], 48 'expanded' => true, 49 'multiple' => $choices['multiple'], 50 'choice_label' => false, 51 'choice_translation_domain' => false, 52 ]); 53 54 $builder->get($choices['name'])->addModelTransformer(new CallbackTransformer( 55 function ($value) use ($choices) { 56 if (is_array($value) && false === $choices['multiple']) { 57 return reset($value); 58 } 59 60 return $value; 61 }, 62 function ($value) { 63 return $value; 64 } 65 )); 66 } 67 } 68 69 /** 70 * {@inheritdoc} 71 */ 72 public function buildView(FormView $view, FormInterface $form, array $options) 73 { 74 $view->vars['choices'] = $options['choices']; 75 $view->vars['scrollable'] = $options['scrollable']; 76 $view->vars['headers_to_disable'] = $options['headers_to_disable']; 77 $view->vars['headers_fixed'] = $options['headers_fixed']; 78 $view->vars['table_label'] = $options['table_label']; 79 } 80 81 /** 82 * {@inheritdoc} 83 */ 84 public function finishView(FormView $view, FormInterface $form, array $options) 85 { 86 $entryIndexMapping = []; 87 88 foreach ($view->children as $childChoiceName => $childChoiceView) { 89 foreach ($childChoiceView->children as $index => $childChoiceEntryView) { 90 $entryIndexMapping[$childChoiceEntryView->vars['value']][$childChoiceName] = $index; 91 } 92 } 93 94 $view->vars['child_choice_entry_index_mapping'] = $entryIndexMapping; 95 } 96 97 /** 98 * {@inheritdoc} 99 */ 100 public function configureOptions(OptionsResolver $resolver) 101 { 102 $resolver 103 ->setRequired([ 104 'multiple_choices', 105 'choices', 106 // in some cases we want to disable 107 // header for columns 108 'headers_to_disable', 109 ]) 110 ->setDefaults([ 111 'scrollable' => true, 112 'headers_to_disable' => [], 113 'headers_fixed' => false, 114 'table_label' => false, 115 ]) 116 ; 117 118 $resolver->setAllowedTypes('choices', 'array'); 119 $resolver->setAllowedTypes('multiple_choices', 'array'); 120 $resolver->setAllowedTypes('scrollable', 'bool'); 121 $resolver->setAllowedTypes('headers_to_disable', 'array'); 122 $resolver->setAllowedTypes('headers_fixed', 'bool'); 123 $resolver->setAllowedTypes('table_label', ['bool', 'string']); 124 } 125 126 /** 127 * {@inheritdoc} 128 */ 129 public function getBlockPrefix() 130 { 131 return 'material_multiple_choice_table'; 132 } 133} 134