<?php
require_once(DIR_FS_ADMIN.DIR_WS_CLASSES.'email.php');
/**
 * Class for emails
 *
 */
class emails extends databasetable
{
	/**
	 * Initiate class
	 *
	 */
	function emails(){
		$this->set_tablename(TABLE_EMAILS);
		$this->set_index('emails_id');
		$this->related_tables = array();
	}
	/**
	 * Get a single Object
	 *
	 * @param integer $id
	 * @return object
	 */
	function get_email($id){
		$values = $this->get_row_by_index($id);
		$email_instance = new email( $this->array2object(current($values)) );
		return $email_instance;
	}
	
	function get_empty_email_object_from_customer($customers_id){
		$customers_email = $this->query(sprintf("SELECT customers_email_address FROM %s WHERE customers_id='%s'",TABLE_CUSTOMERS,$customers_id));
		$customers_email_address = current($customers_email);
		$values = array(
			'subject' => DEFAULT_EMAIL_SUBJECT,
			'from_email' => $customers_email_address['customers_email_address'],
			'customers_id' => $customers_id,
			'body' => DEFAULT_EMAIL_FOOTER
		);
		$email_instance = new email( $this->array2object($values) );
		return $email_instance;
	}
	
	function get_customers_emails($id){
		return $this->query(sprintf("SELECT emails_id,concat(from_email,' ',from_name) as from_string,subject,date_saved FROM %s WHERE customers_id='%s'",$this->get_tablename(),$id));
	}
	
	function get_number_of_allocated_emails_for_customer($id){
		$num_query = $this->query(sprintf("SELECT COUNT(emails_id) as num FROM %s WHERE customers_id='%s'",$this->get_tablename(),$id));
		$num = current($num_query);
		return $num['num'];
	}
}
