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 $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? 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 = [ protected $rules = [
'files.*.file_path' => 'required|string', 'files.*.file_path' => 'required|string',
'files.*.created_at' => 'required|string', 'files.*.created_at' => 'required|string',
@ -57,104 +67,239 @@ class Importer extends Component
return json_encode(array_filter($tmp)); 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 = [ private function getColumns($type)
'category' => 'Category', {
'company' => 'Company', switch ($type) {
'email' => 'Email', case 'asset':
'item_name' => 'Item Name', $results = $this->general_fields + $this->assets_fields;
'location' => 'Location', break;
'maintained' => 'Maintained', case 'accessory':
'manufacturer' => 'Manufacturer', $results = $this->general_fields + $this->accessories_fields;
'order_number' => 'Order Number', break;
'purchase_cost' => 'Purchase Cost', case 'consumable':
'purchase_date' => 'Purchase Date', $results = $this->general_fields + $this->consumables_fields;
'quantity' => 'Quantity', break;
'requestable' => 'Requestable', case 'license':
'serial' => 'Serial Number', $results = $this->general_fields + $this->licenses_fields;
'supplier' => 'Supplier', break;
'username' => 'Username', case 'user':
'department' => 'Department', $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 = [ // set the variables here so we can translate them!
'model_number' => 'Model Number', $this->general_fields = [
'notes' => 'Notes', 'company' => trans('general.company'),
'location' => trans('general.location'),
'quantity' => trans('general.qty'),
'department' => trans('general.department'),
]; ];
static $assets = [ $this->accessories_fields = [
'asset_tag' => 'Asset Tag', 'item_name' => 'Accessory Name',
'asset_model' => 'Model Name', 'model_number' => trans('general.model_number'),
'asset_notes' => 'Asset Notes', 'notes' => trans('general.notes'),
'model_notes' => 'Model Notes', 'category' => trans('general.category'),
'byod' => 'BYOD', 'supplier' => trans('general.supplier'),
'checkout_class' => 'Checkout Type', 'min_amt' => trans('general.min_amt'),
'checkout_location' => 'Checkout Location', 'purchase_cost' => trans('general.purchase_cost'),
'image' => 'Image Filename', 'purchase_date' => trans('general.purchase_date'),
'model_number' => 'Model Number', 'manufacturer' => trans('general.manufacturer'),
'full_name' => 'Full Name',
'status' => 'Status',
'warranty_months' => 'Warranty Months',
'asset_eol_date' => 'EOL Date',
]; ];
static $consumables = [ $this->assets_fields = [
'item_no' => "Item Number", 'item_name' => trans('general.name'),
'model_number' => "Model Number", 'asset_tag' => trans('general.asset_tag'),
'notes' => 'Notes', 'asset_model' => trans('general.model_name'),
'min_amt' => "Minimum Quantity", '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 = [ $this->consumables_fields = [
'asset_tag' => 'Assigned To Asset', 'item_name' => trans('general.item_name'),
'expiration_date' => 'Expiration Date', 'model_number' => trans('general.model_number'),
'full_name' => 'Full Name', 'notes' => trans('general.notes'),
'license_email' => 'Licensed To Email', 'min_amt' => trans('general.min_qty'),
'license_name' => 'Licensed To Name', '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', 'purchase_order' => 'Purchase Order',
'reassignable' => 'Reassignable', 'reassignable' => trans('general.reassignable'),
'seats' => 'Seats', 'seats' => trans('general.seats'),
'notes' => 'Notes', '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 = [ $this->users_fields = [
'employee_num' => 'Employee Number', 'first_name' => trans('general.first_name'),
'first_name' => 'First Name', 'last_name' => trans('general.last_name'),
'last_name' => 'Last Name', 'notes' => trans('general.notes'),
'notes' => 'Notes', 'username' => trans('general.username'),
'jobtitle' => 'Job Title', 'jobtitle' => trans('general.job_title'),
'phone_number' => 'Phone Number', 'phone_number' => trans('general.phone'),
'manager_first_name' => 'Manager First Name', 'manager_first_name' => trans('general.manager_first_name'),
'manager_last_name' => 'Manager Last Name', 'manager_last_name' => trans('general.manager_last_name'),
'activated' => 'Activated', 'activated' => trans('general.activated'),
'address' => 'Address', 'address' => trans('general.address'),
'city' => 'City', 'city' => trans('general.city'),
'state' => 'State', 'state' => trans('general.state'),
'country' => 'Country', 'country' => trans('general.country'),
'zip' => 'Zip', 'zip' => trans('general.zip'),
'vip' => 'VIP', 'vip' => trans('general.vip'),
'remote' => 'Remote', 'remote' => trans('general.remote'),
'email' => trans('general.email'),
'avatar' => trans('general.avatar'),
'gravatar' => trans('general.gravatar'),
'termination_date' => trans('general.termination_date'),
]; ];
static $locations = [ $this->locations_fields = [
'name' => 'Name', 'name' => trans('general.location_name'),
'address' => 'Address', 'address' => trans('general.address'),
'address2' => 'Address 2', 'address2' => trans('general.address2'),
'city' => 'City', 'city' => trans('general.city'),
'state' => 'State', 'state' => trans('general.state'),
'country' => 'Country', 'country' => trans('general.country'),
'zip' => 'Zip', 'zip' => trans('general.zip'),
'currency' => 'Currency', 'currency' => trans('general.currency'),
'ldap_ou' => 'LDAP OU', 'ldap_ou' => trans('general.ldap_ou'),
'manager_username' => 'Manager Username', 'manager_username' => trans('general.manager_username'),
'manager' => 'Manager', 'manager' => trans('general.manager'),
'parent_location' => 'Parent Location', 'parent_location' => trans('general.parent_location'),
'notes' => 'Notes',
]; ];
//array of "real fieldnames" to a list of aliases for that field // "real fieldnames" to a list of aliases for that field
static $aliases = [ $this->aliases_fields = [
'model_number' => 'model_number' =>
[ [
'model', 'model',
@ -213,110 +358,6 @@ class Importer extends Component
[ [
'Manager Username', '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 $this->columnOptions[''] = $this->getColumns(''); //blank mode? I don't know what this is supposed to mean