diff --git a/controllers/attachments_controller.php b/controllers/attachments_controller.php new file mode 100644 index 00000000..8717fc04 --- /dev/null +++ b/controllers/attachments_controller.php @@ -0,0 +1,74 @@ +Attachment->recursive = 1; + $this->set('attachments', $this->paginate()); + } + + function view($id = null) { + if (!$id) { + $this->Session->setFlash(__('Invalid Attachment.', true)); + $this->redirect(array('action'=>'index')); + } + $this->set('attachment', $this->Attachment->read(null, $id)); + $this->layout = 'pdf'; + } + + function add() { + if (!empty($this->data)) { + + $attachment = $this->Attachment->process_attachment($this->data); + + + $this->Attachment->create(); + + if ($this->Attachment->save($attachment)) { + $this->Session->setFlash(__('The Attachment has been saved', true)); + $this->redirect(array('action'=>'index')); + } else { + $this->Session->setFlash(__('The Attachment could not be saved. Please, try again.', true)); + } + } + $principles = $this->Attachment->Principle->find('list'); + $this->set(compact('products', 'principles')); + } + + function edit($id = null) { + if (!$id && empty($this->data)) { + $this->Session->setFlash(__('Invalid Attachment', true)); + $this->redirect(array('action'=>'index')); + } + if (!empty($this->data)) { + $attachment = $this->Attachment->process_attachment($this->data); + if ($this->Attachment->save($attachment)) { + $this->Session->setFlash(__('The Attachment has been saved', true)); + $this->redirect(array('action'=>'index')); + } else { + $this->Session->setFlash(__('The Attachment could not be saved. Please, try again.', true)); + } + } + if (empty($this->data)) { + $this->data = $this->Attachment->read(null, $id); + } + $products = $this->Attachment->Product->find('list'); + $principles = $this->Attachment->Principle->find('list'); + $this->set(compact('products','principles')); + } + + function delete($id = null) { + if (!$id) { + $this->Session->setFlash(__('Invalid id for Attachment', true)); + $this->redirect(array('action'=>'index')); + } + if ($this->Attachment->del($id)) { + $this->Session->setFlash(__('Attachment deleted', true)); + $this->redirect(array('action'=>'index')); + } + } + +} +?> diff --git a/controllers/documents_controller.php b/controllers/documents_controller.php index 056170da..e369c5ca 100755 --- a/controllers/documents_controller.php +++ b/controllers/documents_controller.php @@ -39,7 +39,8 @@ class DocumentsController extends AppController { $this->set('currency', $this->Document->getCurrency($document)); $this->set('enquiry', $this->Document->getEnquiry($document)); - + $this->set('attachments', $this->Document->DocumentAttachment->find('all', array('conditions'=>array('document_id'=> $id)))); + $this->set('principles', $this->Document->LineItem->Product->Principle->find('list')); switch($docType) { case 'quote': @@ -65,8 +66,6 @@ class DocumentsController extends AppController { } - - function getProducts($principleID = null) { $this->layout = 'ajax'; @@ -96,6 +95,52 @@ class DocumentsController extends AppController { } + function getAttachments($documentID) { + $this->layout = 'ajax'; + if(!$documentID) { + echo "FAIL"; + } + else { + $this->set('attachments', $this->Document->DocumentAttachment->find('all', array('conditions'=>array('document_id'=> $documentID)))); + } + } + + function getAttachmentsByPrinciple($principleID) { + $this->layout = 'ajax'; + if(!$principleID) { + echo "FAIL"; + } + else { + $attachments = $this->Document->DocumentAttachment->Attachment->find('all', array('conditions'=>array('principle_id'=> $principleID))); + $this->set('attachments', $attachments); + } + } + + function saveAttachments() { + $this->layout = 'ajax'; + if($this->data) { + $attachments = array(); + foreach($this->data['DocumentAttachment']['attachment_id'] as $i => $attachment_id) { + $docID = $this->data['DocumentAttachment']['document_id']; + $query = "DELETE FROM document_attachments WHERE document_id = {$docID} AND attachment_id = {$attachment_id}"; + echo $query."
"; + //$this->Document->DocumentAttachment->query($query); + + $attachment['DocumentAttachment']['document_id'] = $this->data['DocumentAttachment']['document_id']; + $attachment['DocumentAttachment']['attachment_id'] = $attachment_id; + if(!$this->Document->DocumentAttachment->save($attachment)) { + echo "FAIL"; + return; + } + } + echo "SUCCESS"; + } + else { + echo "FAIL"; + } + } + + /** * Create a New Document * @@ -591,13 +636,21 @@ class DocumentsController extends AppController { $document = $this->Document->read(null,$id); + $attachments = $this->Document->DocumentAttachment->find('all', array('DocumentAttachment.document_id' => $id)); + if(empty($document['Document']['pdf_filename'])) { $this->Session->setFlash(__('Error. Please generate the PDF before attempting to email it', true)); return; } else { $pdf_dir = Configure::read('pdf_directory'); - $this->Email->attachments = array($pdf_dir.'/'.$document['Document']['pdf_filename']); + + $attachment_files = array($pdf_dir.$document['Document']['pdf_filename']); + foreach($attachments as $attachment) { + $attachment_files[] = $attachment['Attachment']['filename']; + } + $this->Email->attachments = $attachment_files; + } $enquiry = $this->Document->getEnquiry($document); @@ -605,7 +658,7 @@ class DocumentsController extends AppController { $this->Email->to = $enquiry['Contact']['email']; //Uncomment this when going live - // $this->Email->cc = array($enquiry['User']['email']); + $this->Email->cc = array($enquiry['User']['email']); // $this->Email->bcc = array('carpis@cmctechnologies.com.au'); $this->Email->subject = $enquiry['Enquiry']['title'].' '; @@ -618,7 +671,7 @@ class DocumentsController extends AppController { $this->set('DocFullName', $this->Document->getDocFullName($document['Document']['type'])); if($this->Email->send()) { $this->Session->setFlash(__('The Email has been sent', true)); - // $this->redirect(array('action'=>'view/'.$id), null, false); + $this->redirect(array('action'=>'view/'.$id), null, false); } else { $this->set('smtp_errors', $this->Email->smtpError); diff --git a/models/attachment.php b/models/attachment.php new file mode 100644 index 00000000..80b54f5a --- /dev/null +++ b/models/attachment.php @@ -0,0 +1,77 @@ + array('numeric'), + 'name' => array('notempty'), + 'type' => array('notempty'), + 'size' => array('numeric'), + 'archived' => array('numeric'), + ); + + //The Associations below have been created with all possible keys, those that are not needed can be removed + var $belongsTo = array( + 'Principle' => array( + 'className' => 'Principle', + 'foreignKey' => 'principle_id', + 'conditions' => '', + 'fields' => '', + 'order' => '' + ) + ); + + var $hasMany = array( + 'DocumentAttachment' => array( + 'className' => 'DocumentAttachment', + 'foreignKey' => 'attachment_id' + ) + ); + + +// Based on comment 8 from: http://bakery.cakephp.org/articles/view/improved-advance-validation-with-parameters + + function isUploadedFile($params){ + $val = array_shift($params); + if ((isset($val['error']) && $val['error'] == 0) || + (!empty( $val['tmp_name']) && $val['tmp_name'] != 'none')) { + return is_uploaded_file($val['tmp_name']); + } + return false; + } + + + /** + * Passed a PHP upload + * array('name', 'type', 'tmp_name', 'error' 'size'). + * + * Move the uploaded file to the storage directory + * and return a cake array for insertion. + * + */ + + function process_attachment($attachment) { + + $upload = $attachment['Attachment']['file']; + + $new_filename = Configure::read('attachments_directory').time().$upload['name']; + + $moved = move_uploaded_file($upload['tmp_name'], $new_filename); + + if($moved) { + $attachment['Attachment']['filename'] = $upload['name']; + $attachment['Attachment']['file'] = $new_filename; + $attachment['Attachment']['size'] = $upload['size']; + $attachment['Attachment']['type'] = $upload['type']; + return $attachment; + } + else { + die("Error! Unable to move the uploaded file"); + } + + + + } + +} +?> diff --git a/models/document.php b/models/document.php index 81b31c16..d56cc53d 100755 --- a/models/document.php +++ b/models/document.php @@ -28,7 +28,14 @@ class Document extends AppModel { 'className'=> 'DocPage', 'foreignKey' => 'document_id', 'order' => 'DocPage.page_number ASC' - ) + ), + + 'DocumentAttachment' => array( + 'className'=> 'DocumentAttachment', + 'foreignKey' => 'document_id', + 'order' => 'DocumentAttachment.id ASC' + ), + ); @@ -53,7 +60,6 @@ class Document extends AppModel { ) ); - //So much refactoring possible on this. function getCurrency($document) { @@ -170,4 +176,4 @@ class Document extends AppModel { } -?> \ No newline at end of file +?> diff --git a/models/document_attachment.php b/models/document_attachment.php new file mode 100644 index 00000000..2009bcfb --- /dev/null +++ b/models/document_attachment.php @@ -0,0 +1,28 @@ + array( + 'className' => 'Document', + 'foreignKey' => 'document_id', + 'conditions' => '', + 'fields' => '', + 'order' => '' + ), + 'Attachment' => array( + 'className' => 'Attachment', + 'foreignKey' => 'attachment_id', + 'conditions' => '', + 'fields' => '', + 'order' => '' + ) + ); + +} +?> diff --git a/views/attachments/add.ctp b/views/attachments/add.ctp new file mode 100644 index 00000000..b1cfb51d --- /dev/null +++ b/views/attachments/add.ctp @@ -0,0 +1,14 @@ +
+create('Attachment', array('type'=>'file'));?> +
+ + input('principle_id'); + echo $form->input('name'); + echo $form->file('file'); + echo $form->input('description'); + echo $form->input('archived'); + ?> +
+end('Submit');?> +
diff --git a/views/attachments/edit.ctp b/views/attachments/edit.ctp new file mode 100644 index 00000000..3cd750ef --- /dev/null +++ b/views/attachments/edit.ctp @@ -0,0 +1,23 @@ +
+create('Attachment');?> +
+ + input('id'); + echo $form->input('principle_id'); + echo $form->input('name'); + echo $form->input('filename'); + echo $form->input('type'); + echo $form->input('size'); + echo $form->input('description'); + echo $form->input('archived'); + ?> +
+end('Submit');?> +
+
+ +
diff --git a/views/attachments/index.ctp b/views/attachments/index.ctp new file mode 100644 index 00000000..af2adaa2 --- /dev/null +++ b/views/attachments/index.ctp @@ -0,0 +1,75 @@ +
+

+

+counter(array( +'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true) +)); +?>

+ + + + + + + + + + + + + > + + + + + + + + + + +
sort('id');?>sort('principle_id');?>sort('created');?>sort('name');?>sort('type');?>sort('size');?>sort('description');?>
+ + + link($attachment['Principle']['short_name'], '/principles/view/'.$attachment['Principle']['id']); + } + else { + echo $html->link($attachment['Principle']['name'], '/principles/view/'.$attachment['Principle']['id']); + } + ?> + + nice($attachment['Attachment']['created']); ?> + + + + + + toReadAbleSize($attachment['Attachment']['size']); ?> + + + + link(__('View', true), array('action' => 'view', $attachment['Attachment']['id'])); ?> + link(__('Edit', true), array('action' => 'edit', $attachment['Attachment']['id'])); ?> + link(__('Delete', true), array('action' => 'delete', $attachment['Attachment']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $attachment['Attachment']['id'])); ?> +
+
+
+ prev('<< '.__('previous', true), array(), null, array('class'=>'disabled'));?> + | numbers();?> + next(__('next', true).' >>', array(), null, array('class' => 'disabled'));?> +
+
+ +
diff --git a/views/attachments/view.ctp b/views/attachments/view.ctp new file mode 100644 index 00000000..4f27f363 --- /dev/null +++ b/views/attachments/view.ctp @@ -0,0 +1,17 @@ + + diff --git a/views/documents/get_attachments.ctp b/views/documents/get_attachments.ctp new file mode 100644 index 00000000..269cadab --- /dev/null +++ b/views/documents/get_attachments.ctp @@ -0,0 +1,10 @@ +"; + echo $html->link($attachment['Attachment']['name'], + "/attachments/view/{$attachment['Attachment']['id']}"); + echo ""; +} + +?> + diff --git a/views/documents/get_attachments_by_principle.ctp b/views/documents/get_attachments_by_principle.ctp new file mode 100644 index 00000000..6f805a2b --- /dev/null +++ b/views/documents/get_attachments_by_principle.ctp @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + +
Add + FilenameNameDesc
+ + + link($attachment['Attachment']['filename'], '/attachments/view/'.$attachment['Attachment']['id']); ?> +
diff --git a/views/documents/save_attachments.ctp b/views/documents/save_attachments.ctp new file mode 100644 index 00000000..e69de29b diff --git a/views/documents/view.ctp b/views/documents/view.ctp index 9dd3bf54..28bf3798 100755 --- a/views/documents/view.ctp +++ b/views/documents/view.ctp @@ -3,6 +3,7 @@ echo $javascript->link('ckeditor/ckeditor'); echo $javascript->link('ckeditor/adapters/jquery'); echo $javascript->link('document_add_edit'); +echo $javascript->link('document_attachments'); ?> element('pdf_created_message'); ?> @@ -18,9 +19,9 @@ echo $this->element($docTypeElement);
- input("DocPage.content", array('class'=>'page', 'label'=>'Page', 'between'=>'')); - ?> +input("DocPage.content", array('class'=>'page', 'label'=>'Page', 'between'=>'')); +?>
@@ -30,12 +31,40 @@ echo $this->element($docTypeElement);
-
+
+

Attachments

+ + + + + + + + + + + + + + + + +
FilenameNameDesc
+ link($attachment['Attachment']['filename'], '/attachments/view/'.$attachment['Attachment']['id']); ?> +
+
+ element('add_attachment'); ?> +
+ + + +
-
@@ -51,3 +80,4 @@ echo $this->element($docTypeElement); + diff --git a/views/elements/add_attachment.ctp b/views/elements/add_attachment.ctp new file mode 100644 index 00000000..19196ebf --- /dev/null +++ b/views/elements/add_attachment.ctp @@ -0,0 +1,14 @@ + +create('DocumentAttachment', array('default'=>false)); ?> +input('DocumentAttachment.document_id', array('type'=>'hidden', 'value'=> $document['Document']['id'])); ?> +input('DocumentAttachment.principle_id', array('id'=>'attachmentPrincipleSelect', 'label'=>'Principle','empty'=>'Select Principle'));?> + +
+ +
+ +end(); +?> + + diff --git a/views/elements/add_edit_line_item.ctp b/views/elements/add_edit_line_item.ctp index 95b2e0f8..02e41515 100755 --- a/views/elements/add_edit_line_item.ctp +++ b/views/elements/add_edit_line_item.ctp @@ -1,4 +1,4 @@ -input('Product.principle_id', array('id'=>'principleSelect', 'label'=>'Principle','empty'=>'Select Principle'));?> +input('Product.principle_id', array('id'=>'principleAttachmentSelect', 'label'=>'Principle','empty'=>'Select Principle'));?>
diff --git a/views/layouts/default.ctp b/views/layouts/default.ctp index 462e008d..8da62452 100755 --- a/views/layouts/default.ctp +++ b/views/layouts/default.ctp @@ -130,6 +130,7 @@ if ($currentuser['User']['access_level'] == 'manager' || $currentuser['User']['a
  • link('Principles', '/principles/index'); ?>
    • link('Principle Index', '/principles/index'); ?>
    • +
    • link('Attachments', '/attachments/index'); ?>
    • link('Add Principle', '/principles/add'); ?>
  • diff --git a/webroot/css/quotenik.css b/webroot/css/quotenik.css index 259cd014..6416cfa1 100755 --- a/webroot/css/quotenik.css +++ b/webroot/css/quotenik.css @@ -1650,3 +1650,15 @@ tr.no_items { font-size: 2em; padding: 0.5em; } +#Attachments { + margin-top: 1.5em; +} +h2.document-attachments { + +} + + +#attachments-list { + margin-top: 0.5em; + list-style: none; +} diff --git a/webroot/js/document_attachments.js b/webroot/js/document_attachments.js new file mode 100644 index 00000000..ac567008 --- /dev/null +++ b/webroot/js/document_attachments.js @@ -0,0 +1,64 @@ +$(function() { + var documentID = $("#documentID").html(); + + + $("#addAttachment").click(function() { + $("#addAttachmentModal").dialog('open'); + }); + + + $("#addAttachmentModal").dialog({ + autoOpen: false, + height: 900, + width: 600, + modal: true, + buttons: { + "Add Attachment": function() { + + var attachmentInputs = $('#DocumentAttachmentAddForm').find('input'); + + $.post('/documents/saveAttachments', attachmentInputs, function(data) { + if(data == 'SUCCESS') { + loadAttachments(); + $(this).dialog('close'); + } + else { + alert("Attachments could not be saved") + } + + }); + + }, + Cancel: function() { + $( this ).dialog( "close" ); + } + }, + close: function() { + loadAttachments(); + } + + + }); + + $("#attachmentPrincipleSelect").change(function() { + var principleID = $(this).val(); + loadPrincipleAttachments(principleID); + }); + + function loadPrincipleAttachments(id) { + $.get('/documents/getAttachmentsByPrinciple/'+id, function(attachments) { + $('#principleAttachments').html(attachments); + }); + } + + function loadAttachments() { + + $.get('/documents/getAttachments/'+documentID, function(attachments) { + + + }); + + + } + +});