Simplified attachments

This commit is contained in:
Karl Cordes 2013-04-04 21:54:28 +11:00
parent bdbec2b9b3
commit 59090dc452
3 changed files with 55 additions and 68 deletions

View file

@ -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/';
/**

View file

@ -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'));
}
}
}
}
?>

View file

@ -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;
}
}
}
?>