Skip to content

Commit 88334af

Browse files
fixes & updates
1 parent bda63d6 commit 88334af

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+662
-141
lines changed

app/base/abstracts/Controllers/AdminManageFrontendModelsPage.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Degami\Basics\Html\TagElement;
1919
use DI\DependencyException;
2020
use DI\NotFoundException;
21+
use App\Base\Abstracts\Models\BaseModel;
2122

2223
/**
2324
* Base for admin page that manages a Frontend Model
@@ -56,4 +57,12 @@ protected function getTranslationsButton($object): string
5657

5758
return '';
5859
}
60+
61+
protected function getModelRowButtons(BaseModel $object) : array
62+
{
63+
return [
64+
static::FRONTEND_BTN => $this->getFrontendModelButton($object),
65+
static::TRANSLATIONS_BTN => $this->getTranslationsButton($object),
66+
] + parent::getModelRowButtons($object);
67+
}
5968
}

app/base/abstracts/Controllers/AdminManageModelsPage.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use App\Base\Abstracts\Controllers\BasePage;
3434
use App\Site\Models\MediaElement;
3535
use Symfony\Component\HttpFoundation\Response;
36+
use Degami\PHPFormsApi as FAPI;
3637

3738
/**
3839
* Base for admin page that manages a Model
@@ -172,8 +173,11 @@ protected function collectActionButtons() : self
172173
}
173174
}
174175

175-
if (($this->template_data['action'] ?? 'list') != 'list') {
176+
if (!in_array(($this->template_data['action'] ?? 'list'), ['list', 'duplicate', 'delete',])) {
176177
$this->addBackButton();
178+
if (($this->template_data['action'] ?? 'list') != 'new') { // no need to remove something that is not there
179+
$this->addRemoveButton();
180+
}
177181
}
178182

179183
return $this;
@@ -739,6 +743,26 @@ public function getModelTableName(): mixed
739743
protected function beforeRender(): BasePage|Response
740744
{
741745
if ($this->getRequest()->query->get('action') == 'duplicate') {
746+
747+
$form = FAPI\FormBuilder::getForm([$this, 'getDuplicateFormDefinition'], $this->getFormId())
748+
->setValidate([[$this, 'duplicateFormValidate']])
749+
->setSubmit([[$this, 'duplicateFormSubmitted']]);
750+
751+
$this->template_data = [
752+
'action' => $this->getRequest()->query->get('action') ?? 'list',
753+
'form' => $form,
754+
];
755+
756+
$this->getApp()->event('before_form_process', ['form' => $form]);
757+
$form->process();
758+
759+
if ($form && $form->isSubmitted()) {
760+
$this->getApp()->event('form_submitted', ['form' => $form]);
761+
return $form->getSubmitResults(get_class($this) . '::duplicateFormSubmitted');
762+
}
763+
764+
/*
765+
// if we wish to use "javascript confirmation"
742766
$object = $this->getObject();
743767
$copy = $object?->duplicate()->persist();
744768
if ($copy) {
@@ -747,12 +771,46 @@ protected function beforeRender(): BasePage|Response
747771
} else {
748772
$this->addErrorFlashMessage($this->getUtils()->translate('Error duplicating object', locale: $this->getCurrentLocale()));
749773
return $this->doRedirect($this->getControllerUrl() . '?action=list');
750-
}
774+
}*/
751775
}
752776

753777
return parent::beforeRender();
754778
}
755779

780+
public function getDuplicateFormDefinition(FAPI\Form $form, array &$form_state): FAPI\Form
781+
{
782+
$object = $this->getObject();
783+
$this->fillConfirmationForm('Do you confirm the duplication of the selected element?', $form, $this->getControllerUrl() . '?action=edit&' . $this->getObjectIdQueryParam() . '=' . $object->getId());
784+
return $form;
785+
}
786+
787+
public function duplicateFormValidate(FAPI\Form $form, &$form_state): bool|string
788+
{
789+
return true;
790+
}
791+
792+
public function duplicateFormSubmitted(FAPI\Form $form, &$form_state): mixed
793+
{
794+
795+
$object = $this->getObject();
796+
$copy = $object?->duplicate()->persist();
797+
if ($copy) {
798+
$this->addSuccessFlashMessage($this->getUtils()->translate('Object duplicated successfully', locale: $this->getCurrentLocale()));
799+
return $this->doRedirect($this->getControllerUrl() . '?action=edit&' . $this->getObjectIdQueryParam() . '=' . $copy->getId());
800+
} else {
801+
$this->addErrorFlashMessage($this->getUtils()->translate('Error duplicating object', locale: $this->getCurrentLocale()));
802+
return $this->doRedirect($this->getControllerUrl() . '?action=list');
803+
}
804+
}
805+
806+
protected function getModelRowButtons(BaseModel $object) : array
807+
{
808+
return [
809+
static::EDIT_BTN => $this->getEditButton($object->getId()),
810+
static::DELETE_BTN => $this->getDeleteButton($object->getId()),
811+
];
812+
}
813+
756814
public static function exposeDataToDashboard() : mixed
757815
{
758816
return App::getInstance()->containerCall([static::getObjectClass(), 'getCollection'])->count();

app/base/abstracts/Controllers/AdminManageProductsPage.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ function ($product) {
9797
'Tax Class' => $product->getTaxClassId() ? TaxClass::load($product->getTaxClassId())?->getClassName() : 'N/A',
9898
'Price' => $product->getPrice(),
9999
'Is Physical' => $product->isPhysical() ? 'Yes' : 'No',
100-
'actions' => [
101-
static::EDIT_BTN => $this->getEditButton($product->id),
102-
static::DELETE_BTN => $this->getDeleteButton($product->id),
103-
],
100+
'actions' => $this->getModelRowButtons($product),
104101
];
105102
},
106103
$data

app/base/abstracts/Controllers/AdminPage.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,18 @@ public function addBackButton(?array $query_params = null) : void
283283
);
284284
}
285285

286+
public function addRemoveButton() : void
287+
{
288+
$this->addActionButton(
289+
'remove-btn',
290+
'remove-btn',
291+
$this->getHtmlRenderer()->getIcon('trash') . ' ' . $this->getUtils()->translate('Remove', locale: $this->getCurrentLocale()),
292+
'btn btn-sm btn-outline-danger with-enable-switch',
293+
['disabled' => 'disabled', 'data-goto-url' => $this->getControllerUrl() . '?action=delete&' . $this->getObjectIdQueryParam() . '=' . $this->getObject()->getId(), ],
294+
order: PHP_INT_MAX - 1
295+
);
296+
}
297+
286298
/**
287299
* get sidebar size
288300
*

app/base/controllers/Admin/Blocks.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,7 @@ function ($e) {
443443
)
444444
),
445445
'Order' => $block->order,
446-
'actions' => [
447-
static::EDIT_BTN => $this->getEditButton($block->id),
448-
static::DELETE_BTN => $this->getDeleteButton($block->id),
449-
],
446+
'actions' => $this->getModelRowButtons($block),
450447
];
451448
},
452449
$data

app/base/controllers/Admin/Commerce/Carts.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use DI\DependencyException;
1919
use DI\NotFoundException;
2020
use Exception;
21-
use App\Base\Abstracts\Controllers\AdminManageFrontendModelsPage;
21+
use App\Base\Abstracts\Controllers\AdminManageModelsPage;
2222
use App\Base\Models\Address;
2323
use Degami\PHPFormsApi as FAPI;
2424
use App\Base\Models\Cart as CartModel;
@@ -34,7 +34,7 @@
3434
/**
3535
* "Carts" Admin Page
3636
*/
37-
class Carts extends AdminManageFrontendModelsPage
37+
class Carts extends AdminManageModelsPage
3838
{
3939
/**
4040
* {@inheritdoc}
@@ -497,10 +497,7 @@ function (CartModel $cart) {
497497
'Total' => $this->getUtils()->formatPrice($cart->getTotalInclTax(), $cart->getCurrencyCode()),
498498
'Created At' => $cart->getCreatedAt(),
499499
'Updated At' => $cart->getUpdatedAt(),
500-
'actions' => [
501-
static::EDIT_BTN => $this->getEditButton($cart->id),
502-
static::DELETE_BTN => $this->getDeleteButton($cart->id),
503-
],
500+
'actions' => $this->getModelRowButtons($cart),
504501
];
505502
},
506503
$data

app/base/controllers/Admin/Commerce/Discounts.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use DI\DependencyException;
1919
use DI\NotFoundException;
2020
use Exception;
21-
use App\Base\Abstracts\Controllers\AdminManageFrontendModelsPage;
21+
use App\Base\Abstracts\Controllers\AdminManageModelsPage;
2222
use Degami\PHPFormsApi as FAPI;
2323
use App\Base\Models\Discount as DiscountModel;
2424
use Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException;
@@ -28,7 +28,7 @@
2828
/**
2929
* "Discounts" Admin Page
3030
*/
31-
class Discounts extends AdminManageFrontendModelsPage
31+
class Discounts extends AdminManageModelsPage
3232
{
3333
/**
3434
* @var string page title
@@ -143,7 +143,8 @@ public function getFormDefinition(FAPI\Form $form, array &$form_state): FAPI\For
143143
'type' => 'switchbox',
144144
'title' => 'Active',
145145
'default_value' => $discount->getActive(),
146-
])->addField('discount_amount', [
146+
])
147+
->addField('discount_amount', [
147148
'type' => 'textfield',
148149
'title' => 'Discount Amount',
149150
'validate' => ['required', 'numeric'],
@@ -157,7 +158,25 @@ public function getFormDefinition(FAPI\Form $form, array &$form_state): FAPI\For
157158
'percentage' => '% of Total',
158159
],
159160
'default_value' => $discount->getDiscountType(),
160-
]);
161+
])
162+
->addMarkup('<div class="row">')
163+
->addField('max_usages', [
164+
'type' => 'textfield',
165+
'title' => 'Maximum Usages',
166+
'validate' => ['numeric'],
167+
'default_value' => $discount->getMaxUsages() ?? -1,
168+
'container_class' => 'col-md-6',
169+
'description' => 'Set how many times this discount can be used in total. Set -1 for unlimited.',
170+
])
171+
->addField('max_usages_per_user', [
172+
'type' => 'textfield',
173+
'title' => 'Maximum Usages Per User',
174+
'validate' => ['numeric'],
175+
'default_value' => $discount->getMaxUsagesPerUser() ?? -1,
176+
'container_class' => 'col-md-6',
177+
'description' => 'Set how many times this discount can be used by a single user. Set -1 for unlimited.',
178+
])
179+
->addMarkup('</div>');
161180

162181
$this->addSubmitButton($form);
163182

@@ -275,12 +294,9 @@ function ($discount) {
275294
'Title' => $discount->getTitle(),
276295
'Code' => $discount->getCode(),
277296
'Active' => $discount->getActive() ? 'Yes' : 'No',
278-
'Discount Amount' => $discount->getDiscountAmount(),
297+
'Discount Amount' => number_format($discount->getDiscountAmount(), 2),
279298
'Discount Type' => $discount->getDiscountType(),
280-
'actions' => [
281-
static::EDIT_BTN => $this->getEditButton($discount->id),
282-
static::DELETE_BTN => $this->getDeleteButton($discount->id),
283-
],
299+
'actions' => $this->getModelRowButtons($discount),
284300
];
285301
},
286302
$data

app/base/controllers/Admin/Commerce/OrderPayments.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use DI\DependencyException;
1919
use DI\NotFoundException;
2020
use Exception;
21-
use App\Base\Abstracts\Controllers\AdminManageFrontendModelsPage;
21+
use App\Base\Abstracts\Controllers\AdminManageModelsPage;
2222
use Degami\PHPFormsApi as FAPI;
2323
use App\Base\Models\OrderPayment as OrderPaymentModel;
2424
use Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException;
@@ -28,7 +28,7 @@
2828
/**
2929
* "Order Payments" Admin Page
3030
*/
31-
class OrderPayments extends AdminManageFrontendModelsPage
31+
class OrderPayments extends AdminManageModelsPage
3232
{
3333
/**
3434
* @var string page title
@@ -248,10 +248,8 @@ function ($orderPayment) {
248248
'Transaction Id' => $orderPayment->getTransactionId(),
249249
'Transaction Amount' => $this->getUtils()->formatPrice($orderPayment->getTransactionAmount(), $orderPayment->getCurrencyCode()),
250250
'actions' => [
251-
static::VIEW_BTN => $this->getViewButton($orderPayment->id),
252-
static::EDIT_BTN => $this->getEditButton($orderPayment->id),
253-
static::DELETE_BTN => $this->getDeleteButton($orderPayment->id),
254-
],
251+
static::VIEW_BTN => $this->getViewButton($orderPayment->id),
252+
] + $this->getModelRowButtons($orderPayment),
255253
];
256254
},
257255
$data

app/base/controllers/Admin/Commerce/OrderShipments.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use DI\DependencyException;
2222
use DI\NotFoundException;
2323
use Exception;
24-
use App\Base\Abstracts\Controllers\AdminManageFrontendModelsPage;
24+
use App\Base\Abstracts\Controllers\AdminManageModelsPage;
2525
use Degami\PHPFormsApi as FAPI;
2626
use App\Base\Models\OrderShipment as OrderShipmentModel;
2727
use Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException;
@@ -32,7 +32,7 @@
3232
/**
3333
* "Order Shipments" Admin Page
3434
*/
35-
class OrderShipments extends AdminManageFrontendModelsPage
35+
class OrderShipments extends AdminManageModelsPage
3636
{
3737
public function __construct(
3838
protected ContainerInterface $container,
@@ -328,9 +328,7 @@ function ($orderShipment) {
328328
'Status' => $orderShipment->getStatus(),
329329
'actions' => [
330330
static::VIEW_BTN => $this->getViewButton($orderShipment->id),
331-
static::EDIT_BTN => $this->getEditButton($orderShipment->id),
332-
static::DELETE_BTN => $this->getDeleteButton($orderShipment->id),
333-
],
331+
] + $this->getModelRowButtons($orderShipment),
334332
];
335333
},
336334
$data

app/base/controllers/Admin/Commerce/OrderStatuses.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use DI\DependencyException;
1919
use DI\NotFoundException;
2020
use Exception;
21-
use App\Base\Abstracts\Controllers\AdminManageFrontendModelsPage;
21+
use App\Base\Abstracts\Controllers\AdminManageModelsPage;
2222
use Degami\PHPFormsApi as FAPI;
2323
use App\Base\Models\OrderStatus as OrderStatusModel;
2424
use Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException;
@@ -28,7 +28,7 @@
2828
/**
2929
* "Order Statuses" Admin Page
3030
*/
31-
class OrderStatuses extends AdminManageFrontendModelsPage
31+
class OrderStatuses extends AdminManageModelsPage
3232
{
3333
/**
3434
* @var string page title
@@ -243,10 +243,7 @@ function ($orderStatus) {
243243
'ID' => $orderStatus->id,
244244
'Website' => $orderStatus->getWebsiteId() == null ? 'All websites' : $orderStatus->getWebsite()->domain,
245245
'Status' => $orderStatus->getStatus(),
246-
'actions' => [
247-
static::EDIT_BTN => $this->getEditButton($orderStatus->id),
248-
static::DELETE_BTN => $this->getDeleteButton($orderStatus->id),
249-
],
246+
'actions' => $this->getModelRowButtons($orderStatus),
250247
];
251248
},
252249
$data

0 commit comments

Comments
 (0)