<?php 
class ControllerInformationmyown extends Controller {
	private $error = array(); 
	    
  	public function index() {
	/*if (!$this->customer->isLogged()) {
	  		$this->session->data['redirect'] = $this->url->link('information/myown', '', 'SSL');
	  
	  		$this->redirect($this->url->link('account/login', '', 'SSL'));
    	}  */
		$this->language->load('information/myown');

    	$this->document->setTitle($this->language->get('heading_title'));  
		
		 $this->load->model('information/myown');
  
     /*    if ($this->request->server['REQUEST_METHOD'] == 'POST') {
   
         $this->model_information_myown->addCustomer($this->request->post);
      
         $this->redirect($this->url->link('account/successnew'));
     } */
	 
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
		
			$this->cart->clear();

						unset($this->session->data['shipping_method']);
						unset($this->session->data['shipping_methods']);
						unset($this->session->data['payment_method']);
						unset($this->session->data['payment_methods']);
						unset($this->session->data['guest']);
						unset($this->session->data['comment']);
						unset($this->session->data['order_id']);	
						unset($this->session->data['coupon']);
						unset($this->session->data['reward']);
						unset($this->session->data['voucher']);
						unset($this->session->data['vouchers']);

		
			$contest_master =  $this->model_information_myown->addCustomer($this->request->post);
		  
		 	$this->session->data['vouchers'][rand()] = array(
				'description'      => sprintf($this->language->get('text_for'), $this->currency->format($this->currency->convert(50, $this->currency->getCode(), $this->config->get('config_currency'))), $this->config->get('config_name')),
				'to_name'          => $this->config->get('config_name'),
				'to_email'         => $this->config->get('config_email'),
				'from_name'        => '',
				'from_email'       => '',
				'message'          => $this->request->post['describe'],
				'cid'               => $contest_master,
				'amount'           => $this->currency->convert(50, $this->currency->getCode(), $this->config->get('config_currency')),
				'voucher_theme_id' => 8
			);  
	  	  	 
	  		
			$this->redirect($this->url->link('checkout/cart'));
		
    	}
		
		$this->load->model('catalog/category');
		$this->load->model('tool/image');
			
		$this->data['informations'] = array();
			$results = $this->model_catalog_category->getInformations();
				foreach ($results as $result) {
					$this->data['informations'][] = array(
						'title'   	     => $result['title'],
						'description'    => $result['description'],
						);	
				}

      	$this->data['breadcrumbs'] = array();

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),        	
        	'separator' => false
      	);

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('information/myown'),
        	'separator' => $this->language->get('text_separator')
      	);	
			
    	$this->data['heading_title'] = $this->language->get('heading_title');

    	$this->data['text_location'] = $this->language->get('text_location');
		$this->data['text_contact'] = $this->language->get('text_contact');
		$this->data['text_address'] = $this->language->get('text_address');
    	$this->data['text_telephone'] = $this->language->get('text_telephone');
    	$this->data['text_fax'] = $this->language->get('text_fax');
    	$this->data['text_male'] = $this->language->get('text_male');
    	$this->data['text_female'] = $this->language->get('text_female');

    	$this->data['entry_describe'] = $this->language->get('entry_describe');
    	$this->data['entry_anniversary'] = $this->language->get('entry_anniversary');
    	$this->data['entry_firstjob'] = $this->language->get('entry_firstjob');
		$this->data['entry_marriage'] = $this->language->get('entry_marriage');
		$this->data['entry_birthdate'] = $this->language->get('entry_birthdate');
		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_justspecialme'] = $this->language->get('entry_justspecialme');
		$this->data['entry_email'] = $this->language->get('entry_email');
		$this->data['entry_male_female'] = $this->language->get('entry_male_female');
		$this->data['entry_age'] = $this->language->get('entry_age');

		if (isset($this->error['firstjob'])) {
    		$this->data['error_firstjob'] = $this->error['firstjob'];
		} else {
			$this->data['error_firstjob'] = '';
		}

		
		if (isset($this->error['designerror'])) {
			$this->data['error_design'] = $this->error['designerror'];
		} else {
			$this->data['error_design'] = '';
		}
		
		if (isset($this->error['specialoccassion'])) {
			$this->data['error_specialoccassion'] = $this->error['specialoccassion'];
		} else {
			$this->data['error_specialoccassion'] = '';
		}
		
		
		if (isset($this->error['anniversary'])) {
    		$this->data['error_anniversary'] = $this->error['anniversary'];
		} else {
			$this->data['error_anniversary'] = '';
		}
		
		
		if (isset($this->error['male_female'])) {
    		$this->data['error_male_female'] = $this->error['male_female'];
		} else {
			$this->data['error_male_female'] = '';
		}
	
        if (isset($this->error['age'])) {
			$this->data['error_age'] = $this->error['age'];
		} else {
			$this->data['error_age'] = '';
		}	
      	
		
		if (isset($this->error['enquiry'])) {
			$this->data['error_enquiry'] = $this->error['enquiry'];
		} else {
			$this->data['error_enquiry'] = '';
		}		
		
 		if (isset($this->error['captcha'])) {
			$this->data['error_captcha'] = $this->error['captcha'];
		} else {
			$this->data['error_captcha'] = '';
		}	

    	$this->data['button_continue'] = $this->language->get('button_continue');
    
		$this->data['action'] = $this->url->link('information/myown');
		$this->data['store'] = $this->config->get('config_name');
    	$this->data['address'] = nl2br($this->config->get('config_address'));
    	$this->data['telephone'] = $this->config->get('config_telephone');
    	$this->data['fax'] = $this->config->get('config_fax');
		
		
		$this->data['images'] = array();
		
		$results = $this->model_information_myown->get_image_pics();
		
		foreach ($results as $result) {
			if ($result['image']) {
				$image = $this->model_tool_image->resize($result['image'], 140, 140);
			} else {
				$image = false;
			}
				
		$this->data['images'][] = array(
					'theme'       => $result['theme'],
					'contest_sample_designs_id'  => $result['contest_sample_designs_id'],
					'thumb'       => $image
				);
		}
    	
		if (isset($this->request->post['prize_amount'])) {
			$this->data['prize_amount'] = $this->request->post['prize_amount'];
		} else {
			$this->data['prize_amount'] = '';
		}
		
		if (isset($this->request->post['deposit_amount'])) {
			$this->data['deposit_amount'] = $this->request->post['deposit_amount'];
		} else {
			$this->data['deposit_amount'] = '';
		}
		
		if (isset($this->request->post['design_category'])) {
			$this->data['design_category'] = $this->request->post['design_category'];
		} else {
			$this->data['design_category'] = '';
		} 
		if (isset($this->request->post['describe'])) {
			$this->data['describe'] = $this->request->post['describe'];
		} else {
			$this->data['describe'] = '';
		} 
		
		if (isset($this->request->post['ocassion'])) {
			$this->data['ocassion'] = $this->request->post['ocassion'];
		} else {
			$this->data['ocassion'] = '';
		} 
		
		if (isset($this->request->post['male_female'])) {
			$this->data['male_female'] = $this->request->post['male_female'];
		} else {
			$this->data['male_female'] = $this->customer->getFirstName();
		}
		
		if (isset($this->request->post['marriage'])) {
			$this->data['marriage'] = $this->request->post['marriage'];
		} else {
			$this->data['marriage'] = $this->customer->getFirstName();
		}
		
		if (isset($this->request->post['anniversary'])) {
			$this->data['anniversary'] = $this->request->post['anniversary'];
		} else {
			$this->data['anniversary'] = $this->customer->getFirstName();
		}
		
		if (isset($this->request->post['firstjob'])) {
			$this->data['firstjob'] = $this->request->post['firstjob'];
		} else {
			$this->data['firstjob'] = $this->customer->getFirstName();
		}
		
		
		if (isset($this->request->post['age'])) {
			$this->data['age'] = $this->request->post['age'];
		} else {
			$this->data['age'] = $this->customer->getFirstName();
		}
	
		
		if (isset($this->request->post['birthdate'])) {
			$this->data['birthdate'] = $this->request->post['birthdate'];
		} else {
			$this->data['birthdate'] = $this->customer->getEmail();
		}
		
		if (isset($this->request->post['justspecialme'])) {
			$this->data['justspecialme'] = $this->request->post['justspecialme'];
		} else {
			$this->data['justspecialme'] = '';
		}
		
		
		if (isset($this->request->post['masculine_feminine'])) {
			$this->data['masculine_feminine'] = $this->request->post['masculine_feminine'];
		} else {
			$this->data['masculine_feminine'] = '37';
		}
		
		
	/*	if (isset($this->request->post['themes'])) {
			$this->data['themes'] = $this->request->post['themes'];
		} else {
			$this->data['themes'] = '';
		} */
		
	//	$this->log->write('jjjjjjfffffffffffffffff');
		
	/*	if (isset($this->request->post['themes'])) {
		$ids = implode(',' , $this->request->post['themes']);
		$this->log->write($ids.'fffffffffffffffff');
		$this->data['all_themes'] = $this->request->post[$ids];
		}  */
		
		if (isset($this->request->post['themeee'])) {
			$this->data['themeee'] = $this->request->post['themeee'];
		} else {
			$this->data['themeee'] = array(0);
		}
		
		if (isset($this->request->post['youthful_mature'])) {
			$this->data['youthful_mature'] = $this->request->post['youthful_mature'];
		} else {
			$this->data['youthful_mature'] = '37';
		}
		
		if (isset($this->request->post['funky_graceful'])) {
			$this->data['funky_graceful'] = $this->request->post['funky_graceful'];
		} else {
			$this->data['funky_graceful'] = '37';
		}
		
		if (isset($this->request->post['bold_subtle'])) {
			$this->data['bold_subtle'] = $this->request->post['bold_subtle'];
		} else {
			$this->data['bold_subtle'] = '37';
		}
		
		if (isset($this->request->post['simple_intricate'])) {
			$this->data['simple_intricate'] = $this->request->post['simple_intricate'];
		} else {
			$this->data['simple_intricate'] = '37';
		}
		
		if (isset($this->request->post['classic_contemporary'])) {
			$this->data['classic_contemporary'] = $this->request->post['classic_contemporary'];
		} else {
			$this->data['classic_contemporary'] = '37';
		} 
		
		if (isset($this->request->post['image1'])) {
			$this->data['image1'] = $this->request->post['image1'];
		} else {
			$this->data['image1'] = '';
		}
		
		$this->load->model('information/myown');
		
		$this->data['occassions'] = $this->model_information_myown->getoccasionsdes();
		
		$this->data['designs_categorys'] = $this->model_information_myown->get_design_category();
		
		
		if (isset($this->request->post['captcha'])) {
			$this->data['captcha'] = $this->request->post['captcha'];
		} else {
			$this->data['captcha'] = '';
		}		

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/information/myown.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/information/myown.tpl';
		} else {
			$this->template = 'default/template/information/myown.tpl';
		}
		
		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'
		);
				
 		$this->response->setOutput($this->render());		
  	}
	
	public function upload() {
		
		$json = array();
		
		if (!empty($this->request->files['file']['name'])) {
			$filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'));
			
			if ((strlen($filename) < 3) || (strlen($filename) > 128)) {
        		$json['error'] = $this->language->get('error_filename');
	  		}	  	
			
			$allowed = array();
			
			$filetypes = explode(',', $this->config->get('config_upload_allowed'));
			
			foreach ($filetypes as $filetype) {
				$allowed[] = trim($filetype);
			}
			
			if (!in_array(substr(strrchr($filename, '.'), 1), $allowed)) {
				$json['error'] = $this->language->get('error_filetype');
       		}	
						
			if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
				$json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
			}
		} else {
			$json['error'] = $this->language->get('error_upload');
		}
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && !isset($json['error'])) {
			if (is_uploaded_file($this->request->files['file']['tmp_name']) && file_exists($this->request->files['file']['tmp_name'])) {
				$file = basename($filename);
				
				// Hide the uploaded file name sop people can not link to it directly.
				$this->load->library('encryption');
				
				$encryption = new Encryption($this->config->get('config_encryption'));
				
				$json['file'] = $file;
				
				move_uploaded_file($this->request->files['file']['tmp_name'], DIR_NEWIMAGE . $file);
				
			}
						
			$json['success'] = $this->language->get('text_upload');
		}	
		
		$this->response->setOutput(json_encode($json));		
	}

  	public function success() {
		$this->language->load('information/myown');

		$this->document->setTitle($this->language->get('heading_title')); 

      	$this->data['breadcrumbs'] = array();

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),
        	'separator' => false
      	);

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('information/myown'),
        	'separator' => $this->language->get('text_separator')
      	);	
		
    	$this->data['heading_title'] = $this->language->get('heading_title');

    	$this->data['text_message'] = $this->language->get('text_message');

    	$this->data['button_continue'] = $this->language->get('button_continue');

    	$this->data['continue'] = $this->url->link('common/home');

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/success.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/success.tpl';
		} else {
			$this->template = 'default/template/common/success.tpl';
		}
		
		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'
		);
				
 		$this->response->setOutput($this->render()); 
	}

	public function captcha() {
		$this->load->library('captcha');
		
		$captcha = new Captcha();
		
		$this->session->data['captcha'] = $captcha->getCode();
		
		$captcha->showImage();
	}
	
  	private function validate() {
	
		if ($this->request->post['design_category'] == '') {
      		$this->error['designerror'] = $this->language->get('error_design');
    	}
		
		if ($this->request->post['ocassion'] == '') {
      		$this->error['specialoccassion'] = $this->language->get('error_specialoccassion');
    	}
		

		if (!$this->error) {
			return true;
		} else {
	  		return false;
		}  	  
  	}
}
?>