2022-01-13 01:19:13 -08:00
< ? php
2024-05-29 12:07:48 -07:00
namespace App\Livewire ;
2022-01-13 01:19:13 -08:00
2023-03-18 20:31:56 -07:00
use App\Models\CustomField ;
2022-01-13 01:19:13 -08:00
use App\Models\Import ;
2024-05-29 04:53:51 -07:00
use Illuminate\Support\Facades\Storage ;
2024-07-16 16:08:42 -07:00
use Livewire\Attributes\Computed ;
2024-07-11 13:32:16 -07:00
use Livewire\Component ;
2022-01-13 01:19:13 -08:00
class Importer extends Component
{
2024-07-15 16:15:58 -07:00
public $progress = - 1 ; //upload progress - '-1' means don't show
2023-03-21 22:26:32 -07:00
public $progress_message ;
2024-07-15 16:15:58 -07:00
public $progress_bar_class = 'progress-bar-warning' ;
2022-09-19 21:04:46 -07:00
public $message ; //status/error message?
public $message_type ; //success/error?
2023-03-18 20:31:56 -07:00
//originally from ImporterFile
2022-12-14 10:55:50 -08:00
public $import_errors ; //
2024-07-16 17:03:42 -07:00
public $activeFileId ;
2024-07-16 13:30:29 -07:00
public $headerRow = [];
2024-07-16 14:20:41 -07:00
public $typeOfImport ;
2023-03-18 20:31:56 -07:00
public $importTypes ;
public $columnOptions ;
public $statusType ;
public $statusText ;
public $update ;
public $send_welcome ;
public $run_backup ;
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
2022-12-14 10:55:50 -08:00
2023-05-16 22:31:58 -07:00
// Make these variables public - we set the properties in the constructor so we can localize them (versus the old static arrays)
public $accessories_fields ;
2023-06-07 07:52:18 -07:00
public $assets_fields ;
2023-05-16 22:31:58 -07:00
public $users_fields ;
public $licenses_fields ;
public $locations_fields ;
public $consumables_fields ;
public $components_fields ;
public $aliases_fields ;
2022-01-13 01:19:13 -08:00
protected $rules = [
'files.*.file_path' => 'required|string' ,
'files.*.created_at' => 'required|string' ,
2023-03-18 20:31:56 -07:00
'files.*.filesize' => 'required|integer' ,
2024-07-16 13:30:29 -07:00
'headerRow' => 'array' ,
2024-07-16 14:20:41 -07:00
'typeOfImport' => 'string' ,
2023-03-18 20:31:56 -07:00
'field_map' => 'array'
2022-01-13 01:19:13 -08:00
];
2024-02-22 10:28:01 -08:00
/**
2024-05-29 12:07:48 -07:00
* This is used in resources / views / livewire . importer . blade . php , and we kinda shouldn ' t need to check for
2024-02-22 10:28:01 -08:00
* activeFile here , but there ' s some UI goofiness that allows this to crash out on some imports .
*
2024-02-22 10:30:23 -08:00
* @ return string
2024-02-22 10:28:01 -08:00
*/
2023-03-18 20:31:56 -07:00
public function generate_field_map ()
{
2024-02-22 10:28:01 -08:00
$tmp = array ();
if ( $this -> activeFile ) {
2024-07-16 13:30:29 -07:00
$tmp = array_combine ( $this -> headerRow , $this -> field_map );
2024-02-22 10:31:02 -08:00
$tmp = array_filter ( $tmp );
2024-02-22 10:28:01 -08:00
}
return json_encode ( $tmp );
2023-03-18 20:31:56 -07:00
}
private function getColumns ( $type )
{
2023-03-21 22:26:32 -07:00
switch ( $type ) {
2023-03-18 20:31:56 -07:00
case 'asset' :
2023-05-23 20:31:53 -07:00
$results = $this -> assets_fields ;
2023-03-18 20:31:56 -07:00
break ;
case 'accessory' :
2023-05-23 20:31:53 -07:00
$results = $this -> accessories_fields ;
2023-03-18 20:31:56 -07:00
break ;
case 'consumable' :
2023-05-23 20:31:53 -07:00
$results = $this -> consumables_fields ;
2023-03-18 20:31:56 -07:00
break ;
2023-05-20 14:20:49 -07:00
case 'component' :
2023-05-23 20:31:53 -07:00
$results = $this -> components_fields ;
2023-05-20 14:20:49 -07:00
break ;
2023-03-18 20:31:56 -07:00
case 'license' :
2023-05-23 20:31:53 -07:00
$results = $this -> licenses_fields ;
2023-03-18 20:31:56 -07:00
break ;
case 'user' :
2023-05-23 20:31:53 -07:00
$results = $this -> users_fields ;
2023-03-18 20:31:56 -07:00
break ;
2023-04-16 07:47:42 -07:00
case 'location' :
2023-05-23 20:31:53 -07:00
$results = $this -> locations_fields ;
2023-04-16 07:47:42 -07:00
break ;
2023-03-18 20:31:56 -07:00
default :
2023-05-23 20:31:53 -07:00
$results = [];
2023-03-18 20:31:56 -07:00
}
2023-03-21 22:26:32 -07:00
asort ( $results , SORT_FLAG_CASE | SORT_STRING );
if ( $type == " asset " ) {
2023-03-18 20:31:56 -07:00
// add Custom Fields after a horizontal line
2023-03-21 22:26:32 -07:00
$results [ '-' ] = " ——— " . trans ( 'admin/custom_fields/general.custom_fields' ) . " ———’ " ;
foreach ( CustomField :: orderBy ( 'name' ) -> get () as $field ) {
2023-03-18 20:31:56 -07:00
$results [ $field -> db_column_name ()] = $field -> name ;
}
}
return $results ;
}
2024-07-16 14:20:41 -07:00
public function updatingTypeOfImport ( $type )
2023-03-18 20:31:56 -07:00
{
2024-07-16 14:20:41 -07:00
// go through each header, find a matching field to try and map it to.
foreach ( $this -> headerRow 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 [ $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 [ $type ] as $v => $text ) {
if ( strcasecmp ( $text , $header ) === 0 ) { // case-INSENSITIVe on purpose!
$this -> field_map [ $i ] = $v ;
continue 2 ; //don't bother with the alias check, go to the next header
2023-03-18 20:31:56 -07:00
}
2024-07-16 14:20:41 -07:00
}
// 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 [ $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
2023-03-18 20:31:56 -07:00
}
}
}
}
2024-07-16 14:20:41 -07:00
// and if you got here, we got nothing. Let's recommend 'null'
$this -> field_map [ $i ] = null ; // Booooo :(
2023-03-18 20:31:56 -07:00
}
}
2022-01-13 01:19:13 -08:00
public function mount ()
{
2023-02-28 21:58:02 -08:00
$this -> authorize ( 'import' );
2023-03-18 20:31:56 -07:00
$this -> importTypes = [
2024-07-16 17:03:42 -07:00
'asset' => trans ( 'general.assets' ),
'accessory' => trans ( 'general.accessories' ),
2023-03-18 20:31:56 -07:00
'consumable' => trans ( 'general.consumables' ),
2024-07-16 17:03:42 -07:00
'component' => trans ( 'general.components' ),
'license' => trans ( 'general.licenses' ),
'user' => trans ( 'general.users' ),
'location' => trans ( 'general.locations' ),
2023-03-18 20:31:56 -07:00
];
2023-05-23 20:31:53 -07:00
/**
* These are the item - type specific columns
*/
2024-07-16 17:03:42 -07:00
$this -> accessories_fields = [
2023-05-16 22:31:58 -07:00
'company' => trans ( 'general.company' ),
'location' => trans ( 'general.location' ),
'quantity' => trans ( 'general.qty' ),
2023-05-20 14:21:13 -07:00
'item_name' => trans ( 'general.item_name_var' , [ 'item' => trans ( 'general.accessory' )]),
'model_number' => trans ( 'general.model_no' ),
2023-05-16 22:31:58 -07:00
'notes' => trans ( 'general.notes' ),
'category' => trans ( 'general.category' ),
'supplier' => trans ( 'general.supplier' ),
2023-05-20 14:21:13 -07:00
'min_amt' => trans ( 'mail.min_QTY' ),
2023-05-16 22:31:58 -07:00
'purchase_cost' => trans ( 'general.purchase_cost' ),
'purchase_date' => trans ( 'general.purchase_date' ),
'manufacturer' => trans ( 'general.manufacturer' ),
2023-05-23 12:13:06 -07:00
'order_number' => trans ( 'general.order_number' ),
2023-05-16 22:31:58 -07:00
];
$this -> assets_fields = [
2023-05-23 20:31:53 -07:00
'company' => trans ( 'general.company' ),
'location' => trans ( 'general.location' ),
2023-05-20 14:21:13 -07:00
'item_name' => trans ( 'general.item_name_var' , [ 'item' => trans ( 'general.asset' )]),
2023-05-16 22:31:58 -07:00
'asset_tag' => trans ( 'general.asset_tag' ),
'asset_model' => trans ( 'general.model_name' ),
'byod' => trans ( 'general.byod' ),
2023-05-20 14:21:13 -07:00
'model_number' => trans ( 'general.model_no' ),
2023-05-16 22:31:58 -07:00
'status' => trans ( 'general.status' ),
2023-05-20 14:21:13 -07:00
'warranty_months' => trans ( 'admin/hardware/form.warranty' ),
2023-05-16 22:31:58 -07:00
'category' => trans ( 'general.category' ),
2023-05-20 14:21:13 -07:00
'requestable' => trans ( 'admin/hardware/general.requestable' ),
'serial' => trans ( 'general.serial_number' ),
2023-05-16 22:31:58 -07:00
'supplier' => trans ( 'general.supplier' ),
'purchase_cost' => trans ( 'general.purchase_cost' ),
'purchase_date' => trans ( 'general.purchase_date' ),
2023-05-20 14:21:13 -07:00
'purchase_order' => trans ( 'admin/licenses/form.purchase_order' ),
'asset_notes' => trans ( 'general.item_notes' , [ 'item' => trans ( 'admin/hardware/general.asset' )]),
'model_notes' => trans ( 'general.item_notes' , [ 'item' => trans ( 'admin/hardware/form.model' )]),
2023-05-16 22:31:58 -07:00
'manufacturer' => trans ( 'general.manufacturer' ),
2023-05-23 12:13:06 -07:00
'order_number' => trans ( 'general.order_number' ),
2023-05-23 12:57:34 -07:00
'image' => trans ( 'general.importer.image_filename' ),
2023-08-08 09:45:33 -07:00
'asset_eol_date' => trans ( 'admin/hardware/form.eol_date' ),
2023-05-23 19:20:52 -07:00
/**
* Checkout fields :
* Assets can be checked out to other assets , people , or locations , but we currently
* only support checkout to people and locations in the importer
**/
2023-05-23 12:57:34 -07:00
'checkout_class' => trans ( 'general.importer.checkout_type' ),
'first_name' => trans ( 'general.importer.checked_out_to_first_name' ),
'last_name' => trans ( 'general.importer.checked_out_to_last_name' ),
'full_name' => trans ( 'general.importer.checked_out_to_fullname' ),
'email' => trans ( 'general.importer.checked_out_to_email' ),
'username' => trans ( 'general.importer.checked_out_to_username' ),
'checkout_location' => trans ( 'general.importer.checkout_location' ),
2024-05-30 16:13:20 -07:00
/**
* These are here so users can import history , to replace the dinosaur that
* was the history importer
*/
'last_checkin' => trans ( 'admin/hardware/table.last_checkin_date' ),
'last_checkout' => trans ( 'admin/hardware/table.checkout_date' ),
'expected_checkin' => trans ( 'admin/hardware/form.expected_checkin' ),
'last_audit_date' => trans ( 'general.last_audit' ),
'next_audit_date' => trans ( 'general.next_audit_date' ),
2023-05-16 22:31:58 -07:00
];
$this -> consumables_fields = [
2023-05-23 20:31:53 -07:00
'company' => trans ( 'general.company' ),
'location' => trans ( 'general.location' ),
'quantity' => trans ( 'general.qty' ),
2023-05-20 14:21:13 -07:00
'item_name' => trans ( 'general.item_name_var' , [ 'item' => trans ( 'general.consumable' )]),
'model_number' => trans ( 'general.model_no' ),
2023-05-16 22:31:58 -07:00
'notes' => trans ( 'general.notes' ),
2023-05-20 14:21:13 -07:00
'min_amt' => trans ( 'mail.min_QTY' ),
2023-05-16 22:31:58 -07:00
'category' => trans ( 'general.category' ),
'purchase_cost' => trans ( 'general.purchase_cost' ),
'purchase_date' => trans ( 'general.purchase_date' ),
2023-05-20 14:21:13 -07:00
'checkout_class' => trans ( 'general.importer.checkout_type' ),
2023-05-16 22:31:58 -07:00
'supplier' => trans ( 'general.supplier' ),
2023-05-23 12:13:06 -07:00
'manufacturer' => trans ( 'general.manufacturer' ),
'order_number' => trans ( 'general.order_number' ),
'item_no' => trans ( 'admin/consumables/general.item_no' ),
2023-05-16 22:31:58 -07:00
];
$this -> components_fields = [
2023-05-23 20:31:53 -07:00
'company' => trans ( 'general.company' ),
'location' => trans ( 'general.location' ),
'quantity' => trans ( 'general.qty' ),
2023-05-20 14:21:13 -07:00
'item_name' => trans ( 'general.item_name_var' , [ 'item' => trans ( 'general.component' )]),
'model_number' => trans ( 'general.model_no' ),
2023-05-16 22:31:58 -07:00
'notes' => trans ( 'general.notes' ),
'category' => trans ( 'general.category' ),
'supplier' => trans ( 'general.supplier' ),
2023-05-20 14:21:13 -07:00
'min_amt' => trans ( 'mail.min_QTY' ),
2023-05-16 22:31:58 -07:00
'purchase_cost' => trans ( 'general.purchase_cost' ),
'purchase_date' => trans ( 'general.purchase_date' ),
2023-05-23 12:13:06 -07:00
'manufacturer' => trans ( 'general.manufacturer' ),
'order_number' => trans ( 'general.order_number' ),
'serial' => trans ( 'general.serial_number' ),
2023-05-16 22:31:58 -07:00
];
$this -> licenses_fields = [
2023-05-23 20:31:53 -07:00
'company' => trans ( 'general.company' ),
'location' => trans ( 'general.location' ),
2023-05-20 14:21:13 -07:00
'item_name' => trans ( 'general.item_name_var' , [ 'item' => trans ( 'general.license' )]),
2023-06-01 11:51:06 -07:00
'asset_tag' => trans ( 'general.importer.checked_out_to_tag' ),
'expiration_date' => trans ( 'admin/licenses/form.expiration' ),
'full_name' => trans ( 'general.importer.checked_out_to_fullname' ),
'license_email' => trans ( 'admin/licenses/form.to_email' ),
'license_name' => trans ( 'admin/licenses/form.to_name' ),
'purchase_order' => trans ( 'admin/licenses/form.purchase_order' ),
2023-09-28 06:24:07 -07:00
'order_number' => trans ( 'general.order_number' ),
2023-05-20 14:21:13 -07:00
'reassignable' => trans ( 'admin/licenses/form.reassignable' ),
2023-06-01 11:51:06 -07:00
'seats' => trans ( 'admin/licenses/form.seats' ),
2023-05-16 22:31:58 -07:00
'notes' => trans ( 'general.notes' ),
'category' => trans ( 'general.category' ),
'supplier' => trans ( 'general.supplier' ),
'purchase_cost' => trans ( 'general.purchase_cost' ),
'purchase_date' => trans ( 'general.purchase_date' ),
2023-06-01 11:51:06 -07:00
'maintained' => trans ( 'admin/licenses/form.maintained' ),
2023-05-20 14:21:13 -07:00
'checkout_class' => trans ( 'general.importer.checkout_type' ),
'serial' => trans ( 'general.license_serial' ),
2023-07-06 17:36:13 -07:00
'email' => trans ( 'general.importer.checked_out_to_email' ),
'username' => trans ( 'general.importer.checked_out_to_username' ),
2023-07-20 05:48:51 -07:00
'manufacturer' => trans ( 'general.manufacturer' ),
2023-05-16 22:31:58 -07:00
];
2024-07-16 17:03:42 -07:00
$this -> users_fields = [
2023-06-15 02:02:05 -07:00
'id' => trans ( 'general.id' ),
2023-05-23 20:31:53 -07:00
'company' => trans ( 'general.company' ),
'location' => trans ( 'general.location' ),
'department' => trans ( 'general.department' ),
2023-05-16 22:31:58 -07:00
'first_name' => trans ( 'general.first_name' ),
'last_name' => trans ( 'general.last_name' ),
'notes' => trans ( 'general.notes' ),
2023-05-20 14:52:42 -07:00
'username' => trans ( 'admin/users/table.username' ),
'jobtitle' => trans ( 'admin/users/table.title' ),
'phone_number' => trans ( 'admin/users/table.phone' ),
2023-05-20 14:21:13 -07:00
'manager_first_name' => trans ( 'general.importer.manager_first_name' ),
'manager_last_name' => trans ( 'general.importer.manager_last_name' ),
2023-05-16 22:31:58 -07:00
'activated' => trans ( 'general.activated' ),
'address' => trans ( 'general.address' ),
'city' => trans ( 'general.city' ),
'state' => trans ( 'general.state' ),
'country' => trans ( 'general.country' ),
'zip' => trans ( 'general.zip' ),
2023-05-20 14:52:42 -07:00
'vip' => trans ( 'general.importer.vip' ),
'remote' => trans ( 'admin/users/general.remote' ),
'email' => trans ( 'admin/users/table.email' ),
2023-05-22 13:20:08 -07:00
'website' => trans ( 'general.website' ),
2023-05-20 14:52:42 -07:00
'avatar' => trans ( 'general.image' ),
2023-05-22 13:20:08 -07:00
'gravatar' => trans ( 'general.importer.gravatar' ),
2024-07-16 17:03:42 -07:00
'start_date' => trans ( 'general.start_date' ),
'end_date' => trans ( 'general.end_date' ),
'employee_num' => trans ( 'general.employee_number' ),
2023-05-16 22:31:58 -07:00
];
2024-07-16 17:03:42 -07:00
$this -> locations_fields = [
2023-05-20 14:52:42 -07:00
'name' => trans ( 'general.item_name_var' , [ 'item' => trans ( 'general.location' )]),
2023-05-16 22:31:58 -07:00
'address' => trans ( 'general.address' ),
2023-05-20 14:52:42 -07:00
'address2' => trans ( 'general.importer.address2' ),
2023-05-16 22:31:58 -07:00
'city' => trans ( 'general.city' ),
'state' => trans ( 'general.state' ),
'country' => trans ( 'general.country' ),
'zip' => trans ( 'general.zip' ),
2023-05-20 14:52:42 -07:00
'currency' => trans ( 'general.importer.currency' ),
'ldap_ou' => trans ( 'admin/locations/table.ldap_ou' ),
'manager_username' => trans ( 'general.importer.manager_username' ),
'manager' => trans ( 'general.importer.manager_full_name' ),
'parent_location' => trans ( 'admin/locations/table.parent' ),
2023-05-16 22:31:58 -07:00
];
// "real fieldnames" to a list of aliases for that field
$this -> aliases_fields = [
2023-05-20 14:21:13 -07:00
'item_name' =>
[
2023-05-23 12:13:06 -07:00
'item name' ,
'asset name' ,
'accessory name' ,
'user name' ,
'consumable name' ,
'component name' ,
2023-05-20 14:52:42 -07:00
'name' ,
2023-05-20 14:21:13 -07:00
],
2023-05-23 12:13:06 -07:00
'item_no' => [
'item number' ,
'item no.' ,
'item #' ,
],
'asset_model' =>
2023-05-20 14:21:13 -07:00
[
'model name' ,
2023-05-23 12:13:06 -07:00
'model' ,
2023-05-20 14:21:13 -07:00
],
2024-08-16 12:29:53 -07:00
'eol_date' =>
[
'eol' ,
'eol date' ,
'asset eol date' ,
],
2023-05-22 13:20:08 -07:00
'gravatar' =>
[
'gravatar' ,
],
2023-05-20 14:52:42 -07:00
'currency' =>
[
'$' ,
],
'jobtitle' =>
[
'job title for user' ,
2023-05-22 13:20:08 -07:00
'job title' ,
2023-05-20 14:52:42 -07:00
],
2024-05-30 16:13:20 -07:00
'full_name' =>
[
'full name' ,
'fullname' ,
trans ( 'general.importer.checked_out_to_fullname' )
],
2023-05-20 14:52:42 -07:00
'username' =>
[
'user name' ,
2023-05-23 13:18:32 -07:00
'username' ,
trans ( 'general.importer.checked_out_to_username' ),
],
'first_name' =>
[
'first name' ,
trans ( 'general.importer.checked_out_to_first_name' ),
],
'last_name' =>
[
'last name' ,
'lastname' ,
trans ( 'general.importer.checked_out_to_last_name' ),
],
'email' =>
[
'email' ,
'e-mail' ,
trans ( 'general.importer.checked_out_to_email' ),
2023-05-20 14:52:42 -07:00
],
'phone_number' =>
[
'phone' ,
'phone number' ,
'phone num' ,
'telephone number' ,
'telephone' ,
'tel.' ,
],
2024-05-30 16:13:20 -07:00
2023-05-20 14:21:13 -07:00
'serial' =>
[
'serial number' ,
'serial no.' ,
'serial no' ,
'product key' ,
'key' ,
],
2023-05-16 22:31:58 -07:00
'model_number' =>
[
'model' ,
'model no' ,
'model no.' ,
'model number' ,
'model num' ,
'model num.'
],
'warranty_months' =>
[
'Warranty' ,
'Warranty Months'
],
'qty' =>
[
'QTY' ,
'Quantity'
],
'zip' =>
[
'Postal Code' ,
2023-05-20 14:21:13 -07:00
'Post Code' ,
'Zip Code'
2023-05-16 22:31:58 -07:00
],
'min_amt' =>
[
'Min Amount' ,
2023-05-23 12:13:06 -07:00
'Minimum Amount' ,
'Min Quantity' ,
'Minimum Quantity' ,
2023-05-16 22:31:58 -07:00
],
'next_audit_date' =>
[
'Next Audit' ,
],
2024-05-30 16:13:20 -07:00
'last_checkout' =>
[
'Last Checkout' ,
'Last Checkout Date' ,
'Checkout Date' ,
],
2023-05-16 22:31:58 -07:00
'address2' =>
[
'Address 2' ,
'Address2' ,
],
'ldap_ou' =>
[
'LDAP OU' ,
'OU' ,
],
'parent_location' =>
[
'Parent' ,
'Parent Location' ,
],
'manager' =>
[
'Managed By' ,
'Manager Name' ,
'Manager Full Name' ,
],
'manager_username' =>
[
'Manager Username' ,
],
];
2023-03-18 20:31:56 -07:00
$this -> columnOptions [ '' ] = $this -> getColumns ( '' ); //blank mode? I don't know what this is supposed to mean
2024-07-16 17:03:42 -07:00
foreach ( $this -> importTypes as $type => $name ) {
2023-03-18 20:31:56 -07:00
$this -> columnOptions [ $type ] = $this -> getColumns ( $type );
}
2022-01-13 01:19:13 -08:00
}
2023-03-21 22:26:32 -07:00
public function selectFile ( $id )
2023-02-28 18:36:52 -08:00
{
2023-09-20 17:22:12 -07:00
$this -> clearMessage ();
2023-04-16 07:47:42 -07:00
2024-07-16 17:03:42 -07:00
$this -> activeFileId = $id ;
2023-09-20 17:22:12 -07:00
if ( ! $this -> activeFile ) {
2023-09-20 17:35:34 -07:00
$this -> message = trans ( 'admin/hardware/message.import.file_missing' );
$this -> message_type = 'danger' ;
2023-09-20 17:22:12 -07:00
2023-09-20 17:35:34 -07:00
return ;
2023-09-20 17:22:12 -07:00
}
2024-07-16 13:30:29 -07:00
$this -> headerRow = $this -> activeFile -> header_row ;
2024-07-16 14:20:41 -07:00
$this -> typeOfImport = $this -> activeFile -> import_type ;
2024-07-16 13:30:29 -07:00
2023-03-21 22:26:32 -07:00
$this -> field_map = null ;
2024-07-16 13:30:29 -07:00
foreach ( $this -> headerRow as $element ) {
2024-07-16 17:03:42 -07:00
if ( isset ( $this -> activeFile -> field_map [ $element ])) {
2023-03-21 22:26:32 -07:00
$this -> field_map [] = $this -> activeFile -> field_map [ $element ];
} else {
$this -> field_map [] = null ; // re-inject the 'nulls' if a file was imported with some 'Do Not Import' settings
}
}
$this -> import_errors = null ;
$this -> statusText = null ;
2022-01-13 01:19:13 -08:00
2022-11-22 12:41:25 -08:00
}
2022-09-19 21:04:46 -07:00
public function destroy ( $id )
{
2024-07-16 17:17:45 -07:00
$this -> authorize ( 'import' );
2024-07-16 17:21:18 -07:00
$import = Import :: find ( $id );
// Check that the import wasn't deleted after while page was already loaded...
2024-07-17 13:40:41 -07:00
// @todo: next up...handle the file being missing for other interactions...
// for example having an import open in two tabs, deleting it, and then changing
// the import type in the other tab. The error message below wouldn't display in that case.
2024-07-16 17:21:18 -07:00
if ( ! $import ) {
2024-07-17 16:28:26 -07:00
$this -> message = trans ( 'admin/hardware/message.import.file_already_deleted' );
2024-07-16 17:21:18 -07:00
$this -> message_type = 'danger' ;
return ;
}
2024-07-16 17:17:45 -07:00
if ( Storage :: delete ( 'private_uploads/imports/' . $import -> file_path )) {
$import -> delete ();
$this -> message = trans ( 'admin/hardware/message.import.file_delete_success' );
$this -> message_type = 'success' ;
unset ( $this -> files );
return ;
2022-09-19 21:04:46 -07:00
}
2024-07-16 16:08:42 -07:00
2024-07-16 17:17:45 -07:00
$this -> message = trans ( 'admin/hardware/message.import.file_delete_error' );
$this -> message_type = 'danger' ;
2022-09-19 21:04:46 -07:00
}
2023-09-20 17:22:12 -07:00
public function clearMessage ()
{
$this -> message = null ;
$this -> message_type = null ;
}
2024-07-16 16:08:42 -07:00
#[Computed]
public function files ()
{
return Import :: orderBy ( 'id' , 'desc' ) -> get ();
}
2024-07-16 17:03:42 -07:00
#[Computed]
public function activeFile ()
{
2024-07-17 17:10:12 -07:00
return Import :: find ( $this -> activeFileId );
2024-07-16 17:03:42 -07:00
}
2022-01-13 01:19:13 -08:00
public function render ()
{
2024-05-29 12:57:43 -07:00
return view ( 'livewire.importer' )
2024-07-16 17:03:42 -07:00
-> extends ( 'layouts.default' )
-> section ( 'content' );
2022-01-13 01:19:13 -08:00
}
}