-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUserImportModel.php
80 lines (63 loc) · 2.14 KB
/
UserImportModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php namespace VojtaSvoboda\UserImportExport\Models;
use Backend\Models\ImportModel;
use System\Classes\MediaLibrary;
use System\Models\File;
use RainLab\Location\Models\State;
use RainLab\User\Models\User;
class UserImportModel extends ImportModel
{
public $rules = [];
public $imageStoragePath = '/users';
public $imagePublic = true;
public function importData($results, $sessionKey = null)
{
foreach ($results as $row => $data) {
$data += [
'is_activated' => true,
];
if (empty($data['username'])) {
$data['username'] = $data['email'];
}
if (empty($data['password'])) {
$data['password'] = $data['username'];
}
if (empty($data['password_confirmation'])) {
$data['password_confirmation'] = $data['password'];
}
try {
$user = new User();
$user->fill($data);
// try to find avatar
$avatar = $this->findAvatar($data['username']);
if ($avatar) {
$user->avatar = $avatar;
}
// save user
$user->save();
// activate user (it sends welcome email)
// $user->attemptActivation($user->activation_code);
$this->logCreated();
} catch (\Exception $ex) {
$this->logError($row, $ex->getMessage());
}
}
}
/**
* @param string $username
* @return string|null
*/
private function findAvatar($username)
{
$library = MediaLibrary::instance();
$files = $library->listFolderContents($this->imageStoragePath, 'title', 'image');
foreach ($files as $file) {
$pathinfo = pathinfo($file->publicUrl);
if ($pathinfo['filename'] == $username) {
$newFile = new File();
$newFile->is_public = $this->imagePublic;
$newFile->fromFile(storage_path('app/media' . $file->path));
return $newFile;
}
}
}
}