<?php 
class ControllerInformationDesign 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')) {
		
		 //$contest_master =  $this->model_information_myown->addCustomer($this->request->post);
		    /*$contentmail = 'Prize Amount :' . $this->request->post['prize_amount'] . "\n" . 'Deposit Amount :' . $this->request->post['deposit_amount'] . "\n" .
			'Email :' .$this->request->post['email'] . "\n" .
			'Description :' . $this->request->post['describe']  . "\n" . 
			'See where your imagination fits on these scales :' . "\n" . 
			'Masculine & Feminine : ' . $this->request->post['imag1'] . "\n" . 
			'Youthful & Mature : ' . $this->request->post['imag2'] . "\n" . 
			'Funky & Graceful : ' . $this->request->post['imag3'] . "\n" . 
			'Bold & Subtle : ' . $this->request->post['imag4'] . "\n" . 
			'Simple & Intricate : ' . $this->request->post['imag5'] . "\n" . 
			'Classic & Contemporary : ' . $this->request->post['imag6'] . "\n" . 
			'I wish to capture a special ocassion on my jewellery : ' . $this->request->post['ocassion']  ; 
			$additional_email = $this->request->post['email'];
			$mail = new Mail();
			$mail->protocol = $this->config->get('config_mail_protocol');
			$mail->parameter = $this->config->get('config_mail_parameter');
			$mail->hostname = $this->config->get('config_smtp_host');
			$mail->username = $this->config->get('config_smtp_username');
			$mail->password = $this->config->get('config_smtp_password');
			$mail->port = $this->config->get('config_smtp_port');
			$mail->timeout = $this->config->get('config_smtp_timeout');				
		//	$mail->setTo($this->config->get('config_email'));
			
			$to_addresses = array($this->config->get('config_email'),$additional_email);
			$mail->setTo($to_addresses);
			
	  		$mail->setFrom($this->request->post['email']); 
	  		$mail->setSender($this->request->post['prize_amount']);
	  		$mail->setSubject(sprintf($this->language->get('email_subject'), $this->request->post['prize_amount']));
	  		$mail->setText(strip_tags(html_entity_decode($contentmail, ENT_QUOTES,'UTF-8')));
			$mail->addAttachment(DIR_NEWIMAGE . $this->request->post['image1']);
      		$mail->send(); */
		 /*	$this->session->data['vouchers'][rand()] = array(
				'description'      => sprintf($this->language->get('text_for'), $this->currency->format($this->currency->convert($this->request->post['deposit_amount'], $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'        => $this->customer->getFirstName(),
				'from_email'       => $this->customer->getEmail(),
				'message'          => $this->request->post['describe'],
				'cid'               => $contest_master,
				'amount'           => $this->currency->convert($this->request->post['deposit_amount'], $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['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['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'] = '';
		}
		
		
	/*	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'] = '';
		}
		
		if (isset($this->request->post['funky_graceful'])) {
			$this->data['funky_graceful'] = $this->request->post['funky_graceful'];
		} else {
			$this->data['funky_graceful'] = '';
		}
		
		if (isset($this->request->post['bold_subtle'])) {
			$this->data['bold_subtle'] = $this->request->post['bold_subtle'];
		} else {
			$this->data['bold_subtle'] = '';
		}
		
		if (isset($this->request->post['simple_intricate'])) {
			$this->data['simple_intricate'] = $this->request->post['simple_intricate'];
		} else {
			$this->data['simple_intricate'] = '';
		}
		
		if (isset($this->request->post['classic_contemporary'])) {
			$this->data['classic_contemporary'] = $this->request->post['classic_contemporary'];
		} else {
			$this->data['classic_contemporary'] = '';
		} 
		
		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/design.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/information/design.tpl';
		} else {
			$this->template = 'default/template/information/design.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 ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 32)) {
      		$this->error['name'] = $this->language->get('error_name');
    	}

    	if ((utf8_strlen($this->request->post['enquiry']) < 10) || (utf8_strlen($this->request->post['enquiry']) > 3000)) {
      		$this->error['enquiry'] = $this->language->get('error_enquiry');
    	}

    	if (!isset($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
      		$this->error['captcha'] = $this->language->get('error_captcha');
    	}
		
		if (!$this->error) {
	  		return true;
		} else {
	  		return false;
		}  	  
  	}
}
?>