소스 검색

[Administration] Utilisateurs : import CSV #1332

feature/souke
Guillaume Bourgeois 1 년 전
부모
커밋
d024c6a37e
2개의 변경된 파일25개의 추가작업 그리고 6개의 파일을 삭제
  1. +8
    -3
      backend/controllers/UserImportController.php
  2. +17
    -3
      common/logic/User/User/Service/UserBulkImporter.php

+ 8
- 3
backend/controllers/UserImportController.php 파일 보기

@@ -39,7 +39,7 @@
namespace backend\controllers;

use backend\forms\UserImportUploadForm;
use common\helpers\CSV;
use yii\base\ErrorException;
use yii\filters\AccessControl;
use yii\web\UploadedFile;

@@ -71,8 +71,13 @@ class UserImportController extends BackendController
if (\Yii::$app->request->isPost) {
$model->file = UploadedFile::getInstance($model, 'file');
if($model->file && $model->validate()) {
$this->getUserModule()->getBulkImporter()->import($model->file->tempName);
$this->setFlash('success', "Fichier importé.");
try {
$this->getUserModule()->getBulkImporter()->import($model->file->tempName);
$this->setFlash('success', "Fichier importé.");
}
catch(ErrorException $exception) {
$this->setFlash('error', $exception->getMessage());
}
}
}


+ 17
- 3
common/logic/User/User/Service/UserBulkImporter.php 파일 보기

@@ -5,8 +5,7 @@ namespace common\logic\User\User\Service;
use common\helpers\CSV;
use common\helpers\Password;
use common\logic\AbstractManager;
use common\logic\User\User\Model\User;
use common\logic\User\UserProducer\Model\UserProducer;
use yii\base\ErrorException;

class UserBulkImporter extends AbstractManager
{
@@ -28,9 +27,12 @@ class UserBulkImporter extends AbstractManager
$this->userBuilder = $this->loadService(UserBuilder::class);
}

/**
* @throws ErrorException
*/
public function import(string $fileName): void
{
$usersArray = CSV::csv2array($fileName);
$usersArray = $this->loadCsv($fileName);

foreach($usersArray as $userData) {
$this->userBuilder->createUser(
@@ -46,4 +48,16 @@ class UserBulkImporter extends AbstractManager
);
}
}

public function loadCsv(string $fileName): array
{
$usersArray = CSV::csv2array($fileName);

if(count($usersArray) && count($usersArray[0]) != 6) {
throw new ErrorException("Le fichier n'a pas le bon nombre de colonnes.
Veuillez vous baser sur le fichier d'exemple téléchargeable ci-dessous.");
}

return $usersArray;
}
}

Loading…
취소
저장