<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: 			*/

// +----------------------------------------------------------------------+

// | PHP version 4/5                                                      |

// +----------------------------------------------------------------------+

// | Copyright (c) 2004-2010 ARMIA INC                                    |

// +----------------------------------------------------------------------+

// | This source file is a part of iScripts VistaCart                     |

// +----------------------------------------------------------------------+

// | Authors: Jipson<jipson.thomas@armiasystems.com>             		  |

// +----------------------------------------------------------------------+

class Setting extends AppModel

{

   var $name = 'Setting';

  // var $belongsTo = array('Product','User'); 

  var $validateFile = array(

                          'size' => 204800,

                          'type' => 'jpg,pjpeg,jpeg,png,gif,pem,octet-stream '

                          );

						  

   ###### Function to update quantity in cart........

   function update($field,$val){

   		$Db = ConnectionManager::getDataSource($this->useDbConfig);

		$sttbl = $Db->fullTableName('settings');

		$this->query("UPDATE $sttbl SET value = '".$val."' WHERE fieldname = '".$field."'");

		return true;

   }

   

        ###### Function to update offline credit payment ........

   function updateofflinecredit($val){

   		$Db = ConnectionManager::getDataSource($this->useDbConfig);

		$sttbl = $Db->fullTableName('settings');

		$this->query("UPDATE $sttbl SET value='".$val."' where fieldname='offline_creditpay'");

		return true;

   }

   

   

	function load()  

     {  

         $sett = $this->find('all');  

   

         foreach ($sett as $variable)  

         {  

             Configure::write  

                 (  

                     'Neutrino.'.$variable['Setting']['fieldname'],  

                     $variable['Setting']['value']  

                 );  

         }  

     } 

	  function handleFileUpload($fileData, $fileName,$from='')

	  {

		$error = false;

	 

		//Get file type

		$typeArr = explode('/', $fileData['type']);

		//print_r($typeArr);exit;

		//If size is provided for validation check with that size. Else compare the size with INI file

		if (($this->validateFile['size'] && $fileData['size'] > $this->validateFile['size']) || $fileData['error'] == UPLOAD_ERR_INI_SIZE)

		{

		  $error = 'File is too large to upload';

		}

		elseif ($this->validateFile['type'] && (strpos($this->validateFile['type'], strtolower($typeArr[1])) === false))

		{

		  //File type is not the one we are going to accept. Error!!

		  $error = 'Invalid file type';

		}

		else

		{

		  //Data looks OK at this stage. Let's proceed.

		  if ($fileData['error'] == UPLOAD_ERR_OK)

		  {

			//Oops!! File size is zero. Error!

			if ($fileData['size'] == 0)

			{

			  $error = 'Zero size file found.';

			}

			else

			{

			  if (is_uploaded_file($fileData['tmp_name']))

			  {

				//Finally we can upload file now. Let's do it and return without errors if success in moving.

				$path = WWW_ROOT.IMAGES_URL ;

				$newFileName = $fileName;

				while (file_exists("$path/".$newFileName)) {

				 	unlink("$path/".$newFileName);

				}

				if (!move_uploaded_file($fileData['tmp_name'], WWW_ROOT.IMAGES_URL.'/'."$fileName"))

				{

				  $error = true;

				}
                                
				if($from != 'permiss'){

					$this->resizeImg(WWW_ROOT.IMAGES_URL.'/'."$fileName", 48,46 , false, 9, 0,"_disp");

				}

			  }

			  else

			  {

				$error = true;

			  }

			}

		  }

		}

		return $error;

	  } 

	  function resizeImg($imgPath, $maxWidth, $maxHeight, $directOutput = true, $quality = 9, $verbose,$imageType)

		{

			 $size = getimagesize($imgPath);

			 //print_r($size);exit;

		   // break and return false if failed to read image infos

			 if(!$size){

			   if($verbose && !$directOutput)echo "<br />Not able to read image infos.<br />";

			   

			   return false;

			 }

		

		   // relation: width/height

			 $relation = $size[0]/$size[1];

		   // maximal size (if parameter == false, no resizing will be made)

			 $maxSize = array($maxWidth?$maxWidth:$size[0],$maxHeight?$maxHeight:$size[1]);

		   // declaring array for new size (initial value = original size)

			 $newSize = $size;

		   // width/height relation

			 $relation = array($size[1]/$size[0], $size[0]/$size[1]);

		

		

			if(($newSize[0] > $maxWidth))

			{

				$newSize[0]=$maxSize[0];

				$newSize[1]=$newSize[0]*$relation[0];

			}

			

			

		

			/*

			if(($newSize[1] > $maxHeight))

			{

				$newSize[1]=$maxSize[1];

				$newSize[0]=$newSize[1]*$relation[1];

			}

			*/

			

			 // create image

			   switch($size[2])

			   {

				 case 1:

				   if(function_exists("imagecreatefromgif"))

				   {

					 $originalImage = imagecreatefromgif($imgPath);

				   }else{

					 if($verbose && !$directOutput)echo "<br />No GIF support in this php installation, sorry.<br />";

					 return false;

				   }

				   break;

				 case 2: $originalImage = imagecreatefromjpeg($imgPath); break;

				 case 3: $originalImage = imagecreatefrompng($imgPath); break;

				 default:

				   if($verbose && !$directOutput)echo "<br />No valid image type.<br />";

				   return false;

			   }

		

			 // create new image

		

			   $resizedImage = imagecreatetruecolor($newSize[0], $newSize[1]); 

		

			   imagecopyresampled($resizedImage, $originalImage,0, 0, 0, 0,$newSize[0], $newSize[1], $size[0], $size[1]);

		

			$rz=$imgPath;

		

			 // output or save

			   if($directOutput)

				{

				 imagepng($resizedImage);

				 }

				 else

				{

					

					 //$rz=preg_replace("/\.([a-zA-Z]{3,4})$/","".$imageType.".png",$imgPath);

					 $rz=preg_replace("/\.([a-zA-Z]{3,4})$/","".$imageType.".gif",$imgPath);

					/// echo $quality; exit;  

				 //imagepng($resizedImage, $rz, $quality);

				 imagegif($resizedImage, $rz, $quality);

				 }

			 // return true if successfull

			   return $rz;

		}

}

?>