<?php
class PrintersController extends AppController {

	var $name = 'Printers';
var $uses = array('Printer');
var $components = array('RequestHandler');

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

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

	function add() {
		if (!empty($this->data)) {
		if ($this->data['Printer']['is_default']==1)
			{$this->Printer->query("update printers set is_default=0");}
			
			$this->Printer->create();
			if ($this->Printer->save($this->data)) {
				$this->Session->setFlash(__('The printer has been saved', true), 'default', array('class' => 'success'));
		   //$x=$this->params['isAjax'];		
  if ($this->RequestHandler->isAjax()) {
  //die(print_r($this->params['nyro']));		
  $this->nyro = array(
						'close' => true, 'reload' => false, 'hide_content' => true,
						'js_callback' => 'after_add_Printer',
						'js_callback_param' => $this->Printer->read(null, $this->Printer->getLastInsertID()),
					);
				} 			

			else
			
			$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Printer could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		//$products = $this->Printer->Product->getGrouped();
		//$this->set(compact('products'));
	}

	function edit($id = null) {
		if ((!$id && empty($this->data)) || ($id && !($item = $this->Printer->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Printer', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->data['Printer']['is_default']==1)
			{$this->Printer->query("update printers set is_default=0");}
			
			$this->Printer->data = null;	// unset model data so modified field can be updated automagically
			if ($this->Printer->save($this->data)) {
				$this->Session->setFlash(__('The Printer has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Printer could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
		}
	//	$products = $this->Printer->Product->getGrouped();
	//	$this->set(compact('products'));
	}

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