public function import($table, $view) { JToolBarHelper::back('Back', 'index.php?option=com_joomleague&view=' . $view); $msg = array(); $app = JFactory::getApplication(); $jinput = $app->input; $replace = $jinput->getInt('csv-replace', 0); $delimiter = $jinput->getString('csvdelimiter', ';'); $tblObject = JTable::getInstance($table, 'Table'); $filename = ''; $csvimport = false; $file = JRequest::getVar('FileCSV', null, 'files', 'array'); if (isset($file['tmp_name']) && trim($file['tmp_name']) != '') { $filename = $file['tmp_name']; $csvimport = true; } if ($csvimport) { $handle = fopen($filename, 'r'); if (!$handle) { $msg = JText::_('COM_JOOMLEAGUE_ADMIN_IMPORT_CTRL_CANNOT_OPEN'); $this->setRedirect('index.php?option=com_joomleague&view=' . $view, $msg, 'error'); return; } // get fields, on first row of the file $fields = array(); if (($data = fgetcsv($handle, 1000, $delimiter, '"')) !== FALSE) { $numfields = count($data); for ($c = 0; $c < $numfields; $c++) { // here, we make sure that the field match one of the fields of table or special fields, // otherwise, we don't add it $value = JoomleagueHelper::removeBOM(trim($data[$c])); if (property_exists($tblObject, $value)) { $fields[$c] = $value; } } } // If there is no validated fields, there is a problem... if (!count($fields)) { $msg = JText::_('COM_JOOMLEAGUE_ADMIN_IMPORT_CTRL_ERROR_PARSING'); $this->setRedirect('index.php?option=com_joomleague&view=' . $view, $msg, 'error'); return; } else { $msg[] = $numfields . " fields found in first row"; $msg[] = count($fields) . " fields were kept"; } // Now get the records, meaning the rest of the rows. $records = array(); $row = 1; while (($data = fgetcsv($handle, 10000, $delimiter, '"')) !== FALSE) { $num = count($data); if ($numfields != $num) { $msg[] = JText::_('COM_JOOMLEAGUE_ADMIN_IMPORT_CTRL_WRONG_NUMBER_OF_FIELDS'); } else { $r = array(); // only extract columns with validated header, from previous step. foreach ($fields as $k => $v) { $r[$k] = $this->_formatcsvfield($v, $data[$k]); } $records[] = $r; } $row++; } fclose($handle); $msg[] = JText::_('COM_JOOMLEAGUE_ADMIN_IMPORT_CTRL_TOTAL_RECORDS_FOUND') . count($records); // database update if (count($records)) { $model = $this->getModel('import'); $result = $model->import($fields, $records, $replace, $table); $error = $result['errormsg']; $app = JFactory::getApplication(); if ($error) { $msgError = array(); $msgError[] = JText::_('COM_JOOMLEAGUE_GLOBAL_ERROR_CHECK'); foreach ($result['errormsg'] as $error) { $msgError[] = $error; } $app->enqueueMessage(implode('<p>', $msgError), 'warning'); } $msg[] = JText::_('COM_JOOMLEAGUE_ADMIN_IMPORT_CTRL_TOTAL_ADDED_RECORDS') . ' ' . $result['added']; $msg[] = JText::_('COM_JOOMLEAGUE_ADMIN_IMPORT_CTRL_TOTAL_UPDATED_RECORDS') . ' ' . $result['updated']; $msg[] = JText::_('COM_JOOMLEAGUE_ADMIN_IMPORT_CTRL_TOTAL_EXISTS_RECORDS') . ' ' . $result['exists']; $app->enqueueMessage(implode('<p>', $msg)); } $this->setRedirect('index.php?option=com_joomleague&view=' . $view); } }