<?php
class help {

public function actionExcelImport() {
		$this->layout = "/layouts/main";
		$customer = zdCustomers::model()->findbyPk(Yii::app()->user->id);
		$criteria = new CDbCriteria;
		$criteria->addCondition("customer_id = " . Yii::app()->user->id);
		$zdFile = ZdUploadFile::model()->find($criteria);

		$userid = Yii::app()->user->id;

		$db = Yii::app()->params['new_db_store_preffix'] . $customer->app_id;
		$newconnection = new CDbConnection('mysql:host=localhost;dbname=' . $db, Yii::app()->params['oc_uname'], Yii::app()->params['oc_pass']);
		$newconnection->active = true;
		$query = $newconnection->createCommand("SELECT COUNT(product_id) AS totalcount FROM oc_product");
		
		$res_count = $query->queryScalar();

        $totalcount = 0;
        if ($res_count > 0)
            $totalcount = (int) $res_count;

        if ($zdFile == null)
            $zdFile = new ZdUploadFile;

        if (isset($_POST['ZdUploadFile'])) {
            $zdFile->attributes = $_POST['ZdUploadFile'];
            $zdFile->file = CUploadedFile::getInstance($zdFile, 'image');
            $zdFile->customer_id = $customer->id;
            $is_valid_upload_file = $zdFile->validate();

			Yii::import('ext.phpexcelreader.JPhpExcelReader');
			if ($zdFile->validate()) {
				$zdFile->file->saveAs(Yii::app()->params['excel_upload_path'] . Yii::app()->user->id . '-' . $zdFile->file);
				$zdFile->file = Yii::app()->user->id . '-' . $zdFile->file;
				$zdFile->save(false);
				$criteria = new CDbCriteria;
				$criteria->addCondition("customer_id = " . Yii::app()->user->id);
				$excel_files = ZdUploadFile::model()->findAll($criteria);
				$filepath = Yii::app()->params['excel_upload_path'] . $excel_files[0]->file;

				$this->delete_old_excel_data($userid);
				 
				 
				$data = new JPhpExcelReader($filepath);
				$numRows = $data->sheets[0]['numRows'];
				for ($i = 3; $i <= $numRows; $i++) {
					$image_name = '';
					if (!empty($data->sheets[0]['cells'][$i][3]) || !empty($data->sheets[0]['cells'][$i][2])) {
						$product_category = !empty($data->sheets[0]['cells'][$i][10]) ? $data->sheets[0]['cells'][$i][10] : '';
						$product_name = !empty($data->sheets[0]['cells'][$i][2]) ? $data->sheets[0]['cells'][$i][2] : '';
						$product_desc = !empty($data->sheets[0]['cells'][$i][9]) ? $data->sheets[0]['cells'][$i][9] : '';
						$product_model = !empty($data->sheets[0]['cells'][$i][3]) ? $data->sheets[0]['cells'][$i][3] : '';
						$product_qty = !empty($data->sheets[0]['cells'][$i][4]) ? $data->sheets[0]['cells'][$i][4] : '';
						$product_price = !empty($data->sheets[0]['cells'][$i][5]) ? $data->sheets[0]['cells'][$i][5] : '';
						$product_sku = !empty($data->sheets[0]['cells'][$i][6]) ? $data->sheets[0]['cells'][$i][6] : '';
						$products_location = !empty($data->sheets[0]['cells'][$i][7]) ? $data->sheets[0]['cells'][$i][7] : '';
						$product_img_url = !empty($data->sheets[0]['cells'][$i][8]) ? $data->sheets[0]['cells'][$i][8] : '';
						 
						$sql = "INSERT INTO `zd_temp_excel_data` SET
                                                product_category   = '".addslashes($product_category)."',	   
                                                product_name       = '".addslashes($product_name)."',
                                                product_desc       = '".addslashes($product_desc)."',
                                                product_model      = '".addslashes($product_model)."',
                                                product_qty        ='$product_qty',
                                                product_price      = '$product_price',
                                                product_sku        = '".addslashes($product_sku)."',
                                                products_location  = '".addslashes($products_location)."',
                                                product_img_url    = '$product_img_url', 
                                                customer_id        = '$userid'";
						Yii::app()->db->createCommand($sql)->execute();
					}
				}
				 
				Yii::app()->user->setFlash('filesuccess', "File is successfully uploaded!");
			}
			else
			Yii::app()->user->setFlash('error', "Sorry! This file format is not allowed!");
		}
		$exceldata = array();
		$error_message = array();
		 
		 

		/* if (isset($_POST['excelprod'])) {
		 }*/

		$exist = $this->check_product_exist($exceldata, '', 0);


		$download_link = '/exceluploads/ZipDandyProducts_Template.xls';
		$this->render('import-excel', array(
            'zdFile' => $zdFile,
            'model' => $customer,
            'download_link' => $download_link,
            'totalcount' => $totalcount,
            'dataerrors' => $error_message,
            'exist' => $exist)
		);
	}
        
        
}


?>