|
29 | 29 | use App\App; |
30 | 30 | use App\Base\Abstracts\Controllers\BasePage; |
31 | 31 | use App\Base\Abstracts\Models\BaseCollection; |
| 32 | +use App\Site\Models\Book; |
32 | 33 | use Degami\Basics\Html\TagElement; |
33 | 34 | use App\Site\Models\DownloadableProduct; |
34 | 35 | use Exception; |
@@ -335,13 +336,30 @@ public function getFormDefinition(FAPI\Form $form, array &$form_state): FAPI\For |
335 | 336 | ]); |
336 | 337 | } |
337 | 338 |
|
338 | | - if ($this->getRequest()->query->get('product_id')) { |
| 339 | + if ($this->getRequest()->query->get('product_type') == 'downloadable' && $this->getRequest()->query->get('product_id')) { |
339 | 340 | /** @var DownloadableProduct $product */ |
340 | 341 | $product = $this->containerCall([DownloadableProduct::class, 'load'], ['id' => $this->getRequest()->query->get('product_id')]); |
341 | 342 | $form->addField('product_id', [ |
342 | 343 | 'type' => 'hidden', |
343 | 344 | 'default_value' => $product->getId(), |
344 | 345 | ]); |
| 346 | + $form->addField('product_type', [ |
| 347 | + 'type' => 'hidden', |
| 348 | + 'default_value' => 'downloadable', |
| 349 | + ]); |
| 350 | + } |
| 351 | + |
| 352 | + if ($this->getRequest()->query->get('product_type') == 'downloadable' && $this->getRequest()->query->get('product_id')) { |
| 353 | + /** @var Book $product */ |
| 354 | + $product = $this->containerCall([Book::class, 'load'], ['id' => $this->getRequest()->query->get('product_id')]); |
| 355 | + $form->addField('product_id', [ |
| 356 | + 'type' => 'hidden', |
| 357 | + 'default_value' => $product->getId(), |
| 358 | + ]); |
| 359 | + $form->addField('product_type', [ |
| 360 | + 'type' => 'hidden', |
| 361 | + 'default_value' => 'book', |
| 362 | + ]); |
345 | 363 | } |
346 | 364 |
|
347 | 365 | break; |
@@ -470,6 +488,58 @@ function ($product) use ($not_in) { |
470 | 488 | 'suffix' => '<br /><br />', |
471 | 489 | ])->addMarkup('<a class="btn btn-danger btn-sm" href="' . $this->getUrl('crud.app.site.controllers.admin.json.downloadablemedia', ['id' => $product->getId()]) . '?page_id=' . $product->getId() . '&action=new">Cancel</a>'); |
472 | 490 |
|
| 491 | + $this->addSubmitButton($form, true); |
| 492 | + break; |
| 493 | + case 'book_assoc': |
| 494 | + $not_in = array_map( |
| 495 | + function ($el) { |
| 496 | + return $el->page_id; |
| 497 | + }, |
| 498 | + $this->getDb()->book_media_elementList()->where('media_element_id', $media->getId())->fetchAll() |
| 499 | + ); |
| 500 | + |
| 501 | + $products = array_filter( |
| 502 | + array_map( |
| 503 | + function ($product) use ($not_in) { |
| 504 | + /** @var Book $product */ |
| 505 | + if (in_array($product->getId(), $not_in)) { |
| 506 | + return null; |
| 507 | + } |
| 508 | + |
| 509 | + return ['title' => $product->getTitle() . ' - ' . $product->getRewrite()->getUrl(), 'id' => $product->getId()]; |
| 510 | + }, |
| 511 | + Book::getCollection()->getItems() |
| 512 | + ) |
| 513 | + ); |
| 514 | + |
| 515 | + $products = array_combine(array_column($products, 'id'), array_column($products, 'title')); |
| 516 | + |
| 517 | + $form->addField('product_id', [ |
| 518 | + 'type' => 'select', |
| 519 | + 'options' => ['' => ''] + $products, |
| 520 | + 'default_value' => '', |
| 521 | + ])->addField('media_id', [ |
| 522 | + 'type' => 'hidden', |
| 523 | + 'default_value' => $media->getId(), |
| 524 | + ]); |
| 525 | + |
| 526 | + $this->addSubmitButton($form, true); |
| 527 | + break; |
| 528 | + case 'book_deassoc': |
| 529 | + /** @var Book $product */ |
| 530 | + $product = $this->containerCall([Book::class, 'load'], ['id' => $this->getRequest()->query->get('product_id')]); |
| 531 | + $form->addField('product_id', [ |
| 532 | + 'type' => 'hidden', |
| 533 | + 'default_value' => $product->getId(), |
| 534 | + ])->addField('media_id', [ |
| 535 | + 'type' => 'hidden', |
| 536 | + 'default_value' => $media->getId(), |
| 537 | + ])->addField('confirm', [ |
| 538 | + 'type' => 'markup', |
| 539 | + 'value' => 'Do you confirm the disassociation of the selected element from the "' . $product->getTitle() . '" product (ID: ' . $product->getId() . ') ?', |
| 540 | + 'suffix' => '<br /><br />', |
| 541 | + ])->addMarkup('<a class="btn btn-danger btn-sm" href="' . $this->getUrl('crud.app.site.controllers.admin.json.bookmedia', ['id' => $product->getId()]) . '?page_id=' . $product->getId() . '&action=new">Cancel</a>'); |
| 542 | + |
473 | 543 | $this->addSubmitButton($form, true); |
474 | 544 | break; |
475 | 545 | case 'delete': |
@@ -578,8 +648,10 @@ public function formSubmitted(FAPI\Form $form, &$form_state): mixed |
578 | 648 |
|
579 | 649 | if ($values['page_id'] != null) { |
580 | 650 | $this->containerCall([Page::class, 'load'], ['id' => $values['page_id']])->addMedia($media); |
581 | | - } else if ($values['product_id'] != null) { |
| 651 | + } else if ($values['product_type'] == 'downloadable' && $values['product_id'] != null) { |
582 | 652 | $this->containerCall([DownloadableProduct::class, 'load'], ['id' => $values['product_id']])->addMedia($media); |
| 653 | + } else if ($values['product_type'] == 'book' && $values['product_id'] != null) { |
| 654 | + $this->containerCall([Book::class, 'load'], ['id' => $values['product_id']])->addMedia($media); |
583 | 655 | } else { |
584 | 656 | $this->addSuccessFlashMessage($this->getUtils()->translate("Media Saved.")); |
585 | 657 | } |
|
0 commit comments