<?php
class TablesController extends AppController {

	var $name = 'Tables';
	var $uses = array('Table', 'TableGroup');

    var $components = array(
	    'Filter' => array(
	        'index' => array(
	            'table_group_id' => array(
        	        'form_options' => array('label' => 'TableGroup', 'options' => 'TableGroups'),
	            	'filter_options' => array(),
	            ),
				'name' => array(
        	        'form_options' => array('label' => 'Name'),
	            	'filter_options' => array('fields' => array('Table.name'), 'operator' => 'like^'),
				),
	        )
        ),
	);

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

	function index() {
		if (!empty($this->filterParams['table_group_id'])) {
			$this->set('tableGroup', $this->TableGroup->read(null, $this->filterParams['table_group_id']));
		}

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

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

	function add() {
		if (!empty($this->data)) {
			$this->Table->create();
			if ($this->Table->save($this->data)) {
				$this->Session->setFlash(__('The table 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 table could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		$tableGroups = $this->Table->TableGroup->find('list');
		$this->set(compact('tableGroups'));
	}

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

	function delete($id = null) {
		if (!$id || !$this->Table->read(null, $id)) {
			$this->Session->setFlash(__('Invalid Table', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Table->delete($id)) {
			$this->Session->setFlash(__('Table deleted', true), 'default', array('class' => 'success'));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Table 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->TableGroup->create();
			if ($this->TableGroup->save($this->data)) {
				$this->Session->setFlash(__('The Table 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_table_group',
						'js_callback_param' => $this->TableGroup->read(null, $this->TableGroup->getLastInsertID()),
					);
				} else {
					$this->redirect(array('action'=>'index'));
				}
			} else {
				$this->Session->setFlash(__('The Table 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->TableGroup->read(null, $id)))) {
			$this->Session->setFlash(__('Invalid Table Group', true) . ($id ? " #{$id}" : ""), 'default', array('class' => 'error'));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
		$val=($this->data['TableGroup']['data']);
		$val1 =explode("\n",$val);
		$vi=0;
		foreach ($val1 as $valx)
				{
				if ($vi)
				{
				$sto=explode(",",$valx);
				if (count($sto)==12);
				{
				if($sto[0]=='STO')
				{
				$this->data['Table'][$vi-1]['name']=$sto[1];
				$this->data['Table'][$vi-1]['color']=$sto[2]+$sto[3]*256+$sto[4]*65536;
				$this->data['Table'][$vi-1]['position_x']=$sto[5];
				$this->data['Table'][$vi-1]['position_y']=$sto[6];
				$this->data['Table'][$vi-1]['angle']=$sto[7];
				$this->data['Table'][$vi-1]['width']=$sto[8];
				$this->data['Table'][$vi-1]['height']=$sto[9];
				if ($sto[10]=='KRUZNI')$this->data['Table'][$vi-1]['type']=1;  else 
				$this->data['Table'][$vi-1]['type']=0;
				if ($sto[11] != -1) $this->data['Table'][$vi-1]['id']=$sto[11];
				
				//$this->data['Table'][$vi-1]["id"]=$this->TableGroup->data['Table'][$vi-1]["id"];
				print_r($this->TableGroup->data['Table'][$vi-1]["id"]);
				$this->data['Table'][$vi-1]['table_group_id']=$id;
				$this->Table->save($this->data['Table'][$vi-1]);
				}
				}
				}
				$vi++;
				}
			
			$this->TableGroup->data = null;	// unset model data so modified field can be updated automagically
			if ($this->TableGroup->save($this->data)) {
				$this->Session->setFlash(__('The Table Group has been saved', true), 'default', array('class' => 'success'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Table Group could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
			}
		}
		if (empty($this->data)) {
			$this->data = $item;
			$this->set('item', $item);
		}
	}

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

	function _faTableGroups() {
		return $this->TableGroup->find('list');
	}



	// create default table configuration
	function init_table_db()
	{
		$this->autoRender = false;
		$this->TableGroup->query("TRUNCATE " . $this->TableGroup->useTable);
		$this->TableGroup->Table->query("TRUNCATE " . $this->TableGroup->Table->useTable);

		$this->TableGroup->create();
		$this->TableGroup->save(array('name' => 'Lokal'));
		$id_group = $this->TableGroup->getInsertId();

		$data = array();
		for($i = 1; $i <= 50; $i++) {
			$data[] = array('table_group_id' => $id_group, 'name' => "STL{$i}");
		}
		$this->TableGroup->Table->saveAll($data);

		$this->TableGroup->create();
		$this->TableGroup->save(array('name' => 'Terasa'));
		$id_group = $this->TableGroup->getInsertId();

		$data = array();
		for($i = 1; $i <= 100; $i++) {
			$data[] = array('table_group_id' => $id_group, 'name' => "STT{$i}");
		}
		$this->TableGroup->Table->saveAll($data);

	}

}
?>