<?php
class ProductOptionsController extends AppController {

	var $name = 'ProductOptions';
	var $helpers = array('Html', 'Form');

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

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid ProductOption.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('productOption', $this->ProductOption->read(null, $id));
	}

	function add($catid = null) {
		if (!$catid && empty($this->data)) {	
			$this->Session->setFlash(__('Invalid Product Options Category. Options can only be added to an existing Options Category.', true));
			$this->redirect(array('action'=>'index'));
		}

		$optcat = $this->ProductOption->ProductOptionsCategory->read(null, $catid);
		if (!empty($this->data)) {
			$this->ProductOption->create();
			$productid = $this->data['ProductOption']['product_id'];
			if ($this->ProductOption->save($this->data)) {
				$this->Session->setFlash(__('The Product Option has been saved', true));
				$this->redirect(array('controller' => 'products', 'action'=>'view', $productid));
			} else {
				$this->Session->setFlash(__('The ProductOption could not be saved. Please, try again.', true));
			}
		}
		$optcat = $this->ProductOption->ProductOptionsCategory->read(null, $catid);
		$this->set('optcat', $optcat);
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid ProductOption', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			$productid = $this->data['ProductOption']['product_id'];
			if ($this->ProductOption->save($this->data)) {
				$this->Session->setFlash(__('The Product Option has been saved', true));
				$this->redirect(array('controller'=> 'products', 'action'=>'view', $productid));
			} else {
				$this->Session->setFlash(__('The ProductOption could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->ProductOption->read(null, $id);
			$product = $this->ProductOption->ProductOptionsCategory->Product->read(null, $this->data['ProductOption']['product_id']);
		}
		
		$this->set('product', $product);
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for ProductOption', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->ProductOption->del($id)) {
			$this->Session->setFlash(__('ProductOption deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}

}
?>
