<?php
class ControllerDesaccountDesignerprofile extends Controller {
	private $error = array(); 

	public function index() {
		if ($this->designer->isLogged()) {
			$this->session->data['redirect'] = $this->url->link('desaccount/designerprofile', '', 'SSL');

		//	$this->redirect($this->url->link('desaccount/login', '', 'SSL'));
		}

		$this->language->load('desaccount/mydesign');
		
		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('desaccount/designer');
		
		$this->load->model('account/customer');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
		
			$this->model_desaccount_designer->editnewsampletab($this->request->post);
			
			$des_id = $_GET['dsid'];
			
			$cust_id = $this->customer->getId();
			
			$des_mail_id = $this->db->query("SELECT email,dispname,designer_id from " . DB_PREFIX . "designer where designer_id = '" . (int)$des_id  . "'");
			
			$des_mail_id_2 = $this->db->query("SELECT name ,MAX(contest_id) AS connid from " . DB_PREFIX . "contest_master where customer_id = '" . (int)$cust_id  . "'");
			
			$des_mail_id_3 = $this->db->query("SELECT max(c.contest_id) as contest_id  from " . DB_PREFIX . "contest_master c where c.customer_id = '" . (int)$cust_id  . "'");
			
			$storelogo = $this->db->query("	SELECT value FROM `setting` a,contest_master b where `key`='config_logo' and a.store_id=b.store_id and b.contest_id='" . $des_mail_id_3->row['contest_id'] . "'");
			$contestlogo='<p>	<img alt="" src="'. HTTPS_IMAGE . $storelogo->row['value'] .'"  /></p>' . '<br>';
			
			if($des_mail_id_3->num_rows != 0)
			{
			
		//	SELECT name,MAX(contest_id) FROM `contest_master` WHERE customer_id=205
			
			$link = HTTP_SERVER.'index.php?route=desaccount/overall&cid='.$des_mail_id_2->row['connid'];
			
			$message =  $contestlogo;
			
			$message .= 'Hi '.$des_mail_id->row['dispname'] . ',<br><br>';
			
//			$message .= "<b>".$this->customer->getFirstName() . "</b>&nbsp;" .'has invited you to submit designs for contest'. "&nbsp;<b>" .$des_mail_id_2->row['name'].'.</b><br><br>';
			$message .= "<b>".$this->customer->getFirstName() . "</b>&nbsp;" .'has invited you to submit designs for contest.</b><br><br>';
			
			$message .= 'Please check the design brief for this contest here:' . "&nbsp;&nbsp;" . $link . '<br><br>';
			
			$message .= 'Regards, <br><br>';
			$message .= 'Customer Service Team<br><br>';
			
			$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($des_mail_id->row['email']);
	  		$mail->setFrom($this->config->get('config_email'));
		  	$mail->setSender($this->config->get('config_name'));
	  		$mail->setSubject(sprintf('Invitation to submit designs for contest'));
	  		$mail->setHtml((html_entity_decode($message, ENT_QUOTES, 'UTF-8')));
      		$mail->send();
			
			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('desaccount/designerlist', '', 'SSL'));
		} else {
		print '<script type="text/javascript">';
		print 'alert("You currently dont have any active contest")';
		print '</script>'; 
		}
		}

      	$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('text_account'),
			'href'      => $this->url->link('desaccount/account', '', 'SSL'),        	
        	'separator' => $this->language->get('text_separator')
      	);

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->language->get('text_edit'),
			'href'      => $this->url->link('desaccount/designerprofile', '', 'SSL'),       	
        	'separator' => $this->language->get('text_separator')
      	);
		
		$this->data['logged'] = $this->customer->isLogged();
		
		$this->data['heading_title'] = $this->language->get('heading_title');

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

		$this->data['entry_firstname'] = $this->language->get('entry_firstname');
		$this->data['entry_lastname'] = $this->language->get('entry_lastname');
		$this->data['entry_email'] = $this->language->get('entry_email');
		$this->data['entry_fax'] = $this->language->get('entry_fax');
		$this->data['entry_password'] = $this->language->get('entry_password');
    	$this->data['entry_confirm'] = $this->language->get('entry_confirm');
    	$this->data['entry_dispname'] = $this->language->get('entry_dispname');
    	$this->data['entry_profpic'] = $this->language->get('entry_profpic');
		$this->data['text_browse'] = $this->language->get('text_browse');
		$this->data['text_clear'] = $this->language->get('text_clear');	
		$this->data['entry_gender'] = $this->language->get('entry_gender');	
		$this->data['text_male'] = $this->language->get('text_male');	
		$this->data['text_female'] = $this->language->get('text_female');	
		$this->data['text_english'] = $this->language->get('text_english');	
		$this->data['text_french'] = $this->language->get('text_french');	
		$this->data['text_italin'] = $this->language->get('text_italin');	
		$this->data['entry_yourself'] = $this->language->get('entry_yourself');	
		$this->data['entry_langknown'] = $this->language->get('entry_langknown');	
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		$this->data['text_cheque'] = $this->language->get('text_cheque');
		$this->data['text_paypal'] = $this->language->get('text_paypal');
		$this->data['text_money'] = $this->language->get('text_money');
		$this->data['text_bank'] = $this->language->get('text_bank');
		$this->data['entry_paypalemail'] = $this->language->get('entry_paypalemail');
		$this->data['text_page_content'] = $this->language->get('text_page_content');
		$this->data['text_page_content2'] = $this->language->get('text_page_content2');

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

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

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

		if (isset($this->error['lastname'])) {
			$this->data['error_lastname'] = $this->error['lastname'];
		} else {
			$this->data['error_lastname'] = '';
		}
		
		if (isset($this->error['email'])) {
			$this->data['error_email'] = $this->error['email'];
		} else {
			$this->data['error_email'] = '';
		}	
	
		
		if (isset($this->error['password'])) { 
			$this->data['error_password'] = $this->error['password'];
		} else {
			$this->data['error_password'] = '';
		}

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

		$this->data['action'] = $this->url->link('desaccount/designerprofile', 'dsid=' . $_GET["dsid"], 'SSL');
		
		
		$this->data['designerid'] = $this->designer->getId();
		
		$this->data['designer_details'] = array();
		
		$this->load->model('tool/image');
		
	//	$this->data['idd'] = $_SESSION['views'];
		
		$results2 = $this->model_desaccount_designer->passing_designer_id($this->data['designerid']);
		
		foreach ($results2 as $result) {
			
		$this->data['designer_details'][] = array(
					'product_id'  => $result['product_id'],
					'sketch_id'  => $result['sketch_id'],
					'image'  => $result['image']
				);
		}
		
		if(isset($_GET['sid']))
		
		{
		
		$this->data['con_id'] = $_GET['sid'];  
		
		$this->data['desgn_id'] = $this->model_account_customer->getsketchidnew2($this->data['con_id']);
		
		$this->data['desgnn_id'] = $this->model_account_customer->getsketchidnew3($this->data['con_id']);	
		
		$this->data['desgn_img'] = $this->model_tool_image->resize($this->data['desgn_id'], 200, 280);
		
		$this->data['designer_details_new'] = array();
		$results2 = $this->model_account_customer->getting_designer_newdetails($this->data['con_id']);
		foreach ($results2 as $result) {
			
		$this->data['designer_details_new'][] = array(
					'dispname'  => $result['dispname'],
					'lang'  => $result['lang'],
					'lang1'  => $result['lang1'],
					'lang2'  => $result['lang2'],
					'about_yourself'  => $result['about_yourself']
				);
		}
		
		$this->data['designer_images'] = array();
		
		$results2 = $this->model_account_customer->getting_designer_newimages($this->data['desgnn_id']);
		
		foreach ($results2 as $result) {
			
		if ($result['image']) {
				$image = $this->model_tool_image->resize($result['image'], 170, 170);
			} else {
				$image = false;
			}
			
		$this->data['designer_images'][] = array(
					'imagee'  => $image
				);
		}
		
		}
		
		if(isset($_GET['dsid']))
		
		{
		
		$this->data['designer_id'] = $_GET['dsid'];  
		
	//	$this->data['desgn_id'] = $this->model_account_customer->getsketchidnew2($this->data['con_id']);
		
		$this->data['des_imagee'] = $this->model_account_customer->getsketchidnew3_image($this->data['designer_id']);	
		
		$this->data['desgn_img'] = $this->model_tool_image->resize($this->data['des_imagee'], 200, 280);
		
		$this->data['designer_details_new'] = array();
		$results2 = $this->model_account_customer->getting_designer_newdetails_part2($this->data['designer_id']);
		foreach ($results2 as $result) {
			
		$this->data['designer_details_new'][] = array(
					'dispname'  => $result['dispname'],
					'lang'  => $result['lang'],
					'lang1'  => $result['lang1'],
					'lang2'  => $result['lang2'],
					'about_yourself'  => $result['about_yourself']
				);
		}
		
		$this->data['designer_images'] = array();
		
		$results2 = $this->model_account_customer->getting_designer_newimages($this->data['designer_id']);
		
		foreach ($results2 as $result) {
			
		if ($result['image']) {
				$image = $this->model_tool_image->resize($result['image'], 170, 170);
			} else {
				$image = false;
			}
			
		$this->data['designer_images'][] = array(
					'imagee'  => $image
				);
		}
		
		}
		
		if (isset($this->request->post['ones'])) {
    		$this->data['ones'] = $this->request->post['ones'];
		} else {
			$this->data['ones'] = '';
		}

		
		
		
		
		
		
		
		if ($this->request->server['REQUEST_METHOD'] != 'POST') {
			$designer_info = $this->model_desaccount_designer->getdesigner($this->designer->getId());
		}

	/*	if (isset($this->request->post['firstname'])) {
			$this->data['firstname'] = $this->request->post['firstname'];
		} elseif (isset($designer_info)) {
			$this->data['firstname'] = $designer_info['firstname'];
		} else {
			$this->data['firstname'] = '';
		}

		if (isset($this->request->post['lastname'])) {
			$this->data['lastname'] = $this->request->post['lastname'];
		} elseif (isset($designer_info)) {
			$this->data['lastname'] = $designer_info['lastname'];
		} else {
			$this->data['lastname'] = '';
		}

		if (isset($this->request->post['email'])) {
			$this->data['email'] = $this->request->post['email'];
		} elseif (isset($designer_info)) {
			$this->data['email'] = $designer_info['email'];
		} else {
			$this->data['email'] = '';
		}  */

		if (isset($this->request->post['newsletter'])) {
			$this->data['newsletter'] = $this->request->post['newsletter'];
		} else {
			$this->data['newsletter'] = '';
		}
		
		if (isset($this->request->post['password'])) {
    		$this->data['password'] = $this->request->post['password'];
		} else {
			$this->data['password'] = '';
		}

		if (isset($this->request->post['confirm'])) {
    		$this->data['confirm'] = $this->request->post['confirm'];
		} else {
			$this->data['confirm'] = '';
		}
		
		// address Starts
		
		$this->data['button_edit'] = $this->language->get('button_edit');
		$this->data['entry_newsletter'] = $this->language->get('entry_newsletter');
    	$this->data['button_delete'] = $this->language->get('button_delete');
		
		$this->data['addresses'] = array();
		$this->load->model('desaccount/address');
		$results = $this->model_desaccount_address->getAddresses();

    	foreach ($results as $result) {
			if ($result['address_format']) {
      			$format = $result['address_format'];
    		} else {
				$format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
			}
		
    		$find = array(
	  			'{firstname}',
	  			'{lastname}',
	  			'{company}',
      			'{address_1}',
      			'{address_2}',
     			'{city}',
      			'{postcode}',
      			'{zone}',
				'{zone_code}',
      			'{country}'
			);
	
			$replace = array(
	  			'firstname' => $result['firstname'],
	  			'lastname'  => $result['lastname'],
	  			'company'   => $result['company'],
      			'address_1' => $result['address_1'],
      			'address_2' => $result['address_2'],
      			'city'      => $result['city'],
      			'postcode'  => $result['postcode'],
      			'zone'      => $result['zone'],
				'zone_code' => $result['zone_code'],
      			'country'   => $result['country']  
			);

      		$this->data['addresses'][] = array(
        		'address_id' => $result['address_id'],
        		'address'    => str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format)))),
        		'update'     => $this->url->link('desaccount/address/update', 'address_id=' . $result['address_id'], 'SSL'),
				'delete'     => $this->url->link('desaccount/address/delete', 'address_id=' . $result['address_id'], 'SSL')
      		);
    	}
		
		$this->data['newsletter'] = $this->designer->getNewsletter();
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		
		// address Ends

		$this->data['back'] = $this->url->link('desaccount/account', '', 'SSL');

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/desaccount/designerprofile.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/desaccount/designerprofile.tpl';
		} else {
			$this->template = 'default/template/desaccount/designerprofile.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());	
	}

	private function validate() {
		if ((utf8_strlen($this->request->post['firstname']) < 1) || (utf8_strlen($this->request->post['firstname']) > 32)) {
			$this->error['firstname'] = $this->language->get('error_firstname');
		}

		if ((utf8_strlen($this->request->post['lastname']) < 1) || (utf8_strlen($this->request->post['lastname']) > 32)) {
			$this->error['lastname'] = $this->language->get('error_lastname');
		}

		if ((utf8_strlen($this->request->post['email']) > 96) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
			$this->error['email'] = $this->language->get('error_email');
		}
		
		if (($this->designer->getEmail() != $this->request->post['email']) && $this->model_desaccount_designer->getTotaldesignersByEmail($this->request->post['email'])) {
			$this->error['warning'] = $this->language->get('error_exists');
		}


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