From 82dc9ec163d0e9975b267d29062155301561d967 Mon Sep 17 00:00:00 2001 From: Devansh Bawari Date: Mon, 28 Aug 2023 15:26:39 +0530 Subject: [PATCH] fix: fixed mass update options and range options --- .../Admin/src/Resources/lang/en/app.php | 93 ++++++++------- packages/Webkul/DataGrid/src/Column.php | 112 ++++++++++++------ packages/Webkul/DataGrid/src/DataGrid.php | 16 ++- .../DataGrid/src/Enums/FormInputTypeEnum.php | 16 +++ .../DataGrid/src/Enums/RangeOptionEnum.php | 46 +++++++ packages/Webkul/DataGrid/src/MassAction.php | 1 + 6 files changed, 201 insertions(+), 83 deletions(-) create mode 100644 packages/Webkul/DataGrid/src/Enums/FormInputTypeEnum.php create mode 100644 packages/Webkul/DataGrid/src/Enums/RangeOptionEnum.php diff --git a/packages/Webkul/Admin/src/Resources/lang/en/app.php b/packages/Webkul/Admin/src/Resources/lang/en/app.php index 23915f3c77a..693883698c4 100755 --- a/packages/Webkul/Admin/src/Resources/lang/en/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/en/app.php @@ -60,7 +60,7 @@ 'price' => 'Price', 'tax' => 'Tax', 'sub-total' => 'Sub Total', - 'discount' => 'Discount', + 'discount' => 'Discount', 'shipping-and-handling' => 'Shipping and Handling', 'total-paid' => 'Total Paid', 'total-refund' => 'Total Refund', @@ -135,7 +135,7 @@ 'item-refunded' => 'Refunded (:qty_refunded)', 'qty-to-ship' => 'Qty. To Ship', 'qty-available' => 'Qty. Available', - 'create-success' => 'Shipment created successfully' + 'create-success' => 'Shipment created successfully', ], 'view' => [ @@ -234,7 +234,6 @@ 'create-success' => 'Refund created successfully', 'invalid-refund-amount-error' => 'Refund amount should be non zero.', - ], ], @@ -304,8 +303,8 @@ 'index' => [ 'login-message' => 'you logged in as :customer_name', 'title' => 'Customers', - - 'datagrid' => [ + + 'datagrid' => [ 'id' => 'Customer ID', 'order' => ':order Order(s)', 'address' => ':address Address(s)', @@ -370,7 +369,7 @@ 'view' => [ 'address' => 'Address', - 'back-btn' =>'Back', + 'back-btn' => 'Back', 'add-note' => 'Add Note', 'active' => 'Active', 'address-delete-success' => 'Address Deleted Successfully', @@ -439,11 +438,11 @@ 'group-default' => 'Default Group Can not be Deleted', ], - 'datagrid' => [ - 'code' => 'Code', - 'id' => 'ID', - 'name' => 'Name', - 'edit' => 'Edit', + 'datagrid' => [ + 'code' => 'Code', + 'id' => 'ID', + 'name' => 'Name', + 'edit' => 'Edit', 'delete' => 'Delete', ], ], @@ -478,7 +477,7 @@ 'update-success' => 'Update Successfully', ], - 'datagrid' => [ + 'datagrid' => [ 'customer_names' => 'Name', 'comment' => 'Comment', 'date' => 'Date', @@ -509,7 +508,7 @@ 'address-2' => 'Address 2', 'city' => 'City', 'state' => 'State', - 'select-country' => "Select Country", + 'select-country' => 'Select Country', 'country' => 'Country', 'default-address' => 'Default Address', 'first-name' => 'First Name', @@ -529,7 +528,7 @@ 'address-2' => 'Address 2', 'city' => 'City', 'state' => 'State', - 'select-country' => "Select Country", + 'select-country' => 'Select Country', 'country' => 'Country', 'default-address' => 'Default Address', 'first-name' => 'First Name', @@ -563,7 +562,7 @@ 'delete' => 'Delete', ], - 'create' => [ + 'create' => [ 'code' => 'Code', 'name' => 'Name', 'direction' => 'Direction', @@ -614,15 +613,12 @@ ], 'edit' => [ - 'title' => 'Edit Currency', - 'success' => 'Currency updated successfully.', + 'title' => 'Edit Currency', + 'success' => 'Currency updated successfully.', 'delete-success' => 'Currency deleted successfully.', ], ], - - - ], 'exchange-rates' => [ @@ -737,16 +733,16 @@ 'taxes' => [ 'tax-categories' => [ 'index' => [ - 'title' => 'Tax Categories', - 'tax-category' => 'Tax Category', + 'title' => 'Tax Categories', + 'tax-category' => 'Tax Category', 'delete-success' => 'Tax Category Deleted Successfully', 'datagrid' => [ - 'id' => 'ID', - 'name' => 'Name', - 'code' => 'Code', - 'edit' => 'Edit', - 'delete' => 'Delete', + 'id' => 'ID', + 'name' => 'Name', + 'code' => 'Code', + 'edit' => 'Edit', + 'delete' => 'Delete', 'actions' => 'Actions', ], @@ -761,7 +757,7 @@ 'title' => 'Tax Categories', 'add-tax-rates' => 'Add Tax Rates', 'empty-text' => 'Tax Rates are not available please create new Tax Rates.', - 'success' => 'New Tax Category Created', + 'success' => 'New Tax Category Created', ], 'edit' => [ @@ -950,8 +946,8 @@ 'edit' => [ 'title' => 'Edit User', - 'success' => 'User updated successfully.' - ] + 'success' => 'User updated successfully.', + ], ], 'edit' => [ @@ -1485,7 +1481,6 @@ 'update-status' => 'Update Status', 'delete-success' => 'Selected :resource were successfully deleted', - ], ], @@ -1846,13 +1841,13 @@ 'path-info' => 'Example: "/sitemap/" or "/" for base path', 'save-btn' => 'Save Sitemap', 'title' => 'Create Sitemap', - 'success' => 'Sitemap created successfully' + 'success' => 'Sitemap created successfully', ], - 'edit' => [ - 'title' => 'Edit Sitemap', - 'success' => 'Sitemap Updated successfully', - 'delete-success' => 'Sitemap Deleted successfully' + 'edit' => [ + 'title' => 'Edit Sitemap', + 'success' => 'Sitemap Updated successfully', + 'delete-success' => 'Sitemap Deleted successfully', ], ], @@ -2293,7 +2288,7 @@ ], 'sidebar' => [ - 'collapse' => 'Collapse', + 'collapse' => 'Collapse', 'dashboard' => 'Dashboard', 'sales' => 'Sales', 'orders' => 'Orders', @@ -2357,14 +2352,23 @@ 'title' => 'Custom Filters', 'clear-all' => 'Clear All', ], + + 'date-options' => [ + 'today' => 'Today', + 'yesterday' => 'Yesterday', + 'this-week' => 'This Week', + 'this-month' => 'This Month', + 'last-month' => 'Last Month', + 'last-three-months' => 'Last 3 Months', + 'last-six-months' => 'Last 6 Months', + 'this-year' => 'This Year', + ], ], 'table' => [ - 'actions' => 'Actions', - 'no-records-available' => 'No Records Available.' + 'actions' => 'Actions', + 'no-records-available' => 'No Records Available.', ], - - ], 'products' => [ @@ -2379,7 +2383,6 @@ ], ], - 'save' => 'Save', 'copy-of' => 'Copy of :value', 'copy-of-slug' => 'copy-of-:value', @@ -2464,8 +2467,7 @@ 'newsletter-subscriptions' => 'Newsletter Subscriptions', ], - - // Need to CHECK AND REMOVE remove + // Need to CHECK AND REMOVE remove 'sales-old' => [ 'orders' => [ @@ -3163,7 +3165,6 @@ 'illegal-format' => 'Error! This type of format is either not supported or its illegal format', ], - 'cms-old' => [ 'pages' => [ 'general' => 'General', @@ -3237,7 +3238,7 @@ 'email' => 'Email', 'notification_label' => 'Notifications', 'notification_label-info' => '"Notification" is a message or alert that informs users about events, updates, or actions, enhancing user engagement and awareness.', - 'notifications' => [ + 'notifications' => [ 'verification' => 'Send a verification e-mail after customer registration', 'registration' => 'Send a confirmation e-mail after customer registration', 'customer-registration-confirmation-mail-to-admin' => 'Send a confirmation e-mail to admin after customer registration', diff --git a/packages/Webkul/DataGrid/src/Column.php b/packages/Webkul/DataGrid/src/Column.php index d2fb7193b8e..fbc92c40562 100644 --- a/packages/Webkul/DataGrid/src/Column.php +++ b/packages/Webkul/DataGrid/src/Column.php @@ -3,25 +3,27 @@ namespace Webkul\DataGrid; use Webkul\DataGrid\Enums\ColumnTypeEnum; +use Webkul\DataGrid\Enums\FormInputTypeEnum; +use Webkul\DataGrid\Enums\RangeOptionEnum; class Column { /** * Fully qualified database column name. */ - public ?string $databaseColumnName; + protected ?string $databaseColumnName; /** * Form input type. * * @var string */ - public ?string $formInputType = null; + protected ?string $formInputType = null; /** * Form options. */ - public ?array $formOptions = null; + protected ?array $formOptions = null; /** * Create a column instance. @@ -35,7 +37,31 @@ public function __construct( public bool $sortable = false, public mixed $closure = null, ) { + $this->init(); + } + + /** + * Initialize all necessary settings for the columns. + */ + public function init(): void + { $this->setDatabaseColumnName(); + + switch ($this->type) { + case ColumnTypeEnum::DATE_RANGE->value: + $this->setFormInputType(FormInputTypeEnum::DATE->value); + + $this->setFormOptions($this->getRangeOptions()); + + break; + + case ColumnTypeEnum::DATE_TIME_RANGE->value: + $this->setFormInputType(FormInputTypeEnum::DATE_TIME->value); + + $this->setFormOptions($this->getRangeOptions('Y-m-d H:i:s')); + + break; + } } /** @@ -48,78 +74,96 @@ public function setDatabaseColumnName(?string $databaseColumnName = null): void } /** - * Set column type. + * Get the database column name. */ - public function setColumnType(): void + public function getDatabaseColumnName(): string { - switch ($this->type) { - case ColumnTypeEnum::DATE_RANGE->value: - $this->formInputType = 'date'; - - $this->formOptions = $this->getDateOptions(); + return $this->databaseColumnName; + } - break; + /** + * Set form input type. + */ + public function setFormInputType(string $formInputType): void + { + $this->formInputType = $formInputType; + } - case ColumnTypeEnum::DATE_TIME_RANGE->value: - $this->formInputType = 'datetime-local'; + /** + * Get form input type. + */ + public function getFormInputType(): ?string + { + return $this->formInputType; + } - $this->formOptions = $this->getDateOptions('Y-m-d H:i:s'); + /** + * Set form options. + */ + public function setFormOptions(array $formOptions): void + { + $this->formOptions = $formOptions; + } - break; - } + /** + * Get form options. + */ + public function getFormOptions(): ?array + { + return $this->formOptions; } /** - * Get date options. + * Get range options. */ - public function getDateOptions(string $format = 'Y-m-d'): array + public function getRangeOptions(string $format = 'Y-m-d'): array { return [ [ - 'name' => 'today', - 'label' => 'Today', + 'name' => RangeOptionEnum::TODAY->value, + 'label' => trans('admin::app.components.datagrid.filters.date-options.today'), 'from' => now()->today()->format($format), 'to' => now()->today()->format($format), ], [ - 'name' => 'yesterday', - 'label' => 'Yesterday', + 'name' => RangeOptionEnum::YESTERDAY->value, + 'label' => trans('admin::app.components.datagrid.filters.date-options.yesterday'), 'from' => now()->yesterday()->format($format), 'to' => now()->yesterday()->format($format), ], [ - 'name' => 'this_week', - 'label' => 'This Week', + 'name' => RangeOptionEnum::THIS_WEEK->value, + 'label' => trans('admin::app.components.datagrid.filters.date-options.this-week'), 'from' => now()->startOfWeek()->format($format), 'to' => now()->endOfWeek()->format($format), ], [ - 'name' => 'this_month', - 'label' => 'This Month', + 'name' => RangeOptionEnum::THIS_MONTH->value, + 'label' => trans('admin::app.components.datagrid.filters.date-options.this-month'), 'from' => now()->startOfMonth()->format($format), 'to' => now()->endOfMonth()->format($format), ], [ - 'name' => 'last_month', - 'label' => 'Last Month', + 'name' => RangeOptionEnum::LAST_MONTH->value, + 'label' => trans('admin::app.components.datagrid.filters.date-options.last-month'), 'from' => now()->subMonth(1)->startOfMonth()->format($format), 'to' => now()->subMonth(1)->endOfMonth()->format($format), ], [ - 'name' => 'last_three_months', - 'label' => 'Last 3 Months', + 'name' => RangeOptionEnum::LAST_THREE_MONTHS->value, + 'label' => trans('admin::app.components.datagrid.filters.date-options.last-three-months'), 'from' => now()->subMonth(3)->startOfMonth()->format($format), 'to' => now()->subMonth(1)->endOfMonth()->format($format), ], [ - 'name' => 'last_six_months', - 'label' => 'Last 6 Months', + 'name' => RangeOptionEnum::LAST_SIX_MONTHS->value, + 'label' => trans('admin::app.components.datagrid.filters.date-options.last-six-months'), 'from' => now()->subMonth(6)->startOfMonth()->format($format), 'to' => now()->subMonth(1)->endOfMonth()->format($format), ], [ - 'name' => 'this_year', - 'label' => 'This Year', + 'name' => RangeOptionEnum::THIS_YEAR->value, + 'label' => trans('admin::app.components.datagrid.filters.date-options.this-year'), 'from' => now()->startOfYear()->format($format), 'to' => now()->endOfYear()->format($format), ], diff --git a/packages/Webkul/DataGrid/src/DataGrid.php b/packages/Webkul/DataGrid/src/DataGrid.php index bda3b4c88a3..771720762fa 100644 --- a/packages/Webkul/DataGrid/src/DataGrid.php +++ b/packages/Webkul/DataGrid/src/DataGrid.php @@ -125,6 +125,7 @@ public function addMassAction(array $massAction): void title: $massAction['title'], method: $massAction['method'], url: $massAction['url'], + options: $massAction['options'] ?? [], ); } @@ -179,7 +180,7 @@ public function processRequestedFilters(array $requestedFilters) foreach ($requestedValues as $value) { collect($this->columns) ->filter(fn ($column) => $column->searchable && $column->type !== ColumnTypeEnum::BOOLEAN->value) - ->each(fn ($column) => $scopeQueryBuilder->orWhere($column->databaseColumnName, $value)); + ->each(fn ($column) => $scopeQueryBuilder->orWhere($column->getDatabaseColumnName(), $value)); } }); } else { @@ -190,7 +191,7 @@ public function processRequestedFilters(array $requestedFilters) case ColumnTypeEnum::DATE_TIME_RANGE->value: $this->queryBuilder->where(function ($scopeQueryBuilder) use ($column, $requestedValues) { foreach ($requestedValues as $value) { - $scopeQueryBuilder->whereBetween($column->databaseColumnName, [$value[0] ?? '', $value[1] ?? '']); + $scopeQueryBuilder->whereBetween($column->getDatabaseColumnName(), [$value[0] ?? '', $value[1] ?? '']); } }); @@ -199,7 +200,7 @@ public function processRequestedFilters(array $requestedFilters) default: $this->queryBuilder->where(function ($scopeQueryBuilder) use ($column, $requestedValues) { foreach ($requestedValues as $value) { - $scopeQueryBuilder->orWhere($column->databaseColumnName, $value); + $scopeQueryBuilder->orWhere($column->getDatabaseColumnName(), $value); } }); @@ -258,6 +259,15 @@ public function formatData(): array { $paginator = $this->paginator->toArray(); + /** + * TODO: need to handle this... + */ + foreach ($this->columns as $column) { + $column->input_type = $column->getFormInputType(); + + $column->options = $column->getFormOptions(); + } + foreach ($paginator['data'] as $record) { foreach ($this->columns as $column) { if ($closure = $column->closure) { diff --git a/packages/Webkul/DataGrid/src/Enums/FormInputTypeEnum.php b/packages/Webkul/DataGrid/src/Enums/FormInputTypeEnum.php new file mode 100644 index 00000000000..8370b4841f9 --- /dev/null +++ b/packages/Webkul/DataGrid/src/Enums/FormInputTypeEnum.php @@ -0,0 +1,16 @@ +