1
|
|
|
<?php namespace VojtaSvoboda\UserImportExport\Models; |
2
|
|
|
|
3
|
|
|
use Backend\Models\ImportModel; |
4
|
|
|
use Cms\Classes\MediaLibrary; |
5
|
|
|
use System\Models\File; |
6
|
|
|
use RainLab\Location\Models\State; |
7
|
|
|
use RainLab\User\Models\User; |
8
|
|
|
|
9
|
|
|
class UserImportModel extends ImportModel |
10
|
|
|
{ |
11
|
|
|
public $rules = []; |
12
|
|
|
|
13
|
|
|
public $imageStoragePath = '/users'; |
14
|
|
|
|
15
|
|
|
public $imagePublic = true; |
16
|
|
|
|
17
|
|
|
public function importData($results, $sessionKey = null) |
18
|
|
|
{ |
19
|
|
|
foreach ($results as $row => $data) |
20
|
|
|
{ |
21
|
|
|
$data += [ |
22
|
|
|
'is_activated' => true, |
23
|
|
|
|
24
|
|
|
/* prepare for future usage */ |
25
|
|
|
// 'state_id' => $this->getStateId($data['state']), |
|
|
|
|
26
|
|
|
// 'country_id' => 14 |
|
|
|
|
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
if ( empty($data['username']) ) { |
30
|
|
|
$data['username'] = $data['email']; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ( empty($data['password']) ) { |
34
|
|
|
$data['password'] = $data['username']; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ( empty($data['password_confirmation']) ) { |
38
|
|
|
$data['password_confirmation'] = $data['password']; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
try { |
42
|
|
|
$user = new User(); |
43
|
|
|
$user->fill($data); |
44
|
|
|
|
45
|
|
|
// try to find avatar |
46
|
|
|
$avatar = $this->findAvatar($data['username']); |
47
|
|
|
if ( $avatar ) { |
48
|
|
|
$user->avatar = $avatar; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// save user |
52
|
|
|
$user->save(); |
53
|
|
|
|
54
|
|
|
// activate user |
55
|
|
|
$user->attemptActivation($user->activation_code); |
56
|
|
|
|
57
|
|
|
$this->logCreated(); |
58
|
|
|
|
59
|
|
|
} catch (\Exception $ex) { |
60
|
|
|
$this->logError($row, $ex->getMessage()); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $username |
67
|
|
|
* |
68
|
|
|
* @return string|null |
69
|
|
|
*/ |
70
|
|
|
private function findAvatar($username) |
71
|
|
|
{ |
72
|
|
|
$library = MediaLibrary::instance(); |
73
|
|
|
$files = $library->listFolderContents($this->imageStoragePath, 'title', 'image'); |
74
|
|
|
|
75
|
|
|
foreach($files as $file) { |
76
|
|
|
$pathinfo = pathinfo($file->publicUrl); |
77
|
|
|
if ( $pathinfo['filename'] == $username ) { |
78
|
|
|
$file = new File(); |
79
|
|
|
$file->is_public = $this->imagePublic; |
80
|
|
|
$file->fromFile(base_path() . $pathinfo['dirname'] . '/' . $pathinfo['basename']); |
81
|
|
|
|
82
|
|
|
return $file; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get state by ident |
89
|
|
|
* |
90
|
|
|
* @param $stateIdent |
91
|
|
|
* |
92
|
|
|
* @return null |
93
|
|
|
*/ |
94
|
|
|
/* |
|
|
|
|
95
|
|
|
private function getStateId($stateIdent) |
96
|
|
|
{ |
97
|
|
|
$state = State::where('code', $stateIdent)->first(); |
98
|
|
|
if ( !$state ) return null; |
99
|
|
|
|
100
|
|
|
return $state->id; |
101
|
|
|
} |
102
|
|
|
*/ |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.