<?php
class SuppliersController extends AppController {

	var $name = 'Suppliers';

	function index() {
		$this->Supplier->recursive = 0;
		$this->set('items', $this->paginate());
	}

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

	function add() {
		if (!empty($this->data)) {
			$this->Supplier->create();
			if ($this->Supplier->save($this->data)) {
				$this->Session->setFlash(__('The supplier 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_supplier',
						'js_callback_param' => $this->Supplier->read(null, $this->Supplier->getLastInsertID()),
					);
				} else {
					$this->redirect(array('action'=>'index'));
				}
			} else {
				$this->Session->setFlash(__('The supplier could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
	}

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

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