<?php

/**
 * UsersController - User accounts management
 * 
 * @author Lukáš Havlíček
 * @version 1.0
 */
class Admin_CategoryiesController extends Zend_Controller_Action 
{
	
	
	public function init()
	{
		$this->view->topmenu = 6;
		$this->view->leftmenu = 4;
	}
	/**
	 * The default action - show the home page
	 */
    public function indexAction() 
    {
    	$table = new Categoryies();
    	$this->view->data = $table->getMenu();
    }
    
    public function sortAction() 
    {
		$table = new Categoryies();
		$this->view->data = $table->getMenu();
    }
    
    public function ajaxSortAction() 
    {
		$handle = $_POST['sort'];
//		$handle = explode('&', $handle);
//
//		foreach ($handle as $k => $v) {
//			$handle[$k] = str_replace("id", "'id'", $v);
//			$handle[$k] = str_replace("children", "'children'", $v);
//			$parts = explode('=', $v);
//			$$parts[0] = $parts[1];
//			
//			
//		}
//		
//		Zend_Debug::dump($$parts);
		
		
		Zend_Debug::dump($_POST);
		die();
    }
    
    public function addAction()
    {
    	$form = $this->getForm();
    	
    	if ($this->_request->isPost()) {
    		
    		if ($form->isValid($_POST)) {
    			$table = new Categoryies();
    			$values = $form->getValues();
    			$values['position'] = $table->getPositionForInsert($values['id_parent']);
    			$table->insert($values);
    			$this->_redirect('/admin/categoryies/index/msg/1');
    			
    		} else {
    			
    			$form->populate($form->getUnfilteredValues());
    			if ($form->isErrors()) {
    				$helper = new FormHelper();
    				$errors = $helper->errorsToString($form);
    				$this->view->errors = $errors;
    			}
                        	
    		}
    		
    	}
    	
    	$this->view->form = $form;
    	
    	$this->view->edit = 0;
    	$this->render('add-edit');
    }
    
    public function editAction()
    {
    	$id = (int) $this->_getParam('id');
    	
    	$form = $this->getForm($id);
    	$table = new Categoryies();
    	
    	
    	if ($this->_request->isPost()) {
    		
    		if ($form->isValid($_POST)) {
    			
    			$values = $form->getValues();
    			$where  = $table->getAdapter()->quoteInto('id_category = ?', $values['id_category']);
    			
    			$row = $table->fetchRow($where);
    			
    			if ($row->id_parent != $values['id_parent']) {
    				$values['position'] = $table->getPositionForInsert($values['id_parent']);
    			}
    			
    			unset($values['id_category']);
    			$table->update($values, $where);
    			$table->repairPositions($row->id_parent);
    			$this->_redirect('/admin/categoryies/index/msg/2');
    			
    		} else {
    			
    			$form->populate($form->getUnfilteredValues());
    			if ($form->isErrors()) {
    				$helper = new FormHelper();
    				$errors = $helper->errorsToString($form);
    				$this->view->errors = $errors;
    			}
                        	
    		}
    		
    	}
    	
    	$row = $table->fetchRow($table->select()->where('id_category = ?', $id));
    	$form->populate($row->toArray());
    	
    	$this->view->form = $form;
    	$this->view->edit = 1;
    	$this->view->delAvail = !$table->hasChildren($id);
    	
    	//rights
    	$rights = new Rights();
    	$select = $rights->select()->setIntegrityCheck(false)
    					 ->from($rights)
    					 ->where('id_category = ?', $id)
    					 ->joinLeftUsing('iso_groups', 'id_group', array('group_name'));
    	
 		$rows = $rights->fetchAll($select);

    	$this->view->rights = $rows;

    	
    	$this->render('add-edit');
    }
    
    private function getForm($edit = 0)
    {
    	$form = new Zend_Form();
    	$form->clearDecorators();
    				
    	$form->addElement(new Zend_Form_Element_Text(array(
    					'name'	=> 'category_name',
    					'label' => 'nazev-kategorie-f',
    					'required' => true
    				)));
    				
    	$table = new Categoryies();
    	
    	$desc = array();
    	
    	if ($edit) {
    		$desc = $table->getAllDescendants($edit);
    	}
    	
    	$parents = $table->getParentsForSelect(false, $desc);
    	
    	
    	
    	$form->addElement(new Zend_Form_Element_Select(array(
    					'name'	=> 'id_parent',
    					'label' => 'nadrazena-kategorie-f',
    					'multioptions' => $parents
    				)));
    	
    	$form->addElement(new Zend_Form_Element_Checkbox(array(
    					'name'	=> 'category_active',
    					'label' => 'aktivni-f'
    				)));
    				
    	$form->addElement(new Zend_Form_Element_Hidden(array(
    					'name'	=> 'id_category'
    				)));
    	
    	return $form;
    }
    
    public function activateAction()
    {
    	$id = (int) $this->_getParam('id', 0);
    	if ($id) $this->changeState($id, 1);
    	$this->_redirect('/admin/categoryies');
    }
    
    public function deactivateAction()
    {
    	$id = (int) $this->_getParam('id', 0);
    	if ($id) $this->changeState($id, 0);
    	$this->_redirect('/admin/categoryies');
    }
    
    private function changeState($id, $state)
    {
    	$table = new Categoryies();
    	$row = $table->fetchRow($table->select()->where('id_category = ?', (int) $id));
    	if ($row) {
    		$row->category_active = (int) $state;
    		$row->save();
    		$this->_redirect('/admin/categoryies/index/msg/4');
    	} else {
    		$this->_redirect('/admin/categoryies');
    	}
    }
    
    public function deleteAction()
    {
    	$id   = (int) $this->_getParam('id', 0);
    	$thru = (int) $this->_getParam('thru', 0);
    	
    	$table = new Categoryies();
    	
    	if ($thru && $id) {
    		$table->delete($table->getAdapter()->quoteInto('id_category = ?', $id));
    		$this->_redirect('/admin/categoryies');
    	}
    	
    	if ($this->_request->isPost()) {
    		$id = (int) $this->_request->getPost('id');
    		if ($id) {
    			$table->delete($table->getAdapter()->quoteInto('id_category = ?', $id));
    			$this->_redirect('/admin/categoryies/index/msg/3');
    		} else {
    			$this->_redirect('/admin/categoryies');
    		}
    	} else {
    		$row = $table->fetchRow($table->select()->where('id_category = ?', $id));
    		if ($row) {
    			$this->view->data = $row;
    		} else {
    			$this->_redirect('/admin/categoryies');
    		}
    	}
    }
    
    public function upAction()
    {
    	$id = (int) $this->_getParam('id', 0);
    	if ($id) $this->move($id, 1);
    	$this->_redirect('/admin/categoryies');
    }
    
    public function downAction()
    {
    	$id = (int) $this->_getParam('id', 0);
    	if ($id) $this->move($id, 0);
    	$this->_redirect('/admin/categoryies');
    }
    
    private function move($id, $state)
    {
    	$table = new Categoryies();
    	$row = $table->fetchRow($table->select()->where('id_category = ?', (int) $id));
    	
    	if ($row) {
    		if ($state == 1) {
	    		$row2 = $table->fetchRow($table->select()->where('id_parent = ?', (int) $row->id_parent)->where('position = ?', $row->position - 1));
	    		if ($row2) {
	    			$row->position = $row->position - 1;
	    			$row2->position = $row2->position + 1;
	    			$row->save();
	    			$row2->save();
	    		}
    		} else {
    			$row2 = $table->fetchRow($table->select()->where('id_parent = ?', (int) $row->id_parent)->where('position = ?', $row->position + 1));
	    		if ($row2) {
	    			$row->position = $row->position + 1;
	    			$row2->position = $row2->position - 1;
	    			$row->save();
	    			$row2->save();
	    		}
    		}
    		
    		
    		$this->_redirect('/admin/categoryies');	
    		
    	}
    }
    
    public function addRightsGroupAction()
    {
    	$id = (int) $this->_getParam('id', 0);
    	
    	$table = new Categoryies();
    	$tableCont = new Rights();

    	if ($this->_request->isPost()) {
			
    		$form = $this->getRightsAddForm();
    		if ($form->isValid($_POST)) {
    			
				$values = $form->getValues();
				
				$tableCont->insert($values);
				
				$this->_redirect('/admin/categoryies/add-rights-group/id/' . $id);
				
    		}

    	}
    	
    	
    	$rows = $tableCont->fetchAll($tableCont->select()->where('id_category = ?', $id));
    	$avoid = array();
    	foreach ($rows as $i) {
    		$avoid[] = $i->id_group;
    	}
    	
    	
    	//rights
    	$rights = new Rights();
    	$select = $rights->select()->setIntegrityCheck(false)
    					 ->from($rights)
    					 ->where('id_category = ?', $id)
    					 ->joinLeftUsing('iso_groups', 'id_group', array('group_name'));
    	
 		$rows = $rights->fetchAll($select);

    	$this->view->rights = $rows;
    	
    	$form = $this->getRightsAddForm($avoid);
    	$form->populate(array('id_category' => $id));
    	$this->view->form = $form;
    	$this->view->item = $table->fetchRow($table->select()->where('id_category = ?', $id));
    }
    
    private function getRightsAddForm($avoid = null)
    {
    	$form = new Zend_Form();
    	$form->clearDecorators();
    				
    	$table = new Groups();
    	$multi = $table->getGroupsForSelect($avoid);
    				
    	$form->addElement(new Zend_Form_Element_Select(array(
    					'name'	=> 'id_group',
    					'multioptions'	=> $multi
    				)));
    				
    	$form->addElement(new Zend_Form_Element_Hidden(array(
    					'name'	=> 'id_category'
    				)));
    	
    	return $form;
	}
	
	public function deleteRightsGroupAction()
    {
    	
    	$id   = (int) $this->_getParam('id', 0);
    	$rights = (int) $this->_getParam('rights', 0);
    	$thru = (int) $this->_getParam('thru', 0);
    	
    	$table = new Rights();
    	
    	if ($thru && $id && $rights) {
    		$table->delete('id_rights = ' . $rights);
    		$this->_redirect('/admin/categoryies/edit/id/' . $id . '#rights');
    	}
    	
    	if ($this->_request->isPost()) {
    		$id = (int) $this->_request->getPost('id');
    		$pass = (int) $this->_request->getPost('pass');
    		if ($id && $pass) {
    			$table->delete('id_rights = ' . $id);
    			$this->_redirect('/admin/categoryies/edit/id/' . $pass . '/msg/3#rights');
    		} else {
    			$this->_redirect('/admin/categoryies/edit/id/' . $pass);
    		}
    	} else {
    		$row = $table->fetchRow(
    			$table->select()->setIntegrityCheck(false)
    				  ->from($table)
    				  ->joinLeftUsing('iso_groups', 'id_group')
    				  ->where('id_rights = ?', $rights)
    			);
    		if ($row) {
    			$this->view->data = $row;
    		} else {
    			$this->_redirect('/admin/categoryies/edit/id/' . $id);
    		}
    	}
    }
    
    public function activateRightsGroupAction()
    {
    	$id = (int) $this->_getParam('id', 0);
    	$rights = (int) $this->_getParam('rights', 0);
    	if ($id && $rights) $this->changeRightsGroupState($rights, $id, 1);
    	$this->_redirect('/admin/categoryies/edit/id/' . $id . '#rights');
    }
    
    public function deactivateRightsGroupAction()
    {
    	$id = (int) $this->_getParam('id', 0);
    	$rights = (int) $this->_getParam('rights', 0);
    	if ($id && $rights) $this->changeRightsGroupState($rights, $id, 0);
    	$this->_redirect('/admin/categoryies/edit/id/' . $id . '#rights');
    }
    
    private function changeRightsGroupState($rights, $id, $state)
    {
    	
    	$table = new Rights();
    	$row = $table->fetchRow($table->select()->where('id_rights = ?', (int) $rights));
    	if ($row) {
    		
    		$row->rights_active = (int) $state;
    		$row->save();
    		
    		$this->_redirect('/admin/categoryies/edit/id/' . $id . '/msg/5#rights');
    	} else {
    		$this->_redirect('/admin/categoryies/edit/id/' . $id . '#rights');
    	}
    }
    
    public function editRightsAction()
    {
    	$request = $this->getRequest();
    	if ($request->isPost()) {
    		
			$id_category = $request->getPost('id_category');

			if (!$id_category) $this->redirect('/');
			
			$read = $request->getPost('read');
			$add = $request->getPost('add');
			$edit = $request->getPost('edit');
			$del = $request->getPost('del');
    		
			$table = new Rights();
			
			foreach ($read as $k => $i) {
				$data['read']	= $i;
				$data['add']	= $add[$k]; 
				$data['edit']	= $edit[$k]; 
				$data['del']	= $del[$k];
				
				if ($data['add'] || $data['edit'] || $data['del']) {
					$data['read'] = 1;
				}
				
				$table->update($data, 'id_rights = ' . (int) $k);
			}
			
			$this->_redirect('/admin/categoryies/edit/id/' . $id_category . '#rights');
    	}
    	$this->_redirect('/admin/categoryies');
    }
}
