Jump to content

Asking for support conversion unit producing CSV import 1.3 to 1.6


Recommended Posts

Hello, trainee in a company, I have MODIFY several modules 1.3 to 1.6. * I have 3 problems, drive key, the code no longer works in 1.6. * And conversion to bootstrap.

 

I started the dev, but now PrestaShop think that the module is downloaded. At installation, PrestaShop replied that the module is not found.

<?php

class CsvUpdate extends Module
{
	/** @var max image size */
	protected $maxImageSize = 307200;

	
		// /* The parent construct is required for translations */
		// parent::__construct();
		
	public function __construct(){
		$this->name = 'csvupdate';
		$this->displayName = 'My Module of CSV product import update';
		$this->tab = 'front_office_features';
		$this->version = '0.1';
		$this->author = 'ADB Distribution';
		$this->displayName = 'My Module of CSV Product Import Update';
		$this->description = 'With this module, your customers will be able to import and update your products.';
		$this->bootstrap = true;
		parent::__construct();
	}

	function install(){
			
		if (!parent::install())
			return false;
	}

	function getContent(){
		
		if( !ini_get('safe_mode') ){
			set_time_limit(300);
		} 
		/* display the module name */
		$this->_html = '<h2>'.$this->displayName.'</h2>';

		/* update the products */
		if (isset($_POST['submitUpdate'])){
			
			// Forbidden key
			$forbidden = array('submitUpdate');
			
			// Parse CSV data
			if (isset($_FILES['csv_file']) AND isset($_FILES['csv_file']['tmp_name']) AND !empty($_FILES['csv_file']['tmp_name'])) {
				$csvfilehandle = fopen($_FILES['csv_file']['tmp_name'], 'r');
				$csvdata = array();
				$field_terminater = "\t";
				while($data = fgetcsv($csvfilehandle, 1000, $field_terminater))
					array_push($csvdata, $data);
			}
			else
				$this->_html .= $this->displayError($this->l('An error occurred during the file upload.'));
			
			// Clean data
			$cleandata = array();
			unset($csvdata[0]);
			unset($csvdata[1]);
			foreach($csvdata as $key=>$csvproduct) {
				preg_match('/([A-Z])0*([0-9]+)/', trim($csvproduct[0]), $reference_matches);
				$quantity_matches=trim($csvproduct[1]);
				$price_matches=str_replace (',' ,'.', trim($csvproduct[2]));
				$active = trim($csvproduct[3])=='OUI' ? 1 : 0 ;
				if($csvproduct[3]=='OUI'){
					$cleanproduct = array(
						'reference' => $reference_matches[1].$reference_matches[2],
						'quantity' => $quantity_matches,
						'price' => $price_matches
					);
					array_push($cleandata, $cleanproduct);
				}
			}
			
			// Update products database
			foreach($cleandata as $cleanproduct) {
				$product = null;
				$product = Product::getByReference($cleanproduct['reference']);
				$attribute=null;
				$attribute=Db::getInstance()->getRow('SELECT pa.*,p.price as product_price FROM `'._DB_PREFIX_.'product_attribute` pa LEFT JOIN `'._DB_PREFIX_.'product` p ON pa.id_product=p.id_product WHERE pa.reference="'.$cleanproduct['reference'].'"');
				
				
				//print_r($attribute);
				if($product) {
					if(!Db::getInstance()->autoExecute(_DB_PREFIX_.'product', $cleanproduct, 'UPDATE', '`reference` = "'.$cleanproduct['reference'].'"'))
						$this->_html .= $this->displayError('Erreur lors de la mise a jour du produit '.$cleanproduct['reference'].' : '.Db::getInstance()->getMsgError());
					else
						$this->_html .= $this->displayConfirmation('Le produit '.$cleanproduct['reference'].' a bien ete mis a jour !');
				}
				if($attribute){
					$cleanproduct['price']-=$attribute['price'];
					if(!Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'product_attribute` SET quantity='.$cleanproduct['quantity'].', price='.$cleanproduct['price'].' WHERE reference="'.$cleanproduct['reference'].'"'))
						$this->_html .= $this->displayError('Erreur lors de la mise a jour du produit '.$cleanproduct['reference'].' : '.Db::getInstance()->getMsgError());
					else{
						$this->_html .= $this->displayConfirmation('Le produit '.$cleanproduct['reference'].' a bien ete mis a jour !');
					}
				}
				if (!$product && !$attribute){
					$this->_html .= $this->displayError('Le produit '.$cleanproduct['reference'].' existe dans le fichier mais pas sur le site !');
				}
				// $this->_html .= $this->displayConfirmation(print_r($cleanproduct,1));
			}
		}

		/* display the editorial's form */
		$this->_displayForm();

		return $this->_html;
	}

	private function _displayForm(){
		
		global $cookie;
		
		/* Languages preliminaries */
		$defaultLanguage = intval(Configuration::get('PS_LANG_DEFAULT'));
		$languages = Language::getLanguages();
		$iso = Language::getIsoById($defaultLanguage);

		/* xml loading */
		$this->_html .= '<br />
		<form method="post" action="'.$_SERVER['REQUEST_URI'].'" enctype="multipart/form-data">
			<fieldset style="width: 900px;">
				<legend><img src="'.$this->_path.'logo.gif" alt="" title="" /> '.$this->displayName.'</legend>
				<label>'.$this->l('Fichier CSV').' </label>
				<div class="margin-form">
					<input type="file" name="csv_file" />
					<p style="clear: both">'.$this->l('Colonnes : Référence | Quantité | Prix HT | Publier sur le site').'</p>
				</div>
				<div class="clear pspace"></div>
				<div class="margin-form clear"><input type="submit" name="submitUpdate" value="'.$this->l('Envoyer le fichier').'" class="button" /></div>
			</fieldset>
		</form>';
	}

}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More