瀏覽代碼

[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…
取消
儲存