<?php

/**
 * Generated: 2014-03-24 15:01:35
 * @author Rtn_Module_Tool
 */
class Bip_Jqgrid_Admin_Index extends Rtn_JqGrid {

	public $beanName = 'viewbipintems';
	public $actions = array(
	    'add' => null, 
	    'show' => null,
	    'remove' => null,
	    'edit' => null,
	    'active' => null
	);
    public $rownum = 30;
    public $sortname = 'id';
    public $sortorder = 'DESC';

	protected function _renderColmodel() {
		if (Bip_Module_Module::getInstance()->groupsEnabled()) {
			$session = new Zend_Session_Namespace('bip');
			if(isset($session->id))
				$bean = R::findOne('category', "WHERE `id` = $session->id");
			
			$columns[] = [
			    'name' => 'groupname',
			    'label' => __('Kategoria'),
			    'stype' => 'select',
			    'searchoptions' => [
				'defaultValue' => (isset($bean)) ? $bean->name : '',
				'value' => Bip_Model_MenuHelper::getValuesToSelectBox('category', 'name', 'order', true)
			    ]
			];
		}
		$columns[] = [
		    'name' => 'title',
		    'label' => __('Tytuł'),
		];
		$columns[] = [
		    'name' => 'data',
		    'label' => __('Data'),
		    'width' => 70
		];
		$columns[] = [
		    'name' => 'link',
		    'label' => __('Link'),
		];
		return $columns;
	}
	
	public function loadDatagrid($id = null) {
		$this->colmodel = $this->_fixColmodel(); // nie może być w konstruktorze (za wczesnie)
		
		$query = R::$f->begin();
		$grid_params = static::get_prss();

		$this->extraTransformQuery($query);

		if (NULL != $rules_to_sql = $this->_searchRules($id)) {
			$query->where($rules_to_sql);
		}
		
		if (NULL != $order_rules_to_sql = $this->_orderRulesToSql()) {
			$query->order($order_rules_to_sql);
		}

		$count = R::count($this->beanName, $rules_to_sql);

		$query->limit(($grid_params['rows'] * ($grid_params['page'] - 1)), $grid_params['rows']);

		$beans = R::findAll($this->beanName, $query);
		$rows = array();

		foreach (array_keys($beans) as $bean_k) {
			$beans[$bean_k] = $this->_transformBean($beans[$bean_k]);

			$beans[$bean_k]['_grid_extra'] = array(
				'can_edit' => $this->_canRowBeEdited($beans[$bean_k]),
				'can_clone' => $this->_canRowBeCloned($beans[$bean_k]),
				'can_send_test' => $this->_canRowBeSendTest($beans[$bean_k]),
				'can_delete' => $this->_canRowBeDeleted($beans[$bean_k]),
				'can_refresh' => $this->_canRowBeRefresh($beans[$bean_k])
			);

			array_push($rows, $beans[$bean_k]);
		}

		$this->_searchArray($rows);
		$this->_orderArray($rows);


		$response = $this->_renderResponse($count, $grid_params['rows'], $grid_params['page'], $rows);

		return $response;
	}
	
	private function _searchRules($id) {
		$sql_arr = array();
		
		foreach (static::getSearchRules() as $rule) {
			if ($rule->data) {
				if (!$this->lookupField($rule->field)) {
					continue;
				}

				$op2op = array('eq' => '=', 'bw' => 'like');
				$op = $op2op[$rule->op];

				$value = $rule->data;
				$value = ($op == 'like') ? ('%' . $value . '%') : ($value);

				$col = $this->_getColmodelColumn($rule->field);
				if(isset($col['_type']) && $col['_type'] === 'boolean'){
					if($value==='true'){
						$sql_arr[] = "`{$rule->field}` = TRUE";
					}
					elseif($value==='false'){
						$sql_arr[] = "(`{$rule->field}` = FALSE OR `{$rule->field}` IS NULL)";
					}
				}
				else{
					$sql_arr[] = "`{$rule->field}` $op '$value'";
				}
			}
		}
		if($id){
			$sql_arr[] = "`category_id` = '$id'";
		}
		$sql_arr[] = "`current` = '1'";
		return join(' AND ', $sql_arr);
	}
	
	protected function _canRowBeShow($bean) {
		return true;
	}
	
	protected function setUrls() {
		$module = $this->controller->getRequest()->getModuleName();
		$controller = $this->controller->getRequest()->getControllerName();

		$helper = new Zend_View_Helper_Url();

		if (array_key_exists('add', $this->actions))
			$this->url_add = $helper->url(array('module' => $module, 'controller' => $controller, 'action' => 'add'));
		if (array_key_exists('clone', $this->actions))
			$this->url_clone = $helper->url(array('module' => $module, 'controller' => $controller, 'action' => 'clone', 'id' => false));
		if (array_key_exists('remove', $this->actions))
			$this->url_remove = $helper->url(array('module' => $module, 'controller' => $controller, 'action' => 'ajax-remove', 'id' => false), 'default', true);
		if (array_key_exists('show', $this->actions))
			$this->url_show = $helper->url(array('module' => $module, 'controller' => 'admin_children', 'action' => 'index', 'id' => false));
		if (array_key_exists('edit', $this->actions))
			$this->url_edit = $helper->url(array('module' => $module, 'controller' => $controller, 'action' => 'edit', 'id' => false));
		if (array_key_exists('export', $this->actions))
			$this->url_export = $helper->url(array('module' => $module, 'controller' => $controller, 'action' => 'export', 'id' => false));
		if (array_key_exists('active', $this->actions))
			$this->url_active = $helper->url(array('module' => $module, 'controller' => $controller, 'action' => 'ajax-toggle-active', 'id' => false));
		if (array_key_exists('refresh', $this->actions))
			$this->url_refresh = $helper->url(array('module' => $module, 'controller' => $controller, 'action' => 'ajax-refresh', 'id' => false));
		if (array_key_exists('reorder', $this->actions))
			$this->url_reorder = $helper->url(array('module' => $module, 'controller' => $controller, 'action' => 'ajax-reorder'));
		if (array_key_exists('replace', $this->actions))
			$this->url_replace = $helper->url(array('module' => $module, 'controller' => $controller, 'action' => 'ajax-replace'));
		$this->url = $helper->url(array('module' => $module, 'controller' => $controller, 'action' => 'ajax-load-datagrid'));
	}
	
	protected function _extraTransformRow($row, $bean) {
		$urlHelper = new Zend_View_Helper_Url();
		$row['link'] = $urlHelper->url([
		    'catid' => $bean['category_id'],
		    'id' => $bean['id'],
		    'slug' => Eppe_Slugify::slugify($bean['title'])
		], 'bip-article');
		
		return $row;
	}
}
