<?php
class ModelUserContest extends Model {
	public function addcontest($data) {
		$this->db->query("INSERT INTO `" . DB_PREFIX . "contest_master` SET name = '" . $this->db->escape($data['name']) . "', password = '" . $this->db->escape(md5($data['password'])) . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', user_group_id = '" . (int)$data['user_group_id'] . "', status = '" . (int)$data['status'] . "', date_added = NOW()");
	}
	
	public function editcontest($contest_id, $data) {
		$this->db->query("UPDATE `" . DB_PREFIX . "contest_master` SET name = '" . $this->db->escape($data['name']) . "', prize_amount = '" . $this->db->escape($data['prize_amount']) . "', status = '" . (int)$data['status'] . "' WHERE contest_id = '" . (int)$contest_id . "'");
		
		if($data['status']==3){
			$storelogo = $this->db->query("	SELECT value,b.name as contestname FROM `setting` a,contest_master b where `key`='config_logo' and a.store_id=b.store_id and b.contest_id='" . $contest_id . "'");
			$contestlogo='<p>	<img alt="" src="'. HTTPS_IMAGE . $storelogo->row['value'] .'"  /></p>' . '<br>';
			
		
		    $mailcmd = $this->db->query("SELECT distinct c.email,d.dispname,c.firstname,csa.contest_id from " . DB_PREFIX . "designer d ," . DB_PREFIX . "contest_sketch_assignment csa ," . DB_PREFIX . "sketches_master sm ," . DB_PREFIX . "customer c , " . DB_PREFIX . "contest_master cm where csa.sketch_id = sm.sketch_id and cm.customer_id = c.customer_id and sm.designer_id = d.designer_id and  csa.contest_id=cm.contest_id and cm.contest_id=" .(int)$contest_id);
			$information_info = $this->db->query("SELECT * FROM " . DB_PREFIX . "information_description WHERE language_id=1 and information_id = '45'");

			$message = $contestlogo . html_entity_decode($information_info->row['description'], ENT_QUOTES, 'UTF-8'); 
			$message = str_replace('%CustomerFirstName%',$mailcmd->row['firstname'],$message);
			$message = str_replace('%ContestId%',$contest_id ,$message);
			$message = str_replace('%ContestName%',$storelogo->row['contestname'],$message);
		
		  if($mailcmd->num_rows != 0)
		  {			
			$link = HTTP_SERVER.'index.php?route=desaccount/overall&cid='. $mailcmd->row['contest_id'];
//			$message = html_entity_decode($information_info->row['description'], ENT_QUOTES, 'UTF-8');
			$subject = html_entity_decode($information_info->row['title'], ENT_QUOTES, 'UTF-8');  

			$emails = array();
			$emails[] = $mailcmd->row['email'];
			$emails = array_unique($emails);
			foreach ($emails as $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($email);
//				$mail->setTo($email.','.$this->config->get('config_email'));
		  		$mail->setFrom($this->config->get('config_email'));
		  		$mail->setSender($this->config->get('config_name'));
	  			$mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
	  			$mail->setHtml((html_entity_decode($message, ENT_QUOTES, 'UTF-8')));
			//	$mail->addAttachment(DIR_IMAGE . $this->config->get('config_logo'));
	      		$mail->send();
			}
		  }
		}
	}

	public function editPassword($contest_id, $password) {
		$this->db->query("UPDATE `" . DB_PREFIX . "user` SET password = '" . $this->db->escape(md5($password)) . "' WHERE user_id = '" . (int)$user_id . "'");
	}

	public function editCode($email, $code) {
		$this->db->query("UPDATE `" . DB_PREFIX . "contest_master` SET code = '" . $this->db->escape($code) . "' WHERE email = '" . $this->db->escape($email) . "'");
	}
			
	public function deletecontest($contest_id) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "contest_master` WHERE contest_id = '" . (int)$contest_id . "'");
	}
	
	public function getcontest($contest_id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "contest_master` WHERE contest_id = '" . (int)$contest_id . "'");
	
		return $query->row;
	}
	
	public function getcontestBycontestname($name) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "contest_master` WHERE name = '" . $this->db->escape($name) . "'");
	
		return $query->row;
	}
		
	public function getcontestByCode($code) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "contest_master` WHERE code = '" . $this->db->escape($code) . "' AND code != ''");
	
		return $query->row;
	}
		
	public function getcontests($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "contest_master`";
			
		$sort_data = array(
			'name',
			'status',
			'date_added'
		);	
			
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY contest_id ";	
		}
			
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " DESC";
		}
		
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
		
	public function getcontest_status_master_des($data = array()) {
		$sql = "SELECT * FROM `" . DB_PREFIX . "contest_status_master`";
			
		$query = $this->db->query($sql);
	
		return $query->rows;
	}

	public function getTotalcontests() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "contest_master`");
		
		return $query->row['total'];
	}

	public function get_customer_name($customer_id) {
      	$query = $this->db->query("SELECT CONCAT(firstname, ' ',lastname) AS name FROM `" . DB_PREFIX . "customer` WHERE customer_id = '" . (int)$customer_id . "'");
		
		if(isset($query->row['name']))
		{
		return $query->row['name'];
		} else {
		return '';
		}
	}

	public function get_store_name($store_id) {
      	$query = $this->db->query("SELECT name FROM `" . DB_PREFIX . "store` WHERE store_id = '" . (int)$store_id . "'");
		
		return $query->row['name'];
	
	}

	public function get_status_contest($status) {
      	$query = $this->db->query("SELECT description FROM `" . DB_PREFIX . "contest_status_master` WHERE contest_status_master_id = '" . (int)$status . "'");
		
		return $query->row['description'];
	
	}

	public function getTotalcontestsByGroupId($user_group_id) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "contest_master` WHERE user_group_id = '" . (int)$user_group_id . "'");
		
		return $query->row['total'];
	}
	
	public function getTotalcontestsByEmail($email) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "contest_master` WHERE email = '" . $this->db->escape($email) . "'");
		
		return $query->row['total'];
	}	
}
?>