<?php

class DeviceSimulatorManagerHandler extends Handler{
	var $request = array();
	
	var $fileType = array(
		'gif'	=>	'image/gif',
		'jpeg'	=>	'image/jpeg',
		'png'	=>	'image/png'	
	);
	
	function __construct(){
		$this->Handler('DeviceSimulatorManagerHandler');		
	}
	
	function thorUpload($request = array()){ 

		return $request;
	} 
	
	function otsDefaultAction($request = array()) {
		global $ROOT_PATH;
		//mkdir($ROOT_PATH.'/images/thermal/ots/', 0775);
		//unlink ($ROOT_PATH.'/images/thermal/ots/main_color/4/0.jpeg');
		//unlink ($ROOT_PATH.'/images/thermal/ots/main_color/4/1.jpeg');
		//unlink ($ROOT_PATH.'/images/thermal/ots/main_color/4/2.jpeg');
		//rmdir($ROOT_PATH.'/images/thermal/ots/main_color/4/');
		
		$id=2;
		$result['poweroff'] = $this->DB->SelectAny('select poweroff_description from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['button_description_0'] = $this->DB->SelectAny('select button_description_0_0 ,button_description_0_1,button_description_0_2,button_description_0_3,button_description_0_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		//$result['button_description_1'] = $this->DB->SelectAny('select button_description_1_0,button_description_1_1,button_description_1_2,button_description_1_3,button_description_1_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		//$result['button_description_2'] = $this->DB->SelectAny('select button_description_2_0,button_description_2_1,button_description_2_2,button_description_2_3,button_description_2_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['visions_icon_description_0'] = $this->DB->SelectAny('select visions_icon_description_0_0,visions_icon_description_0_1,visions_icon_description_0_2,visions_icon_description_0_3,visions_icon_description_0_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		//$result['visions_icon_description_1'] = $this->DB->SelectAny('select visions_icon_description_1_0,visions_icon_description_1_1,visions_icon_description_1_2,visions_icon_description_1_3,visions_icon_description_1_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC'); 
		//$result['visions_icon_description_2'] = $this->DB->SelectAny('select visions_icon_description_2_0,visions_icon_description_2_1,visions_icon_description_2_2,visions_icon_description_2_3,visions_icon_description_2_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['url'] = $this->DB->SelectAny('select url from devicesimulator WHERE id='.$id.' ORDER BY id ASC');		
		return $result;		
	} 
	
	function  otsAction ($request = array()) { //method for frontend page
		global $ROOT_PATH;
		//printArray($_FILES);
		if (!empty($_FILES)){
			
			
			$this->uploadImages($_FILES['main_color'], 'main_color', $ROOT_PATH.'/images/thermal/ots/', '/images/thermal/ots/');
			$this->uploadImages($_FILES['main_brightness'], 'main_brightness', $ROOT_PATH.'/images/thermal/ots/', '/images/thermal/ots/');
			$this->uploadImages($_FILES['main_polarity'], 'main_polarity', $ROOT_PATH.'/images/thermal/ots/', '/images/thermal/ots/');
			$this->uploadImages($_FILES['reticle'], 'reticle', $ROOT_PATH.'/images/thermal/ots/', '/images/thermal/ots/');
			$this->uploadImages($_FILES['control_panel'], 'control_panel', $ROOT_PATH.'/images/thermal/ots/', '/images/thermal/ots/');
			//$this->uploadImages($_FILES['arrow_button_ico'], 'arrow_button_ico', $ROOT_PATH.'/images/thermal/ots/', '/images/thermal/ots/');
			$this->uploadImages($_FILES['visons_icons'], 'visons_icons', $ROOT_PATH.'/images/thermal/ots/', '/images/thermal/ots/');			
		}
		if ( !empty($_POST['button_description']) ) $this->DB->update('devicesimulator', $this->textProcess($_POST['button_description'], 'button_description'), array( 'id' => 2 ) ); // для серии ThOR id = $_POST['id'] ;
		if ( !empty($_POST['poweroff_description']) ) $this->DB->update('devicesimulator', $this->textProcess($_POST['poweroff_description'], 'poweroff_description'), array( 'id' => 2 ) );
		if ( !empty($_POST['visions_icon_description']) ) $this->DB->update('devicesimulator', $this->textProcess($_POST['visions_icon_description'], 'visions_icon_description'), array( 'id' => 2 ) ); 
		if ( !empty($_POST['url']) ) $this->DB->update('devicesimulator', $this->textProcess($_POST['url'], 'url'), array( 'id' => 2 ) );
		
		
		
	} 
	
	function defaultActionOts($request = array()) { //backend
		global $ROOT_PATH;		
			if (!empty($_FILES)){
			$this->uploadImages($_FILES['main_color'], 'main_color', $ROOT_PATH.'/images/thermal/ots/', '/images/thermal/ots/');
			$this->uploadImages($_FILES['main_brightness'], 'main_brightness', $ROOT_PATH.'/images/thermal/ots/', '/images/thermal/ots/');
			$this->uploadImages($_FILES['main_polarity'], 'main_polarity', $ROOT_PATH.'/images/thermal/ots/', '/images/thermal/ots/');
			$this->uploadImages($_FILES['reticle'], 'reticle', $ROOT_PATH.'/images/thermal/ots/', '/images/thermal/ots/');
			$this->uploadImages($_FILES['control_panel'], 'control_panel', $ROOT_PATH.'/images/thermal/ots/', '/images/thermal/ots/');
			$this->uploadImages($_FILES['arrow_button_ico'], 'arrow_button_ico', $ROOT_PATH.'/images/thermal/ots/', '/images/thermal/ots/');
			$this->uploadImages($_FILES['visons_icons'], 'visons_icons', $ROOT_PATH.'/images/thermal/ots/', '/images/thermal/ots/');			
		}
		//$this->textProcess($)
		//printArray($_POST['button_description']);
		/*$this->textProcess($_POST['button_description'], 'button_description');
		$this->textProcess($_POST['visions_icon_description'], 'visions_icon_description');
		$this->textProcess($_POST['poweroff_description'], 'poweroff_description'); */			
	
		if ( !empty($_POST['button_description']) ) $this->DB->update('devicesimulator', $this->textProcess($_POST['button_description'], 'button_description'), array( 'id' => $request['id'] ) ); // для серии ThOR id = $_POST['id'] ;
		if ( !empty($_POST['poweroff_description']) ) $this->DB->update('devicesimulator', $this->textProcess($_POST['poweroff_description'], 'poweroff_description'), array( 'id' => $request['id'] ) );
		if ( !empty($_POST['visions_icon_description']) ) $this->DB->update('devicesimulator', $this->textProcess($_POST['visions_icon_description'], 'visions_icon_description'), array( 'id' => $request['id'] ) ); 
		if ( !empty($_POST['url']) ) $this->DB->update('devicesimulator', $this->textProcess($_POST['url'], 'url'), array( 'id' => $request['id'] ) );
		
		$id=2;
		$result['poweroff'] = $this->DB->SelectAny('select poweroff_description from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['button_description_0'] = $this->DB->SelectAny('select button_description_0_0 ,button_description_0_1,button_description_0_2,button_description_0_3,button_description_0_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		//$result['button_description_1'] = $this->DB->SelectAny('select button_description_1_0,button_description_1_1,button_description_1_2,button_description_1_3,button_description_1_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		//$result['button_description_2'] = $this->DB->SelectAny('select button_description_2_0,button_description_2_1,button_description_2_2,button_description_2_3,button_description_2_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['visions_icon_description_0'] = $this->DB->SelectAny('select visions_icon_description_0_0,visions_icon_description_0_1,visions_icon_description_0_2,visions_icon_description_0_3,visions_icon_description_0_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		//$result['visions_icon_description_1'] = $this->DB->SelectAny('select visions_icon_description_1_0,visions_icon_description_1_1,visions_icon_description_1_2,visions_icon_description_1_3,visions_icon_description_1_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC'); 
		//$result['visions_icon_description_2'] = $this->DB->SelectAny('select visions_icon_description_2_0,visions_icon_description_2_1,visions_icon_description_2_2,visions_icon_description_2_3,visions_icon_description_2_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['url'] = $this->DB->SelectAny('select url from devicesimulator WHERE id='.$id.' ORDER BY id ASC');		
		return $result;
	}
	
	
	/* *******************************/
	function uploadImages($files, $arrayName, $uploaddir, $uploaddirDB){
		GLOBAL $ROOT_PATH;			
		$duplicateArray = array();		
		//$uploaddir   = $ROOT_PATH.'/images/thermal/thor/';
		//$uploaddir   = $ROOT_PATH.'/images/thermal/';
		//$uploaddirDB = '/images/thermal/thor/';
		//$uploaddirDB = '/images/thermal/';
		$ext='';
		//printArray($_FILES);
		
		
		if(!empty($files)){
			foreach($files as $key=>$value){
				foreach ($value as $keyIn => $valueIn ) {					
					//printArray($valueIn);
					//echo "<br>".$keyIn;
					//if( $key == 'size' && $ )
					foreach ($valueIn as $i=>$row){
						
						/*echo 'key = '.$key.'<br>';
						echo 'keyIn= '.$keyIn.'<br>';
						echo 'keyInIn = '.$i.' value='.$row.' <br>';*/
						
						/*if ($key == 'size' && $row <= 0 ) break;
						else continue; */
						
						if ( $key == 'tmp_name' && !empty($row) ) {
							//echo $row.'<br>'.$uploaddir.$arrayName.'_'.$keyIn.'_'.$i.'.'.$ext[1];
							//copy($row, $uploaddir.$arrayName.'_'.$keyIn.'_'.$i.'.'.$ext[1]);		
							
							if ( /*!is_dir ($uploaddir.$arrayName.'/'.$keyIn) && */ !file_exists($uploaddir.$arrayName.'/'.$keyIn) ){
								mkdir( $uploaddir.$arrayName, 0755 );
								mkdir( $uploaddir.$arrayName.'/'.$keyIn, 0755 );								
							}
							
							if ( is_dir($uploaddir.$arrayName.'/'.$keyIn ) ) {
								if (file_exists($uploaddir.$arrayName.'/'.$keyIn.'/'.$i.'.gif') )  { unlink($uploaddir.$arrayName.'/'.$keyIn.'/'.$i.'.gif');  } 
								if (file_exists($uploaddir.$arrayName.'/'.$keyIn.'/'.$i.'.jpeg'))  { unlink($uploaddir.$arrayName.'/'.$keyIn.'/'.$i.'.jpeg'); }
								if (file_exists($uploaddir.$arrayName.'/'.$keyIn.'/'.$i.'.jpg') )  { unlink($uploaddir.$arrayName.'/'.$keyIn.'/'.$i.'.jpg');  }
								if (file_exists($uploaddir.$arrayName.'/'.$keyIn.'/'.$i.'.png') )  { unlink($uploaddir.$arrayName.'/'.$keyIn.'/'.$i.'.png');  }
																
								copy( $row, $uploaddir.$arrayName.'/'.$keyIn.'/'.$i.'.'.strtolower($ext[1]) ); // str_to_lower
								//echo  $uploaddir.$arrayName.'/'.$keyIn.'/'.$i.'.'.strtolower($ext[1]).'<br>';
							}
							//copy( $row, $uploaddir.$arrayName.'/'.$keyIn.'/'.$i.'.'.$ext[1] );
							//echo $uploaddir.$arrayName.'/'.$keyIn.'<br />';							
						} 						
						if ( $key == 'type' && !empty($row) ) {						
							$ext = explode('/',$row);												
							$fileName[$arrayName.'_'.$keyIn.'_'.$i] = $uploaddirDB.$arrayName.'_'.$keyIn.'_'.$i.'.'.strtolower($ext[1]);
						}					
					}
				}				
			}						
		}
		if (!empty($fileName)) return $fileName = array_unique($fileName);
		else return false;
	}
	/*************************************/
	
	function textProcess( $array, $arrayName ){ // prepare to array text inputs for insert to database
		$result=array();
		if( !empty($array) && is_array( $array ) ) {			
			foreach ( $array as $key => $value ) {					
				//echo 'key='.$key.'<br /> value='.$value."<br>";					
				if( is_array($value) && !empty($value) ) {
					foreach ($value as $keyIn => $valIn ){
						//if ( !get_magic_quotes_gpc() ) 	$valIn = addslashes($valIn);							
						$result[$arrayName.'_'.$key.'_'.$keyIn] = trim( $valIn ); 
						//echo 'keyIn='.$keyIn.'<br /> value='.$valIn.'<br>';
					}
				} 
			}
		}
		else {
			//if ( !get_magic_quotes_gpc() ) 	$array = addslashes($array);
			if (!empty($array)) $result[$arrayName] = trim( $array ); 
		}					
		
		if (!empty ($result)) return $result;
		else return false;		
	}
	
	
	function thorAction( $request = array() ){ // upload pictures of device separated by names
		//printArray($_FILES['main_brightness']);	
		GLOBAL $ROOT_PATH;	
		if (!empty($_FILES)){
			$this->uploadImages($_FILES['main_color'], 'main_color', $ROOT_PATH.'/images/thermal/thor/', '/images/thermal/thor/');
			$this->uploadImages($_FILES['main_brightness'], 'main_brightness', $ROOT_PATH.'/images/thermal/thor/', '/images/thermal/thor/');
			//$this->uploadImages($_FILES['main_polarity'], 'main_polarity', $ROOT_PATH.'/images/thermal/thor/', '/images/thermal/thor/');
			$this->uploadImages($_FILES['reticle'], 'reticle', $ROOT_PATH.'/images/thermal/thor/', '/images/thermal/thor/');
			$this->uploadImages($_FILES['control_panel'], 'control_panel', $ROOT_PATH.'/images/thermal/thor/', '/images/thermal/thor/');
			$this->uploadImages($_FILES['arrow_button_ico'], 'arrow_button_ico', $ROOT_PATH.'/images/thermal/thor/', '/images/thermal/thor/');
			$this->uploadImages($_FILES['visons_icons'], 'visons_icons', $ROOT_PATH.'/images/thermal/thor/', '/images/thermal/thor/');
			
		}
		//$this->textProcess($)
		//printArray($_POST['button_description']);
		/*$this->textProcess($_POST['button_description'], 'button_description');
		$this->textProcess($_POST['visions_icon_description'], 'visions_icon_description');
		$this->textProcess($_POST['poweroff_description'], 'poweroff_description'); */			
	
		if ( !empty($_POST['button_description']) ) $this->DB->update('devicesimulator', $this->textProcess($_POST['button_description'], 'button_description'), array( 'id' => 1 ) ); // для серии ThOR id = $_POST['id'] ;
		if ( !empty($_POST['poweroff_description']) ) $this->DB->update('devicesimulator', $this->textProcess($_POST['poweroff_description'], 'poweroff_description'), array( 'id' => 1 ) );
		if ( !empty($_POST['visions_icon_description']) ) $this->DB->update('devicesimulator', $this->textProcess($_POST['visions_icon_description'], 'visions_icon_description'), array( 'id' => 1 ) ); 
		if ( !empty($_POST['url']) ) $this->DB->update('devicesimulator', $this->textProcess($_POST['url'], 'url'), array( 'id' => 1 ) );
	} 
	
	function defaultAction($request = array()){	// select description for buttons and actions of the device	
		$id = 1;		
		$result['poweroff'] = $this->DB->SelectAny('select poweroff_description from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['button_description_0'] = $this->DB->SelectAny('select button_description_0_0 ,button_description_0_1,button_description_0_2,button_description_0_3,button_description_0_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['button_description_1'] = $this->DB->SelectAny('select button_description_1_0,button_description_1_1,button_description_1_2,button_description_1_3,button_description_1_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['button_description_2'] = $this->DB->SelectAny('select button_description_2_0,button_description_2_1,button_description_2_2,button_description_2_3,button_description_2_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['visions_icon_description_0'] = $this->DB->SelectAny('select visions_icon_description_0_0,visions_icon_description_0_1,visions_icon_description_0_2,visions_icon_description_0_3,visions_icon_description_0_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['visions_icon_description_1'] = $this->DB->SelectAny('select visions_icon_description_1_0,visions_icon_description_1_1,visions_icon_description_1_2,visions_icon_description_1_3,visions_icon_description_1_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC'); 
		$result['visions_icon_description_2'] = $this->DB->SelectAny('select visions_icon_description_2_0,visions_icon_description_2_1,visions_icon_description_2_2,visions_icon_description_2_3,visions_icon_description_2_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['url'] = $this->DB->SelectAny('select url from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		//printArray ($result);
		return $result;
	}


	
	// запросы для страницы симулятора
/*	function fillTextDescription ($request = array()){
		$id=1;
		$result['poweroff'] = $this->DB->SelectAny('select poweroff_description from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['button_description_0'] = $this->DB->SelectAny('select button_description_0_0,button_description_0_1,button_description_0_2,button_description_0_3,button_description_0_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['button_description_1'] = $this->DB->SelectAny('select button_description_1_0,button_description_1_1,button_description_1_2,button_description_1_3,button_description_1_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['button_description_2'] = $this->DB->SelectAny('select button_description_2_0,button_description_2_1,button_description_2_2,button_description_2_3,button_description_2_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['visions_icon_description_0'] = $this->DB->SelectAny('select visions_icon_description_0_0,visions_icon_description_0_1,visions_icon_description_0_2,visions_icon_description_0_3,visions_icon_description_0_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		$result['visions_icon_description_1'] = $this->DB->SelectAny('select visions_icon_description_1_0,visions_icon_description_1_1,visions_icon_description_1_2,visions_icon_description_1_3,visions_icon_description_1_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC'); 
		$result['visions_icon_description_2'] = $this->DB->SelectAny('select visions_icon_description_2_0,visions_icon_description_2_1,visions_icon_description_2_2,visions_icon_description_2_3,visions_icon_description_2_4 from devicesimulator WHERE id='.$id.' ORDER BY id ASC');
		//printArray ($result);
		return $result;
	} */ 

	
	
}

?>