Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CreateOfferingSelect.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Filament\Forms\Components;
  3. use App\Filament\Company\Resources\Common\OfferingResource;
  4. use App\Models\Common\Offering;
  5. use Filament\Forms\Components\Actions\Action;
  6. use Filament\Forms\Components\Select;
  7. use Filament\Forms\Form;
  8. use Filament\Forms\Get;
  9. use Filament\Support\Enums\MaxWidth;
  10. class CreateOfferingSelect extends Select
  11. {
  12. protected bool $isPurchasable = true;
  13. protected bool $isSellable = true;
  14. public function purchasable(bool $condition = true): static
  15. {
  16. $this->isPurchasable = $condition;
  17. $this->isSellable = false;
  18. return $this;
  19. }
  20. public function sellable(bool $condition = true): static
  21. {
  22. $this->isSellable = $condition;
  23. $this->isPurchasable = false;
  24. return $this;
  25. }
  26. protected function setUp(): void
  27. {
  28. parent::setUp();
  29. $this
  30. ->searchable()
  31. ->preload()
  32. ->createOptionForm(fn (Form $form) => $this->createOfferingForm($form))
  33. ->createOptionAction(fn (Action $action) => $this->createOfferingAction($action));
  34. $this->relationship(
  35. $this->isPurchasable() && ! $this->isSellable() ? 'purchasableOffering' :
  36. ($this->isSellable() && ! $this->isPurchasable() ? 'sellableOffering' : 'offering'),
  37. 'name'
  38. );
  39. $this->createOptionUsing(function (array $data) {
  40. if ($this->isSellableAndPurchasable()) {
  41. $data['sellable'] = isset($data['attributes']) && in_array('Sellable', $data['attributes'], true);
  42. $data['purchasable'] = isset($data['attributes']) && in_array('Purchasable', $data['attributes'], true);
  43. } else {
  44. $data['sellable'] = $this->isSellable;
  45. $data['purchasable'] = $this->isPurchasable;
  46. }
  47. unset($data['attributes']);
  48. $offering = Offering::create($data);
  49. return $offering->getKey();
  50. });
  51. }
  52. protected function createOfferingForm(Form $form): Form
  53. {
  54. return $form->schema([
  55. OfferingResource::getGeneralSection($this->isSellableAndPurchasable()),
  56. OfferingResource::getSellableSection()->visible(
  57. fn (Get $get) => $this->isSellableAndPurchasable()
  58. ? in_array('Sellable', $get('attributes') ?? [])
  59. : $this->isSellable()
  60. ),
  61. OfferingResource::getPurchasableSection()->visible(
  62. fn (Get $get) => $this->isSellableAndPurchasable()
  63. ? in_array('Purchasable', $get('attributes') ?? [])
  64. : $this->isPurchasable()
  65. ),
  66. ]);
  67. }
  68. protected function createOfferingAction(Action $action): Action
  69. {
  70. return $action
  71. ->label('Add offering')
  72. ->slideOver()
  73. ->modalWidth(MaxWidth::ThreeExtraLarge);
  74. }
  75. public function isSellable(): bool
  76. {
  77. return $this->isSellable;
  78. }
  79. public function isPurchasable(): bool
  80. {
  81. return $this->isPurchasable;
  82. }
  83. public function isSellableAndPurchasable(): bool
  84. {
  85. return $this->isSellable && $this->isPurchasable;
  86. }
  87. }