<?php
class Skupina extends Super {

  protected $id;
  protected $nazev;
  protected $koeficient;
  protected $procenta; //vypočítaná hodnota
  protected $hranice;
  protected $popis;
  protected $stav = 1;  // default stav 

  protected $getter = array('id', 'nazev', 'popis', 'koeficient', 'hranice', 'automaticky', 'stav');
  protected $setter = array('nazev', 'popis', 'koeficient', 'hranice', 'automaticky', 'stav');
  public $validate = array(
  	'id'    => array('digits' => 'true'),
  	'nazev' => array('required' => 'true', 'maxlength' => 35),
  	'sleva' => array('required' => 'true', 'numberDE' => 'true'),
		'hranice'  => array('number' => 'true'),
  	'automaticky' => array(),
  	'koeficient'  => array('number' => 'true'),
  	'sleva'		=> array('required' => 'true', 'numberDE' => 'true'),
    'stav'   => array('range' => '[0,1]' )
  );
	public $label = array(
  	'id'     => 'Id',
  	'nazev'  => 'Název',
  	'koeficient' => 'Koef.',
		'sleva'	=> 'Sleva',
  	'hranice' => 'Hranice.',
		'automaticky'	=> 'Automaticky',
    'popis' 	 => 'Popis',
    'stav' 	 => 'Stav'
  );
  public $valAll = array(
  	'id'     => true,
  	'nazev'  => true,
  	'hranice' => true,
  	'koeficient' => true,
		'automaticky'	=> true,
    'popis' 	 => true,
    'stav' 	 => true
  );
	public $filtry = array(
      'ID' => array(
      	'type' => 'text',
				'key'	 => 'ID',
				'sql'	 => 'num',
    		'size' => 3
    	),
      'NAZEV' => array(
      	'type' => 'text',
    		'key'	 => 'NAZEV',
    		'sql'	 => 'text',
    		'size' => 12
    	),
			'HRANICE' => array(
      	'type' => 'text',
    		'key'	 => 'HRANICE',
    		'sql'	 => 'text',
    		'size' => 12
    	),
			'AUTOMAT' => array(
      	'type' => 'select_an',
    		'key'	 => 'AUTOMATICKY',
    		'sql'	 => 'num',
    		'size' => 12
    	),
      'S_NAZEV' => array(
      	'type' => 'stav',
    		'key'	 => 'STAV',
    		'table' => 'SKUPINA_UZIVATEL',
    		'sql'	 => 'num'
    	),
      'UPRAVIT' => array(
      	'type' => 'submit'
    	),
      'SMAZAT' => array(
      	'type' => 'all'
    	)
    );
  public $state_validate = array();

  function __construct() {
    /*
     * stavy:
     * 1 - aktivní
     * 2 - neaktivní
     */
    $this->state_validate[1] = $this->valAll;
    $this->state_validate[0] = $this->valAll;
    $this->table = 'SKUPINA_UZIVATEL';

  }

  public function getToSelect() {
    $res = dibi::query('SELECT E.*
    	FROM ['.$this->table.'] AS E
    	WHERE STAV = %i',
         1);
    return $res->fetchAssoc('ID');
  }

  public function loadById($id, $hash = false, $postRestore = false) {
    $result = dibi::query('SELECT  E.*, S.NAZEV AS S_NAZEV, S.POPIS AS S_POPIS FROM ['.$this->table.'] AS E 
    	INNER JOIN STAV AS S ON S.STAV_ID = E.STAV AND ENTITA = %s
    	WHERE E.ID = %i;', $this->table, $id);

    if (count($result) == 0) {
      throw new Appliexception(1, 'Požadovaný záznam neexistuje', 'seznam', __METHOD__.' Neexistující záznam '.$id );
    }

    $row = $result->fetch()->getArrayCopy();
    $this->setFromRow($row);
    $this->my_stav = '<div title="'.$row['S_POPIS'].'" class="block_state"> '.$row['STAV'].' - '.$row['S_NAZEV'].'</div><br />';

    if ($hash) {
      $this->hash  = $this->getHash();
    } 

    $this->myStates();

    if ($postRestore) {
      $this->setFromPost();
    }
    return $this->toArray();
  }

  public function getAllQuery() {
  	$query = array();
  	$query[] = 'SELECT E.*, (1 - E.KOEFICIENT)*100 AS SLEVA, IF(E.AUTOMATICKY = 1,"Ano","Ne") AS AUTOMAT, S.NAZEV AS S_NAZEV
			FROM ['.$this->table.'] AS E';
  	array_push($query, 'INNER JOIN [STAV] S ON S.STAV_ID = E.STAV AND S.ENTITA = %s',$this->table );
  	return $query;
	}

	public function beforeInsert() {

		$_POST['sleva'] = Tools::numberFromDE($_POST['sleva']);
		$this->koeficient = 1 - ($_POST['sleva'] / 100);

		if (isset($_POST['automaticky'])) {
			$this->automaticky = 1;
		}
		else {
			$this->automaticky = 0;
		}

	}

	public function beforeUpdate() {

		$_POST['sleva'] = Tools::numberFromDE($_POST['sleva']);
		$this->koeficient = 1 - ($_POST['sleva'] / 100);
		
		if (isset($_POST['automaticky'])) {
			$this->automaticky = 1;
		}
		else {
			$this->automaticky = 0;
		}
	}

}
?>