<?PHP
class messages extends engine {

	var $call_sql_class = array('templates', 'language_menus', 'emails');
	var $call_class = array('Form', 'Email', 'Functions', 'Editor');

	var $message_types = array(
		array('name' => 'EMAIL'),
		array('name' => 'SMS'),
		array('name' => 'LETTER')
	);

	function get_emails_data(){

		$where = '';
		$order = '';
		if($_GET['filter'])
			$where = $this->emails->filter($_GET, array('client_email', 'client_name', 'from_name', 'from_email', 'subject', 'is_sent', 'created_at', 'sent_at'));
		if($_GET['sort'])
			$order = " ORDER BY ".$_GET['sort']." ".$_GET['order'];

		$this->emails->emails(($where ? "WHERE " : "").$where.$order);
		return $this;
	}
	function get_mass_mail_data(){

		$this->templates->Select("WHERE type IN('EMAIL') ORDER BY name");
		return $this;
	}
	function get_statistics_data(){

		//$this->banners->Select();
		return $this;
	}
	function get_emailer_data(){

		$this->emails->query_limit(50);
		if($_GET['search']){

			$list = array('client_name', 'client_email', 'subject', 'content', 'is_sent', 'created_at', 'sent_at');
			foreach($list as $v){

				if($_GET[$v]){

					$this->emails->Select($v." LIKE '%".$_GET[$v]."%' ");
				}
			}
		}
		$this->emails->Select();
		return $this;
	}
	function get_dashboard_data(){

		$this->templates->Select("WHERE type IN('SMS', 'EMAIL', 'LETTER')");

		return $this;
	}

	function get_edit_data(){

		return $this;
	}

	function get_messages_data(){

		return $this;
	}

	function get_send_to_friend_data(){

		$this->email_content = $this->Functions->getMessage('friend_email', 'EMAIL', $_SESSION['language']);
		return $this;
	}

	function message_list(){

		$this->language_menus->Select("");

		$this->templates->Select("WHERE type IN('SMS', 'EMAIL', 'LETTER')");
		return $this;
	}

	function edit(){

		$this->language_menus->Select("");

		$this->templates->Select("WHERE id = ".$_GET['id']);
		return $this;
	}


	//update
	function insert(){

		$this->templates->defaults(array('is_active' => 0));
		$this->templates->Insert($_POST);
	}

	function update(){

		$_POST['content'] = $this->removeFirefoxBr($_POST['content']);

		$this->templates->defaults(array('is_active' => 0));
		$this->templates->Update($_POST, "WHERE id = ".$_POST['id']);
	}

	function delete(){

		$this->templates->Delete("WHERE id = ".$_GET['id']);
	}

	function removeFirefoxBr($content){

		$content = str_replace('&amp;', '&#38;', $content);
		$content = addslashes(str_replace('<br type="_moz" />', '', stripslashes($content)));

		return $content;
	}

	function send_to_friend(){

		$email = $this->Functions->getMessage('friend_email', 'EMAIL', $_SESSION['language']);

		$this->Email->SendEmail($_POST['to_name'], $_POST['to_email'], $_POST['from_name'], $email['from_email'], $email['subject'], nl2br($email['content']).'<br /><br />'.$_POST['comment'].'<br /><br />'.$_POST['from_name']);
		die(json_encode(array('status'=>$this->Select('Message sent'), 'reload' => true)));
	}

	function create_emails(){

		$email_data = $this->Functions->getMessage($_POST['message'], 'EMAIL', $_SESSION['language']);

		foreach(explode(',', $_POST['emails']) as $email){

			$email = trim($email);
			$this->emails->Insert(array(
				'client_email' => $email,
				'client_name' => $email,
				'from_name' => $email_data['from_name'],
				'from_email' => $email_data['from_email'],
				'subject' => $email_data['subject'],
				'content' => addslashes($email_data['content']),
				'created_at' => 'NOW()'
			));
		}
	}
}

?>