From 47f1c94b2c2bc858880601ed94134952e0466a43 Mon Sep 17 00:00:00 2001 From: Fady Mondy Date: Wed, 28 Aug 2024 18:18:57 +0300 Subject: [PATCH] add import/export fn --- README.md | 56 ++++++++----------- resources/lang/ar/messages.php | 13 +++++ resources/lang/en/messages.php | 13 +++++ src/Export/ExportAccounts.php | 1 - .../Actions/ExportAccountsAction.php | 44 +++++++++++++++ .../Actions/ImportAccountsAction.php | 46 +++++++++++++++ .../AccountResource/Tables/AccountsTable.php | 38 ++----------- src/Import/ImportAccounts.php | 34 ++++++++++- 8 files changed, 178 insertions(+), 67 deletions(-) create mode 100644 src/Filament/Resources/AccountResource/Actions/ExportAccountsAction.php create mode 100644 src/Filament/Resources/AccountResource/Actions/ImportAccountsAction.php diff --git a/README.md b/README.md index f6017aa..6070d8e 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,29 @@ full accounts manager with API/Notifications/Contacts to manage your contacts an ![Edit Request](https://raw.githubusercontent.com/tomatophp/filament-accounts/master/arts/edit-request.png) ![Contacts](https://raw.githubusercontent.com/tomatophp/filament-accounts/master/arts/contacts.png) - +## Features + +- [x] Accounts Manager +- [x] Cached Meta +- [x] Account Locations +- [x] Account Teams +- [x] Account Types +- [x] Account Requests +- [x] Account SaaS Panel With Edit Profile [Jetstream] +- [x] Account Login By +- [x] Account Active/Block +- [x] Account Avatar +- [x] Account OTP Activation +- [x] Account Filament Alerts Integration +- [x] Account Impersonate Integration +- [x] Account APIs +- [x] Auth Builder Service +- [x] Support Multi Tenants +- [x] Account Table Column +- [x] Account Panel Events +- [x] Attach New Field To Accounts +- [ ] Export +- [ ] Import ## Installation @@ -137,38 +159,6 @@ return [ * guard: Auth Guard */ "guard" => "accounts", - - - /** - * Accounts Relations Managers - * - * you can set selected relations to show in account resource - */ - "relations" => \TomatoPHP\FilamentAccounts\Filament\Resources\AccountResource\Releations\AccountReleations::class, - - /** - * Accounts Resource Builder - * - * you can change the form, table, actions and filters of account resource by using filament-helpers class commands - * - * link: https://github.com/tomatophp/filament-helpers - */ - "accounts" => [ - "form" => \TomatoPHP\FilamentAccounts\Filament\Resources\AccountResource\Forms\AccountsForm::class, - "table" => \TomatoPHP\FilamentAccounts\Filament\Resources\AccountResource\Tables\AccountsTable::class, - "actions" => \TomatoPHP\FilamentAccounts\Filament\Resources\AccountResource\Actions\AccountsActions::class, - "filters" => \TomatoPHP\FilamentAccounts\Filament\Resources\AccountResource\Filters\AccountsFilters::class, - "pages" => \TomatoPHP\FilamentAccounts\Filament\Resources\AccountResource\Pages\AccountPagesList::class, - ], - - "teams" => [ - "allowed" => false, - "model" => \TomatoPHP\FilamentAccounts\Models\Team::class, - "invitation" => \TomatoPHP\FilamentAccounts\Models\TeamInvitation::class, - "membership" => \TomatoPHP\FilamentAccounts\Models\Membership::class, - "resource" => \TomatoPHP\FilamentAccounts\Filament\Resources\TeamResource::class, - ] - ]; ``` diff --git a/resources/lang/ar/messages.php b/resources/lang/ar/messages.php index f992dec..6021c16 100644 --- a/resources/lang/ar/messages.php +++ b/resources/lang/ar/messages.php @@ -49,6 +49,19 @@ "icon" => "الأيقونة", "type" => "النوع", "providers" => "ارسال عن طريق" + ], + "export" => [ + "title" => "تصدير", + "columns" => "الأعمدة", + ], + "import" => [ + "title" => "استيراد", + "excel" => "ملف اكسيل", + "hint" => "يرجى تحميل ملف اكسيل لاستيراد الحسابات", + "success" => 'تم استيراد الحسابات بنجاح', + "body" => 'تم استيراد الحسابات بنجاح', + "error" => "خطأ أثناء استيراد الحسابات", + "error-body" => "حدث خطأ أثناء استيراد الحسابات", ] ], "meta" => [ diff --git a/resources/lang/en/messages.php b/resources/lang/en/messages.php index 6cc63fc..fa31855 100644 --- a/resources/lang/en/messages.php +++ b/resources/lang/en/messages.php @@ -51,6 +51,19 @@ "icon" => "Icon", "type" => "Type", "providers" => "Send By" + ], + "export" => [ + "title" => "Export", + "columns" => "Columns" + ], + "import" => [ + "title" => "Import", + "excel" => "Excel", + "hint" => "You can upload the same style of exported file", + "success" => 'Success', + "body" => 'Accounts imported successfully', + "error" => "Error", + "error-body" => "Error while importing accounts", ] ], "meta" => [ diff --git a/src/Export/ExportAccounts.php b/src/Export/ExportAccounts.php index 888aa84..aba0c5f 100644 --- a/src/Export/ExportAccounts.php +++ b/src/Export/ExportAccounts.php @@ -23,7 +23,6 @@ public function headings(): array public function collection() { - dd($this->data['columns']); $select = array_keys(collect($this->data['columns'])->filter(fn($item, $key) => !str($key)->contains('.'))->toArray()); return config('filament-accounts.model')::query() ->select($select) diff --git a/src/Filament/Resources/AccountResource/Actions/ExportAccountsAction.php b/src/Filament/Resources/AccountResource/Actions/ExportAccountsAction.php new file mode 100644 index 0000000..fa6a607 --- /dev/null +++ b/src/Filament/Resources/AccountResource/Actions/ExportAccountsAction.php @@ -0,0 +1,44 @@ +label(trans('filament-accounts::messages.accounts.export.title')) + ->requiresConfirmation() + ->color('info') + ->icon('heroicon-o-arrow-down-on-square') + ->fillForm([ + 'columns' => [ + 'id' => trans('filament-accounts::messages.accounts.coulmns.id'), + 'name' => trans('filament-accounts::messages.accounts.coulmns.name'), + 'email' => trans('filament-accounts::messages.accounts.coulmns.email'), + 'phone' => trans('filament-accounts::messages.accounts.coulmns.phone'), + 'address' => trans('filament-accounts::messages.accounts.coulmns.address'), + 'type' => trans('filament-accounts::messages.accounts.coulmns.type'), + 'is_login' => trans('filament-accounts::messages.accounts.coulmns.is_login'), + 'is_active' => trans('filament-accounts::messages.accounts.coulmns.is_active'), + 'created_at' => trans('filament-accounts::messages.accounts.coulmns.created_at'), + 'updated_at' => trans('filament-accounts::messages.accounts.coulmns.updated_at'), + ] + ]) + ->form([ + Forms\Components\KeyValue::make('columns') + ->label(trans('filament-accounts::messages.accounts.export.columns')) + ->required() + ->editableKeys(false) + ->addable(false) + ]) + ->action(function (array $data){ + return Excel::download(new ExportAccounts($data), 'accounts.csv'); + }); + } +} diff --git a/src/Filament/Resources/AccountResource/Actions/ImportAccountsAction.php b/src/Filament/Resources/AccountResource/Actions/ImportAccountsAction.php new file mode 100644 index 0000000..dbccbb3 --- /dev/null +++ b/src/Filament/Resources/AccountResource/Actions/ImportAccountsAction.php @@ -0,0 +1,46 @@ +label(trans('filament-accounts::messages.accounts.import.title')) + ->form([ + Forms\Components\FileUpload::make('excel') + ->hint(trans('filament-accounts::messages.accounts.import.hint')) + ->label(trans('filament-accounts::messages.accounts.import.excel')) + ->acceptedFileTypes(['text/csv', 'application/vnd.ms-excel']) + ->required() + ]) + ->action(function (array $data){ + try { + Excel::import(new ImportAccounts(), storage_path('app/public/' . $data['excel'])); + + + Notification::make() + ->title(trans('filament-accounts::messages.accounts.import.success')) + ->body(trans('filament-accounts::messages.accounts.import.body')) + ->success() + ->send(); + }catch (\Exception $e) { + Notification::make() + ->title(trans('filament-accounts::messages.accounts.import.error')) + ->body(trans('filament-accounts::messages.accounts.import.error-body')) + ->danger() + ->send(); + } + + }) + ->color('warning') + ->icon('heroicon-o-arrow-up-on-square'); + } +} diff --git a/src/Filament/Resources/AccountResource/Tables/AccountsTable.php b/src/Filament/Resources/AccountResource/Tables/AccountsTable.php index 8944cfc..c05f304 100644 --- a/src/Filament/Resources/AccountResource/Tables/AccountsTable.php +++ b/src/Filament/Resources/AccountResource/Tables/AccountsTable.php @@ -9,6 +9,8 @@ use TomatoPHP\FilamentAccounts\Components\AccountColumn; use TomatoPHP\FilamentAccounts\Export\ExportAccounts; use TomatoPHP\FilamentAccounts\Filament\Resources\AccountResource\Actions\AccountsActions; +use TomatoPHP\FilamentAccounts\Filament\Resources\AccountResource\Actions\ExportAccountsAction; +use TomatoPHP\FilamentAccounts\Filament\Resources\AccountResource\Actions\ImportAccountsAction; use TomatoPHP\FilamentAccounts\Filament\Resources\AccountResource\Filters\AccountsFilters; use TomatoPHP\FilamentHelpers\Contracts\TableBuilder; use TomatoPHP\FilamentTypes\Components\TypeColumn; @@ -117,38 +119,10 @@ public function table(Table $table): Table ->toggleable(isToggledHiddenByDefault: true), ]); return $table -// ->headerActions([ -// Tables\Actions\Action::make('export') -// ->requiresConfirmation() -// ->color('info') -// ->icon('heroicon-o-arrow-down-on-square') -// ->fillForm([ -// 'columns' => [ -// 'id' => trans('filament-accounts::messages.accounts.coulmns.id'), -// 'name' => trans('filament-accounts::messages.accounts.coulmns.name'), -// 'email' => trans('filament-accounts::messages.accounts.coulmns.email'), -// 'phone' => trans('filament-accounts::messages.accounts.coulmns.phone'), -// 'address' => trans('filament-accounts::messages.accounts.coulmns.address'), -// 'type' => trans('filament-accounts::messages.accounts.coulmns.type'), -// 'is_login' => trans('filament-accounts::messages.accounts.coulmns.is_login'), -// 'is_active' => trans('filament-accounts::messages.accounts.coulmns.is_active'), -// 'created_at' => trans('filament-accounts::messages.accounts.coulmns.created_at'), -// 'updated_at' => trans('filament-accounts::messages.accounts.coulmns.updated_at'), -// ] -// ]) -// ->form([ -// Forms\Components\KeyValue::make('columns') -// ->required() -// ->editableKeys(false) -// ->addable(false) -// ]) -// ->action(function (array $data){ -// return Excel::download(new ExportAccounts($data), 'accounts.csv'); -// }), -// Tables\Actions\Action::make('import') -// ->color('warning') -// ->icon('heroicon-o-arrow-up-on-square'), -// ]) + ->headerActions([ + ExportAccountsAction::make(), + ImportAccountsAction::make() + ]) ->columns($colums) ->filters(AccountsFilters::make()) ->actions(AccountsActions::make()) diff --git a/src/Import/ImportAccounts.php b/src/Import/ImportAccounts.php index d4f298b..57cd98b 100644 --- a/src/Import/ImportAccounts.php +++ b/src/Import/ImportAccounts.php @@ -4,6 +4,7 @@ use Illuminate\Support\Collection; use Maatwebsite\Excel\Concerns\ToCollection; +use TomatoPHP\FilamentAccounts\Models\Account; class ImportAccounts implements ToCollection { @@ -12,6 +13,37 @@ class ImportAccounts implements ToCollection */ public function collection(Collection $collection) { - // + foreach ($collection as $row) { + if(str($row[0])->contains(trans('filament-accounts::messages.accounts.coulmns.id'))){ + continue; + } + else { + $account = Account::query()->firstOrCreate([ + "email" => $row[2]??null, + "phone" => $row[3]??null, + "username" => $row[2]??null, + ], [ + "name" => $row[1], + "email" => $row[2]??null, + "phone" => $row[3]??null, + "username" => $row[2]??null, + "address" => $row[4]??null, + "type" => $row[5]??'account', + "is_active" => $row[6]??false, + ]); + + if($account->exists){ + $account->update([ + "name" => $row[1], + "email" => $row[2]??null, + "phone" => $row[3]??null, + "username" => $row[2]??null, + "address" => $row[4]??null, + "type" => $row[5]??'account', + "is_active" => $row[6]??false, + ]); + } + } + } } }