Added Ajax Add and edit for Contact users. Will extend to regular users and principle contacts shortly

This commit is contained in:
Karl Cordes 2011-03-11 12:22:57 +11:00
parent f5b2ed3c67
commit d5cb432a6d
7 changed files with 271 additions and 181 deletions

View file

@ -115,6 +115,41 @@ class UsersController extends AppController {
}
function add_edit() {
Configure::write('debug', 0);
if(isset($this->params['named']['type'])) {
$this->set('type', $this->params['named']['type']);
}
if(isset($this->params['named']['principle_id'])) {
$this->set('principle_id', $this->params['named']['principle_id']);
}
if(isset($this->params['named']['customer_id'])) {
$this->set('customer_id', $this->params['named']['customer_id']);
}
if(isset($this->params['named']['user_id'])) {
$userID = $this->params['named']['user_id'];
$this->set('user_id', $userID);
}
if(isset($this->params['named']['action'])) {
$action = $this->params['named']['action'];
$this->set('action', $action);
}
if(isset($action) && isset($userID)) {
$this->data = $this->User->read(null, $userID);
}
}

View file

@ -12,7 +12,7 @@ class VaultTwoShell extends Shell {
var $uses = array('Enquiry', 'Contact', 'Invoice', 'PurchaseOrder', 'User', 'Email', 'EmailRecipient', 'Job');
// var $email_dir = '/var/www/cakephp/app/emails';
// var $email_dir = '/var/www/cakephp/app/emails';
function main() {
@ -214,7 +214,7 @@ class VaultTwoShell extends Shell {
}
else {
echo "Fatal Error: Unable to create user for email '{$recEmailLC}'\n";
die();
continue;
}
}
@ -253,125 +253,129 @@ class VaultTwoShell extends Shell {
$newEmail['Email']['user_id'] = $recipientsIDs['from'][0]; //We should never have more than one 'from' user ID.
}
else {
die("Email has no from Recipient!! Critical Failure"); //Change this to logging for deployment.
print_r($recipientsIDs);
echo "Email has no From Recipient ID. Ignoring this.\n";
continue;
}
//die("Email has no from Recipient!! Critical Failure"); //Change this to logging for deployment.
// $newEmail['Email']['date'] = $this_header->date;
$newEmail['Email']['udate'] = $this_header->udate;
$newEmail['Email']['subject'] = $subjDecoded[0]->text; //Hacky. Will probably fix this quickly.
$newEmail['Email']['udate'] = $this_header->udate;
$newEmail['Email']['subject'] = $subjDecoded[0]->text; //Hacky. Will probably fix this quickly.
$recipientCount = 0;
foreach($recipientsIDs as $type => $typeEmails) {
foreach($typeEmails as $userID) {
$recipientCount = 0;
foreach($recipientsIDs as $type => $typeEmails) {
foreach($typeEmails as $userID) {
$newEmail['EmailRecipient'][$recipientCount]['user_id'] = $userID;
switch($type) {
case 'to':
$newEmail['EmailRecipient'][$recipientCount]['type'] = 'to';
$recipientCount++;
break;
$newEmail['EmailRecipient'][$recipientCount]['user_id'] = $userID;
switch($type) {
case 'to':
$newEmail['EmailRecipient'][$recipientCount]['type'] = 'to';
$recipientCount++;
break;
case 'cc':
$newEmail['EmailRecipient'][$recipientCount]['type'] = 'cc';
$recipientCount++;
break;
default:
}
case 'cc':
$newEmail['EmailRecipient'][$recipientCount]['type'] = 'cc';
$recipientCount++;
break;
default:
}
}
}
/** End Email creation **/
/** End Email creation **/
/** Start Attachments **/
/** Start Attachments **/
$relative_path = $this->getAttachmentDirectory($email_dir, $unix_time);
$attachments = $this->fetchBodyAttachments($mbox, $i, $email_dir, $relative_path, $ripmime_path);
$relative_path = $this->getAttachmentDirectory($email_dir, $unix_time);
$attachments = $this->fetchBodyAttachments($mbox, $i, $email_dir, $relative_path, $ripmime_path);
$attachmentCount = 0;
$attachmentCount = 0;
$biggestHTML = 0;
$biggestHTML = 0;
foreach($attachments as $attachment) {
if(empty($attachment['type'])) {
continue;
}
$newEmail['EmailAttachment'][$attachmentCount]['name'] = $attachment['name'];
$newEmail['EmailAttachment'][$attachmentCount]['type'] = $attachment['type'];
$newEmail['EmailAttachment'][$attachmentCount]['size'] = $attachment['size'];
$newEmail['EmailAttachment'][$attachmentCount]['filename'] = $attachment['filename'];
$newEmail['EmailAttachment'][$attachmentCount]['is_message_body'] = $attachment['is_message_body'];
$attachmentCount++;
foreach($attachments as $attachment) {
if(empty($attachment['type'])) {
continue;
}
/** End attachments */
$newEmail['EmailAttachment'][$attachmentCount]['name'] = $attachment['name'];
$newEmail['EmailAttachment'][$attachmentCount]['type'] = $attachment['type'];
$newEmail['EmailAttachment'][$attachmentCount]['size'] = $attachment['size'];
$newEmail['EmailAttachment'][$attachmentCount]['filename'] = $attachment['filename'];
$newEmail['EmailAttachment'][$attachmentCount]['is_message_body'] = $attachment['is_message_body'];
$attachmentCount++;
}
/** End attachments */
/* If we found Identifiers for this Email. Create the HABTM associations.
/* If we found Identifiers for this Email. Create the HABTM associations.
*
*/
if(count($found_jobs) > 0) {
*/
if(count($found_jobs) > 0) {
// echo "Found Job IDs: \n";
// print_r($found_jobs);
foreach($found_jobs as $jobID) {
$newEmail['Job'][] = array('job_id'=> $jobID);
}
// print_r($found_jobs);
foreach($found_jobs as $jobID) {
$newEmail['Job'][] = array('job_id'=> $jobID);
}
}
if(count($found_pos) > 0) {
if(count($found_pos) > 0) {
// echo "Found PO IDs: \n";
// print_r($found_pos);
foreach($found_pos as $poID) {
$newEmail['PurchaseOrder'][] = array('purchase_order_id'=> $poID);
}
foreach($found_pos as $poID) {
$newEmail['PurchaseOrder'][] = array('purchase_order_id'=> $poID);
}
}
if(count($found_enquiries) > 0) {
if(count($found_enquiries) > 0) {
// echo "Found enquiry IDs: \n";
// print_r($found_enquiries);
foreach($found_enquiries as $enqID) {
$newEmail['Enquiry'][] = array('enquiry_id'=> $enqID);
}
foreach($found_enquiries as $enqID) {
$newEmail['Enquiry'][] = array('enquiry_id'=> $enqID);
}
}
if(count($found_invoices) > 0) {
if(count($found_invoices) > 0) {
// echo "Found Invoice IDs: \n";
// print_r($found_invoices);
foreach($found_invoices as $invID) {
$newEmail['Invoice'][] = array('invoice_id'=> $invID);
}
foreach($found_invoices as $invID) {
$newEmail['Invoice'][] = array('invoice_id'=> $invID);
}
}
print_r($newEmail);
print_r($newEmail);
$this->Email->create();
$emailID = $this->Email->saveAll($newEmail);
if(!empty($emailID)) {
echo "Success. We made an email. Storing Email UID $this_uid\n";
}
else {
echo "Error! Didn't save the email\n";
}
$store[] = $this_uid;
echo "--------END MESSAGE ------\n";
$this->Email->create();
$emailID = $this->Email->saveAll($newEmail);
if(!empty($emailID)) {
echo "Success. We made an email. Storing Email UID $this_uid\n";
}
else {
$discard[] = $this_uid;
echo "email {$i} will not be saved. Subject: ".$subjDecoded[0]->text."\n";
echo "Error! Didn't save the email\n";
}
// print_r($recipients);
// echo "----------------------\n";
$store[] = $this_uid;
echo "--------END MESSAGE ------\n";
}
else {
$discard[] = $this_uid;
echo "email {$i} will not be saved. Subject: ".$subjDecoded[0]->text."\n";
}
// print_r($recipients);
// echo "----------------------\n";
}
@ -403,7 +407,7 @@ class VaultTwoShell extends Shell {
imap_close($mbox);
}
/**
* Make a 'HashMap' (array) in the format.
@ -596,7 +600,7 @@ class VaultTwoShell extends Shell {
print_r($type);
echo count($type)."\n";
*/
*/
if(count($type) != 2) {
//echo "Didnt find a proper type. Skipping it.";
continue;
@ -606,7 +610,7 @@ class VaultTwoShell extends Shell {
//echo "Didnt find a proper name. Skipping it";
continue;
}
$oldNamePath = $email_dir.'/'.$relative_path.'/'.$name[1];
$newNamePath = $email_dir.'/'.$relative_path.'/'.$uuid.'-'.$name[1];
$newName = $relative_path.'/'.$uuid.'-'.$name[1];
@ -618,7 +622,7 @@ class VaultTwoShell extends Shell {
$attachments[$j]['name'] = $newName;
$attachments[$j]['filename'] = $name[1];
$attachments[$j]['size'] = $size;
$attachments[$j]['is_message_body'] = 0;
$attachments[$j]['is_message_body'] = 0;
echo "in message number $msg_number: found attachment ".$attachments[$j]['name'].' '.$attachments[$j]['type']."\n";
@ -693,22 +697,22 @@ class VaultTwoShell extends Shell {
*
* @param <type> $attachments
function moveAttachments($attachments, $email_dir, $unix_time) {
function moveAttachments($attachments, $email_dir, $unix_time) {
$month_year = date('m-Y');
$month_year = date('m-Y');
$uuid = String::uuid();
$uuid = String::uuid();
foreach($attachments as $attachment) {
echo $email_dir.'/'.$attachment['name']."\n";
}
die();
foreach($attachments as $attachment) {
echo $email_dir.'/'.$attachment['name']."\n";
}
die();
}*/
}*/
}

View file

@ -91,7 +91,8 @@
<div class="related">
<h3><?php __('Contacts');?><?php echo $html->image('system-users.png'); ?></h3>
<button id="addUser">Add Contact</button>
<button id="addContactUser" class="contact">Add Contact</button>
<div class="customer_id" id="<?=$customer['Customer']['id']?>" style="display: none;"></div>
<?php if (!empty($customer['Contact'])):?>
<table cellpadding = "0" cellspacing = "0">
<tr>
@ -121,9 +122,11 @@
<td><?php echo $contact['Contact']['fax'];?></td>
<td><?php echo $contact['Contact']['notes'];?></td>
<td class="actions">
<?php echo $html->link(__('View', true), array('controller'=> 'users', 'action'=>'view', $contact['Contact']['id'])); ?>
<?php echo $html->link(__('Edit', true), array('controller'=> 'users', 'action'=>'edit', $contact['Contact']['id'])); ?>
<?php echo $html->link(__('Delete', true), array('controller'=> 'users', 'action'=>'delete', $contact['Contact']['id'])); ?>
<button id="<?=$contact['Contact']['id']?>" class="ViewEditButton">View/Edit</button>
<?php //echo $html->link(__('View', true), array('controller'=> 'users', 'action'=>'view', $contact['Contact']['id'])); ?>
<?php //echo $html->link(__('Edit', true), array('controller'=> 'users', 'action'=>'edit', $contact['Contact']['id'])); ?>
<?php //echo $html->link(__('Delete', true), array('controller'=> 'users', 'action'=>'delete', $contact['Contact']['id'])); ?>
</td>
</tr>
@ -134,27 +137,9 @@
</div>
<? echo $javascript->link('add_user'); ?>
<? echo $javascript->link('add_edit_user'); ?>
<div id="addUserDiv">
<?php echo $form->create('User');?>
<?php
echo $form->input('first_name');
echo $form->input('last_name');
echo $form->input('email');
echo $form->input('job_title');
echo $form->input('phone');
echo $form->input('mobile');
echo $form->input('fax');
echo $form->input('phone_extension');
echo $form->input('direct_phone');
echo $form->input('notes');
echo $form->input('type', array('type'=>'hidden', 'value'=>'contact'));
echo $form->input('customer_id', array('type'=>'hidden', 'value'=>$customer['Customer']['id']));
echo $form->submit('Add Contact', array('id'=>'submitUserButton'));
?>
</div>

61
views/users/add_edit.ctp Executable file
View file

@ -0,0 +1,61 @@
<?=$javascript->link('add_edit_user_ajax');?>
<div class="users form">
<?php echo $form->create('User',array('action'=>'add_user'));?>
<?php
echo "TYPE IS :".$type."<br>";
echo "customer_id".$customer_id."<br>";
echo $form->input('first_name');
echo $form->input('last_name');
echo $form->input('email');
echo $form->input('job_title');
echo $form->input('phone');
echo $form->input('mobile');
echo $form->input('fax');
echo $form->input('phone_extension');
echo $form->input('direct_phone');
echo $form->input('notes');
switch($type) {
case 'contact':
echo $form->input('customer_id', array('type'=>'hidden', 'value'=>$customer_id));
echo $form->input('type', array('type'=>'hidden', 'value'=>'contact'));
$buttonStr = 'Contact';
break;
case 'principle':
echo $form->input('principle_id', array('type'=>'hidden', 'value'=>$principle_id));
$buttonStr = 'Principle Contact';
case 'user':
$buttonStr = 'User';
break;
default:
break;
}
switch($action) {
case 'add':
$actionStr = 'Add';
break;
case 'edit':
$actionStr ='Edit';
echo $form->input('id', array('type'=>'hidden', 'value'=>$user_id));
break;
}
echo $form->submit($actionStr.' '.$buttonStr, array('id'=>'submitUserButton'));
?>
</div>

View file

@ -1,25 +0,0 @@
<div class="users form">
<?php echo $form->create('User');?>
<fieldset>
<legend><?php __('Edit User');?></legend>
<?php
echo $form->input('id');
echo $form->input('username');
echo $form->input('password');
echo $form->input('first_name');
echo $form->input('last_name');
echo $form->input('email');
echo $form->input('job_title');
echo $form->input('group_id');
?>
</fieldset>
<?php echo $form->end('Submit');?>
</div>
<div class="actions">
<ul>
<li><?php echo $html->link(__('Delete', true), array('action'=>'delete', $form->value('User.id')), null, sprintf(__('Are you sure you want to delete # %s?', true), $form->value('User.id'))); ?></li>
<li><?php echo $html->link(__('List Users', true), array('action'=>'index'));?></li>
<li><?php echo $html->link(__('List Enquiries', true), array('controller'=> 'enquiries', 'action'=>'index')); ?> </li>
<li><?php echo $html->link(__('New Enquiry', true), array('controller'=> 'enquiries', 'action'=>'add')); ?> </li>
</ul>
</div>

View file

@ -0,0 +1,63 @@
/**
* JS for adding a Contact to a company.
*
* 1. open a new model window on the add form.
*
* 2. When the submit button is clicked, check it for errors
*
* 3. If it passes the first validation, submit it to PHP controller via AJAX.
*
* 4a. If PHP reports sucessful save
* i. close the Modal Window
* ii. add the new contacts user details to the contact table.
*
* 4b. If PHP reports validation error. Display error. Set error class to the fields that failed.
*
*/
$(function() {
//Style the Button
$( "#addUser" ).button({
text: true,
icons: {
primary: "ui-icon-plus"
}
});
//The diaglog box for Adding a contact.
$("#addUserDiv").dialog({
autoOpen: false,
width: 450,
modal: true,
title: 'Add a Contact'
});
$("#addContactUser").click(function() { //Meh, we'll just repeat this for the 3 user types. Easiest way to handle this.'
var userType = 'contact';
var customer_id = $('.customer_id').attr('id');
var thisAction = 'add'
$("#addUserDiv").load('/users/add_edit/action:'+thisAction+'/type:'+userType+'/customer_id:'+customer_id);
$("#addUserDiv").dialog('open');
return false;
});
//Editing a Customer Contact
$('.ViewEditButton').click(function() {
var userType ='contact';
var thisAction = 'edit';
var user_id = $(this).attr('id');
$("#addUserDiv").load('/users/add_edit/action:'+thisAction+'/type:'+userType+'/user_id:'+user_id);
$("#addUserDiv").dialog('open');
});
});

View file

@ -1,49 +1,15 @@
/**
* JS for adding a Contact to a company.
*
* 1. open a new model window on the add form.
*
* 2. When the submit button is clicked, check it for errors
*
* 3. If it passes the first validation, submit it to PHP controller via AJAX.
*
* 4a. If PHP reports sucessful save
* i. close the Modal Window
* ii. add the new contacts user details to the contact table.
*
* 4b. If PHP reports validation error. Display error. Set error class to the fields that failed.
*
*/
$(function() {
//Style the Button
$( "#addUser" ).button({
text: true,
icons: {
primary: "ui-icon-plus"
}
})
//The diaglog box for Adding a contact.
$("#addUserDiv").dialog({
autoOpen: false,
width: 450,
modal: true,
title: 'Add a Contact'
});
$("#addUser").click(function() {
$("#addUserDiv").dialog('open');
return false;
});
/**
* Javascript to do the actual validation for Adding/Editing users in the modal window.
*/
var _loadingDiv = $("#ajaxLoading");
$('#UserAddForm, #UserEditForm').submit(function(){
$('#submitUserButton').button();
$('#UserAddUserForm').submit(function(){
_loadingDiv.show();
$.post('/users/add_user',
$(this).serializeArray(),
@ -70,6 +36,8 @@ $(function() {
function onSuccess(data) {
flashMessage(data.message);
_loadingDiv.hide();
$('#submitUserButton').button('disable');
window.setTimeout(function() {
// window.location.href = '/posts';
window.location.reload(true);
@ -91,7 +59,7 @@ $(function() {
function flashMessage(message) {
var _insert = $(document.createElement('div')).css('display', 'none');
_insert.attr('id', 'flashMessage').addClass('message').text(message);
_insert.insertAfter($("#UserAddForm")).fadeIn();
_insert.insertAfter($("#UserAddUserForm")).fadeIn();
}
function camelize(string) {
@ -105,5 +73,4 @@ $(function() {
}
});
});