From 59090dc452c4381380c4c116372ca76212548431 Mon Sep 17 00:00:00 2001 From: Karl Cordes Date: Thu, 4 Apr 2013 21:54:28 +1100 Subject: [PATCH] Simplified attachments --- app/config/core.php | 22 ++----- app/controllers/attachments_controller.php | 33 ++++++----- app/models/attachment.php | 68 ++++++++++------------ 3 files changed, 55 insertions(+), 68 deletions(-) diff --git a/app/config/core.php b/app/config/core.php index 0bac275d..d75df79e 100644 --- a/app/config/core.php +++ b/app/config/core.php @@ -52,8 +52,6 @@ if($host == 'cmc.lan') { $basedir = '/var/www/CMC-Sales/app'; Configure::write('email_directory', '/var/www/cakephp/app/emails'); - Configure::write('pdf_directory', $basedir.'/webroot/pdf/'); - Configure::write('attachments_directory', $basedir.'/attachments_files/'); Configure::write('smtp_settings', array( 'port' => '25', @@ -76,22 +74,6 @@ if($host == 'cmc.lan') { else { //Staging config $basedir = '/home/karlcordes/CMC-Sales/app/'; - - Configure::write('smtp_settings', array( - 'port' => '1025', - 'timeout' => '30', - 'host' => 'localhost')); - - - - - /** - * - * Cache Engine Configuration - * Default settings provided below - * - * File storage engine. - */ Cache::config('default', array( 'engine' => 'File', //[required] 'duration'=> 3600, //[optional] @@ -105,6 +87,10 @@ else { //Staging config +Configure::write('pdf_directory', $basedir.'/webroot/pdf/'); +Configure::write('attachments_directory', $basedir.'/webroot/attachments_files/'); + + // $output_dir = '/var/www/cakephp/app/webroot/pdf/'; /** diff --git a/app/controllers/attachments_controller.php b/app/controllers/attachments_controller.php index 47950e0a..fef9ba25 100644 --- a/app/controllers/attachments_controller.php +++ b/app/controllers/attachments_controller.php @@ -6,7 +6,7 @@ class AttachmentsController extends AppController { function index() { $this->Attachment->recursive = 1; - $this->set('attachments', $this->paginate()); + $this->set('attachments', $this->paginate()); } function view($id = null) { @@ -14,23 +14,26 @@ class AttachmentsController extends AppController { $this->Session->setFlash(__('Invalid Attachment.', true)); $this->redirect(array('action'=>'index')); } - $this->set('attachment', $this->Attachment->read(null, $id)); - $this->layout = 'pdf'; - } + $this->set('attachment', $this->Attachment->read(null, $id)); + $this->layout = 'pdf'; + } function add() { if (!empty($this->data)) { - - $attachment = $this->Attachment->process_attachment($this->data); + $attachment = $this->Attachment->process_attachment($this->data); + if(!$attachment) { + $this->Session->setFlash('The Attachment could not be saved. The filename exists'); + } + else { + $this->Attachment->create(); - $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)); + 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'); @@ -62,11 +65,13 @@ class AttachmentsController extends AppController { $this->Session->setFlash(__('Invalid id for Attachment', true)); $this->redirect(array('action'=>'index')); } + $attachment = $this->Attachment->read(null, $id); if ($this->Attachment->del($id)) { + unlink($attachment['Attachment']['file']); $this->Session->setFlash(__('Attachment deleted', true)); $this->redirect(array('action'=>'index')); } - } + } } ?> diff --git a/app/models/attachment.php b/app/models/attachment.php index 80b54f5a..80edc095 100644 --- a/app/models/attachment.php +++ b/app/models/attachment.php @@ -7,7 +7,7 @@ class Attachment extends AppModel { 'name' => array('notempty'), 'type' => array('notempty'), 'size' => array('numeric'), - 'archived' => array('numeric'), + 'archived' => array('numeric'), ); //The Associations below have been created with all possible keys, those that are not needed can be removed @@ -25,53 +25,49 @@ class Attachment extends AppModel { 'DocumentAttachment' => array( 'className' => 'DocumentAttachment', 'foreignKey' => 'attachment_id' - ) + ) ); -// Based on comment 8 from: http://bakery.cakephp.org/articles/view/improved-advance-validation-with-parameters + // 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; - } + 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. - * - */ + /** + * 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) { + function process_attachment($attachment) { - $upload = $attachment['Attachment']['file']; + $upload = $attachment['Attachment']['file']; - $new_filename = Configure::read('attachments_directory').time().$upload['name']; + $new_filename = Configure::read('attachments_directory').$upload['name']; + if(!file_exists($new_filename)) { + move_uploaded_file($upload['tmp_name'], $new_filename); - $moved = move_uploaded_file($upload['tmp_name'], $new_filename); + $attachment['Attachment']['filename'] = $upload['name']; + $attachment['Attachment']['file'] = $new_filename; + $attachment['Attachment']['size'] = $upload['size']; + $attachment['Attachment']['type'] = $upload['type']; + return $attachment; - 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"); - } + } + return false; - - - } + } } ?>