Author: Jan Schneider (jan@horde.org)
Inheritance: extends Horde_Translation_Autodetect
Ejemplo n.º 1
0
 /**
  *
  * @throws Horde_Data_Exception
  */
 public function importData($text)
 {
     $this->_iCal = new Horde_Icalendar();
     if (!$this->_iCal->parsevCalendar($text)) {
         throw new Horde_Data_Exception(Horde_Data_Translation::t("There was an error importing the iCalendar data."));
     }
     return $this->_iCal->getComponents();
 }
Ejemplo n.º 2
0
Archivo: Csv.php Proyecto: horde/horde
 /**
  * Takes all necessary actions for the given import step, parameters and
  * form values and returns the next necessary step.
  *
  * @param integer $action  The current step. One of the IMPORT_* constants.
  * @param array $param     An associative array containing needed
  *                         parameters for the current step. Keys for this
  *                         driver:
  *   - check_charset: (boolean) Do some checks to see if the correct
  *                    charset has been provided. Throws charset exception
  *                    on error.
  *   - import_mapping: TODO
  *
  * @return mixed  Either the next step as an integer constant or imported
  *                data set after the final step.
  * @throws Horde_Data_Exception
  * @throws Horde_Data_Exception_Charset
  */
 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);
     }
 }
Ejemplo n.º 3
0
Archivo: Base.php Proyecto: horde/horde
 /**
  * Takes all necessary actions for the given import step, parameters and
  * form values and returns the next necessary step.
  *
  * @param integer $action  The current step. One of the IMPORT_* constants.
  * @param array $param     An associative array containing needed
  *                         parameters for the current step.
  *
  * @return mixed  Either the next step as an integer constant or imported
  *                data set after the final step.
  * @throws Horde_Data_Exception
  */
 public function nextStep($action, array $param = array())
 {
     /* First step. */
     if (is_null($action)) {
         return Horde_Data::IMPORT_FILE;
     }
     switch ($action) {
         case Horde_Data::IMPORT_FILE:
             if (!isset($this->_browser)) {
                 throw new LogicException('Missing browser parameter.');
             }
             /* Sanitize uploaded file. */
             try {
                 $this->_browser->wasFileUploaded('import_file', $param['file_types'][$this->_vars->import_format]);
             } catch (Horde_Exception $e) {
                 throw new Horde_Data_Exception($e);
             }
             if ($_FILES['import_file']['size'] <= 0) {
                 throw new Horde_Data_Exception(Horde_Data_Translation::t("The file contained no data."));
             }
             $this->storage->set('format', $this->_vars->import_format);
             break;
         case Horde_Data::IMPORT_MAPPED:
             if (!$this->_vars->dataKeys || !$this->_vars->appKeys) {
                 throw new Horde_Data_Exception(Horde_Data_Translation::t("You didn\\'t map any fields from the imported file to the corresponding fields."));
             }
             $dataKeys = explode("\t", $this->_vars->dataKeys);
             $appKeys = explode("\t", $this->_vars->appKeys);
             $dates = $map = array();
             if (!($import_data = $this->storage->get('data'))) {
                 $import_data = array();
             }
             foreach ($appKeys as $key => $app) {
                 $map[$dataKeys[$key]] = $app;
                 if (isset($param['time_fields']) && isset($param['time_fields'][$app])) {
                     $dates[$dataKeys[$key]]['type'] = $param['time_fields'][$app];
                     $dates[$dataKeys[$key]]['values'] = array();
                     $i = 0;
                     /* Build an example array of up to 10 date/time fields. */
                     while ($i < count($import_data) && count($dates[$dataKeys[$key]]['values']) < 10) {
                         if (!empty($import_data[$i][$dataKeys[$key]])) {
                             $dates[$dataKeys[$key]]['values'][] = $import_data[$i][$dataKeys[$key]];
                         }
                         ++$i;
                     }
                 }
             }
             $this->storage->set('map', $map);
             if (count($dates) > 0) {
                 foreach ($dates as $key => $data) {
                     if (count($data['values'])) {
                         $this->storage->set('dates', $dates);
                         return Horde_Data::IMPORT_DATETIME;
                     }
                 }
             }
             return $this->nextStep(Horde_Data::IMPORT_DATA, $param);
         case Horde_Data::IMPORT_DATETIME:
         case Horde_Data::IMPORT_DATA:
             if ($action == Horde_Data::IMPORT_DATETIME) {
                 $params = array('delimiter' => $this->_vars->delimiter, 'format' => $this->_vars->format, 'order' => $this->_vars->order, 'day_delimiter' => $this->_vars->day_delimiter, 'day_format' => $this->_vars->day_format, 'time_delimiter' => $this->_vars->time_delimiter, 'time_format' => $this->_vars->time_format);
             }
             if (!$this->storage->exists('data')) {
                 throw new Horde_Data_Exception(Horde_Data_Translation::t("The uploaded data was lost since the previous step."));
             }
             /* Build the result data set as an associative array. */
             $data = array();
             $data_map = $this->storage->get('map');
             foreach ($this->storage->get('data') as $row) {
                 $data_row = array();
                 foreach ($row as $key => $val) {
                     if (isset($data_map[$key])) {
                         $mapped_key = $data_map[$key];
                         if ($action == Horde_Data::IMPORT_DATETIME && !empty($val) && isset($param['time_fields']) && isset($param['time_fields'][$mapped_key])) {
                             $val = $this->_mapDate($val, $param['time_fields'][$mapped_key], $params, $key);
                         }
                         $data_row[$mapped_key] = $val;
                     }
                 }
                 $data[] = $data_row;
             }
             return $data;
     }
 }
Ejemplo n.º 4
0
 /**
  * Returns the plural translation of a message.
  *
  * @param string $singular  The singular version to translate.
  * @param string $plural    The plural version to translate.
  * @param integer $number   The number that determines singular vs. plural.
  *
  * @return string  The string translation, or the original string if no
  *                 translation exists.
  */
 public static function ngettext($singular, $plural, $number)
 {
     self::$_domain = 'Horde_Data';
     self::$_directory = '@data_dir@' == '@' . 'data_dir' . '@' ? __DIR__ . '/../../../locale' : '@data_dir@/Horde_Data/locale';
     return parent::ngettext($singular, $plural, $number);
 }
Ejemplo n.º 5
0
 /**
  * Takes all necessary actions for the given import step, parameters and
  * form values and returns the next necessary step.
  *
  * @param integer $action  The current step. One of the IMPORT_* constants.
  * @param array $param     An associative array containing needed
  *                         parameters for the current step.
  *
  * @return mixed  Either the next step as an integer constant or imported
  *                data set after the final step.
  * @throws Horde_Data_Exception
  */
 public function nextStep($action, array $param = array())
 {
     switch ($action) {
         case Horde_Data::IMPORT_FILE:
             parent::nextStep($action, $param);
             $format = $this->storage->get('format');
             if (in_array($format, array('mulberry', 'pine'))) {
                 $filedata = $this->importFile($_FILES['import_file']['tmp_name']);
                 switch ($format) {
                     case 'mulberry':
                         $appKeys = array('alias', 'name', 'email', 'company', 'workAddress', 'workPhone', 'homePhone', 'fax', 'notes');
                         $dataKeys = array(0, 1, 2, 3, 4, 5, 6, 7, 9);
                         break;
                     case 'pine':
                         $appKeys = array('alias', 'name', 'email', 'notes');
                         $dataKeys = array(0, 1, 2, 4);
                         break;
                 }
                 foreach ($appKeys as $key => $app) {
                     $map[$dataKeys[$key]] = $app;
                 }
                 $data = array();
                 foreach ($filedata as $row) {
                     $hash = array();
                     switch ($format) {
                         case 'mulberry':
                             if (preg_match("/^Grp:/", $row[0]) || empty($row[1])) {
                                 continue;
                             }
                             $row[1] = preg_replace('/^([^,"]+),\\s*(.*)$/', '$2 $1', $row[1]);
                             foreach ($dataKeys as $key) {
                                 if (array_key_exists($key, $row)) {
                                     $hash[$key] = stripslashes(preg_replace('/\\\\r/', "\n", $row[$key]));
                                 }
                             }
                             break;
                         case 'pine':
                             if (count($row) < 3 || preg_match("/^#DELETED/", $row[0]) || preg_match("/[()]/", $row[2])) {
                                 continue;
                             }
                             $row[1] = preg_replace('/^([^,"]+),\\s*(.*)$/', '$2 $1', $row[1]);
                             /* Address can be a full RFC822 address */
                             $addr_ob = new Horde_Mail_Rfc822_Address($row[2]);
                             if (!$addr_ob->valid) {
                                 continue;
                             }
                             $row[2] = $addr_ob->bare_address;
                             if (empty($row[1]) && !is_null($addr_ob->personal)) {
                                 $row[1] = $addr_ob->personal;
                             }
                             foreach ($dataKeys as $key) {
                                 if (array_key_exists($key, $row)) {
                                     $hash[$key] = $row[$key];
                                 }
                             }
                             break;
                     }
                     $data[] = $hash;
                 }
                 $this->storage->set('data', $data);
                 $this->storage->set('map', $map);
                 return $this->nextStep(Horde_Data::IMPORT_DATA, $param);
             }
             /* Store uploaded file data so that we can read it again in the
              * next step after the user gives some format details. */
             try {
                 $this->_browser->wasFileUploaded('import_file', Horde_Data_Translation::t("TSV file"));
             } catch (Horde_Browser_Exception $e) {
                 throw new Horde_Data_Exception($e);
             }
             $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."));
             }
             $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')) {
                 $line_no = 1;
                 while ($line_no < 3 && ($line = fgets($fp))) {
                     $newline = Horde_String::length($line) > 100 ? "\n" : '';
                     $first_lines .= substr($line, 0, 100) . $newline;
                     ++$line_no;
                 }
             }
             $this->storage->set('first_lines', $first_lines);
             return Horde_Data::IMPORT_TSV;
         case Horde_Data::IMPORT_TSV:
             $file_name = Horde_Util::getTempFile('import');
             file_put_contents($file_name, $this->storage->get('file_data'));
             $this->storage->set('header', $this->_vars->header);
             $this->storage->set('data', $this->importFile($file_name, $this->storage->get('header')));
             $this->storage->set('map');
             return Horde_Data::IMPORT_MAPPED;
     }
     return parent::nextStep($action, $param);
 }