/** * 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); }
/** * 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; }
/** * 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); }