<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of JqGrid
 *
 * @author arson
 */
abstract class Eppe_Jqgrid_Jqgrid {

	/**
	 * @var bool Czy można zmieniać
	 */
	public $canReorder = false;

	/**
	 * @var bool Czy widoczne domyślne akcje (edycja/usuń)
	 */
	public $showDefaultActions = true;
	public $name;
	public $autowidth = true;
	public $caption = 'Grid';
	public $options = array();
	public $url;
	public $url_remove;
	public $url_clone;
	public $url_view;
	public $url_edit;
	public $url_export;
	public $url_send_test;
	public $url_show;
	public $url_add;
	public $url_active;
	public $url_reorder;
	public $url_replace;
	public $url_refresh;
	public $id_col = 'id';
	public $multiselect = false;
	public $sortname = 'order';
	public $sortorder = 'desc';
	public $rownum = 100;
	public $rowlist = array(10, 20, 30);
	public $colmodel;
	public $custom_buttons = array();
	public $toolbar = array();
	public $custom_js;
	public $custom_actions;
	public $height = 'auto';
	public $scroll = false;
	public $shrinktofit = true;
	public $search_button = false;
	public $search_toolbar = true;
	public $loadonce = false;
	public $hiddengrid = false;
	public $tree_grid = false;
	public $expand_column = '';
	public $view = 'jq/grid';

	public function set($key, $value = null) {
		if (!in_array($key, get_object_vars($this)))
			throw new Exception('Property not found');

		$this->$key = $value;

		return $this;
	}

	public function setUrlAdd($url) {
		$this->url_add = $url;
	}

	public function setUrlShow($url) {
		$this->url_show = $url;
	}

	public function setUrlEdit($url) {
		$this->url_edit = $url;
	}

	public function setUrlImport($url) {
		$this->url_import = $url;
	}

	public function setUrlExport($url) {
		$this->url_export = $url;
	}

	public function setUrlRemove($url) {
		$this->url_remove = $url;
	}

	public function setUrlRefresh($url) {
		$this->url_refresh = $url;
	}

	public function setUrlClone($url) {
		$this->url_clone = $url;
	}

	public function setUrlSendTest($url) {
		$this->url_send_test = $url;
	}

	public function setUrlActive($url) {
		$this->url_active = $url;
	}

	public function setUrlReorder($url) {
		$this->url_reorder = $url;
	}

	public function setUrlReplace($url) {
		$this->url_replace = $url;
	}

	public function setUrlLoadDatagrid($url) {
		$this->url = $url;
	}

	/**
	 * @return array colmodel
	 */
	protected function _renderColmodel() {
		return array();
	}

	protected function _renderCustomActions() {
		return array();
	}

	protected function _fixColmodel() {

		// domyślne akcje typu dodaj/usun

		if ($this->showDefaultActions OR !empty($this->custom_actions)) {
			$colmodelActions = array(
				array(
					'name' => '', 'label' => __('Akcje'),
					'sortable' => false,
					'width' => '80',
					'custom_formatter' => 'actions',
					'align' => 'center',
					'search' => false
				)
			);
			if ($this->url_reorder) {
				array_unshift($colmodelActions, array(
					'name' => 'reorder', 'label' => __('Sortuj'),
					'sortable' => false,
					'width' => '35',
					'custom_formatter' => 'move',
					'align' => 'center',
					'search' => false
				));
			}
		} else {
			$colmodelActions = array();
		}

		// aktywowanie / deaktywowanie elementów

		if (!empty($this->url_active)) {
			$colmodelActive = array(
				array(
					'name' => 'active', 'label' => __('Aktywny'),
					'width' => '80',
					'custom_formatter' => 'active',
					'align' => 'center',
					'search' => false
				)
			);
		} else {
			$colmodelActive = array();
		}

		return array_merge($colmodelActive, $colmodelActions, $this->_renderColmodel());
	}

	/**
	 * @return string ajax url_view
	 */
	protected function fix_url_view() {

	}

	/**
	 * @return array
	 */
	protected function fix_custom_buttons() {

	}

	protected function fix_toolbar() {

	}

	protected function fix_custom_js() {

	}

	public function __construct() {
		$this->custom_actions = $this->_renderCustomActions();
		$this->url_view = $this->fix_url_view();
		$this->custom_buttons = $this->fix_custom_buttons();
		$this->toolbar = $this->fix_toolbar();
		$this->custom_js = $this->fix_custom_js();

		$this->caption = __($this->caption);
	}

	public function selfTest() {
		$required = array('name', 'url', 'sortname', 'colmodel');
		foreach ($required as $property) {
			if (empty($this->$property)) {
				throw new Exception("Jq_Grid selftest failed($property)");
			}
		}
	}

	public function render() {
		$this->colmodel = $this->_fixColmodel(); // nie może być w konstruktorze (za wczesnie)

		$this->selfTest();

		$view = new Zend_View();
		$view->grid = $this;
		$view->isMobile = FALSE; // (Zend_Registry::get('client_device') == 'mobile');
//		$view->setScriptPath( APPLICATION_PATH . '/../library/Eppe/Jqgrid/');
		$view->setScriptPath(__DIR__);

		return $view->render('Jqgrid.phtml');
	}

}

