LicenseImportCommand.php
Go to the documentation of this file.
1 <?php
2 namespace App\Console\Commands;
3 
8 
9 class LicenseImportCommand extends Command {
10 
16  protected $name = 'license-import:csv';
17 
23  protected $description = 'Import Licenses from CSV';
24 
30  public function __construct()
31  {
32  parent::__construct();
33  }
34 
40  public function fire()
41  {
42  $filename = $this->argument('filename');
43 
44 
45  if (!$this->option('testrun')=='true') {
46  $this->comment('======= Importing Licenses from '.$filename.' =========');
47  } else {
48  $this->comment('====== TEST ONLY License Import for '.$filename.' ====');
49  $this->comment('============== NO DATA WILL BE WRITTEN ==============');
50  }
51 
52  if (! ini_get("auto_detect_line_endings")) {
53  ini_set("auto_detect_line_endings", '1');
54  }
55 
56  $csv = Reader::createFromPath($this->argument('filename'));
57  $csv->setNewline("\r\n");
58  $csv->setOffset(1);
59  $duplicates = '';
60 
61  // Loop through the records
62  $nbInsert = $csv->each(function ($row) use ($duplicates) {
63  $status_id = 1;
64 
65  // Let's just map some of these entries to more user friendly words
66 
67  if (array_key_exists('0',$row)) {
68  $user_name = trim($row[0]);
69  } else {
70  $user_name = '';
71  }
72 
73  if (array_key_exists('1',$row)) {
74  $user_email = trim($row[1]);
75  } else {
76  $user_email = '';
77  }
78 
79  if (array_key_exists('2',$row)) {
80  $user_username = trim($row[2]);
81  } else {
82  $user_username = '';
83  }
84 
85  if (array_key_exists('3',$row)) {
86  $user_license_name = trim($row[3]);
87  } else {
88  $user_license_name = '';
89  }
90 
91  if (array_key_exists('4',$row)) {
92  $user_license_serial = trim($row[4]);
93  } else {
94  $user_license_serial = '';
95  }
96 
97  if (array_key_exists('5',$row)) {
98  $user_licensed_to_name = trim($row[5]);
99  } else {
100  $user_licensed_to_name = '';
101  }
102 
103  if (array_key_exists('6',$row)) {
104  $user_licensed_to_email = trim($row[6]);
105  } else {
106  $user_licensed_to_email = '';
107  }
108 
109  if (array_key_exists('7',$row)) {
110  $user_license_seats = trim($row[7]);
111  } else {
112  $user_license_seats = '';
113  }
114 
115  if (array_key_exists('8',$row)) {
116  $user_license_reassignable = trim($row[8]);
117  if ($user_license_reassignable!='') {
118  if ((strtolower($user_license_reassignable)=='yes') || (strtolower($user_license_reassignable)=='true') || ($user_license_reassignable=='1')) {
119  $user_license_reassignable = 1;
120  }
121  } else {
122  $user_license_reassignable = 0;
123  }
124  } else {
125  $user_license_reassignable = 0;
126  }
127 
128  if (array_key_exists('9',$row)) {
129  $user_license_supplier = trim($row[9]);
130  } else {
131  $user_license_supplier = '';
132  }
133 
134  if (array_key_exists('10',$row)) {
135  $user_license_maintained = trim($row[10]);
136 
137  if ($user_license_maintained!='') {
138  if ((strtolower($user_license_maintained)=='yes') || (strtolower($user_license_maintained)=='true') || ($user_license_maintained=='1')) {
139  $user_license_maintained = 1;
140  }
141  } else {
142  $user_license_maintained = 0;
143  }
144 
145 
146  } else {
147  $user_license_maintained = '';
148  }
149 
150  if (array_key_exists('11',$row)) {
151  $user_license_notes = trim($row[11]);
152  } else {
153  $user_license_notes = '';
154  }
155 
156  if (array_key_exists('12',$row)) {
157  if ($row[12]!='') {
158  $user_license_purchase_date = date("Y-m-d 00:00:01", strtotime($row[12]));
159  } else {
160  $user_license_purchase_date = '';
161  }
162  } else {
163  $user_license_purchase_date = 0;
164  }
165 
166  // A number was given instead of a name
167  if (is_numeric($user_name)) {
168  $this->comment('User '.$user_name.' is not a name - assume this user already exists');
169  $user_username = '';
170  // No name was given
171 
172  } elseif ($user_name=='') {
173  $this->comment('No user data provided - skipping user creation, just adding license');
174  $first_name = '';
175  $last_name = '';
176  $user_username = '';
177 
178  } else {
179 
180  $name = explode(" ", $user_name);
181  $first_name = $name[0];
182  $email_last_name = '';
183  $email_prefix = $first_name;
184 
185  if (!array_key_exists(1, $name)) {
186  $last_name='';
187  $email_last_name = $last_name;
188  $email_prefix = $first_name;
189  } else {
190  $last_name = str_replace($first_name,'',$user_name);
191 
192  if ($this->option('email_format')=='filastname') {
193  $email_last_name.=str_replace(' ','',$last_name);
194  $email_prefix = $first_name[0].$email_last_name;
195 
196  } elseif ($this->option('email_format')=='firstname.lastname') {
197  $email_last_name.=str_replace(' ','',$last_name);
198  $email_prefix = $first_name.'.'.$email_last_name;
199 
200  } elseif ($this->option('email_format')=='firstname') {
201  $email_last_name.=str_replace(' ','',$last_name);
202  $email_prefix = $first_name;
203  }
204 
205 
206  }
207 
208 
209  $user_username = $email_prefix;
210 
211  // Generate an email based on their name if no email address is given
212  if ($user_email=='') {
213  if ($first_name=='Unknown') {
214  $status_id = 7;
215  }
216  $email = strtolower($email_prefix).'@'.$this->option('domain');
217  $user_email = str_replace("'",'',$email);
218  }
219  }
220 
221  $this->comment('Full Name: '.$user_name);
222  $this->comment('First Name: '.$first_name);
223  $this->comment('Last Name: '.$last_name);
224  $this->comment('Username: '.$user_username);
225  $this->comment('Email: '.$user_email);
226  $this->comment('License Name: '.$user_license_name);
227  $this->comment('Serial No: '.$user_license_serial);
228  $this->comment('Licensed To Name: '.$user_licensed_to_name);
229  $this->comment('Licensed To Email: '.$user_licensed_to_email);
230  $this->comment('Seats: '.$user_license_seats);
231  $this->comment('Reassignable: '.$user_license_reassignable);
232  $this->comment('Supplier: '.$user_license_supplier);
233  $this->comment('Maintained: '.$user_license_maintained);
234  $this->comment('Notes: '.$user_license_notes);
235  $this->comment('Purchase Date: '.$user_license_purchase_date);
236 
237  $this->comment('------------- Action Summary ----------------');
238 
239  if ($user_username!='') {
240  if ($user = User::where('username', $user_username)->whereNotNull('username')->first()) {
241  $this->comment('User '.$user_username.' already exists');
242  } else {
243  // Create the user
244  $user = Sentry::createUser(array(
245  'first_name' => $first_name,
246  'last_name' => $last_name,
247  'email' => $user_email,
248  'username' => $user_username,
249  'password' => substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 10),
250  'activated' => true,
251  'permissions' => array(
252  'admin' => 0,
253  'user' => 1,
254  ),
255  'notes' => 'User importerd through license importer'
256  ));
257 
258  // Find the group using the group id
259  $userGroup = Sentry::findGroupById(3);
260 
261  // Assign the group to the user
262  $user->addGroup($userGroup);
263  $this->comment('User '.$first_name.' created');
264  }
265  } else {
266  $user = new User;
267  $user->user_id = NULL;
268  }
269 
270 
271  // Check for the supplier match and create it if it doesn't exist
272  if ($supplier = Supplier::where('name', $user_license_supplier)->first()) {
273  $this->comment('Supplier '.$user_license_supplier.' already exists');
274  } else {
275  $supplier = new Supplier();
276  $supplier->name = e($user_license_supplier);
277  $supplier->user_id = 1;
278 
279  if ($supplier->save()) {
280  $this->comment('Supplier '.$user_license_supplier.' was created');
281  } else {
282  $this->comment('Something went wrong! Supplier '.$user_license_supplier.' was NOT created');
283  }
284 
285  }
286 
287 
288  // Add the license
289  $license = new License();
290  $license->name = e($user_license_name);
291  if ($user_license_purchase_date!='') {
292  $license->purchase_date = $user_license_purchase_date;
293  } else {
294  $license->purchase_date = NULL;
295  }
296  $license->serial = e($user_license_serial);
297  $license->seats = e($user_license_seats);
298  $license->supplier_id = $supplier->id;
299  $license->user_id = 1;
300  if ($user_license_purchase_date!='') {
301  $license->purchase_date = $user_license_purchase_date;
302  } else {
303  $license->purchase_date = NULL;
304  }
305  $license->license_name = $user_licensed_to_name;
306  $license->license_email = $user_licensed_to_email;
307  $license->notes = e($user_license_notes);
308 
309  if ($license->save()) {
310  $this->comment('License '.$user_license_name.' with serial number '.$user_license_serial.' was created');
311 
312 
313  $license_seat_created = 0;
314 
315  for ($x = 0; $x < $user_license_seats; $x++) {
316  // Create the license seat entries
317  $license_seat = new LicenseSeat();
318  $license_seat->license_id = $license->id;
319 
320  // Only assign the first seat to the user
321  if ($x==0) {
322  $license_seat->assigned_to = $user->id;
323  } else {
324  $license_seat->assigned_to = NULL;
325  }
326 
327  if ($license_seat->save()) {
328  $license_seat_created++;
329  }
330  }
331 
332  if ($license_seat_created > 0) {
333  $this->comment($license_seat_created.' seats were created');
334  } else {
335  $this->comment('Something went wrong! NO seats for '.$user_license_name.' were created');
336  }
337 
338 
339 
340  } else {
341  $this->comment('Something went wrong! License '.$user_license_name.' was NOT created');
342  }
343 
344 
345  $this->comment('=====================================');
346 
347  return true;
348 
349  });
350 
351 
352  }
353 
359  protected function getArguments()
360  {
361  return array(
362  array('filename', InputArgument::REQUIRED, 'File for the CSV import.'),
363  );
364  }
365 
366 
372  protected function getOptions()
373  {
374  return array(
375  array('domain', null, InputOption::VALUE_REQUIRED, 'Email domain for generated email addresses.', null),
376  array('email_format', null, InputOption::VALUE_REQUIRED, 'The format of the email addresses that should be generated. Options are firstname.lastname, firstname, filastname', null),
377  array('testrun', null, InputOption::VALUE_REQUIRED, 'Test the output without writing to the database or not.', null),
378  );
379  }
380 
381 
382 }
getArguments()
Get the console command arguments.
getOptions()
Get the console command options.
__construct()
Create a new command instance.