/**
  * Get the available fields
  *
  * @copyright
  * @author 		RolandD
  * @todo
  * @see
  * @access 		protected
  * @param
  * @return 		array	an array of options
  * @since 		4.3
  */
 protected function getOptions()
 {
     // Get the template ID
     $jinput = JFactory::getApplication()->input;
     $session = JFactory::getSession();
     $sess_template_id = $session->get('com_csvi.select_template', 0);
     if ($sess_template_id !== 0) {
         $sess_template_id = unserialize($sess_template_id);
     }
     $template_id = $jinput->get('template_id', $sess_template_id, 'int');
     // Load the selected template
     require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/template.php';
     $template = new CsviTemplate();
     $template->load($template_id);
     // Load the available fields
     require_once JPATH_COMPONENT_ADMINISTRATOR . '/models/availablefields.php';
     $availablefields_model = new CsviModelAvailablefields();
     $fields = $availablefields_model->getAvailableFields($template->get('operation', 'options'), $template->get('component', 'options'), 'array');
     if (!is_array($fields)) {
         $avfields = array();
     } else {
         $avfields = array();
         foreach ($fields as $field) {
             $avfields[$field] = $field;
         }
     }
     return array_merge(parent::getOptions(), $avfields);
 }
Beispiel #2
0
 /**
  * Prepare for export
  *
  * @copyright
  * @author 		RolandD
  * @todo
  * @see
  * @access 		public
  * @param
  * @return
  * @since 		3.0
  */
 public function getPrepareExport()
 {
     // Load the basics
     $jinput = JFactory::getApplication()->input;
     $db = JFactory::getDbo();
     $exportfile_model = $this->_getModel('exportfile');
     // Load the backend language file
     $lang = JFactory::getLanguage();
     $lang->load('com_csvi', JPATH_ADMINISTRATOR);
     // Load the template
     $template = new CsviTemplate();
     $template->load($jinput->get('template_id', 0, 'int'));
     $template->set('exportto', 'general', 'tofront');
     $jinput->set('template', $template);
     // Set the export type
     $jinput->set('export_type', $template->get('operation', 'options'));
     // Initiate the log
     $csvilog = new CsviLog();
     // Create a new Import ID in the logger
     $csvilog->setId();
     // Set to collect debug info
     $csvilog->setDebug($template->get('collect_debug_info', 'general'));
     // Set some log info
     $csvilog->SetAction('export');
     $csvilog->SetActionType($template->get('export_type'), $template->getName());
     // Add the logger to the registry
     $jinput->set('csvilog', $csvilog);
     // Load the fields to export
     $exportfields = $exportfile_model->getExportFields();
     if (!empty($exportfields)) {
         $jinput->set('export.fields', $exportfields);
         // Allow big SQL selects
         $db->setQuery("SET OPTION SQL_BIG_SELECTS=1");
         $db->query();
         // Get the filename for the export file
         $jinput->set('export.filename', $exportfile_model->exportFilename());
         // See if we need to get an XML/HTML class
         $export_format = $template->get('export_file', 'general');
         if ($export_format == 'xml' || $export_format == 'html') {
             $exportclass = $exportfile_model->getExportClass();
             if ($exportclass) {
                 $jinput->set('export.class', $exportclass);
             } else {
                 $csvilog->AddStats('incorrect', JText::_('COM_CSVI_NO_EXPORT_CLASS'));
                 $jinput->set('logcount', 0);
                 return false;
             }
         }
         // Return all is good
         return true;
     } else {
         $csvilog->AddStats('incorrect', JText::_('COM_CSVI_NO_EXPORT_FIELDS'));
         $jinput->set('logcount', 0);
         return false;
     }
 }
Beispiel #3
0
 /**
  * Build the command to use for the cron command to do an import/export
  *
  * @copyright
  * @author 		RolandD
  * @todo
  * @see
  * @access 		public
  * @param
  * @return 		string	the parameters for the cron line
  * @since 		3.0
  */
 public function getCronLine()
 {
     $jinput = JFactory::getApplication()->input;
     $db = JFactory::getDbo();
     $settings = $jinput->get('com_csvi.data', array(), 'array');
     $cronline = '';
     $notemplate = false;
     $details = new StdClass();
     // Get the template used
     $template_id = $jinput->get('template_id', 0, 'int');
     if ($template_id) {
         $cronline .= ' template_id="' . $template_id . '"';
         // Load the template settings to compare against selection
         $query = $db->getQuery(true);
         $query->select('settings');
         $query->from('#__csvi_template_settings');
         $query->where('id = ' . $template_id);
         $db->setQuery($query);
         $template = new CsviTemplate(json_decode($db->loadResult(), true));
         $details->type = $template->get('action', 'options');
     } else {
         $notemplate = true;
         // Initialise the details
         $details->type = $settings['options']['action'];
     }
     // Check if this is an import or export cron
     if ($details->type == 'export') {
         foreach ($settings as $group => $values) {
             switch ($group) {
                 case 'options':
                     break;
                 case 'general':
                     if ($notemplate) {
                         $general = $settings['general'];
                     } else {
                         $general = CsviHelper::recurseArrayDiff($settings['general'], $template->get('general'));
                     }
                     foreach ($general as $name => $setting) {
                         switch ($name) {
                             case 'exportto':
                                 if (!empty($setting)) {
                                     if ($setting == 'todownload') {
                                         $setting = 'tofile';
                                     }
                                     $cronline .= ' jform:general:' . $name . '="' . $setting . '" ';
                                 }
                                 break;
                             case 'localpath':
                                 if (!empty($setting)) {
                                     if ($template->get('exportto', 'general') == 'todownload' || $template->get('exportto', 'general') == 'tofile') {
                                         $cronline .= ' jform:general:' . $name . '="' . $setting . '" ';
                                     }
                                 }
                                 break;
                             default:
                                 if (!empty($setting)) {
                                     $cronline .= ' jform:general:' . $name . '="' . $setting . '" ';
                                 }
                                 break;
                         }
                     }
                     break;
                 case 'export_fields':
                     if ($notemplate) {
                         if (array_key_exists('export_fields', $settings)) {
                             $fields = $settings['export_fields'];
                         } else {
                             $fields = array();
                         }
                     } else {
                         $fields = $template->get('export_fields', '', array());
                     }
                     if (!empty($fields)) {
                         $fields['_selected_name'] = CsviHelper::recurseArrayDiff($settings['export_fields']['_selected_name'], $fields['_selected_name']);
                         if (!empty($fields['_selected_name'])) {
                             $cronline .= ' jform:export_fields:_selected_name = "' . implode('|', $settings['export_fields']['_selected_name']) . '|"';
                             $cronline .= ' jform:export_fields:_column_header = "' . implode('|', $settings['export_fields']['_column_header']) . '|"';
                             $cronline .= ' jform:export_fields:_default_value = "' . implode('|', $settings['export_fields']['_default_value']) . '|"';
                             $cronline .= ' jform:export_fields:_process_field = "' . implode('|', $settings['export_fields']['_process_field']) . '|"';
                         }
                     }
                     break;
                 default:
                     if ($notemplate) {
                         $values = $settings[$group];
                     } else {
                         $values = CsviHelper::recurseArrayDiff($settings[$group], $template->get($group));
                     }
                     $cronline .= $this->_getCronSetting($values, $group);
                     break;
             }
         }
     } else {
         if ($details->type == 'import') {
             foreach ($settings as $group => $values) {
                 switch ($group) {
                     case 'options':
                         break;
                     case 'import_fields':
                         if ($notemplate) {
                             if (array_key_exists('import_fields', $settings)) {
                                 $fields = $settings['import_fields'];
                             } else {
                                 $fields = array();
                             }
                         } else {
                             // Create a default value
                             $default = array();
                             $default['_selected_name'][] = '';
                             $fields = $template->get('import_fields', '', $default);
                             $fields['_selected_name'] = CsviHelper::recurseArrayDiff($settings['import_fields']['_selected_name'], $fields['_selected_name']);
                         }
                         if (!empty($fields)) {
                             if (!empty($fields['_selected_name'])) {
                                 $cronline .= ' jform:import_fields:_selected_name = "' . implode('|', $settings['import_fields']['_selected_name']) . '|"';
                                 $cronline .= ' jform:import_fields:_default_value = "' . implode('|', $settings['import_fields']['_default_value']) . '|"';
                                 $cronline .= ' jform:import_fields:_process_field = "' . implode('|', $settings['import_fields']['_process_field']) . '|"';
                             }
                         }
                         break;
                     default:
                         if ($notemplate) {
                             $values = $settings[$group];
                             if (!is_array($values)) {
                                 $values = array();
                                 $values[$group] = $settings[$group];
                             }
                         } else {
                             $values = CsviHelper::recurseArrayDiff($settings[$group], $template->get($group));
                         }
                         $cronline .= $this->_getCronSetting($values, $group);
                         break;
                 }
             }
         }
     }
     return $cronline;
 }
Beispiel #4
0
 /**
  * Import the files
  *
  * @copyright
  * @author 		RolandD
  * @todo
  * @see
  * @access 		public
  * @param
  * @return 		string	JSON encoded text
  * @since 		3.0
  */
 public function display($tpl = null)
 {
     $jinput = JFactory::getApplication()->input;
     if ($jinput->get('importsession', true, 'bool')) {
         // Process the data
         $this->get('ProcessData');
         // Empty the message stack
         $session = JFactory::getSession();
         $session->set('application.queue', null);
         // Collect the results
         $result = array();
         // Set the view mode
         if ($jinput->get('csvipreview', false, 'bool')) {
             $result['view'] = 'preview';
             $result['headers'] = $jinput->get('headers_preview', null, null);
             $result['output'] = $jinput->get('data_preview', null, null);
             if (empty($results['headers']) && empty($result['output'])) {
                 $result['process'] = false;
                 $csvilog = $jinput->get('csvilog', null, null);
                 $result['url'] = JURI::root() . 'administrator/index.php?option=com_csvi&task=process.finished&run_id=' . $csvilog->getId();
                 // Clean the session, nothing to import
                 $this->get('CleanSession');
             } else {
                 $result['process'] = true;
             }
         } else {
             $result['view'] = '';
             // Get the number of records processed
             $result['records'] = $jinput->get('recordsprocessed', 0, 'int');
             if ($result['records'] == 0 || $jinput->get('finished', false)) {
                 $result['process'] = false;
                 $result['url'] = JURI::root() . 'administrator/index.php?option=com_csvi&task=process.finished&run_id=' . $jinput->get('run_id', 0, 'int');
             } else {
                 // Check if we are finished
                 $result['process'] = true;
             }
         }
     } else {
         // Collect the results
         $result = array();
         $result['process'] = false;
         $result['url'] = JURI::root() . 'administrator/index.php?option=com_csvi&task=process.finished&run_id=' . $jinput->get('run_id', 0, 'int');
         // Clean the session, nothing to import
         $this->get('CleanSession');
     }
     // If the import is finished, call the plugins
     if (!$result['process']) {
         // Load the template
         $session = JFactory::getSession();
         $template = new CsviTemplate();
         $template->load(unserialize($session->get('com_csvi.select_template')));
         $options = array();
         $options[] = $template->get('options');
         $dispatcher = JDispatcher::getInstance();
         JPluginHelper::importPlugin('csvi');
         $dispatcher->trigger('onImportComplete', $options);
     } else {
         // Import is not finished, lets sleep
         if ($jinput->get('currentline', 0, 'int') > 0 && !$jinput->get('finished', false)) {
             $settings = new CsviSettings();
             sleep($settings->get('import.import_wait', 0));
         }
     }
     // Output the results
     echo json_encode($result);
 }
Beispiel #5
0
 /**
  * Process an uploaded file with headers
  *
  * @copyright
  * @author 		RolandD
  * @todo
  * @see
  * @access
  * @param
  * @return
  * @since 		5.8
  */
 public function processFile($pk, $validData)
 {
     $jinput = JFactory::getApplication()->input;
     // Get the file details
     $upload = array();
     $upload['name'] = $_FILES['jform']['name']['mapfile'];
     $upload['type'] = $_FILES['jform']['type']['mapfile'];
     $upload['tmp_name'] = $_FILES['jform']['tmp_name']['mapfile'];
     $upload['error'] = $_FILES['jform']['error']['mapfile'];
     if (!$upload['error']) {
         // Move the temporary file
         if (is_uploaded_file($upload['tmp_name'])) {
             // Get some basic info
             jimport('joomla.filesystem.file');
             jimport('joomla.filesystem.folder');
             $folder = CSVIPATH_TMP . '/' . time();
             $upload_parts = pathinfo($upload['name']);
             // Create the temp folder
             if (JFolder::create($folder)) {
                 // Move the uploaded file to its temp location
                 if (JFile::upload($upload['tmp_name'], $folder . '/' . $upload['name'])) {
                     if (array_key_exists('extension', $upload_parts)) {
                         // Load the base class
                         require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/file.php';
                         // Load the extension specific class
                         switch ($upload_parts['extension']) {
                             case 'xml':
                                 require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/file/import/xml.php';
                                 $fileclass = 'Xml';
                                 break;
                             case 'xls':
                                 require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/file/import/xls.php';
                                 require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/file/import/excel_reader2.php';
                                 $fileclass = 'Xls';
                                 break;
                             case 'ods':
                                 require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/file/import/ods.php';
                                 require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/file/import/ods_reader.php';
                                 $fileclass = 'Ods';
                                 break;
                             default:
                                 // Treat any unknown type as CSV
                                 require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/file/import/csv.php';
                                 $fileclass = 'Csv';
                                 break;
                         }
                         // Set the file class name
                         $fileclass .= 'File';
                         // Get a template object
                         if (!class_exists('CsviTemplate')) {
                             require JPATH_COMPONENT_ADMINISTRATOR . '/helpers/template.php';
                         }
                         $template = new CsviTemplate();
                         $template->set('source', 'general', 'fromserver');
                         $template->set('local_csv_file', 'general', $folder . '/' . $upload['name']);
                         $jinput->set('template', $template);
                         // Get the file handler
                         $file = new $fileclass();
                         // Validate and process the file
                         $file->validateFile();
                         $file->processFile();
                         // Get the header
                         if ($file->loadColumnHeaders()) {
                             $header = $jinput->get('columnheaders', array(), 'array');
                             if (is_array($header)) {
                                 // Load the table
                                 $table = $this->getTable('mapheaders');
                                 // Remove existing entries
                                 $db = JFactory::getDbo();
                                 $query = $db->getQuery(true)->delete($db->qn('#__csvi_mapheaders'))->where($db->qn('map_id') . '=' . $pk);
                                 $db->setQuery($query);
                                 $db->query();
                                 // Store the headers
                                 $map = array();
                                 $map['map_id'] = $pk;
                                 foreach ($header as $name) {
                                     $map['csvheader'] = $name;
                                     // Store the data
                                     $table->save($map);
                                     $table->reset();
                                 }
                             } else {
                                 return false;
                             }
                         } else {
                             return false;
                         }
                     } else {
                         return false;
                     }
                 } else {
                     return false;
                 }
             } else {
                 return false;
             }
         } else {
             return false;
         }
     }
 }
Beispiel #6
0
 /**
  * Export for front-end
  *
  * @copyright
  * @author 		RolandD
  * @todo
  * @see
  * @access 		public
  * @param
  * @return
  * @since 		3.0
  */
 public function export()
 {
     // Create the view
     $view = $this->getView('export', 'raw');
     // Add the export model
     $view->setModel($this->getModel('export', 'CsviModel'), true);
     // Add the export model path
     $this->addModelPath(JPATH_COMPONENT_ADMINISTRATOR . '/models');
     // General export functions
     $view->setModel($this->getModel('exportfile', 'CsviModel'));
     // Log functions
     $view->setModel($this->getModel('log', 'CsviModel'));
     // Settings functions
     $view->setModel($this->getModel('settings', 'CsviModel'));
     // General category functions
     $view->setModel($this->getModel('category', 'CsviModel'));
     // Available fields
     $view->setModel($this->getModel('availablefields', 'CsviModel'));
     // Load the model
     $model = $this->getModel('exportfile');
     // Add extra helper paths
     $view->addHelperPath(JPATH_COMPONENT_ADMINISTRATOR . '/helpers');
     $view->addHelperPath(JPATH_COMPONENT_ADMINISTRATOR . '/helpers/xml');
     $view->addHelperPath(JPATH_COMPONENT_ADMINISTRATOR . '/helpers/html');
     // Load the helper classes
     $view->loadHelper('csvidb');
     $view->loadHelper('template');
     $view->loadHelper('csvisef');
     // Load the template details
     $jinput = JFactory::getApplication()->input;
     $template = new CsviTemplate();
     $template->load($jinput->get('template_id', 0, 'int'));
     $jinput->set('jform', $template->getSettings());
     // Set the output destination
     $template->set('exportto', 'general', $jinput->get('exportto', 'tofront', 'cmd'));
     $jinput->set('template', $template);
     if ($template->get('action', 'options', 'export') == 'export') {
         // Prepare for export
         if ($model->getPrepareExport()) {
             // Set the export override
             $app = JFactory::getApplication();
             $jinput = JFactory::getApplication()->input;
             $overridefile = JPATH_BASE . '/templates/' . $app->getTemplate() . '/html/com_csvi/models/export/' . $template->get('operation', 'options') . '.php';
             // Add the export model path if override exists
             if (file_exists($overridefile)) {
                 $this->addModelPath(JPATH_BASE . '/templates/' . $app->getTemplate() . '/html/com_csvi/models/' . $template->get('component', 'options') . '/export');
             } else {
                 $this->addModelPath(JPATH_COMPONENT_ADMINISTRATOR . '/models/' . $template->get('component', 'options') . '/export');
             }
             // Load export specifc helper
             $view->loadHelper($template->get('component', 'options'));
             $view->loadHelper($template->get('component', 'options') . '_config');
             // Display it all
             $view->display();
         } else {
             // Clean up first
             $model->getCleanSession();
             // Add appropriate message
         }
     } else {
         // Add appropriate message
     }
 }