1 | <?php |
||
16 | class JpasImportHandler implements ImportHandler |
||
17 | { |
||
18 | use DateFormat; |
||
19 | |||
20 | private $unique; |
||
21 | private $changes; |
||
22 | |||
23 | /** |
||
24 | * Set our variables as new collections. |
||
25 | */ |
||
26 | public function __construct() |
||
27 | { |
||
28 | $this->unique = new Collection(); |
||
29 | $this->changes = new Collection(); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Initial call from UserController. |
||
34 | * Based on what we get, we will either call an initial import or the resolve import. |
||
35 | * |
||
36 | * @param $import |
||
37 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | public function handle($import) |
||
41 | { |
||
42 | $excel = $import->get(); |
||
43 | $data = Input::all(); |
||
44 | |||
45 | //We always pass a token and the file. If there is anything more, then we are resolving. |
||
46 | if ($data['resolveImport']) { |
||
47 | $this->resolveImport($excel, $data); |
||
48 | } else { |
||
49 | $this->initialImport($excel); |
||
50 | } |
||
51 | |||
52 | //Return this data. It is just used for the initial import, but still nice to see. |
||
53 | return ['unique' => $this->unique, 'changes' => $this->changes]; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Cycle through our excel spreadsheet. |
||
58 | * If we can't figure where data goes, we will push it to a collection so the user can map it. |
||
59 | * Otherwise, we will update the record. |
||
60 | * |
||
61 | * @param $excel |
||
62 | */ |
||
63 | private function initialImport($excel) |
||
64 | { |
||
65 | foreach ($excel as $row) { |
||
66 | $row->name = preg_replace('/(,\s|\s)/', '_', $row->name); |
||
67 | if (!is_null($row->name) && $row->name != '') { //we get a bunch of null records that we can ignore. |
||
68 | $user = User::where('jpas_name', $row->name)->first(); //see if the record maps to a user |
||
69 | if (is_null($user)) { |
||
70 | $this->unique->push($row); //if no results, we need to have the admin map this record to a user. |
||
71 | } else { |
||
72 | $this->getUserChanges($this->mapJpasToUser($user, $row)); |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * User has mapped some accounts. We need to update those records. |
||
80 | * |
||
81 | * @param $excel |
||
82 | * @param $data |
||
83 | */ |
||
84 | private function resolveImport($excel, $data) |
||
85 | { |
||
86 | foreach ($data as $jpasName => $userGroup) { |
||
87 | if ($jpasName == 'approve') { |
||
88 | $this->updateUserData($userGroup); |
||
89 | } elseif ($userGroup != '' && is_numeric($userGroup)) { |
||
90 | $this->importNewData($excel, $userGroup, $jpasName); |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Place changes so the user can approve/reject them. |
||
97 | * |
||
98 | * @param $user |
||
99 | */ |
||
100 | private function getUserChanges($user) |
||
101 | { |
||
102 | if ($user->isDirty()) { |
||
103 | foreach ($user->getDirty() as $attribute => $newValue) { |
||
104 | $this->changes->push([ |
||
105 | 'user' => $user, |
||
106 | 'field' => $attribute, |
||
107 | 'original' => $user->getOriginal($attribute), |
||
108 | 'new' => $newValue, |
||
109 | ]); |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Update user. If there are changes, let's log them to an array. |
||
116 | * |
||
117 | * @param $user |
||
118 | */ |
||
119 | private function updateAndLogUser($user) |
||
120 | { |
||
121 | //We will let the user know what changes were made. |
||
122 | if ($user->isDirty()) { |
||
123 | $log = "The following changes have been made for $user->userFullName: \n"; |
||
124 | foreach ($user->getDirty() as $attribute => $value) { |
||
125 | $original = $user->getOriginal($attribute); |
||
126 | $log .= "Changed $attribute from '$original' to '$value'.\n"; |
||
127 | } |
||
128 | $user->save(); |
||
129 | $this->changes->push($log); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Map the data from our JPAS Excel to our User model. |
||
135 | * |
||
136 | * @param $user |
||
137 | * @param $data |
||
138 | */ |
||
139 | private function mapJpasToUser($user, $data) |
||
149 | |||
150 | /** |
||
151 | * @param $excel |
||
152 | * @param $userGroup |
||
153 | * @param $jpasName |
||
154 | * |
||
155 | * @return mixed |
||
156 | */ |
||
157 | private function importNewData($excel, $userGroup, $jpasName) |
||
167 | |||
168 | /** |
||
169 | * @param $userGroup |
||
170 | */ |
||
171 | private function updateUserData($userGroup) |
||
183 | } |
||
184 |