Beispiel #1
0
 /**
  * Import a SJIS encoded file, and check if getNextRow() properly
  * converts all the data into UTF-8
  */
 public function testFileImportEncoding()
 {
     $importFile = new ImportFile($this->_file, ',', '"', FALSE, FALSE);
     $row = $importFile->getNextRow();
     // Hardcode some Japanese strings
     $this->assertEquals('名前', $row[0]);
     $this->assertEquals('請求先郵便番号', $row[10]);
     $this->assertEquals('年間売上', $row[20]);
     $this->assertEquals('チームID', $row[30]);
 }
 public function testGetFileRowCount()
 {
     $if1 = new ImportFile($this->_fileSample1, ',', "\"", FALSE);
     $if2 = new ImportFile($this->_fileSample2, ',', "\"", FALSE);
     $if3 = new ImportFile($this->_fileSample3, ',', "\"", FALSE);
     $if4 = new ImportFile($this->_fileSample4, ',', "\"", FALSE);
     $this->assertEquals($this->_fileLineCount1, $if1->getNumberOfLinesInfile());
     $this->assertEquals($this->_fileLineCount2, $if2->getNumberOfLinesInfile());
     $this->assertEquals($this->_fileLineCount3, $if3->getNumberOfLinesInfile());
     $this->assertEquals($this->_fileLineCount4, $if4->getNumberOfLinesInfile());
 }
 /** 
  * @see SugarView::display()
  */
 public function display()
 {
     global $mod_strings, $sugar_config;
     // Check to be sure we are getting an import file that is in the right place
     if (realpath(dirname($_REQUEST['tmp_file']) . '/') != realpath($sugar_config['upload_dir'])) {
         trigger_error($mod_strings['LBL_CANNOT_OPEN'], E_USER_ERROR);
     }
     // Open the import file
     $importSource = new ImportFile($_REQUEST['tmp_file'], $_REQUEST['custom_delimiter'], html_entity_decode($_REQUEST['custom_enclosure'], ENT_QUOTES));
     //Ensure we have a valid file.
     if (!$importSource->fileExists()) {
         trigger_error($mod_strings['LBL_CANNOT_OPEN'], E_USER_ERROR);
     }
     $importer = new Importer($importSource, $this->bean);
     $importer->import();
 }
Beispiel #4
0
 public function __construct()
 {
     parent::__construct();
     $this->tableName = 'account_statement';
     $this->allowServerFile = false;
     $this->duplicateControl = false;
     $this->dateFormat = true;
     $this->decimalSeparator = true;
     $this->ignoreEmptyRows = true;
     $this->presets = [['name' => 'Osuuspankki', 'selections' => ['charset' => 1, 'format' => 0, 'field_delim' => 1, 'enclosure_char' => 0, 'row_delim' => 0, 'date_format' => 0], 'mappings' => ['map_column1' => 1, 'map_column2' => 2, 'map_column7' => 3], 'values' => ['decimal_separator' => ',', 'skip_rows' => '0']], ['name' => 'Nordea', 'selections' => ['charset' => 0, 'format' => 0, 'field_delim' => 2, 'enclosure_char' => 2, 'row_delim' => 1, 'date_format' => 0], 'mappings' => ['map_column1' => 1, 'map_column2' => 2, 'map_column8' => 3], 'values' => ['decimal_separator' => ',', 'skip_rows' => '1']]];
 }
Beispiel #5
0
 /**
  * @see SugarView::display()
  */
 public function display()
 {
     global $mod_strings, $sugar_config;
     // Check to be sure we are getting an import file that is in the right place
     $uploadFile = "upload://" . basename($_REQUEST['tmp_file']);
     if (!file_exists($uploadFile)) {
         trigger_error($mod_strings['LBL_CANNOT_OPEN'], E_USER_ERROR);
     }
     // Open the import file
     $importSource = new ImportFile($uploadFile, $_REQUEST['custom_delimiter'], html_entity_decode($_REQUEST['custom_enclosure'], ENT_QUOTES));
     //Ensure we have a valid file.
     if (!$importSource->fileExists()) {
         trigger_error($mod_strings['LBL_CANNOT_OPEN'], E_USER_ERROR);
     }
     if (!ImportCacheFiles::ensureWritable()) {
         trigger_error($mod_strings['LBL_ERROR_IMPORT_CACHE_NOT_WRITABLE'], E_USER_ERROR);
     }
     $importer = new Importer($importSource, $this->bean);
     $importer->import();
 }
 /**
  * Actually split the file into parts
  *
  * @param string $delimiter
  * @param string $enclosure
  * @param bool $has_header true if file has a header row
  */
 public function splitSourceFile($delimiter = ',', $enclosure = '"', $has_header = false)
 {
     if (!$this->fileExists()) {
         return false;
     }
     $importFile = new ImportFile($this->_sourceFile, $delimiter, $enclosure, false);
     $filecount = 0;
     $fw = sugar_fopen("{$this->_sourceFile}-{$filecount}", "w");
     $count = 0;
     // skip first row if we have a header row
     if ($has_header && $importFile->getNextRow()) {
         // mark as duplicate to stick header row in the dupes file
         $importFile->markRowAsDuplicate();
         // same for error records file
         $importFile->writeErrorRecord();
     }
     while ($row = $importFile->getNextRow()) {
         // after $this->_recordThreshold rows, close this import file and goto the next one
         if ($count >= $this->_recordThreshold) {
             fclose($fw);
             $filecount++;
             $fw = sugar_fopen("{$this->_sourceFile}-{$filecount}", "w");
             $count = 0;
         }
         // Bug 25119: Trim the enclosure string to remove any blank spaces that may have been added.
         $enclosure = trim($enclosure);
         if (!empty($enclosure)) {
             foreach ($row as $key => $v) {
                 $row[$key] = preg_replace("/{$enclosure}/", "{$enclosure}{$enclosure}", $v);
             }
         }
         $line = $enclosure . implode($enclosure . $delimiter . $enclosure, $row) . $enclosure . PHP_EOL;
         //Would normally use fputcsv() here. But when enclosure character is used and the field value doesn't include delimiter, enclosure, escape character, "\n", "\r", "\t", or " ", php default function 'fputcsv' will not use enclosure for this string.
         fputs($fw, $line);
         $count++;
     }
     fclose($fw);
     $this->_fileCount = $filecount;
     $this->_recordCount = $filecount * $this->_recordThreshold + $count;
     // increment by one to get true count of files created
     ++$this->_fileCount;
 }
Beispiel #7
0
     header('Content-Transfer-Encoding: binary');
     header('Expires: 0');
     header('Cache-Control: must-revalidate');
     header('Pragma: public');
     header('Content-Length: ' . filesize($file));
     readfile($file);
     exit;
 }
 if (isset($_REQUEST['submit-import'])) {
     $file = $_FILES['import-file']["name"];
     $table = $_POST['import-table'];
     $format = $_FILES['import-file']["type"];
     echo $format;
     if (in_array($_FILES['import-file']["type"], array('application/vnd.ms-excel', 'text/xml', 'application/octet-stream'))) {
         if (move_uploaded_file($_FILES['import-file']['tmp_name'], "files/" . $_FILES['import-file']["name"])) {
             $import = new ImportFile("mysql", "127.0.0.1", "3306", "loft", "dake", "boromir", $table);
             switch ($format) {
                 case "text/xml":
                     $data = XmlFileFormat::retrieveArrFromFile("files" . DIRECTORY_SEPARATOR . $file);
                     break;
                 case "application/vnd.ms-excel":
                     $data = CsvFileFormat::retrieveArrFromFile("files" . DIRECTORY_SEPARATOR . $file);
                     $data = $import->convertInAssocArr($data, ImportExportBase::users_clmns);
                     break;
                 case "application/octet-stream":
                     $data = JsonFileFormat::retrieveArrFromFile("files" . DIRECTORY_SEPARATOR . $file);
                     break;
             }
             $import->writeAssosArrByTable($data);
         }
     }
Beispiel #8
0
 /**
  * @see SugarView::display()
  */
 public function display()
 {
     global $mod_strings, $app_strings, $current_user;
     global $sugar_config, $locale;
     $this->ss->assign("IMPORT_MODULE", $_REQUEST['import_module']);
     $this->ss->assign("TYPE", !empty($_REQUEST['type']) ? $_REQUEST['type'] : "import");
     $this->ss->assign("SOURCE_ID", $_REQUEST['source_id']);
     $this->instruction = 'LBL_SELECT_PROPERTY_INSTRUCTION';
     $this->ss->assign('INSTRUCTION', $this->getInstruction());
     $this->ss->assign("MODULE_TITLE", $this->getModuleTitle(false), ENT_NOQUOTES);
     $this->ss->assign("CURRENT_STEP", $this->currentStep);
     $sugar_config['import_max_records_per_file'] = empty($sugar_config['import_max_records_per_file']) ? 1000 : $sugar_config['import_max_records_per_file'];
     $importSource = isset($_REQUEST['source']) ? $_REQUEST['source'] : 'csv';
     // Clear out this user's last import
     $seedUsersLastImport = BeanFactory::getBean('Import_2');
     $seedUsersLastImport->mark_deleted_by_user_id($current_user->id);
     ImportCacheFiles::clearCacheFiles();
     // handle uploaded file
     $uploadFile = new UploadFile('userfile');
     if (isset($_FILES['userfile']) && $uploadFile->confirm_upload()) {
         $uploadFile->final_move('IMPORT_' . $this->bean->object_name . '_' . $current_user->id);
         $uploadFileName = $uploadFile->get_upload_path('IMPORT_' . $this->bean->object_name . '_' . $current_user->id);
     } elseif (!empty($_REQUEST['tmp_file'])) {
         $uploadFileName = "upload://" . basename($_REQUEST['tmp_file']);
     } else {
         $this->_showImportError($mod_strings['LBL_IMPORT_MODULE_ERROR_NO_UPLOAD'], $_REQUEST['import_module'], 'Step2', true, null, true);
         return;
     }
     //check the file size, we dont want to process an empty file
     if (isset($_FILES['userfile']['size']) && $_FILES['userfile']['size'] == 0) {
         //this file is empty, throw error message
         $this->_showImportError($mod_strings['LBL_NO_LINES'], $_REQUEST['import_module'], 'Step2', false, null, true);
         return;
     }
     $mimeTypeOk = true;
     //check to see if the file mime type is not a form of text or application octed streramand fire error if not
     if (isset($_FILES['userfile']['type']) && strpos($_FILES['userfile']['type'], 'octet-stream') === false && strpos($_FILES['userfile']['type'], 'text') === false && strpos($_FILES['userfile']['type'], 'application/vnd.ms-excel') === false) {
         //this file does not have a known text or application type of mime type, issue the warning
         $error_msgs[] = $mod_strings['LBL_MIME_TYPE_ERROR_1'];
         $error_msgs[] = $mod_strings['LBL_MIME_TYPE_ERROR_2'];
         $this->_showImportError($error_msgs, $_REQUEST['import_module'], 'Step2', true, $mod_strings['LBL_OK']);
         $mimeTypeOk = false;
     }
     $this->ss->assign("FILE_NAME", $uploadFileName);
     // Now parse the file and look for errors
     $importFile = new ImportFile($uploadFileName, $_REQUEST['custom_delimiter'], html_entity_decode($_REQUEST['custom_enclosure'], ENT_QUOTES), FALSE);
     if ($this->shouldAutoDetectProperties($importSource)) {
         $GLOBALS['log']->debug("Auto detecing csv properties...");
         $autoDetectOk = $importFile->autoDetectCSVProperties();
         $importFileMap = array();
         $this->ss->assign("SOURCE", 'csv');
         if ($autoDetectOk === FALSE) {
             //show error only if previous mime type check has passed
             if ($mimeTypeOk) {
                 $this->ss->assign("AUTO_DETECT_ERROR", $mod_strings['LBL_AUTO_DETECT_ERROR']);
             }
         } else {
             $dateFormat = $importFile->getDateFormat();
             $timeFormat = $importFile->getTimeFormat();
             if ($dateFormat) {
                 $importFileMap['importlocale_dateformat'] = $dateFormat;
             }
             if ($timeFormat) {
                 $importFileMap['importlocale_timeformat'] = $timeFormat;
             }
         }
     } else {
         $impotMapSeed = $this->getImportMap($importSource);
         $importFile->setImportFileMap($impotMapSeed);
         $importFileMap = $impotMapSeed->getMapping($_REQUEST['import_module']);
     }
     $delimeter = $importFile->getFieldDelimeter();
     $enclosure = $importFile->getFieldEnclosure();
     $hasHeader = $importFile->hasHeaderRow();
     $encodeOutput = TRUE;
     //Handle users navigating back through the wizard.
     if (!empty($_REQUEST['previous_action']) && $_REQUEST['previous_action'] == 'Confirm') {
         $encodeOutput = FALSE;
         $importFileMap = $this->overloadImportFileMapFromRequest($importFileMap);
         $delimeter = !empty($_REQUEST['custom_delimiter']) ? $_REQUEST['custom_delimiter'] : $delimeter;
         $enclosure = isset($_REQUEST['custom_enclosure']) ? $_REQUEST['custom_enclosure'] : $enclosure;
         $enclosure = html_entity_decode($enclosure, ENT_QUOTES);
         $hasHeader = !empty($_REQUEST['has_header']) ? $_REQUEST['has_header'] : $hasHeader;
         if ($hasHeader == 'on') {
             $hasHeader = true;
         } else {
             if ($hasHeader == 'off') {
                 $hasHeader = false;
             }
         }
     }
     $this->ss->assign("IMPORT_ENCLOSURE_OPTIONS", $this->getEnclosureOptions($enclosure));
     $this->ss->assign("IMPORT_DELIMETER_OPTIONS", $this->getDelimeterOptions($delimeter));
     $this->ss->assign("CUSTOM_DELIMITER", $delimeter);
     $this->ss->assign("CUSTOM_ENCLOSURE", htmlentities($enclosure, ENT_QUOTES, 'utf-8'));
     $hasHeaderFlag = $hasHeader ? " CHECKED" : "";
     $this->ss->assign("HAS_HEADER_CHECKED", $hasHeaderFlag);
     if (!$importFile->fileExists()) {
         $this->_showImportError($mod_strings['LBL_CANNOT_OPEN'], $_REQUEST['import_module'], 'Step2', false, null, true);
         return;
     }
     //Check if we will exceed the maximum number of records allowed per import.
     $maxRecordsExceeded = FALSE;
     $maxRecordsWarningMessg = "";
     $lineCount = $importFile->getNumberOfLinesInfile();
     $maxLineCount = isset($sugar_config['import_max_records_total_limit']) ? $sugar_config['import_max_records_total_limit'] : 5000;
     if (!empty($maxLineCount) && $lineCount > $maxLineCount) {
         $maxRecordsExceeded = TRUE;
         $maxRecordsWarningMessg = string_format($mod_strings['LBL_IMPORT_ERROR_MAX_REC_LIMIT_REACHED'], array($lineCount, $maxLineCount));
     }
     //Retrieve a sample set of data
     $rows = $this->getSampleSet($importFile);
     $this->ss->assign('column_count', $this->getMaxColumnsInSampleSet($rows));
     $this->ss->assign('HAS_HEADER', $importFile->hasHeaderRow(FALSE));
     $this->ss->assign('getNumberJs', $locale->getNumberJs());
     $this->setImportFileCharacterSet($importFile, $importFileMap);
     $this->setDateTimeProperties($importFileMap);
     $this->setCurrencyOptions($importFileMap);
     $this->setNumberFormatOptions($importFileMap);
     $this->setNameFormatProperties($importFileMap);
     $importMappingJS = $this->getImportMappingJS();
     $this->ss->assign("SAMPLE_ROWS", $rows);
     $JS = $this->_getJS($maxRecordsExceeded, $maxRecordsWarningMessg, $importMappingJS, $importFileMap);
     $this->ss->assign("JAVASCRIPT", $JS);
     $content = $this->ss->fetch('modules/Import/tpls/confirm.tpl');
     $this->ss->assign("CONTENT", $content);
     $this->ss->display('modules/Import/tpls/wizardWrapper.tpl');
 }
 /**
  * @see SugarFieldBase::importSanitize()
  */
 public function importSanitize($value, $vardef, $focus, ImportFieldSanitize $settings)
 {
     if (!isset($vardef['module'])) {
         return false;
     }
     $newbean = loadBean($vardef['module']);
     // Bug 38885 - If we are relating to the Users table on user_name, there's a good chance
     // that the related field data is the full_name, rather than the user_name. So to be sure
     // let's try to lookup the field the relationship is expecting to use (user_name).
     if ($vardef['module'] == 'Users' && isset($vardef['rname']) && $vardef['rname'] == 'user_name') {
         $userFocus = new User();
         $query = sprintf("SELECT user_name FROM {$userFocus->table_name} WHERE %s=%s AND deleted=0", $userFocus->db->concat('users', array('first_name', 'last_name')), $userFocus->db->quoted($value));
         $username = $userFocus->db->getOne($query);
         if (!empty($username)) {
             $value = $username;
         }
     }
     // Bug 32869 - Assumed related field name is 'name' if it is not specified
     if (!isset($vardef['rname'])) {
         $vardef['rname'] = 'name';
     }
     // Bug 27046 - Validate field against type as it is in the related field
     $rvardef = $newbean->getFieldDefinition($vardef['rname']);
     if (isset($rvardef['type']) && method_exists($this, $rvardef['type'])) {
         $fieldtype = $rvardef['type'];
         $returnValue = $settings->{$fieldtype}($value, $rvardef);
         if (!$returnValue) {
             return false;
         } else {
             $value = $returnValue;
         }
     }
     if (isset($vardef['id_name'])) {
         $idField = $vardef['id_name'];
         // Bug 24075 - clear out id field value if it is invalid
         if (isset($focus->{$idField})) {
             $checkfocus = loadBean($vardef['module']);
             if ($checkfocus && is_null($checkfocus->retrieve($focus->{$idField}))) {
                 $focus->{$idField} = '';
             }
         }
         // be sure that the id isn't already set for this row
         if (empty($focus->{$idField}) && $idField != $vardef['name'] && !empty($vardef['rname']) && !empty($vardef['table'])) {
             // Bug 27562 - Check db_concat_fields first to see if the field name is a concat
             $relatedFieldDef = $newbean->getFieldDefinition($vardef['rname']);
             if (isset($relatedFieldDef['db_concat_fields']) && is_array($relatedFieldDef['db_concat_fields'])) {
                 $fieldname = $focus->db->concat($vardef['table'], $relatedFieldDef['db_concat_fields']);
             } else {
                 $fieldname = $vardef['rname'];
             }
             // lookup first record that matches in linked table
             $query = "SELECT id\n                            FROM {$vardef['table']}\n                            WHERE {$fieldname} = '" . $focus->db->quote($value) . "'\n                                AND deleted != 1";
             $result = $focus->db->limitQuery($query, 0, 1, true, "Want only a single row");
             if (!empty($result)) {
                 if ($relaterow = $focus->db->fetchByAssoc($result)) {
                     $focus->{$idField} = $relaterow['id'];
                 } elseif (!$settings->addRelatedBean || $newbean->bean_implements('ACL') && !$newbean->ACLAccess('save') || in_array($newbean->module_dir, array('Teams', 'Users'))) {
                     return false;
                 } else {
                     // add this as a new record in that bean, then relate
                     if (isset($relatedFieldDef['db_concat_fields']) && is_array($relatedFieldDef['db_concat_fields'])) {
                         $relatedFieldParts = explode(' ', $value);
                         foreach ($relatedFieldDef['db_concat_fields'] as $relatedField) {
                             $newbean->{$relatedField} = array_shift($relatedFieldParts);
                         }
                     } else {
                         $newbean->{$vardef}['rname'] = $value;
                     }
                     if (!isset($focus->assigned_user_id) || $focus->assigned_user_id == '') {
                         $newbean->assigned_user_id = $GLOBALS['current_user']->id;
                     } else {
                         $newbean->assigned_user_id = $focus->assigned_user_id;
                     }
                     if (!isset($focus->modified_user_id) || $focus->modified_user_id == '') {
                         $newbean->modified_user_id = $GLOBALS['current_user']->id;
                     } else {
                         $newbean->modified_user_id = $focus->modified_user_id;
                     }
                     // populate fields from the parent bean to the child bean
                     $focus->populateRelatedBean($newbean);
                     $newbean->save(false);
                     $focus->{$idField} = $newbean->id;
                     $settings->createdBeans[] = ImportFile::writeRowToLastImport($focus->module_dir, $newbean->object_name, $newbean->id);
                 }
             }
         }
     }
     return $value;
 }
Beispiel #10
0
 public function testWriteRowToLastImport()
 {
     $file = SugarTestImportUtilities::createFile(3, 2);
     $importFile = new ImportFile($file, ',', '"');
     $record = $importFile->writeRowToLastImport("Tests", "Test", "TestRunner");
     $query = "SELECT * \n                        FROM users_last_import\n                        WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'\n                            AND import_module = 'Tests'\n                            AND bean_type = 'Test'\n                            AND bean_id = 'TestRunner'\n                            AND id = '{$record}'\n                            AND deleted=0";
     $result = $GLOBALS['db']->query($query);
     $this->assertNotNull($GLOBALS['db']->fetchByAssoc($result));
     $query = "DELETE FROM users_last_import\n                        WHERE assigned_user_id = '{$GLOBALS['current_user']->id}'\n                            AND import_module = 'Tests'\n                            AND bean_type = 'Test'\n                            AND bean_id = 'TestRunner'\n                            AND id = '{$record}'\n                            AND deleted=0";
     $GLOBALS['db']->query($query);
 }
 function action_RefreshTable()
 {
     $offset = isset($_REQUEST['offset']) ? $_REQUEST['offset'] : 0;
     $tableID = isset($_REQUEST['tableID']) ? $_REQUEST['tableID'] : 'errors';
     $has_header = $_REQUEST['has_header'] == 'on' ? TRUE : FALSE;
     if ($tableID == 'dup') {
         $tableFilename = ImportCacheFiles::getDuplicateFileName();
     } else {
         $tableFilename = ImportCacheFiles::getErrorRecordsFileName();
     }
     $if = new ImportFile($tableFilename, ",", '"', FALSE, FALSE);
     $if->setHeaderRow($has_header);
     $lv = new ImportListView($if, array('offset' => $offset), $tableID);
     $lv->display(FALSE);
     sugar_cleanup(TRUE);
 }
Beispiel #12
0
 /**
  * @see SugarView::display()
  */
 public function display()
 {
     global $mod_strings, $app_strings, $current_user, $sugar_config, $app_list_strings, $locale;
     $this->ss->assign("IMPORT_MODULE", $_REQUEST['import_module']);
     $has_header = isset($_REQUEST['has_header']) ? 1 : 0;
     $sugar_config['import_max_records_per_file'] = empty($sugar_config['import_max_records_per_file']) ? 1000 : $sugar_config['import_max_records_per_file'];
     $this->ss->assign("CURRENT_STEP", $this->currentStep);
     // attempt to lookup a preexisting field map
     // use the custom one if specfied to do so in step 1
     $mapping_file = new ImportMap();
     $field_map = $mapping_file->set_get_import_wizard_fields();
     $default_values = array();
     $ignored_fields = array();
     if (!empty($_REQUEST['source_id'])) {
         $GLOBALS['log']->fatal("Loading import map properties.");
         $mapping_file = new ImportMap();
         $mapping_file->retrieve($_REQUEST['source_id'], false);
         $_REQUEST['source'] = $mapping_file->source;
         $has_header = $mapping_file->has_header;
         if (isset($mapping_file->delimiter)) {
             $_REQUEST['custom_delimiter'] = $mapping_file->delimiter;
         }
         if (isset($mapping_file->enclosure)) {
             $_REQUEST['custom_enclosure'] = htmlentities($mapping_file->enclosure);
         }
         $field_map = $mapping_file->getMapping();
         //print_r($field_map);die();
         $default_values = $mapping_file->getDefaultValues();
         $this->ss->assign("MAPNAME", $mapping_file->name);
         $this->ss->assign("CHECKMAP", 'checked="checked" value="on"');
     } else {
         $classname = $this->getMappingClassName(ucfirst($_REQUEST['source']));
         //Set the $_REQUEST['source'] to be 'other' for ImportMapOther special case
         if ($classname == 'ImportMapOther') {
             $_REQUEST['source'] = 'other';
         }
         if (class_exists($classname)) {
             $mapping_file = new $classname();
             $ignored_fields = $mapping_file->getIgnoredFields($_REQUEST['import_module']);
             $field_map2 = $mapping_file->getMapping($_REQUEST['import_module']);
             $field_map = array_merge($field_map, $field_map2);
         }
     }
     $delimiter = $this->getRequestDelimiter();
     $this->ss->assign("CUSTOM_DELIMITER", $delimiter);
     $this->ss->assign("CUSTOM_ENCLOSURE", !empty($_REQUEST['custom_enclosure']) ? $_REQUEST['custom_enclosure'] : "");
     //populate import locale  values from import mapping if available, these values will be used througout the rest of the code path
     $uploadFileName = $_REQUEST['file_name'];
     // Now parse the file and look for errors
     $importFile = new ImportFile($uploadFileName, $delimiter, html_entity_decode($_REQUEST['custom_enclosure'], ENT_QUOTES), FALSE);
     if (!$importFile->fileExists()) {
         $this->_showImportError($mod_strings['LBL_CANNOT_OPEN'], $_REQUEST['import_module'], 'Step2');
         return;
     }
     $charset = $importFile->autoDetectCharacterSet();
     // retrieve first 3 rows
     $rows = array();
     //Keep track of the largest row count found.
     $maxFieldCount = 0;
     for ($i = 0; $i < 3; $i++) {
         $rows[] = $importFile->getNextRow();
         $maxFieldCount = $importFile->getFieldCount() > $maxFieldCount ? $importFile->getFieldCount() : $maxFieldCount;
     }
     $ret_field_count = $maxFieldCount;
     // Bug 14689 - Parse the first data row to make sure it has non-empty data in it
     $isempty = true;
     if ($rows[(int) $has_header] != false) {
         foreach ($rows[(int) $has_header] as $value) {
             if (strlen(trim($value)) > 0) {
                 $isempty = false;
                 break;
             }
         }
     }
     if ($isempty || $rows[(int) $has_header] == false) {
         $this->_showImportError($mod_strings['LBL_NO_LINES'], $_REQUEST['import_module'], 'Step2');
         return;
     }
     // save first row to send to step 4
     $this->ss->assign("FIRSTROW", base64_encode(serialize($rows[0])));
     // Now build template
     $this->ss->assign("TMP_FILE", $uploadFileName);
     $this->ss->assign("SOURCE", $_REQUEST['source']);
     $this->ss->assign("TYPE", $_REQUEST['type']);
     $this->ss->assign("DELETE_INLINE_PNG", SugarThemeRegistry::current()->getImage('basic_search', 'align="absmiddle" alt="' . $app_strings['LNK_DELETE'] . '" border="0"'));
     $this->ss->assign("PUBLISH_INLINE_PNG", SugarThemeRegistry::current()->getImage('advanced_search', 'align="absmiddle" alt="' . $mod_strings['LBL_PUBLISH'] . '" border="0"'));
     $this->instruction = 'LBL_SELECT_MAPPING_INSTRUCTION';
     $this->ss->assign('INSTRUCTION', $this->getInstruction());
     $this->ss->assign("MODULE_TITLE", $this->getModuleTitle(false));
     $this->ss->assign("STEP4_TITLE", strip_tags(str_replace("\n", "", getClassicModuleTitle($mod_strings['LBL_MODULE_NAME'], array($mod_strings['LBL_MODULE_NAME'], $mod_strings['LBL_STEP_4_TITLE']), false))));
     $this->ss->assign("HEADER", $app_strings['LBL_IMPORT'] . " " . $mod_strings['LBL_MODULE_NAME']);
     // we export it as email_address, but import as email1
     $field_map['email_address'] = 'email1';
     // build each row; row count is determined by the the number of fields in the import file
     $columns = array();
     $mappedFields = array();
     // this should be populated if the request comes from a 'Back' button click
     $importColumns = $this->getImportColumns();
     $column_sel_from_req = false;
     if (!empty($importColumns)) {
         $column_sel_from_req = true;
     }
     for ($field_count = 0; $field_count < $ret_field_count; $field_count++) {
         // See if we have any field map matches
         $defaultValue = "";
         // Bug 31260 - If the data rows have more columns than the header row, then just add a new header column
         if (!isset($rows[0][$field_count])) {
             $rows[0][$field_count] = '';
         }
         // See if we can match the import row to a field in the list of fields to import
         $firstrow_name = trim(str_replace(":", "", $rows[0][$field_count]));
         if ($has_header && isset($field_map[$firstrow_name])) {
             $defaultValue = $field_map[$firstrow_name];
         } elseif (isset($field_map[$field_count])) {
             $defaultValue = $field_map[$field_count];
         } elseif (empty($_REQUEST['source_id'])) {
             $defaultValue = trim($rows[0][$field_count]);
         }
         // build string of options
         $fields = $this->bean->get_importable_fields();
         $options = array();
         $defaultField = '';
         global $current_language;
         $moduleStrings = return_module_language($current_language, $this->bean->module_dir);
         foreach ($fields as $fieldname => $properties) {
             // get field name
             if (!empty($moduleStrings['LBL_EXPORT_' . strtoupper($fieldname)])) {
                 $displayname = str_replace(":", "", $moduleStrings['LBL_EXPORT_' . strtoupper($fieldname)]);
             } else {
                 if (!empty($properties['vname'])) {
                     $displayname = str_replace(":", "", translate($properties['vname'], $this->bean->module_dir));
                 } else {
                     $displayname = str_replace(":", "", translate($properties['name'], $this->bean->module_dir));
                 }
             }
             // see if this is required
             $req_mark = "";
             $req_class = "";
             if (array_key_exists($fieldname, $this->bean->get_import_required_fields())) {
                 $req_mark = ' ' . $app_strings['LBL_REQUIRED_SYMBOL'];
                 $req_class = ' class="required" ';
             }
             // see if we have a match
             $selected = '';
             if ($column_sel_from_req && isset($importColumns[$field_count])) {
                 if ($fieldname == $importColumns[$field_count]) {
                     $selected = ' selected="selected" ';
                     $defaultField = $fieldname;
                     $mappedFields[] = $fieldname;
                 }
             } else {
                 if (!empty($defaultValue) && !in_array($fieldname, $mappedFields) && !in_array($fieldname, $ignored_fields)) {
                     if (strtolower($fieldname) == strtolower($defaultValue) || strtolower($fieldname) == str_replace(" ", "_", strtolower($defaultValue)) || strtolower($displayname) == strtolower($defaultValue) || strtolower($displayname) == str_replace(" ", "_", strtolower($defaultValue))) {
                         $selected = ' selected="selected" ';
                         $defaultField = $fieldname;
                         $mappedFields[] = $fieldname;
                     }
                 }
             }
             // get field type information
             $fieldtype = '';
             if (isset($properties['type']) && isset($mod_strings['LBL_IMPORT_FIELDDEF_' . strtoupper($properties['type'])])) {
                 $fieldtype = ' [' . $mod_strings['LBL_IMPORT_FIELDDEF_' . strtoupper($properties['type'])] . '] ';
             }
             if (isset($properties['comment'])) {
                 $fieldtype .= ' - ' . $properties['comment'];
             }
             $options[$displayname . $fieldname] = '<option value="' . $fieldname . '" title="' . $displayname . htmlentities($fieldtype) . '"' . $selected . $req_class . '>' . $displayname . $req_mark . '</option>\\n';
         }
         // get default field value
         $defaultFieldHTML = '';
         if (!empty($defaultField)) {
             $defaultFieldHTML = getControl($_REQUEST['import_module'], $defaultField, $fields[$defaultField], isset($default_values[$defaultField]) ? $default_values[$defaultField] : '');
         }
         if (isset($default_values[$defaultField])) {
             unset($default_values[$defaultField]);
         }
         // Bug 27046 - Sort the column name picker alphabetically
         ksort($options);
         // to be displayed in UTF-8 format
         if (!empty($charset) && $charset != 'UTF-8') {
             if (isset($rows[1][$field_count])) {
                 $rows[1][$field_count] = $locale->translateCharset($rows[1][$field_count], $charset);
             }
         }
         $cellOneData = isset($rows[0][$field_count]) ? $rows[0][$field_count] : '';
         $cellTwoData = isset($rows[1][$field_count]) ? $rows[1][$field_count] : '';
         $cellThreeData = isset($rows[2][$field_count]) ? $rows[2][$field_count] : '';
         $columns[] = array('field_choices' => implode('', $options), 'default_field' => $defaultFieldHTML, 'cell1' => strip_tags($cellOneData), 'cell2' => strip_tags($cellTwoData), 'cell3' => strip_tags($cellThreeData), 'show_remove' => false);
     }
     // add in extra defaulted fields if they are in the mapping record
     if (count($default_values) > 0) {
         foreach ($default_values as $field_name => $default_value) {
             // build string of options
             $fields = $this->bean->get_importable_fields();
             $options = array();
             $defaultField = '';
             foreach ($fields as $fieldname => $properties) {
                 // get field name
                 if (!empty($properties['vname'])) {
                     $displayname = str_replace(":", "", translate($properties['vname'], $this->bean->module_dir));
                 } else {
                     $displayname = str_replace(":", "", translate($properties['name'], $this->bean->module_dir));
                 }
                 // see if this is required
                 $req_mark = "";
                 $req_class = "";
                 if (array_key_exists($fieldname, $this->bean->get_import_required_fields())) {
                     $req_mark = ' ' . $app_strings['LBL_REQUIRED_SYMBOL'];
                     $req_class = ' class="required" ';
                 }
                 // see if we have a match
                 $selected = '';
                 if (strtolower($fieldname) == strtolower($field_name) && !in_array($fieldname, $mappedFields) && !in_array($fieldname, $ignored_fields)) {
                     $selected = ' selected="selected" ';
                     $defaultField = $fieldname;
                     $mappedFields[] = $fieldname;
                 }
                 // get field type information
                 $fieldtype = '';
                 if (isset($properties['type']) && isset($mod_strings['LBL_IMPORT_FIELDDEF_' . strtoupper($properties['type'])])) {
                     $fieldtype = ' [' . $mod_strings['LBL_IMPORT_FIELDDEF_' . strtoupper($properties['type'])] . '] ';
                 }
                 if (isset($properties['comment'])) {
                     $fieldtype .= ' - ' . $properties['comment'];
                 }
                 $options[$displayname . $fieldname] = '<option value="' . $fieldname . '" title="' . $displayname . $fieldtype . '"' . $selected . $req_class . '>' . $displayname . $req_mark . '</option>\\n';
             }
             // get default field value
             $defaultFieldHTML = '';
             if (!empty($defaultField)) {
                 $defaultFieldHTML = getControl($_REQUEST['import_module'], $defaultField, $fields[$defaultField], $default_value);
             }
             // Bug 27046 - Sort the column name picker alphabetically
             ksort($options);
             $columns[] = array('field_choices' => implode('', $options), 'default_field' => $defaultFieldHTML, 'show_remove' => true);
             $ret_field_count++;
         }
     }
     $this->ss->assign("COLUMNCOUNT", $ret_field_count);
     $this->ss->assign("rows", $columns);
     $this->ss->assign('datetimeformat', $GLOBALS['timedate']->get_cal_date_time_format());
     // handle building index selector
     global $dictionary, $current_language;
     // show notes
     if ($this->bean instanceof Person) {
         $module_key = "LBL_CONTACTS_NOTE_";
     } elseif ($this->bean instanceof Company) {
         $module_key = "LBL_ACCOUNTS_NOTE_";
     } else {
         $module_key = "LBL_" . strtoupper($_REQUEST['import_module']) . "_NOTE_";
     }
     $notetext = '';
     for ($i = 1; isset($mod_strings[$module_key . $i]); $i++) {
         $notetext .= '<li>' . $mod_strings[$module_key . $i] . '</li>';
     }
     $this->ss->assign("NOTETEXT", $notetext);
     $this->ss->assign("HAS_HEADER", $has_header ? 'on' : 'off');
     // get list of required fields
     $required = array();
     foreach (array_keys($this->bean->get_import_required_fields()) as $name) {
         $properties = $this->bean->getFieldDefinition($name);
         if (!empty($properties['vname'])) {
             $required[$name] = str_replace(":", "", translate($properties['vname'], $this->bean->module_dir));
         } else {
             $required[$name] = str_replace(":", "", translate($properties['name'], $this->bean->module_dir));
         }
     }
     // include anything needed for quicksearch to work
     require_once "include/TemplateHandler/TemplateHandler.php";
     // Bug #46879 : createQuickSearchCode() function in IBM RTC call function getQuickSearchDefaults() to get instance and then getQSDLookup() function
     // if we call this function as static it replaces context and use ImportViewStep3 as $this in getQSDLookup()
     $template_handler = new TemplateHandler();
     $quicksearch_js = $template_handler->createQuickSearchCode($fields, $fields, 'importstep3');
     $this->ss->assign("QS_JS", $quicksearch_js);
     $this->ss->assign("JAVASCRIPT", $this->_getJS($required));
     $this->ss->assign('required_fields', implode(', ', $required));
     $this->ss->assign('CSS', $this->_getCSS());
     $content = $this->ss->fetch($this->getCustomFilePathIfExists('modules/Import/tpls/step3.tpl'));
     $this->ss->assign("CONTENT", $content);
     $this->ss->display($this->getCustomFilePathIfExists('modules/Import/tpls/wizardWrapper.tpl'));
 }
Beispiel #13
0
    /**
     * @see SugarView::display()
     */
    public function display()
    {
        global $mod_strings, $app_strings, $current_user, $sugar_config, $app_list_strings, $locale;
        $this->ss->assign("IMPORT_MODULE", $_REQUEST['import_module']);
        $has_header = isset($_REQUEST['has_header']) ? 1 : 0;
        $sugar_config['import_max_records_per_file'] = empty($sugar_config['import_max_records_per_file']) ? 1000 : $sugar_config['import_max_records_per_file'];
        // Clear out this user's last import
        $seedUsersLastImport = new UsersLastImport();
        $seedUsersLastImport->mark_deleted_by_user_id($current_user->id);
        ImportCacheFiles::clearCacheFiles();
        // attempt to lookup a preexisting field map
        // use the custom one if specfied to do so in step 1
        $field_map = array();
        $default_values = array();
        $ignored_fields = array();
        if (!empty($_REQUEST['source_id'])) {
            $mapping_file = new ImportMap();
            $mapping_file->retrieve($_REQUEST['source_id'], false);
            $_REQUEST['source'] = $mapping_file->source;
            $has_header = $mapping_file->has_header;
            if (isset($mapping_file->delimiter)) {
                $_REQUEST['custom_delimiter'] = $mapping_file->delimiter;
            }
            if (isset($mapping_file->enclosure)) {
                $_REQUEST['custom_enclosure'] = htmlentities($mapping_file->enclosure);
            }
            $field_map = $mapping_file->getMapping();
            $default_values = $mapping_file->getDefaultValues();
            $this->ss->assign("MAPNAME", $mapping_file->name);
            $this->ss->assign("CHECKMAP", 'checked="checked" value="on"');
        } else {
            // Try to see if we have a custom mapping we can use
            // based upon the where the records are coming from
            // and what module we are importing into
            $classname = 'ImportMap' . ucfirst($_REQUEST['source']);
            if (file_exists("modules/Import/{$classname}.php")) {
                require_once "modules/Import/{$classname}.php";
            } elseif (file_exists("custom/modules/Import/{$classname}.php")) {
                require_once "custom/modules/Import/{$classname}.php";
            } else {
                require_once "custom/modules/Import/ImportMapOther.php";
                $classname = 'ImportMapOther';
                $_REQUEST['source'] = 'other';
            }
            if (class_exists($classname)) {
                $mapping_file = new $classname();
                if (isset($mapping_file->delimiter)) {
                    $_REQUEST['custom_delimiter'] = $mapping_file->delimiter;
                }
                if (isset($mapping_file->enclosure)) {
                    $_REQUEST['custom_enclosure'] = htmlentities($mapping_file->enclosure);
                }
                $ignored_fields = $mapping_file->getIgnoredFields($_REQUEST['import_module']);
                $field_map = $mapping_file->getMapping($_REQUEST['import_module']);
            }
        }
        $this->ss->assign("CUSTOM_DELIMITER", !empty($_REQUEST['custom_delimiter']) ? $_REQUEST['custom_delimiter'] : ",");
        $this->ss->assign("CUSTOM_ENCLOSURE", !empty($_REQUEST['custom_enclosure']) ? $_REQUEST['custom_enclosure'] : "");
        // handle uploaded file
        $uploadFile = new UploadFile('userfile');
        if (isset($_FILES['userfile']) && $uploadFile->confirm_upload()) {
            $uploadFile->final_move('IMPORT_' . $this->bean->object_name . '_' . $current_user->id);
            $uploadFileName = $uploadFile->get_upload_path('IMPORT_' . $this->bean->object_name . '_' . $current_user->id);
        } else {
            $this->_showImportError($mod_strings['LBL_IMPORT_MODULE_ERROR_NO_UPLOAD'], $_REQUEST['import_module'], 'Step2');
            return;
        }
        // split file into parts
        $splitter = new ImportFileSplitter($uploadFileName, $sugar_config['import_max_records_per_file']);
        $splitter->splitSourceFile($_REQUEST['custom_delimiter'], html_entity_decode($_REQUEST['custom_enclosure'], ENT_QUOTES), $has_header);
        // Now parse the file and look for errors
        $importFile = new ImportFile($uploadFileName, $_REQUEST['custom_delimiter'], html_entity_decode($_REQUEST['custom_enclosure'], ENT_QUOTES));
        if (!$importFile->fileExists()) {
            $this->_showImportError($mod_strings['LBL_CANNOT_OPEN'], $_REQUEST['import_module'], 'Step2');
            return;
        }
        // retrieve first 3 rows
        $rows = array();
        $system_charset = $locale->default_export_charset;
        $user_charset = $locale->getExportCharset();
        $other_charsets = 'UTF-8, UTF-7, ASCII, CP1252, EUC-JP, SJIS, eucJP-win, SJIS-win, JIS, ISO-2022-JP';
        $detectable_charsets = "UTF-8, {$user_charset}, {$system_charset}, {$other_charsets}";
        // Bug 26824 - mb_detect_encoding() thinks CP1252 is IS0-8859-1, so use that instead in the encoding list passed to the function
        $detectable_charsets = str_replace('CP1252', 'ISO-8859-1', $detectable_charsets);
        $charset_for_import = $user_charset;
        //We will set the default import charset option by user's preference.
        $able_to_detect = function_exists('mb_detect_encoding');
        for ($i = 0; $i < 3; $i++) {
            $rows[$i] = $importFile->getNextRow();
            if (!empty($rows[$i]) && $able_to_detect) {
                foreach ($rows[$i] as &$temp_value) {
                    $current_charset = mb_detect_encoding($temp_value, $detectable_charsets);
                    if (!empty($current_charset) && $current_charset != "UTF-8") {
                        $temp_value = $locale->translateCharset($temp_value, $current_charset);
                        // we will use utf-8 for displaying the data on the page.
                        $charset_for_import = $current_charset;
                        //set the default import charset option according to the current_charset.
                        //If it is not utf-8, tt may be overwritten by the later one. So the uploaded file should not contain two types of charset($user_charset, $system_charset), and I think this situation will not occur.
                    }
                }
            }
        }
        $ret_field_count = $importFile->getFieldCount();
        // Bug 14689 - Parse the first data row to make sure it has non-empty data in it
        $isempty = true;
        if ($rows[(int) $has_header] != false) {
            foreach ($rows[(int) $has_header] as $value) {
                if (strlen(trim($value)) > 0) {
                    $isempty = false;
                    break;
                }
            }
        }
        if ($isempty || $rows[(int) $has_header] == false) {
            $this->_showImportError($mod_strings['LBL_NO_LINES'], $_REQUEST['import_module'], 'Step2');
            return;
        }
        // save first row to send to step 4
        $this->ss->assign("FIRSTROW", base64_encode(serialize($rows[0])));
        // Now build template
        $this->ss->assign("TMP_FILE", $uploadFileName);
        $this->ss->assign("FILECOUNT", $splitter->getFileCount());
        $this->ss->assign("RECORDCOUNT", $splitter->getRecordCount());
        $this->ss->assign("RECORDTHRESHOLD", $sugar_config['import_max_records_per_file']);
        $this->ss->assign("SOURCE", $_REQUEST['source']);
        $this->ss->assign("TYPE", $_REQUEST['type']);
        $this->ss->assign("DELETE_INLINE_PNG", SugarThemeRegistry::current()->getImage('basic_search', 'align="absmiddle" alt="' . $app_strings['LNK_DELETE'] . '" border="0"'));
        $this->ss->assign("PUBLISH_INLINE_PNG", SugarThemeRegistry::current()->getImage('advanced_search', 'align="absmiddle" alt="' . $mod_strings['LBL_PUBLISH'] . '" border="0"'));
        $this->ss->assign("MODULE_TITLE", $this->getModuleTitle());
        $this->ss->assign("STEP4_TITLE", strip_tags(str_replace("\n", "", getClassicModuleTitle($mod_strings['LBL_MODULE_NAME'], array($mod_strings['LBL_MODULE_NAME'], $mod_strings['LBL_STEP_4_TITLE']), false))));
        $this->ss->assign("HEADER", $app_strings['LBL_IMPORT'] . " " . $mod_strings['LBL_MODULE_NAME']);
        // we export it as email_address, but import as email1
        $field_map['email_address'] = 'email1';
        // build each row; row count is determined by the the number of fields in the import file
        $columns = array();
        $mappedFields = array();
        for ($field_count = 0; $field_count < $ret_field_count; $field_count++) {
            // See if we have any field map matches
            $defaultValue = "";
            // Bug 31260 - If the data rows have more columns than the header row, then just add a new header column
            if (!isset($rows[0][$field_count])) {
                $rows[0][$field_count] = '';
            }
            // See if we can match the import row to a field in the list of fields to import
            $firstrow_name = trim(str_replace(":", "", $rows[0][$field_count]));
            if ($has_header && isset($field_map[$firstrow_name])) {
                $defaultValue = $field_map[$firstrow_name];
            } elseif (isset($field_map[$field_count])) {
                $defaultValue = $field_map[$field_count];
            } elseif (empty($_REQUEST['source_id'])) {
                $defaultValue = trim($rows[0][$field_count]);
            }
            // build string of options
            $fields = $this->bean->get_importable_fields();
            $options = array();
            $defaultField = '';
            foreach ($fields as $fieldname => $properties) {
                // get field name
                if (!empty($properties['vname'])) {
                    $displayname = str_replace(":", "", translate($properties['vname'], $this->bean->module_dir));
                } else {
                    $displayname = str_replace(":", "", translate($properties['name'], $this->bean->module_dir));
                }
                // see if this is required
                $req_mark = "";
                $req_class = "";
                if (array_key_exists($fieldname, $this->bean->get_import_required_fields())) {
                    $req_mark = ' ' . $app_strings['LBL_REQUIRED_SYMBOL'];
                    $req_class = ' class="required" ';
                }
                // see if we have a match
                $selected = '';
                if (!empty($defaultValue) && !in_array($fieldname, $mappedFields) && !in_array($fieldname, $ignored_fields)) {
                    if (strtolower($fieldname) == strtolower($defaultValue) || strtolower($fieldname) == str_replace(" ", "_", strtolower($defaultValue)) || strtolower($displayname) == strtolower($defaultValue) || strtolower($displayname) == str_replace(" ", "_", strtolower($defaultValue))) {
                        $selected = ' selected="selected" ';
                        $defaultField = $fieldname;
                        $mappedFields[] = $fieldname;
                    }
                }
                // get field type information
                $fieldtype = '';
                if (isset($properties['type']) && isset($mod_strings['LBL_IMPORT_FIELDDEF_' . strtoupper($properties['type'])])) {
                    $fieldtype = ' [' . $mod_strings['LBL_IMPORT_FIELDDEF_' . strtoupper($properties['type'])] . '] ';
                }
                if (isset($properties['comment'])) {
                    $fieldtype .= ' - ' . $properties['comment'];
                }
                $options[$displayname . $fieldname] = '<option value="' . $fieldname . '" title="' . $displayname . htmlentities($fieldtype) . '"' . $selected . $req_class . '>' . $displayname . $req_mark . '</option>\\n';
            }
            // get default field value
            $defaultFieldHTML = '';
            if (!empty($defaultField)) {
                $defaultFieldHTML = getControl($_REQUEST['import_module'], $defaultField, $fields[$defaultField], isset($default_values[$defaultField]) ? $default_values[$defaultField] : '');
            }
            if (isset($default_values[$defaultField])) {
                unset($default_values[$defaultField]);
            }
            // Bug 27046 - Sort the column name picker alphabetically
            ksort($options);
            $columns[] = array('field_choices' => implode('', $options), 'default_field' => $defaultFieldHTML, 'cell1' => str_replace("&quot;", '', htmlspecialchars($rows[0][$field_count])), 'cell2' => str_replace("&quot;", '', htmlspecialchars($rows[1][$field_count])), 'cell3' => str_replace("&quot;", '', htmlspecialchars($rows[2][$field_count])), 'show_remove' => false);
        }
        // add in extra defaulted fields if they are in the mapping record
        if (count($default_values) > 0) {
            foreach ($default_values as $field_name => $default_value) {
                // build string of options
                $fields = $this->bean->get_importable_fields();
                $options = array();
                $defaultField = '';
                foreach ($fields as $fieldname => $properties) {
                    // get field name
                    if (!empty($properties['vname'])) {
                        $displayname = str_replace(":", "", translate($properties['vname'], $this->bean->module_dir));
                    } else {
                        $displayname = str_replace(":", "", translate($properties['name'], $this->bean->module_dir));
                    }
                    // see if this is required
                    $req_mark = "";
                    $req_class = "";
                    if (array_key_exists($fieldname, $this->bean->get_import_required_fields())) {
                        $req_mark = ' ' . $app_strings['LBL_REQUIRED_SYMBOL'];
                        $req_class = ' class="required" ';
                    }
                    // see if we have a match
                    $selected = '';
                    if (strtolower($fieldname) == strtolower($field_name) && !in_array($fieldname, $mappedFields) && !in_array($fieldname, $ignored_fields)) {
                        $selected = ' selected="selected" ';
                        $defaultField = $fieldname;
                        $mappedFields[] = $fieldname;
                    }
                    // get field type information
                    $fieldtype = '';
                    if (isset($properties['type']) && isset($mod_strings['LBL_IMPORT_FIELDDEF_' . strtoupper($properties['type'])])) {
                        $fieldtype = ' [' . $mod_strings['LBL_IMPORT_FIELDDEF_' . strtoupper($properties['type'])] . '] ';
                    }
                    if (isset($properties['comment'])) {
                        $fieldtype .= ' - ' . $properties['comment'];
                    }
                    $options[$displayname . $fieldname] = '<option value="' . $fieldname . '" title="' . $displayname . $fieldtype . '"' . $selected . $req_class . '>' . $displayname . $req_mark . '</option>\\n';
                }
                // get default field value
                $defaultFieldHTML = '';
                if (!empty($defaultField)) {
                    $defaultFieldHTML = getControl($_REQUEST['import_module'], $defaultField, $fields[$defaultField], $default_value);
                }
                // Bug 27046 - Sort the column name picker alphabetically
                ksort($options);
                $columns[] = array('field_choices' => implode('', $options), 'default_field' => $defaultFieldHTML, 'show_remove' => true);
                $ret_field_count++;
            }
        }
        $this->ss->assign("COLUMNCOUNT", $ret_field_count);
        $this->ss->assign("rows", $columns);
        // get list of valid date/time formats
        $timeFormat = $current_user->getUserDateTimePreferences();
        $timeOptions = get_select_options_with_id($sugar_config['time_formats'], $timeFormat['time']);
        $dateOptions = get_select_options_with_id($sugar_config['date_formats'], $timeFormat['date']);
        $this->ss->assign('TIMEOPTIONS', $timeOptions);
        $this->ss->assign('DATEOPTIONS', $dateOptions);
        $this->ss->assign('datetimeformat', $GLOBALS['timedate']->get_cal_date_time_format());
        // get list of valid timezones
        $userTZ = $current_user->getPreference('timezone');
        if (empty($userTZ)) {
            $userTZ = TimeDate::userTimezone();
        }
        $this->ss->assign('TIMEZONE_CURRENT', $userTZ);
        $this->ss->assign('TIMEZONEOPTIONS', TimeDate::getTimezoneList());
        // get currency preference
        require_once 'modules/Currencies/ListCurrency.php';
        $currency = new ListCurrency();
        $cur_id = $locale->getPrecedentPreference('currency', $current_user);
        if ($cur_id) {
            $selectCurrency = $currency->getSelectOptions($cur_id);
            $this->ss->assign("CURRENCY", $selectCurrency);
        } else {
            $selectCurrency = $currency->getSelectOptions();
            $this->ss->assign("CURRENCY", $selectCurrency);
        }
        $currenciesVars = "";
        $i = 0;
        foreach ($locale->currencies as $id => $arrVal) {
            $currenciesVars .= "currencies[{$i}] = '{$arrVal['symbol']}';\n";
            $i++;
        }
        $currencySymbolsJs = <<<eoq
var currencies = new Object;
{$currenciesVars}
function setSymbolValue(id) {
    document.getElementById('symbol').value = currencies[id];
}
eoq;
        $this->ss->assign('currencySymbolJs', $currencySymbolsJs);
        // fill significant digits dropdown
        $significantDigits = $locale->getPrecedentPreference('default_currency_significant_digits', $current_user);
        $sigDigits = '';
        for ($i = 0; $i <= 6; $i++) {
            if ($significantDigits == $i) {
                $sigDigits .= '<option value="' . $i . '" selected="true">' . $i . '</option>';
            } else {
                $sigDigits .= '<option value="' . $i . '">' . $i . '</option>';
            }
        }
        $this->ss->assign('sigDigits', $sigDigits);
        $num_grp_sep = $current_user->getPreference('num_grp_sep');
        $dec_sep = $current_user->getPreference('dec_sep');
        $this->ss->assign("NUM_GRP_SEP", empty($num_grp_sep) ? $sugar_config['default_number_grouping_seperator'] : $num_grp_sep);
        $this->ss->assign("DEC_SEP", empty($dec_sep) ? $sugar_config['default_decimal_seperator'] : $dec_sep);
        $this->ss->assign('getNumberJs', $locale->getNumberJs());
        // Name display format
        $this->ss->assign('default_locale_name_format', $locale->getLocaleFormatMacro($current_user));
        $this->ss->assign('getNameJs', $locale->getNameJs());
        // Charset
        $charsetOptions = get_select_options_with_id($locale->getCharsetSelect(), $charset_for_import);
        //wdong,  bug 25927, here we should use the charset testing results from above.
        $this->ss->assign('CHARSETOPTIONS', $charsetOptions);
        // handle building index selector
        global $dictionary, $current_language;
        require_once "include/templates/TemplateGroupChooser.php";
        $chooser_array = array();
        $chooser_array[0] = array();
        $idc = new ImportDuplicateCheck($this->bean);
        $chooser_array[1] = $idc->getDuplicateCheckIndexes();
        $chooser = new TemplateGroupChooser();
        $chooser->args['id'] = 'selected_indices';
        $chooser->args['values_array'] = $chooser_array;
        $chooser->args['left_name'] = 'choose_index';
        $chooser->args['right_name'] = 'ignore_index';
        $chooser->args['left_label'] = $mod_strings['LBL_INDEX_USED'];
        $chooser->args['right_label'] = $mod_strings['LBL_INDEX_NOT_USED'];
        $this->ss->assign("TAB_CHOOSER", $chooser->display());
        // show notes
        if ($this->bean instanceof Person) {
            $module_key = "LBL_CONTACTS_NOTE_";
        } elseif ($this->bean instanceof Company) {
            $module_key = "LBL_ACCOUNTS_NOTE_";
        } else {
            $module_key = "LBL_" . strtoupper($_REQUEST['import_module']) . "_NOTE_";
        }
        $notetext = '';
        for ($i = 1; isset($mod_strings[$module_key . $i]); $i++) {
            $notetext .= '<li>' . $mod_strings[$module_key . $i] . '</li>';
        }
        $this->ss->assign("NOTETEXT", $notetext);
        $this->ss->assign("HAS_HEADER", $has_header ? 'on' : 'off');
        // get list of required fields
        $required = array();
        foreach (array_keys($this->bean->get_import_required_fields()) as $name) {
            $properties = $this->bean->getFieldDefinition($name);
            if (!empty($properties['vname'])) {
                $required[$name] = str_replace(":", "", translate($properties['vname'], $this->bean->module_dir));
            } else {
                $required[$name] = str_replace(":", "", translate($properties['name'], $this->bean->module_dir));
            }
        }
        // include anything needed for quicksearch to work
        require_once "include/TemplateHandler/TemplateHandler.php";
        $quicksearch_js = TemplateHandler::createQuickSearchCode($fields, $fields, 'importstep3');
        $this->ss->assign("JAVASCRIPT", $quicksearch_js . "\n" . $this->_getJS($required));
        $this->ss->assign('required_fields', implode(', ', $required));
        $this->ss->display('modules/Import/tpls/step3.tpl');
    }
Beispiel #14
0
 /** 
  * @see SugarView::display()
  */
 public function display()
 {
     global $sugar_config;
     // Increase the max_execution_time since this step can take awhile
     ini_set("max_execution_time", max($sugar_config['import_max_execution_time'], 3600));
     // stop the tracker
     TrackerManager::getInstance()->pause();
     // use our own error handler
     set_error_handler('handleImportErrors', E_ALL);
     global $mod_strings, $app_strings, $current_user, $import_bean_map;
     global $app_list_strings, $timedate;
     $update_only = isset($_REQUEST['import_type']) && $_REQUEST['import_type'] == 'update';
     $firstrow = unserialize(base64_decode($_REQUEST['firstrow']));
     // All the Look Up Caches are initialized here
     $enum_lookup_cache = array();
     // Let's try and load the import bean
     $focus = loadImportBean($_REQUEST['import_module']);
     if (!$focus) {
         trigger_error($mod_strings['LBL_ERROR_IMPORTS_NOT_SET_UP'], E_USER_ERROR);
     }
     // setup the importable fields array.
     $importable_fields = $focus->get_importable_fields();
     // loop through all request variables
     $importColumns = array();
     foreach ($_REQUEST as $name => $value) {
         // only look for var names that start with "fieldNum"
         if (strncasecmp($name, "colnum_", 7) != 0) {
             continue;
         }
         // pull out the column position for this field name
         $pos = substr($name, 7);
         if (isset($importable_fields[$value])) {
             // now mark that we've seen this field
             $importColumns[$pos] = $value;
         }
     }
     // set the default locale settings
     $ifs = new ImportFieldSanitize();
     $ifs->dateformat = $_REQUEST['importlocale_dateformat'];
     $ifs->timeformat = $_REQUEST['importlocale_timeformat'];
     $ifs->timezone = $_REQUEST['importlocale_timezone'];
     $currency = new Currency();
     $currency->retrieve($_REQUEST['importlocale_currency']);
     $ifs->currency_symbol = $currency->symbol;
     $ifs->default_currency_significant_digits = $_REQUEST['importlocale_default_currency_significant_digits'];
     $ifs->num_grp_sep = $_REQUEST['importlocale_num_grp_sep'];
     $ifs->dec_sep = $_REQUEST['importlocale_dec_sep'];
     $ifs->default_locale_name_format = $_REQUEST['importlocale_default_locale_name_format'];
     // Check to be sure we are getting an import file that is in the right place
     if (realpath(dirname($_REQUEST['tmp_file']) . '/') != realpath($sugar_config['upload_dir'])) {
         trigger_error($mod_strings['LBL_CANNOT_OPEN'], E_USER_ERROR);
     }
     // Open the import file
     $importFile = new ImportFile($_REQUEST['tmp_file'], $_REQUEST['custom_delimiter'], html_entity_decode($_REQUEST['custom_enclosure'], ENT_QUOTES));
     if (!$importFile->fileExists()) {
         trigger_error($mod_strings['LBL_CANNOT_OPEN'], E_USER_ERROR);
     }
     $fieldDefs = $focus->getFieldDefinitions();
     unset($focus);
     while ($row = $importFile->getNextRow()) {
         $focus = loadImportBean($_REQUEST['import_module']);
         $focus->unPopulateDefaultValues();
         $focus->save_from_post = false;
         $focus->team_id = null;
         $ifs->createdBeans = array();
         $do_save = true;
         for ($fieldNum = 0; $fieldNum < $_REQUEST['columncount']; $fieldNum++) {
             // loop if this column isn't set
             if (!isset($importColumns[$fieldNum])) {
                 continue;
             }
             // get this field's properties
             $field = $importColumns[$fieldNum];
             $fieldDef = $focus->getFieldDefinition($field);
             $fieldTranslated = translate(isset($fieldDef['vname']) ? $fieldDef['vname'] : $fieldDef['name'], $_REQUEST['module']) . " (" . $fieldDef['name'] . ")";
             // Bug 37241 - Don't re-import over a field we already set during the importing of another field
             if (!empty($focus->{$field})) {
                 continue;
             }
             //DETERMINE WHETHER OR NOT $fieldDef['name'] IS DATE_MODIFIED AND SET A VAR, USE DOWN BELOW
             // translate strings
             global $locale;
             if (empty($locale)) {
                 $locale = new Localization();
             }
             if (isset($row[$fieldNum])) {
                 $rowValue = $locale->translateCharset(strip_tags(trim($row[$fieldNum])), $_REQUEST['importlocale_charset'], $sugar_config['default_charset']);
             } else {
                 $rowValue = '';
             }
             // If there is an default value then use it instead
             if (!empty($_REQUEST[$field])) {
                 if (is_array($_REQUEST[$field])) {
                     $defaultRowValue = encodeMultienumValue($_REQUEST[$field]);
                 } else {
                     $defaultRowValue = $_REQUEST[$field];
                 }
                 // translate default values to the date/time format for the import file
                 if ($fieldDef['type'] == 'date' && $ifs->dateformat != $timedate->get_date_format()) {
                     $defaultRowValue = $timedate->swap_formats($defaultRowValue, $ifs->dateformat, $timedate->get_date_format());
                 }
                 if ($fieldDef['type'] == 'time' && $ifs->timeformat != $timedate->get_time_format()) {
                     $defaultRowValue = $timedate->swap_formats($defaultRowValue, $ifs->timeformat, $timedate->get_time_format());
                 }
                 if (($fieldDef['type'] == 'datetime' || $fieldDef['type'] == 'datetimecombo') && $ifs->dateformat . ' ' . $ifs->timeformat != $timedate->get_date_time_format()) {
                     $defaultRowValue = $timedate->swap_formats($defaultRowValue, $ifs->dateformat . ' ' . $ifs->timeformat, $timedate->get_date_time_format());
                 }
                 if (in_array($fieldDef['type'], array('currency', 'float', 'int', 'num')) && $ifs->num_grp_sep != $current_user->getPreference('num_grp_sep')) {
                     $defaultRowValue = str_replace($current_user->getPreference('num_grp_sep'), $ifs->num_grp_sep, $defaultRowValue);
                 }
                 if (in_array($fieldDef['type'], array('currency', 'float')) && $ifs->dec_sep != $current_user->getPreference('dec_sep')) {
                     $defaultRowValue = str_replace($current_user->getPreference('dec_sep'), $ifs->dec_sep, $defaultRowValue);
                 }
                 $currency->retrieve('-99');
                 $user_currency_symbol = $currency->symbol;
                 if ($fieldDef['type'] == 'currency' && $ifs->currency_symbol != $user_currency_symbol) {
                     $defaultRowValue = str_replace($user_currency_symbol, $ifs->currency_symbol, $defaultRowValue);
                 }
                 if (empty($rowValue)) {
                     $rowValue = $defaultRowValue;
                     unset($defaultRowValue);
                 }
             }
             // Bug 22705 - Don't update the First Name or Last Name value if Full Name is set
             if (in_array($field, array('first_name', 'last_name')) && !empty($focus->full_name)) {
                 continue;
             }
             // loop if this value has not been set
             if (!isset($rowValue)) {
                 continue;
             }
             // If the field is required and blank then error out
             if (array_key_exists($field, $focus->get_import_required_fields()) && empty($rowValue) && $rowValue != '0') {
                 $importFile->writeError($mod_strings['LBL_REQUIRED_VALUE'], $fieldTranslated, 'NULL');
                 $do_save = false;
             }
             // Handle the special case "Sync to Outlook"
             if ($focus->object_name == "Contacts" && $field == 'sync_contact') {
                 $bad_names = array();
                 $returnValue = $ifs->synctooutlook($rowValue, $fieldDef, $bad_names);
                 // try the default value on fail
                 if (!$returnValue && !empty($defaultRowValue)) {
                     $returnValue = $ifs->synctooutlook($defaultRowValue, $fieldDef, $bad_names);
                 }
                 if (!$returnValue) {
                     $importFile->writeError($mod_strings['LBL_ERROR_SYNC_USERS'], $fieldTranslated, explode(",", $bad_names));
                     $do_save = 0;
                 }
             }
             // Handle email1 and email2 fields ( these don't have the type of email )
             if ($field == 'email1' || $field == 'email2') {
                 $returnValue = $ifs->email($rowValue, $fieldDef);
                 // try the default value on fail
                 if (!$returnValue && !empty($defaultRowValue)) {
                     $returnValue = $ifs->email($defaultRowValue, $fieldDef);
                 }
                 if ($returnValue === FALSE) {
                     $do_save = 0;
                     $importFile->writeError($mod_strings['LBL_ERROR_INVALID_EMAIL'], $fieldTranslated, $rowValue);
                 } else {
                     $rowValue = $returnValue;
                     // check for current opt_out and invalid email settings for this email address
                     // if we find any, set them now
                     $emailres = $focus->db->query("SELECT opt_out, invalid_email FROM email_addresses \n                                WHERE email_address = '" . $focus->db->quote($rowValue) . "'");
                     if ($emailrow = $focus->db->fetchByAssoc($emailres)) {
                         $focus->email_opt_out = $emailrow['opt_out'];
                         $focus->invalid_email = $emailrow['invalid_email'];
                     }
                 }
             }
             // Handle splitting Full Name into First and Last Name parts
             if ($field == 'full_name' && !empty($rowValue)) {
                 $ifs->fullname($rowValue, $fieldDef, $focus);
             }
             // to maintain 451 compatiblity
             if (!isset($fieldDef['module']) && $fieldDef['type'] == 'relate') {
                 $fieldDef['module'] = ucfirst($fieldDef['table']);
             }
             if (isset($fieldDef['custom_type']) && !empty($fieldDef['custom_type'])) {
                 $fieldDef['type'] = $fieldDef['custom_type'];
             }
             // If the field is empty then there is no need to check the data
             if (!empty($rowValue)) {
                 switch ($fieldDef['type']) {
                     case 'enum':
                     case 'multienum':
                         if (isset($fieldDef['type']) && $fieldDef['type'] == "multienum") {
                             $returnValue = $ifs->multienum($rowValue, $fieldDef);
                         } else {
                             $returnValue = $ifs->enum($rowValue, $fieldDef);
                         }
                         // try the default value on fail
                         if (!$returnValue && !empty($defaultRowValue)) {
                             if (isset($fieldDef['type']) && $fieldDef['type'] == "multienum") {
                                 $returnValue = $ifs->multienum($defaultRowValue, $fieldDef);
                             } else {
                                 $returnValue = $ifs->enum($defaultRowValue, $fieldDef);
                             }
                         }
                         if ($returnValue === FALSE) {
                             $importFile->writeError($mod_strings['LBL_ERROR_NOT_IN_ENUM'] . implode(",", $app_list_strings[$fieldDef['options']]), $fieldTranslated, $rowValue);
                             $do_save = 0;
                         } else {
                             $rowValue = $returnValue;
                         }
                         break;
                     case 'relate':
                     case 'parent':
                         $returnValue = $ifs->relate($rowValue, $fieldDef, $focus, empty($defaultRowValue));
                         if (!$returnValue && !empty($defaultRowValue)) {
                             $returnValue = $ifs->relate($defaultRowValue, $fieldDef, $focus);
                         }
                         // Bug 33623 - Set the id value found from the above method call as an importColumn
                         if ($returnValue !== false) {
                             $importColumns[] = $fieldDef['id_name'];
                         }
                         break;
                     case 'teamset':
                         $returnValue = $ifs->teamset($rowValue, $fieldDef, $focus);
                         $importColumns[] = 'team_set_id';
                         $importColumns[] = 'team_id';
                         break;
                     case 'fullname':
                         break;
                     default:
                         if (method_exists('ImportFieldSanitize', $fieldDef['type'])) {
                             $fieldtype = $fieldDef['type'];
                             $returnValue = $ifs->{$fieldtype}($rowValue, $fieldDef);
                             // try the default value on fail
                             if (!$returnValue && !empty($defaultRowValue)) {
                                 $returnValue = $ifs->{$fieldtype}($defaultRowValue, $fieldDef);
                             }
                             if (!$returnValue) {
                                 $do_save = 0;
                                 $importFile->writeError($mod_strings['LBL_ERROR_INVALID_' . strtoupper($fieldDef['type'])], $fieldTranslated, $rowValue);
                             } else {
                                 $rowValue = $returnValue;
                             }
                         }
                 }
             }
             $focus->{$field} = $rowValue;
             unset($defaultRowValue);
         }
         // Now try to validate flex relate fields
         if (isset($focus->field_defs['parent_name']) && isset($focus->parent_name) && $focus->field_defs['parent_name']['type'] == 'parent') {
             // populate values from the picker widget if the import file doesn't have them
             $parent_idField = $focus->field_defs['parent_name']['id_name'];
             if (empty($focus->{$parent_idField}) && !empty($_REQUEST[$parent_idField])) {
                 $focus->{$parent_idField} = $_REQUEST[$parent_idField];
             }
             $parent_typeField = $focus->field_defs['parent_name']['type_name'];
             if (empty($focus->{$parent_typeField}) && !empty($_REQUEST[$parent_typeField])) {
                 $focus->{$parent_typeField} = $_REQUEST[$parent_typeField];
             }
             // now validate it
             $returnValue = $ifs->parent($focus->parent_name, $focus->field_defs['parent_name'], $focus, empty($_REQUEST['parent_name']));
             if (!$returnValue && !empty($_REQUEST['parent_name'])) {
                 $returnValue = $ifs->parent($_REQUEST['parent_name'], $focus->field_defs['parent_name'], $focus);
             }
         }
         // check to see that the indexes being entered are unique.
         if (isset($_REQUEST['display_tabs_def']) && $_REQUEST['display_tabs_def'] != "") {
             $idc = new ImportDuplicateCheck($focus);
             if ($idc->isADuplicateRecord(explode('&', $_REQUEST['display_tabs_def']))) {
                 $importFile->markRowAsDuplicate();
                 $this->_undoCreatedBeans($ifs->createdBeans);
                 continue;
             }
         }
         // if the id was specified
         $newRecord = true;
         if (!empty($focus->id)) {
             $focus->id = $this->_convertId($focus->id);
             // check if it already exists
             $query = "SELECT * FROM {$focus->table_name} WHERE id='" . $focus->db->quote($focus->id) . "'";
             $result = $focus->db->query($query) or sugar_die("Error selecting sugarbean: ");
             $dbrow = $focus->db->fetchByAssoc($result);
             if (isset($dbrow['id']) && $dbrow['id'] != -1) {
                 // if it exists but was deleted, just remove it
                 if (isset($dbrow['deleted']) && $dbrow['deleted'] == 1 && $update_only == false) {
                     $query2 = "DELETE FROM {$focus->table_name} WHERE id='" . $focus->db->quote($focus->id) . "'";
                     $result2 = $focus->db->query($query2) or sugar_die($mod_strings['LBL_ERROR_DELETING_RECORD'] . " " . $focus->id);
                     if ($focus->hasCustomFields()) {
                         $query3 = "DELETE FROM {$focus->table_name}_cstm WHERE id_c='" . $focus->db->quote($focus->id) . "'";
                         $result2 = $focus->db->query($query3);
                     }
                     $focus->new_with_id = true;
                 } else {
                     if (!$update_only) {
                         $do_save = 0;
                         $importFile->writeError($mod_strings['LBL_ID_EXISTS_ALREADY'], 'ID', $focus->id);
                         $this->_undoCreatedBeans($ifs->createdBeans);
                         continue;
                     }
                     $existing_focus = loadImportBean($_REQUEST['import_module']);
                     $newRecord = false;
                     if (!$existing_focus->retrieve($dbrow['id']) instanceof SugarBean) {
                         $do_save = 0;
                         $importFile->writeError($mod_strings['LBL_RECORD_CANNOT_BE_UPDATED'], 'ID', $focus->id);
                         $this->_undoCreatedBeans($ifs->createdBeans);
                         continue;
                     } else {
                         $newData = $focus->toArray();
                         foreach ($newData as $focus_key => $focus_value) {
                             if (in_array($focus_key, $importColumns)) {
                                 $existing_focus->{$focus_key} = $focus_value;
                             }
                         }
                         $focus = $existing_focus;
                     }
                     unset($existing_focus);
                 }
             } else {
                 $focus->new_with_id = true;
             }
         }
         if ($do_save) {
             // Populate in any default values to the bean
             $focus->populateDefaultValues();
             if (!isset($focus->assigned_user_id) || $focus->assigned_user_id == '' && $newRecord) {
                 $focus->assigned_user_id = $current_user->id;
             }
             /*
              * Bug 34854: Added all conditions besides the empty check on date modified. Currently, if
              * we do an update to a record, it doesn't update the date_modified value.
              * Hack note: I'm doing a to_display and back to_db on the fetched row to make sure that any truncating that happens
              * when $focus->date_modified goes to_display and back to_db also happens on the fetched db value. Otherwise,
              * in some cases we truncate the seconds on one and not the other, and the comparison fails when it should pass
              */
             if (!empty($focus->new_with_id) && !empty($focus->date_modified) || empty($focus->new_with_id) && $timedate->to_db($focus->date_modified) != $timedate->to_db($timedate->to_display_date_time($focus->fetched_row['date_modified']))) {
                 $focus->update_date_modified = false;
             }
             $focus->optimistic_lock = false;
             if ($focus->object_name == "Contacts" && isset($focus->sync_contact)) {
                 //copy the potential sync list to another varible
                 $list_of_users = $focus->sync_contact;
                 //and set it to false for the save
                 $focus->sync_contact = false;
             } else {
                 if ($focus->object_name == "User" && !empty($current_user) && $focus->is_admin && !is_admin($current_user) && is_admin_for_module($current_user, 'Users')) {
                     sugar_die($GLOBALS['mod_strings']['ERR_IMPORT_SYSTEM_ADMININSTRATOR']);
                 }
             }
             //bug# 40260 setting it true as the module in focus is involved in an import
             $focus->in_import = true;
             // call any logic needed for the module preSave
             $focus->beforeImportSave();
             $focus->save(false);
             // call any logic needed for the module postSave
             $focus->afterImportSave();
             if ($focus->object_name == "Contacts" && isset($list_of_users)) {
                 $focus->process_sync_to_outlook($list_of_users);
             }
             // Update the created/updated counter
             $importFile->markRowAsImported($newRecord);
             // Add ID to User's Last Import records
             if ($newRecord) {
                 ImportFile::writeRowToLastImport($_REQUEST['import_module'], $focus->object_name == 'Case' ? 'aCase' : $focus->object_name, $focus->id);
             }
         } else {
             $this->_undoCreatedBeans($ifs->createdBeans);
         }
     }
     // save mapping if requested
     if (isset($_REQUEST['save_map_as']) && $_REQUEST['save_map_as'] != '') {
         $mapping_file = new ImportMap();
         if (isset($_REQUEST['has_header']) && $_REQUEST['has_header'] == 'on') {
             $header_to_field = array();
             foreach ($importColumns as $pos => $field_name) {
                 if (isset($firstrow[$pos]) && isset($field_name)) {
                     $header_to_field[$firstrow[$pos]] = $field_name;
                 }
             }
             $mapping_file->setMapping($header_to_field);
         } else {
             $mapping_file->setMapping($importColumns);
         }
         // save default fields
         $defaultValues = array();
         for ($i = 0; $i < $_REQUEST['columncount']; $i++) {
             if (isset($importColumns[$i]) && !empty($_REQUEST[$importColumns[$i]])) {
                 $field = $importColumns[$i];
                 $fieldDef = $focus->getFieldDefinition($field);
                 if (!empty($fieldDef['custom_type']) && $fieldDef['custom_type'] == 'teamset') {
                     require_once 'include/SugarFields/Fields/Teamset/SugarFieldTeamset.php';
                     $sugar_field = new SugarFieldTeamset('Teamset');
                     $teams = $sugar_field->getTeamsFromRequest($field);
                     if (isset($_REQUEST['primary_team_name_collection'])) {
                         $primary_index = $_REQUEST['primary_team_name_collection'];
                     }
                     //If primary_index was selected, ensure that the first Array entry is the primary team
                     if (isset($primary_index)) {
                         $count = 0;
                         $new_teams = array();
                         foreach ($teams as $id => $name) {
                             if ($primary_index == $count++) {
                                 $new_teams[$id] = $name;
                                 unset($teams[$id]);
                                 break;
                             }
                         }
                         foreach ($teams as $id => $name) {
                             $new_teams[$id] = $name;
                         }
                         $teams = $new_teams;
                     }
                     //if
                     $json = getJSONobj();
                     $defaultValues[$field] = $json->encode($teams);
                 } else {
                     $defaultValues[$field] = $_REQUEST[$importColumns[$i]];
                 }
             }
         }
         $mapping_file->setDefaultValues($defaultValues);
         $result = $mapping_file->save($current_user->id, $_REQUEST['save_map_as'], $_REQUEST['import_module'], $_REQUEST['source'], isset($_REQUEST['has_header']) && $_REQUEST['has_header'] == 'on', $_REQUEST['custom_delimiter'], html_entity_decode($_REQUEST['custom_enclosure'], ENT_QUOTES));
     }
     $importFile->writeStatus();
 }
Beispiel #15
0
         echo json_encode(['name' => $field_def->name]);
     }
     echo "\n]}";
     break;
 case 'get_import_preview':
     if (!sesAdminAccess()) {
         header('HTTP/1.1 403 Forbidden');
         exit;
     }
     $table = getRequest('table', '');
     if ($table == 'account_statement') {
         require 'import_statement.php';
         $import = new ImportStatement();
     } else {
         require 'import.php';
         $import = new ImportFile();
     }
     $import->create_import_preview();
     break;
 case 'get_list':
     require 'list.php';
     $listFunc = getRequest('listfunc', '');
     $strList = getRequest('table', '');
     if (!$strList) {
         header('HTTP/1.1 400 Bad Request');
         die('Table must be defined');
     }
     include 'list_switch.php';
     if (!$strTable) {
         header('HTTP/1.1 400 Bad Request');
         die('Invalid table name');
 /**
  * @see SugarFieldBase::importSanitize()
  */
 public function importSanitize($value, $vardef, $focus, ImportFieldSanitize $settings)
 {
     if (!isset($vardef['module'])) {
         return false;
     }
     $newbean = BeanFactory::getBean($vardef['module']);
     // Bug 32869 - Assumed related field name is 'name' if it is not specified
     if (!isset($vardef['rname'])) {
         $vardef['rname'] = 'name';
     }
     // Bug 27046 - Validate field against type as it is in the related field
     $rvardef = $newbean->getFieldDefinition($vardef['rname']);
     if (isset($rvardef['type']) && method_exists($this, $rvardef['type'])) {
         $fieldtype = $rvardef['type'];
         $returnValue = $settings->{$fieldtype}($value, $rvardef);
         if (!$returnValue) {
             return false;
         } else {
             $value = $returnValue;
         }
     }
     if (isset($vardef['id_name'])) {
         $idField = $vardef['id_name'];
         // Bug 24075 - clear out id field value if it is invalid
         if (isset($focus->{$idField})) {
             $checkfocus = BeanFactory::getBean($vardef['module']);
             if ($checkfocus && is_null($checkfocus->retrieve($focus->{$idField}))) {
                 $focus->{$idField} = '';
             }
         }
         // fixing bug #47722: Imports to Custom Relate Fields Do Not Work
         if (!isset($vardef['table'])) {
             // Set target module table as the default table name
             $vardef['table'] = $newbean->table_name;
         }
         // be sure that the id isn't already set for this row
         if (empty($focus->{$idField}) && $idField != $vardef['name'] && !empty($vardef['rname']) && !empty($vardef['table'])) {
             // Bug 27562 - Check db_concat_fields first to see if the field name is a concatenation.
             $relatedFieldDef = $newbean->getFieldDefinition($vardef['rname']);
             if (isset($relatedFieldDef['db_concat_fields']) && is_array($relatedFieldDef['db_concat_fields'])) {
                 $fieldname = $focus->db->concat($vardef['table'], $relatedFieldDef['db_concat_fields']);
             } else {
                 $fieldname = $vardef['rname'];
             }
             // lookup first record that matches in linked table
             $query = "SELECT id\n                            FROM {$vardef['table']}\n                            WHERE {$fieldname} = '" . $focus->db->quote($value) . "'\n                                AND deleted != 1";
             $result = $focus->db->limitQuery($query, 0, 1, true, "Want only a single row");
             if (!empty($result)) {
                 if ($relaterow = $focus->db->fetchByAssoc($result)) {
                     $focus->{$idField} = $relaterow['id'];
                 } elseif (!$settings->addRelatedBean || $newbean->bean_implements('ACL') && !$newbean->ACLAccess('save') || in_array($newbean->module_dir, array('Teams', 'Users'))) {
                     return false;
                 } else {
                     // add this as a new record in that bean, then relate
                     if (isset($relatedFieldDef['db_concat_fields']) && is_array($relatedFieldDef['db_concat_fields'])) {
                         assignConcatenatedValue($newbean, $relatedFieldDef, $value);
                     } else {
                         $newbean->{$vardef}['rname'] = $value;
                     }
                     if (!isset($focus->assigned_user_id) || $focus->assigned_user_id == '') {
                         $newbean->assigned_user_id = $GLOBALS['current_user']->id;
                     } else {
                         $newbean->assigned_user_id = $focus->assigned_user_id;
                     }
                     if (!isset($focus->modified_user_id) || $focus->modified_user_id == '') {
                         $newbean->modified_user_id = $GLOBALS['current_user']->id;
                     } else {
                         $newbean->modified_user_id = $focus->modified_user_id;
                     }
                     // populate fields from the parent bean to the child bean
                     $focus->populateRelatedBean($newbean);
                     $newbean->save(false);
                     $focus->{$idField} = $newbean->id;
                     $settings->createdBeans[] = ImportFile::writeRowToLastImport($focus->module_dir, $newbean->object_name, $newbean->id);
                 }
             }
         }
     }
     return $value;
 }
Beispiel #17
0
 /**
  * @param $path
  * @param $name string - open|array - upload from tmp ($_FILE[file_name])
  * @throws Exception
  * @return ImportFile
  */
 public static function factory($path, $name)
 {
     if (is_array($name)) {
         $file = new ImportFile($path);
         $file->removeOldFiles();
         $file->upload($name);
     } else {
         if (empty($name)) {
             throw new Exception("File name epmty!");
         }
         $file = new ImportFile($path, $name);
         $file->check();
     }
     return $file;
 }
Beispiel #18
0
      }
      $.cookie("updateversion", $.toJSON(data), { expires: 1 });
    }
  </script>
<?php 
    }
}
if ($strFunc == 'system' && getRequest('operation', '') == 'export' && sesAdminAccess()) {
    createFuncMenu($strFunc);
    require_once 'export.php';
    $export = new ExportData();
    $export->launch();
} elseif ($strFunc == 'system' && getRequest('operation', '') == 'import' && sesAdminAccess()) {
    createFuncMenu($strFunc);
    require_once 'import.php';
    $import = new ImportFile();
    $import->launch();
} elseif ($strFunc == 'import_statement') {
    createFuncMenu($strFunc);
    require_once 'import_statement.php';
    $import = new ImportStatement();
    $import->launch();
} else {
    switch ($strFunc) {
        case 'reports':
            createFuncMenu($strFunc);
            switch ($strForm) {
                case 'invoice':
                    require_once 'invoice_report.php';
                    $invoiceReport = new InvoiceReport();
                    $invoiceReport->createReport();
Beispiel #19
0
 protected function getListViewTableFromFile($fileName, $tableName)
 {
     $has_header = $_REQUEST['has_header'] == 'on' ? TRUE : FALSE;
     $if = new ImportFile($fileName, ",", '"', FALSE, FALSE);
     $if->setHeaderRow($has_header);
     $lv = new ImportListView($if, array('offset' => 0), $tableName);
     return $lv->display(TRUE);
 }
 /**
  * @ticket 48289
  */
 public function testTabDelimiter()
 {
     // create the test file
     $sample_file = $GLOBALS['sugar_config']['upload_dir'] . '/TestCharset.csv';
     copy('tests/modules/Import/TestCharset.csv', $sample_file);
     // use '\t' to simulate the bug
     $importFile = new ImportFile($sample_file, '\\t', '"', false, false);
     $this->assertTrue($importFile->fileExists());
     $c = $importFile->getNextRow();
     $this->assertTrue(is_array($c), 'incorrect return type.');
     $this->assertEquals(1, count($c), 'incorrect array count.');
     // cleanup
     $this->unlink[] = $sample_file;
 }
 /**
  * Validate relate fields
  *
  * @param  $value  string
  * @param  $vardef array
  * @param  $focus  object bean of the module we're importing into
  * @param  $addRelatedBean bool true if we want to add the related bean if it is not found
  * @return string sanitized and validated value on success, bool false on failure
  */
 public function relate($value, $vardef, &$focus, $addRelatedBean = true)
 {
     if (!isset($vardef['module'])) {
         return false;
     }
     $newbean = loadBean($vardef['module']);
     // Bug 27046 - Validate field against type as it is in the related field
     $rvardef = $newbean->getFieldDefinition($vardef['rname']);
     if (isset($rvardef['type']) && method_exists($this, $rvardef['type'])) {
         $fieldtype = $rvardef['type'];
         $returnValue = $this->{$fieldtype}($value, $rvardef);
         if (!$returnValue) {
             return false;
         } else {
             $value = $returnValue;
         }
     }
     if (isset($vardef['id_name'])) {
         $idField = $vardef['id_name'];
         // Bug 24075 - clear out id field value if it is invalid
         if (isset($focus->{$idField})) {
             $checkfocus = loadBean($vardef['module']);
             if ($checkfocus && is_null($checkfocus->retrieve($focus->{$idField}))) {
                 $focus->{$idField} = '';
             }
         }
         // be sure that the id isn't already set for this row
         if (empty($focus->{$idField}) && $idField != $vardef['name'] && !empty($vardef['rname']) && !empty($vardef['table'])) {
             // Bug 27562 - Check db_concat_fields first to see if the field name is a concat
             $relatedFieldDef = $newbean->getFieldDefinition($vardef['rname']);
             if (isset($relatedFieldDef['db_concat_fields']) && is_array($relatedFieldDef['db_concat_fields'])) {
                 $fieldname = db_concat($vardef['table'], $relatedFieldDef['db_concat_fields']);
             } else {
                 $fieldname = $vardef['rname'];
             }
             // lookup first record that matches in linked table
             $query = "SELECT id \n                            FROM {$vardef['table']} \n                            WHERE {$fieldname} = '" . $focus->db->quote($value) . "'\n                                AND deleted != 1";
             $result = $focus->db->limitQuery($query, 0, 1, true, "Want only a single row");
             if (!empty($result)) {
                 if ($relaterow = $focus->db->fetchByAssoc($result)) {
                     $focus->{$idField} = $relaterow['id'];
                 } elseif (!$addRelatedBean || $newbean->bean_implements('ACL') && !$newbean->ACLAccess('save') || in_array($newbean->module_dir, array('Teams', 'Users')) && !is_admin($GLOBALS['current_user'])) {
                     return false;
                 } else {
                     // add this as a new record in that bean, then relate
                     if (isset($relatedFieldDef['db_concat_fields']) && is_array($relatedFieldDef['db_concat_fields'])) {
                         $relatedFieldParts = explode(' ', $value);
                         foreach ($relatedFieldDef['db_concat_fields'] as $relatedField) {
                             $newbean->{$relatedField} = array_shift($relatedFieldParts);
                         }
                     } else {
                         $newbean->{$vardef}['rname'] = $value;
                     }
                     if (!isset($focus->assigned_user_id) || $focus->assigned_user_id == '') {
                         $newbean->assigned_user_id = $GLOBALS['current_user']->id;
                     } else {
                         $newbean->assigned_user_id = $focus->assigned_user_id;
                     }
                     if (!isset($focus->modified_user_id) || $focus->modified_user_id == '') {
                         $newbean->modified_user_id = $GLOBALS['current_user']->id;
                     } else {
                         $newbean->modified_user_id = $focus->modified_user_id;
                     }
                     // populate fields from the parent bean to the child bean
                     $focus->populateRelatedBean($newbean);
                     $newbean->save(false);
                     $focus->{$idField} = $newbean->id;
                     self::$createdBeans[] = array($newbean->object_name, ImportFile::writeRowToLastImport($focus->module_dir, $newbean->object_name, $newbean->id));
                 }
             }
         }
     }
     return $value;
 }
 /**
  * Actually split the file into parts
  *
  * @param string $delimiter 
  * @param string $enclosure
  * @param bool $has_header true if file has a header row
  */
 public function splitSourceFile($delimiter = ',', $enclosure = '"', $has_header = false)
 {
     if (!$this->fileExists()) {
         return false;
     }
     $importFile = new ImportFile($this->_sourceFile, $delimiter, $enclosure, false);
     $filecount = 0;
     $fw = sugar_fopen("{$this->_sourceFile}-{$filecount}", "w");
     $count = 0;
     // skip first row if we have a header row
     if ($has_header && $importFile->getNextRow()) {
         // mark as duplicate to stick header row in the dupes file
         $importFile->markRowAsDuplicate();
         // same for error records file
         $importFile->writeErrorRecord();
     }
     while ($row = $importFile->getNextRow()) {
         // after $this->_recordThreshold rows, close this import file and goto the next one
         if ($count >= $this->_recordThreshold) {
             fclose($fw);
             $filecount++;
             $fw = sugar_fopen("{$this->_sourceFile}-{$filecount}", "w");
             $count = 0;
         }
         // Bug 25119: Trim the enclosure string to remove any blank spaces that may have been added.
         $enclosure = trim($enclosure);
         if (empty($enclosure)) {
             fputs($fw, implode($delimiter, $row) . PHP_EOL);
         } else {
             fputcsv($fw, $row, $delimiter, $enclosure);
         }
         $count++;
     }
     fclose($fw);
     $this->_fileCount = $filecount;
     $this->_recordCount = $filecount * $this->_recordThreshold + $count;
     // increment by one to get true count of files created
     ++$this->_fileCount;
 }