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 */
26declare(strict_types=1);
27
28namespace PrestaShopBundle\Form\Admin\Configure\ShopParameters\OrderStates;
29
30use PrestaShop\PrestaShop\Adapter\Configuration;
31use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\DefaultLanguage;
32use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\TypedRegex;
33use PrestaShop\PrestaShop\Core\MailTemplate\Layout\Layout;
34use PrestaShop\PrestaShop\Core\MailTemplate\ThemeCatalogInterface;
35use PrestaShopBundle\Form\Admin\Type\ColorPickerType;
36use PrestaShopBundle\Form\Admin\Type\TranslatableChoiceType;
37use PrestaShopBundle\Form\Admin\Type\TranslatableType;
38use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
39use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
40use Symfony\Component\Form\Extension\Core\Type\TextType;
41use Symfony\Component\Form\FormBuilderInterface;
42use Symfony\Component\OptionsResolver\OptionsResolver;
43use Symfony\Component\Routing\Router;
44use Symfony\Component\Translation\TranslatorInterface;
45
46/**
47 * Type is used to created form for order state add/edit actions
48 */
49class OrderStateType extends TranslatorAwareType
50{
51    /**
52     * @var array
53     */
54    private $templates;
55
56    /**
57     * @var Router
58     */
59    private $routing;
60
61    /**
62     * @var array
63     */
64    private $templateAttributes;
65
66    /**
67     * @param TranslatorInterface $translator
68     * @param array $locales
69     * @param ThemeCatalogInterface $themeCatalog
70     * @param Router $routing
71     * @param Configuration $configuration
72     *
73     * @throws \PrestaShop\PrestaShop\Core\Exception\InvalidArgumentException
74     */
75    public function __construct(
76        TranslatorInterface $translator,
77        array $locales,
78        ThemeCatalogInterface $themeCatalog,
79        Router $routing,
80        Configuration $configuration
81    ) {
82        parent::__construct($translator, $locales);
83        $this->routing = $routing;
84        $mailTheme = $configuration->get('PS_MAIL_THEME', 'modern');
85
86        $mailLayouts = $themeCatalog->getByName($mailTheme)->getLayouts();
87
88        foreach ($locales as $locale) {
89            $languageId = $locale['id_lang'];
90            $this->templates[$languageId] = $this->templateAttributes[$languageId] = [];
91
92            /** @var Layout $mailLayout */
93            foreach ($mailLayouts as $mailLayout) {
94                $this->templates[$languageId][$mailLayout->getName()] = $mailLayout->getName();
95                $this->templateAttributes[$languageId][$mailLayout->getName()] = [
96                    'data-preview' => $this->routing->generate(
97                        empty($mailLayout->getModuleName()) ?
98                            'admin_mail_theme_preview_layout' :
99                            'admin_mail_theme_preview_module_layout',
100                        [
101                            'theme' => $mailTheme,
102                            'layout' => $mailLayout->getName(),
103                            'type' => 'html',
104                            'locale' => $locale['iso_code'],
105                            'module' => $mailLayout->getModuleName(),
106                        ]
107                    ),
108                ];
109            }
110        }
111    }
112
113    /**
114     * {@inheritdoc}
115     */
116    public function buildForm(FormBuilderInterface $builder, array $options)
117    {
118        $builder
119            ->add('name', TranslatableType::class, [
120                'type' => TextType::class,
121                'constraints' => [
122                    new DefaultLanguage(),
123                ],
124                'options' => [
125                    'constraints' => [
126                        new TypedRegex([
127                            'type' => 'generic_name',
128                        ]),
129                    ],
130                ],
131            ])
132            ->add('color', ColorPickerType::class, [
133                'required' => false,
134            ])
135            ->add('loggable', CheckboxType::class, [
136                'required' => false,
137                'label' => $this->trans('Consider the associated order as validated.', 'Admin.Shopparameters.Feature'),
138                'attr' => [
139                    'material_design' => true,
140                ],
141            ])
142            ->add('invoice', CheckboxType::class, [
143                'required' => false,
144                'label' => $this->trans('Allow a customer to download and view PDF versions of his/her invoices.', 'Admin.Shopparameters.Feature'),
145                'attr' => [
146                    'material_design' => true,
147                ],
148            ])
149            ->add('hidden', CheckboxType::class, [
150                'required' => false,
151                'label' => $this->trans('Hide this status in all customer orders.', 'Admin.Shopparameters.Feature'),
152                'attr' => [
153                    'material_design' => true,
154                ],
155            ])
156            ->add('send_email', CheckboxType::class, [
157                'required' => false,
158                'label' => $this->trans('Send an email to the customer when his/her order status has changed.', 'Admin.Shopparameters.Feature'),
159                'attr' => [
160                    'material_design' => true,
161                ],
162            ])
163            ->add('pdf_invoice', CheckboxType::class, [
164                'required' => false,
165                'label' => $this->trans('Attach invoice PDF to email.', 'Admin.Shopparameters.Feature'),
166                'attr' => [
167                    'material_design' => true,
168                ],
169            ])
170            ->add('pdf_delivery', CheckboxType::class, [
171                'required' => false,
172                'label' => $this->trans('Attach delivery slip PDF to email.', 'Admin.Shopparameters.Feature'),
173                'attr' => [
174                    'material_design' => true,
175                ],
176            ])
177            ->add('shipped', CheckboxType::class, [
178                'required' => false,
179                'label' => $this->trans('Set the order as shipped.', 'Admin.Shopparameters.Feature'),
180                'attr' => [
181                    'material_design' => true,
182                ],
183            ])
184            ->add('paid', CheckboxType::class, [
185                'required' => false,
186                'label' => $this->trans('Set the order as paid.', 'Admin.Shopparameters.Feature'),
187                'attr' => [
188                    'material_design' => true,
189                ],
190            ])
191            ->add('delivery', CheckboxType::class, [
192                'required' => false,
193                'label' => $this->trans('Set the order as in transit.', 'Admin.Shopparameters.Feature'),
194                'attr' => [
195                    'material_design' => true,
196                ],
197            ])
198            ->add('template', TranslatableChoiceType::class, [
199                'hint' => sprintf(
200                    '%s<br>%s',
201                    $this->trans('Only letters, numbers and underscores ("_") are allowed.', 'Admin.Shopparameters.Help'),
202                    $this->trans('Email template for both .html and .txt.', 'Admin.Shopparameters.Help')
203                ),
204                'required' => false,
205                'choices' => $this->templates,
206                'row_attr' => $this->templateAttributes,
207            ])
208        ;
209    }
210
211    /**
212     * {@inheritdoc}
213     */
214    public function configureOptions(OptionsResolver $resolver)
215    {
216        $resolver
217            ->setDefaults([
218                'translation_domain' => 'Admin.Shopparameters.Feature',
219                'allow_extra_fields' => true,
220            ])
221        ;
222    }
223}
224