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;
28
29use Symfony\Component\Form\AbstractType;
30use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
31use Symfony\Component\Form\FormInterface;
32use Symfony\Component\Form\FormView;
33use Symfony\Component\OptionsResolver\OptionsResolver;
34
35/**
36 * Displays a switch (ON / OFF by default).
37 */
38class SwitchType extends AbstractType
39{
40    public const TRANS_DOMAIN = 'Admin.Global';
41
42    /**
43     * {@inheritdoc}
44     */
45    public function configureOptions(OptionsResolver $resolver)
46    {
47        $resolver->setDefaults([
48            'choices' => [
49                'No' => false,
50                'Yes' => true,
51            ],
52            'multiple' => false,
53            'expanded' => false,
54            'disabled' => false,
55            'choice_translation_domain' => self::TRANS_DOMAIN,
56        ]);
57        $resolver->setAllowedTypes('disabled', 'bool');
58    }
59
60    /**
61     * {@inheritdoc}
62     */
63    public function buildView(FormView $view, FormInterface $form, array $options)
64    {
65        if (true === $options['disabled']) {
66            $view->vars['disabled'] = true;
67        }
68        $view->vars['attr']['class'] = 'ps-switch';
69        if (isset($options['attr']['class'])) {
70            $view->vars['attr']['class'] .= ' ' . $options['attr']['class'];
71        }
72    }
73
74    /**
75     * {@inheritdoc}
76     */
77    public function getParent()
78    {
79        return ChoiceType::class;
80    }
81}
82