<?php
class EmailRecipient extends AppModel {

    var $name = 'EmailRecipient';


    var $belongsTo = array(
	    'Email' => array('className'=>'Email', 'foreign_key'=>'email_id'),
	    'User' => array('className' => 'User', 'foreign_key' =>'user_id'
	    )
    );

    /**
     * Given an Email ID, return a formatted array of the Users.
     *
     * $recipients['to']=> array(0=>array('first_name'=>'Bill Smith')) etc.
     * $recipients['from']=> array(...
     *
     * @param <type> $id
     */
    function getRecipients($id) {
	$recipients =  $this->find('all', array(
		'fields' => array('EmailRecipient.id','EmailRecipient.user_id', 'EmailRecipient.type', 'User.id', 'User.first_name', 'User.last_name','User.email', 'User.principle_id','User.type', 'User.customer_id'),
		'conditions'=>array('Email.id'=>$id)));

	$recipientFormatted = array();
	foreach($recipients as $recipient) {
	    switch($recipient['EmailRecipient']['type']) {
		case 'to':
		    $recipientFormatted['to'][] = $recipient['User'];
		    break;

		case 'cc': $recipientFormatted['cc'][] = $recipient['User'];
		    break;

		default:

	    }
	    
	}
    
	return $recipientFormatted;
    }


}
?>