<?php
/**
 * Generated: 2014-03-24 15:01:35
 * @author Rtn_Module_Tool
 */
class Bip_Controller_Admin_SearchController extends Eppe_Crud_Controller_Action {
	
	protected $grid = 'Bip_Jqgrid_Admin_Search';
	protected $crud = 'Bip_CRUD_Admin_Search';
	
	public function init() {
                parent::init();
        }
	
	public function indexAction() {
		if ($this->isAjax) {
			$this->ajaxResponse->data = $this->grid->render();
		}
		else {
			$this->view->grid = $this->grid->render();
		}
	}
	
	protected function _add() {
		$formClassName = $this->crud->getFormClassName();

		$form = new $formClassName();

		if ($this->_isPost()) {
			$post = $this->_getPost();
			if (isset($post['Anuluj'])) {
				$this->_redirectIndex();
			}

			if ($form->isValid($post) && $beanId = $this->crud->create($post)) {
				$bean = R::findOne('biparticle', "WHERE `id` = $beanId");
				$bean->uid = $bean->id;
				R::store($bean);
				
				$this->_addMessage(__('Zmiany zostały zapisane'));
				$this->_redirectIndex();
			}
			else {
				$form->populate($this->_getPost());
			}
		}

		$this->view->form = $form;
	}
	
	protected function _edit($beanId) {
		$bean = $this->crud->findBean($beanId);

		$formClassName = $this->crud->getFormClassName();

		$form = new $formClassName(array('bean' => $bean));

		if ($this->_isPost()) {
			$post = $this->_getPost();

			if (isset($post['Anuluj'])) {
				$this->_redirectIndex();
			}

			if ($form->isValid($post)) {
				if ($this->crud->update($beanId, $post)) {
					$this->_addMessage(__('Zmiany zostały zapisane'));
					$this->_redirectIndex();
				}
			}
			else {
				$form->populate($this->_getPost());
			}
		} else {
			$form->populateWithBean($bean);
		}
		
		$this->view->form = $form;
		$this->view->oldPdfs = R::findAll('bippdf', "WHERE `biparticle_id` = {$bean->id}");
		
	}
	
	public function ajaxLoadDatagridAction() {
		$session = new Zend_Session_Namespace('bip');
		echo $this->grid->loadDatagrid($session->id);
		$session->id = 0;
		exit;
	}
	
	public function ajaxRemoveOldPdfAction() {
		 $id = $this->_getParam('id');
		$bean = R::find('bippdf', "WHERE `id` = '$id'");
		R::trashAll($bean);
		exit;
	}
}


//	public function indexAction() {
//		$beans = R::findAll('artykuly');
//		$cyrylicFrom = array(
//		    '³','æ','¿','¶','ê','±','¦','ñ','Ê'
//		);
//		$cyrylicTo = array(
//		    'ł','ć','ż','ś','ę','ą','Ś','ń','Ę'
//		);
//		
//		
//		foreach ($beans as $bean) {
//			$bean->tytul= str_replace($cyrylicFrom, $cyrylicTo, $bean->tytul);
//			$bean->tresc = str_replace($cyrylicFrom, $cyrylicTo, $bean->tresc);
//			$bean->autor = str_replace($cyrylicFrom, $cyrylicTo, $bean->autor);
//			R::store($bean);
//		}
///*		echo 'Go Go!' . PHP_EOL;
//		$counter = 0;
//		$magicArray = array();
//		if (($handle = fopen("oilcategorie.csv", "r")) !== FALSE) {
//			while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
//				$magicArray[$data[0]] = $data[1];
//				$counter++;
//				
//			}
//			fclose($handle);
//		}
//		
//		$magicBeans = R::findAll('artykuly');
//		foreach ($magicBeans as $fasolkaWszystkichSamkow) {
//			if(array_key_exists($fasolkaWszystkichSamkow->parent, $magicArray)){
//				$fasolkaWszystkichSamkow->parent = $magicArray[$fasolkaWszystkichSamkow->parent];
//			}else{
//				$fasolkaWszystkichSamkow->parent = 0;
//			}
//			R::store($fasolkaWszystkichSamkow);
//		}
//		echo 'Koniec!' . PHP_EOL;
//*/		
//			
//}
	