Blame view

administrator/components/com_jmap/framework/file/metainfo.php 5.89 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
<?php
// namespace administrator\components\com_jmap\framework\files;
/**
 * @package JMAP::FRAMEWORK::administrator::components::com_jmap
 * @subpackage framework
 * @subpackage file
 * @author Joomla! Extensions Store
 * @copyright (C) 2015 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
 */
defined('_JEXEC') or die('Restricted access');

// CSV import fields
define ('COM_JMAP_METAINFO_LINK', 0);
define ('COM_JMAP_METAINFO_TITLE', 1);
define ('COM_JMAP_METAINFO_DESC', 2);
define ('COM_JMAP_METAINFO_IMAGE', 3);
define ('COM_JMAP_METAINFO_ROBOTS', 4);
define ('COM_JMAP_METAINFO_STATUS', 5);
define ('COM_JMAP_METAINFO_EXCLUDED', 6);
define ('COM_JMAP_METAINFO_FIELD_NUM', 7);

jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.archive');

/**
 * Importer class for metainfo records
 * 
 * @package JMAP::FRAMEWORK::administrator::components::com_jmap
 * @subpackage framework
 * @subpackage file
 * @since 3.5
 */
class JMapFileMetainfo extends JObject {
	/**
	 * Database connector
	 * 
	 * @access private
	 * @var Object
	 */
	private $dbo;
	
	/**
	 * Application object
	 *
	 * @access private
	 * @var Object
	 */
	private $app;
	
	/**
	 * Store uploaded file to cache folder,
	 * fully manage error messages and ask for database insert
	 * 
	 * @access public
	 * @return boolean 
	 */
	public function import() {
		// Get file info
		$file = $this->app->input->files->get('datasourceimport', null, 'raw');
		$tmpFile = $file['tmp_name'];
		$tmpFileName = $file['name'];
		try {
			if(!$tmpFile || !$tmpFileName) {
				throw new JMapException(JText::_('COM_JMAP_NOFILE_SELECTED'), 'error');
			}
			
			$tmpFileExtension = @array_pop(explode('.', $tmpFileName));
			if($tmpFileExtension != 'csv') {
				throw new JMapException(JText::_('COM_JMAP_METAINFO_EXT_ERROR'), 'error');
			}

			// Deserialize contents
			$fileHandle = fopen($tmpFile, "r");
			if(!is_resource($fileHandle)) {
				throw new JMapException(JText::_('COM_JMAP_DATA_FILE_NOT_READABLE'), 'error');
			}
			
			// Parse the CSV files dataset into an importable array
			$skip = true;
			$dbQueryArray = array();
			while ( $csvRecord = fgetcsv ( $fileHandle, 0, ';', '"' ) ) {
				// Skip prima riga intestazioni
				if($skip) {
					$skip = false;
					continue;
				}
				//Insert
				array_push ( $dbQueryArray, $csvRecord );
			}
			
			// Check if some valid data to import are available
			if(!count($dbQueryArray)) {
				throw new JMapException(JText::_('COM_JMAP_METAINFO_NO_IMPORT_DATA_FOUND'), 'warning');
			}
			
			// Prepare the values array
			foreach ($dbQueryArray as $dbRecord) {
				// Ensyre at least that the number of csv fields are correct
				if(count($dbRecord) != COM_JMAP_METAINFO_FIELD_NUM) {
					continue;
				}
				
				// Check if the link as primary key already exists in this table
				$selectQuery = "SELECT" . $this->dbo->quoteName('id') .
							   "\n FROM " . $this->dbo->quoteName('#__jmap_metainfo') .
							   "\n WHERE" .
							   $this->dbo->quoteName('linkurl') . " = " . $this->dbo->quote($dbRecord[COM_JMAP_METAINFO_LINK]);
				$linkExists = $this->dbo->setQuery ( $selectQuery )->loadResult();
	
				// If the link exists just update it, otherwise insert a new one
				if($linkExists) {
					$query = "UPDATE" .
							 "\n " . $this->dbo->quoteName('#__jmap_metainfo') .
							 "\n SET " .
							 "\n " . $this->dbo->quoteName('meta_title') . " = " . $this->dbo->quote($dbRecord[COM_JMAP_METAINFO_TITLE]) . "," .
							 "\n " . $this->dbo->quoteName('meta_desc') . " = " . $this->dbo->quote($dbRecord[COM_JMAP_METAINFO_DESC]) . "," .
							 "\n " . $this->dbo->quoteName('meta_image') . " = " . $this->dbo->quote($dbRecord[COM_JMAP_METAINFO_IMAGE]) . "," .
							 "\n " . $this->dbo->quoteName('robots') . " = " . $this->dbo->quote($dbRecord[COM_JMAP_METAINFO_ROBOTS]) . "," .
							 "\n " . $this->dbo->quoteName('published') . " = " . (int)($dbRecord[COM_JMAP_METAINFO_STATUS]) . "," .
							 "\n " . $this->dbo->quoteName('excluded') . " = " . (int)($dbRecord[COM_JMAP_METAINFO_EXCLUDED]) .
							 "\n WHERE " .
							 "\n " . $this->dbo->quoteName('linkurl') . " = " . $this->dbo->quote($dbRecord[COM_JMAP_METAINFO_LINK]);
					$this->dbo->setQuery ( $query );
				} else {
					$query = "INSERT INTO" .
							 "\n " . $this->dbo->quoteName('#__jmap_metainfo') . "(" .
							 $this->dbo->quoteName('linkurl') . "," .
							 $this->dbo->quoteName('meta_title') . "," .
							 $this->dbo->quoteName('meta_desc') . "," .
							 $this->dbo->quoteName('meta_image') . "," .
							 $this->dbo->quoteName('robots') . "," .
							 $this->dbo->quoteName('published') . "," .
							 $this->dbo->quoteName('excluded') . ") VALUES (" .
							 $this->dbo->quote($dbRecord[COM_JMAP_METAINFO_LINK]) . "," .
							 $this->dbo->quote($dbRecord[COM_JMAP_METAINFO_TITLE]) . "," .
							 $this->dbo->quote($dbRecord[COM_JMAP_METAINFO_DESC]) . "," .
							 $this->dbo->quote($dbRecord[COM_JMAP_METAINFO_IMAGE]) . "," .
							 $this->dbo->quote($dbRecord[COM_JMAP_METAINFO_ROBOTS]) . "," .
							 (int)($dbRecord[COM_JMAP_METAINFO_STATUS]) . "," .
							 (int)($dbRecord[COM_JMAP_METAINFO_EXCLUDED]) . ")";
					$this->dbo->setQuery ( $query );
				}
				$this->dbo->execute ();
				if ($this->dbo->getErrorNum ()) {
					throw new JMapException(JText::sprintf('COM_JMAP_METAINFO_ERROR_STORING_DATA', $this->dbo->getErrorMsg()), 'error');
				}
			}
		}
		catch(JMapException $e) {
			$this->setError($e);
			return false;
		} catch (Exception $e) {
			$jmapException = new JMapException(JText::sprintf('COM_JMAP_METAINFO_ERROR_STORING_DATA', $e->getMessage()), 'error');
			$this->setError($jmapException);
			return false;
		}
		
		return true;
	}

	/**
	 * Class constructor
	 * 
	 * @access public
	 * @param Object $dbo
	 * @param Object $app
	 * @return Object &
	 */
	public function __construct($dbo, $app) {
		// DB connector
		$this->dbo = $dbo;
		
		// Application
		$this->app = $app;
	}
}