<?php
class EmailsController extends AppController {

    var $name = 'Emails';
    var $helpers = array('Html', 'Form', 'Number', 'Text', 'Link');
    var $components = array('RequestHandler');



    var $paginate = array(
	    'fields'=> array('Email.id', 'Email.user_id', 'Email.subject','Email.udate','User.id','User.first_name','User.last_name','User.email'),
	    'contain' => array('User', 'EmailRecipient'),
	    'limit' => 150,
	    'order'=>array('Email.id' => 'desc')

    );


    function index() {
	$this->Email->recursive = 0;
	$this->set('emails', $this->paginate());
    }

    function view($id = null) {

	if (!$id) {
	    $this->Session->setFlash(__('Invalid Email.', true));
	    $this->redirect(array('action'=>'index'));
	}

	$email = $this->Email->read(null, $id);
	$this->set('email', $email);


	$this->set('recipients', $this->Email->EmailRecipient->getRecipients($id));

	$body_ID = $this->findBodyID($email['EmailAttachment']);
	$this->set('body_ID', $body_ID);
    }

    /**
     * Returns the email_attachment ID of the first HTML attachment.
     *
     *
     * @param <type> $email_attachments
     * @return <type>
     */
    function findBodyID($email_attachments) {
	foreach($email_attachments as $attachment) {
	    if($attachment['is_message_body'] == 1) {
		return $attachment['id'];
	    }
	}

	//Havent found one yet. Just return the first email attachment ID.
	foreach($email_attachments as $attachment) {
	    return $attachment['id'];
	}
	

	//Should do something else here to take a stab at it at least.
	return false;

    }

    function frame($id = null) {
	$email = $this->Email->find('first', array('conditions'=>array('Email.id'=>$id)));

	$body_ID = $this->findBodyID($email['EmailAttachment']);
	$this->set('body_ID', $body_ID);
	$this->set('recipients', $this->Email->EmailRecipient->getRecipients($id));

	$this->set('email', $email);
    }


    function print_frame($id = null) {
	$this->layout = 'ajax';
	$email = $this->Email->find('first', array('conditions'=>array('Email.id'=>$id)));

	$body_ID = $this->findBodyID($email['EmailAttachment']);
	$this->set('body_ID', $body_ID);
	$this->set('recipients', $this->Email->EmailRecipient->getRecipients($id));

	$this->set('email', $email);
    }



    /**
     * Fetch all the Emails that are from, to, cc a User ID
     *
     * @param <type> $id
     * @return <type>
     */
    function view_user_emails($id) {
	if(!$id) {
	    return;
	}
	else {

	    $user = $this->Email->User->find('first', array('conditions'=>array('User.id'=>$id), 'fields'=>array('User.id','User.access_level'), 'recursive'=>0));

	    if($user['User']['access_level'] == 'manager' || $user['User']['access_level'] == 'admin') {
		if( (!$this->isAdmin()) && (!$this->isManager()) ) {
		    echo "Unable to view Emails for this User. You have insufficient privileges.";
		    return;
		}
	    }

	    //$this->set('emails', $this->paginate());
	    $fromMailIDs = $this->Email->find('list', array('conditions'=>array('Email.user_id'=>$id)));
	    $recMailIDs = $this->Email->EmailRecipient->find('list', array('conditions'=>array('EmailRecipient.user_id'=>$id),
		    'fields'=>array('EmailRecipient.email_id', 'EmailRecipient.email_id')));

	    // print_r($recMailIDs);

//	    print_r($fromMailIDs);
	    $allIDs = $fromMailIDs + $recMailIDs;

	    sort($allIDs);

	    $this->set('allIDs', $allIDs);



	    $this->paginate['Email'] = array(
		    'conditions' => array('Email.id'=>$allIDs),
		    'order'=>array('Email.udate DESC'),
		    'contain' => array('EmailRecipient', 'User'),
		    'limit' => 100,
	    );


	    $userMail = $this->paginate('Email');
	    //$userMail = $this->Email->find('all', array('conditions'=>array('Email.id'=>$allIDs), 'order'=>array('Email.udate DESC')));
	    $this->set('userMail', $userMail);


	}
    }




    /* old shit here, Might be useful */

    function show($id = null) {
	$this->layout = 'minimal';
	$this->set('email', $this->Email->read(null, $id));
    }


    /* Violates DRY. Should probably clean this up at some point */
    function printview($id = null) {
	$this->layout = 'minimal';
	$this->set('email', $this->Email->read(null, $id));

    }




}
?>
