2009-02-09 14:25:49 -08:00
|
|
|
<?php
|
|
|
|
|
class EmailsController extends AppController {
|
|
|
|
|
|
2011-03-09 23:18:26 -08:00
|
|
|
var $name = 'Emails';
|
|
|
|
|
var $helpers = array('Html', 'Form', 'Number', 'Text', 'Link');
|
|
|
|
|
var $components = array('RequestHandler');
|
2009-02-09 14:25:49 -08:00
|
|
|
|
2011-03-09 23:18:26 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
var $paginate = array(
|
2011-03-20 21:31:00 -07:00
|
|
|
'fields'=> array('Email.id', 'Email.user_id', 'Email.subject','Email.udate','User.id','User.first_name','User.last_name','User.email'),
|
2011-03-20 22:08:07 -07:00
|
|
|
'contain' => array('User', 'EmailRecipient'),
|
2011-03-09 23:18:26 -08:00
|
|
|
'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'));
|
2009-02-09 14:25:49 -08:00
|
|
|
}
|
|
|
|
|
|
2011-03-09 23:18:26 -08:00
|
|
|
$email = $this->Email->read(null, $id);
|
|
|
|
|
$this->set('email', $email);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->set('recipients', $this->Email->EmailRecipient->getRecipients($id));
|
|
|
|
|
|
|
|
|
|
/*$recQuery = array("OR" => array("User.id"=> array()));
|
|
|
|
|
foreach($email['EmailRecipient'] as $rec) {
|
|
|
|
|
$recQuery["OR"]["User.id"][] = $rec['id'];
|
2009-02-09 14:25:49 -08:00
|
|
|
}
|
|
|
|
|
|
2011-03-09 23:18:26 -08:00
|
|
|
$users = $this->Email->User->find('all', array('conditions'=>$recQuery));
|
|
|
|
|
$this->set('users',$users);/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
2009-02-09 14:25:49 -08:00
|
|
|
|
2011-03-09 23:18:26 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//print_r($users);
|
|
|
|
|
//print_r($recQuery);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$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'];
|
|
|
|
|
}
|
2009-08-13 19:09:47 -07:00
|
|
|
}
|
2011-03-21 17:02:46 -07:00
|
|
|
|
|
|
|
|
//Havent found one yet. Just return the first email attachment ID.
|
|
|
|
|
foreach($email_attachments as $attachment) {
|
|
|
|
|
return $attachment['id'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-03-20 23:12:26 -07:00
|
|
|
//Should do something else here to take a stab at it at least.
|
2011-03-09 23:18:26 -08:00
|
|
|
return false;
|
2009-08-13 19:09:47 -07:00
|
|
|
|
2011-03-09 23:18:26 -08:00
|
|
|
}
|
2009-02-09 21:39:26 -08:00
|
|
|
|
2011-03-09 23:18:26 -08:00
|
|
|
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);
|
|
|
|
|
}
|
2011-03-16 18:48:27 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch all the Emails that are from, to, cc a User ID
|
|
|
|
|
*
|
|
|
|
|
* @param <type> $id
|
2011-03-20 23:58:21 -07:00
|
|
|
* @return <type>
|
2011-03-16 18:48:27 -07:00
|
|
|
*/
|
|
|
|
|
function view_user_emails($id) {
|
|
|
|
|
if(!$id) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2011-03-20 23:58:21 -07:00
|
|
|
|
|
|
|
|
$user = $this->Email->User->find('first', array('conditions'=>array('User.id'=>$id), 'fields'=>array('User.id','User.access_level'), 'recursive'=>0));
|
2011-03-21 17:02:46 -07:00
|
|
|
|
2011-03-20 23:58:21 -07:00
|
|
|
if($user['User']['access_level'] == 'manager' || $user['User']['access_level'] == 'admin') {
|
|
|
|
|
if( (!$this->isAdmin()) && (!$this->isManager()) ) {
|
2011-03-21 17:02:46 -07:00
|
|
|
echo "Unable to view Emails for this User. You have insufficient privileges.";
|
|
|
|
|
return;
|
2011-03-20 23:58:21 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-20 19:26:07 -07:00
|
|
|
//$this->set('emails', $this->paginate());
|
|
|
|
|
$fromMailIDs = $this->Email->find('list', array('conditions'=>array('Email.user_id'=>$id)));
|
2011-03-20 23:12:26 -07:00
|
|
|
$recMailIDs = $this->Email->EmailRecipient->find('list', array('conditions'=>array('EmailRecipient.user_id'=>$id),
|
2011-03-20 23:58:21 -07:00
|
|
|
'fields'=>array('EmailRecipient.email_id', 'EmailRecipient.email_id')));
|
2011-03-16 18:48:27 -07:00
|
|
|
|
2011-03-20 23:58:21 -07:00
|
|
|
// print_r($recMailIDs);
|
2011-03-20 23:12:26 -07:00
|
|
|
|
|
|
|
|
// print_r($fromMailIDs);
|
2011-03-20 19:26:07 -07:00
|
|
|
$allIDs = $fromMailIDs + $recMailIDs;
|
|
|
|
|
|
|
|
|
|
sort($allIDs);
|
|
|
|
|
|
|
|
|
|
$this->set('allIDs', $allIDs);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-20 22:08:07 -07:00
|
|
|
$this->paginate['Email'] = array(
|
2011-03-20 23:58:21 -07:00
|
|
|
'conditions' => array('Email.id'=>$allIDs),
|
|
|
|
|
'order'=>array('Email.udate DESC'),
|
|
|
|
|
'contain' => array('EmailRecipient', 'User'),
|
|
|
|
|
'limit' => 100,
|
2011-03-20 19:26:07 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$userMail = $this->paginate('Email');
|
|
|
|
|
//$userMail = $this->Email->find('all', array('conditions'=>array('Email.id'=>$allIDs), 'order'=>array('Email.udate DESC')));
|
|
|
|
|
$this->set('userMail', $userMail);
|
2011-03-20 23:58:21 -07:00
|
|
|
|
2011-03-20 21:31:00 -07:00
|
|
|
|
2011-03-16 18:48:27 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-09 23:18:26 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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));
|
|
|
|
|
|
|
|
|
|
}
|
2009-02-09 21:39:26 -08:00
|
|
|
|
|
|
|
|
|
2011-03-20 23:58:21 -07:00
|
|
|
|
2009-02-09 14:25:49 -08:00
|
|
|
|
|
|
|
|
}
|
2009-02-09 21:39:26 -08:00
|
|
|
?>
|