Example #1
0
	/**
	 * Generic second step of the importer. Handles uploaded files, parses out first row and shows field matching page.
	 */
	protected function _ImportStep2()
	{
		$importer = new ISC_ADMIN_CSVPARSER;

		// Haven't been to this step before, need to parse CSV file
		if (!isset($this->ImportSession['FieldSeparator'])) {
			if (isset($_POST['Headers'])) {
				$this->ImportSession['Headers'] = $_POST['Headers'];
			}

			if (isset($_POST['OverrideDuplicates'])) {
				$this->ImportSession['OverrideDuplicates'] = $_POST['OverrideDuplicates'];
			}

			// Using a file off the server
			if (isset($_POST['serverfile']) && $_POST['serverfile'] != "") {
				$_POST['serverfile'] = basename($_POST['serverfile']);
				if (!is_file($this->ServerImportDirectory . "/".  $_POST['serverfile'])) {
					$this->_ImportStep1(GetLang('ImportInvalidServerFile'), MSG_ERROR);
					$GLOBALS['ISC_CLASS_ADMIN_ENGINE']->PrintFooter();
					exit;
				}
				$newfilename = $this->ServerImportDirectory . '/' . $_POST['serverfile'];
			} else {
				if (!isset($_FILES['importfile'])) {
					$this->_ImportStep1($this->_GetUploadError(0), MSG_ERROR);
					$GLOBALS['ISC_CLASS_ADMIN_ENGINE']->PrintFooter();
					exit;
				}
				if (!is_uploaded_file($_FILES['importfile']['tmp_name']) || $_FILES['importfile']['error']) {
					$this->_ImportStep1($this->_GetUploadError($_FILES['importfile']['error']), MSG_ERROR);
					$GLOBALS['ISC_CLASS_ADMIN_ENGINE']->PrintFooter();
					exit;
				}

				// Move the uploaded file to the cache directory temporarily with a new unique name
				while(true) {
					$newfilename = ISC_TMP_IMPORT_DIRECTORY . '/' . $this->type . '-import-' . md5(uniqid(rand(), true));
					if (!is_file($newfilename)) {
						break;
					}
				}

				if (!move_uploaded_file($_FILES['importfile']['tmp_name'], $newfilename)) {
					$this->_ImportStep1(GetLang('ImportUploadMoveFailed'), MSG_ERROR);
					$GLOBALS['ISC_CLASS_ADMIN_ENGINE']->PrintFooter();
					exit;
				}
			}

			$separator = html_entity_decode($_POST['FieldSeparator']);
			// convert to actual tab separator
			if (trim(isc_strtoupper($separator)) == "TAB") {
				$separator = "	";
			}

			$this->ImportSession['FieldEnclosure'] = html_entity_decode($_POST['FieldEnclosure']);
			$this->ImportSession['FieldSeparator'] = $separator;

			if (isset($this->ImportSession['FieldSeparator']) && $this->ImportSession['FieldSeparator'] != "") {
				$importer->FieldSeparator = $this->ImportSession['FieldSeparator'];
			}

			if (isset($this->ImportSession['FieldEnclosure']) && $this->ImportSession['FieldEnclosure'] != "") {
				$importer->FieldEnclosure = $this->ImportSession['FieldEnclosure'];
			}

			$this->ImportSession['ImportFile'] = $newfilename;

			$importer->OpenCSVFile($newfilename);
			$header = $importer->FetchNextRecord();
			$importer->CloseCSVFile();

			$this->ImportSession['TotalFileSize'] = filesize($newfilename);
			$this->ImportSession['LastPosition'] = 0;
			$this->ImportSession['PageSize'] = 3000;

			if (!$header) {
				$this->_ImportStep1('Invalid file', MSG_ERROR);
				$GLOBALS['ISC_CLASS_ADMIN_ENGINE']->PrintFooter();
				exit;
			}

			if (isset($_POST['Headers']) && $_POST['Headers'] == 1) {
				$this->ImportSession['Header'] = $header;
			}
		}
		// Already been past this step once, no need to reparse CSV file
		else {
			$importer->OpenCSVFile($this->ImportSession['ImportFile']);
			$header = $importer->FetchNextRecord();
			$importer->CloseCSVFile();
		}

		$this->_PreFieldMatch($header);

		$fieldlist = '';
		foreach($this->_ImportFields as $column => $field) {
			$fieldlist .= $this->_buildMatchField($column, $field, $header);
		}

		$GLOBALS['ImportFieldList'] = $fieldlist;

		$GLOBALS['ImportSession'] = $_REQUEST['ImportSession'];
		$this->SaveImportSession();

		$GLOBALS['ISC_CLASS_ADMIN_ENGINE']->PrintHeader();
		$this->template->display('import.'.$this->type.'.step2.tpl');
		$GLOBALS['ISC_CLASS_ADMIN_ENGINE']->PrintFooter();
	}