Refactored the static arrays into mount arrays for translations

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2023-05-16 22:31:58 -07:00
parent d228b7f347
commit 2c4c9a16c9

View file

@ -38,6 +38,16 @@ class Importer extends Component
public $field_map; // we need a separate variable for the field-mapping, because the keys in the normal array are too complicated for Livewire to understand
public $file_id; // TODO: I can't figure out *why* we need this, but it really seems like we do. I can't seem to pull the id from the activeFile for some reason?
// Make these variables public - we set the properties in the constructor so we can localize them (versus the old static arrays)
public $general_fields;
public $accessories_fields;
public $users_fields;
public $licenses_fields;
public $locations_fields;
public $consumables_fields;
public $components_fields;
public $aliases_fields;
protected $rules = [
'files.*.file_path' => 'required|string',
'files.*.created_at' => 'required|string',
@ -57,104 +67,239 @@ class Importer extends Component
return json_encode(array_filter($tmp));
}
// all of these 'statics', alas, may have to change to something else to handle translations?
// I'm not sure. Maybe I use them to 'populate' the translations? TBH, I don't know yet.
static $general = [
'category' => 'Category',
'company' => 'Company',
'email' => 'Email',
'item_name' => 'Item Name',
'location' => 'Location',
'maintained' => 'Maintained',
'manufacturer' => 'Manufacturer',
'order_number' => 'Order Number',
'purchase_cost' => 'Purchase Cost',
'purchase_date' => 'Purchase Date',
'quantity' => 'Quantity',
'requestable' => 'Requestable',
'serial' => 'Serial Number',
'supplier' => 'Supplier',
'username' => 'Username',
'department' => 'Department',
private function getColumns($type)
{
switch ($type) {
case 'asset':
$results = $this->general_fields + $this->assets_fields;
break;
case 'accessory':
$results = $this->general_fields + $this->accessories_fields;
break;
case 'consumable':
$results = $this->general_fields + $this->consumables_fields;
break;
case 'license':
$results = $this->general_fields + $this->licenses_fields;
break;
case 'user':
$results = $this->general_fields + $this->users_fields;
break;
case 'location':
$results = $this->general_fields + $this->locations_fields;
break;
default:
$results = $this->general_fields;
}
asort($results, SORT_FLAG_CASE | SORT_STRING);
if ($type == "asset") {
// add Custom Fields after a horizontal line
$results['-'] = "———" . trans('admin/custom_fields/general.custom_fields') . "———’";
foreach (CustomField::orderBy('name')->get() as $field) {
$results[$field->db_column_name()] = $field->name;
}
}
return $results;
}
public function updating($name, $new_import_type)
{
if ($name == "activeFile.import_type") {
\Log::debug("WE ARE CHANGING THE import_type!!!!! TO: " . $new_import_type);
\Log::debug("so, what's \$this->>field_map at?: " . print_r($this->field_map, true));
// go through each header, find a matching field to try and map it to.
foreach ($this->activeFile->header_row as $i => $header) {
// do we have something mapped already?
if (array_key_exists($i, $this->field_map)) {
// yes, we do. Is it valid for this type of import?
// (e.g. the import type might have been changed...?)
if (array_key_exists($this->field_map[$i], $this->columnOptions[$new_import_type])) {
//yes, this key *is* valid. Continue on to the next field.
continue;
} else {
//no, this key is *INVALID* for this import type. Better set it to null
// and we'll hope that the $aliases_fields or something else picks it up.
$this->field_map[$i] = null; // fingers crossed! But it's not likely, tbh.
} // TODO - strictly speaking, this isn't necessary here I don't think.
}
// first, check for exact matches
foreach ($this->columnOptions[$new_import_type] as $value => $text) {
if (strcasecmp($text, $header) === 0) { // case-INSENSITIVe on purpose!
$this->field_map[$i] = $value;
continue 2; //don't bother with the alias check, go to the next header
}
}
// if you got here, we didn't find a match. Try the $aliases_fields
foreach ($this->aliases_fields as $key => $alias_values) {
foreach ($alias_values as $alias_value) {
if (strcasecmp($alias_value, $header) === 0) { // aLsO CaSe-INSENSitiVE!
// Make *absolutely* sure that this key actually _exists_ in this import type -
// you can trigger this by importing accessories with a 'Warranty' column (which don't exist
// in "Accessories"!)
if (array_key_exists($key, $this->columnOptions[$new_import_type])) {
$this->field_map[$i] = $key;
continue 3; // bust out of both of these loops; as well as the surrounding one - e.g. move on to the next header
}
}
}
}
// and if you got here, we got nothing. Let's recommend 'null'
$this->field_map[$i] = null; // Booooo :(
}
}
}
public function boot() { // FIXME - delete or undelete.
///////$this->activeFile = null; // I do *not* understand why I have to do this, but, well, whatever.
}
public function mount()
{
$this->authorize('import');
$this->progress = -1; // '-1' means 'don't show the progressbar'
$this->progress_bar_class = 'progress-bar-warning';
$this->importTypes = [
'asset' => trans('general.assets'),
'accessory' => trans('general.accessories'),
'consumable' => trans('general.consumables'),
'component' => trans('general.components'),
'license' => trans('general.licenses'),
'user' => trans('general.users'),
'location' => trans('general.locations'),
];
static $accessories = [
'model_number' => 'Model Number',
'notes' => 'Notes',
// set the variables here so we can translate them!
$this->general_fields = [
'company' => trans('general.company'),
'location' => trans('general.location'),
'quantity' => trans('general.qty'),
'department' => trans('general.department'),
];
static $assets = [
'asset_tag' => 'Asset Tag',
'asset_model' => 'Model Name',
'asset_notes' => 'Asset Notes',
'model_notes' => 'Model Notes',
'byod' => 'BYOD',
'checkout_class' => 'Checkout Type',
'checkout_location' => 'Checkout Location',
'image' => 'Image Filename',
'model_number' => 'Model Number',
'full_name' => 'Full Name',
'status' => 'Status',
'warranty_months' => 'Warranty Months',
'asset_eol_date' => 'EOL Date',
$this->accessories_fields = [
'item_name' => 'Accessory Name',
'model_number' => trans('general.model_number'),
'notes' => trans('general.notes'),
'category' => trans('general.category'),
'supplier' => trans('general.supplier'),
'min_amt' => trans('general.min_amt'),
'purchase_cost' => trans('general.purchase_cost'),
'purchase_date' => trans('general.purchase_date'),
'manufacturer' => trans('general.manufacturer'),
];
static $consumables = [
'item_no' => "Item Number",
'model_number' => "Model Number",
'notes' => 'Notes',
'min_amt' => "Minimum Quantity",
$this->assets_fields = [
'item_name' => trans('general.name'),
'asset_tag' => trans('general.asset_tag'),
'asset_model' => trans('general.model_name'),
'asset_notes' => trans('general.asset_notes'),
'model_notes' => trans('general.model_notes'),
'byod' => trans('general.byod'),
'checkout_class' => trans('general.checkout_type'),
'checkout_location' => trans('general.checkout_location'),
'image' => trans('general.image_filename'),
'model_number' => trans('general.model_number'),
'email' => trans('general.checked_out_email'),
'full_name' => trans('general.checked_out_fullname'),
'status' => trans('general.status'),
'warranty_months' => trans('general.warranty_months'),
'category' => trans('general.category'),
'reassignable' => trans('general.reassignable'),
'requestable' => trans('general.requestable'),
'serial' => trans('general.serial'),
'supplier' => trans('general.supplier'),
'purchase_cost' => trans('general.purchase_cost'),
'purchase_date' => trans('general.purchase_date'),
'purchase_order' => trans('general.purchase_order'),
'notes' => trans('general.notes'),
'manufacturer' => trans('general.manufacturer'),
];
static $licenses = [
'asset_tag' => 'Assigned To Asset',
'expiration_date' => 'Expiration Date',
'full_name' => 'Full Name',
'license_email' => 'Licensed To Email',
'license_name' => 'Licensed To Name',
$this->consumables_fields = [
'item_name' => trans('general.item_name'),
'model_number' => trans('general.model_number'),
'notes' => trans('general.notes'),
'min_amt' => trans('general.min_qty'),
'category' => trans('general.category'),
'purchase_cost' => trans('general.purchase_cost'),
'purchase_date' => trans('general.purchase_date'),
'checkout_class' => trans('general.checkout_type'),
'supplier' => trans('general.supplier'),
];
$this->components_fields = [
'item_name' => trans('general.components_name'),
'model_number' => trans('general.model_number'),
'notes' => trans('general.notes'),
'category' => trans('general.category'),
'supplier' => trans('general.supplier'),
'min_amt' => trans('general.min_amt'),
'purchase_cost' => trans('general.purchase_cost'),
'purchase_date' => trans('general.purchase_date'),
];
$this->licenses_fields = [
'item_name' => 'License Name',
'asset_tag' => trans('general.assigned_to_tag'),
'expiration_date' => trans('general.expiration_name'),
'full_name' => trans('general.full_name'),
'license_email' => trans('general.license_email'),
'license_name' => trans('general.license_to_name'),
'purchase_order' => 'Purchase Order',
'reassignable' => 'Reassignable',
'seats' => 'Seats',
'notes' => 'Notes',
'reassignable' => trans('general.reassignable'),
'seats' => trans('general.seats'),
'notes' => trans('general.notes'),
'category' => trans('general.category'),
'supplier' => trans('general.supplier'),
'purchase_cost' => trans('general.purchase_cost'),
'purchase_date' => trans('general.purchase_date'),
'maintained' => trans('general.maintained'),
'checkout_class' => trans('general.checkout_type'),
];
static $users = [
'employee_num' => 'Employee Number',
'first_name' => 'First Name',
'last_name' => 'Last Name',
'notes' => 'Notes',
'jobtitle' => 'Job Title',
'phone_number' => 'Phone Number',
'manager_first_name' => 'Manager First Name',
'manager_last_name' => 'Manager Last Name',
'activated' => 'Activated',
'address' => 'Address',
'city' => 'City',
'state' => 'State',
'country' => 'Country',
'zip' => 'Zip',
'vip' => 'VIP',
'remote' => 'Remote',
$this->users_fields = [
'first_name' => trans('general.first_name'),
'last_name' => trans('general.last_name'),
'notes' => trans('general.notes'),
'username' => trans('general.username'),
'jobtitle' => trans('general.job_title'),
'phone_number' => trans('general.phone'),
'manager_first_name' => trans('general.manager_first_name'),
'manager_last_name' => trans('general.manager_last_name'),
'activated' => trans('general.activated'),
'address' => trans('general.address'),
'city' => trans('general.city'),
'state' => trans('general.state'),
'country' => trans('general.country'),
'zip' => trans('general.zip'),
'vip' => trans('general.vip'),
'remote' => trans('general.remote'),
'email' => trans('general.email'),
'avatar' => trans('general.avatar'),
'gravatar' => trans('general.gravatar'),
'termination_date' => trans('general.termination_date'),
];
static $locations = [
'name' => 'Name',
'address' => 'Address',
'address2' => 'Address 2',
'city' => 'City',
'state' => 'State',
'country' => 'Country',
'zip' => 'Zip',
'currency' => 'Currency',
'ldap_ou' => 'LDAP OU',
'manager_username' => 'Manager Username',
'manager' => 'Manager',
'parent_location' => 'Parent Location',
'notes' => 'Notes',
$this->locations_fields = [
'name' => trans('general.location_name'),
'address' => trans('general.address'),
'address2' => trans('general.address2'),
'city' => trans('general.city'),
'state' => trans('general.state'),
'country' => trans('general.country'),
'zip' => trans('general.zip'),
'currency' => trans('general.currency'),
'ldap_ou' => trans('general.ldap_ou'),
'manager_username' => trans('general.manager_username'),
'manager' => trans('general.manager'),
'parent_location' => trans('general.parent_location'),
];
//array of "real fieldnames" to a list of aliases for that field
static $aliases = [
// "real fieldnames" to a list of aliases for that field
$this->aliases_fields = [
'model_number' =>
[
'model',
@ -213,110 +358,6 @@ class Importer extends Component
[
'Manager Username',
],
];
private function getColumns($type)
{
switch ($type) {
case 'asset':
$results = self::$general + self::$assets;
break;
case 'accessory':
$results = self::$general + self::$accessories;
break;
case 'consumable':
$results = self::$general + self::$consumables;
break;
case 'license':
$results = self::$general + self::$licenses;
break;
case 'user':
$results = self::$general + self::$users;
break;
case 'location':
$results = self::$general + self::$locations;
break;
default:
$results = self::$general;
}
asort($results, SORT_FLAG_CASE | SORT_STRING);
if ($type == "asset") {
// add Custom Fields after a horizontal line
$results['-'] = "———" . trans('admin/custom_fields/general.custom_fields') . "———’";
foreach (CustomField::orderBy('name')->get() as $field) {
$results[$field->db_column_name()] = $field->name;
}
}
return $results;
}
public function updating($name, $new_import_type)
{
if ($name == "activeFile.import_type") {
\Log::debug("WE ARE CHANGING THE import_type!!!!! TO: " . $new_import_type);
\Log::debug("so, what's \$this->>field_map at?: " . print_r($this->field_map, true));
// go through each header, find a matching field to try and map it to.
foreach ($this->activeFile->header_row as $i => $header) {
// do we have something mapped already?
if (array_key_exists($i, $this->field_map)) {
// yes, we do. Is it valid for this type of import?
// (e.g. the import type might have been changed...?)
if (array_key_exists($this->field_map[$i], $this->columnOptions[$new_import_type])) {
//yes, this key *is* valid. Continue on to the next field.
continue;
} else {
//no, this key is *INVALID* for this import type. Better set it to null
// and we'll hope that the aliases or something else picks it up.
$this->field_map[$i] = null; // fingers crossed! But it's not likely, tbh.
} // TODO - strictly speaking, this isn't necessary here I don't think.
}
// first, check for exact matches
foreach ($this->columnOptions[$new_import_type] as $value => $text) {
if (strcasecmp($text, $header) === 0) { // case-INSENSITIVe on purpose!
$this->field_map[$i] = $value;
continue 2; //don't bother with the alias check, go to the next header
}
}
// if you got here, we didn't find a match. Try the aliases
foreach (self::$aliases as $key => $alias_values) {
foreach ($alias_values as $alias_value) {
if (strcasecmp($alias_value, $header) === 0) { // aLsO CaSe-INSENSitiVE!
// Make *absolutely* sure that this key actually _exists_ in this import type -
// you can trigger this by importing accessories with a 'Warranty' column (which don't exist
// in "Accessories"!)
if (array_key_exists($key, $this->columnOptions[$new_import_type])) {
$this->field_map[$i] = $key;
continue 3; // bust out of both of these loops; as well as the surrounding one - e.g. move on to the next header
}
}
}
}
// and if you got here, we got nothing. Let's recommend 'null'
$this->field_map[$i] = null; // Booooo :(
}
}
}
public function boot() { // FIXME - delete or undelete.
///////$this->activeFile = null; // I do *not* understand why I have to do this, but, well, whatever.
}
public function mount()
{
$this->authorize('import');
$this->progress = -1; // '-1' means 'don't show the progressbar'
$this->progress_bar_class = 'progress-bar-warning';
$this->importTypes = [
'asset' => trans('general.assets'),
'accessory' => trans('general.accessories'),
'consumable' => trans('general.consumables'),
'component' => trans('general.components'),
'license' => trans('general.licenses'),
'user' => trans('general.users'),
'location' => trans('general.locations'),
];
$this->columnOptions[''] = $this->getColumns(''); //blank mode? I don't know what this is supposed to mean