Skip to content

Commit 157a434

Browse files
authored
Merge pull request #13041 from snipe/features/refactor_importer_for_localization
Refactor importer for localization
2 parents f1bf726 + b57730e commit 157a434

20 files changed

+724
-1588
lines changed

app/Http/Livewire/Importer.php

Lines changed: 318 additions & 168 deletions
Large diffs are not rendered by default.

app/Importer/AssetImporter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,9 @@ public function createAssetIfNotExists(array $row)
132132
}
133133
}
134134

135-
// FIXME: this disables model validation. Need to find a way to avoid double-logs without breaking everything.
136-
// $asset->unsetEventDispatcher();
135+
137136
if ($asset->save()) {
138-
$asset->logCreate('Imported using csv importer');
137+
$asset->logCreate(trans('general.importer.import_note'));
139138
$this->log('Asset '.$this->item['name'].' with serial number '.$this->item['serial'].' was created');
140139

141140
// If we have a target to checkout to, lets do so.

app/Importer/Importer.php

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -27,60 +27,14 @@ abstract class Importer
2727
protected $updating;
2828
/**
2929
* Default Map of item fields->csv names
30+
*
31+
* This has been moved into Livewire/Importer.php to be more granular.
32+
* @todo - remove references to this property since we don't use it anymore.
33+
*
3034
* @var array
3135
*/
3236
private $defaultFieldMap = [
33-
'asset_tag' => 'asset tag',
34-
'activated' => 'activated',
35-
'category' => 'category',
36-
'checkout_class' => 'checkout type', // Supports Location or User for assets. Using checkout_class instead of checkout_type because type exists on asset already.
37-
'checkout_location' => 'checkout location',
38-
'company' => 'company',
39-
'item_name' => 'item name',
40-
'item_number' => 'item number',
41-
'image' => 'image',
42-
'expiration_date' => 'expiration date',
43-
'location' => 'location',
44-
'notes' => 'notes',
45-
'license_email' => 'licensed to email',
46-
'license_name' => 'licensed to name',
47-
'maintained' => 'maintained',
48-
'manufacturer' => 'manufacturer',
49-
'asset_model' => 'model name',
50-
'model_number' => 'model number',
51-
'order_number' => 'order number',
52-
'purchase_cost' => 'purchase cost',
53-
'purchase_date' => 'purchase date',
54-
'purchase_order' => 'purchase order',
55-
'qty' => 'quantity',
56-
'reassignable' => 'reassignable',
57-
'requestable' => 'requestable',
58-
'seats' => 'seats',
59-
'serial' => 'serial number',
60-
'status' => 'status',
61-
'supplier' => 'supplier',
62-
'termination_date' => 'termination date',
63-
'warranty_months' => 'warranty',
64-
'full_name' => 'full name',
65-
'email' => 'email',
66-
'username' => 'username',
67-
'address' => 'address',
68-
'address2' => 'address2',
69-
'city' => 'city',
70-
'state' => 'state',
71-
'country' => 'country',
72-
'zip' => 'zip',
73-
'jobtitle' => 'job title',
74-
'employee_num' => 'employee number',
75-
'phone_number' => 'phone number',
76-
'first_name' => 'first name',
77-
'last_name' => 'last name',
78-
'department' => 'department',
79-
'manager_name' => 'manager full name',
80-
'manager_username' => 'manager username',
81-
'min_amt' => 'minimum quantity',
82-
'remote' => 'remote',
83-
'vip' => 'vip',
37+
8438
];
8539
/**
8640
* Map of item fields->csv names
@@ -288,6 +242,8 @@ protected function createOrFetchUser($row, $type = 'user')
288242

289243
$user_array = [
290244
'full_name' => $this->findCsvMatch($row, 'full_name'),
245+
'first_name' => $this->findCsvMatch($row, 'first_name'),
246+
'last_name' => $this->findCsvMatch($row, 'last_name'),
291247
'email' => $this->findCsvMatch($row, 'email'),
292248
'manager_id'=> '',
293249
'department_id' => '',
@@ -296,7 +252,6 @@ protected function createOrFetchUser($row, $type = 'user')
296252
'remote' => $this->fetchHumanBoolean(($this->findCsvMatch($row, 'remote'))),
297253
];
298254

299-
300255
if ($type == 'manager') {
301256
$user_array['full_name'] = $this->findCsvMatch($row, 'manager');
302257
$user_array['username'] = $this->findCsvMatch($row, 'manager_username');
@@ -312,8 +267,9 @@ protected function createOrFetchUser($row, $type = 'user')
312267

313268

314269
// If the full name and username is empty, bail out--we need this to extract first name (at the very least)
315-
if ((empty($user_array['username'])) && (empty($user_array['full_name']))) {
316-
$this->log('Insufficient user data provided (Full name or username is required) - skipping user creation.');
270+
if ((empty($user_array['username'])) && (empty($user_array['full_name'])) && (empty($user_array['first_name']))) {
271+
$this->log('Insufficient user data provided (Full name, first name or username is required) - skipping user creation.');
272+
\Log::debug('User array: ');
317273
\Log::debug(print_r($user_array, true));
318274
\Log::debug(print_r($row, true));
319275
return false;
@@ -325,10 +281,12 @@ protected function createOrFetchUser($row, $type = 'user')
325281
$user_array['email'] = User::generateEmailFromFullName($user_array['full_name']);
326282
}
327283

328-
// Get some fields for first name and last name based off of full name
329-
$user_formatted_array = User::generateFormattedNameFromFullName($user_array['full_name'], Setting::getSettings()->username_format);
330-
$user_array['first_name'] = $user_formatted_array['first_name'];
331-
$user_array['last_name'] = $user_formatted_array['last_name'];
284+
if (empty($user_array['first_name'])) {
285+
// Get some fields for first name and last name based off of full name
286+
$user_formatted_array = User::generateFormattedNameFromFullName($user_array['full_name'], Setting::getSettings()->username_format);
287+
$user_array['first_name'] = $user_formatted_array['first_name'];
288+
$user_array['last_name'] = $user_formatted_array['last_name'];
289+
}
332290

333291
if (empty($user_array['username'])) {
334292
$user_array['username'] = $user_formatted_array['username'];

app/Importer/ItemImporter.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,11 @@ protected function handle($row)
112112
*/
113113
protected function determineCheckout($row)
114114
{
115+
// Locations don't get checked out to anyone/anything
115116
if (get_class($this) == LocationImporter::class) {
116117
return;
117118
}
118119

119-
// We only support checkout-to-location for asset, so short circuit otherwise.
120-
if (get_class($this) != AssetImporter::class) {
121-
return $this->createOrFetchUser($row);
122-
}
123-
124120
if (strtolower($this->item['checkout_class']) === 'location' && $this->findCsvMatch($row, 'checkout_location') != null ) {
125121
return Location::findOrFail($this->createOrFetchLocation($this->findCsvMatch($row, 'checkout_location')));
126122
}

app/Importer/UserImporter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@ public function createUserIfNotExists(array $row)
4646
$this->item['first_name'] = $this->findCsvMatch($row, 'first_name');
4747
$this->item['last_name'] = $this->findCsvMatch($row, 'last_name');
4848
$this->item['email'] = $this->findCsvMatch($row, 'email');
49+
$this->item['gravatar'] = $this->findCsvMatch($row, 'gravatar');
4950
$this->item['phone'] = $this->findCsvMatch($row, 'phone_number');
51+
$this->item['website'] = $this->findCsvMatch($row, 'website');
5052
$this->item['jobtitle'] = $this->findCsvMatch($row, 'jobtitle');
5153
$this->item['address'] = $this->findCsvMatch($row, 'address');
5254
$this->item['city'] = $this->findCsvMatch($row, 'city');
5355
$this->item['state'] = $this->findCsvMatch($row, 'state');
5456
$this->item['country'] = $this->findCsvMatch($row, 'country');
57+
$this->item['start_date'] = $this->findCsvMatch($row, 'start_date');
58+
$this->item['end_date'] = $this->findCsvMatch($row, 'end_date');
5559
$this->item['zip'] = $this->findCsvMatch($row, 'zip');
5660
$this->item['activated'] = ($this->fetchHumanBoolean($this->findCsvMatch($row, 'activated')) == 1) ? '1' : 0;
5761
$this->item['employee_num'] = $this->findCsvMatch($row, 'employee_num');

app/Importer/import_mappings.md

Lines changed: 0 additions & 45 deletions
This file was deleted.

resources/lang/en/general.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@
368368
'notification_warning' => 'Warning:',
369369
'notification_info' => 'Info:',
370370
'asset_information' => 'Asset Information',
371-
'model_name' => 'Model Name:',
372-
'asset_name' => 'Asset Name:',
371+
'model_name' => 'Model Name',
372+
'asset_name' => 'Asset Name',
373373
'consumable_information' => 'Consumable Information:',
374374
'consumable_name' => 'Consumable Name:',
375375
'accessory_information' => 'Accessory Information:',
@@ -448,5 +448,30 @@
448448
'modal_confirm_generic' => 'Are you sure?',
449449
'cannot_be_deleted' => 'This item cannot be deleted',
450450
'undeployable_tooltip' => 'This item cannot be checked out. Check the quantity remaining.',
451+
'serial_number' => 'Serial Number',
452+
'item_notes' => ':item Notes',
453+
'item_name_var' => ':item Name',
454+
'importer' => [
455+
'checked_out_to_fullname' => 'Checked Out to: Full Name',
456+
'checked_out_to_first_name' => 'Checked Out to: First Name',
457+
'checked_out_to_last_name' => 'Checked Out to: Last Name',
458+
'checked_out_to_username' => 'Checked Out to: Username',
459+
'checked_out_to_email' => 'Checked Out to: Email',
460+
'checked_out_to_tag' => 'Checked Out to: Asset Tag',
461+
'manager_first_name' => 'Manager First Name',
462+
'manager_last_name' => 'Manager First Name',
463+
'manager_full_name' => 'Manager Full Name',
464+
'manager_username' => 'Manager Username',
465+
'checkout_type' => 'Checkout Type',
466+
'checkout_location' => 'Checkout to Location',
467+
'image_filename' => 'Image Filename',
468+
'do_not_import' => 'Do Not Import',
469+
'vip' => 'VIP',
470+
'avatar' => 'Avatar',
471+
'gravatar' => 'Gravatar Email',
472+
'currency' => 'Currency',
473+
'address2' => 'Address Line 2',
474+
'import_note' => 'Imported using csv importer',
475+
],
451476

452477
];

resources/views/livewire/importer.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class="col-md-12 table table-striped snipe-table">
149149
'id' => 'import_type',
150150
'class' => 'livewire-select2',
151151
'style' => 'min-width: 350px',
152-
'data-placeholder' => trans('general.select_var', ['thing' => trans('general.import_type')]), /* TODO: translate me */
152+
'data-placeholder' => trans('general.select_var', ['thing' => trans('general.import_type')]),
153153
'placeholder' => '', //needed so that the form-helper will put an empty option first
154154
'data-minimum-results-for-search' => '-1', // Remove this if the list gets long enough that we need to search
155155
'data-livewire-component' => $_instance->id
@@ -223,7 +223,7 @@ class="col-md-12 table table-striped snipe-table">
223223
{{ Form::select('field_map.'.$index, $columnOptions[$activeFile->import_type], @$field_map[$index],
224224
[
225225
'class' => 'mappings livewire-select2',
226-
'placeholder' => 'Do Not Import',
226+
'placeholder' => trans('general.importer.do_not_import'),
227227
'style' => 'min-width: 100%',
228228
'data-livewire-component' => $_instance->id
229229
],[

sample_csvs/MOCK_ACCESSORIES.csv

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)