<?php
class NotesController extends AppController {

	var $name = 'Notes';
	var $uses = array('Note', 'NoteGroup');

    var $components = array(
	    'Filter' => array(
	        'index' => array(
	            'note_group_id' => array(
        	        'form_options' => array('label' => 'NoteGroup', 'options' => 'NoteGroups'),
	            	'filter_options' => array(),
	            ),
				'name' => array(
        	        'form_options' => array('label' => 'Name'),
	            	'filter_options' => array('fields' => array('Note.name'), 'operator' => 'like^'),
				),
	        )
        ),
	);

	function  beforeFilter() {
		parent::beforeFilter();
		$this->Auth->mapActions(array('group_add' => 'create', 'group_edit' => 'update', 'group_delete' => 'delete'));
	}

	function index() {
		if (!empty($this->filterParams['note_group_id'])) {
			$this->set('noteGroup', $this->NoteGroup->read(null, $this->filterParams['note_group_id']));
		}

		$this->Note->recursive = 0;
		$this->set('items', $this->paginate());
	}

	function view($id = null) {
		if (!$id  || !($item = $this->Note->read(null, $id))) {
			$this->Session->setFlash(__('Invalid Note', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('item', $item);
	}

	function add() {
		if (!empty($this->data)) {
			$this->Note->create();
			if ($this->Note->save($this->data)) {
				$this->Session->setFlash(__('The note has been saved', true), 'default', array('class' => 'success'));
                if ($this->RequestHandler->isAjax()) {
					$this->nyro = array(
						'close' => true, 'reload' => false, 'hide_content' => true,
						'js_callback' => 'after_add_article',
						'js_callback_param' => $this->Article->read(null, $this->Article->getLastInsertID()),
					);
				} else {
					$this->redirect(array('action'=>'index'));
				}
			} else {
				$this->Session->setFlash(__('The note could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		$noteGroups = $this->Note->NoteGroup->find('list');
		$this->set(compact('noteGroups'));
	}

	function edit($id = null) {
		if ((!$id && empty($this->data)) || ($id && !($item = $this->Note->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Note', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->Note->data = null;	// unset model data so modified field can be updated automagically
			if ($this->Note->save($this->data)) {
				$this->Session->setFlash(__('The note has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The note could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
		}
		$noteGroups = $this->Note->NoteGroup->find('list');
		$this->set(compact('noteGroups'));
	}

	function delete($id = null) {
		if (!$id || !$this->Note->read(null, $id)) {
			$this->Session->setFlash(__('Invalid Note', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Note->delete($id)) {
			$this->Session->setFlash(__('Note deleted', true), 'default', array('class' => 'success'));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Note delete failed. Please, try again.', true), 'default', array('class' => 'error'));
		$this->redirect(array('action' => 'index'));
	}

// TABLE GROUPS
	function group_add() {
		if (!empty($this->data)) {
			$this->NoteGroup->create();
			if ($this->NoteGroup->save($this->data)) {
				$this->Session->setFlash(__('The Note Group has been saved', true), 'default', array('class' => 'success'));
                if ($this->RequestHandler->isAjax()) {
					$this->nyro = array(
						'close' => true, 'reload' => false, 'hide_content' => true,
						'js_callback' => 'after_add_note_group',
						'js_callback_param' => $this->NoteGroup->read(null, $this->NoteGroup->getLastInsertID()),
					);
				} else {
					$this->redirect(array('action'=>'index'));
				}
			} else {
				$this->Session->setFlash(__('The Note Group could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
	}

	function group_edit($id = null) {
		if ((!$id && empty($this->data)) || ($id && !($item = $this->NoteGroup->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Note Group', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->NoteGroup->data = null;	// unset model data so modified field can be updated automagically
			if ($this->NoteGroup->save($this->data)) {
				$this->Session->setFlash(__('The Note Group has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Note Group could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
		}
	}

	function group_delete($id = null) {
		if (!$id || !$this->NoteGroup->read(null, $id)) {
			$this->Session->setFlash(__('Invalid Note Group', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->NoteGroup->delete($id)) {
			$this->Session->setFlash(__('Note Group deleted', true), 'default', array('class' => 'success'));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Note Group delete failed. Please, try again.', true), 'default', array('class' => 'error'));
		$this->redirect(array('action' => 'index'));
	}

	function _faNoteGroups() {
		return $this->NoteGroup->find('list');
	}

}
?>