Ejemplo n.º 1
0
 protected function btnNext_Click()
 {
     $blnError = false;
     if ($this->intStep == 1) {
         if ($this->chkHeaderRow->Checked) {
             $this->blnHeaderRow = true;
         } else {
             $this->blnHeaderRow = false;
         }
         // Check errors
         if ($this->lstFieldSeparator->SelectedValue == 'other' && !$this->txtFieldSeparator->Text) {
             $this->flcFileCsv->Warning = "Please enter the field separator.";
             $blnError = true;
         } elseif ($this->lstTextDelimiter->SelectedValue == 'other' && !$this->txtTextDelimiter->Text) {
             $this->flcFileCsv->Warning = "Please enter the text delimiter.";
             $blnError = true;
         } else {
             // Step 1 complete
             // File Not Uploaded
             if (!file_exists($this->flcFileCsv->File) || !$this->flcFileCsv->Size) {
                 //throw new QCallerException('FileAssetType must be a valid QFileAssetType constant value');
                 $this->flcFileCsv->Warning = 'The file could not be uploaded. Please provide a valid file.';
                 $blnError = true;
                 // File Has Incorrect MIME Type (only if an acceptiblemimearray is setup)
             } elseif (is_array($this->strAcceptibleMimeArray) && !array_key_exists($this->flcFileCsv->Type, $this->strAcceptibleMimeArray)) {
                 $this->flcFileCsv->Warning = "Extension must be 'csv' or 'txt'";
                 $blnError = true;
                 // File Successfully Uploaded
             } else {
                 $this->flcFileCsv->Warning = "";
                 // Setup Filename, Base Filename and Extension
                 $strFilename = $this->flcFileCsv->FileName;
                 $intPosition = strrpos($strFilename, '.');
             }
             if (!$blnError) {
                 $this->FileCsvData = new File_CSV_DataSource();
                 // Setup the settings which have got on step 1
                 $this->FileCsvData->settings($this->GetCsvSettings());
                 $file = fopen($this->flcFileCsv->File, "r");
                 // Counter of files
                 $i = 1;
                 // Counter of rows
                 $j = 1;
                 $this->strFilePathArray = array();
                 // The uploaded file splits up in order to avoid out of memory
                 while ($row = fgets($file, 1000)) {
                     if ($j == 1) {
                         $strFilePath = sprintf('%s/%s_con_%s.csv', __TRACMOR_TMP__, $_SESSION['intUserAccountId'], $i);
                         $this->strFilePathArray[] = $strFilePath;
                         $file_part = fopen($strFilePath, "w+");
                         if ($i == 1) {
                             if ($this->blnHeaderRow) {
                                 $strHeaderRow = $row;
                             } else {
                                 // Add empty row which would be as header row
                                 $strHeaderRow = "\n";
                                 fwrite($file_part, $strHeaderRow);
                             }
                         } else {
                             fwrite($file_part, $strHeaderRow);
                         }
                     }
                     fwrite($file_part, $row);
                     $j++;
                     if ($j > 200) {
                         $j = 1;
                         $i++;
                         fclose($file_part);
                     }
                 }
                 $this->intTotalCount = ($i - 1) * 200 + $j - 1;
                 if (true && QApplication::$TracmorSettings->AssetLimit != null && QApplication::$TracmorSettings->AssetLimit < $this->intTotalCount + Asset::CountAll()) {
                     $blnError = true;
                     $this->btnNext->Warning = $i . " " . $j . "Sorry that is too many assets. Your asset limit is = " . QApplication::$TracmorSettings->AssetLimit . ", this import has " . $this->intTotalCount . " assets, and you already have " . Asset::CountAll() . " assets in the database.";
                 } else {
                     $this->arrMapFields = array();
                     $this->arrTracmorField = array();
                     // Load first file
                     $this->FileCsvData->load($this->strFilePathArray[0]);
                     $file_skipped = fopen($this->strFilePath = sprintf('%s/%s_contact_skipped.csv', __TRACMOR_TMP__, $_SESSION['intUserAccountId']), "w+");
                     // Get Headers
                     if ($this->blnHeaderRow) {
                         $this->arrCsvHeader = $this->FileCsvData->getHeaders();
                         // Create the header row in the skipped error file
                         $this->PutSkippedRecordInFile($file_skipped, $this->arrCsvHeader);
                     }
                     /*else {
                         // If it is not first file
                         $this->FileCsvData->appendRow($this->FileCsvData->getHeaders());
                       }*/
                     $strFirstRowArray = $this->FileCsvData->getRow(0);
                     for ($i = 0; $i < count($strFirstRowArray); $i++) {
                         $this->arrMapFields[$i] = array();
                         if ($this->blnHeaderRow && array_key_exists($i, $this->arrCsvHeader)) {
                             if ($this->arrCsvHeader[$i] == '') {
                                 $this->arrCsvHeader[$i] = ' ';
                             }
                             $this->lstMapHeader_Create($this, $i, $this->arrCsvHeader[$i]);
                             $this->arrMapFields[$i]['header'] = $this->arrCsvHeader[$i];
                         } else {
                             $this->lstMapHeader_Create($this, $i);
                         }
                         // Create Default Value TextBox, ListBox and DateTimePicker
                         if ($this->blnHeaderRow && array_key_exists($i, $this->arrCsvHeader) && $this->arrCsvHeader[$i] || !$this->blnHeaderRow) {
                             $txtDefaultValue = new QTextBox($this);
                             $txtDefaultValue->Width = 200;
                             $this->txtMapDefaultValueArray[] = $txtDefaultValue;
                             $lstDefaultValue = new QListBox($this);
                             $lstDefaultValue->Width = 200;
                             $lstDefaultValue->Display = false;
                             $this->lstMapDefaultValueArray[] = $lstDefaultValue;
                             $dtpDate = new QDateTimePicker($this);
                             $dtpDate->DateTimePickerType = QDateTimePickerType::Date;
                             $dtpDate->DateTimePickerFormat = QDateTimePickerFormat::MonthDayYear;
                             $dtpDate->Display = false;
                             $this->dtpDateArray[] = $dtpDate;
                             if (array_key_exists($i, $this->lstMapHeaderArray)) {
                                 $this->lstTramorField_Change(null, $this->lstMapHeaderArray[$i]->ControlId, null);
                             }
                         }
                         $this->arrMapFields[$i]['row1'] = $strFirstRowArray[$i];
                     }
                     $this->btnNext->Text = "Import Now";
                     fclose($file_skipped);
                     // Create Add Field button
                     $btnAddField = new QButton($this);
                     $btnAddField->Text = "Add Field";
                     $btnAddField->AddAction(new QClickEvent(), new QServerAction('btnAddField_Click'));
                     $btnAddField->AddAction(new QEnterKeyEvent(), new QServerAction('btnAddField_Click'));
                     $btnAddField->AddAction(new QEnterKeyEvent(), new QTerminateAction());
                     $this->lstMapHeaderArray[] = $btnAddField;
                 }
             }
         }
     } elseif ($this->intStep == 2) {
         // Step 2 complete
         $blnError = false;
         $blnCompany = false;
         $blnLastName = false;
         $blnContactId = false;
         $this->intCompanyArray = array();
         foreach (Company::LoadAll() as $objCompany) {
             $this->intCompanyArray[strtolower($objCompany->ShortDescription)] = $objCompany->CompanyId;
         }
         for ($i = 0; $i < count($this->lstMapHeaderArray) - 1; $i++) {
             $lstMapHeader = $this->lstMapHeaderArray[$i];
             $strSelectedValue = strtolower($lstMapHeader->SelectedValue);
             if ($strSelectedValue == "company") {
                 $blnCompany = true;
             }
             if ($strSelectedValue == "last name") {
                 $blnLastName = true;
             } elseif ($strSelectedValue == "id") {
                 $blnContactId = true;
             }
         }
         if ($this->lstMapDefaultValueArray) {
             // Checking errors for required Default Value text fields
             foreach ($this->lstMapDefaultValueArray as $lstDefault) {
                 if ($lstDefault->Display && $lstDefault->Required && !$lstDefault->SelectedValue) {
                     $lstDefault->Warning = "You must select one default value.";
                     $blnError = true;
                     break;
                 } else {
                     $blnError = false;
                     $lstDefault->Warning = "";
                 }
             }
         }
         if ($this->txtMapDefaultValueArray) {
             // Checking errors for required Default Value lst fields
             foreach ($this->txtMapDefaultValueArray as $txtDefault) {
                 if ($txtDefault->Display && $txtDefault->Required && !$txtDefault->Text) {
                     $txtDefault->Warning = "You must enter default value.";
                     break;
                 } else {
                     $blnError = false;
                     $txtDefault->Warning = "";
                 }
             }
         }
         // If all required fields have no errors
         if (!$blnError && $blnCompany && $blnLastName && ($this->lstImportAction->SelectedValue != 2 || $blnContactId)) {
             $this->btnNext->Warning = "";
             // Setup keys for main required fields
             foreach ($this->arrTracmorField as $key => $value) {
                 if ($value == 'company') {
                     $this->intCompanyKey = $key;
                 } elseif ($value == 'description') {
                     $this->intDescriptionKey = $key;
                 } elseif ($value == 'last name') {
                     $this->intLastNameKey = $key;
                 } elseif ($value == 'first name') {
                     $this->intFirstNameKey = $key;
                 } elseif ($value == 'title') {
                     $this->intTitleKey = $key;
                 } elseif ($value == 'email') {
                     $this->intEmailKey = $key;
                 } elseif ($value == 'office phone') {
                     $this->intOfficePhoneKey = $key;
                 } elseif ($value == 'home phone') {
                     $this->intHomePhoneKey = $key;
                 } elseif ($value == 'mobile phone') {
                     $this->intMobilePhoneKey = $key;
                 } elseif ($value == 'fax') {
                     $this->intFaxKey = $key;
                 } elseif ($this->lstImportAction->SelectedValue == 2 && $value == 'id') {
                     $this->intItemIdKey = $key;
                 }
             }
             $this->objNewContactArray = array();
             $this->blnImportEnd = false;
             $j = 1;
             $this->btnNext->RemoveAllActions('onclick');
             // Add new ajax actions for button
             $this->btnNext->AddAction(new QClickEvent(), new QAjaxAction('btnNext_Click'));
             $this->btnNext->AddAction(new QClickEvent(), new QToggleEnableAction($this->btnNext));
             $this->btnNext->AddAction(new QEnterKeyEvent(), new QAjaxAction('btnNext_Click'));
             $this->btnNext->AddAction(new QEnterKeyEvent(), new QToggleEnableAction($this->btnNext));
             $this->btnNext->AddAction(new QEnterKeyEvent(), new QTerminateAction());
             $this->btnNext->Warning = "Please wait...";
             $this->intImportStep = 2;
             $this->intCurrentFile = 0;
             $this->strSelectedValueArray = array();
             // New categories
             $this->dtgContact = new QDataGrid($this);
             $this->dtgContact->Name = 'contact_list';
             $this->dtgContact->CellPadding = 5;
             $this->dtgContact->CellSpacing = 0;
             $this->dtgContact->CssClass = "datagrid";
             $this->dtgContact->UseAjax = true;
             $this->dtgContact->ShowColumnToggle = false;
             $this->dtgContact->ShowExportCsv = false;
             $this->dtgContact->ShowHeader = false;
             $this->dtgContact->AddColumn(new QDataGridColumnExt('Contact', '<?= $_ITEM ?>', 'CssClass="dtg_column"', 'HtmlEntities="false"'));
             // Updated categories
             $this->dtgUpdatedItems = new QDataGrid($this);
             $this->dtgUpdatedItems->Name = 'updated_contact_list';
             $this->dtgUpdatedItems->CellPadding = 5;
             $this->dtgUpdatedItems->CellSpacing = 0;
             $this->dtgUpdatedItems->CssClass = "datagrid";
             $this->dtgUpdatedItems->UseAjax = true;
             $this->dtgUpdatedItems->ShowColumnToggle = false;
             $this->dtgUpdatedItems->ShowExportCsv = false;
             $this->dtgUpdatedItems->ShowHeader = false;
             $this->dtgUpdatedItems->AddColumn(new QDataGridColumnExt('Contact Name', '<?= $_ITEM ?>', 'CssClass="dtg_column"', 'HtmlEntities="false"'));
             // Create the label for successful import
             $this->lblImportSuccess = new QLabel($this);
             $this->lblImportSuccess->HtmlEntities = false;
             $this->lblImportSuccess->Display = false;
             // Undo Last Import button
             $this->btnUndoLastImport = new QButton($this);
             $this->btnUndoLastImport->Text = "Undo Last Import";
             $this->btnUndoLastImport->Display = false;
             $this->btnUndoLastImport->AddAction(new QClickEvent(), new QServerAction('btnCancel_Click'));
             $this->btnUndoLastImport->AddAction(new QEnterKeyEvent(), new QServerAction('btnCancel_Click'));
             $this->btnUndoLastImport->AddAction(new QEnterKeyEvent(), new QTerminateAction());
             // Import More button
             $this->btnImportMore = new QButton($this);
             $this->btnImportMore->Text = "Import More";
             $this->btnImportMore->Display = false;
             $this->btnImportMore->AddAction(new QClickEvent(), new QServerAction('btnImportMore_Click'));
             $this->btnImportMore->AddAction(new QEnterKeyEvent(), new QServerAction('btnImportMore_Click'));
             $this->btnImportMore->AddAction(new QEnterKeyEvent(), new QTerminateAction());
             // Return to Assets button
             $this->btnReturnTo = new QButton($this);
             $this->btnReturnTo->Text = "Return to Contacts";
             $this->btnReturnTo->Display = false;
             $this->btnReturnTo->AddAction(new QClickEvent(), new QServerAction('btnReturnTo_Click'));
             $this->btnReturnTo->AddAction(new QEnterKeyEvent(), new QServerAction('btnReturnTo_Click'));
             $this->btnReturnTo->AddAction(new QEnterKeyEvent(), new QTerminateAction());
         } else {
             $this->btnNext->Warning = "You must select all required fields.";
             $blnError = true;
         }
     } else {
         // Step 3 complete
         set_time_limit(0);
         $file_skipped = fopen($strFilePath = sprintf('%s/%s_contact_skipped.csv', __TRACMOR_TMP__, $_SESSION['intUserAccountId']), "a");
         if (!$this->blnImportEnd) {
             // Category
             if ($this->intImportStep == 2) {
                 $strContactArray = array();
                 $this->objNewContactArray = array();
                 // Load all categories
                 foreach (Contact::LoadAll() as $objContact) {
                     $strContactArray[] = stripslashes(sprintf("%s %s", $objContact->LastName, $objContact->LastName));
                 }
                 // Add Default value
                 $txtDefaultValue = trim($this->txtMapDefaultValueArray[$this->intCompanyKey]->Text);
                 /*if ($txtDefaultValue && !$this->in_array_nocase($txtDefaultValue, $strContactArray)) {
                     $strContactArray[] = $txtDefaultValue;
                     $objNewContact = new Contact();
                     $objNewContact->ShortDescription = addslashes($txtDefaultValue);
                     $objNewContact->Save();
                     $this->objNewContactArray[$objNewContact->ContactId] = $objNewContact->ShortDescription;
                   }*/
                 $this->btnNext->Warning = "Contacts have been imported. Please wait...";
             }
             for ($j = $this->intCurrentFile; $j < count($this->strFilePathArray); $j++) {
                 $this->FileCsvData->load($this->strFilePathArray[$j]);
                 if (!$j) {
                     //$this->FileCsvData->appendRow($this->FileCsvData->getHeaders());
                 }
                 // Category Import
                 if ($this->intImportStep == 2) {
                     $arrItemCustomField = array();
                     foreach ($this->arrTracmorField as $key => $value) {
                         if (substr($value, 0, 8) == 'contact_') {
                             $intItemCustomFieldKeyArray[substr($value, 8)] = $key;
                             if (array_key_exists(substr($value, 8), $this->arrItemCustomField)) {
                                 $arrItemCustomField[substr($value, 8)] = $this->arrItemCustomField[substr($value, 8)];
                             }
                         }
                     }
                     $strContactValuesArray = array();
                     $strUpdatedContactValuesArray = array();
                     $strItemCFVArray = array();
                     $strUpdatedItemCFVArray = array();
                     $strNewContactArray = array();
                     $this->arrOldItemArray = array();
                     $this->objUpdatedItemArray = array();
                     $objContactArray = array();
                     $objExpansionMap[Contact::ExpandCompany] = true;
                     $objExpansionMap[Contact::ExpandAddress] = true;
                     foreach (Contact::LoadArrayBySearchHelper(null, null, null, null, null, null, null, null, null, null, $objExpansionMap) as $objContact) {
                         $objContactArray[$objContact->ContactId] = $objContact;
                     }
                     for ($i = 0; $i < $this->FileCsvData->countRows(); $i++) {
                         $strRowArray = $this->FileCsvData->getRow($i);
                         $objContact = null;
                         if ($this->lstImportAction->SelectedValue == 2) {
                             $intItemId = intval(trim($strRowArray[$this->intItemIdKey]));
                             foreach ($objContactArray as $objItem) {
                                 if ($objItem->ContactId == $intItemId) {
                                     $objContact = $objItem;
                                     break;
                                 }
                             }
                         } else {
                             $intItemId = 0;
                         }
                         $strCompany = strtolower(trim($strRowArray[$this->intCompanyKey]));
                         if ($strCompany && array_key_exists($strCompany, $this->intCompanyArray)) {
                             $intCompanyId = $this->intCompanyArray[substr($strCompany, 0)];
                         } else {
                             $intCompanyId = null;
                         }
                         // Create action
                         if ($intCompanyId && trim($strRowArray[$this->intLastNameKey]) && (!$intItemId || !$objContact)) {
                             $strContactArray[] = trim($strRowArray[$this->intLastNameKey]);
                             $strDescription = "";
                             if (isset($this->intDescriptionKey)) {
                                 if (trim($strRowArray[$this->intDescriptionKey])) {
                                     $strDescription = trim($strRowArray[$this->intDescriptionKey]);
                                 } else {
                                     $strDescription = isset($this->txtMapDefaultValueArray[$this->intDescriptionKey]) ? trim($this->txtMapDefaultValueArray[$this->intDescriptionKey]->Text) : '';
                                 }
                             }
                             $strLastName = "";
                             if (isset($this->intLastNameKey)) {
                                 if (trim($strRowArray[$this->intLastNameKey])) {
                                     $strLastName = trim($strRowArray[$this->intLastNameKey]);
                                 } else {
                                     $strLastName = isset($this->txtMapDefaultValueArray[$this->intLastNameKey]) ? trim($this->txtMapDefaultValueArray[$this->intLastNameKey]->Text) : '';
                                 }
                             }
                             $strFirstName = "";
                             if (isset($this->intFirstNameKey)) {
                                 if (trim($strRowArray[$this->intFirstNameKey])) {
                                     $strFirstName = trim($strRowArray[$this->intFirstNameKey]);
                                 } else {
                                     $strFirstName = isset($this->txtMapDefaultValueArray[$this->intFirstNameKey]) ? trim($this->txtMapDefaultValueArray[$this->intFirstNameKey]->Text) : '';
                                 }
                             }
                             $strEmail = "";
                             if (isset($this->intEmailKey)) {
                                 if (trim($strRowArray[$this->intEmailKey])) {
                                     $strEmail = trim($strRowArray[$this->intEmailKey]);
                                 } else {
                                     $strEmail = isset($this->txtMapDefaultValueArray[$this->intEmailKey]) ? trim($this->txtMapDefaultValueArray[$this->intEmailKey]->Text) : '';
                                 }
                             }
                             $strOfficePhone = "";
                             if (isset($this->intOfficePhoneKey)) {
                                 if (trim($strRowArray[$this->intOfficePhoneKey])) {
                                     $strOfficePhone = trim($strRowArray[$this->intOfficePhoneKey]);
                                 } else {
                                     $strOfficePhone = isset($this->txtMapDefaultValueArray[$this->intOfficePhoneKey]) ? trim($this->txtMapDefaultValueArray[$this->intOfficePhoneKey]->Text) : '';
                                 }
                             }
                             $strHomePhone = "";
                             if (isset($this->intHomePhoneKey)) {
                                 if (trim($strRowArray[$this->intHomePhoneKey])) {
                                     $strHomePhone = trim($strRowArray[$this->intHomePhoneKey]);
                                 } else {
                                     $strHomePhone = isset($this->txtMapDefaultValueArray[$this->intHomePhoneKey]) ? trim($this->txtMapDefaultValueArray[$this->intHomePhoneKey]->Text) : '';
                                 }
                             }
                             $strMobilePhone = "";
                             if (isset($this->intMobilePhoneKey)) {
                                 if (trim($strRowArray[$this->intMobilePhoneKey])) {
                                     $strMobilePhone = trim($strRowArray[$this->intMobilePhoneKey]);
                                 } else {
                                     $strMobilePhone = isset($this->txtMapDefaultValueArray[$this->intMobilePhoneKey]) ? trim($this->txtMapDefaultValueArray[$this->intMobilePhoneKey]->Text) : '';
                                 }
                             }
                             $strFax = "";
                             if (isset($this->intFaxKey)) {
                                 if (trim($strRowArray[$this->intFaxKey])) {
                                     $strFax = trim($strRowArray[$this->intFaxKey]);
                                 } else {
                                     $strFax = isset($this->txtMapDefaultValueArray[$this->intFaxKey]) ? trim($this->txtMapDefaultValueArray[$this->intFaxKey]->Text) : '';
                                 }
                             }
                             $strTitle = "";
                             if (isset($this->intTitleKey)) {
                                 if (trim($strRowArray[$this->intTitleKey])) {
                                     $strTitle = trim($strRowArray[$this->intTitleKey]);
                                 } else {
                                     $strTitle = isset($this->txtMapDefaultValueArray[$this->intTitleKey]) ? trim($this->txtMapDefaultValueArray[$this->intTitleKey]->Text) : '';
                                 }
                             }
                             $strCFVArray = array();
                             $blnCheckCFVError = false;
                             // Custom Field import
                             foreach ($arrItemCustomField as $objCustomField) {
                                 if ($objCustomField->CustomFieldQtypeId != 2) {
                                     $strShortDescription = trim($strRowArray[$intItemCustomFieldKeyArray[$objCustomField->CustomFieldId]]) ? addslashes(trim($strRowArray[$intItemCustomFieldKeyArray[$objCustomField->CustomFieldId]])) : addslashes($this->txtMapDefaultValueArray[$intItemCustomFieldKeyArray[$objCustomField->CustomFieldId]]->Text);
                                     $strCFVArray[$objCustomField->CustomFieldId] = $strShortDescription ? sprintf("'%s'", $strShortDescription) : "NULL";
                                 } else {
                                     $objDatabase = CustomField::GetDatabase();
                                     $strShortDescription = addslashes(trim($strRowArray[$intItemCustomFieldKeyArray[$objCustomField->CustomFieldId]]));
                                     $blnInList = false;
                                     foreach (CustomFieldValue::LoadArrayByCustomFieldId($objCustomField->CustomFieldId) as $objCustomFieldValue) {
                                         if (strtolower($objCustomFieldValue->ShortDescription) == strtolower($strShortDescription)) {
                                             $blnInList = true;
                                             break;
                                         }
                                     }
                                     // Add the CustomFieldValue
                                     /*if (!$blnInList && !in_array($strShortDescription, $strAddedCFVArray)) {
                                     			$strQuery = sprintf("INSERT INTO custom_field_value (custom_field_id, short_description, created_by, creation_date) VALUES (%s, '%s', %s, NOW());", $objCustomField->CustomFieldId, $strShortDescription, $_SESSION['intUserAccountId']);
                                     			$objDatabase->NonQuery($strQuery);
                                     			$strAddedCFVArray[] = $strShortDescription;
                                     		}
                                     		else*/
                                     if (!$blnInList && $this->lstMapDefaultValueArray[$intItemCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedValue != null) {
                                         $strShortDescription = $this->lstMapDefaultValueArray[$intItemCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedName;
                                     } elseif (!$blnInList) {
                                         $blnCheckCFVError = true;
                                         break;
                                     }
                                     if (!$blnCheckCFVError) {
                                         if ($strShortDescription) {
                                             $strCFVArray[$objCustomField->CustomFieldId] = sprintf("'%s'", $strShortDescription);
                                         } else {
                                             $strCFVArray[$objCustomField->CustomFieldId] = "NULL";
                                         }
                                     }
                                 }
                             }
                             if (!$blnCheckCFVError) {
                                 if (isset($strCFVArray) && count($strCFVArray)) {
                                     $strItemCFVArray[] = implode(', ', $strCFVArray);
                                 } else {
                                     $strItemCFVArray[] = "";
                                 }
                                 $strContactValuesArray[] = sprintf("('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', NOW())", $intCompanyId, addslashes($strFirstName), addslashes($strLastName), addslashes($strTitle), addslashes($strEmail), addslashes($strDescription), addslashes($strOfficePhone), addslashes($strHomePhone), addslashes($strMobilePhone), addslashes($strFax), $_SESSION['intUserAccountId']);
                                 $strNewContactArray[] = addslashes(sprintf("%s %s", isset($this->intFirstNameKey) ? trim($strRowArray[$this->intFirstNameKey]) : "", trim($strRowArray[$this->intLastNameKey])));
                             } else {
                                 $this->intSkippedRecordCount++;
                                 $this->PutSkippedRecordInFile($file_skipped, $strRowArray);
                             }
                         } elseif ($intCompanyId && trim($strRowArray[$this->intLastNameKey]) && $this->lstImportAction->SelectedValue == 2 && $objContact) {
                             if (!$blnError) {
                                 $strUpdateFieldArray = array();
                                 //$objContact = $objContactArray[strtolower(trim($strRowArray[$this->intCompanyKey]))];
                                 $strUpdateFieldArray[] = sprintf("`company_id`='%s'", addslashes($intCompanyId));
                                 $strDescription = "";
                                 if (isset($this->intDescriptionKey)) {
                                     if (trim($strRowArray[$this->intDescriptionKey])) {
                                         $strDescription = trim($strRowArray[$this->intDescriptionKey]);
                                     } else {
                                         $strDescription = isset($this->txtMapDefaultValueArray[$this->intDescriptionKey]) ? trim($this->txtMapDefaultValueArray[$this->intDescriptionKey]->Text) : '';
                                     }
                                     $strUpdateFieldArray[] = sprintf("`description`='%s'", addslashes($strDescription));
                                 }
                                 $strLastName = "";
                                 if (isset($this->intLastNameKey)) {
                                     if (trim($strRowArray[$this->intLastNameKey])) {
                                         $strLastName = trim($strRowArray[$this->intLastNameKey]);
                                     } else {
                                         $strLastName = isset($this->txtMapDefaultValueArray[$this->intLastNameKey]) ? trim($this->txtMapDefaultValueArray[$this->intLastNameKey]->Text) : '';
                                     }
                                     $strUpdateFieldArray[] = sprintf("`last_name`='%s'", addslashes($strLastName));
                                 }
                                 $strFirstName = "";
                                 if (isset($this->intFirstNameKey)) {
                                     if (trim($strRowArray[$this->intFirstNameKey])) {
                                         $strFirstName = trim($strRowArray[$this->intFirstNameKey]);
                                     } else {
                                         $strFirstName = isset($this->txtMapDefaultValueArray[$this->intFirstNameKey]) ? trim($this->txtMapDefaultValueArray[$this->intFirstNameKey]->Text) : '';
                                     }
                                     $strUpdateFieldArray[] = sprintf("`first_name`='%s'", addslashes($strFirstName));
                                 }
                                 $strEmail = "";
                                 if (isset($this->intEmailKey)) {
                                     if (trim($strRowArray[$this->intEmailKey])) {
                                         $strEmail = trim($strRowArray[$this->intEmailKey]);
                                     } else {
                                         $strEmail = isset($this->txtMapDefaultValueArray[$this->intEmailKey]) ? trim($this->txtMapDefaultValueArray[$this->intEmailKey]->Text) : '';
                                     }
                                     $strUpdateFieldArray[] = sprintf("`email`='%s'", addslashes($strEmail));
                                 }
                                 $strOfficePhone = "";
                                 if (isset($this->intOfficePhoneKey)) {
                                     if (trim($strRowArray[$this->intOfficePhoneKey])) {
                                         $strOfficePhone = trim($strRowArray[$this->intOfficePhoneKey]);
                                     } else {
                                         $strOfficePhone = isset($this->txtMapDefaultValueArray[$this->intOfficePhoneKey]) ? trim($this->txtMapDefaultValueArray[$this->intOfficePhoneKey]->Text) : '';
                                     }
                                     $strUpdateFieldArray[] = sprintf("`phone_office`='%s'", addslashes($strOfficePhone));
                                 }
                                 $strHomePhone = "";
                                 if (isset($this->intHomePhoneKey)) {
                                     if (trim($strRowArray[$this->intHomePhoneKey])) {
                                         $strHomePhone = trim($strRowArray[$this->intHomePhoneKey]);
                                     } else {
                                         $strHomePhone = isset($this->txtMapDefaultValueArray[$this->intHomePhoneKey]) ? trim($this->txtMapDefaultValueArray[$this->intHomePhoneKey]->Text) : '';
                                     }
                                     $strUpdateFieldArray[] = sprintf("`phone_home`='%s'", addslashes($strHomePhone));
                                 }
                                 $strMobilePhone = "";
                                 if (isset($this->intMobilePhoneKey)) {
                                     if (trim($strRowArray[$this->intMobilePhoneKey])) {
                                         $strMobilePhone = trim($strRowArray[$this->intMobilePhoneKey]);
                                     } else {
                                         $strMobilePhone = isset($this->txtMapDefaultValueArray[$this->intMobilePhoneKey]) ? trim($this->txtMapDefaultValueArray[$this->intMobilePhoneKey]->Text) : '';
                                     }
                                     $strUpdateFieldArray[] = sprintf("`phone_mobile`='%s'", addslashes($strMobilePhone));
                                 }
                                 $strFax = "";
                                 if (isset($this->intFaxKey)) {
                                     if (trim($strRowArray[$this->intFaxKey])) {
                                         $strFax = trim($strRowArray[$this->intFaxKey]);
                                     } else {
                                         $strFax = isset($this->txtMapDefaultValueArray[$this->intFaxKey]) ? trim($this->txtMapDefaultValueArray[$this->intFaxKey]->Text) : '';
                                     }
                                     $strUpdateFieldArray[] = sprintf("`fax`='%s'", addslashes($strFax));
                                 }
                                 $strTitle = "";
                                 if (isset($this->intTitleKey)) {
                                     if (trim($strRowArray[$this->intTitleKey])) {
                                         $strTitle = trim($strRowArray[$this->intTitleKey]);
                                     } else {
                                         $strTitle = isset($this->txtMapDefaultValueArray[$this->intTitleKey]) ? trim($this->txtMapDefaultValueArray[$this->intTitleKey]->Text) : '';
                                     }
                                     $strUpdateFieldArray[] = sprintf("`title`='%s'", addslashes($strTitle));
                                 }
                                 $strUpdateFieldArray[] = sprintf("modified_by='%s'", $_SESSION['intUserAccountId']);
                                 $this->arrOldItemArray[$objContact->ContactId] = $objContact;
                                 $blnCheckCFVError = false;
                                 foreach ($arrItemCustomField as $objCustomField) {
                                     //$objItem = $objContactArray[strtolower($objUpdatedItem)];
                                     if ($objCustomField->CustomFieldQtypeId != 2) {
                                         $strShortDescription = trim($strRowArray[$intItemCustomFieldKeyArray[$objCustomField->CustomFieldId]]) ? addslashes(trim($strRowArray[$intItemCustomFieldKeyArray[$objCustomField->CustomFieldId]])) : addslashes($this->txtMapDefaultValueArray[$intItemCustomFieldKeyArray[$objCustomField->CustomFieldId]]->Text);
                                         $strCFVArray[$objCustomField->CustomFieldId] = $strShortDescription ? sprintf("'%s'", $strShortDescription) : "NULL";
                                     } else {
                                         $objDatabase = CustomField::GetDatabase();
                                         $strShortDescription = addslashes(trim($strRowArray[$intItemCustomFieldKeyArray[$objCustomField->CustomFieldId]]));
                                         $strCFVArray[$objCustomField->CustomFieldId] = $strShortDescription ? sprintf("'%s'", $strShortDescription) : "NULL";
                                         $blnInList = false;
                                         foreach (CustomFieldValue::LoadArrayByCustomFieldId($objCustomField->CustomFieldId) as $objCustomFieldValue) {
                                             if (strtolower($objCustomFieldValue->ShortDescription) == strtolower($strShortDescription)) {
                                                 //$intItemKeyntCustomFieldValueId = $objCustomFieldValue->CustomFieldValueId;
                                                 $blnInList = true;
                                                 break;
                                             }
                                         }
                                         // Add the CustomFieldValue
                                         /*if (!$blnInList && !in_array($strShortDescription, $strAddedCFVArray)) {
                                         			$strQuery = sprintf("INSERT INTO custom_field_value (custom_field_id, short_description, created_by, creation_date) VALUES (%s, '%s', %s, NOW());", $objCustomField->CustomFieldId, $strShortDescription, $_SESSION['intUserAccountId']);
                                         			$objDatabase->NonQuery($strQuery);
                                         			$strAddedCFVArray[] = $strShortDescription;
                                         		}
                                         		else*/
                                         if (!$blnInList && $this->lstMapDefaultValueArray[$intItemCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedValue != null) {
                                             $strShortDescription = $this->lstMapDefaultValueArray[$intItemCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedName;
                                         } elseif (!$blnInList) {
                                             $blnCheckCFVError = true;
                                             break;
                                         }
                                         if (!$blnCheckCFVError) {
                                             if ($strShortDescription) {
                                                 $strCFVArray[$objCustomField->CustomFieldId] = sprintf("'%s'", $strShortDescription);
                                             } else {
                                                 $strCFVArray[$objCustomField->CustomFieldId] = "NULL";
                                             }
                                         }
                                     }
                                 }
                                 if (!$blnCheckCFVError) {
                                     if (isset($strCFVArray) && count($strCFVArray)) {
                                         $strUpdatedItemCFVArray[$objContact->ContactId] = $strCFVArray;
                                     } else {
                                         $strUpdatedItemCFVArray[$objContact->ContactId] = "";
                                     }
                                     $strUpdatedContactValuesArray[] = sprintf("UPDATE `contact` SET %s WHERE `contact_id`='%s'", implode(", ", $strUpdateFieldArray), $objContact->ContactId);
                                     $this->objUpdatedItemArray[$objContact->ContactId] = sprintf("%s %s", $objContact->FirstName, $objContact->LastName);
                                 } else {
                                     $this->intSkippedRecordCount++;
                                     $this->PutSkippedRecordInFile($file_skipped, $strRowArray);
                                 }
                             } else {
                                 $this->intSkippedRecordCount++;
                                 $this->PutSkippedRecordInFile($file_skipped, $strRowArray);
                             }
                         } else {
                             $this->intSkippedRecordCount++;
                             $this->PutSkippedRecordInFile($file_skipped, $strRowArray);
                         }
                     }
                     if (count($strContactValuesArray)) {
                         $objDatabase = Contact::GetDatabase();
                         $objDatabase->NonQuery(sprintf("INSERT INTO `contact` (`company_id`, `first_name`, `last_name`, `title`, `email`, `description`, `phone_office`, `phone_home`, `phone_mobile`, `fax`, `created_by`, `creation_date`) VALUES %s;", implode(", ", $strContactValuesArray)));
                         $intStartId = $objDatabase->InsertId();
                         $strItemIdArray = array();
                         for ($i = 0; $i < count($strNewContactArray); $i++) {
                             $this->objNewContactArray[$intStartId + $i] = $strNewContactArray[$i];
                             $objDatabase = CustomField::GetDatabase();
                             $strItemCFVArray[$i] = sprintf("('%s', %s)", $intStartId + $i, $strItemCFVArray[$i]);
                             $strItemIdArray[$i] = sprintf("(%s)", $intStartId + $i);
                         }
                         $strCFVNameArray = array();
                         foreach ($arrItemCustomField as $objCustomField) {
                             $strCFVNameArray[] = sprintf("`cfv_%s`", $objCustomField->CustomFieldId);
                         }
                         if (count($strItemCFVArray) > 0 && count($strCFVNameArray) > 0) {
                             $strQuery = sprintf("INSERT INTO `contact_custom_field_helper` (`contact_id`, %s) VALUES %s", implode(", ", $strCFVNameArray), implode(", ", $strItemCFVArray));
                         } else {
                             $strQuery = sprintf("INSERT INTO `contact_custom_field_helper` (`contact_id`) VALUES %s", implode(", ", $strItemIdArray));
                         }
                         $objDatabase->NonQuery($strQuery);
                     }
                     if (count($strUpdatedContactValuesArray)) {
                         $objDatabase = Category::GetDatabase();
                         foreach ($strUpdatedContactValuesArray as $query) {
                             $objDatabase->NonQuery($query);
                         }
                         foreach ($this->objUpdatedItemArray as $intItemKey => $objUpdatedItem) {
                             if (isset($strUpdatedItemCFVArray[$intItemKey]) && count($strUpdatedItemCFVArray[$intItemKey])) {
                                 $strCFVArray = array();
                                 foreach ($arrItemCustomField as $objCustomField) {
                                     $strCFVArray[] = sprintf("`cfv_%s`=%s", $objCustomField->CustomFieldId, $strUpdatedItemCFVArray[$intItemKey][$objCustomField->CustomFieldId]);
                                 }
                                 if (isset($strCFVArray) && count($strCFVArray)) {
                                     $strQuery = sprintf("UPDATE `contact_custom_field_helper` SET %s WHERE `contact_id`='%s'", implode(", ", $strCFVArray), $intItemKey);
                                     $objDatabase->NonQuery($strQuery);
                                 }
                             }
                         }
                     }
                 }
             }
             if ($this->intImportStep == 3) {
                 $this->blnImportEnd = true;
                 $this->btnNext->Warning = "";
                 $this->lblImportResults->Display = true;
                 if (count($this->objUpdatedItemArray)) {
                     $this->lblImportUpdatedItems->Display = true;
                     $this->dtgUpdatedItems->Paginator = new QPaginator($this->dtgUpdatedItems);
                     $this->dtgUpdatedItems->ItemsPerPage = 20;
                 }
                 if (count($this->objNewContactArray)) {
                     $this->lblImportContacts->Display = true;
                     $this->dtgContact->Paginator = new QPaginator($this->dtgContact);
                     $this->dtgContact->ItemsPerPage = 20;
                 }
                 $this->btnNext->Display = false;
                 $this->btnCancel->Display = false;
                 $this->btnUndoLastImport->Display = true;
                 $this->btnImportMore->Display = true;
                 $this->btnReturnTo->Display = true;
                 $this->lblImportSuccess->Display = true;
                 $this->lblImportSuccess->Text = sprintf("Success:<br/>" . "<b>%s</b> Records imported successfully<br/>" . "<b>%s</b> Records skipped due to error<br/>", count($this->objNewContactArray) + count($this->objUpdatedItemArray), $this->intSkippedRecordCount);
                 if ($this->intSkippedRecordCount) {
                     $this->lblImportSuccess->Text .= sprintf("<a href='./contact_import.php?intDownloadCsv=1'>Click here to download records that could not be imported</a>");
                 }
                 $this->lblImportSuccess->Text .= "<br/><br/>";
                 $this->intImportStep = -1;
             }
             // Enable Next button
             $this->btnNext->Enabled = true;
             if (!$this->blnImportEnd && !$this->intCurrentFile) {
                 $this->intImportStep++;
             }
         }
         fclose($file_skipped);
     }
     if (!$blnError) {
         if (($this->blnImportEnd || $this->intImportStep == 2) && $this->intImportStep != -1) {
             $this->intStep++;
             $this->DisplayStepForm($this->intStep);
         }
         if (!$this->blnImportEnd) {
             QApplication::ExecuteJavaScript("document.getElementById('" . $this->btnNext->ControlId . "').click();");
         }
         if (!($this->intCurrentFile < count($this->strFilePathArray))) {
             $this->intCurrentFile = 0;
             $this->intImportStep++;
         }
     }
 }
Ejemplo n.º 2
0
 protected function btnNext_Click()
 {
     $blnError = false;
     if ($this->intStep == 1) {
         if ($this->chkHeaderRow->Checked) {
             $this->blnHeaderRow = true;
         } else {
             $this->blnHeaderRow = false;
         }
         // Check errors
         if ($this->lstFieldSeparator->SelectedValue == 'other' && !$this->txtFieldSeparator->Text) {
             $this->flcFileCsv->Warning = "Please enter the field separator.";
             $blnError = true;
         } elseif ($this->lstTextDelimiter->SelectedValue == 'other' && !$this->txtTextDelimiter->Text) {
             $this->flcFileCsv->Warning = "Please enter the text delimiter.";
             $blnError = true;
         } else {
             // Step 1 complete
             // File Not Uploaded
             if (!file_exists($this->flcFileCsv->File) || !$this->flcFileCsv->Size) {
                 //throw new QCallerException('FileAssetType must be a valid QFileAssetType constant value');
                 $this->flcFileCsv->Warning = 'The file could not be uploaded. Please provide a valid file.';
                 $blnError = true;
                 // File Has Incorrect MIME Type (only if an acceptiblemimearray is setup)
             } elseif (is_array($this->strAcceptibleMimeArray) && !array_key_exists($this->flcFileCsv->Type, $this->strAcceptibleMimeArray)) {
                 $this->flcFileCsv->Warning = "Extension must be 'csv' or 'txt'";
                 $blnError = true;
                 // File Successfully Uploaded
             } else {
                 $this->flcFileCsv->Warning = "";
                 // Setup Filename, Base Filename and Extension
                 $strFilename = $this->flcFileCsv->FileName;
                 $intPosition = strrpos($strFilename, '.');
             }
             if (!$blnError) {
                 $this->FileCsvData = new File_CSV_DataSource();
                 // Setup the settings which have got on step 1
                 $this->FileCsvData->settings($this->GetCsvSettings());
                 $file = fopen($this->flcFileCsv->File, "r");
                 // Counter of files
                 $i = 1;
                 // Counter of rows
                 $j = 1;
                 $this->strFilePathArray = array();
                 // The uploaded file splits up in order to avoid out of memory
                 while ($row = fgets($file, 1000)) {
                     if ($j == 1) {
                         $strFilePath = sprintf('%s/%s_%s.csv', __DOCROOT__ . __SUBDIRECTORY__ . __TRACMOR_TMP__, $_SESSION['intUserAccountId'], $i);
                         $this->strFilePathArray[] = $strFilePath;
                         $file_part = fopen($strFilePath, "w+");
                         if ($i == 1) {
                             $strHeaderRow = $row;
                         } else {
                             fwrite($file_part, $strHeaderRow);
                         }
                     }
                     fwrite($file_part, $row);
                     $j++;
                     if ($j > 200) {
                         $j = 1;
                         $i++;
                         fclose($file_part);
                     }
                 }
                 $this->intTotalCount = ($i - 1) * 200 + $j - 1;
                 if (QApplication::$TracmorSettings->AssetLimit != null && QApplication::$TracmorSettings->AssetLimit < $this->intTotalCount + Asset::CountAll()) {
                     $blnError = true;
                     $this->btnNext->Warning = $i . " " . $j . "Sorry that is too many assets. Your asset limit is = " . QApplication::$TracmorSettings->AssetLimit . ", this import has " . $this->intTotalCount . " assets, and you already have " . Asset::CountAll() . " assets in the database.";
                 } else {
                     $this->arrMapFields = array();
                     $this->arrTracmorField = array();
                     // Load first file
                     $this->FileCsvData->load($this->strFilePathArray[0]);
                     $file_skipped = fopen($this->strFilePath = sprintf('%s/%s_skipped.csv', __DOCROOT__ . __SUBDIRECTORY__ . __TRACMOR_TMP__, $_SESSION['intUserAccountId']), "w+");
                     // Get Headers
                     if ($this->blnHeaderRow) {
                         $this->arrCsvHeader = $this->FileCsvData->getHeaders();
                         // Create the header row in the skipped error file
                         $this->PutSkippedRecordInFile($file_skipped, $this->arrCsvHeader);
                     }
                     /*else {
                         // If it is not first file
                         $this->FileCsvData->appendRow($this->FileCsvData->getHeaders());
                       }*/
                     $strFirstRowArray = $this->FileCsvData->getRow(0);
                     for ($i = 0; $i < count($strFirstRowArray); $i++) {
                         $this->arrMapFields[$i] = array();
                         if ($this->blnHeaderRow && array_key_exists($i, $this->arrCsvHeader)) {
                             if ($this->arrCsvHeader[$i] == '') {
                                 $this->arrCsvHeader[$i] = ' ';
                             }
                             $this->lstMapHeader_Create($this, $i, $this->arrCsvHeader[$i]);
                             $this->arrMapFields[$i]['header'] = $this->arrCsvHeader[$i];
                         } else {
                             $this->lstMapHeader_Create($this, $i);
                         }
                         // Create Default Value TextBox, ListBox and DateTimePicker
                         if ($this->blnHeaderRow && array_key_exists($i, $this->arrCsvHeader) && $this->arrCsvHeader[$i] || !$this->blnHeaderRow) {
                             $txtDefaultValue = new QTextBox($this);
                             $txtDefaultValue->Width = 200;
                             $this->txtMapDefaultValueArray[] = $txtDefaultValue;
                             $lstDefaultValue = new QListBox($this);
                             $lstDefaultValue->Width = 200;
                             $lstDefaultValue->Display = false;
                             $this->lstMapDefaultValueArray[] = $lstDefaultValue;
                             $dtpDate = new QDateTimePicker($this);
                             $dtpDate->DateTimePickerType = QDateTimePickerType::Date;
                             $dtpDate->DateTimePickerFormat = QDateTimePickerFormat::MonthDayYear;
                             $dtpDate->Display = false;
                             $this->dtpDateArray[] = $dtpDate;
                             if (array_key_exists($i, $this->lstMapHeaderArray)) {
                                 $this->lstTramorField_Change(null, $this->lstMapHeaderArray[$i]->ControlId, null);
                             }
                         }
                         $this->arrMapFields[$i]['row1'] = $strFirstRowArray[$i];
                     }
                     $this->btnNext->Text = "Import Now";
                     fclose($file_skipped);
                     // Create Add Field button
                     $btnAddField = new QButton($this);
                     $btnAddField->Text = "Add Field";
                     $btnAddField->AddAction(new QClickEvent(), new QServerAction('btnAddField_Click'));
                     $btnAddField->AddAction(new QEnterKeyEvent(), new QServerAction('btnAddField_Click'));
                     $btnAddField->AddAction(new QEnterKeyEvent(), new QTerminateAction());
                     $this->lstMapHeaderArray[] = $btnAddField;
                 }
             }
         }
     } elseif ($this->intStep == 2) {
         // Step 2 complete
         $blnError = false;
         $blnAssetCode = false;
         $blnLocation = false;
         $blnAssetModelCode = false;
         $blnAssetModelShortDescription = false;
         $blnCategory = false;
         $blnManufacturer = false;
         // Checking errors (Location, Asset Code, Model Short Description, Model Code, Category and Manufacturer must be selected)
         for ($i = 0; $i < count($this->lstMapHeaderArray) - 1; $i++) {
             $lstMapHeader = $this->lstMapHeaderArray[$i];
             $strSelectedValue = strtolower($lstMapHeader->SelectedValue);
             if ($strSelectedValue == "location") {
                 $blnLocation = true;
             } elseif ($strSelectedValue == "asset code") {
                 $blnAssetCode = true;
             } elseif ($strSelectedValue == "asset model short description") {
                 $blnAssetModelShortDescription = true;
             } elseif ($strSelectedValue == "asset model code") {
                 $blnAssetModelCode = true;
             } elseif ($strSelectedValue == "category") {
                 $blnCategory = true;
             } elseif ($strSelectedValue == "manufacturer") {
                 $blnManufacturer = true;
             }
         }
         if ($this->lstMapDefaultValueArray) {
             // Checking errors for required Default Value text fields
             foreach ($this->lstMapDefaultValueArray as $lstDefault) {
                 if ($lstDefault->Display && $lstDefault->Required && !$lstDefault->SelectedValue) {
                     $lstDefault->Warning = "You must select one default value.";
                     $blnError = true;
                     break;
                 } else {
                     $blnError = false;
                     $lstDefault->Warning = "";
                 }
             }
         }
         if ($this->txtMapDefaultValueArray) {
             // Checking errors for required Default Value lst fields
             foreach ($this->txtMapDefaultValueArray as $txtDefault) {
                 if ($txtDefault->Display && $txtDefault->Required && !$txtDefault->Text) {
                     $txtDefault->Warning = "You must enter default value.";
                     break;
                 } else {
                     $blnError = false;
                     $txtDefault->Warning = "";
                 }
             }
         }
         // If all required fields have no errors
         if (!$blnError && $blnAssetCode && $blnAssetModelCode && $blnAssetModelShortDescription && $blnLocation && $blnCategory && $blnManufacturer) {
             $this->btnNext->Warning = "";
             // Setup keys for main required fields
             foreach ($this->arrTracmorField as $key => $value) {
                 if ($value == 'location') {
                     $this->intLocationKey = $key;
                 } elseif ($value == 'category') {
                     $this->intCategoryKey = $key;
                 } elseif ($value == 'manufacturer') {
                     $this->intManufacturerKey = $key;
                 } elseif ($value == 'created by') {
                     $this->intCreatedByKey = $key;
                 } elseif ($value == 'created date') {
                     $this->intCreatedDateKey = $key;
                 } elseif ($value == 'modified by') {
                     $this->intModifiedByKey = $key;
                 } elseif ($value == 'modified date') {
                     $this->intModifiedDateKey = $key;
                 }
             }
             $strLocationArray = array();
             $strNewLocationArray = array();
             // Load all locations
             foreach (Location::LoadAll() as $objLocation) {
                 $strLocationArray[] = stripslashes($objLocation->ShortDescription);
             }
             $txtDefaultValue = trim($this->txtMapDefaultValueArray[$this->intLocationKey]->Text);
             // Add default value in database if it is not exist
             if ($txtDefaultValue && !$this->in_array_nocase($txtDefaultValue, $strLocationArray)) {
                 $strLocationArray[] = $txtDefaultValue;
                 $objNewLocation = new Location();
                 $objNewLocation->ShortDescription = addslashes($txtDefaultValue);
                 $objNewLocation->EnabledFlag = 1;
                 $objNewLocation->Save();
                 $this->objNewLocationArray[$objNewLocation->LocationId] = $objNewLocation->ShortDescription;
             }
             $this->objNewLocationArray = array();
             $this->objNewCategoryArray = array();
             $this->objNewManufacturerArray = array();
             $this->objNewAssetModelArray = array();
             $this->strModelValuesArray = array();
             $this->blnImportEnd = false;
             $j = 1;
             $strLocationValuesArray = array();
             // Add all unique locations in database
             foreach ($this->strFilePathArray as $strFilePath) {
                 $this->FileCsvData->load($strFilePath);
                 if ($j != 1) {
                     //$this->FileCsvData->appendRow($this->FileCsvData->getHeaders());
                 }
                 // Location Import
                 for ($i = 0; $i < $this->FileCsvData->countRows(); $i++) {
                     $strRowArray = $this->FileCsvData->getRow($i);
                     if (trim($strRowArray[$this->intLocationKey]) && !$this->in_array_nocase(trim($strRowArray[$this->intLocationKey]), $strLocationArray)) {
                         $strLocationArray[] = trim($strRowArray[$this->intLocationKey]);
                         /*$objNewLocation = new Location();
                           $objNewLocation->ShortDescription = addslashes(trim($strRowArray[$this->intLocationKey]));
                           $objNewLocation->Save();*/
                         $strLocationValuesArray[] = sprintf("('%s', '%s', NOW())", addslashes(trim($strRowArray[$this->intLocationKey])), $_SESSION['intUserAccountId']);
                         $strNewLocation[] = addslashes(trim($strRowArray[$this->intLocationKey]));
                         //$this->objNewLocationArray[$objNewLocation->LocationId] = $objNewLocation->ShortDescription;
                     }
                 }
                 $j++;
             }
             if (count($strLocationValuesArray)) {
                 $objDatabase = Location::GetDatabase();
                 $objDatabase->NonQuery(sprintf("INSERT INTO `location` (`short_description`, `created_by`, `creation_date`) VALUES %s;", implode(", ", $strLocationValuesArray)));
                 $intStartId = $objDatabase->InsertId();
                 for ($i = 0; $i < count($strNewLocation); $i++) {
                     $this->objNewLocationArray[$intStartId + $i] = $strNewLocation[$i];
                 }
             }
             $this->btnNext->RemoveAllActions('onclick');
             // Add new ajax actions for button
             $this->btnNext->AddAction(new QClickEvent(), new QAjaxAction('btnNext_Click'));
             $this->btnNext->AddAction(new QClickEvent(), new QToggleEnableAction($this->btnNext));
             $this->btnNext->AddAction(new QEnterKeyEvent(), new QAjaxAction('btnNext_Click'));
             $this->btnNext->AddAction(new QEnterKeyEvent(), new QToggleEnableAction($this->btnNext));
             $this->btnNext->AddAction(new QEnterKeyEvent(), new QTerminateAction());
             $this->btnNext->Warning = "Locations have been imported. Please wait...";
             $this->intImportStep = 2;
             $this->intCurrentFile = 0;
             $this->strSelectedValueArray = array();
             // New locations
             $this->dtgLocation = new QDataGrid($this);
             $this->dtgLocation->Name = 'location_list';
             $this->dtgLocation->CellPadding = 5;
             $this->dtgLocation->CellSpacing = 0;
             $this->dtgLocation->CssClass = "datagrid";
             $this->dtgLocation->UseAjax = true;
             $this->dtgLocation->ShowColumnToggle = false;
             $this->dtgLocation->ShowExportCsv = false;
             $this->dtgLocation->ShowHeader = false;
             $this->dtgLocation->AddColumn(new QDataGridColumnExt('Location', '<?= $_ITEM ?>', 'CssClass="dtg_column"', 'HtmlEntities="false"'));
             // New categories
             $this->dtgCategory = new QDataGrid($this);
             $this->dtgCategory->Name = 'category_list';
             $this->dtgCategory->CellPadding = 5;
             $this->dtgCategory->CellSpacing = 0;
             $this->dtgCategory->CssClass = "datagrid";
             $this->dtgCategory->UseAjax = true;
             $this->dtgCategory->ShowColumnToggle = false;
             $this->dtgCategory->ShowExportCsv = false;
             $this->dtgCategory->ShowHeader = false;
             $this->dtgCategory->AddColumn(new QDataGridColumnExt('Manufacturer', '<?= $_ITEM ?>', 'CssClass="dtg_column"', 'HtmlEntities="false"'));
             // New manufacturers
             $this->dtgManufacturer = new QDataGrid($this);
             $this->dtgManufacturer->Name = 'manufacturer_list';
             $this->dtgManufacturer->CellPadding = 5;
             $this->dtgManufacturer->CellSpacing = 0;
             $this->dtgManufacturer->CssClass = "datagrid";
             $this->dtgManufacturer->UseAjax = true;
             $this->dtgManufacturer->ShowColumnToggle = false;
             $this->dtgManufacturer->ShowExportCsv = false;
             $this->dtgManufacturer->ShowHeader = false;
             $this->dtgManufacturer->AddColumn(new QDataGridColumnExt('Manufacturer', '<?= $_ITEM ?>', 'CssClass="dtg_column"', 'HtmlEntities="false"'));
             // New asset models
             $this->dtgAssetModel = new QDataGrid($this);
             $this->dtgAssetModel->Name = 'asset_model_list';
             $this->dtgAssetModel->CellPadding = 5;
             $this->dtgAssetModel->CellSpacing = 0;
             $this->dtgAssetModel->CssClass = "datagrid";
             $this->dtgAssetModel->UseAjax = true;
             $this->dtgAssetModel->ShowColumnToggle = false;
             $this->dtgAssetModel->ShowExportCsv = false;
             $this->dtgAssetModel->ShowHeader = false;
             $this->dtgAssetModel->AddColumn(new QDataGridColumnExt('Model', '<?= $_ITEM ?>', 'CssClass="dtg_column"', 'HtmlEntities="false"'));
             // New assets
             $this->dtgAsset = new QDataGrid($this);
             $this->dtgAsset->Name = 'asset_list';
             $this->dtgAsset->CellPadding = 5;
             $this->dtgAsset->CellSpacing = 0;
             $this->dtgAsset->CssClass = "datagrid";
             $this->dtgAsset->UseAjax = true;
             $this->dtgAsset->ShowColumnToggle = false;
             $this->dtgAsset->ShowExportCsv = false;
             $this->dtgAsset->ShowHeader = false;
             $this->dtgAsset->AddColumn(new QDataGridColumnExt('Asset Tag', '<?= $_ITEM ?>', 'CssClass="dtg_column"', 'HtmlEntities="false"'));
             // Updated assets
             $this->dtgUpdatedAsset = new QDataGrid($this);
             $this->dtgUpdatedAsset->Name = 'updated_asset_list';
             $this->dtgUpdatedAsset->CellPadding = 5;
             $this->dtgUpdatedAsset->CellSpacing = 0;
             $this->dtgUpdatedAsset->CssClass = "datagrid";
             $this->dtgUpdatedAsset->UseAjax = true;
             $this->dtgUpdatedAsset->ShowColumnToggle = false;
             $this->dtgUpdatedAsset->ShowExportCsv = false;
             $this->dtgUpdatedAsset->ShowHeader = false;
             $this->dtgUpdatedAsset->AddColumn(new QDataGridColumnExt('Asset Tag', '<?= $_ITEM ?>', 'CssClass="dtg_column"', 'HtmlEntities="false"'));
             // Create the label for successful import
             $this->lblImportSuccess = new QLabel($this);
             $this->lblImportSuccess->HtmlEntities = false;
             $this->lblImportSuccess->Display = false;
             // Undo Last Import button
             $this->btnUndoLastImport = new QButton($this);
             $this->btnUndoLastImport->Text = "Undo Last Import";
             $this->btnUndoLastImport->Display = false;
             $this->btnUndoLastImport->AddAction(new QClickEvent(), new QServerAction('btnCancel_Click'));
             $this->btnUndoLastImport->AddAction(new QEnterKeyEvent(), new QServerAction('btnCancel_Click'));
             $this->btnUndoLastImport->AddAction(new QEnterKeyEvent(), new QTerminateAction());
             // Import More button
             $this->btnImportMore = new QButton($this);
             $this->btnImportMore->Text = "Import More";
             $this->btnImportMore->Display = false;
             $this->btnImportMore->AddAction(new QClickEvent(), new QServerAction('btnImportMore_Click'));
             $this->btnImportMore->AddAction(new QEnterKeyEvent(), new QServerAction('btnImportMore_Click'));
             $this->btnImportMore->AddAction(new QEnterKeyEvent(), new QTerminateAction());
             // Return to Assets button
             $this->btnReturnToAssets = new QButton($this);
             $this->btnReturnToAssets->Text = "Return to Assets";
             $this->btnReturnToAssets->Display = false;
             $this->btnReturnToAssets->AddAction(new QClickEvent(), new QServerAction('btnReturnToAssets_Click'));
             $this->btnReturnToAssets->AddAction(new QEnterKeyEvent(), new QServerAction('btnReturnToAssets_Click'));
             $this->btnReturnToAssets->AddAction(new QEnterKeyEvent(), new QTerminateAction());
         } else {
             $this->btnNext->Warning = "You must select all required fields (Asset Tag, Model Number, Model Short Description, Location, Category and Manufacturer).";
             $blnError = true;
         }
     } else {
         // Step 3 complete
         set_time_limit(0);
         $file_skipped = fopen($strFilePath = sprintf('%s/%s_skipped.csv', __DOCROOT__ . __SUBDIRECTORY__ . __TRACMOR_TMP__, $_SESSION['intUserAccountId']), "a");
         if (!$this->blnImportEnd) {
             // Category
             if ($this->intImportStep == 2) {
                 $strCategoryArray = array();
                 $this->objNewCategoryArray = array();
                 // Load all categories
                 foreach (Category::LoadAll() as $objCategory) {
                     $strCategoryArray[] = stripslashes($objCategory->ShortDescription);
                 }
                 // Add Default value
                 $txtDefaultValue = trim($this->txtMapDefaultValueArray[$this->intCategoryKey]->Text);
                 if ($txtDefaultValue && !$this->in_array_nocase($txtDefaultValue, $strCategoryArray)) {
                     $strCategoryArray[] = $txtDefaultValue;
                     $objNewCategory = new Category();
                     $objNewCategory->ShortDescription = addslashes($txtDefaultValue);
                     $objNewCategory->AssetFlag = true;
                     $objNewCategory->InventoryFlag = false;
                     $objNewCategory->Save();
                     $this->objNewCategoryArray[$objNewCategory->CategoryId] = $objNewCategory->ShortDescription;
                 }
                 $this->btnNext->Warning = "Categories have been imported. Please wait...";
             } elseif ($this->intImportStep == 3) {
                 $strManufacturerArray = array();
                 $this->objNewManufacturerArray = array();
                 // Load all manufacturers
                 foreach (Manufacturer::LoadAll() as $objManufacturer) {
                     $strManufacturerArray[] = stripslashes($objManufacturer->ShortDescription);
                 }
                 $txtDefaultValue = trim($this->txtMapDefaultValueArray[$this->intManufacturerKey]->Text);
                 // Add Default Value
                 if ($txtDefaultValue && !$this->in_array_nocase($txtDefaultValue, $strManufacturerArray)) {
                     $strManufacturerArray[] = $txtDefaultValue;
                     $objNewManufacturer = new Manufacturer();
                     $objNewManufacturer->ShortDescription = addslashes($txtDefaultValue);
                     $objNewManufacturer->Save();
                     $this->objNewManufacturerArray[$objNewManufacturer->ManufacturerId] = $objNewManufacturer->ShortDescription;
                 }
                 $this->btnNext->Warning = "Manufacturers have been imported. Please wait...";
             } elseif ($this->intImportStep == 4) {
                 $intCategoryArray = array();
                 // Load all categories with keys=category_id
                 foreach (Category::LoadAllWithFlags(true, false) as $objCategory) {
                     //$intCategoryArray["'" . strtolower($objCategory->ShortDescription) . "'"] = $objCategory->CategoryId;
                     $intCategoryArray[$objCategory->CategoryId] = strtolower($objCategory->ShortDescription);
                 }
                 $intManufacturerArray = array();
                 // Load all manufacturers with keys=manufacturer_id
                 foreach (Manufacturer::LoadAll() as $objManufacturer) {
                     //$intManufacturerArray["'" . strtolower($objManufacturer->ShortDescription) . "'"] = $objManufacturer->ManufacturerId;
                     $intManufacturerArray[$objManufacturer->ManufacturerId] = strtolower($objManufacturer->ShortDescription);
                 }
                 $intModelCustomFieldKeyArray = array();
                 $arrAssetModelCustomField = array();
                 // Setup keys
                 foreach ($this->arrTracmorField as $key => $value) {
                     if ($value == 'asset model short description') {
                         $intModelShortDescriptionKey = $key;
                     } elseif ($value == 'asset model long description') {
                         $intModelLongDescriptionKey = $key;
                     } elseif ($value == 'asset model code') {
                         $intModelCodeKey = $key;
                     } elseif ($value == 'asset code') {
                         $intAssetCode = $key;
                     } elseif (substr($value, 0, 6) == 'model_') {
                         $intModelCustomFieldKeyArray[substr($value, 6)] = $key;
                         if (array_key_exists(substr($value, 6), $this->arrAssetCustomField)) {
                             $arrAssetModelCustomField[substr($value, 6)] = $this->arrAssetCustomField[substr($value, 6)];
                         }
                     }
                 }
                 $strAssetModelArray = array();
                 // Load all asset models
                 foreach (AssetModel::LoadAll() as $objAssetModel) {
                     $strAssetModelArray[] = strtolower(sprintf("%s_%s_%s_%s", $objAssetModel->AssetModelCode, $objAssetModel->ShortDescription, $objAssetModel->CategoryId, $objAssetModel->ManufacturerId));
                 }
                 $this->btnNext->Warning = sprintf("Please wait... Model import complete: %s%s", ceil(($this->intCurrentFile + 1) * 200 / $this->intTotalCount * 100), "%");
             } elseif ($this->intImportStep == 5) {
                 $intCategoryArray = array();
                 // Load all categories with keys=category_id
                 foreach (Category::LoadAllWithFlags(true, false) as $objCategory) {
                     //$intCategoryArray["'" . strtolower($objCategory->ShortDescription) . "'"] = $objCategory->CategoryId;
                     $intCategoryArray[$objCategory->CategoryId] = strtolower($objCategory->ShortDescription);
                 }
                 $intManufacturerArray = array();
                 // Load all manufacturers with keys=manufacturer_id
                 foreach (Manufacturer::LoadAll() as $objManufacturer) {
                     //$intManufacturerArray["'" . strtolower($objManufacturer->ShortDescription) . "'"] = $objManufacturer->ManufacturerId;
                     $intManufacturerArray[$objManufacturer->ManufacturerId] = strtolower($objManufacturer->ShortDescription);
                 }
                 if ($this->intCurrentFile == 0) {
                     $this->intAssetModelArray = array();
                     // Load all asset models with keys=asset_model_id
                     foreach (AssetModel::LoadAll() as $objAssetModel) {
                         //$intAssetModelArray["'" . strtolower($objAssetModel->ShortDescription) . "'"] = $objAssetModel->AssetModelId;
                         $this->intAssetModelArray[$objAssetModel->AssetModelId] = strtolower(sprintf("%s_%s_%s_%s", $objAssetModel->AssetModelCode, $objAssetModel->ShortDescription, $objAssetModel->CategoryId, $objAssetModel->ManufacturerId));
                     }
                 }
                 $intAssetCustomFieldKeyArray = array();
                 $arrAssetCustomField = array();
                 // Setup keys
                 foreach ($this->arrTracmorField as $key => $value) {
                     if ($value == 'asset model short description') {
                         $intModelShortDescriptionKey = $key;
                     } elseif ($value == 'asset model code') {
                         $intModelCodeKey = $key;
                     } elseif ($value == 'asset code') {
                         $intAssetCode = $key;
                     } elseif (substr($value, 0, 6) == 'asset_') {
                         $intAssetCustomFieldKeyArray[substr($value, 6)] = $key;
                         if (array_key_exists(substr($value, 6), $this->arrAssetCustomField)) {
                             $arrAssetCustomField[substr($value, 6)] = $this->arrAssetCustomField[substr($value, 6)];
                         }
                     }
                 }
                 $intLocationArray = array();
                 // Load all locations with keys=location_id
                 foreach (Location::LoadAll() as $objLocation) {
                     //$intLocationArray["'" . strtolower($objLocation->ShortDescription) . "'"] = $objLocation->LocationId;
                     $intLocationArray[$objLocation->LocationId] = strtolower($objLocation->ShortDescription);
                 }
                 $strAssetArray = array();
                 $strUpdatedAssetArray = array();
                 // Load all assets
                 foreach (Asset::LoadAll() as $objAsset) {
                     $strAssetArray[] = strtolower($objAsset->AssetCode);
                 }
                 $this->btnNext->Warning = sprintf("Please wait... Asset import complete: %s%s", ceil(($this->intCurrentFile + 1) * 200 / $this->intTotalCount * 100), "%");
             }
             for ($j = $this->intCurrentFile; $j < count($this->strFilePathArray); $j++) {
                 $this->FileCsvData->load($this->strFilePathArray[$j]);
                 if (!$j) {
                     //$this->FileCsvData->appendRow($this->FileCsvData->getHeaders());
                 }
                 // Category Import
                 if ($this->intImportStep == 2) {
                     $strCategoryValuesArray = array();
                     $strNewCategoryArray = array();
                     for ($i = 0; $i < $this->FileCsvData->countRows(); $i++) {
                         $strRowArray = $this->FileCsvData->getRow($i);
                         if (trim($strRowArray[$this->intCategoryKey]) && !$this->in_array_nocase(trim($strRowArray[$this->intCategoryKey]), $strCategoryArray)) {
                             $strCategoryArray[] = trim($strRowArray[$this->intCategoryKey]);
                             /*$objNewCategory = new Category();
                               $objNewCategory->ShortDescription = addslashes(trim($strRowArray[$this->intCategoryKey]));
                               $objNewCategory->AssetFlag = true;
                               $objNewCategory->InventoryFlag = false;
                               $objNewCategory->Save();
                               $this->objNewCategoryArray[$objNewCategory->CategoryId] = $objNewCategory->ShortDescription;*/
                             $strCategoryValuesArray[] = sprintf("('%s', '1', '0', '%s', NOW())", addslashes(trim($strRowArray[$this->intCategoryKey])), $_SESSION['intUserAccountId']);
                             $strNewCategoryArray[] = addslashes(trim($strRowArray[$this->intCategoryKey]));
                         }
                     }
                     if (count($strCategoryValuesArray)) {
                         $objDatabase = Category::GetDatabase();
                         $objDatabase->NonQuery(sprintf("INSERT INTO `category` (`short_description`, `asset_flag`, `inventory_flag`, `created_by`, `creation_date`) VALUES %s;", implode(", ", $strCategoryValuesArray)));
                         $intStartId = $objDatabase->InsertId();
                         for ($i = 0; $i < count($strNewCategoryArray); $i++) {
                             $this->objNewCategoryArray[$intStartId + $i] = $strNewCategoryArray[$i];
                         }
                     }
                 } elseif ($this->intImportStep == 3) {
                     $strManufacturerValuesArray = array();
                     $strNewManufacturerArray = array();
                     for ($i = 0; $i < $this->FileCsvData->countRows(); $i++) {
                         $strRowArray = $this->FileCsvData->getRow($i);
                         if (trim($strRowArray[$this->intManufacturerKey]) && !$this->in_array_nocase(trim($strRowArray[$this->intManufacturerKey]), $strManufacturerArray)) {
                             $strManufacturerArray[] = trim($strRowArray[$this->intManufacturerKey]);
                             /*$objNewManufacturer = new Manufacturer();
                               $objNewManufacturer->ShortDescription = addslashes(trim($strRowArray[$this->intManufacturerKey]));
                               $objNewManufacturer->Save();
                               $this->objNewManufacturerArray[$objNewManufacturer->ManufacturerId] = $objNewManufacturer->ShortDescription;*/
                             $strManufacturerValuesArray[] = sprintf("('%s', '%s', NOW())", addslashes(trim($strRowArray[$this->intManufacturerKey])), $_SESSION['intUserAccountId']);
                             $strNewManufacturerArray[] = addslashes(trim($strRowArray[$this->intManufacturerKey]));
                         }
                     }
                     if (count($strManufacturerValuesArray)) {
                         $objDatabase = Manufacturer::GetDatabase();
                         $objDatabase->NonQuery(sprintf("INSERT INTO `manufacturer` (`short_description`, `created_by`, `creation_date`) VALUES %s;", implode(", ", $strManufacturerValuesArray)));
                         $intStartId = $objDatabase->InsertId();
                         for ($i = 0; $i < count($strNewManufacturerArray); $i++) {
                             $this->objNewManufacturerArray[$intStartId + $i] = $strNewManufacturerArray[$i];
                         }
                     }
                 } elseif ($this->intImportStep == 4) {
                     $objNewAssetModelArray = array();
                     for ($i = 0; $i < $this->FileCsvData->countRows(); $i++) {
                         $strRowArray = $this->FileCsvData->getRow($i);
                         $strShortDescription = trim($strRowArray[$intModelShortDescriptionKey]) ? addslashes(trim($strRowArray[$intModelShortDescriptionKey])) : false;
                         $strAssetModelCode = trim($strRowArray[$intModelCodeKey]) ? addslashes(trim($strRowArray[$intModelCodeKey])) : addslashes(trim($this->txtMapDefaultValueArray[$intModelCodeKey]->Text));
                         $strKeyArray = array_keys($intCategoryArray, addslashes(strtolower(trim($strRowArray[$this->intCategoryKey]))));
                         if (count($strKeyArray)) {
                             $intCategoryId = $strKeyArray[0];
                         } else {
                             $strKeyArray = array_keys($intCategoryArray, addslashes(strtolower(trim($this->txtMapDefaultValueArray[$this->intCategoryKey]->Text))));
                             if (count($strKeyArray)) {
                                 $intCategoryId = $strKeyArray[0];
                             } else {
                                 $intCategoryId = false;
                             }
                         }
                         $strKeyArray = array_keys($intManufacturerArray, addslashes(strtolower(trim($strRowArray[$this->intManufacturerKey]))));
                         if (count($strKeyArray)) {
                             $intManufacturerId = $strKeyArray[0];
                         } else {
                             $strKeyArray = array_keys($intManufacturerArray, addslashes(strtolower(trim($this->txtMapDefaultValueArray[$this->intManufacturerKey]->Text))));
                             if (count($strKeyArray)) {
                                 $intManufacturerId = $strKeyArray[0];
                             } else {
                                 $intManufacturerId = false;
                             }
                         }
                         if (!$strShortDescription || $intCategoryId === false || $intManufacturerId === false) {
                             //$blnError = true;
                             //$this->intSkippedRecordCount++;
                             //$this->PutSkippedRecordInFile($file_skipped, $strRowArray);
                             //echo sprintf("Desc: %s AssetCode: %s Cat: %s Man: %s<br/>", $strShortDescription, $strAssetModelCode, $intCategoryId, $intManufacturerId);
                             //break;
                             $strAssetModel = null;
                         } else {
                             //$blnError = false;
                             $strAssetModel = strtolower(sprintf("%s_%s_%s_%s", $strAssetModelCode, $strShortDescription, $intCategoryId, $intManufacturerId));
                         }
                         if ($strAssetModel && !$this->in_array_nocase($strAssetModel, $strAssetModelArray)) {
                             $strAssetModelArray[] = $strAssetModel;
                             /*$objNewAssetModel = new AssetModel();
                                               $objNewAssetModel->ShortDescription = $strShortDescription;
                                               $objNewAssetModel->AssetModelCode = $strAssetModelCode;
                                               $objNewAssetModel->CategoryId = $intCategoryId;
                                               $objNewAssetModel->ManufacturerId = $intManufacturerId;
                             
                                               if (isset($intModelLongDescriptionKey)) {
                                                 $objNewAssetModel->LongDescription = addslashes(trim($strRowArray[$intModelLongDescriptionKey]));
                                               }
                                               $objNewAssetModel->Save();
                             
                                               // Asset Model Custom Field import
                                               foreach ($arrAssetModelCustomField as $objCustomField) {
                                                 if ($objCustomField->CustomFieldQtypeId != 2) {
                                                   $objCustomField->CustomFieldSelection = new CustomFieldSelection;
                                       						$objCustomField->CustomFieldSelection->newCustomFieldValue = new CustomFieldValue;
                                       						$objCustomField->CustomFieldSelection->newCustomFieldValue->CustomFieldId = $objCustomField->CustomFieldId;
                                       						if (trim($strRowArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]])) {
                                       						  $objCustomField->CustomFieldSelection->newCustomFieldValue->ShortDescription = addslashes(trim($strRowArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]));
                                       						}
                                       						else {
                                       						  $objCustomField->CustomFieldSelection->newCustomFieldValue->ShortDescription = addslashes($this->txtMapDefaultValueArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]->Text);
                                       						}
                                       						$objCustomField->CustomFieldSelection->newCustomFieldValue->Save();
                                       						$objCustomField->CustomFieldSelection->EntityId = $objNewAssetModel->AssetModelId;
                                       						$objCustomField->CustomFieldSelection->EntityQtypeId = 4;
                                       						$objCustomField->CustomFieldSelection->CustomFieldValueId = $objCustomField->CustomFieldSelection->newCustomFieldValue->CustomFieldValueId;
                                       						$objCustomField->CustomFieldSelection->Save();
                                                 }
                                                 else {
                                                   $data = trim($strRowArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]);
                                                   $blnInList = false;
                                                   $objCustomField->CustomFieldSelection = new CustomFieldSelection;
                                             			$objCustomField->CustomFieldSelection->EntityId = $objNewAssetModel->AssetModelId;
                                             			$objCustomField->CustomFieldSelection->EntityQtypeId = 4;
                                             			foreach (CustomFieldValue::LoadArrayByCustomFieldId($objCustomField->CustomFieldId) as $objCustomFieldValue) {
                                         					  if (strtolower($objCustomFieldValue->ShortDescription) == $data) {
                                              					$objCustomField->CustomFieldSelection->CustomFieldValueId = $objCustomFieldValue->CustomFieldValueId;
                                             					$blnInList = true;
                                             					break;
                                         					  }
                                         					}
                                         					if (!$blnInList && $this->lstMapDefaultValueArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedValue != null) {
                                          					  $objCustomField->CustomFieldSelection->CustomFieldValueId = $this->lstMapDefaultValueArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedValue;
                                          					  $objCustomField->CustomFieldSelection->Save();
                                         					}
                                         					elseif ($data) {
                                         					  $objCustomField->CustomFieldSelection->Save();
                                         					}
                                                 }
                                               }*/
                             $this->strModelValuesArray[] = sprintf("('%s', '%s', '%s', '%s', '%s', '%s', NOW())", $strShortDescription, isset($intModelLongDescriptionKey) ? addslashes(trim($strRowArray[$intModelLongDescriptionKey])) : null, $strAssetModelCode, $intCategoryId, $intManufacturerId, $_SESSION['intUserAccountId']);
                             $objNewAssetModelArray[] = $strShortDescription;
                         }
                     }
                     //if ($this->intCurrentFile == count($this->strFilePathArray)) {
                     if (count($this->strModelValuesArray)) {
                         //$strNewModelArray = array_merge($this->objNewAssetModelArray, array());
                         //$this->objNewAssetModelArray = array();
                         $objDatabase = AssetModel::GetDatabase();
                         //var_dump($this->strModelValuesArray);
                         //exit();
                         $objDatabase->NonQuery(sprintf("INSERT INTO `asset_model` (`short_description`, `long_description`, `asset_model_code`, `category_id`, `manufacturer_id`, `created_by`, `creation_date`) VALUES %s;", implode(", ", $this->strModelValuesArray)));
                         $intStartId = $objDatabase->InsertId();
                         for ($i = 0; $i < count($objNewAssetModelArray); $i++) {
                             //$objDatabase->NonQuery(sprintf("INSERT INTO `asset_model` (`short_description`, `long_description`, `asset_model_code`, `category_id`, `manufacturer_id`, `created_by`, `creation_date`) VALUES %s;", $this->strModelValuesArray[$i]));
                             //$intStartId = $objDatabase->InsertId();
                             $this->objNewAssetModelArray[$intStartId + $i] = $objNewAssetModelArray[$i];
                         }
                         $this->strModelValuesArray = array();
                     }
                     //}
                     //$this->intCurrentFile++;
                     //break;
                 } elseif ($this->intImportStep == 5) {
                     $strAssetValuesArray = array();
                     $objAssetValuesArray = array();
                     $strAssetCFVArray = array();
                     $strUpdatedAssetCFVArray = array();
                     $strAssetCodeArray = array();
                     $strAddedCFVArray = array();
                     // This will add extra commas for blank values
                     $this->FileCsvData->symmetrize();
                     for ($i = 0; $i < $this->FileCsvData->countRows(); $i++) {
                         $strRowArray = $this->FileCsvData->getRow($i);
                         // The addslashes was causing match-up problems
                         //$strShortDescription = (trim($strRowArray[$intModelShortDescriptionKey])) ? addslashes(trim($strRowArray[$intModelShortDescriptionKey])) : false;
                         $strShortDescription = trim($strRowArray[$intModelShortDescriptionKey]) ? trim($strRowArray[$intModelShortDescriptionKey]) : false;
                         $strAssetModelCode = trim($strRowArray[$intModelCodeKey]) ? addslashes(trim($strRowArray[$intModelCodeKey])) : addslashes(trim($this->txtMapDefaultValueArray[$intModelCodeKey]->Text));
                         $strKeyArray = array_keys($intCategoryArray, addslashes(strtolower(trim($strRowArray[$this->intCategoryKey]))));
                         if (count($strKeyArray)) {
                             $intCategoryId = $strKeyArray[0];
                         } else {
                             $strKeyArray = array_keys($intCategoryArray, addslashes(strtolower(trim($this->txtMapDefaultValueArray[$this->intCategoryKey]->Text))));
                             if (count($strKeyArray)) {
                                 $intCategoryId = $strKeyArray[0];
                             } else {
                                 $intCategoryId = false;
                             }
                         }
                         $strKeyArray = array_keys($intManufacturerArray, addslashes(strtolower(trim($strRowArray[$this->intManufacturerKey]))));
                         if (count($strKeyArray)) {
                             $intManufacturerId = $strKeyArray[0];
                         } else {
                             $strKeyArray = array_keys($intManufacturerArray, addslashes(strtolower(trim($this->txtMapDefaultValueArray[$this->intManufacturerKey]->Text))));
                             if (count($strKeyArray)) {
                                 $intManufacturerId = $strKeyArray[0];
                             } else {
                                 $intManufacturerId = false;
                             }
                         }
                         if (!$strShortDescription || $intCategoryId === false || $intManufacturerId === false) {
                             //$blnError = true;
                             $this->intSkippedRecordCount++;
                             $this->PutSkippedRecordInFile($file_skipped, $strRowArray);
                             //break;
                         } else {
                             //$blnError = false;
                             $strAssetModel = strtolower(sprintf("%s_%s_%s_%s", $strAssetModelCode, $strShortDescription, $intCategoryId, $intManufacturerId));
                             $strAssetCode = addslashes(trim($strRowArray[$intAssetCode]));
                             if ($strAssetCode && !$this->in_array_nocase($strAssetCode, $strAssetArray)) {
                                 $intLocationKeyArray = array_keys($intLocationArray, addslashes(strtolower(trim($strRowArray[$this->intLocationKey]))));
                                 if (!count($intLocationKeyArray)) {
                                     $intLocationKeyArray = array_keys($intLocationArray, addslashes(strtolower(trim($this->txtMapDefaultValueArray[$this->intLocationKey]->Text))));
                                 }
                                 $intModelKeyArray = array_keys($this->intAssetModelArray, $strAssetModel);
                                 if (count($intLocationKeyArray) && count($intModelKeyArray)) {
                                     $strAssetArray[] = strtolower($strAssetCode);
                                     $strAssetValuesArray[] = sprintf("('%s', '%s', '%s', '%s', NOW())", $strAssetCode, $intLocationKeyArray[0], $intModelKeyArray[0], $_SESSION['intUserAccountId']);
                                     /*$objNewAsset = new Asset();
                                       $objNewAsset->AssetCode = $strAssetCode;
                                       $objNewAsset->LocationId = $intLocationKeyArray[0];
                                       $objNewAsset->AssetModelId = $intModelKeyArray[0];
                                       if (isset($this->intCreatedByKey)) {
                                         if (isset($this->intUserArray[strtolower(trim($strRowArray[$this->intCreatedByKey]))])) {
                                           $objNewAsset->CreatedBy = $this->intUserArray[strtolower(trim($strRowArray[$this->intCreatedByKey]))];
                                         }
                                         else {
                                           $objNewAsset->CreatedBy = $this->lstMapDefaultValueArray[$this->intCreatedByKey]->SelectedValue;
                                         }
                                       }
                                       $objNewAsset->Save();*/
                                     $strCFVArray = array();
                                     $objDatabase = CustomField::GetDatabase();
                                     // Asset Custom Field import
                                     foreach ($arrAssetCustomField as $objCustomField) {
                                         if ($objCustomField->CustomFieldQtypeId != 2) {
                                             $strShortDescription = trim($strRowArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]]) ? addslashes(trim($strRowArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]])) : addslashes($this->txtMapDefaultValueArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]]->Text);
                                             $strCFVArray[$objCustomField->CustomFieldId] = $strShortDescription ? sprintf("'%s'", $strShortDescription) : "NULL";
                                             /*$strQuery = sprintf("INSERT INTO `custom_field_value` " .
                                                                         "(`custom_field_id`,`short_description`, `created_by`, `creation_date`) " .
                                                                         "VALUES ('%s', '%s', '%s', 'NOW()');",
                                                                         $objCustomField->CustomFieldId, $strShortDescription, $_SESSION['intUserAccountId']);
                                                     $objDatabase->NonQuery($strQuery);
                                                     $this->strSelectedValueArray[] = sprintf("('%s', '%s', '%s')", $objNewAsset->AssetId, 1, $objDatabase->InsertId());
                                                     $strCFVArray[] = sprintf("`cfv_%s`='%s'", $objCustomField->CustomFieldId, $strShortDescription);
                                                     $objCustomField->CustomFieldSelection = new CustomFieldSelection;
                                             		$objCustomField->CustomFieldSelection->newCustomFieldValue = new CustomFieldValue;
                                             		$objCustomField->CustomFieldSelection->newCustomFieldValue->CustomFieldId = $objCustomField->CustomFieldId;
                                             		if (trim($strRowArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]])) {
                                             		  $objCustomField->CustomFieldSelection->newCustomFieldValue->ShortDescription = addslashes(trim($strRowArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]]));
                                             		}
                                             		else {
                                             		  $objCustomField->CustomFieldSelection->newCustomFieldValue->ShortDescription = addslashes($this->txtMapDefaultValueArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]]->Text);
                                             		}
                                             		$objCustomField->CustomFieldSelection->newCustomFieldValue->Save();
                                             		$objCustomField->CustomFieldSelection->EntityId = $objNewAsset->AssetId;
                                             		$objCustomField->CustomFieldSelection->EntityQtypeId = 1;
                                             		$objCustomField->CustomFieldSelection->CustomFieldValueId = $objCustomField->CustomFieldSelection->newCustomFieldValue->CustomFieldValueId;
                                             		$objCustomField->CustomFieldSelection->Save();*/
                                         } else {
                                             $objDatabase = Asset::GetDatabase();
                                             $strShortDescription = addslashes(trim($strRowArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]]));
                                             $blnInList = false;
                                             foreach (CustomFieldValue::LoadArrayByCustomFieldId($objCustomField->CustomFieldId) as $objCustomFieldValue) {
                                                 if (strtolower($objCustomFieldValue->ShortDescription) == strtolower($strShortDescription)) {
                                                     //$intCustomFieldValueId = $objCustomFieldValue->CustomFieldValueId;
                                                     $blnInList = true;
                                                     break;
                                                 }
                                             }
                                             // Add the CustomFieldValue
                                             if (!$blnInList && !in_array($strShortDescription, $strAddedCFVArray)) {
                                                 $strQuery = sprintf("INSERT INTO custom_field_value (custom_field_id, short_description, created_by, creation_date) VALUES (%s, '%s', %s, NOW());", $objCustomField->CustomFieldId, $strShortDescription, $_SESSION['intUserAccountId']);
                                                 $objDatabase->NonQuery($strQuery);
                                                 $strAddedCFVArray[] = $strShortDescription;
                                             } elseif (!$blnInList && $this->lstMapDefaultValueArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedValue != null) {
                                                 //$intCustomFieldValueId = $this->lstMapDefaultValueArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedValue;
                                                 $strShortDescription = $this->lstMapDefaultValueArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedName;
                                             }
                                             if ($strShortDescription) {
                                                 //$this->strSelectedValueArray[] = sprintf("('%s', '%s', '%s')", $objNewAsset->AssetId, 1, $intCustomFieldValueId);
                                                 //$strCFVArray[] = sprintf("`cfv_%s`='%s'", $objCustomField->CustomFieldId, $strShortDescription);
                                                 $strCFVArray[$objCustomField->CustomFieldId] = sprintf("'%s'", $strShortDescription);
                                             } else {
                                                 $strCFVArray[$objCustomField->CustomFieldId] = "NULL";
                                             }
                                         }
                                     }
                                     $strAssetCodeArray[] = $strAssetCode;
                                     /*$this->objNewAssetArray[$objNewAsset->AssetId] = $objNewAsset->AssetCode;
                                       if (count($strCFVArray)) {
                                         $strQuery = sprintf("UPDATE `asset_custom_field_helper` " .
                                                             "SET %s " .
                                                             "WHERE `asset_id`='%s';", implode(", ", $strCFVArray), $objNewAsset->AssetId);
                                         $objDatabase->NonQuery($strQuery);
                                       }*/
                                     if (count($strCFVArray)) {
                                         $strAssetCFVArray[] = implode(', ', $strCFVArray);
                                     } else {
                                         $strAssetCFVArray[] = "";
                                     }
                                 } else {
                                     $this->intSkippedRecordCount++;
                                     $this->PutSkippedRecordInFile($file_skipped, $strRowArray);
                                 }
                             } elseif ($strAssetCode && $this->lstImportAction->SelectedValue == 2 && $this->in_array_nocase($strAssetCode, $strAssetArray)) {
                                 $intLocationKeyArray = array_keys($intLocationArray, addslashes(strtolower(trim($strRowArray[$this->intLocationKey]))));
                                 if (isset($this->intCreatedByKey)) {
                                     if (isset($strRowArray[$this->intCreatedByKey]) && isset($this->intUserArray[strtolower(trim($strRowArray[$this->intCreatedByKey]))])) {
                                         $intCreatedBy = $this->intUserArray[strtolower(trim($strRowArray[$this->intCreatedByKey]))];
                                     } else {
                                         $intCreatedBy = $this->lstMapDefaultValueArray[$this->intCreatedByKey]->SelectedValue;
                                     }
                                 } else {
                                     $intCreatedBy = false;
                                 }
                                 if (!count($intLocationKeyArray)) {
                                     $intLocationKeyArray = array_keys($intLocationArray, addslashes(strtolower(trim($this->txtMapDefaultValueArray[$this->intLocationKey]->Text))));
                                 }
                                 $intModelKeyArray = array_keys($this->intAssetModelArray, $strAssetModel);
                                 if (count($intLocationKeyArray) && count($intModelKeyArray)) {
                                     $objAssetArray = Asset::LoadArrayBySearchHelper($strAssetCode, null, null, null, null, false, null, null, null, null, null, null, null, null, null, false, null, null, null, false, false, false);
                                     $objAsset = $objAssetArray[0];
                                     $strUpdatedAssetArray[] = strtolower($strAssetCode);
                                     $strCategoryKeyArray = array_keys($intCategoryArray, addslashes(strtolower(trim($strRowArray[$this->intCategoryKey]))));
                                     // Only fields that can normally be updated when editing an asset can be updated
                                     //if ($objAsset->LocationId != $intLocationKeyArray[0] || $objAsset->AssetModel->CategoryId != $intCategoryId || $objAsset->AssetModel->ManufacturerId != $intManufacturerId) {
                                     if ($objAsset->LocationId != $intLocationKeyArray[0] || $objAsset->CreatedBy != false && $objAsset->CreatedBy != $intCreatedBy) {
                                         $this->intSkippedRecordCount++;
                                         $this->PutSkippedRecordInFile($file_skipped, $strRowArray);
                                     } else {
                                         $this->arrOldAssetArray[$objAsset->AssetId] = array();
                                         $this->arrOldAssetArray[$objAsset->AssetId]['AssetModelId'] = $objAsset->AssetModelId;
                                         $this->arrOldAssetArray[$objAsset->AssetId]['ModifiedBy'] = $objAsset->ModifiedBy;
                                         $this->arrOldAssetArray[$objAsset->AssetId]['ModifiedDate'] = $objAsset->ModifiedDate;
                                         $this->arrOldAssetArray[$objAsset->AssetId]['CFV'] = array();
                                         $objAsset->AssetModelId = $intModelKeyArray[0];
                                         $objAssetValuesArray[] = $objAsset;
                                         //$objAssetValuesArray[] = sprintf("('%s', '%s', '%s', '%s', NOW())", $strAssetCode, $intLocationKeyArray[0], $intModelKeyArray[0], $_SESSION['intUserAccountId']);
                                         $strCFVArray = array();
                                         $objDatabase = CustomField::GetDatabase();
                                         // Asset Custom Field import
                                         foreach ($arrAssetCustomField as $objCustomField) {
                                             $this->arrOldAssetArray[$objAsset->AssetId]['CFV'][$objCustomField->CustomFieldId] = $objAsset->GetVirtualAttribute($objCustomField->CustomFieldId);
                                             if ($objCustomField->CustomFieldQtypeId != 2) {
                                                 $strShortDescription = trim($strRowArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]]) ? addslashes(trim($strRowArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]])) : addslashes($this->txtMapDefaultValueArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]]->Text);
                                                 $strCFVArray[$objCustomField->CustomFieldId] = $strShortDescription ? sprintf("'%s'", $strShortDescription) : "NULL";
                                             } else {
                                                 $objDatabase = Asset::GetDatabase();
                                                 $strShortDescription = addslashes(trim($strRowArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]]));
                                                 $strCFVArray[$objCustomField->CustomFieldId] = $strShortDescription ? sprintf("'%s'", $strShortDescription) : "NULL";
                                                 $blnInList = false;
                                                 foreach (CustomFieldValue::LoadArrayByCustomFieldId($objCustomField->CustomFieldId) as $objCustomFieldValue) {
                                                     if (strtolower($objCustomFieldValue->ShortDescription) == strtolower($strShortDescription)) {
                                                         //$intCustomFieldValueId = $objCustomFieldValue->CustomFieldValueId;
                                                         $blnInList = true;
                                                         break;
                                                     }
                                                 }
                                                 // Add the CustomFieldValue
                                                 if (!$blnInList && !in_array($strShortDescription, $strAddedCFVArray)) {
                                                     $strQuery = sprintf("INSERT INTO custom_field_value (custom_field_id, short_description, created_by, creation_date) VALUES (%s, '%s', %s, NOW());", $objCustomField->CustomFieldId, $strShortDescription, $_SESSION['intUserAccountId']);
                                                     $objDatabase->NonQuery($strQuery);
                                                     $strAddedCFVArray[] = $strShortDescription;
                                                 } elseif (!$blnInList && $this->lstMapDefaultValueArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedValue != null) {
                                                     //$intCustomFieldValueId = $this->lstMapDefaultValueArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedValue;
                                                     $strShortDescription = $this->lstMapDefaultValueArray[$intAssetCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedName;
                                                 }
                                                 if ($strShortDescription) {
                                                     $strCFVArray[$objCustomField->CustomFieldId] = sprintf("'%s'", $strShortDescription);
                                                 } else {
                                                     $strCFVArray[$objCustomField->CustomFieldId] = "NULL";
                                                 }
                                             }
                                         }
                                         $strAssetCodeArray[] = $strAssetCode;
                                         if (count($strCFVArray)) {
                                             $strUpdatedAssetCFVArray[$objAsset->AssetId] = $strCFVArray;
                                         } else {
                                             $strUpdatedAssetCFVArray[$objAsset->AssetId] = "";
                                         }
                                     }
                                 } else {
                                     $this->intSkippedRecordCount++;
                                     $this->PutSkippedRecordInFile($file_skipped, $strRowArray);
                                 }
                             } elseif ($this->lstImportAction->SelectedValue == 1 && $this->in_array_nocase($strAssetCode, $strAssetArray)) {
                                 // Skipped and flagged as duplicates
                                 $this->intSkippedRecordCount++;
                                 $this->PutSkippedRecordInFile($file_skipped, $strRowArray);
                             }
                         }
                     }
                     $intAssetCount = count($strAssetValuesArray);
                     if ($intAssetCount) {
                         $objDatabase = Asset::GetDatabase();
                         $strQuery = sprintf("INSERT INTO `asset` (`asset_code`, `location_id`, `asset_model_id`, `created_by`, `creation_date`) VALUES %s;", implode(", ", $strAssetValuesArray));
                         $objDatabase->NonQuery($strQuery);
                         $intInsertId = $objDatabase->InsertId();
                         if ($intInsertId) {
                             $strAssetIdArray = array();
                             for ($i = 0; $i < $intAssetCount; $i++) {
                                 $strAssetCFVArray[$i] = sprintf("('%s', %s)", $intInsertId + $i, $strAssetCFVArray[$i]);
                                 $strAssetIdArray[$i] = sprintf("(%s)", $intInsertId + $i);
                             }
                             $strCFVNameArray = array();
                             foreach ($arrAssetCustomField as $objCustomField) {
                                 $strCFVNameArray[] = sprintf("`cfv_%s`", $objCustomField->CustomFieldId);
                             }
                             if (count($strAssetCFVArray) > 0 && count($strCFVNameArray) > 0) {
                                 $strQuery = sprintf("INSERT INTO `asset_custom_field_helper` (`asset_id`, %s) VALUES %s", implode(", ", $strCFVNameArray), implode(", ", $strAssetCFVArray));
                             } else {
                                 $strQuery = sprintf("INSERT INTO `asset_custom_field_helper` (`asset_id`) VALUES %s", implode(", ", $strAssetIdArray));
                             }
                             $objDatabase->NonQuery($strQuery);
                             for ($i = 0; $i < $intAssetCount; $i++) {
                                 $this->objNewAssetArray[$intInsertId + $i] = $strAssetCodeArray[$i];
                             }
                         }
                     }
                     $intObjAssetCount = count($objAssetValuesArray);
                     if ($intObjAssetCount) {
                         $objDatabase = Asset::GetDatabase();
                         foreach ($objAssetValuesArray as $objAsset) {
                             $this->objUpdatedAssetArray[$objAsset->AssetId] = $objAsset->AssetCode;
                             if (count($strUpdatedAssetCFVArray[$objAsset->AssetId])) {
                                 $strCFVArray = array();
                                 foreach ($arrAssetCustomField as $objCustomField) {
                                     $strCFVArray[] = sprintf("`cfv_%s`=%s", $objCustomField->CustomFieldId, $strUpdatedAssetCFVArray[$objAsset->AssetId][$objCustomField->CustomFieldId]);
                                 }
                                 if (count($strCFVArray)) {
                                     $strQuery = sprintf("UPDATE `asset_custom_field_helper` SET %s WHERE `asset_id`='%s'", implode(", ", $strCFVArray), $objAsset->AssetId);
                                     $objDatabase->NonQuery($strQuery);
                                 }
                             }
                             $objAsset->Save();
                         }
                     }
                     $this->intCurrentFile++;
                     break;
                 }
                 //$j++;
             }
             if ($this->intImportStep == 6) {
                 /*if (count($this->strSelectedValueArray)) {
                     $objDatabase = CustomField::GetDatabase();
                     $strQuery = sprintf("INSERT INTO `custom_field_selection` " .
                                         "(`entity_id`,`entity_qtype_id`, `custom_field_value_id`) " .
                                         "VALUES %s;", implode(", ", $this->strSelectedValueArray));
                     $objDatabase->NonQuery($strQuery);
                   }*/
                 // Insert Values into helper tables
                 $objDatabase = Asset::GetDatabase();
                 $objDatabase->NonQuery("SET FOREIGN_KEY_CHECKS=0;");
                 // Insert into asset_model_custom_field_helper
                 $objDatabase->NonQuery(sprintf("INSERT INTO `asset_model_custom_field_helper` (`asset_model_id`) (SELECT `asset_model_id` FROM `asset_model` WHERE `asset_model_id` NOT IN (SELECT `asset_model_id` FROM `asset_model_custom_field_helper`));"));
                 // Insert into category_custom_field_helper
                 $objDatabase->NonQuery(sprintf("INSERT INTO `category_custom_field_helper` (`category_id`) (SELECT `category_id` FROM `category` WHERE `category_id` NOT IN (SELECT `category_id` FROM `category_custom_field_helper`));"));
                 // Insert into manufacturer_custom_field_helper
                 $objDatabase->NonQuery(sprintf("INSERT INTO `manufacturer_custom_field_helper` (`manufacturer_id`) (SELECT `manufacturer_id` FROM `manufacturer` WHERE `manufacturer_id` NOT IN (SELECT `manufacturer_id` FROM `manufacturer_custom_field_helper`));"));
                 // Inserts end
                 $objDatabase->NonQuery("SET FOREIGN_KEY_CHECKS=1;");
                 $this->blnImportEnd = true;
                 $this->btnNext->Warning = "";
                 $this->lblImportResults->Display = true;
                 if (count($this->objNewAssetArray)) {
                     $this->lblImportAssets->Display = true;
                     $this->dtgAsset->Paginator = new QPaginator($this->dtgAsset);
                     $this->dtgAsset->ItemsPerPage = 20;
                 }
                 if (count($this->objUpdatedAssetArray)) {
                     $this->lblImportUpdatedAssets->Display = true;
                     $this->dtgUpdatedAsset->Paginator = new QPaginator($this->dtgUpdatedAsset);
                     $this->dtgUpdatedAsset->ItemsPerPage = 20;
                 }
                 if (count($this->objNewAssetModelArray)) {
                     $this->lblImportModels->Display = true;
                     $this->dtgAssetModel->Paginator = new QPaginator($this->dtgAssetModel);
                     $this->dtgAssetModel->ItemsPerPage = 20;
                 }
                 if (count($this->objNewManufacturerArray)) {
                     $this->lblImportManufacturers->Display = true;
                     $this->dtgManufacturer->Paginator = new QPaginator($this->dtgManufacturer);
                     $this->dtgManufacturer->ItemsPerPage = 20;
                 }
                 if (count($this->objNewCategoryArray)) {
                     $this->lblImportCategories->Display = true;
                     $this->dtgCategory->Paginator = new QPaginator($this->dtgCategory);
                     $this->dtgCategory->ItemsPerPage = 20;
                 }
                 if (count($this->objNewLocationArray)) {
                     $this->lblImportLocations->Display = true;
                     $this->dtgLocation->Paginator = new QPaginator($this->dtgLocation);
                     $this->dtgLocation->ItemsPerPage = 20;
                 }
                 $this->btnNext->Display = false;
                 $this->btnCancel->Display = false;
                 $this->btnUndoLastImport->Display = true;
                 $this->btnImportMore->Display = true;
                 $this->btnReturnToAssets->Display = true;
                 $this->lblImportSuccess->Display = true;
                 $this->lblImportSuccess->Text = sprintf("Success:<br/>" . "<b>%s</b> Records imported successfully<br/>" . "<b>%s</b> Records skipped due to error<br/>", count($this->objNewAssetArray) + count($this->objUpdatedAssetArray), $this->intSkippedRecordCount);
                 if ($this->intSkippedRecordCount) {
                     $this->lblImportSuccess->Text .= sprintf("<a href='./asset_import.php?intDownloadCsv=1'>Click here to download records that could not be imported</a>");
                 }
                 $this->lblImportSuccess->Text .= "<br/><br/>";
                 $this->intImportStep = -1;
             }
             // Enable Next button
             $this->btnNext->Enabled = true;
             if (!$this->blnImportEnd && !$this->intCurrentFile) {
                 $this->intImportStep++;
             }
         }
         fclose($file_skipped);
     }
     if (!$blnError) {
         if (($this->blnImportEnd || $this->intImportStep == 2) && $this->intImportStep != -1) {
             $this->intStep++;
             $this->DisplayStepForm($this->intStep);
         }
         if (!$this->blnImportEnd) {
             QApplication::ExecuteJavaScript("document.getElementById('" . $this->btnNext->ControlId . "').click();");
         }
         if (!($this->intCurrentFile < count($this->strFilePathArray))) {
             $this->intCurrentFile = 0;
             $this->intImportStep++;
         }
     }
 }
Ejemplo n.º 3
0
 protected function dtgAsset_Bind()
 {
     // Because we want to enable pagination AND sorting, we need to setup the $objClauses array to send to LoadAll()
     // Remember!  We need to first set the TotalItemCount, which will affect the calcuation of LimitClause below
     $this->dtgAsset->TotalItemCount = Asset::CountAll();
     // Setup the $objClauses Array
     $objClauses = array();
     // If a column is selected to be sorted, and if that column has a OrderByClause set on it, then let's add
     // the OrderByClause to the $objClauses array
     if ($objClause = $this->dtgAsset->OrderByClause) {
         array_push($objClauses, $objClause);
     }
     // Add the LimitClause information, as well
     if ($objClause = $this->dtgAsset->LimitClause) {
         array_push($objClauses, $objClause);
     }
     // Set the DataSource to be the array of all Asset objects, given the clauses above
     $this->dtgAsset->DataSource = Asset::LoadAll($objClauses);
 }
Ejemplo n.º 4
0
 public function dtgAsset_Bind()
 {
     // Get Total Count b/c of Pagination
     $this->dtgAsset->TotalItemCount = Asset::CountAll();
     $objClauses = array();
     if ($objClause = $this->dtgAsset->OrderByClause) {
         array_push($objClauses, $objClause);
     }
     if ($objClause = $this->dtgAsset->LimitClause) {
         array_push($objClauses, $objClause);
     }
     $this->dtgAsset->DataSource = Asset::LoadAll($objClauses);
 }
Ejemplo n.º 5
0
 protected function btnNext_Click()
 {
     $blnError = false;
     if ($this->intStep == 1) {
         if ($this->chkHeaderRow->Checked) {
             $this->blnHeaderRow = true;
         } else {
             $this->blnHeaderRow = false;
         }
         // Check errors
         if ($this->lstFieldSeparator->SelectedValue == 'other' && !$this->txtFieldSeparator->Text) {
             $this->flcFileCsv->Warning = "Please enter the field separator.";
             $blnError = true;
         } elseif ($this->lstTextDelimiter->SelectedValue == 'other' && !$this->txtTextDelimiter->Text) {
             $this->flcFileCsv->Warning = "Please enter the text delimiter.";
             $blnError = true;
         } else {
             // Step 1 complete
             // File Not Uploaded
             if (!file_exists($this->flcFileCsv->File) || !$this->flcFileCsv->Size) {
                 //throw new QCallerException('FileAssetType must be a valid QFileAssetType constant value');
                 $this->flcFileCsv->Warning = 'The file could not be uploaded. Please provide a valid file.';
                 $blnError = true;
                 // File Has Incorrect MIME Type (only if an acceptiblemimearray is setup)
             } elseif (is_array($this->strAcceptibleMimeArray) && !array_key_exists($this->flcFileCsv->Type, $this->strAcceptibleMimeArray)) {
                 $this->flcFileCsv->Warning = "Extension must be 'csv' or 'txt'";
                 $blnError = true;
                 // File Successfully Uploaded
             } else {
                 $this->flcFileCsv->Warning = "";
                 // Setup Filename, Base Filename and Extension
                 $strFilename = $this->flcFileCsv->FileName;
                 $intPosition = strrpos($strFilename, '.');
             }
             if (!$blnError) {
                 $this->FileCsvData = new File_CSV_DataSource();
                 // Setup the settings which have got on step 1
                 $this->FileCsvData->settings($this->GetCsvSettings());
                 $file = fopen($this->flcFileCsv->File, "r");
                 // Counter of files
                 $i = 1;
                 // Counter of rows
                 $j = 1;
                 $this->strFilePathArray = array();
                 // The uploaded file splits up in order to avoid out of memory
                 while ($row = fgets($file, 1000)) {
                     if ($j == 1) {
                         $strFilePath = sprintf('%s/%s_mod_%s.csv', __TRACMOR_TMP__, $_SESSION['intUserAccountId'], $i);
                         $this->strFilePathArray[] = $strFilePath;
                         $file_part = fopen($strFilePath, "w+");
                         if ($i == 1) {
                             $strHeaderRow = $row;
                         } else {
                             fwrite($file_part, $strHeaderRow);
                         }
                     }
                     fwrite($file_part, $row);
                     $j++;
                     if ($j > 200) {
                         $j = 1;
                         $i++;
                         fclose($file_part);
                     }
                 }
                 $this->intTotalCount = ($i - 1) * 200 + $j - 1;
                 if (false) {
                     $blnError = true;
                     $this->btnNext->Warning = $i . " " . $j . "Sorry that is too many assets. Your asset limit is = " . QApplication::$TracmorSettings->AssetLimit . ", this import has " . $this->intTotalCount . " assets, and you already have " . Asset::CountAll() . " assets in the database.";
                 } else {
                     $this->arrMapFields = array();
                     $this->arrTracmorField = array();
                     // Load first file
                     $this->FileCsvData->load($this->strFilePathArray[0]);
                     $file_skipped = fopen($this->strFilePath = sprintf('%s/%s_skipped.csv', __TRACMOR_TMP__, $_SESSION['intUserAccountId']), "w+");
                     // Get Headers
                     if ($this->blnHeaderRow) {
                         $this->arrCsvHeader = $this->FileCsvData->getHeaders();
                         // Create the header row in the skipped error file
                         $this->PutSkippedRecordInFile($file_skipped, $this->arrCsvHeader);
                     }
                     /*else {
                         // If it is not first file
                         $this->FileCsvData->appendRow($this->FileCsvData->getHeaders());
                       }*/
                     $strFirstRowArray = $this->FileCsvData->getRow(0);
                     for ($i = 0; $i < count($strFirstRowArray); $i++) {
                         $this->arrMapFields[$i] = array();
                         if ($this->blnHeaderRow && array_key_exists($i, $this->arrCsvHeader)) {
                             if ($this->arrCsvHeader[$i] == '') {
                                 $this->arrCsvHeader[$i] = ' ';
                             }
                             $this->lstMapHeader_Create($this, $i, $this->arrCsvHeader[$i]);
                             $this->arrMapFields[$i]['header'] = $this->arrCsvHeader[$i];
                         } else {
                             $this->lstMapHeader_Create($this, $i);
                         }
                         // Create Default Value TextBox, ListBox and DateTimePicker
                         if ($this->blnHeaderRow && array_key_exists($i, $this->arrCsvHeader) && $this->arrCsvHeader[$i] || !$this->blnHeaderRow) {
                             $txtDefaultValue = new QTextBox($this);
                             $txtDefaultValue->Width = 200;
                             $this->txtMapDefaultValueArray[] = $txtDefaultValue;
                             $lstDefaultValue = new QListBox($this);
                             $lstDefaultValue->Width = 200;
                             $lstDefaultValue->Display = false;
                             $this->lstMapDefaultValueArray[] = $lstDefaultValue;
                             $dtpDate = new QDateTimePicker($this);
                             $dtpDate->DateTimePickerType = QDateTimePickerType::Date;
                             $dtpDate->DateTimePickerFormat = QDateTimePickerFormat::MonthDayYear;
                             $dtpDate->Display = false;
                             $this->dtpDateArray[] = $dtpDate;
                             if (array_key_exists($i, $this->lstMapHeaderArray)) {
                                 $this->lstTramorField_Change(null, $this->lstMapHeaderArray[$i]->ControlId, null);
                             }
                         }
                         $this->arrMapFields[$i]['row1'] = $strFirstRowArray[$i];
                     }
                     $this->btnNext->Text = "Import Now";
                     fclose($file_skipped);
                     // Create Add Field button
                     $btnAddField = new QButton($this);
                     $btnAddField->Text = "Add Field";
                     $btnAddField->AddAction(new QClickEvent(), new QServerAction('btnAddField_Click'));
                     $btnAddField->AddAction(new QEnterKeyEvent(), new QServerAction('btnAddField_Click'));
                     $btnAddField->AddAction(new QEnterKeyEvent(), new QTerminateAction());
                     $this->lstMapHeaderArray[] = $btnAddField;
                 }
             }
         }
     } elseif ($this->intStep == 2) {
         // Step 2 complete
         $blnError = false;
         $blnAssetModelCode = false;
         $blnAssetModelShortDescription = false;
         $blnCategory = false;
         $blnManufacturer = false;
         $blnModelId = false;
         // Checking errors (Model Short Description, Model Code, Category and Manufacturer must be selected)
         for ($i = 0; $i < count($this->lstMapHeaderArray) - 1; $i++) {
             $lstMapHeader = $this->lstMapHeaderArray[$i];
             $strSelectedValue = strtolower($lstMapHeader->SelectedValue);
             if ($strSelectedValue == "short description") {
                 $blnAssetModelShortDescription = true;
             } elseif ($strSelectedValue == "model number") {
                 $blnAssetModelCode = true;
             } elseif ($strSelectedValue == "category") {
                 $blnCategory = true;
             } elseif ($strSelectedValue == "manufacturer") {
                 $blnManufacturer = true;
             } elseif ($strSelectedValue == "id") {
                 $blnModelId = true;
             }
         }
         if ($this->lstMapDefaultValueArray) {
             // Checking errors for required Default Value text fields
             foreach ($this->lstMapDefaultValueArray as $lstDefault) {
                 if ($lstDefault->Display && $lstDefault->Required && !$lstDefault->SelectedValue) {
                     $lstDefault->Warning = "You must select one default value.";
                     $blnError = true;
                     break;
                 } else {
                     $blnError = false;
                     $lstDefault->Warning = "";
                 }
             }
         }
         if ($this->txtMapDefaultValueArray) {
             // Checking errors for required Default Value lst fields
             foreach ($this->txtMapDefaultValueArray as $txtDefault) {
                 if ($txtDefault->Display && $txtDefault->Required && !$txtDefault->Text) {
                     $txtDefault->Warning = "You must enter default value.";
                     $blnError = true;
                     break;
                 } else {
                     $blnError = false;
                     $txtDefault->Warning = "";
                 }
             }
         }
         // If all required fields have no errors
         if (!$blnError && $blnAssetModelCode && $blnAssetModelShortDescription && $blnCategory && $blnManufacturer && ($this->lstImportAction->SelectedValue != 2 || $blnModelId)) {
             $this->btnNext->Warning = "";
             // Setup keys for main required fields
             foreach ($this->arrTracmorField as $key => $value) {
                 if ($value == 'category') {
                     $this->intCategoryKey = $key;
                 } elseif ($value == 'manufacturer') {
                     $this->intManufacturerKey = $key;
                 } elseif (QApplication::$TracmorSettings->DepreciationFlag == '1' && $value == 'depreciation class') {
                     $this->intDepreciationKey = $key;
                 } elseif ($this->lstImportAction->SelectedValue == 2 && $value == 'id') {
                     $this->intItemIdKey = $key;
                 }
                 /*elseif ($value == 'created by') {
                     $this->intCreatedByKey = $key;
                   }
                   elseif ($value == 'created date') {
                     $this->intCreatedDateKey = $key;
                   }
                   elseif ($value == 'modified by') {
                     $this->intModifiedByKey = $key;
                   }
                   elseif ($value == 'modified date') {
                     $this->intModifiedDateKey = $key;
                   }*/
             }
             $this->objNewAssetModelArray = array();
             $this->strModelValuesArray = array();
             $this->blnImportEnd = false;
             $j = 1;
             $this->btnNext->RemoveAllActions('onclick');
             // Add new ajax actions for button
             $this->btnNext->AddAction(new QClickEvent(), new QAjaxAction('btnNext_Click'));
             $this->btnNext->AddAction(new QClickEvent(), new QToggleEnableAction($this->btnNext));
             $this->btnNext->AddAction(new QEnterKeyEvent(), new QAjaxAction('btnNext_Click'));
             $this->btnNext->AddAction(new QEnterKeyEvent(), new QToggleEnableAction($this->btnNext));
             $this->btnNext->AddAction(new QEnterKeyEvent(), new QTerminateAction());
             $this->btnNext->Warning = "Please wait...";
             $this->intImportStep = 2;
             $this->intCurrentFile = 0;
             $this->strSelectedValueArray = array();
             // New asset models
             $this->dtgAssetModel = new QDataGrid($this);
             $this->dtgAssetModel->Name = 'asset_model_list';
             $this->dtgAssetModel->CellPadding = 5;
             $this->dtgAssetModel->CellSpacing = 0;
             $this->dtgAssetModel->CssClass = "datagrid";
             $this->dtgAssetModel->UseAjax = true;
             $this->dtgAssetModel->ShowColumnToggle = false;
             $this->dtgAssetModel->ShowExportCsv = false;
             $this->dtgAssetModel->ShowHeader = false;
             $this->dtgAssetModel->AddColumn(new QDataGridColumnExt('Model', '<?= $_ITEM ?>', 'CssClass="dtg_column"', 'HtmlEntities="false"'));
             // Updated assets
             $this->dtgUpdatedAsset = new QDataGrid($this);
             $this->dtgUpdatedAsset->Name = 'updated_asset_list';
             $this->dtgUpdatedAsset->CellPadding = 5;
             $this->dtgUpdatedAsset->CellSpacing = 0;
             $this->dtgUpdatedAsset->CssClass = "datagrid";
             $this->dtgUpdatedAsset->UseAjax = true;
             $this->dtgUpdatedAsset->ShowColumnToggle = false;
             $this->dtgUpdatedAsset->ShowExportCsv = false;
             $this->dtgUpdatedAsset->ShowHeader = false;
             $this->dtgUpdatedAsset->AddColumn(new QDataGridColumnExt('Asset Tag', '<?= $_ITEM ?>', 'CssClass="dtg_column"', 'HtmlEntities="false"'));
             // Create the label for successful import
             $this->lblImportSuccess = new QLabel($this);
             $this->lblImportSuccess->HtmlEntities = false;
             $this->lblImportSuccess->Display = false;
             // Undo Last Import button
             $this->btnUndoLastImport = new QButton($this);
             $this->btnUndoLastImport->Text = "Undo Last Import";
             $this->btnUndoLastImport->Display = false;
             $this->btnUndoLastImport->AddAction(new QClickEvent(), new QServerAction('btnCancel_Click'));
             $this->btnUndoLastImport->AddAction(new QEnterKeyEvent(), new QServerAction('btnCancel_Click'));
             $this->btnUndoLastImport->AddAction(new QEnterKeyEvent(), new QTerminateAction());
             // Import More button
             $this->btnImportMore = new QButton($this);
             $this->btnImportMore->Text = "Import More";
             $this->btnImportMore->Display = false;
             $this->btnImportMore->AddAction(new QClickEvent(), new QServerAction('btnImportMore_Click'));
             $this->btnImportMore->AddAction(new QEnterKeyEvent(), new QServerAction('btnImportMore_Click'));
             $this->btnImportMore->AddAction(new QEnterKeyEvent(), new QTerminateAction());
             // Return to Assets button
             $this->btnReturnToAssets = new QButton($this);
             $this->btnReturnToAssets->Text = "Return to Models";
             $this->btnReturnToAssets->Display = false;
             $this->btnReturnToAssets->AddAction(new QClickEvent(), new QServerAction('btnReturnToAssets_Click'));
             $this->btnReturnToAssets->AddAction(new QEnterKeyEvent(), new QServerAction('btnReturnToAssets_Click'));
             $this->btnReturnToAssets->AddAction(new QEnterKeyEvent(), new QTerminateAction());
         } else {
             $this->btnNext->Warning = "You must select all required fields (Model Number, Model Short Description, Category and Manufacturer).";
             $blnError = true;
         }
     } else {
         // Step 3 complete
         set_time_limit(0);
         $file_skipped = fopen($strFilePath = sprintf('%s/%s_skipped.csv', __TRACMOR_TMP__, $_SESSION['intUserAccountId']), "a");
         if (!$this->blnImportEnd) {
             // Asset Model
             if ($this->intImportStep == 2) {
                 $intCategoryArray = array();
                 // Load all categories with key=category_id
                 foreach (Category::LoadAllWithFlags(true, false) as $objCategory) {
                     $intCategoryArray[$objCategory->CategoryId] = strtolower($objCategory->ShortDescription);
                 }
                 $intManufacturerArray = array();
                 // Load all manufacturers with key=manufacturer_id
                 foreach (Manufacturer::LoadAll() as $objManufacturer) {
                     $intManufacturerArray[$objManufacturer->ManufacturerId] = strtolower($objManufacturer->ShortDescription);
                 }
                 if (QApplication::$TracmorSettings->DepreciationFlag == '1') {
                     $intDepreciationClassArray = array();
                     foreach (DepreciationClass::LoadAll() as $objDepreciationClass) {
                         $intDepreciationClassArray[$objDepreciationClass->DepreciationClassId] = strtolower($objDepreciationClass->ShortDescription);
                     }
                 }
                 $intModelCustomFieldKeyArray = array();
                 $arrModelCustomField = array();
                 // Setup keys
                 foreach ($this->arrTracmorField as $key => $value) {
                     if ($value == 'short description') {
                         $intModelShortDescriptionKey = $key;
                     } elseif ($value == 'long description') {
                         $intModelLongDescriptionKey = $key;
                     } elseif ($value == 'model number') {
                         $intModelCodeKey = $key;
                     } elseif (substr($value, 0, 6) == 'model_') {
                         $intModelCustomFieldKeyArray[substr($value, 6)] = $key;
                         if (array_key_exists(substr($value, 6), $this->arrModelCustomField)) {
                             $arrModelCustomField[substr($value, 6)] = $this->arrModelCustomField[substr($value, 6)];
                         }
                     }
                 }
                 $strAssetModelArray = array();
                 $strItemCFVArray = array();
                 $strUpdatedItemCFVArray = array();
                 $strUpdatedValuesArray = array();
                 $this->arrOldItemArray = array();
                 $this->objUpdatedItemArray = array();
                 // Load all asset models
                 foreach (AssetModel::LoadAllIntoExtendedArray() as $arrAssetModel) {
                     $strAssetModelArray[] = strtolower(sprintf("%s_%s_%s_%s", addslashes($arrAssetModel['model_code']), addslashes($arrAssetModel['short_description']), $arrAssetModel['category_id'], $arrAssetModel['manufacturer_id']));
                 }
                 $this->btnNext->Warning = sprintf("Please wait... Model import complete: %s%s", ceil(($this->intCurrentFile + 1) * 200 / $this->intTotalCount * 100), "%");
             }
             // Asset
             /*elseif ($this->intImportStep == 5) {
                         $intCategoryArray = array();
                         // Load all categories with keys=category_id
                         foreach (Category::LoadAllWithFlags(true, false) as $objCategory) {
                           //$intCategoryArray["'" . strtolower($objCategory->ShortDescription) . "'"] = $objCategory->CategoryId;
                           $intCategoryArray[$objCategory->CategoryId] = strtolower($objCategory->ShortDescription);
                         }
                         $intManufacturerArray = array();
                         // Load all manufacturers with keys=manufacturer_id
                         foreach (Manufacturer::LoadAll() as $objManufacturer) {
                           //$intManufacturerArray["'" . strtolower($objManufacturer->ShortDescription) . "'"] = $objManufacturer->ManufacturerId;
                           $intManufacturerArray[$objManufacturer->ManufacturerId] = strtolower($objManufacturer->ShortDescription);
                         }
                         if ($this->intCurrentFile == 0) {
                           $this->intAssetModelArray = array();
                           // Load all asset models with keys=asset_model_id
                           foreach (AssetModel::LoadAll() as $objAssetModel) {
                             //$intAssetModelArray["'" . strtolower($objAssetModel->ShortDescription) . "'"] = $objAssetModel->AssetModelId;
                             $this->intAssetModelArray[$objAssetModel->AssetModelId] = strtolower(sprintf("%s_%s_%s_%s", $objAssetModel->AssetModelCode, $objAssetModel->ShortDescription, $objAssetModel->CategoryId, $objAssetModel->ManufacturerId));
                           }
                         }
                         $intAssetCustomFieldKeyArray = array();
                         $arrAssetCustomField = array();
                         // Setup keys
                         foreach ($this->arrTracmorField as $key => $value) {
                           if ($value == 'asset model short description') {
                             $intModelShortDescriptionKey = $key;
                           }
                           elseif ($value == 'asset model code') {
                             $intModelCodeKey = $key;
                           }
                           elseif ($value == 'asset code') {
                             $intAssetCode = $key;
                           }
                           elseif (substr($value, 0, 6) == 'asset_') {
                             $intAssetCustomFieldKeyArray[substr($value, 6)] = $key;
                             if (array_key_exists(substr($value, 6), $this->arrAssetCustomField)) {
                             	$arrAssetCustomField[substr($value, 6)] = $this->arrAssetCustomField[substr($value, 6)];
                             }
                           }
                         }
                         $intLocationArray = array();
                         // Load all locations with keys=location_id
                         foreach (Location::LoadAll() as $objLocation) {
                           //$intLocationArray["'" . strtolower($objLocation->ShortDescription) . "'"] = $objLocation->LocationId;
                           $intLocationArray[$objLocation->LocationId] = strtolower($objLocation->ShortDescription);
                         }
             
                         $strAssetArray = array();
                         $strUpdatedAssetArray = array();
                         // Load all assets
                         foreach (Asset::LoadAll() as $objAsset) {
                           $strAssetArray[] = strtolower($objAsset->AssetCode);
                         }
                         $this->btnNext->Warning = sprintf("Please wait... Asset import complete: %s%s", ceil(($this->intCurrentFile+1)*200/$this->intTotalCount*100), "%");
                       }*/
             // Loads array of AssetModelId
             $arrAssetModelArray = AssetModel::LoadAllIntoArray();
             $arrAssetModelId = array();
             if (count($arrAssetModelArray)) {
                 foreach ($arrAssetModelArray as $arrAssetModel) {
                     $arrAssetModelId[$arrAssetModel['asset_model_id']] = true;
                 }
             }
             for ($j = $this->intCurrentFile; $j < count($this->strFilePathArray); $j++) {
                 $this->FileCsvData->load($this->strFilePathArray[$j]);
                 if (!$j) {
                     //$this->FileCsvData->appendRow($this->FileCsvData->getHeaders());
                 }
                 if ($this->intImportStep == 2) {
                     $objNewAssetModelArray = array();
                     for ($i = 0; $i < $this->FileCsvData->countRows(); $i++) {
                         $strRowArray = $this->FileCsvData->getRow($i);
                         if (isset($strRowArray[$intModelShortDescriptionKey])) {
                             $strShortDescription = trim($strRowArray[$intModelShortDescriptionKey]) ? addslashes(trim($strRowArray[$intModelShortDescriptionKey])) : false;
                         } elseif (isset($this->txtMapDefaultValueArray[$intModelShortDescriptionKey])) {
                             $strShortDescription = $this->txtMapDefaultValueArray[$intModelShortDescriptionKey]->Text;
                         } else {
                             $strShortDescription = false;
                         }
                         //$strShortDescription = (trim($strRowArray[$intModelShortDescriptionKey])) ? trim($strRowArray[$intModelShortDescriptionKey]) : false;
                         $strAssetModelCode = trim($strRowArray[$intModelCodeKey]) ? addslashes(trim($strRowArray[$intModelCodeKey])) : addslashes(trim($this->txtMapDefaultValueArray[$intModelCodeKey]->Text));
                         //$strAssetModelCode = trim($strRowArray[$intModelCodeKey]) ? trim($strRowArray[$intModelCodeKey]) : trim($this->txtMapDefaultValueArray[$intModelCodeKey]->Text);
                         $strKeyArray = array_keys($intCategoryArray, isset($strRowArray[$this->intCategoryKey]) ? strtolower(trim($strRowArray[$this->intCategoryKey])) : array());
                         if (count($strKeyArray)) {
                             $intCategoryId = $strKeyArray[0];
                         } else {
                             $strKeyArray = array_keys($intCategoryArray, strtolower(trim($this->txtMapDefaultValueArray[$this->intCategoryKey]->Text)));
                             if (count($strKeyArray)) {
                                 $intCategoryId = $strKeyArray[0];
                             } else {
                                 $intCategoryId = false;
                             }
                         }
                         $strKeyArray = array_keys($intManufacturerArray, isset($strRowArray[$this->intManufacturerKey]) ? strtolower(trim($strRowArray[$this->intManufacturerKey])) : array());
                         if (count($strKeyArray)) {
                             $intManufacturerId = $strKeyArray[0];
                         } else {
                             $strKeyArray = array_keys($intManufacturerArray, strtolower(trim($this->txtMapDefaultValueArray[$this->intManufacturerKey]->Text)));
                             if (count($strKeyArray)) {
                                 $intManufacturerId = $strKeyArray[0];
                             } else {
                                 $intManufacturerId = false;
                             }
                         }
                         // depreciation
                         if (QApplication::$TracmorSettings->DepreciationFlag == '1' && $this->intDepreciationKey != null) {
                             $strKeyArray = array_keys($intDepreciationClassArray, strtolower(trim($strRowArray[$this->intDepreciationKey])));
                             if (count($strKeyArray)) {
                                 $intDepreciationId = $strKeyArray[0];
                             } else {
                                 $strKeyArray = array_keys($intDepreciationClassArray, strtolower(trim($this->txtMapDefaultValueArray[$this->intDepreciationKey]->Text)));
                                 if (count($strKeyArray)) {
                                     $intDepreciationId = $strKeyArray[0];
                                 } else {
                                     if (trim($strRowArray[$this->intDepreciationKey]) == '') {
                                         // Depreciation class is blank, so null it out
                                         $intDepreciationId = 'NULL';
                                     } else {
                                         // Depreciation class is invalid, so skip this record
                                         $intDepreciationId = false;
                                     }
                                 }
                             }
                         } else {
                             $intDepreciationId = null;
                         }
                         //
                         $objAssetModel = false;
                         if (!$strShortDescription || $intCategoryId === false || $intManufacturerId === false || $intDepreciationId === false) {
                             //$blnError = true;
                             //echo sprintf("Desc: %s AssetCode: %s Cat: %s Man: %s<br/>", $strShortDescription, $strAssetModelCode, $intCategoryId, $intManufacturerId);
                             //break;
                             $strAssetModel = null;
                             $this->intSkippedRecordCount++;
                             $this->PutSkippedRecordInFile($file_skipped, $strRowArray);
                             continue;
                         } else {
                             //$blnError = false;
                             $strAssetModel = strtolower(sprintf("%s_%s_%s_%s", $strAssetModelCode, $strShortDescription, $intCategoryId, $intManufacturerId));
                             if ($this->lstImportAction->SelectedValue == 2) {
                                 $intItemId = intval(trim($strRowArray[$this->intItemIdKey]));
                                 if ($intItemId > 0 && array_key_exists($intItemId, $arrAssetModelId)) {
                                     $objAssetModelArray = AssetModel::LoadArrayBySearchHelper(null, null, null, null, null, null, null, null, null, null, null, null, $intItemId);
                                     if ($objAssetModelArray) {
                                         $objAssetModel = $objAssetModelArray[0];
                                     }
                                 }
                             } else {
                                 $intItemId = 0;
                             }
                         }
                         if ($strAssetModel && !$intItemId && !$this->in_array_nocase($strAssetModel, $strAssetModelArray)) {
                             // Custom Fields Section
                             $strCFVArray = array();
                             $objDatabase = CustomField::GetDatabase();
                             $blnCheckCFVError = false;
                             // Asset Model Custom Field import
                             foreach ($arrModelCustomField as $objCustomField) {
                                 if ($objCustomField->CustomFieldQtypeId != 2) {
                                     $strCSDescription = trim($strRowArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]);
                                     $strCSDescription = strlen($strCSDescription) > 0 ? addslashes($strCSDescription) : addslashes($this->txtMapDefaultValueArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]->Text);
                                     $strCFVArray[$objCustomField->CustomFieldId] = strlen($strCSDescription) > 0 ? sprintf("'%s'", $strCSDescription) : "NULL";
                                 } else {
                                     $objDatabase = AssetModel::GetDatabase();
                                     $strCSDescription = addslashes(trim($strRowArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]));
                                     $blnInList = false;
                                     foreach (CustomFieldValue::LoadArrayByCustomFieldId($objCustomField->CustomFieldId) as $objCustomFieldValue) {
                                         if (strtolower($objCustomFieldValue->ShortDescription) == strtolower($strCSDescription)) {
                                             //$intCustomFieldValueId = $objCustomFieldValue->CustomFieldValueId;
                                             $blnInList = true;
                                             break;
                                         }
                                     }
                                     // Add the CustomFieldValue
                                     // Removed adding new 'select' values
                                     /*if (!$blnInList && !in_array($strCSDescription, $strAddedCFVArray)) {
                                     			$strQuery = sprintf("INSERT INTO custom_field_value (custom_field_id, short_description, created_by, creation_date) VALUES (%s, '%s', %s, NOW());", $objCustomField->CustomFieldId, $strCSDescription, $_SESSION['intUserAccountId']);
                                     			$objDatabase->NonQuery($strQuery);
                                     			$strAddedCFVArray[] = $strCSDescription;
                                     		}
                                     		else*/
                                     if (!$blnInList && $this->lstMapDefaultValueArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedValue != null) {
                                         $strCSDescription = $this->lstMapDefaultValueArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedName;
                                     } elseif (!$blnInList) {
                                         $blnCheckCFVError = true;
                                         break;
                                     }
                                     if (!$blnCheckCFVError) {
                                         if ($strCSDescription) {
                                             $strCFVArray[$objCustomField->CustomFieldId] = sprintf("'%s'", $strCSDescription);
                                         } else {
                                             $strCFVArray[$objCustomField->CustomFieldId] = "NULL";
                                         }
                                     }
                                 }
                             }
                             if (!$blnCheckCFVError) {
                                 $strAssetModelArray[] = $strAssetModel;
                                 // $this->strModelValuesArray[] = sprintf("('%s', '%s', '%s', '%s', '%s', '%s',  NOW())", $strShortDescription, (isset($intModelLongDescriptionKey)) ? addslashes(trim($strRowArray[$intModelLongDescriptionKey])) : null, $strAssetModelCode, $intCategoryId, $intManufacturerId, $_SESSION['intUserAccountId']);
                                 $this->strModelValuesArray[] = sprintf("('%s', '%s', '%s', '%s', '%s', '%s', NOW(), %s)", $strShortDescription, isset($intModelLongDescriptionKey) ? addslashes(trim($strRowArray[$intModelLongDescriptionKey])) : null, $strAssetModelCode, $intCategoryId, $intManufacturerId, $_SESSION['intUserAccountId'], $intDepreciationId ? $intDepreciationId : 'NULL');
                                 $objNewAssetModelArray[] = $strShortDescription;
                                 if (isset($strCFVArray) && count($strCFVArray)) {
                                     $strModelCFVArray[] = implode(', ', $strCFVArray);
                                 } else {
                                     $strModelCFVArray[] = "";
                                 }
                             } else {
                                 $this->intSkippedRecordCount++;
                                 $this->PutSkippedRecordInFile($file_skipped, $strRowArray);
                                 $strAssetModel = null;
                             }
                         } elseif ($strAssetModel && $this->lstImportAction->SelectedValue == 2 && $objAssetModel) {
                             $strUpdateFieldArray = array();
                             $strUpdateFieldArray[] = sprintf("`manufacturer_id`='%s'", $intManufacturerId);
                             $strUpdateFieldArray[] = sprintf("`category_id`='%s'", $intCategoryId);
                             $strUpdateFieldArray[] = sprintf("`short_description`='%s'", $strShortDescription);
                             $strUpdateFieldArray[] = sprintf("`asset_model_code`='%s'", $strAssetModelCode);
                             if (QApplication::$TracmorSettings->DepreciationFlag == '1' && $this->intDepreciationKey != null && $intDepreciationId != null) {
                                 $strUpdateFieldArray[] = sprintf("`depreciation_class_id` = %s", $intDepreciationId);
                             }
                             $strModelLongDescription = "";
                             if (isset($intModelLongDescription)) {
                                 if (trim($strRowArray[$intModelLongDescriptionKey])) {
                                     $strModelLongDescription = trim($strRowArray[$intModelLongDescriptionKey]);
                                 } else {
                                     $strModelLongDescription = isset($txtMapDefaultValueArray[$intModelLongDescriptionKey]) ? trim($txtMapDefaultValueArray[$intModelLongDescriptionKey]->Text) : '';
                                 }
                                 $strUpdateFieldArray[] = sprintf("`asset_model_long_description`='%s'", $strModelLongDescription);
                             }
                             $strUpdateFieldArray[] = sprintf("modified_by='%s'", $_SESSION['intUserAccountId']);
                             $blnCheckCFVError = false;
                             foreach ($arrModelCustomField as $objCustomField) {
                                 if ($objCustomField->CustomFieldQtypeId != 2) {
                                     $strCSDescription = isset($strRowArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]) ? trim($strRowArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]) : "";
                                     $strCSDescription = strlen($strCSDescription) > 0 ? addslashes($strCSDescription) : addslashes($this->txtMapDefaultValueArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]->Text);
                                     $strCFVArray[$objCustomField->CustomFieldId] = strlen($strCSDescription) > 0 ? sprintf("'%s'", $strCSDescription) : "NULL";
                                 } else {
                                     $objDatabase = CustomField::GetDatabase();
                                     $strCSDescription = isset($strRowArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]) ? addslashes(trim($strRowArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]])) : "";
                                     $strCFVArray[$objCustomField->CustomFieldId] = $strCSDescription ? sprintf("'%s'", $strCSDescription) : "NULL";
                                     $blnInList = false;
                                     foreach (CustomFieldValue::LoadArrayByCustomFieldId($objCustomField->CustomFieldId) as $objCustomFieldValue) {
                                         if (strtolower($objCustomFieldValue->ShortDescription) == strtolower($strCSDescription)) {
                                             //$intItemKeyntCustomFieldValueId = $objCustomFieldValue->CustomFieldValueId;
                                             $blnInList = true;
                                             break;
                                         }
                                     }
                                     if (!$blnInList && $this->lstMapDefaultValueArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedValue != null) {
                                         $strCSDescription = $this->lstMapDefaultValueArray[$intModelCustomFieldKeyArray[$objCustomField->CustomFieldId]]->SelectedName;
                                     } elseif (!$blnInList) {
                                         $blnCheckCFVError = true;
                                         break;
                                     }
                                     if (!$blnCheckCFVError) {
                                         if ($strCSDescription) {
                                             $strCFVArray[$objCustomField->CustomFieldId] = sprintf("'%s'", $strCSDescription);
                                         } else {
                                             $strCFVArray[$objCustomField->CustomFieldId] = "NULL";
                                         }
                                     }
                                 }
                             }
                             if (!$blnCheckCFVError) {
                                 $strUpdatedValuesArray[] = sprintf("UPDATE `asset_model` SET %s WHERE `asset_model_id`='%s'", implode(", ", $strUpdateFieldArray), $objAssetModel->AssetModelId);
                                 if (isset($strCFVArray) && count($strCFVArray)) {
                                     $strUpdatedItemCFVArray[$objAssetModel->AssetModelId] = $strCFVArray;
                                 } else {
                                     $strUpdatedItemCFVArray[$objAssetModel->AssetModelId] = "";
                                 }
                                 $this->objUpdatedItemArray[$objAssetModel->AssetModelId] = sprintf("%s", $objAssetModel->ShortDescription);
                                 //$this->arrOldItemArray[$objAssetModel->AssetModelId] = $objAssetModel;
                                 // $strItemQuery = sprintf("UPDATE `asset_model` SET `short_description`='%s', `long_description`='%s', `manufacturer_id`='%s', `category_id`='%s', `asset_model_code`='%s', `modified_by`=%s, `modified_date`=%s WHERE `asset_model_id`='%s'", $objAssetModel->ShortDescription, $objAssetModel->LongDescription, $objAssetModel->ManufacturerId, $objAssetModel->CategoryId, $objAssetModel->AssetModelCode, (!$objAssetModel->ModifiedBy) ? "NULL" : $objAssetModel->ModifiedBy, (!$objAssetModel->ModifiedBy) ? "NULL" : sprintf("'%s'", $objAssetModel->ModifiedDate), $objAssetModel->AssetModelId);
                                 $strItemQuery = sprintf("UPDATE `asset_model` SET `short_description`='%s', `long_description`='%s', `manufacturer_id`='%s', `category_id`='%s', `asset_model_code`='%s', `modified_by`=%s, `modified_date`=%s, `depreciation_class_id`=%s WHERE `asset_model_id`='%s'", $objAssetModel->ShortDescription, $objAssetModel->LongDescription, $objAssetModel->ManufacturerId, $objAssetModel->CategoryId, $objAssetModel->AssetModelCode, !$objAssetModel->ModifiedBy ? "NULL" : $objAssetModel->ModifiedBy, !$objAssetModel->ModifiedBy ? "NULL" : sprintf("'%s'", $objAssetModel->ModifiedDate), $objAssetModel->DepreciationClassId ? sprintf("'%s'", $objAssetModel->DepreciationClassId) : "NULL", $objAssetModel->AssetModelId);
                                 $strCFVArray = array();
                                 foreach ($this->arrModelCustomField as $objCustomField) {
                                     $strCFV = $objAssetModel->GetVirtualAttribute($objCustomField->CustomFieldId);
                                     $strCFVArray[] = sprintf("`cfv_%s`='%s'", $objCustomField->CustomFieldId, $strCFV);
                                 }
                                 if (isset($strCFVArray) && count($strCFVArray)) {
                                     $strCFVQuery = sprintf("UPDATE `asset_model_custom_field_helper` SET %s WHERE `asset_model_id`='%s'", implode(", ", $strCFVArray), $intItemId);
                                 } else {
                                     $strCFVQuery = false;
                                 }
                                 $this->arrOldItemArray[$objAssetModel->AssetModelId] = array("ItemSql" => $strItemQuery, "CFVSql" => $strCFVQuery);
                             }
                         } else {
                             $this->intSkippedRecordCount++;
                             $this->PutSkippedRecordInFile($file_skipped, $strRowArray);
                         }
                     }
                     //if ($this->intCurrentFile == count($this->strFilePathArray)) {
                     // Inserts
                     if (count($this->strModelValuesArray)) {
                         $objDatabase = AssetModel::GetDatabase();
                         //	print sprintf("INSERT INTO `asset_model` (`short_description`, `long_description`, `asset_model_code`, `category_id`, `manufacturer_id`, `created_by`, `creation_date`, `depreciation_class_id`) VALUES %s;", str_replace('""','"', implode(", ", $this->strModelValuesArray)));
                         //   exit();
                         //  $objDatabase->NonQuery(sprintf("INSERT INTO `asset_model` (`short_description`, `long_description`, `asset_model_code`, `category_id`, `manufacturer_id`, `created_by`, `creation_date`) VALUES %s;", str_replace('""','"', implode(", ", $this->strModelValuesArray))));
                         $objDatabase->NonQuery(sprintf("INSERT INTO `asset_model` (`short_description`, `long_description`, `asset_model_code`, `category_id`, `manufacturer_id`, `created_by`, `creation_date`, `depreciation_class_id`) VALUES %s;", str_replace('""', '"', implode(", ", $this->strModelValuesArray))));
                         $intInsertId = $objDatabase->InsertId();
                         if ($intInsertId) {
                             // Update for asset custom fields with allAssetModesF Flag
                             $arrAllAssetModelsFlaggedObjects = EntityQtypeCustomField::LoadArrayByEntityQtypeId(QApplication::Translate(EntityQtype::Asset));
                             foreach ($arrAllAssetModelsFlaggedObjects as $arrAllAssetModelsFlaggedObject) {
                                 if ($arrAllAssetModelsFlaggedObject->CustomField->AllAssetModelsFlag) {
                                     $newAssetCustomField = new AssetCustomFieldAssetModel();
                                     $newAssetCustomField->CustomFieldId = $arrAllAssetModelsFlaggedObject->CustomField->CustomFieldId;
                                     $newAssetCustomField->AssetModelId = $intInsertId;
                                     $newAssetCustomField->Save();
                                 }
                             }
                             $strModelIdArray = array();
                             $strCFVArray = array();
                             for ($i = 0; $i < count($objNewAssetModelArray); $i++) {
                                 $this->objNewAssetModelArray[$intInsertId + $i] = $objNewAssetModelArray[$i];
                                 $strCFVArray[$i] = sprintf("('%s', %s)", $intInsertId + $i, $strModelCFVArray[$i]);
                                 $strModelIdArray[$i] = sprintf("(%s)", $intInsertId + $i);
                             }
                             $strCFVNameArray = array();
                             foreach ($arrModelCustomField as $objCustomField) {
                                 $strCFVNameArray[] = sprintf("`cfv_%s`", $objCustomField->CustomFieldId);
                             }
                             if (count($strModelCFVArray) > 0 && count($strCFVNameArray) > 0) {
                                 $strQuery = sprintf("INSERT INTO `asset_model_custom_field_helper` (`asset_model_id`, %s) VALUES %s", implode(", ", $strCFVNameArray), implode(", ", $strCFVArray));
                             } else {
                                 $strQuery = sprintf("INSERT INTO `asset_model_custom_field_helper` (`asset_model_id`) VALUES %s", implode(", ", $strModelIdArray));
                             }
                             $objDatabase->NonQuery($strQuery);
                         }
                         $this->strModelValuesArray = array();
                     }
                     // Updates
                     if (count($strUpdatedValuesArray)) {
                         $objDatabase = AssetModel::GetDatabase();
                         foreach ($strUpdatedValuesArray as $query) {
                             $objDatabase->NonQuery($query);
                         }
                         foreach ($this->objUpdatedItemArray as $intItemKey => $objUpdatedItem) {
                             if (isset($strUpdatedItemCFVArray[$intItemKey]) && count($strUpdatedItemCFVArray[$intItemKey])) {
                                 $strCFVArray = array();
                                 foreach ($arrModelCustomField as $objCustomField) {
                                     $strCFVArray[] = sprintf("`cfv_%s`=%s", $objCustomField->CustomFieldId, $strUpdatedItemCFVArray[$intItemKey][$objCustomField->CustomFieldId]);
                                 }
                                 if (isset($strCFVArray) && count($strCFVArray)) {
                                     $strQuery = sprintf("UPDATE `asset_model_custom_field_helper` SET %s WHERE `asset_model_id`='%s'", implode(", ", $strCFVArray), $intItemKey);
                                     $objDatabase->NonQuery($strQuery);
                                 }
                             }
                         }
                     }
                     //}
                     //$this->intCurrentFile++;
                     //break;
                 }
                 //$j++;
             }
             if ($this->intImportStep == 3) {
                 /*if (count($this->strSelectedValueArray)) {
                     $objDatabase = CustomField::GetDatabase();
                     $strQuery = sprintf("INSERT INTO `custom_field_selection` " .
                                         "(`entity_id`,`entity_qtype_id`, `custom_field_value_id`) " .
                                         "VALUES %s;", implode(", ", $this->strSelectedValueArray));
                     $objDatabase->NonQuery($strQuery);
                   }*/
                 // Insert Values into helper tables
                 $objDatabase = Asset::GetDatabase();
                 $objDatabase->NonQuery("SET FOREIGN_KEY_CHECKS=0;");
                 // Insert into asset_model_custom_field_helper
                 $objDatabase->NonQuery(sprintf("INSERT INTO `asset_model_custom_field_helper` (`asset_model_id`) (SELECT `asset_model_id` FROM `asset_model` WHERE `asset_model_id` NOT IN (SELECT `asset_model_id` FROM `asset_model_custom_field_helper`));"));
                 // Inserts end
                 $objDatabase->NonQuery("SET FOREIGN_KEY_CHECKS=1;");
                 $this->blnImportEnd = true;
                 $this->btnNext->Warning = "";
                 $this->lblImportResults->Display = true;
                 if (count($this->objNewAssetArray)) {
                     $this->lblImportAssets->Display = true;
                     $this->dtgAsset->Paginator = new QPaginator($this->dtgAsset);
                     $this->dtgAsset->ItemsPerPage = 20;
                 }
                 if (count($this->objUpdatedAssetArray)) {
                     $this->lblImportUpdatedAssets->Display = true;
                     $this->dtgUpdatedAsset->Paginator = new QPaginator($this->dtgUpdatedAsset);
                     $this->dtgUpdatedAsset->ItemsPerPage = 20;
                 }
                 if (count($this->objNewAssetModelArray)) {
                     $this->lblImportModels->Display = true;
                     $this->dtgAssetModel->Paginator = new QPaginator($this->dtgAssetModel);
                     $this->dtgAssetModel->ItemsPerPage = 20;
                 }
                 $this->btnNext->Display = false;
                 $this->btnCancel->Display = false;
                 $this->btnUndoLastImport->Display = true;
                 $this->btnImportMore->Display = true;
                 $this->btnReturnToAssets->Display = true;
                 $this->lblImportSuccess->Display = true;
                 $this->lblImportSuccess->Text = sprintf("Success:<br/>" . "<b>%s</b> Records imported successfully<br/>" . "<b>%s</b> Records skipped due to error<br/>", count($this->objNewAssetModelArray) + count($this->objUpdatedItemArray), $this->intSkippedRecordCount);
                 if ($this->intSkippedRecordCount) {
                     $this->lblImportSuccess->Text .= sprintf("<a href='./asset_model_import.php?intDownloadCsv=1'>Click here to download records that could not be imported</a>");
                 }
                 $this->lblImportSuccess->Text .= "<br/><br/>";
                 $this->intImportStep = -1;
             }
             // Enable Next button
             $this->btnNext->Enabled = true;
             if (!$this->blnImportEnd && !$this->intCurrentFile) {
                 $this->intImportStep++;
             }
         }
         fclose($file_skipped);
     }
     if (!$blnError) {
         if (($this->blnImportEnd || $this->intImportStep == 2) && $this->intImportStep != -1) {
             $this->intStep++;
             $this->DisplayStepForm($this->intStep);
         }
         if (!$this->blnImportEnd) {
             QApplication::ExecuteJavaScript("document.getElementById('" . $this->btnNext->ControlId . "').click();");
         }
         if (!($this->intCurrentFile < count($this->strFilePathArray))) {
             $this->intCurrentFile = 0;
             $this->intImportStep++;
         }
     }
 }
Ejemplo n.º 6
0
 protected function Form_Create()
 {
     if (QApplication::QueryString('intDownloadCsv')) {
         $this->RenderBegin(false);
         session_cache_limiter('must-revalidate');
         // force a "no cache" effect
         header("Pragma: hack");
         // IE chokes on "no cache", so set to something, anything, else.
         $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time()) . " GMT";
         header($ExpStr);
         header('Content-Type: text/csv');
         header('Content-Disposition: csv; filename=skipped_records.csv');
         $file = fopen(sprintf("%s%s/%s_skipped.csv", __DOCROOT__ . __SUBDIRECTORY__, __TRACMOR_TMP__, $_SESSION['intUserAccountId']), "r");
         ob_end_clean();
         while ($row = fgets($file, 1000)) {
             print $row;
             @ob_flush();
             flush();
         }
         QApplication::$JavaScriptArray = array();
         QApplication::$JavaScriptArrayHighPriority = array();
         $this->RenderEnd(false);
         exit;
     }
     // Create the Header Menu
     $this->ctlHeaderMenu_Create();
     $intRoleId = QApplication::$objUserAccount->RoleId;
     $this->blnError = true;
     $objRoleEntityQtypeBuiltInAuthorization = RoleEntityQtypeBuiltInAuthorization::LoadByRoleIdEntityQtypeIdAuthorizationId($intRoleId, EntityQtype::Asset, 2);
     $this->intAssetLimit = QApplication::$TracmorSettings->AssetLimit;
     $this->intAssetCount = Asset::CountAll();
     // Check the user have edit permissions
     if ($objRoleEntityQtypeBuiltInAuthorization && $objRoleEntityQtypeBuiltInAuthorization->AuthorizedFlag) {
         $this->blnError = false;
     }
     if (!$this->blnError) {
         $this->pnlMain_Create();
         $this->pnlStepOne_Create();
         $this->Buttons_Create();
         $this->intStep = 1;
         $this->intSkippedRecordCount = 0;
         $this->blnImportEnd = true;
         $this->btnRemoveArray = array();
         $this->Labels_Create();
         $this->objDatabase = Asset::GetDatabase();
         $this->intItemIdKey = null;
         $this->objUpdatedItemArray = array();
         $this->arrAssetCustomField = array();
         $intCustomFieldIdArray = array();
         // Load Asset Model Custom Field
         foreach (CustomField::LoadArrayByActiveFlagEntity(1, EntityQtype::Asset) as $objCustomField) {
             $this->arrAssetCustomField[$objCustomField->CustomFieldId] = $objCustomField;
             $intCustomFieldIdArray[] = $objCustomField->CustomFieldId;
         }
         if (count($intCustomFieldIdArray)) {
             //QApplication::$Database[1]->EnableProfiling();
             // Load restrict permisions for Asset Cutom Fields
             $objConditions = QQ::AndCondition(QQ::Equal(QQN::RoleEntityQtypeCustomFieldAuthorization()->RoleId, (string) $intRoleId), QQ::In(QQN::RoleEntityQtypeCustomFieldAuthorization()->EntityQtypeCustomField->CustomFieldId, $intCustomFieldIdArray), QQ::Equal(QQN::RoleEntityQtypeCustomFieldAuthorization()->AuthorizedFlag, false));
             $objClauses = array();
             array_push($objClauses, QQ::Expand(QQN::RoleEntityQtypeCustomFieldAuthorization()->EntityQtypeCustomField->EntityQtypeCustomFieldId));
             array_push($objClauses, QQ::OrderBy(QQN::RoleEntityQtypeCustomFieldAuthorization()->EntityQtypeCustomFieldId));
             $arrRoleEntityQtypeCustomFieldAuthorization = RoleEntityQtypeCustomFieldAuthorization::QueryArray($objConditions, $objClauses);
             if ($arrRoleEntityQtypeCustomFieldAuthorization) {
                 foreach ($arrRoleEntityQtypeCustomFieldAuthorization as $objRoleAuth) {
                     if (array_key_exists($objRoleAuth->EntityQtypeCustomField->CustomFieldId, $this->arrAssetCustomField)) {
                         unset($this->arrAssetCustomField[$objRoleAuth->EntityQtypeCustomField->CustomFieldId]);
                     }
                 }
             }
             //QApplication::$Database[1]->OutputProfiling();
         }
         $this->intUserArray = array();
         // Load Users
         /*foreach (UserAccount::LoadAll() as $objUser) {
         			$this->intUserArray[strtolower($objUser->Username)] = $objUser->UserAccountId;
         		}*/
         $this->strAcceptibleMimeArray = array('text/plain' => 'txt', 'text/comma-separated-values' => 'csv', 'text/csv' => 'csv', 'text/x-comma-separated-values' => 'csv', 'application/vnd.ms-excel' => 'csv', 'application/csv' => 'csv', 'text/x-csv' => 'csv');
     }
 }