Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CreateOfferingSelect.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. name: fn () => $this->isPurchasable() && ! $this->isSellable() ? 'purchasableOffering' : ($this->isSellable() && ! $this->isPurchasable() ? 'sellableOffering' : 'offering'),
  36. titleAttribute: 'name'
  37. );
  38. $this->createOptionUsing(function (array $data, Form $form) {
  39. if ($this->isSellableAndPurchasable()) {
  40. $attributes = array_flip($data['attributes'] ?? []);
  41. $data['sellable'] = isset($attributes['Sellable']);
  42. $data['purchasable'] = isset($attributes['Purchasable']);
  43. } else {
  44. $data['sellable'] = $this->isSellable;
  45. $data['purchasable'] = $this->isPurchasable;
  46. }
  47. unset($data['attributes']);
  48. $offering = Offering::create($data);
  49. $form->model($offering)->saveRelationships();
  50. return $offering->getKey();
  51. });
  52. }
  53. protected function createOfferingForm(Form $form): Form
  54. {
  55. return $form->schema([
  56. OfferingResource::getGeneralSection($this->isSellableAndPurchasable()),
  57. OfferingResource::getSellableSection()->visible(
  58. fn (Get $get) => $this->isSellableAndPurchasable()
  59. ? in_array('Sellable', $get('attributes') ?? [])
  60. : $this->isSellable()
  61. ),
  62. OfferingResource::getPurchasableSection()->visible(
  63. fn (Get $get) => $this->isSellableAndPurchasable()
  64. ? in_array('Purchasable', $get('attributes') ?? [])
  65. : $this->isPurchasable()
  66. ),
  67. ]);
  68. }
  69. protected function createOfferingAction(Action $action): Action
  70. {
  71. return $action
  72. ->label('Create offering')
  73. ->slideOver()
  74. ->modalWidth(MaxWidth::ThreeExtraLarge)
  75. ->modalHeading('Create a new offering');
  76. }
  77. public function isSellable(): bool
  78. {
  79. return $this->isSellable;
  80. }
  81. public function isPurchasable(): bool
  82. {
  83. return $this->isPurchasable;
  84. }
  85. public function isSellableAndPurchasable(): bool
  86. {
  87. return $this->isSellable && $this->isPurchasable;
  88. }
  89. }