<?php
class AttachmentsController extends AppController {

	var $name = 'Attachments';
	var $helpers = array('Html', 'Form','Number','Time');


	var $paginate = array(

		'limit' => 500,
		'order' => array(
			array('Principle.short_name' => 'asc'),
			array('Attachment.name' => 'asc')
		),
		'conditions' => array('Attachment.archived' => 0)
	);


	function index() {
		$this->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 archived() {
		$this->set('archived', $this->Attachment->find('all', array('conditions'=> array('archived' => 1), 'order' => array(
			array('Principle.short_name' => 'asc'),
			array('Attachment.name' => 'asc')
		))));
	}

	function add() {
		if (!empty($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();

				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->data;
			$existing = $this->Attachment->find('first', array('conditions'=>array('Attachment.id' => $this->data['Attachment']['id'])));
			//Process editing the PDFs.
			if(!empty($this->data['Attachment']['file']['tmp_name'])) {

				if(!empty($existing)) {
					//Delete old file
					unlink($existing['Attachment']['file']);	

				}
				$attachment = $this->Attachment->process_attachment($this->data);

				if(!$attachment) {
					$this->Session->setFlash('The Attachment could not be saved. The filename exists');
				}	
			}
			else {
				$attachment['Attachment']['file'] = $existing['Attachment']['file'];
			}
			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));
			}		


		}
		else {
			$this->data = $this->Attachment->read(null, $id);
		}
		$principles = $this->Attachment->Principle->find('list');
		$this->set(compact('principles'));
	}

	function delete($id = null) {
		if (!$id) {
			$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'));
		}
	}

}
?>
