Sindbad~EG File Manager

Current Path : /var/www/ispania.gr/plugins/rsdirectory/importcsv/
Upload File :
Current File : /var/www/ispania.gr/plugins/rsdirectory/importcsv/importcsv.php

<?php
/**
 * @package	RSDirectory!
 * @copyright	(c) 2013 - 2021 RSJoomla!
 * @link		https://www.rsjoomla.com
 * @license	GNU General Public License http://www.gnu.org/licenses/gpl-3.0.en.html
 */

// No direct access.
defined('_JEXEC') or die('Restricted access');

/**
 * RSDirectory! CSV Import Plugin.
 */
class PlgRsdirectoryImportCSV extends JPlugin
{
    const IMPORT_OPTION = 'csv';
    const PLUGIN = 'importcsv';
    const EXTENSION = 'plg_rsdirectory_importcsv';
    const OPTION = 'PLG_RSDIRECTORY_IMPORTCSV_NAME';
	const VERSION = '1.0.1';
		
	/**
	 * An errors array.
	 *
	 * @access protected
	 *
	 * @var array
	 */
	protected $errors = array();
		
	/**
	 * Automatically load language files.
	 *
	 * @access protected
	 *
	 * @var boolean
	 */
	protected $autoloadLanguage = true;
		
	/**
	 * An array of columns to skip.
	 *
	 * @access protected
	 *
	 * @static
	 *
	 * @var array
	 */
	protected static $skippedColumns = array(
		'category_id',
		'form_id'
	);

	protected $import_path;

    protected function getImportPath()
	{
		if ($this->import_path === null)
		{
			// Initialize import path.
			$this->import_path = JPATH_ROOT . '/components/com_rsdirectory/files/import/';
		}

		return $this->import_path;
	}
        
    /**
     * Can the plugin run?
     *
     * @access public
     * 
     * @return bool
     */
    public function canRun()
    {
		$app = JFactory::getApplication();
			
        return file_exists(JPATH_ADMINISTRATOR . '/components/com_rsdirectory/helpers/rsdirectory.php') &&
			   $app->isClient('administrator') && $app->input->get('option') == 'com_rsdirectory';
    }
     
    /**
     * Add the current import option to the import options list.
     *
     * @access public
     * 
     * @return string
     */
    public function onRsdirectoryaddImportOptions()
    {
		if ( $this->canRun() )
		{
			return JHtml::_( 'select.option', self::IMPORT_OPTION, JText::_(self::OPTION) );
		}
    }
		
	/**
	 * Display import fieldset.
	 *
	 * @access public
	 */	
	public function onRsdirectoryDisplayImportFieldset()
	{
		if ( !$this->canRun() )
		return;
			
		$style = '#csvSelectColumns {
					position: relative;
					background: #fff;
					padding: 20px;
					width: auto;
					margin: 20px auto;
				}
					
				#csvSelectColumns .control-group {
					margin: 0;
				}';

		JFactory::getApplication()->getDocument()->addStyleDeclaration($style);
		RSDirectoryHelper::loadMagnificPopup();

		JHtml::_('script', 'plg_rsdirectory_importcsv/script.js', array('relative' => true, 'version' => 'auto'));

		JForm::addFormPath(__DIR__ . '/forms');

		$form = JForm::getInstance( 'plg_rsdirectory_importcsv.importoptions', 'importoptions', array('control' => 'jform'));
			
		?>

		<fieldset id="csv" class="import-fieldset hide">
			<?php
				foreach ($form->getFieldsets() as $fieldset)
				{
					if ($fields = $form->getFieldset($fieldset->name))
					{
						foreach ($fields as $field)
						{
							echo $field->renderField();
						}
					}
				}
			?>

			<iframe id="csv_upload_target" name="csv_upload_target" src="" style="width: 0; height: 0; border: none;"></iframe>

			<div id="csvSelectColumns" class="mfp-hide"></div>
				
		</fieldset>
			
		<?php
			
		JText::script('PLG_RSDIRECTORY_IMPORTCSV_REQUIRED_FIELDS_ERROR');
		JText::script('PLG_RSDIRECTORY_IMPORTCSV_SELECT_COLUMNS');
		JText::script('PLG_RSDIRECTORY_IMPORTCSV_DUPLICATES_COLUMNS_ERROR');
		JText::script('PLG_RSDIRECTORY_IMPORTCSV_COLUMNS_SELECTION_ERROR');
		JText::script('PLG_RSDIRECTORY_IMPORTCSV_COLUMNS_SELECTED');
		JText::script('COM_RSDIRECTORY_IMPORTING_DATA');
	}
		
	/**
	 * Import.
	 *
	 * @access public
	 *
	 * @param object $vars
	 */
	public function onRsdirectoryImport($vars)
	{
		// Do a few checks.
		if ( !$this->canRun() || empty($vars->import_from) || $vars->import_from != self::IMPORT_OPTION )
			return;

		// Get mainframe.
		$app = JFactory::getApplication();
			
		// Get form data.
		$jform_data = $app->input->get( 'jform', array(), 'array' );
			
		// Get plugin form data.
		$data = empty($jform_data[self::IMPORT_OPTION]) ? array() : $jform_data[self::IMPORT_OPTION];
			
		switch ($vars->import_action)
		{
			case 'measure':
					
				$this->measure($vars, $data);
				break;
					
			case 'selectColumns':
					
				$this->selectColumns($vars, $data);
				break;
					
			case 'import':
					
				$this->import($vars, $data);
				break;
					
			default:
					
				$this->init($vars, $data);
				break;
		}
	}
		
	/**
	 * Method to initialize the import.
	 *
	 * @access protected
	 *
	 * @param object $vars
	 * @param array $data
	 */
	protected function init($vars, $data)
	{
		if ( empty($data['from']) || !in_array( $data['from'], array('uploaded_file', 'local_file', 'url') ) )
			return;

		// Delete old tmp files.
		$tmp_files = array_diff( scandir($this->getImportPath()), array('..', '.', 'index.html') );
			
		if ($tmp_files)
		{
			foreach ($tmp_files as $tmp_file)
			{
				if ( is_dir($this->getImportPath() . $tmp_file) )
				{
					JFolder::delete($this->getImportPath() . $tmp_file);
				}
				else
				{
					JFile::delete($this->getImportPath() . $tmp_file);
				}
			}
		}
			
		// Init uploaded file.
		if ($data['from'] == 'uploaded_file')
		{
			echo '<script type="text/javascript">';
				
			if ( $this->initUploadedFile() )
			{
				echo 'window.top.window.addLog( window.top.document.getElementById("import-log"), "' . RSDirectoryHelper::escapeHTML( JText::_('PLG_RSDIRECTORY_IMPORTCSV_FILE_UPLOADED') ) . '", "'.(RSDirectoryHelper::isJ4() ? 'table-success' : 'success').'" );';
				echo 'window.top.window.measureCSVImport();';
			}
			else
			{
				$errors = $this->getErrors();
					
				foreach ($errors as $error)
				{
					echo 'window.top.window.addLog( window.top.document.getElementById("import-log"), "' . RSDirectoryHelper::escapeHTML($error) . '", "'.(RSDirectoryHelper::isJ4() ? 'table-danger' : 'error').'" );';
				}
					
				echo 'window.top.window.abortImport("' . RSDirectoryHelper::escapeHTML( JText::_('COM_RSDIRECTORY_IMPORT_ABORTED') ) . '");';
			}
				
			echo '</script>';
		}
		// Init local file.
		else if ($data['from'] == 'local_file')
		{
			$messages = $vars->response['messages'];
				
			if ( $this->initLocalFile($vars, $data) )
			{
				$vars->response['action'] = 'measure';
			}
			else
			{
				$errors = $this->getErrors();
					
				foreach ($errors as $error)
				{
					$messages[] = array(
						'message' => $error,
						'type' => 'error',
						'id' => null,
					);
				}
			}
				
			$vars->response['messages'] = $messages;
		}
		// Init remote file.
		else
		{
			$messages = $vars->response['messages'];
				
			if ( $this->initRemoteFile($vars, $data) )
			{
				$messages[] = array(
					'message' => JText::_('PLG_RSDIRECTORY_IMPORTCSV_FILE_FETCHED'),
					'type' => 'success',
					'id' => null,
				);
					
				$vars->response['action'] = 'measure';
			}
			else
			{
				$errors = $this->getErrors();
					
				foreach ($errors as $error)
				{
					$messages[] = array(
						'message' => $error,
						'type' => 'error',
						'id' => null,
					);
				}
			}
				
			$vars->response['messages'] = $messages;
		}
	}
		
	/**
	 * Method to initialize the import of an uploaded file.
	 *
	 * @access protected
	 *
	 * @return bool
	 */
	protected function initUploadedFile()
	{
		// Get mainframe.
		$app = JFactory::getApplication();
			
		// Get files.
		$jform_files = $app->input->files->get( 'jform', array(), 'raw' );
			
		// Get plugin files.
		$files = empty($jform_files[self::IMPORT_OPTION]) ? array() : $jform_files[self::IMPORT_OPTION];
			
		if ( empty($files['uploaded_file']['name']) )
		{
			$this->setError( JText::_('PLG_RSDIRECTORY_IMPORTCSV_UPLOAD_FAILED') );
		}
		else
		{
			$file = $files['uploaded_file'];
				
			// Generate random hash.
			$hash = RSDirectoryHelper::getHash();
				
			// Set new file name.
			$file_name = "$hash.csv";
				
			// Set the file path.
			$dest = $this->getImportPath() . $file_name;
				
			if ( JFile::upload($file['tmp_name'], $dest, false, true) )
			{
				// Remember the file name.
				$app->setUserState('com_rsdirectory.import.csv.file_path', $dest);
					
				return true;
			}
			else
			{
				$this->setError( JText::_('PLG_RSDIRECTORY_IMPORTCSV_UPLOAD_FAILED') );
			}
		}
			
		return false;
	}
		
	/**
	 * Method to initialize the import of a remote file.
	 *
	 * @access protected
	 *
	 * @param object $vars
	 * @param array $data
	 *
	 * @return bool
	 */
	protected function initRemoteFile($vars, $data)
	{
		// URL not provided.
		if ( empty($data['url']) || !trim($data['url']) )
		{
			$this->setError( JText::_('PLG_RSDIRECTORY_IMPORTCSV_NO_URL_PROVIDED') );
		}
		// cURL is not installed or it's disabled.
		else if ( !function_exists('curl_init') )
		{
			$this->setError( JText::_('PLG_RSDIRECTORY_IMPORTCSV_CURL_ERROR') );
		}
		else
		{
			$curl = curl_init($data['url']); 
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
			$output = curl_exec($curl);
			curl_close($curl);
				
			if ($output)
			{
				// Generate random hash.
				$hash = RSDirectoryHelper::getHash();
					
				// Set new file name.
				$file_name = "$hash.csv";
					
				// Set the file path.
				$dest = $this->getImportPath() . $file_name;
					
				if ( file_put_contents($dest, $output) )	
				{
					// Remember the file name.
					JFactory::getApplication()->setUserState('com_rsdirectory.import.csv.file_path', $dest);
						
					return true;
				}
				// Tmp file could not be written.
				else
				{
					$this->setError( JText::_('PLG_RSDIRECTORY_IMPORTCSV_TMP_ARCHIVE_WRITE_ERROR') );
				}
			}
			else
			{
				$this->setError( JText::_('PLG_RSDIRECTORY_IMPORTCSV_FETCH_ERROR') );
			}
		}
			
		return false;
	}
		
	/**
	 * Method to initialize the import of a local file.
	 *
	 * @access protected
	 *
	 * @param object $vars
	 * @param array $data
	 *
	 * @return bool
	 */
	protected function initLocalFile($vars, $data)
	{
		// URL not provided.
		if ( empty($data['local_file']) || !trim($data['local_file']) )
		{
			$this->setError( JText::_('PLG_RSDIRECTORY_IMPORTCSV_NO_PATH_PROVIDED') );
		}
		else if ( !file_exists($data['local_file']) || !is_file($data['local_file']) )
		{
			$this->setError( JText::_('PLG_RSDIRECTORY_IMPORTCSV_FILE_NOT_FOUND') );
		}
		else
		{
			// Remember the file name.
			JFactory::getApplication()->setUserState('com_rsdirectory.import.csv.file_path', $data['local_file']);
				
			return true;
		}
			
		return false;
	}
		
	/**
	 * Method to measure import data.
	 *
	 * @access protected
	 *
	 * @param object $vars
	 * @param array $data
	 */
	protected function measure($vars, $data)
	{
		$app = JFactory::getApplication();

			
		$messages = $vars->response['messages'];
			
		// Get the CSV file path.
		$file_path = $app->getUserState('com_rsdirectory.import.csv.file_path');
			
		@$fh = fopen($file_path, 'r');
			
		if ($fh !== false)
		{
			$delimiter = isset($data['delimiter']) ? $data['delimiter'] : ',';
			$enclosure = isset($data['enclosure']) ? $data['enclosure'] : '"';
			$escape = isset($data['escape']) ? $data['escape'] : '\\';
				
			$total = 0;
			$first = false;
				
			while ( $line = self::fgetcsv($fh, 0, $delimiter, $enclosure, $escape) )
			{
				// Skip the 1st line.
				if ( !empty($data['ignore_first_line']) && !$first )
				{
					$first = true;
					continue;
				}
					
				$total++;
			}
				
			$messages[] = array(
				'message' => JText::_('COM_RSDIRECTORY_IMPORT_MEASURING_DATA_DONE'),
				'type' => 'success',
				'id' => 'import-msg-data-measuring',
			);
				
			if ($total)
			{
				$totals = array(
					'total' => $total,
				);
					
				$app->setUserState('com_rsdirectory.measure.import.totals', $totals);
					
				$vars->response['action'] = 'selectColumns';
			}
			else
			{
				$messages[] = array(
					'message' => JText::_('PLG_RSDIRECTORY_IMPORTCSV_EMPTY'),
					'type' => 'warning',
					'id' => null,
				);
			}
		}
		else
		{
			$messages[] = array(
				'message' => JText::_('PLG_RSDIRECTORY_IMPORTCSV_COULD_NOT_OPEN_FILE'),
				'type' => 'error',
				'id' => null,
			);
		}
			
		$vars->response['messages'] = $messages;
	}
		
	/**
	 * Display columns assignment table.
	 *
	 * @access protected
	 *
	 * @param object $vars
	 * @param array $data
	 */
	protected function selectColumns($vars, $data)
	{
		?>
			
		<div id="csvSelectColumns" class="form-horizontal">
				
			<h3><?php echo JText::_('PLG_RSDIRECTORY_IMPORTCSV_SELECT_COLUMNS_TITLE'); ?></h3>
				
			<div class="alert alert-info"><?php echo JText::_('PLG_RSDIRECTORY_IMPORTCSV_PREVIEW_NOTE'); ?></div>
				
			<div class="alert alert-error hide"></div>
				
			<?php
				
			// Get mainframe.
			$app = JFactory::getApplication();
				
			// Get the CSV file path.
			$file_path = $app->getUserState('com_rsdirectory.import.csv.file_path');
				
			@$fh = fopen($file_path, 'r');
				
			if ($fh !== false)
			{
				$delimiter = isset($data['delimiter']) ? $data['delimiter'] : ',';
				$enclosure = isset($data['enclosure']) ? $data['enclosure'] : '"';
				$escape = isset($data['escape']) ? $data['escape'] : '\\';
					
				$rows = array();
				$first = false;
				$count = 0;
					
				while ( $line = self::fgetcsv($fh, 0, $delimiter, $enclosure, $escape) )
				{
					$count++;
						
					// Skip the 1st line.
					/*if ( !empty($data['ignore_first_line']) && !$first )
					{
						$first = true;
						continue;
					}*/
						
					foreach ($line as $j => $value)
					{
						$rows[$j][$count] = $value;
					}
						
					// Get just the 1st 5 lines.
					if ($count > 4)
						break;
				}
					
				if ($rows)
				{
					// Get DBO.
					$db = JFactory::getDbo();
						
					// Get the columns of the entries table.
					$columns = $db->getTableColumns('#__rsdirectory_entries');
						
					// Initialize the columns options.
					$options = array(
						array(
							'items' => array(
								JHtml::_( 'select.option', '', JText::_('PLG_RSDIRECTORY_IMPORTCSV_COLUMN_OPTION') )
							),
						),
					);
						
					// Add the core columns group.
					$group = array(
						'text' => JText::_('PLG_RSDIRECTORY_IMPORTCSV_CORE_COLUMNS_OPTION'),
						'items' => array(),
					);
						
					foreach ($columns as $column => $type)
					{
						if ( !in_array($column, self::$skippedColumns) )
						{
							$group['items'][] = JHtml::_( 'select.option', $column, JText::_( 'PLG_RSDIRECTORY_IMPORTCSV_COLUMNS_' . strtoupper($column) ).' ('.$column.')' );
						}
					}
						
					$options[] = $group;
						
						
					// Get the category id.	
					$category_id = empty($data['category_id']) ? 0 : $data['category_id'];
						
					// Get the form id.
					$form_id = RSDirectoryHelper::getCategoryInheritedFormId($category_id);
					
					// Catch all the custom fields for the preselect
					$found_custom_fields = array();
						
					if ( $form_fields = RSDirectoryHelper::getFormFields($form_id, null, 1) )
					{
						// Add the custom fields columns group.
						$group = array(
							'text' => JText::_('PLG_RSDIRECTORY_IMPORTCSV_CUSTOM_FIELDS_COLUMNS_OPTION'),
							'items' => array(),
						);
							
						// Get the columns of the custom fields table.
						$columns = $db->getTableColumns('#__rsdirectory_entries_custom');
							
						foreach ($columns as $column => $type)
						{
							preg_match('/f_(\d+)/', $column, $matches);
								
							// Skip the column if the field id was not found.
							if ( !isset($matches[1]) )
								continue;
								
							// Get the form field.
							$field = RSDirectoryHelper::findElements( array('id' => $matches[1]), $form_fields );
								
							// Skip the column if the field was not found.
							if (!$field)
								continue;
							
							// add the column name
							$suffix = '';

							if (strpos($column, '_address'))
							{
								$suffix = '_address';
							}
							else if (strpos($column, '_lat'))
							{
								$suffix = '_lat';
							}
							else if (strpos($column, '_lng'))
							{
								$suffix = '_lng';
							}

							// add the suffix to the form_field_name
							$form_field_name = $field->form_field_name.$suffix;
							$column_name = $field->column_name.$suffix;

							$found_custom_fields[$form_field_name] = $column_name;

							$form_caption = (string) $field->properties->get('form_caption');
							if ( trim( $form_caption ) )
							{
								$text = $form_caption;
							}
							else
							{
								$arr = explode('-', $field->name);
								$arr = array_map('strtolower', $arr);
								$arr = array_map('ucfirst', $arr);
									
								$text = implode(' ', $arr);
							}
								
							// E.g.: f_10_address -> Address
							$col_substr = str_replace( array($matches[0], '_'), array('', ' '), $column);
							$col_substr = ucfirst( strtolower($col_substr) );
								
							$text .= $col_substr;
								
							$group['items'][] = JHtml::_('select.option', $column, $text.' ('.$form_field_name.')');
						}
							
						$options[] = $group;
					}
						
					?>
						
					<table class="table table-striped">
						<thead>
							<tr>
								<th><?php echo JText::_('PLG_RSDIRECTORY_IMPORTCSV_RSDIRECTORY_COLUMNS'); ?></th>
								<?php for ( $i = 0; $i < $count; $i++ ) { ?>
								<th>
									<?php
									if ( !empty($data['ignore_first_line']) && !$i )
									{
										echo JText::sprintf('PLG_RSDIRECTORY_IMPORTCSV_CSV_COLUMNS');
									}
									else
									{
										echo JText::sprintf( 'PLG_RSDIRECTORY_IMPORTCSV_ENTRY_NUMBER', $i + ( empty($data['ignore_first_line']) ? 1 : 0 ) );
									}
									?>
								</th>
								<?php } ?>
							</tr>
						</thead>
						<tbody>
							<?php foreach ($rows as $i=> $cols) { ?>
							<tr>
								<td>
									<div class="control-group">
									<?php
									$selected = reset($cols);
									if (isset($found_custom_fields[$selected])) {
										$selected = $found_custom_fields[$selected];
									}
									echo JHtml::_(
										'select.groupedlist',
										$options,
										'jform[csv][columns][]',
										array(
											'id' => "csv_columns_$i",
											'list.select' => $selected,
											'list.attr'=>'class="form-select"'
										)
									); ?>
									</div>
								</td>
								<?php foreach ($cols as $col) { ?>
								<td><?php echo RSDirectoryHelper::cut( RSDirectoryHelper::escapeHTML($col), 500 ); ?></td>
								<?php } ?>
							</tr>
							<?php } ?>
						</tbody>
					</table>
						
					<button id="csv-select-columns" class="btn btn-primary"><?php echo JText::_('JSUBMIT'); ?></button>
						
					<?php
				}
			}
			else
			{
				?>
					
				<div class="alert alert-error"><?php echo JText::_('PLG_RSDIRECTORY_IMPORTCSV_COULD_NOT_OPEN_FILE'); ?></div>
					
				<?php
			}
				
			?>
				
		</div>
			
		<?php
	}
		
	/**
	 * Method to import data.
	 *
	 * @access protected
	 *
	 * @param object $vars
	 * @param array $data
	 */
	/**
	 * Method to import data.
	 *
	 * @access protected
	 *
	 * @param object $vars
	 * @param array $data
	 */
	protected function import($vars, $data)
	{
		$app = JFactory::getApplication();
		$db = JFactory::getDbo();

		$messages = $vars->response['messages'];

		$offset = empty($data['offset']) ? 0 : $data['offset'];

		// Set the import limit.
		$limit = 50;

		// Get the CSV file path.
		$file_path = $app->getUserState('com_rsdirectory.import.csv.file_path');

		@$fh = fopen($file_path, 'r');

		$list = array();

		if ($fh !== false)
		{
			$delimiter = isset($data['delimiter']) ? $data['delimiter'] : ',';
			$enclosure = isset($data['enclosure']) ? $data['enclosure'] : '"';
			$escape = isset($data['escape']) ? $data['escape'] : '\\';

			$rows = array();
			$first = false;
			$i = 0;

			while ( $line = self::fgetcsv($fh, 0, $delimiter, $enclosure, $escape) )
			{
				// Skip the 1st line.
				if ( !empty($data['ignore_first_line']) && !$first )
				{
					$first = true;
					continue;
				}

				$i++;

				$list[] = $line;

				// A little optimisation.. Exit the loop if we exceeded the step limit.
				if ($limit + $offset < $i)
					break;
			}

			// Cut a piece of the array.
			$list = array_slice($list, $offset, $limit);
		}

		$columns = explode(',', $data['columns']);

		JPluginHelper::importPlugin('rsdirectory');
		$app->triggerEvent('onRsdirectoryImportCSV', array($data, $list));

		// Get the date columns for the entries table
		$entries_date_columns = $this->getDateColumns('#__rsdirectory_entries');
		$entries_date_columns_keys = array_keys($entries_date_columns);

		if ($list)
		{
			// Get the category id.
			$category_id = empty($data['category_id']) ? 0 : $data['category_id'];

			// Get the form id.
			$form_id = RSDirectoryHelper::getCategoryInheritedFormId($category_id);

			// Get the form Fields
			$form_fields = RSDirectoryHelper::getFormFields($form_id, 1);

			// Remember the entries ids.
			$entries_ids = array();

			// include the table path
			JTable::addIncludePath(JPATH_ADMINISTRATOR.'/components/com_rsdirectory/tables');

			foreach ($list as $line)
			{
				$entry = (object)array(
					'category_id' 			=> $category_id,
					'form_id' 				=> $form_id,
					'title' 				=> '',
					'big_subtitle'  		=> '',
					'small_subtitle' 		=> '',
					'description' 			=> '',
					'metadata_title' 		=> '',
					'metadata_keywords' 	=> '',
					'metadata_description'  => '',
				);
				$entry_custom = new stdClass;
				$update = false;

				foreach ($line as $i => $value)
				{
					if ( empty( $columns[$i] ) )
						continue;

					$column = $columns[$i];

					// Custom field column.
					if ( RSDirectoryHelper::startsWith($column, 'f_') )
					{

						$field_found = RSDirectoryHelper::findFormField(array('column_name' => $column), $form_fields);

						// check for new lines "\n"
						if ($field_found && ($field_found->field_type == 'checkboxgroup' || $field_found->properties->get('multiple')) && is_string($value) && strpos($value, "\n") !== false) {
							$value = str_replace(array("\r\n", "\n"), array("\n", "\r\n"), $value);
						}

						$entry_custom->$column = $value;
					}
					// Core column.
					else
					{
						//if this is a dete or datetime column must check the format and try to change it
						if (in_array($column, $entries_date_columns_keys))
						{
							$format = $entries_date_columns[$column];
							// change the value to SQL accepted
							$value = $this->checkAndChangeDate($value, $format);
						}

						$entry->$column = $value;
					}
				}

				if ( empty($entry->id) )
				{
					// Unset the entry id to avoid ids with the value 0.
					unset($entry->id);
				}
				else
				{
					$query = $db->getQuery(true)
						->select('COUNT(*)')
						->from( $db->qn('#__rsdirectory_entries') )
						->where( $db->qn('id') . ' = ' . $db->q($entry->id) );

					$db->setQuery($query);

					if ( $db->loadResult() )
					{
						$update = true;
					}
				}

				if ($update)
				{
					$db->updateObject('#__rsdirectory_entries', $entry, 'id');

					$entry_custom->entry_id = $entry->id;
				}
				else
				{
					$db->insertObject('#__rsdirectory_entries', $entry, 'id');

					$entry_custom->entry_id = $entry->id;
				}

				// Handle custom fields values.
				$entries_custom = JTable::getInstance('Entriescustom', 'RSDirectoryTable');
				if ($update)
				{
					$entries_custom->load(array('entry_id'=>$entry_custom->entry_id));
				}
				$entries_custom->save($entry_custom);

				$entries_ids[] = $entry->id;
			}

			if ( !empty($data['regenerate_titles']) && $entries_ids )
			{
				// Get entries.
				$entries = RSDirectoryHelper::getEntriesObjectListByIds($entries_ids);

				// Regenerate titles.
				RSDirectoryHelper::regenerateEntriesTitles($entries);
			}

			JPluginHelper::importPlugin('rsdirectory');
			$app->triggerEvent('onRsdirectoryImportCSVComplete', array($entries_ids, $data, $list));
		}

		$count = count($list);
		$totals = $app->getUserState('com_rsdirectory.measure.import.totals');

		$vars->response['total'] = $totals['total'];

		$progress = $offset + $count;
		$progress = $progress > $totals['total'] ? $totals['total'] : $progress;

		$vars->response['progress'] = $progress;
		$vars->response['completition'] = $totals['total'] ? number_format( ($progress * 100) / $totals['total'], 1 ) : 100.0;

		$vars->response['action'] = $vars->response['completition'] < 100 ? 'import' : 'done';
		$vars->response['messages'] = $messages;
	}

	/**
	 * Convert the given date to an SQL format
	 *
	 * @access protected
	 *
	 * @param string $date
	 * @param string $format
	 *
	 * @return string
	 */
	protected function checkAndChangeDate($date, $format)
	{
		$db = JFactory::getDbo();
		$nullDate = $db->getNullDate();

		if ($format == 'Y-m-d') {
			$nullDate = str_replace(' 00:00:00', '',$nullDate);
		}

		if ($date == $nullDate)
		{
			return $date;
		}

		try
		{
			$date = JFactory::getDate($date)->toSql();

			if ($format == 'Y-m-d')
			{
				$date = JFactory::getDate($date)->format($format);

			}
		}
		catch (\Exception $e)
		{
			// in case the date could not be converted return the null date
			$date = $nullDate;
		}

		return $date;
	}

	/**
	 * Get date types field from a table.
	 *
	 * @access protected
	 *
	 * @param string $table
	 *
	 * @return array
	 */
	protected function getDateColumns($table)
	{
		$db = JFactory::getDbo();
		$columns = $db->getTableColumns($table, false);

		$date_columns = array();
		foreach($columns as $column)
		{
			$columnType = strtolower($column->Type);
			$columnName = strtolower($column->Field);

			if ($columnType != 'date' && $columnType != 'datetime')
			{
				continue;
			}

			$date_columns[$columnName] = $columnType == 'date' ? 'Y-m-d' : 'Y-m-d H:i:s';
		}

		return $date_columns;
	}
			
	/**
	 * Convert the section name into a valid id attribute string.
	 *
	 * @access public
	 *
	 * @param string $section
	 *
	 * @return string
	 */
	public static function sectionNameToId($section)
	{
		return "import-msg-$section";
	}
		
	/**
	 * Method to add an error to the errors array.
	 *
	 * @access public
	 *
	 * @param string $error
	 */
	public function setError($error)
	{
		$this->errors[] = $error;
	}
		
	/**
	 * Method to get the errors array.
	 *
	 * @access public
	 *
	 * @return array
	 */
	public function getErrors()
	{
		return $this->errors;
	}
		
	/**
	 * Gets line from file pointer and parse for CSV fields.
	 *
	 * @access public
	 *
	 * @static
	 *
	 * @param resource  $handle
	 * @param int $length
	 * @param string $delimiter
	 * @param string $enclosure
	 * @param string $escape
	 *
	 * @return mixed
	 *
	 * @see fgetcsv
	 */
	public static function fgetcsv($handle, $length = 0, $delimiter = ",", $enclosure = '"', $escape = "\\")
	{
		if ( version_compare( phpversion(), '5.3.0' ) == -1 )
		{
			return fgetcsv($handle, $length, $delimiter, $enclosure);
		}
		else
		{
			return fgetcsv($handle, $length, $delimiter, $enclosure, $escape);
		}
	}
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists