<?php
class ExtraGroupsController extends AppController {

	var $name = 'ExtraGroups';
var $uses = array('ExtraGroup');
var $components = array('RequestHandler');

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

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

	function add() {
		if (!empty($this->data)) {
			$this->ExtraGroup->create();
			if ($this->ExtraGroup->save($this->data)) {
				$this->Session->setFlash(__('The extra group 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_extra_group',
						'js_callback_param' => $this->ExtraGroup->read(null, $this->ExtraGroup->getLastInsertID()),
					);
				} 			

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

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

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