<?php class EnquiryFilesController extends AppController {
	
	var $name = 'EnquiryFiles';
	var $helpers = array('Html', 'Form', 'Time');
	
	function add($enquiryid = null) {
		if(isset($this->params['named']['enquiryid'])) {
			$enquiryid = $this->params['named']['enquiryid'];
			$this->set('enquiry', $this->EnquiryFile->Enquiry->findById($enquiryid));
		}
		
		if (!$enquiryid && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Enquiry ID', true));
			$this->redirect(array('controller'=>'Enquiries', 'action'=>'index'));
		}
		
		 if (!empty($this->data) &&
             is_uploaded_file($this->data['EnquiryFile']['File']['tmp_name'])) {
            $fileData = fread(fopen($this->data['EnquiryFile']['File']['tmp_name'], "r"),
                                     $this->data['EnquiryFile']['File']['size']);

            $this->data['EnquiryFile']['name'] = $this->data['EnquiryFile']['File']['name'];
            $this->data['EnquiryFile']['type'] = $this->data['EnquiryFile']['File']['type'];
            $this->data['EnquiryFile']['size'] = $this->data['EnquiryFile']['File']['size'];
            $this->data['EnquiryFile']['data'] = $fileData;

            $this->EnquiryFile->save($this->data);

            $this->redirect(array('controller' => 'enquiries', 'action' => 'view/'.$enquiryid));
		}
	}


	function download($id) {
    Configure::write('debug', 0);
    $file = $this->EnquiryFile->findById($id);
    header('Content-type: ' . $file['EnquiryFile']['type']);
    header('Content-length: ' . $file['EnquiryFile']['size']);
    header('Content-Disposition: attachment; filename="'.$file['EnquiryFile']['name'].'"');
    echo $file['EnquiryFile']['data'];
    exit();
	}


}
