PluginProbe
Packeta / trunk
Packeta vtrunk
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Order / Form.php

Form.php in Packeta trunk, at src/Packetery/Module/Order/Form.php

194 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare( strict_types=1 );
4
5 namespace Packetery\Module\Order;
6
7 use Packetery\Core\CoreHelper;
8 use Packetery\Core\Entity\Order;
9 use Packetery\Core\Validator;
10 use Packetery\Module\FormFactory;
11 use Packetery\Module\FormValidators;
12 use Packetery\Module\Framework\WpAdapter;
13 use Packetery\Module\Options\OptionsProvider;
14 use Packetery\Nette\Forms;
15
16 class Form {
17
18 public const FIELD_WEIGHT = 'packeteryWeight';
19 public const FIELD_ORIGINAL_WEIGHT = 'packeteryOriginalWeight';
20 public const FIELD_VALUE = 'packeteryValue';
21 public const FIELD_CALCULATED_VALUE = 'packeteryCalculatedValue';
22 public const FIELD_LENGTH = 'packeteryLength';
23 public const FIELD_WIDTH = 'packeteryWidth';
24 public const FIELD_HEIGHT = 'packeteryHeight';
25 public const FIELD_ADULT_CONTENT = 'packeteryAdultContent';
26 public const FIELD_COD = 'packeteryCOD';
27 public const FIELD_CALCULATED_COD = 'packeteryCalculatedCod';
28 public const FIELD_DELIVER_ON = 'packeteryDeliverOn';
29
30 /**
31 * @var FormFactory
32 */
33 private $formFactory;
34
35 /**
36 * @var OptionsProvider
37 */
38 private $options;
39
40 /**
41 * @var WpAdapter
42 */
43 private $wpAdapter;
44
45 /**
46 * FormFactory constructor
47 *
48 * @param FormFactory $formFactory Form factory.
49 * @param OptionsProvider $options Options provider.
50 */
51 public function __construct( FormFactory $formFactory, OptionsProvider $options, WpAdapter $wpAdapter ) {
52 $this->formFactory = $formFactory;
53 $this->options = $options;
54 $this->wpAdapter = $wpAdapter;
55 }
56
57 public function create(): Forms\Form {
58 $form = $this->formFactory->create();
59 $unit = $this->options->getDimensionsUnit();
60
61 $form->addText( self::FIELD_WEIGHT, $this->wpAdapter->__( 'Weight (kg)', 'packeta' ) )
62 ->setRequired( false )
63 ->setNullable()
64 ->addRule( Forms\Form::FLOAT, $this->wpAdapter->__( 'The weight must be a number.', 'packeta' ) )
65 ->addRule( Forms\Form::MIN, $this->wpAdapter->__( 'The weight must be a positive number.', 'packeta' ), 0 );
66 $form->addHidden( self::FIELD_ORIGINAL_WEIGHT );
67 $this->formFactory->addDimension( $form, self::FIELD_LENGTH, (string) $this->wpAdapter->__( 'Length', 'packeta' ), $unit )
68 ->setNullable();
69 $this->formFactory->addDimension( $form, self::FIELD_WIDTH, (string) $this->wpAdapter->__( 'Width', 'packeta' ), $unit )
70 ->setNullable();
71 $this->formFactory->addDimension( $form, self::FIELD_HEIGHT, (string) $this->wpAdapter->__( 'Height', 'packeta' ), $unit )
72 ->setNullable();
73 $form->addCheckbox( self::FIELD_ADULT_CONTENT, $this->wpAdapter->__( 'Adult content', 'packeta' ) )
74 ->setRequired( false );
75 $form->addText( self::FIELD_COD, $this->wpAdapter->__( 'Cash on delivery', 'packeta' ) )
76 ->setRequired( false )
77 ->setNullable()
78 ->addRule( Forms\Form::FLOAT );
79 $form->addHidden( self::FIELD_CALCULATED_COD );
80 $form->addText( self::FIELD_VALUE, $this->wpAdapter->__( 'Order value', 'packeta' ) )
81 ->setRequired( false )
82 ->setNullable()
83 ->addRule( Forms\Form::FLOAT );
84 $form->addHidden( self::FIELD_CALCULATED_VALUE );
85 $form->addText( self::FIELD_DELIVER_ON, $this->wpAdapter->__( 'Planned dispatch', 'packeta' ) )
86 ->setHtmlAttribute( 'class', 'date-picker' )
87 ->setHtmlAttribute( 'autocomplete', 'off' )
88 ->setRequired( false )
89 ->setNullable()
90 // translators: %s: Represents minimal date for delayed delivery.
91 ->addRule( [ FormValidators::class, 'dateIsLater' ], $this->wpAdapter->__( 'Date must be later than %s', 'packeta' ), wp_date( CoreHelper::DATEPICKER_FORMAT ) );
92
93 return $form;
94 }
95
96 public function setDefaults(
97 Forms\Form $form,
98 ?float $weight,
99 ?float $originalWeight,
100 ?float $length,
101 ?float $width,
102 ?float $height,
103 ?float $cod,
104 ?float $calculatedCod,
105 ?float $orderValue,
106 ?float $calculatedValue,
107 ?bool $adultContent,
108 ?string $deliverOn
109 ): void {
110 $form->setDefaults(
111 [
112 self::FIELD_WEIGHT => $weight,
113 self::FIELD_ORIGINAL_WEIGHT => $originalWeight,
114 self::FIELD_LENGTH => $length,
115 self::FIELD_WIDTH => $width,
116 self::FIELD_HEIGHT => $height,
117 self::FIELD_COD => $cod,
118 self::FIELD_CALCULATED_COD => $calculatedCod,
119 self::FIELD_VALUE => $orderValue,
120 self::FIELD_CALCULATED_VALUE => $calculatedValue,
121 self::FIELD_ADULT_CONTENT => $adultContent,
122 self::FIELD_DELIVER_ON => $deliverOn,
123 ]
124 );
125 }
126
127 /**
128 * Gets invalid fields from validation result.
129 *
130 * @param array<string, string> $validationResult Validation result.
131 *
132 * @return string[]
133 */
134 public function getInvalidFieldsFromValidationResult( array $validationResult ): array {
135 $validationFormInputMapping = [
136 self::FIELD_VALUE => Validator\Order::ERROR_TRANSLATION_KEY_VALUE,
137 self::FIELD_WEIGHT => Validator\Order::ERROR_TRANSLATION_KEY_WEIGHT,
138 self::FIELD_HEIGHT => Validator\Order::ERROR_TRANSLATION_KEY_HEIGHT,
139 self::FIELD_WIDTH => Validator\Order::ERROR_TRANSLATION_KEY_WIDTH,
140 self::FIELD_LENGTH => Validator\Order::ERROR_TRANSLATION_KEY_LENGTH,
141 ];
142
143 $fields = [];
144 foreach ( $validationFormInputMapping as $fieldName => $value ) {
145 if ( ! isset( $validationResult[ $value ] ) ) {
146 continue;
147 }
148
149 $fields[] = $fieldName;
150 }
151
152 return $fields;
153 }
154
155 /**
156 * @param string[] $invalidFieldNames
157 *
158 * @return string
159 */
160 public function getInvalidFieldsMessageFromValidationResult( array $invalidFieldNames, Order $order ): string {
161 $fieldTranslations = [
162 self::FIELD_VALUE => $this->wpAdapter->__( 'value', 'packeta' ),
163 self::FIELD_WEIGHT => $this->wpAdapter->__( 'weight', 'packeta' ),
164 self::FIELD_HEIGHT => $this->wpAdapter->__( 'height', 'packeta' ),
165 self::FIELD_WIDTH => $this->wpAdapter->__( 'width', 'packeta' ),
166 self::FIELD_LENGTH => $this->wpAdapter->__( 'length', 'packeta' ),
167 ];
168 $message = '';
169 $invalidFields = [];
170
171 foreach ( $invalidFieldNames as $fieldName ) {
172 if ( isset( $fieldTranslations[ $fieldName ] ) ) {
173 $invalidFields[] = $fieldTranslations[ $fieldName ];
174 }
175 }
176
177 if ( $invalidFields !== [] ) {
178 $invalidFieldsString = implode( ', ', $invalidFields );
179
180 $message = sprintf(
181 // translators: %s: Required fields.
182 (string) $this->wpAdapter->__( 'Please fill in all required shipment details (%s) before submitting.', 'packeta' ),
183 $invalidFieldsString
184 );
185 }
186
187 if ( $order->hasToFillCustomsDeclaration() ) {
188 $message .= " {$this->wpAdapter->__( 'Customs declaration has to be filled in order detail.', 'packeta' )}";
189 }
190
191 return $message;
192 }
193 }
194