public function nextStep($action, array $param = array())
{
switch ($action) {
case Horde_Data::IMPORT_FILE:
parent::nextStep($action, $param);
/* Move uploaded file so that we can read it again in the next
step after the user gave some format details. */
$file_name = $_FILES['import_file']['tmp_name'];
if (($file_data = file_get_contents($file_name)) === false) {
throw new Horde_Data_Exception(Horde_Data_Translation::t("The uploaded file could not be saved."));
}
/* Do charset checking now, if requested. */
if (isset($param['check_charset'])) {
$charset = isset($this->_vars->charset) ? Horde_String::lower($this->_vars->charset) : 'utf-8';
switch ($charset) {
case 'utf-8':
$error = !Horde_String::validUtf8($file_data);
break;
default:
$error = $file_data != Horde_String::convertCharset(Horde_String::convertCharset($file_data, $charset, 'UTF-8'), 'UTF-8', $charset);
break;
}
if ($error) {
$e = new Horde_Data_Exception_Charset(Horde_Data_Translation::t("Incorrect charset given for the data."));
$e->badCharset = $charset;
throw $e;
}
}
$this->storage->set('charset', $this->_vars->charset);
$this->storage->set('file_data', $file_data);
/* Read the file's first two lines to show them to the user. */
$first_lines = '';
if ($fp = @fopen($file_name, 'r')) {
for ($line_no = 1, $line = fgets($fp); $line_no <= 3 && $line; $line_no++, $line = fgets($fp)) {
$line = Horde_String::convertCharset($line, $this->_vars->charset, 'UTF-8');
$first_lines .= Horde_String::truncate($line);
if (Horde_String::length($line) > 100) {
$first_lines .= "\n";
}
}
}
$this->storage->set('first_lines', $first_lines);
/* Import the first line to guess the number of fields. */
if ($first_lines) {
rewind($fp);
$line = self::getCsv($fp);
if ($line) {
$this->storage->set('fields', count($line));
}
}
return Horde_Data::IMPORT_CSV;
case Horde_Data::IMPORT_CSV:
$this->storage->set('header', $this->_vars->header);
$import_mapping = array();
if (isset($param['import_mapping'])) {
$import_mapping = $param['import_mapping'];
}
$file_name = Horde_Util::getTempFile('import');
file_put_contents($file_name, $this->storage->get('file_data'));
$this->storage->set('data', $this->importFile($file_name, $this->_vars->header, $this->_vars->sep, $this->_vars->quote, $this->_vars->fields, $import_mapping, $this->storage->get('charset'), $this->storage->get('crlf')));
$this->storage->set('map');
return Horde_Data::IMPORT_MAPPED;
default:
return parent::nextStep($action, $param);
}
}