PluginProbe
Packeta / 2.1
Packeta v2.1
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
← All changes | src/Packetery/Module/Order/CustomsDeclarationMetabox.php +84 -62 1.6.42.1 View file →
@@ -8,25 +8,27 @@
8 8 declare(strict_types=1);
9 9
10 10 namespace Packetery\Module\Order;
11 11
12 +use Packetery\Core\CoreHelper;
12 13 use Packetery\Core\Entity;
13 14 use Packetery\Core\Entity\Order;
14 -use Packetery\Core\Helper;
15 -use Packetery\Module;
15 +use Packetery\Latte\Engine;
16 +use Packetery\Module\CustomsDeclaration;
16 17 use Packetery\Module\EntityFactory;
17 18 use Packetery\Module\FormFactory;
18 19 use Packetery\Module\FormRules;
20 +use Packetery\Module\Framework\WpAdapter;
19 21 use Packetery\Module\Message;
20 22 use Packetery\Module\MessageManager;
21 -use Packetery\Latte\Engine;
23 +use Packetery\Module\ModuleHelper;
22 24 use Packetery\Nette\Forms\Container;
23 25 use Packetery\Nette\Forms\Controls\BaseControl;
24 26 use Packetery\Nette\Forms\Controls\Checkbox;
27 +use Packetery\Nette\Forms\Controls\UploadControl;
25 28 use Packetery\Nette\Forms\Form;
26 29 use Packetery\Nette\Http\FileUpload;
27 30 use Packetery\Nette\Http\Request;
28 -use Packetery\Module\CustomsDeclaration;
29 31
30 32 /**
31 33 * Class CustomsDeclarationMetabox.
32 34 */
@@ -91,8 +93,13 @@
91 93 */
92 94 private $detailCommonLogic;
93 95
94 96 /**
97 + * @var WpAdapter
98 + */
99 + private $wpAdapter;
100 +
101 + /**
95 102 * Constructor.
96 103 *
97 104 * @param Engine $latteEngine Latte engine.
98 105 * @param FormFactory $formFactory Form factory.
@@ -108,9 +115,10 @@
108 115 CustomsDeclaration\Repository $customsDeclarationRepository,
109 116 EntityFactory\CustomsDeclaration $customsDeclarationEntityFactory,
110 117 Request $request,
111 118 MessageManager $messageManager,
112 - DetailCommonLogic $detailCommonLogic
119 + DetailCommonLogic $detailCommonLogic,
120 + WpAdapter $wpAdapter
113 121 ) {
114 122 $this->latteEngine = $latteEngine;
115 123 $this->formFactory = $formFactory;
116 124 $this->customsDeclarationRepository = $customsDeclarationRepository;
@@ -117,8 +125,9 @@
117 125 $this->customsDeclarationEntityFactory = $customsDeclarationEntityFactory;
118 126 $this->request = $request;
119 127 $this->messageManager = $messageManager;
120 128 $this->detailCommonLogic = $detailCommonLogic;
129 + $this->wpAdapter = $wpAdapter;
121 130 }
122 131
123 132 /**
124 133 * Registers related hooks.
@@ -138,10 +147,10 @@
138 147 public function addMetaBoxes(): void {
139 148 $order = $this->detailCommonLogic->getOrder();
140 149
141 150 if (
142 - null === $order ||
143 - false === $order->getCarrier()->requiresCustomsDeclarations()
151 + $order === null ||
152 + $order->getCarrier()->requiresCustomsDeclarations() === false
144 153 ) {
145 154 return;
146 155 }
147 156
@@ -148,9 +157,9 @@
148 157 add_meta_box(
149 158 'packetery_customs_declaration_metabox',
150 159 __( 'Customs declaration', 'packeta' ),
151 160 [ $this, 'render' ],
152 - Module\Helper::isHposEnabled() ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order',
161 + ModuleHelper::isHposEnabled() ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order',
153 162 'advanced',
154 163 'high'
155 164 );
156 165 }
@@ -185,9 +194,9 @@
185 194 */
186 195 public function saveFields( Order $order ): void {
187 196 if (
188 197 ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ||
189 - null === $this->request->getPost( self::FORM_CONTAINER_NAME )
198 + $this->request->getPost( self::FORM_CONTAINER_NAME ) === null
190 199 ) {
191 200 return;
192 201 }
193 202
@@ -207,9 +216,9 @@
207 216 * @return void
208 217 */
209 218 public function render(): void {
210 219 $order = $this->detailCommonLogic->getOrder();
211 - if ( null === $order ) {
220 + if ( $order === null ) {
212 221 return;
213 222 }
214 223
215 224 $customsDeclaration = $this->customsDeclarationRepository->getByOrderNumber( $order->getNumber() );
@@ -214,9 +223,9 @@
214 223
215 224 $customsDeclaration = $this->customsDeclarationRepository->getByOrderNumber( $order->getNumber() );
216 225
217 226 $formData = [];
218 - if ( null !== $customsDeclaration ) {
227 + if ( $customsDeclaration !== null ) {
219 228 $formData = [
220 229 self::FORM_CONTAINER_NAME => $this->customsDeclarationRepository->declarationToDbArray(
221 230 $customsDeclaration,
222 231 [ 'invoice_file', 'ead_file', 'order_id' ]
@@ -232,10 +241,11 @@
232 241
233 242 $form = $this->createForm( $formData, $customsDeclaration );
234 243 $form->setDefaults( $formData );
235 244
236 - $hasInvoiceFile = null !== $customsDeclaration && $customsDeclaration->hasInvoiceFileContent();
237 - $hasEadFile = null !== $customsDeclaration && $customsDeclaration->hasEadFileContent();
245 + $hasInvoiceFile = $customsDeclaration !== null && $customsDeclaration->hasInvoiceFileContent();
246 + $hasEadFile = $customsDeclaration !== null && $customsDeclaration->hasEadFileContent();
247 + $runWizardUrl = $this->wpAdapter->adminUrl( "admin.php?page=wc-orders&action=edit&id={$order->getNumber()}&wizard-enabled=true&wizard-order-detail-custom-declaration-enabled=true#packetery_customs_declaration_metabox" );
238 248
239 249 $this->latteEngine->render(
240 250 PACKETERY_PLUGIN_DIR . '/template/order/customs-declaration-metabox.latte',
241 251 [
@@ -241,13 +251,15 @@
241 251 [
242 252 'form' => $form,
243 253 'hasInvoiceFile' => $hasInvoiceFile,
244 254 'hasEadFile' => $hasEadFile,
255 + 'runWizardUrl' => $runWizardUrl,
245 256 'translations' => [
246 - 'addCustomsDeclarationItem' => __( 'Add item', 'packeta' ),
247 - 'delete' => __( 'Delete', 'packeta' ),
248 - 'itemsLabel' => __( 'Items', 'packeta' ),
249 - 'fileUploaded' => __( 'File uploaded.', 'packeta' ),
257 + 'addCustomsDeclarationItem' => $this->wpAdapter->__( 'Add item', 'packeta' ),
258 + 'delete' => $this->wpAdapter->__( 'Delete', 'packeta' ),
259 + 'itemsLabel' => $this->wpAdapter->__( 'Items', 'packeta' ),
260 + 'fileUploaded' => $this->wpAdapter->__( 'File uploaded.', 'packeta' ),
261 + 'runWizard' => $this->wpAdapter->__( 'Run customs declaration wizard', 'packeta' ),
250 262 ],
251 263 ]
252 264 );
253 265 }
@@ -264,8 +276,11 @@
264 276 private function createForm( array $structureData, ?Entity\CustomsDeclaration $customsDeclaration ): Form {
265 277 $form = $this->formFactory->create();
266 278
267 279 $activator = $form->addCheckbox( self::FORM_ACTIVATOR_NAME, __( 'View/hide customs declaration form', 'packeta' ) );
280 + if ( $this->request->getQuery( 'wizard-order-detail-custom-declaration-enabled' ) === 'true' ) {
281 + $activator->setValue( true );
282 + }
268 283 $activator
269 284 ->addCondition( Form::FILLED )
270 285 ->toggle( 'customs-declaration-container' );
271 286
@@ -295,14 +310,15 @@
295 310 ->setRequired();
296 311
297 312 $prefixContainer->addText( 'invoice_issue_date', __( 'Invoice issue date', 'packeta' ) )
298 313 ->addConditionOn( $activator, Form::FILLED )
299 - ->setRequired();
314 + ->setRequired()
315 + ->addRule( ...FormRules::getDateParameters() );
300 316
301 317 $invoiceFile = $prefixContainer->addUpload( 'invoice_file', __( 'Invoice PDF file', 'packeta' ) )
302 318 ->setRequired( false );
303 319
304 - if ( null === $customsDeclaration || false === $customsDeclaration->hasInvoiceFileContent() ) {
320 + if ( $customsDeclaration === null || $customsDeclaration->hasInvoiceFileContent() === false ) {
305 321 $invoiceFile
306 322 ->addConditionOn( $activator, Form::FILLED )
307 323 ->addConditionOn( $ead, Form::EQUAL, self::EAD_OWN )
308 324 ->setRequired()
@@ -323,9 +339,9 @@
323 339 ->setRequired( false )
324 340 ->addConditionOn( $ead, Form::EQUAL, self::EAD_OWN )
325 341 ->toggle( 'customs-declaration-own-field-ead_file' );
326 342
327 - if ( null === $customsDeclaration || false === $customsDeclaration->hasEadFileContent() ) {
343 + if ( $customsDeclaration === null || $customsDeclaration->hasEadFileContent() === false ) {
328 344 $eadFile
329 345 ->addConditionOn( $activator, Form::FILLED )
330 346 ->addConditionOn( $ead, Form::EQUAL, self::EAD_OWN )
331 347 ->setRequired();
@@ -334,12 +350,14 @@
334 350 $form->addSubmit( 'save' );
335 351
336 352 $items = $prefixContainer->addContainer( 'items' );
337 353
338 - if ( empty( $structureData[ self::FORM_CONTAINER_NAME ]['items'] ) ) {
354 + $itemsData = $structureData[ self::FORM_CONTAINER_NAME ]['items'] ?? null;
355 +
356 + if ( $itemsData === null ) {
339 357 $this->addCustomsDeclarationItem( $activator, $items, 'new_0' );
340 358 } else {
341 - foreach ( $structureData[ self::FORM_CONTAINER_NAME ]['items'] as $itemId => $itemDefaults ) {
359 + foreach ( $itemsData as $itemId => $itemDefaults ) {
342 360 $this->addCustomsDeclarationItem( $activator, $items, (string) $itemId );
343 361 }
344 362 }
345 363
@@ -351,9 +369,9 @@
351 369
352 370 /**
353 371 * On form error.
354 372 *
355 - * @param \Packetery\Nette\Forms\Form $form Form.
373 + * @param Form $form Form.
356 374 * @return void
357 375 */
358 376 public function onFormError( Form $form ): void {
359 377 /** Form input control. @var BaseControl[] $controls */
@@ -358,14 +376,16 @@
358 376 public function onFormError( Form $form ): void {
359 377 /** Form input control. @var BaseControl[] $controls */
360 378 $controls = $form->getComponents( true, BaseControl::class );
361 379 foreach ( $controls as $control ) {
362 - foreach ( $control->getErrors() as $error ) {
363 - $this->messageManager->flashMessageObject(
364 - Message::create()
365 - ->setText( sprintf( '%s: %s', $control->getCaption(), $error ) )
366 - ->setType( MessageManager::TYPE_ERROR )
367 - );
380 + if ( $control instanceof BaseControl ) {
381 + foreach ( $control->getErrors() as $error ) {
382 + $this->messageManager->flashMessageObject(
383 + Message::create()
384 + ->setText( sprintf( '%s: %s', $control->getCaption(), $error ) )
385 + ->setType( MessageManager::TYPE_ERROR )
386 + );
387 + }
368 388 }
369 389 }
370 390 }
371 391
@@ -376,14 +396,14 @@
376 396 * @return void
377 397 */
378 398 public function onFormSuccess( Form $form ): void {
379 399 $order = $this->detailCommonLogic->getOrder();
380 - if ( null === $order ) {
400 + if ( $order === null ) {
381 401 return;
382 402 }
383 403
384 404 $fieldsToOmit = [];
385 - /** Form container. @var Container $customsDeclarationContainer */
405 + /** @var Container $customsDeclarationContainer */
386 406 $customsDeclarationContainer = $form[ self::FORM_CONTAINER_NAME ];
387 407 $prefixedValues = $form->getValues( 'array' );
388 408 $containerValues = $prefixedValues[ self::FORM_CONTAINER_NAME ];
389 409 $items = $containerValues['items'];
@@ -388,9 +408,9 @@
388 408 $containerValues = $prefixedValues[ self::FORM_CONTAINER_NAME ];
389 409 $items = $containerValues['items'];
390 410 unset( $containerValues['items'] );
391 411
392 - if ( false === $prefixedValues[ self::FORM_ACTIVATOR_NAME ] ) {
412 + if ( $prefixedValues[ self::FORM_ACTIVATOR_NAME ] === false ) {
393 413 return;
394 414 }
395 415
396 416 $this->processUploadedFile(
@@ -408,15 +428,15 @@
408 428 $customsDeclarationContainer,
409 429 $fieldsToOmit
410 430 );
411 431
412 - if ( '' === $containerValues['mrn'] ) {
432 + if ( $containerValues['mrn'] === '' ) {
413 433 $containerValues['mrn'] = null;
414 434 }
415 435
416 436 $containerValues['id'] = null;
417 437 $oldCustomsDeclaration = $this->customsDeclarationRepository->getByOrderNumber( $order->getNumber() );
418 - if ( null !== $oldCustomsDeclaration ) {
438 + if ( $oldCustomsDeclaration !== null ) {
419 439 $containerValues['id'] = $oldCustomsDeclaration->getId();
420 440 }
421 441
422 442 $customsDeclaration = $this->customsDeclarationEntityFactory->fromStandardizedStructure( $containerValues, $order->getNumber() );
@@ -433,9 +453,9 @@
433 453 }
434 454 }
435 455
436 456 foreach ( $items as $itemId => $item ) {
437 - if ( 0 === strpos( (string) $itemId, 'new_' ) ) {
457 + if ( strpos( (string) $itemId, 'new_' ) === 0 ) {
438 458 $itemId = null;
439 459 } else {
440 460 $itemId = (string) $itemId;
441 461 }
@@ -492,9 +512,9 @@
492 512 ->addRule( Form::FLOAT )
493 513 ->addRule( ...FormRules::getGreaterThanParameters( 0 ) )
494 514 ->addFilter(
495 515 static function ( float $value ): float {
496 - return Helper::simplifyWeight( $value );
516 + return CoreHelper::simplifyWeight( $value );
497 517 }
498 518 )
499 519 ->addRule( ...FormRules::getGreaterThanParameters( 0 ) );
500 520
@@ -508,9 +528,9 @@
508 528 * @param string $key File key.
509 529 * @param string $relatedFileIdKey Related file id key.
510 530 * @param array $containerValues Container values.
511 531 * @param Container $formContainer Form container.
512 - * @param array $fieldsToOmit Fields to omit.
532 + * @param string[] $fieldsToOmit Fields to omit.
513 533 *
514 534 * @return void
515 535 */
516 536 private function processUploadedFile( string $key, string $relatedFileIdKey, array &$containerValues, Container $formContainer, array &$fieldsToOmit ): void {
@@ -516,36 +536,38 @@
516 536 private function processUploadedFile( string $key, string $relatedFileIdKey, array &$containerValues, Container $formContainer, array &$fieldsToOmit ): void {
517 537 $fileUpload = $containerValues[ $key ];
518 538 $uploadControl = $formContainer[ $key ];
519 539
520 - if ( $fileUpload->hasFile() && 0 >= $fileUpload->getSize() ) {
521 - $formContainer[ $key ]->addError( __( 'Uploaded file is empty.', 'packeta' ) );
522 - $fileUpload = new FileUpload( null );
523 - }
540 + if ( $uploadControl instanceof UploadControl ) {
541 + if ( $fileUpload->hasFile() && $fileUpload->getSize() <= 0 ) {
542 + $uploadControl->addError( __( 'Uploaded file is empty.', 'packeta' ) );
543 + $fileUpload = new FileUpload( null );
544 + }
524 545
525 - if ( $fileUpload->hasFile() && self::MAX_UPLOAD_FILE_MEGABYTES * 1024 * 1024 === $fileUpload->getSize() ) {
526 - // translators: %d is numeric value.
527 - $formContainer[ $key ]->addError( sprintf( __( 'Uploaded file is too big for storage. Max size is %d MB.', 'packeta' ), self::MAX_UPLOAD_FILE_MEGABYTES ) );
528 - $fileUpload = new FileUpload( null );
529 - }
546 + if ( $fileUpload->hasFile() && self::MAX_UPLOAD_FILE_MEGABYTES * 1024 * 1024 === $fileUpload->getSize() ) {
547 + // translators: %d is numeric value.
548 + $uploadControl->addError( sprintf( __( 'Uploaded file is too big for storage. Max size is %d MB.', 'packeta' ), self::MAX_UPLOAD_FILE_MEGABYTES ) );
549 + $fileUpload = new FileUpload( null );
550 + }
530 551
531 - if ( $fileUpload->hasFile() && $fileUpload->isOk() ) {
532 - $containerValues[ $key ] = static function () use ( $fileUpload ): string {
533 - return $fileUpload->getContents();
534 - };
535 - $containerValues[ $relatedFileIdKey ] = null;
536 - }
552 + if ( $fileUpload->hasFile() && $fileUpload->isOk() ) {
553 + $containerValues[ $key ] = static function () use ( $fileUpload ): string {
554 + return $fileUpload->getContents();
555 + };
556 + $containerValues[ $relatedFileIdKey ] = null;
557 + }
537 558
538 - if ( $fileUpload->hasFile() && false === $fileUpload->isOk() ) {
539 - $containerValues[ $key ] = null;
540 - $containerValues[ $relatedFileIdKey ] = null;
541 - $uploadControl->addError( __( 'File failed to upload.', 'packeta' ) );
542 - }
559 + if ( $fileUpload->hasFile() && $fileUpload->isOk() === false ) {
560 + $containerValues[ $key ] = null;
561 + $containerValues[ $relatedFileIdKey ] = null;
562 + $uploadControl->addError( __( 'File failed to upload.', 'packeta' ) );
563 + }
543 564
544 - if ( $containerValues[ $key ] instanceof FileUpload ) {
545 - $containerValues[ $key ] = null;
546 - $containerValues[ $relatedFileIdKey ] = null;
547 - $fieldsToOmit[] = $key;
548 - $fieldsToOmit[] = $relatedFileIdKey;
565 + if ( $containerValues[ $key ] instanceof FileUpload ) {
566 + $containerValues[ $key ] = null;
567 + $containerValues[ $relatedFileIdKey ] = null;
568 + $fieldsToOmit[] = $key;
569 + $fieldsToOmit[] = $relatedFileIdKey;
570 + }
549 571 }
550 572 }
551 573 }