From 08dfd9dba1129ad2eaac966e435789fd7133d01b Mon Sep 17 00:00:00 2001 From: suraj kashyap Date: Mon, 1 May 2023 18:07:44 +0530 Subject: [PATCH 01/16] Code Refactor and optimization of code. --- src/DataGrids/Admin/ProfileDataGrid.php | 42 +++-- ...er_bulkupload_data_flow_profiles_table.php | 4 +- src/Helpers/ImportProduct.php | 30 +-- .../Admin/BulkUploadController.php | 83 ++------- .../Controllers/Admin/HelperController.php | 175 ++++-------------- src/Http/admin-routes.php | 65 ------- src/Providers/BulkUploadServiceProvider.php | 2 +- src/Requests/AddProfileRequest.php | 39 ++++ src/Resources/lang/en/app.php | 3 +- .../data-flow-profile/index.blade.php | 4 +- .../bulk-upload/upload-files/index.blade.php | 103 +++++------ src/Routes/admin-routes.php | 66 +++++++ src/Routes/web.php | 8 + 13 files changed, 253 insertions(+), 371 deletions(-) delete mode 100644 src/Http/admin-routes.php create mode 100644 src/Requests/AddProfileRequest.php create mode 100644 src/Routes/admin-routes.php create mode 100644 src/Routes/web.php diff --git a/src/DataGrids/Admin/ProfileDataGrid.php b/src/DataGrids/Admin/ProfileDataGrid.php index f0c9404..aa20597 100644 --- a/src/DataGrids/Admin/ProfileDataGrid.php +++ b/src/DataGrids/Admin/ProfileDataGrid.php @@ -2,20 +2,23 @@ namespace Webkul\Bulkupload\DataGrids\Admin; -use DB; +use Illuminate\Support\Facades\DB; use Webkul\Ui\DataGrid\DataGrid; -/** - * Profile Datagrid class - * - */ class ProfileDataGrid extends DataGrid { /** - * @var integer + * Index + * + * @var string */ protected $index = 'id'; + /** + * Prepare the query builder + * + * @return void + */ public function prepareQueryBuilder() { $queryBuilder = DB::table('bulkupload_data_flow_profiles') @@ -30,11 +33,16 @@ public function prepareQueryBuilder() $this->setQueryBuilder($queryBuilder); } + /** + * Add Column + * + * @return void + */ public function addColumns() { $this->addColumn([ 'index' => 'profile_name', - 'label' => trans('bulkupload::app.admin.bulk-upload.data-flow-profile.name'), + 'label' => __('bulkupload::app.admin.bulk-upload.data-flow-profile.name'), 'type' => 'string', 'searchable' => true, 'sortable' => true, @@ -43,7 +51,7 @@ public function addColumns() $this->addColumn([ 'index' => 'name', - 'label' => trans('admin::app.catalog.products.familiy'), + 'label' => __('admin::app.catalog.products.family'), 'type' => 'string', 'searchable' => true, 'sortable' => true, @@ -52,7 +60,7 @@ public function addColumns() $this->addColumn([ 'index' => 'locale_code', - 'label' => trans('bulkupload::app.admin.bulk-upload.data-flow-profile.data-grid.locale_code'), + 'label' => __('bulkupload::app.admin.bulk-upload.data-flow-profile.data-grid.locale_code'), 'type' => 'string', 'searchable' => true, 'sortable' => true, @@ -61,7 +69,7 @@ public function addColumns() $this->addColumn([ 'index' => 'created_at', - 'label' => trans('bulkupload::app.admin.bulk-upload.data-flow-profile.data-grid.created-at'), + 'label' => __('bulkupload::app.admin.bulk-upload.data-flow-profile.data-grid.created-at'), 'type' => 'datetime', 'sortable' => true, 'searchable' => false, @@ -69,6 +77,11 @@ public function addColumns() ]); } + /** + * Prepare actions + * + * @return void + */ public function prepareActions() { $this->addAction([ @@ -81,10 +94,10 @@ public function prepareActions() ]); $this->addAction([ - 'type' => trans('admin::app.datagrid.delete'), + 'type' => __('admin::app.datagrid.delete'), 'method' => 'POST', 'route' => 'bulkupload.admin.profile.delete', - 'confirm_text' => trans('ui::app.datagrid.massaction.delete'), + 'confirm_text' => __('ui::app.datagrid.massaction.delete'), 'icon' => 'icon trash-icon', 'title' => '' ]); @@ -92,6 +105,11 @@ public function prepareActions() $this->enableAction = true; } + /** + * Prepare mass actions + * + * @return void + */ public function prepareMassActions() { $this->addMassAction([ diff --git a/src/Database/Migrations/2021_12_28_150311_alter_bulkupload_data_flow_profiles_table.php b/src/Database/Migrations/2021_12_28_150311_alter_bulkupload_data_flow_profiles_table.php index 7d0e24e..cbdddfe 100644 --- a/src/Database/Migrations/2021_12_28_150311_alter_bulkupload_data_flow_profiles_table.php +++ b/src/Database/Migrations/2021_12_28_150311_alter_bulkupload_data_flow_profiles_table.php @@ -25,6 +25,8 @@ public function up() */ public function down() { - // + Schema::table('bulkupload_data_flow_profiles', function(Blueprint $table) { + $table->dropColumn('locale_code'); + }); } } diff --git a/src/Helpers/ImportProduct.php b/src/Helpers/ImportProduct.php index 31b82f2..6cbde5e 100644 --- a/src/Helpers/ImportProduct.php +++ b/src/Helpers/ImportProduct.php @@ -2,27 +2,10 @@ namespace Webkul\Bulkupload\Helpers; -use Webkul\Bulkupload\Repositories\ImportProductRepository; -use Webkul\Bulkupload\Repositories\DataFlowProfileRepository; +use Webkul\Bulkupload\Repositories\{ImportProductRepository, DataFlowProfileRepository}; class ImportProduct { - /** - * ImportProductRepository object - * - * @var \Webkul\Bulkupload\Repositories\ImportProductRepository - * - */ - protected $importProductRepository; - - /** - * DataFlowProfileRepository object - * - * @var \Webkul\Bulkupload\Repositories\DataFlowProfileRepository - * - */ - protected $dataFlowProfileRepository; - /** * Create a new controller instance. * @@ -32,14 +15,9 @@ class ImportProduct * @return void */ public function __construct( - ImportProductRepository $importProductRepository, - DataFlowProfileRepository $dataFlowProfileRepository - ) - { - $this->importProductRepository = $importProductRepository; - - $this->dataFlowProfileRepository = $dataFlowProfileRepository; - } + protected ImportProductRepository $importProductRepository, + protected DataFlowProfileRepository $dataFlowProfileRepository + ){} /** * store import products for profile execution diff --git a/src/Http/Controllers/Admin/BulkUploadController.php b/src/Http/Controllers/Admin/BulkUploadController.php index 62966a1..6284308 100644 --- a/src/Http/Controllers/Admin/BulkUploadController.php +++ b/src/Http/Controllers/Admin/BulkUploadController.php @@ -3,48 +3,19 @@ namespace Webkul\Bulkupload\Http\Controllers\Admin; use Webkul\Attribute\Repositories\AttributeFamilyRepository; -use Webkul\Bulkupload\Repositories\ImportProductRepository; -use Webkul\Bulkupload\Repositories\DataFlowProfileRepository; +use Webkul\Bulkupload\Repositories\{ImportProductRepository, DataFlowProfileRepository}; +use Webkul\Bulkupload\Requests\AddProfileRequest; +use Webkul\Bulkupload\DataGrids\Admin\ProfileDataGrid; class BulkUploadController extends Controller { - /** - * DataFlowProfileRepository object - * - * @var \Webkul\Bulkupload\Repositories\DataFlowProfileRepository - * - */ - protected $dataFlowProfileRepository; - - /** - * AttributeFamilyRepository object - * - * @var \Webkul\Attribute\Repositories\AttributeFamilyRepository - * - */ - protected $attributeFamilyRepository; - - /** - * ImportProductRepository object - * - * @var \Webkul\Bulkupload\Repositories\ImportProductRepository - * - */ - protected $importProductRepository; - - /** - * @var array - */ - protected $product = []; - /** - * Contains route related configuration + * Contains routes and views related configuration * * @var array */ protected $_config; - /** * Create a new controller instance. * @@ -55,18 +26,11 @@ class BulkUploadController extends Controller * @return void */ public function __construct( - AttributeFamilyRepository $attributeFamilyRepository, - DataFlowProfileRepository $dataFlowProfileRepository, - ImportProductRepository $importProductRepository - ) - { + protected AttributeFamilyRepository $attributeFamilyRepository, + protected DataFlowProfileRepository $dataFlowProfileRepository, + protected ImportProductRepository $importProductRepository + ) { $this->_config = request('_config'); - - $this->attributeFamilyRepository = $attributeFamilyRepository; - - $this->importProductRepository = $importProductRepository; - - $this->dataFlowProfileRepository = $dataFlowProfileRepository; } /** @@ -76,20 +40,22 @@ public function __construct( */ public function index() { - $profiles = null; + if (request()->ajax()) { + return app(ProfileDataGrid::class)->toJson(); + } + + $profiles = []; $families = $this->attributeFamilyRepository->all(); - $allProfiles = $this->importProductRepository->get()->toArray(); + $allProfiles = $this->importProductRepository->all(); if (! empty($allProfiles)) { - foreach ($allProfiles as $allProfile) { - $profilers[] = $allProfile['data_flow_profile_id']; - } - + $profilers = $allProfiles->pluck('data_flow_profile_id'); + foreach ($profilers as $key => $profiler) { $profiles[] = $this->dataFlowProfileRepository->findByfield(['id' => $profilers[$key], 'run_status' => '0']); } } - + return view($this->_config['view'], compact('families', 'profiles')); } @@ -98,20 +64,9 @@ public function index() * * @return \Illuminate\Http\Response */ - public function store() + public function store(AddProfileRequest $addProfileRequest) { - request()->validate([ - 'name' => 'required|unique:bulkupload_data_flow_profiles', - 'attribute_family' => 'required', - 'locale_code' => 'required' - ]); - - $dataFlowProfileAdmin['name'] = request()->name; - $dataFlowProfileAdmin['attribute_family_id'] = request()->attribute_family; - $dataFlowProfileAdmin['locale_code'] = request()->locale_code; - - - $this->dataFlowProfileRepository->create($dataFlowProfileAdmin); + $this->dataFlowProfileRepository->create($addProfileRequest->validated()); session()->flash('success',trans('bulkupload::app.admin.bulk-upload.messages.profile-saved')); diff --git a/src/Http/Controllers/Admin/HelperController.php b/src/Http/Controllers/Admin/HelperController.php index dd0406b..caa3b41 100644 --- a/src/Http/Controllers/Admin/HelperController.php +++ b/src/Http/Controllers/Admin/HelperController.php @@ -3,105 +3,13 @@ namespace Webkul\Bulkupload\Http\Controllers\Admin; use Webkul\Admin\Imports\DataGridImport; -use Maatwebsite\Excel\Validators\Failure; use Webkul\Bulkupload\Helpers\ImportProduct; -use Webkul\Bulkupload\Repositories\ImportProductRepository; -use Webkul\Bulkupload\Repositories\DataFlowProfileRepository; -use Webkul\Bulkupload\Repositories\Products\SimpleProductRepository; -use Webkul\Bulkupload\Repositories\Products\ConfigurableProductRepository; -use Webkul\Bulkupload\Repositories\Products\VirtualProductRepository; -use Webkul\Bulkupload\Repositories\Products\DownloadableProductRepository; -use Webkul\Bulkupload\Repositories\Products\GroupedProductRepository; -use Webkul\Bulkupload\Repositories\Products\BundledProductRepository; -use Webkul\Bulkupload\Repositories\Products\BookingProductRepository; +use Webkul\Bulkupload\Repositories\{ImportProductRepository, DataFlowProfileRepository}; +use Webkul\Bulkupload\Repositories\Products\{SimpleProductRepository, ConfigurableProductRepository, VirtualProductRepository}; +use Webkul\Bulkupload\Repositories\Products\{DownloadableProductRepository, GroupedProductRepository, BundledProductRepository, BookingProductRepository}; class HelperController extends Controller { - - /** - * SimpleProductRepository object - * - * @var \Webkul\Bulkupload\Repositories\Products\SimpleProductRepository - * - */ - protected $simpleProductRepository; - - /** - * ConfigurableProductRepository object - * - * @var \Webkul\Bulkupload\Repositories\Products\ConfigurableProductRepository - * - */ - protected $configurableProductRepository; - - /** - * VirtualProductRepository object - * - * @var \Webkul\Bulkupload\Repositories\Products\VirtualProductRepository; - * - */ - protected $virtualProductRepository; - - /** - * DownloadableProductRepository object - * - * @var \Webkul\Bulkupload\Repositories\Products\DownloadableProductRepository - * - */ - protected $downloadableProductRepository; - - /** - * BundledProductRepository object - * - * @var \Webkul\Bulkupload\Repositories\Products\BundledProductRepository - * - */ - protected $bundledProductRepository; - - /** - * BookingProductRepository object - * - * @var \Webkul\Bulkupload\Repositories\Products\BookingProductRepository - * - */ - protected $bookingProductRepository; - - /** - * GroupedProductRepository object - * - * @var \Webkul\Bulkupload\Repositories\Products\GroupedProductRepository - * - */ - protected $groupedProductRepository; - - /** - * DataFlowProfileRepository object - * - * @var \Webkul\Bulkupload\Repositories\DataFlowProfileRepository - * - */ - protected $dataFlowProfileRepository; - - /** - * ImportProductRepository object - * - * @var \Webkul\Bulkupload\Repositories\ImportProductRepository - * - */ - protected $importProductRepository; - - /** - * @var array - */ - protected $product = []; - - /** - * ImportProduct helper - * - * @var \Webkul\Bulkupload\Helpers\ImportProduct - */ - protected $importProduct; - /** * Create a new controller instance. * @@ -119,65 +27,50 @@ class HelperController extends Controller * @return void */ public function __construct( - ImportProductRepository $importProductRepository, - DataFlowProfileRepository $dataFlowProfileRepository, - SimpleProductRepository $simpleProductRepository, - ConfigurableProductRepository $configurableProductRepository, - VirtualProductRepository $virtualProductRepository, - DownloadableProductRepository $downloadableProductRepository, - BundledProductRepository $bundledProductRepository, - BookingProductRepository $bookingProductRepository, - GroupedProductRepository $groupedProductRepository, - ImportProduct $importProduct - ) - { - $this->importProductRepository = $importProductRepository; - - $this->dataFlowProfileRepository = $dataFlowProfileRepository; - - $this->simpleProductRepository = $simpleProductRepository; - - $this->configurableProductRepository = $configurableProductRepository; - - $this->virtualProductRepository = $virtualProductRepository; - - $this->downloadableProductRepository = $downloadableProductRepository; - - $this->bundledProductRepository = $bundledProductRepository; - - $this->bookingProductRepository = $bookingProductRepository; - - $this->groupedProductRepository = $groupedProductRepository; - - $this->importProduct = $importProduct; - } + protected ImportProductRepository $importProductRepository, + protected DataFlowProfileRepository $dataFlowProfileRepository, + protected SimpleProductRepository $simpleProductRepository, + protected ConfigurableProductRepository $configurableProductRepository, + protected VirtualProductRepository $virtualProductRepository, + protected DownloadableProductRepository $downloadableProductRepository, + protected BundledProductRepository $bundledProductRepository, + protected BookingProductRepository $bookingProductRepository, + protected GroupedProductRepository $groupedProductRepository, + protected ImportProduct $importProduct + ) {} /** * Download sample files. * - * @return \Illuminate\Http\Response + * @return \Symfony\Component\HttpFoundation\Response|void */ public function downloadFile() { - $items = []; - - foreach (config('product_types') as $item) { - $item['children'] = []; + if (! request()->filled('download_sample')) { + session()->flash('error', __('bulkupload::app.admin.bulk-upload.upload-files.select-sample-file')); - array_push($items, $item); + return redirect()->back(); } + $items = array_values(array_map(function($item) { + $item['children'] = []; + return $item; + }, config('product_types'))); + $types = core()->sortItems($items); - foreach ($types as $key => $productType) { - if (request()->download_sample == $key.'-csv') { - return response()->download(public_path('storage/downloads/sample-files/bulk'.$key.'productupload.csv')); - } else if (request()->download_sample == $key.'-xls') { - return response()->download(public_path('storage/downloads/sample-files/bulk'.$key.'productupload.xlsx')); - } else if (empty(request()->download_sample)) { - return redirect()->back(); - } + foreach ($types as $productType) { + if (request()->input('download_sample') == $productType['key'] . '-csv') { + + return response()->download(public_path('storage/downloads/sample-files/bulk'.$productType['key'] .'productupload.csv')); + + } else if (request()->input('download_sample') == $productType['key'] . '-xls') { + + return response()->download(public_path('storage/downloads/sample-files/bulk'.$productType['key'] .'productupload.xlsx')); + } } + + return redirect()->back(); } /** diff --git a/src/Http/admin-routes.php b/src/Http/admin-routes.php deleted file mode 100644 index 7f97b0a..0000000 --- a/src/Http/admin-routes.php +++ /dev/null @@ -1,65 +0,0 @@ - ['web']], function () { - - Route::prefix(config('app.admin_url'))->group(function () { - - Route::group(['middleware' => ['admin']], function () { - Route::prefix('bulkupload')->group(function () { - - // Bulk Upload Products - Route::get('/upload-files', 'Webkul\Bulkupload\Http\Controllers\Admin\BulkUploadController@index')->defaults('_config', [ - 'view' => 'bulkupload::admin.bulk-upload.upload-files.index' - ])->name('admin.bulk-upload.index'); - - Route::get('/run-profile', 'Webkul\Bulkupload\Http\Controllers\Admin\BulkUploadController@index')->defaults('_config', [ - 'view' => 'bulkupload::admin.bulk-upload.run-profile.index' - ])->name('admin.run-profile.index'); - - Route::post('/read-csv', 'Webkul\Bulkupload\Http\Controllers\Admin\HelperController@readCSVData') - ->name('bulk-upload-admin.read-csv'); - - Route::post('/getprofiles', 'Webkul\Bulkupload\Http\Controllers\Admin\HelperController@getAllDataFlowProfiles') - ->name('bulk-upload-admin.get-all-profile'); - - // Download Sample Files - Route::post('/download','Webkul\Bulkupload\Http\Controllers\Admin\HelperController@downloadFile')->defaults('_config',[ - 'view' => 'bulkupload::admin.bulk-upload.upload-files.index' - ])->name('download-sample-files'); - - // import new products - Route::post('/importnew', 'Webkul\Bulkupload\Http\Controllers\Admin\HelperController@importNewProductsStore')->defaults('_config',['view' => 'bulkupload::admin.bulk-upload.upload-files.index' ])->name('import-new-products-form-submit'); - - Route::prefix('dataflowprofile')->group(function () { - Route::get('/', 'Webkul\Bulkupload\Http\Controllers\Admin\BulkUploadController@index')->defaults('_config', [ - 'view' => 'bulkupload::admin.bulk-upload.data-flow-profile.index' - ])->name('admin.dataflow-profile.index'); - - Route::post('/addprofile', 'Webkul\Bulkupload\Http\Controllers\Admin\BulkUploadController@store')->defaults('_config', [ - 'view' => 'bulkupload::admin.bulk-upload.data-flow-profile.index' - ])->name('bulkupload.bulk-upload.dataflow.add-profile'); - - Route::post('/runprofile', 'Webkul\Bulkupload\Http\Controllers\Admin\HelperController@runProfile')->defaults('_config', [ - 'view' => 'bulkupload::admin.bulk-upload.run-profile.progressbar' - ])->name('bulk-upload-admin.run-profile'); - - // edit actions - Route::post('/delete/{id}','Webkul\Bulkupload\Http\Controllers\Admin\BulkUploadController@destroy')->name('bulkupload.admin.profile.delete'); - - Route::get('/edit/{id}', 'Webkul\Bulkupload\Http\Controllers\Admin\BulkUploadController@edit')->defaults('_config', [ - 'view' => 'bulkupload::admin.bulk-upload.data-flow-profile.edit' - ])->name('bulkupload.admin.profile.edit'); - - Route::post('/update/{id}', 'Webkul\Bulkupload\Http\Controllers\Admin\BulkUploadController@update')->defaults('_config', [ - 'view' => 'bulkupload::admin.bulk-upload.data-flow-profile.index' - ])->name('admin.bulk-upload.dataflow.update-profile'); - - //mass destroy - Route::post('/massdestroy', 'Webkul\Bulkupload\Http\Controllers\Admin\BulkUploadController@massDestroy')->defaults('_config', [ - 'redirect' => 'admin.dataflow-profile.index' - ])->name('bulkupload.admin.profile.massDelete'); - }); - }); - }); - }); -}); diff --git a/src/Providers/BulkUploadServiceProvider.php b/src/Providers/BulkUploadServiceProvider.php index 8127c58..a877b87 100644 --- a/src/Providers/BulkUploadServiceProvider.php +++ b/src/Providers/BulkUploadServiceProvider.php @@ -8,7 +8,7 @@ class BulkUploadServiceProvider extends ServiceProvider { public function boot() { - include __DIR__ . '/../Http/admin-routes.php'; + include __DIR__ . '/../Routes/web.php'; $this->app->register(ModuleServiceProvider::class); $this->app->register(EventServiceProvider::class); diff --git a/src/Requests/AddProfileRequest.php b/src/Requests/AddProfileRequest.php new file mode 100644 index 0000000..143d2b2 --- /dev/null +++ b/src/Requests/AddProfileRequest.php @@ -0,0 +1,39 @@ + + */ + public function rules(): array + { + return [ + 'name' => 'required|unique:bulkupload_data_flow_profiles', + 'attribute_family_id' => 'required', + 'locale_code' => 'required' + ]; + } + + public function prepareForValidation() + { + $this->merge([ + 'attribute_family_id' => $this->input('attribute_family'), + ]); + + $this->request->remove('attribute_family'); + } +} diff --git a/src/Resources/lang/en/app.php b/src/Resources/lang/en/app.php index 09ba67a..8b95d40 100644 --- a/src/Resources/lang/en/app.php +++ b/src/Resources/lang/en/app.php @@ -55,7 +55,8 @@ 'upload-sample-files' => 'Upload Sample Files', 'file' => 'CSV/XLS/XLSX file', 'image' => 'Image Zip file', - 'save' => 'Save' + 'save' => 'Save', + 'select-sample-file' => 'Please select a sample file name', ], 'messages' => [ diff --git a/src/Resources/views/admin/bulk-upload/data-flow-profile/index.blade.php b/src/Resources/views/admin/bulk-upload/data-flow-profile/index.blade.php index c2d6b76..72ef1fa 100644 --- a/src/Resources/views/admin/bulk-upload/data-flow-profile/index.blade.php +++ b/src/Resources/views/admin/bulk-upload/data-flow-profile/index.blade.php @@ -23,7 +23,7 @@
- + + +
- + - @foreach ($families as $family) - @endforeach + @endforeach @if ($familyId) @endif - @{{ errors.first('attribute_family_id') }} + +
+ +
+ + + +
diff --git a/src/Resources/views/admin/bulk-upload/data-flow-profile/index.blade.php b/src/Resources/views/admin/bulk-upload/data-flow-profile/index.blade.php index 72ef1fa..b9a004a 100644 --- a/src/Resources/views/admin/bulk-upload/data-flow-profile/index.blade.php +++ b/src/Resources/views/admin/bulk-upload/data-flow-profile/index.blade.php @@ -4,28 +4,36 @@ {{ __('bulkupload::app.admin.bulk-upload.data-flow-profile.index') }} @stop +@push('css') + +@endpush + @section('content') -
+

{{ __('bulkupload::app.admin.bulk-upload.data-flow-profile.add-profile') }}

-
+ @csrf input('family') ?> -
- - - {{ $errors->first('name') }} +
+ + +
-
+
- @@ -39,7 +47,7 @@ @endif - {{ $errors->first('attribute_family') }} +
diff --git a/src/Routes/admin-routes.php b/src/Routes/admin-routes.php index b7aa949..4254efd 100644 --- a/src/Routes/admin-routes.php +++ b/src/Routes/admin-routes.php @@ -32,6 +32,10 @@ Route::post('/importnew', [Webkul\Bulkupload\Http\Controllers\Admin\HelperController::class, 'importNewProductsStore'])->defaults('_config',['view' => 'bulkupload::admin.bulk-upload.upload-files.index' ])->name('import-new-products-form-submit'); Route::prefix('dataflowprofile')->group(function () { + Route::post('/runprofile', [Webkul\Bulkupload\Http\Controllers\Admin\HelperController::class, 'runProfile'])->defaults('_config', [ + 'view' => 'bulkupload::admin.bulk-upload.run-profile.progressbar' + ])->name('bulk-upload-admin.run-profile'); + Route::get('/', [Webkul\Bulkupload\Http\Controllers\Admin\BulkUploadController::class, 'index'])->defaults('_config', [ 'view' => 'bulkupload::admin.bulk-upload.data-flow-profile.index' ])->name('admin.dataflow-profile.index'); @@ -40,11 +44,6 @@ 'view' => 'bulkupload::admin.bulk-upload.data-flow-profile.index' ])->name('bulkupload.bulk-upload.dataflow.add-profile'); - Route::post('/runprofile', [Webkul\Bulkupload\Http\Controllers\Admin\HelperController::class, 'runProfile'])->defaults('_config', [ - 'view' => 'bulkupload::admin.bulk-upload.run-profile.progressbar' - ])->name('bulk-upload-admin.run-profile'); - - // edit actions Route::post('/delete/{id}',[Webkul\Bulkupload\Http\Controllers\Admin\BulkUploadController::class, 'destroy'])->name('bulkupload.admin.profile.delete'); Route::get('/edit/{id}', [Webkul\Bulkupload\Http\Controllers\Admin\BulkUploadController::class, 'edit'])->defaults('_config', [ @@ -55,7 +54,6 @@ 'view' => 'bulkupload::admin.bulk-upload.data-flow-profile.index' ])->name('admin.bulk-upload.dataflow.update-profile'); - //mass destroy Route::post('/massdestroy', [Webkul\Bulkupload\Http\Controllers\Admin\BulkUploadController::class, 'massDestroy'])->defaults('_config', [ 'redirect' => 'admin.dataflow-profile.index' ])->name('bulkupload.admin.profile.massDelete'); diff --git a/webpack.mix.js b/webpack.mix.js index 3dcb352..9924903 100644 --- a/webpack.mix.js +++ b/webpack.mix.js @@ -1,4 +1,9 @@ -const { mix } = require("laravel-mix"); +const mix = require("laravel-mix"); + +if (mix == 'undefined') { + const { mix } = require("laravel-mix"); +} + require("laravel-mix-merge-manifest"); if (mix.inProduction()) { @@ -10,19 +15,17 @@ if (mix.inProduction()) { mix.setPublicPath(publicPath).mergeManifest(); mix.disableNotifications(); -mix - .copyDirectory( - __dirname + "/src/Resources/assets/images", - publicPath + "/images" - ) - .sass( - __dirname + "/src/Resources/assets/sass/admin.scss", - "css/bulk-admin.css" - ) +mix.js(__dirname + "/src/Resources/assets/js/app.js", "js/bulk-admin.js") + .sass(__dirname + "/src/Resources/assets/sass/admin.scss", "css/bulk-admin.css") + .copy(__dirname + '/src/Resources/assets/images', publicPath + '/images') .options({ - processCssUrls: false, + processCssUrls: false }); +if (! mix.inProduction()) { + mix.sourceMaps(); +} + if (mix.inProduction()) { mix.version(); -} +} \ No newline at end of file From 6f00baf34795d64addb53623ebf5723a936ba7e2 Mon Sep 17 00:00:00 2001 From: suraj kashyap Date: Tue, 2 May 2023 15:45:17 +0530 Subject: [PATCH 03/16] Optimize and code refactor for simple product. --- src/Helpers/ImportProduct.php | 94 +++++------- .../Admin/BulkUploadController.php | 24 +-- .../Controllers/Admin/HelperController.php | 45 ++---- src/Models/ImportProduct.php | 7 +- src/Providers/BulkUploadServiceProvider.php | 5 + .../Products/HelperRepository.php | 61 ++------ .../Products/SimpleProductRepository.php | 144 ++++-------------- src/Requests/AddProfileRequest.php | 21 +-- src/Requests/ImportProductRequest.php | 31 ++++ src/Resources/assets/sass/app.scss | 11 ++ .../data-flow-profile/edit.blade.php | 6 +- .../data-flow-profile/index.blade.php | 12 +- .../bulk-upload/run-profile/index.blade.php | 37 ++--- .../bulk-upload/upload-files/index.blade.php | 18 ++- .../views/admin/layouts/style.blade.php | 1 + src/Routes/admin-routes.php | 3 +- webpack.mix.js | 2 +- 17 files changed, 198 insertions(+), 324 deletions(-) create mode 100644 src/Requests/ImportProductRequest.php create mode 100644 src/Resources/views/admin/layouts/style.blade.php diff --git a/src/Helpers/ImportProduct.php b/src/Helpers/ImportProduct.php index 6cbde5e..9276831 100644 --- a/src/Helpers/ImportProduct.php +++ b/src/Helpers/ImportProduct.php @@ -17,7 +17,7 @@ class ImportProduct public function __construct( protected ImportProductRepository $importProductRepository, protected DataFlowProfileRepository $dataFlowProfileRepository - ){} + ) {} /** * store import products for profile execution @@ -26,13 +26,6 @@ public function __construct( */ public function store() { - request()->validate ([ - 'attribute_family' => 'required', - 'file_path' => 'required', - 'image_path' => 'mimetypes:application/zip|max:10000', - 'data_flow_profile' => 'required', - ]); - $valid_extension = ['csv', 'xls', 'xlsx']; $valid_image_extension = ['zip', 'rar']; @@ -41,101 +34,98 @@ public function store() $linkFilesDir = 'imported-products/admin/link-files'; $linkSampleFilesDir = 'imported-products/admin/link-sample-files'; $sampleFileDir = 'imported-products/admin/sample-file'; - - $attribute_family_id = request()->attribute_family; - $data_flow_profile_id = request()->data_flow_profile; - - $image = request()->file('image_path'); - $file = request()->file('file_path'); - $linkFiles = request()->file('link_files'); - $linkSampleFiles = request()->file('link_sample_files'); - $sampleFile = request()->file('sample_file'); - - if (! isset($image)) { - $image = ''; - } - - if (request()->is_downloadable) { + + if (request()->has('is_downloadable')) { $product['is_downloadable'] = 1; - if (! empty($linkFiles) && in_array($linkFiles->getClientOriginalExtension(), $valid_image_extension)) { - $uploadedLinkFiles = $linkFiles->storeAs($linkFilesDir, uniqid().'.'.$linkFiles->getClientOriginalExtension()); + if ( + request()->hasFile('link_files') + && ! in_array(request()->file('link_files')->getClientOriginalExtension(), $valid_image_extension) + ) { + $uploadedLinkFiles = request()->file('link_files')->storeAs($linkFilesDir, uniqid() . '.' . request()->file('link_files')->getClientOriginalExtension()); $product['upload_link_files'] = $uploadedLinkFiles; } else { - session()->flash('error', trans('bulkupload::app.admin.bulk-upload.messages.file-format-error')); + session()->flash('error', __('bulkupload::app.admin.bulk-upload.messages.file-format-error')); return redirect()->route('admin.bulk-upload.index'); } - if (request()->is_link_have_sample) { + if (request()->input('is_link_have_sample')) { $product['is_links_have_samples'] = 1; - if (in_array($linkSampleFiles->getClientOriginalExtension(), $valid_image_extension)) { - $uploadedLinkSampleFiles = $linkSampleFiles->storeAs($linkSampleFilesDir, uniqid().'.'.$linkSampleFiles->getClientOriginalExtension()); + if (in_array(request()->file('link_sample_files')->getClientOriginalExtension(), $valid_image_extension)) { + $uploadedLinkSampleFiles = request()->file('link_sample_files')->storeAs($linkSampleFilesDir, uniqid().'.'.request()->file('link_sample_files')->getClientOriginalExtension()); $product['upload_link_sample_files'] = $uploadedLinkSampleFiles; } else { - session()->flash('error', trans('bulkupload::app.admin.bulk-upload.messages.file-format-error')); + session()->flash('error', __('bulkupload::app.admin.bulk-upload.messages.file-format-error')); return redirect()->route('admin.bulk-upload.index'); } } - if (request()->is_sample) { + if (request()->input('is_sample')) { $product['is_samples_available'] = 1; - if (in_array($sampleFile->getClientOriginalExtension(), $valid_image_extension)) { - $uploadedSampleFiles = $sampleFile->storeAs($sampleFileDir, uniqid().'.'.$sampleFile->getClientOriginalExtension()); + if (in_array(request()->file('sample_file')->getClientOriginalExtension(), $valid_image_extension)) { + $uploadedSampleFiles = request()->file('sample_file')->storeAs($sampleFileDir, uniqid().'.'.request()->file('sample_file')->getClientOriginalExtension()); $product['upload_sample_files'] = $uploadedSampleFiles; } else { - session()->flash('error', trans('bulkupload::app.admin.bulk-upload.messages.file-format-error')); + session()->flash('error', __('bulkupload::app.admin.bulk-upload.messages.file-format-error')); return redirect()->route('admin.bulk-upload.index'); } } } - $product['data_flow_profile_id'] = $data_flow_profile_id; - $product['attribute_family_id'] = $attribute_family_id; + $product['data_flow_profile_id'] = request()->input('data_flow_profile'); + $product['attribute_family_id'] = request()->input('attribute_family'); - if ((! empty($image) && in_array($image->getClientOriginalExtension(), $valid_image_extension)) && (in_array($file->getClientOriginalExtension(), $valid_extension))) { - $uploadedImage = $image->storeAs($imageDir, uniqid().'.'.$image->getClientOriginalExtension()); + if ( + request()->hasFile('image_path') + && in_array(request()->file('image_path')->getClientOriginalExtension(), $valid_image_extension) + && in_array(request()->file('file_path')->getClientOriginalExtension(), $valid_extension) + ) { + $uploadedImage = request()->file('image_path')->storeAs($imageDir, uniqid().'.'.request()->file('image_path')->getClientOriginalExtension()); $product['image_path'] = $uploadedImage; - $uploadedFile = $file->storeAs($fileDir, uniqid().'.'.$file->getClientOriginalExtension()); + $uploadedFile = request()->file('file_path')->storeAs($fileDir, uniqid() . '.' . request()->file('file_path')->getClientOriginalExtension()); $product['file_path'] = $uploadedFile; - } else if ( empty($image) && (in_array($file->getClientOriginalExtension(), $valid_extension))) { + } elseif ( + ! request()->hasFile('image_path') + && in_array(request()->file('file_path')->getClientOriginalExtension(), $valid_extension) + ) { $product['image_path'] = ''; - $uploadedFile = $file->storeAs($fileDir, uniqid().'.'.$file->getClientOriginalExtension()); + $uploadedFile = request()->file('file_path')->storeAs($fileDir, uniqid() . '.' . request()->file('file_path')->getClientOriginalExtension()); $product['file_path'] = $uploadedFile; } else { - session()->flash('error', trans('bulkupload::app.admin.bulk-upload.messages.file-format-error')); + session()->flash('error', __('bulkupload::app.admin.bulk-upload.messages.file-format-error')); return redirect()->route('admin.bulk-upload.index'); } - $data = $this->importProductRepository->findOneByField('data_flow_profile_id', $data_flow_profile_id); + $importedProduct = $this->importProductRepository->findOneByField('data_flow_profile_id', request()->input('data_flow_profile')); - if ($data) { - $this->dataFlowProfileRepository->update(['run_status' => '0'], $data_flow_profile_id); + if ($importedProduct) { + $this->dataFlowProfileRepository->update(['run_status' => '0'], request()->input('data_flow_profile')); - $this->importProductRepository->Update($product, $data->id); + $this->importProductRepository->update($product, $importedProduct->id); - session()->flash('success',trans('bulkupload::app.admin.bulk-upload.messages.update-profile')); + session()->flash('success', __('bulkupload::app.admin.bulk-upload.messages.update-profile')); return redirect()->route('admin.bulk-upload.index'); - } else { - $importNewProductsStore = $this->importProductRepository->create($product); + } - session()->flash('success',trans('bulkupload::app.admin.bulk-upload.messages.profile-saved')); + $this->importProductRepository->create($product); - return redirect()->route('admin.bulk-upload.index'); - } + session()->flash('success', __('bulkupload::app.admin.bulk-upload.messages.profile-saved')); + + return redirect()->route('admin.bulk-upload.index'); } } \ No newline at end of file diff --git a/src/Http/Controllers/Admin/BulkUploadController.php b/src/Http/Controllers/Admin/BulkUploadController.php index 413a0a8..b115271 100644 --- a/src/Http/Controllers/Admin/BulkUploadController.php +++ b/src/Http/Controllers/Admin/BulkUploadController.php @@ -9,13 +9,6 @@ class BulkUploadController extends Controller { - /** - * Contains routes and views related configuration - * - * @var array - */ - protected $_config; - /** * Create a new controller instance. * @@ -28,9 +21,10 @@ class BulkUploadController extends Controller public function __construct( protected AttributeFamilyRepository $attributeFamilyRepository, protected DataFlowProfileRepository $dataFlowProfileRepository, - protected ImportProductRepository $importProductRepository + protected ImportProductRepository $importProductRepository, + protected $_config = null ) { - $this->_config = request('_config'); + $this->_config = $_config ?? request('_config'); } /** @@ -93,12 +87,12 @@ public function edit($id) * @param int $id * @return \Illuminate\Http\Response */ - public function update($id) + public function update(AddProfileRequest $addProfileRequest, $id) { - $this->dataFlowProfileRepository->update(request()->except('_token'), $id); + $this->dataFlowProfileRepository->update($addProfileRequest->validated(), $id); + + session()->flash('success', __('bulkupload::app.admin.bulk-upload.messages.update-profile')); - session()->flash('success', trans('admin::app.response.update-success', ['name' => 'Product'])); - return redirect()->route('admin.dataflow-profile.index'); } @@ -113,9 +107,7 @@ public function destroy($id) { $this->dataFlowProfileRepository->findOrFail($id)->delete(); - session()->flash('success',trans('bulkupload::app.admin.bulk-upload.messages.profile-deleted')); - - return response()->json(['message' => true], 200); + return response()->json(['message' => __('bulkupload::app.admin.bulk-upload.messages.profile-deleted')], 200); } /** diff --git a/src/Http/Controllers/Admin/HelperController.php b/src/Http/Controllers/Admin/HelperController.php index caa3b41..ca02da5 100644 --- a/src/Http/Controllers/Admin/HelperController.php +++ b/src/Http/Controllers/Admin/HelperController.php @@ -2,8 +2,10 @@ namespace Webkul\Bulkupload\Http\Controllers\Admin; +use Illuminate\Support\Facades\Storage; use Webkul\Admin\Imports\DataGridImport; use Webkul\Bulkupload\Helpers\ImportProduct; +use Webkul\Bulkupload\Requests\ImportProductRequest; use Webkul\Bulkupload\Repositories\{ImportProductRepository, DataFlowProfileRepository}; use Webkul\Bulkupload\Repositories\Products\{SimpleProductRepository, ConfigurableProductRepository, VirtualProductRepository}; use Webkul\Bulkupload\Repositories\Products\{DownloadableProductRepository, GroupedProductRepository, BundledProductRepository, BookingProductRepository}; @@ -80,9 +82,7 @@ public function downloadFile() */ public function getAllDataFlowProfiles() { - $attribute_family_id = request()->attribute_family_id; - - $dataFlowProfiles = $this->dataFlowProfileRepository->findByField('attribute_family_id', request()->attribute_family_id); + $dataFlowProfiles = $this->dataFlowProfileRepository->findByField('attribute_family_id', request()->input('attribute_family_id')); return ['dataFlowProfiles' => $dataFlowProfiles]; } @@ -90,15 +90,15 @@ public function getAllDataFlowProfiles() /** * Read count of records in CSV/XLSX * - * @return \Illuminate\Http\Response + * @return integer|void */ public function readCSVData() { $countCSV = 0; - $dataFlowProfileRecord = $this->importProductRepository->findOneByField('data_flow_profile_id', request()->data_flow_profile_id); + $dataFlowProfileRecord = $this->importProductRepository->findOneByField('data_flow_profile_id', request()->input('data_flow_profile_id')); - $this->dataFlowProfileRepository->update(['run_status' => '1'], request()->data_flow_profile_id); + $this->dataFlowProfileRepository->update(['run_status' => '0'], request()->input('data_flow_profile_id')); if ($dataFlowProfileRecord) { $csvData = (new DataGridImport)->toArray($dataFlowProfileRecord->file_path)[0]; @@ -116,40 +116,26 @@ public function readCSVData() } /** - * store import products for profile execution + * Store import products for profile execution * * @return \Illuminate\Http\Response */ - public function importNewProductsStore() + public function importNewProductsStore(ImportProductRequest $importProductRequest) { - $dataFlowProfileId = request()->data_flow_profile; - - if ($dataFlowProfileId) { - $importedProducts = $this->importProduct->store(); - - return $importedProducts; - } else { - session()->flash('error', trans('bulkupload::app.admin.bulk-upload.messages.data-profile-not-selected')); - - return back(); - } + return $this->importProduct->store(); } /** - * profile execution to upload products + * Profile execution to upload products * - * @return \Illuminate\Http\Response + * @return \Illuminate\Http\JsonResponse|void */ public function runProfile() { - $data_flow_profile_id = request()->data_flow_profile_id; - $numberOfCSVRecord = request()->numberOfCSVRecord; - $countOfStartedProfiles = request()->countOfStartedProfiles; $product = []; $imageZipName = null; - $dataFlowProfileRecord = $this->importProductRepository->findOneByField - ('data_flow_profile_id', $data_flow_profile_id); + $dataFlowProfileRecord = $this->importProductRepository->findOneByField('data_flow_profile_id', request()->input('data_flow_profile_id')); if ($dataFlowProfileRecord) { $csvData = (new DataGridImport)->toArray($dataFlowProfileRecord->file_path)[0]; @@ -158,16 +144,15 @@ public function runProfile() $imageZipName = $this->storeImageZip($dataFlowProfileRecord); } - if ($numberOfCSVRecord >= 0) { - for ($i = $countOfStartedProfiles; $i < count($csvData); $i++) { + if ((int) request()->input('numberOfCSVRecord') >= 0) { + for ($i = (int) request()->input('countOfStartedProfiles'); $i < count($csvData); $i++) { $product['loopCount'] = $i; - switch($csvData[$i]['type']) { + switch ($csvData[$i]['type']) { case "simple": $simpleProduct = $this->simpleProductRepository->createProduct(request()->all(), $imageZipName, $product); return response()->json($simpleProduct); - case "virtual": $virtualProduct = $this->virtualProductRepository->createProduct(request()->all(), $imageZipName); diff --git a/src/Models/ImportProduct.php b/src/Models/ImportProduct.php index 387a540..f2aba73 100644 --- a/src/Models/ImportProduct.php +++ b/src/Models/ImportProduct.php @@ -7,7 +7,10 @@ class ImportProduct extends Model implements ImportProductContract { - protected $table = "import_products"; - + /** + * Define guarded property + * + * @var array + */ protected $guarded = []; } diff --git a/src/Providers/BulkUploadServiceProvider.php b/src/Providers/BulkUploadServiceProvider.php index a877b87..b51424f 100644 --- a/src/Providers/BulkUploadServiceProvider.php +++ b/src/Providers/BulkUploadServiceProvider.php @@ -3,6 +3,7 @@ namespace Webkul\Bulkupload\Providers; use Illuminate\Support\ServiceProvider; +use Illuminate\Support\Facades\Event; class BulkUploadServiceProvider extends ServiceProvider { @@ -26,6 +27,10 @@ public function boot() $this->publishes([ __DIR__ . '/../Resources/views/admin/bulk-upload/layouts/nav-aside.blade.php' => resource_path('views/vendor/admin/layouts/nav-aside.blade.php'), ]); + + Event::listen('bagisto.admin.layout.head', function($viewRenderEventManager) { + $viewRenderEventManager->addTemplate('bulkupload::admin.layouts.style'); + }); view()->composer(['bulkupload::admin.bulk-upload.upload-files.index'], function ($view) { $items = []; diff --git a/src/Repositories/Products/HelperRepository.php b/src/Repositories/Products/HelperRepository.php index 72cab73..91b4b50 100644 --- a/src/Repositories/Products/HelperRepository.php +++ b/src/Repositories/Products/HelperRepository.php @@ -2,7 +2,6 @@ namespace Webkul\Bulkupload\Repositories\Products; -use Illuminate\Container\Container as App; use Webkul\Core\Eloquent\Repository; use Illuminate\Support\Facades\Validator; use Webkul\Product\Models\ProductAttributeValue; @@ -14,32 +13,11 @@ class HelperRepository extends Repository { /** - * DataFlowProfileRepository object + * Contains product id * - * @var \Webkul\Bulkupload\Repositories\DataFlowProfileRepository + * @var mixed */ - protected $dataFlowProfileRepository; - - /** - * ProductFlatRepository object - * - * @var \Webkul\Product\Repositories\ProductFlatRepository - */ - protected $productFlatRepository; - - /** - * ProductRepository object - * - * @var \Webkul\Product\Repositories\ProductRepository - */ - protected $productRepository; - - /** - * ProductAttributeValueRepository object - * - * @var \Webkul\Product\Repositories\ProductAttributeValueRepository - */ - protected $productAttributeValueRepository; + protected $id; /** * Create a new repository instance. @@ -51,20 +29,11 @@ class HelperRepository extends Repository * @return void */ public function __construct( - DataFlowProfileRepository $dataFlowProfileRepository, - ProductAttributeValueRepository $productAttributeValueRepository, - ProductFlatRepository $productFlatRepository, - ProductRepository $productRepository - ) - { - $this->dataFlowProfileRepository = $dataFlowProfileRepository; - - $this->productAttributeValueRepository = $productAttributeValueRepository; - - $this->productFlatRepository = $productFlatRepository; - - $this->productRepository = $productRepository; - } + protected DataFlowProfileRepository $dataFlowProfileRepository, + protected ProductAttributeValueRepository $productAttributeValueRepository, + protected ProductFlatRepository $productFlatRepository, + protected ProductRepository $productRepository + ) {} /** * Specify Model class name @@ -79,18 +48,15 @@ function model() /** * validation Rules for creating product * - * @param integer $dataFlowProfileId - * @param array $records + * @param integer $dataFlowProfileId + * @param array $records * @param \Webkul\Bulkupload\Contracts\ImportProduct $dataFlowProfileRecord * @param \Webkul\Product\Contracts\Product $product - * @return array + * @return array|void */ public function validateCSV($dataFlowProfileId, $records, $dataFlowProfileRecord, $product) { - $messages = []; - $rules = []; - - $profiler = $this->dataFlowProfileRepository->findOneByField('id', $dataFlowProfileId); + $this->dataFlowProfileRepository->findOneByField('id', $dataFlowProfileId); if ($dataFlowProfileRecord) { foreach($records as $data) { @@ -211,7 +177,6 @@ public function createProductValidation($record, $loopCount) } return null; - } catch(\EXception $e) { - } + } catch(\EXception $e) {} } } diff --git a/src/Repositories/Products/SimpleProductRepository.php b/src/Repositories/Products/SimpleProductRepository.php index 07f812e..f0f3b01 100644 --- a/src/Repositories/Products/SimpleProductRepository.php +++ b/src/Repositories/Products/SimpleProductRepository.php @@ -2,132 +2,43 @@ namespace Webkul\Bulkupload\Repositories\Products; -use Storage; -use Illuminate\Container\Container as App; +use Illuminate\Support\Facades\{Validator,Event, Log, Storage}; use Webkul\Admin\Imports\DataGridImport; -use Illuminate\Support\Facades\Validator; use Webkul\Core\Eloquent\Repository; use Webkul\Category\Repositories\CategoryRepository; -use Webkul\Bulkupload\Repositories\ImportProductRepository; -use Webkul\Product\Repositories\ProductFlatRepository; use Webkul\Inventory\Repositories\InventorySourceRepository; -use Webkul\Product\Repositories\ProductRepository; -use Webkul\Attribute\Repositories\AttributeFamilyRepository; -use Webkul\Bulkupload\Repositories\Products\HelperRepository; -use Webkul\Attribute\Repositories\AttributeOptionRepository; -use Webkul\Bulkupload\Repositories\ProductImageRepository; -use Webkul\Product\Repositories\ProductCustomerGroupPriceRepository; +use Webkul\Product\Repositories\{ProductFlatRepository, ProductRepository}; +use Webkul\Attribute\Repositories\{AttributeFamilyRepository, AttributeOptionRepository}; +use Webkul\Bulkupload\Repositories\{Products\HelperRepository, ImportProductRepository, ProductImageRepository}; class SimpleProductRepository extends Repository { - /** - * ImportProductRepository object - * - * @var \Webkul\Bulkupload\Repositories\ImportProductRepository - */ - protected $importProductRepository; - - /** - * CategoryRepository object - * - * @var \Webkul\Category\Repositories\CategoryRepository - */ - protected $categoryRepository; - - /** - * ProductFlatRepository object - * - * @var \Webkul\Product\Repositories\ProductFlatRepository - */ - protected $productFlatRepository; - - /** - * ProductRepository object - * - * @var \Webkul\Product\Repositories\ProductRepository - */ - protected $productRepository; - - /** - * AttributeFamilyRepository object - * - * @var \Webkul\Attribute\Repositories\AttributeFamilyRepository - */ - protected $attributeFamilyRepository; - - /** - * HelperRepository object - * - * @var \Webkul\Bulkupload\Repositories\Products\HelperRepository - */ - protected $helperRepository; - - /** - * ProductImageRepository object - * - * @var \Webkul\Bulkupload\Repositories\ProductImageRepository - */ - protected $productImageRepository; - - /** - * AttributeOptionRepository object - * - * @var \Webkul\Attribute\Repositories\AttributeOptionRepository - */ - protected $attributeOptionRepository; - - /** - * InventorySourceRepository object - * - * @var \Webkul\Inventory\Repositories\InventorySourceRepository - */ - protected $inventorySourceRepository; - /** * Create a new repository instance. * - * @param \Webkul\Bulkupload\Repositories\ImportProductRepository $importProductRepository - * @param \Webkul\Attribute\Repositories\AttributeOptionRepository $attributeOptionRepository * @param \Webkul\Category\Repositories\CategoryRepository $categoryRepository + * @param \Webkul\Inventory\Repositories\InventorySourceRepository $inventorySourceRepository * @param \Webkul\Product\Repositories\ProductFlatRepository $productFlatRepository * @param \Webkul\Product\Repositories\ProductRepository $productRepository * @param \Webkul\Attribute\Repositories\AttributeFamilyRepository $attributeFamilyRepository + * @param \Webkul\Attribute\Repositories\AttributeOptionRepository $attributeOptionRepository * @param \Webkul\Bulkupload\Repositories\Products\HelperRepository $helperRepository + * @param \Webkul\Bulkupload\Repositories\ImportProductRepository $importProductRepository * @param \Webkul\Bulkupload\Repositories\ProductImageRepository $productImageRepository - * @param \Webkul\Inventory\Repositories\InventorySourceRepository $inventorySourceRepository * * @return void */ public function __construct( - ImportProductRepository $importProductRepository, - AttributeOptionRepository $attributeOptionRepository, - CategoryRepository $categoryRepository, - ProductFlatRepository $productFlatRepository, - ProductRepository $productRepository, - AttributeFamilyRepository $attributeFamilyRepository, - HelperRepository $helperRepository, - ProductImageRepository $productImageRepository, - InventorySourceRepository $inventorySourceRepository - ) - { - $this->importProductRepository = $importProductRepository; - - $this->categoryRepository = $categoryRepository; - - $this->attributeOptionRepository = $attributeOptionRepository; - - $this->productFlatRepository = $productFlatRepository; - - $this->productRepository = $productRepository; - - $this->productImageRepository = $productImageRepository; - - $this->attributeFamilyRepository = $attributeFamilyRepository; - - $this->helperRepository = $helperRepository; - - $this->inventorySourceRepository = $inventorySourceRepository; - } + protected CategoryRepository $categoryRepository, + protected InventorySourceRepository $inventorySourceRepository, + protected ProductFlatRepository $productFlatRepository, + protected ProductRepository $productRepository, + protected AttributeFamilyRepository $attributeFamilyRepository, + protected AttributeOptionRepository $attributeOptionRepository, + protected HelperRepository $helperRepository, + protected ImportProductRepository $importProductRepository, + protected ProductImageRepository $productImageRepository, + ) {} /* * Specify Model class name @@ -151,10 +62,7 @@ function model() public function createProduct($requestData, $imageZipName, $product) { try { - $inventory = []; - - $dataFlowProfileRecord = $this->importProductRepository->findOneByField - ('data_flow_profile_id', $requestData['data_flow_profile_id']); + $dataFlowProfileRecord = $this->importProductRepository->findOneByField('data_flow_profile_id', $requestData['data_flow_profile_id']); $csvData = (new DataGridImport)->toArray($dataFlowProfileRecord->file_path)[0]; @@ -168,7 +76,6 @@ public function createProduct($requestData, $imageZipName, $product) $processRecords = (int)$requestData['countOfStartedProfiles'] + (int)$requestData['numberOfCSVRecord']; - if ($requestData['numberOfCSVRecord'] > $processCSVRecords) { for ($i = $requestData['countOfStartedProfiles']; $i < $uptoProcessCSVRecords; $i++) { $invalidateProducts = $this->store($csvData[$i], $i, $dataFlowProfileRecord, $requestData, $imageZipName); @@ -209,7 +116,7 @@ public function createProduct($requestData, $imageZipName, $product) return $dataToBeReturn; } catch (\Exception $e) { - Log::error('simple create product log: '. $e->getMessage()); + Log::error('simple create product log: ' . $e->getMessage()); $categoryError = explode('[' ,$e->getMessage()); $categorySlugError = explode(']' ,$e->getMessage()); @@ -281,7 +188,11 @@ public function store($csvData, $i, $dataFlowProfileRecord, $requestData, $image $data['attribute_family_id'] = $attributeFamilyData->id; $data['sku'] = $csvData['sku']; + Event::dispatch('catalog.product.create.before'); + $simpleproductData = $this->productRepository->create($data); + + Event::dispatch('catalog.product.create.after', $simpleproductData); } else { $simpleproductData = $productData; } @@ -297,7 +208,6 @@ public function store($csvData, $i, $dataFlowProfileRecord, $requestData, $image $searchIndex = strtolower($value['code']); if (array_key_exists($searchIndex, $csvData)) { - if (is_null($csvData[$searchIndex])) { continue; } @@ -386,7 +296,7 @@ public function store($csvData, $i, $dataFlowProfileRecord, $requestData, $image foreach ($individualProductimages as $imageArraykey => $imageURL) { if (filter_var(trim($imageURL), FILTER_VALIDATE_URL)) { - $imagePath = storage_path('app/public/imported-products/extracted-images/admin/'.$dataFlowProfileRecord->id); + $imagePath = storage_path('app/public/imported-products/extracted-images/admin/' . $dataFlowProfileRecord->id); if (!file_exists($imagePath)) { mkdir($imagePath, 0777, true); @@ -438,15 +348,19 @@ public function store($csvData, $i, $dataFlowProfileRecord, $requestData, $image return $dataToBeReturn; } + Event::dispatch('catalog.product.update.before', $simpleproductData->id); + $configSimpleProductAttributeStore = $this->productRepository->update($data, $simpleproductData->id); + Event::dispatch('catalog.product.update.after', $configSimpleProductAttributeStore); + if (isset($imageZipName)) { $this->productImageRepository->bulkuploadImages($data, $simpleproductData, $imageZipName); } else if (isset($csvData['images'])) { $this->productImageRepository->bulkuploadImages($data, $simpleproductData, $imageZipName = null); } } catch(\Exception $e) { - \Log::error('simple product store function'. $e->getMessage()); + Log::error('simple product store function'. $e->getMessage()); } } } \ No newline at end of file diff --git a/src/Requests/AddProfileRequest.php b/src/Requests/AddProfileRequest.php index d247796..edbe338 100644 --- a/src/Requests/AddProfileRequest.php +++ b/src/Requests/AddProfileRequest.php @@ -21,24 +21,17 @@ public function authorize(): bool */ public function rules(): array { - return [ - 'name' => 'required|unique:bulkupload_data_flow_profiles', + $rules = [ 'attribute_family_id' => 'required', 'locale_code' => 'required' ]; - } - /** - * Prepare for validation - * - * @return void - */ - public function prepareForValidation(): void - { - $this->merge([ - 'attribute_family_id' => $this->input('attribute_family'), - ]); + if (request()->routeIs('admin.bulk-upload.dataflow.update-profile')){ + $rules['name'] = 'required|unique:bulkupload_data_flow_profiles,name,' . $this->id; + } else { + $rules['name'] = 'required|unique:bulkupload_data_flow_profiles'; + } - $this->request->remove('attribute_family'); + return $rules; } } diff --git a/src/Requests/ImportProductRequest.php b/src/Requests/ImportProductRequest.php new file mode 100644 index 0000000..099033f --- /dev/null +++ b/src/Requests/ImportProductRequest.php @@ -0,0 +1,31 @@ + + */ + public function rules(): array + { + return [ + 'attribute_family' => 'required', + 'file_path' => 'required', + 'image_path' => 'mimetypes:application/zip|max:10000', + 'data_flow_profile' => 'required', + ]; + } +} diff --git a/src/Resources/assets/sass/app.scss b/src/Resources/assets/sass/app.scss index 6fa0b45..e054447 100644 --- a/src/Resources/assets/sass/app.scss +++ b/src/Resources/assets/sass/app.scss @@ -1,3 +1,5 @@ +@import "admin.scss"; + .bulk-upload-icon { width: 48px; height: 48px; @@ -49,4 +51,13 @@ progress { height: 18px; background-image: url("../images/finish.svg"); } +} + +.display-flex { + display: flex +} + +.checkbox { + height: 17px; + width: 17px; } \ No newline at end of file diff --git a/src/Resources/views/admin/bulk-upload/data-flow-profile/edit.blade.php b/src/Resources/views/admin/bulk-upload/data-flow-profile/edit.blade.php index e82e66d..cd477a6 100644 --- a/src/Resources/views/admin/bulk-upload/data-flow-profile/edit.blade.php +++ b/src/Resources/views/admin/bulk-upload/data-flow-profile/edit.blade.php @@ -20,7 +20,7 @@

{{ __('bulkupload::app.admin.bulk-upload.data-flow-profile.edit-profile') }}

- + @csrf input('family') ?> @@ -51,7 +51,7 @@ + @if ($familyId) - + @endif - +
diff --git a/src/Resources/views/admin/bulk-upload/run-profile/index.blade.php b/src/Resources/views/admin/bulk-upload/run-profile/index.blade.php index 33929b2..edd6cac 100644 --- a/src/Resources/views/admin/bulk-upload/run-profile/index.blade.php +++ b/src/Resources/views/admin/bulk-upload/run-profile/index.blade.php @@ -126,36 +126,27 @@ computed: { isDisabled () { - if (this.data_flow_profile == '' || this.data_flow_profile == 'Please Select') { + if (this.data_flow_profile == '' || this.data_flow_profile == 'Please Select') return true; - } else { - return false; - } + + return false; } }, methods:{ - detectProfile: function() { - event.target.disabled = true; - }, - runProfiler: function(e) { event.target.disabled = true; - this.detectProfile(); - - const uri = "{{ route('bulk-upload-admin.read-csv') }}" - this.$http.post(uri, { + this.$http.post("{{ route('bulk-upload-admin.read-csv') }}", { data_flow_profile_id: this.data_flow_profile - }) - .then((result) => { + }).then((result) => { totalRecords = result.data; if (typeof(totalRecords) == 'number') { this.product.totalCSVRecords = this.product.remainDataInCSV = totalRecords; } - if(totalRecords > this.product.countOfStartedProfiles) { + if (totalRecords > this.product.countOfStartedProfiles) { this.initiateProfiler(totalRecords); } else { window.flashMessages = [{ @@ -165,24 +156,18 @@ this.$root.addFlashMessages() } - }) - .catch(function (error) { - }); + }).catch(function (error) {}); }, initiateProfiler: function(totalRecords) { - - const url = "{{ route('bulk-upload-admin.run-profile') }}" - - this.$http.post(url, { + this.$http.post("{{ route('bulk-upload-admin.run-profile') }}", { data_flow_profile_id: this.data_flow_profile, numberOfCSVRecord: totalRecords, countOfStartedProfiles: this.product.countOfStartedProfiles, totalNumberOfCSVRecord: this.product.totalCSVRecords, productUploaded: this.product.countOfImportedProduct, errorCount: this.product.countOfError - }) - .then((result) => { + }).then((result) => { this.data = result.data; if (this.data.error) { @@ -202,9 +187,7 @@ this.product.countOfStartedProfiles = this.data.countOfStartedProfiles; this.calculateProgress(result.data); - }) - .catch(function(error) { - }); + }).catch(function(error) {}); }, calculateProgress(result) { diff --git a/src/Resources/views/admin/bulk-upload/upload-files/index.blade.php b/src/Resources/views/admin/bulk-upload/upload-files/index.blade.php index 92dd57a..3212257 100644 --- a/src/Resources/views/admin/bulk-upload/upload-files/index.blade.php +++ b/src/Resources/views/admin/bulk-upload/upload-files/index.blade.php @@ -14,8 +14,8 @@