<?php class ProductAttachmentsController extends AppController {
	
	var $name = 'ProductAttachments';
	var $helpers = array('Html', 'Form', 'Time');


	function add($enquiryid = null) {
		if(isset($this->params['named']['productid'])) {
			$productid = $this->params['named']['productid'];
			$this->set('product', $this->ProductAttachment->Product->findById($productid));
		}
		
		if (!$productid && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Product ID', true));
			$this->redirect(array('controller'=>'Products', 'action'=>'index'));
		}
		
		 if (!empty($this->data) &&
             is_uploaded_file($this->data['ProductAttachment']['File']['tmp_name'])) {
            $fileData = fread(fopen($this->data['ProductAttachment']['File']['tmp_name'], "r"),
                                     $this->data['ProductAttachment']['File']['size']);

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

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

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


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