Skip to content

Commit

Permalink
add import/export fn
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Aug 28, 2024
1 parent 758da2e commit 47f1c94
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 67 deletions.
56 changes: 23 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
]

];

```
Expand Down
13 changes: 13 additions & 0 deletions resources/lang/ar/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@
"icon" => "الأيقونة",
"type" => "النوع",
"providers" => "ارسال عن طريق"
],
"export" => [
"title" => "تصدير",
"columns" => "الأعمدة",
],
"import" => [
"title" => "استيراد",
"excel" => "ملف اكسيل",
"hint" => "يرجى تحميل ملف اكسيل لاستيراد الحسابات",
"success" => 'تم استيراد الحسابات بنجاح',
"body" => 'تم استيراد الحسابات بنجاح',
"error" => "خطأ أثناء استيراد الحسابات",
"error-body" => "حدث خطأ أثناء استيراد الحسابات",
]
],
"meta" => [
Expand Down
13 changes: 13 additions & 0 deletions resources/lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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" => [
Expand Down
1 change: 0 additions & 1 deletion src/Export/ExportAccounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace TomatoPHP\FilamentAccounts\Filament\Resources\AccountResource\Actions;

use Filament\Tables\Actions\Action;
use Filament\Forms;
use Maatwebsite\Excel\Facades\Excel;
use TomatoPHP\FilamentAccounts\Export\ExportAccounts;

class ExportAccountsAction
{
public static function make(): Action
{
return Action::make('export')
->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');
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace TomatoPHP\FilamentAccounts\Filament\Resources\AccountResource\Actions;

use Filament\Notifications\Notification;
use Filament\Tables\Actions\Action;
use Filament\Forms;
use Maatwebsite\Excel\Facades\Excel;
use TomatoPHP\FilamentAccounts\Import\ImportAccounts;

class ImportAccountsAction
{
public static function make(): Action
{
return Action::make('import')
->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');
}
}
38 changes: 6 additions & 32 deletions src/Filament/Resources/AccountResource/Tables/AccountsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down
34 changes: 33 additions & 1 deletion src/Import/ImportAccounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use TomatoPHP\FilamentAccounts\Models\Account;

class ImportAccounts implements ToCollection
{
Expand All @@ -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,
]);
}
}
}
}
}

0 comments on commit 47f1c94

Please sign in to comment.