<?php

class adminController extends baseadminController {

  function __construct() {
    $this->controller_name = 'Info';
    $this->controller_fname = 'admin';
    $this->controller_info = 'Info';
    $this->entita_classname = 'Atribut';
    parent::__construct();
    $this->tpl = 'page.tpl';
  }

  public function index($args){

  	$this->html = '<h1>Informace o e-shopu</h1>';
  	
  	$produkty = dibi::query('SELECT count(P.ID) AS POCET, S.STAV_ID, S.NAZEV AS S_NAZEV
			FROM STAV S
			LEFT JOIN PRODUKT P ON P.STAV = S.STAV_ID
			WHERE ENTITA = "PRODUKT"
			GROUP BY S.STAV_ID');
  	$objednavky = dibi::query('SELECT count(O.ID) AS POCET, S.STAV_ID, S.NAZEV AS S_NAZEV
			FROM STAV S
			LEFT JOIN OBJEDNAVKA O ON O.STAV = S.STAV_ID
			WHERE ENTITA = "OBJEDNAVKA"
			GROUP BY S.STAV_ID');

  	$this->html .= '<h2>Produkty</h2><table class="seznam" style="margin:5px auto;" cellspacing="0">';  	
  	$this->html .= '<thead><tr><th class="a_c"> Stav </th><th class="a_c"> Počet produktů </th></tr></thead>';
  	foreach($produkty as $item) {
  		$this->html .= '<tbody><tr><td>'.$item['S_NAZEV'].'</td><td class="a_c">'.$item['POCET'].'</td></tr>';
  	}
  	$this->html .= '</table>';
  	unset($produkty);

  	$this->html .= '<h2>Objednávky</h2><table class="seznam" style="margin:5px auto;" cellspacing="0">';  	
  	$this->html .= '<thead><tr><th class="a_c"> Stav </th><th class="a_c"> Počet objednávek </th></tr></thead>';
  	foreach($objednavky as $item) {
  		$this->html .= '<tbody><tr><td>'.$item['S_NAZEV'].'</td><td class="a_c">'.$item['POCET'].'</td></tr>';
  	}
  	$this->html .= '</table>';
  	unset($produkty);

		$this->setView();

  }

  public function prihlasit($args) {

    $form_id = 'loginForm';
    if ($this->registry->user->login) {
    	return $this->index($args);
    }

    if ($this->registry->form_send) {
      if (Validate::check($_POST['auth_mail'],'email') AND Validate::check($_POST['auth_heslo'], 'required')) {
        if ($this->registry->user->login($_POST['auth_mail'], $_POST['auth_heslo']) == true) {

          $this->setMessage('Přihlášení proběhlo úspěšně', CSS_MESS, 1);

          if (isset($this->registry->next_request)) {  // pokud byl původní požedavek na nějakou akc
            $next_request = $this->registry->next_request;
            $key = array_search(SEND,$next_request);  // zruší příznak SEND, pokud je nastaven    
            if ($key != false) {
              unset($next_request[$key]);
            }
            $url = implode(DIRSEP, $next_request);
            unset($this->registry->next_request);
          }
          else {
            $url = 'admin/index';
          }
          Tools::redirect(URL.$url);
          die;

        }
        else {
          $this->setMessage('Špatné přihlašovací údaje', CSS_ERR);
        }
      }
      else {
        $valid_err = Validate::error();
        $this->setMessage('Špatně vyplněné údaje'.$valid_err, CSS_ERR);
      }
    }
//    $js = new Jscript();
    $this->registry->jscript->addScript('$("#auth_mail").focus();','document_ready');

    $form = new Form($form_id, '', 'post', array('class' => 'loginForm'));
    $form->addLabel('E-mail', 'auth_mail' );
    $form->addInput('text', 'auth_mail', @$_POST['auth_mail'], array('class' => 'trida'), array('required' => 'true', 'email' => 'true'));
    $form->addTag('br');
    $form->addLabel('Heslo', 'auth_heslo' );
    $form->addInput('password', 'auth_heslo','', array('class' => 'trida'), array('required' => 'true'));
    $form->openTag('div', array('class' => 'bb_c') );
    $form->addInput('submit', 'send', 'Přihlásit se');
    $form->openTag('a', array('href' => '<tag:url_ztracene_heslo />', 'class' => 'button'));
    $form->addInnerHTML('Ztracené heslo');
    $form->closeTag('a');
    $form->closeTag();
    $form->closeForm();
    $this->registry->template->setHTML('loginform', $form->get());

    $this->registry->template->set('url_ztracene_heslo', URL.'uzivatel'.DIRSEP.'ztraceneheslo');
    $this->setView('login.tpl');

  }

  public function odhlasit($args) {
    $this->registry->user->neregistrovany();
    Tools::redirect(URL.'admin/index');
  }

  public function ajaxvalidate($args) {

    switch(@$_POST['akce']) {
      /*** funkce pro kategorie ****/
      case 'getKatURL':
        $return = Kategorie::getUrl(@$_POST['nazev']);
        if ( $return == false ) {
          $return = 'FALSE';
        }
        break;

        /*** funkce pro produkty ****/
      case 'getProdURL':
        $return = Produkt::getUrl(@$_POST['nazev']);
        if ( $return == false ) {
          $return = 'FALSE';
        }
        break;

      case 'getTax':
        $dan = new Dan();
        $dan->loadById(@$_POST['id']);
        $return = $dan->koeficient;
        if ( $return == false ) {
          $return = 'FALSE';
        }
        break;
        
      case 'getAtribValue':
        $result = dibi::query('SELECT * FROM HODNOTA_ATRIBUTU WHERE ID_ATR = %i AND STAV = 1 ORDER BY HODNOTA', $_POST['id']);
        $return = '';
        if (count($result) > 0) {
        	foreach($result as $item) {
        		$return .= '<option value="'.$item['ID'].'" ondblclick="addValue();">'.$item['HODNOTA'].'</option>';
        	}
        }
        else {
        	$return .= '<option value="">Neexistují žádné hodnoty</option>';
        }
        break;
    }

    die(@$return);

  }

	public function lovrequest($args) {

		$table_id = 'LOV';
		
    $this->registry->jscript->addJqFunction('"#'.$table_id.'"', 'tablesorter','','','','','','document_ready');

    $filtry = array(
          'ID' => array(
          	'type' => 'text',
    				'key'	 => 'ID',
    				'sql'	 => 'num',
        		'size' => 3
        	),
          'NAZEV' => array(
          	'type' => 'text',
        		'key'	 => 'NAZEV',
        		'sql'	 => 'text',
        		'size' => 12
        	),
          'VYBRAT' => array(
          	'type' => 'buttons',
        		'key'	 => '',
        	)
    );
    $filtr_query = array();
    array_push($filtr_query,'WHERE 1');
    foreach($filtry as $filtr) {
			if (!empty($_POST[$filtr['key']]) OR is_numeric(@$_POST[$filtr['key']])) {
      	if ($filtr['sql'] == 'num') {
        	array_push($filtr_query,'AND E.'.$filtr['key'].' = %i',$_POST[$filtr['key']]);
        }
        elseif ($filtr['sql'] == 'text') {
        	array_push($filtr_query,'AND E.'.$filtr['key'].' LIKE %s',"%".$_POST[$filtr['key']]."%");
        }
    	}
    }

    $this->item_to_page = 10;
		$typ = @$args[0];
		switch($typ) {
			case 'vyrobci':
				$entita = new Vyrobce();
				$query = $this->getLovQuery('VYROBCE');
				$nadpis = 'Vyberte výrobce';
				break;
		}
	
  	$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  		<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cs" lang="cs">
  		<head>
  		<meta name="robots" content="noindex, nofollow" />
  		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  		<link type="text/css" rel="stylesheet" href="/templates/admin/styles/style.css" />
  		<script type="text/javascript" src="http://mvc/jscript/jquery-1.3.2.min.js"></script>
  		<script type="text/javascript">
  		$(document).ready(function() {
  			$("table tr.zaznamy").click(function() {
  				var ftr = $(this).find(":first");
  				var name = ftr.next().html();
  				var id = ftr.text();
  				parent.setLOV(id,name);
				});
			});
  		</script>
  		</head><body><h1 class="lov">'.$nadpis.':</h1><div id="lov">';

    $filtr_query = array();
    $this->setFilter($filtr_query, $entita);
    $this->myOrder($filtr_query, $entita);
    $strankovani = $this->addLimit($query, $filtr_query ,$entita);
		$query = array_merge($query,$filtr_query);

		$result = dibi::query($query);
    $zaznamy = $result->fetchAll();
		array_unshift($zaznamy, array('ID' => '&nbsp;', 'NAZEV' => 'Neuvedeno', 'VYBRAT' => ''));
    array_walk($zaznamy,
      'Tools::addLink',
      array(
        'VYBRAT' => array(
          'link' => '#',
          'addindex' => 'ID',
          'text'	 => 'Vybrat'
        )
      )
    );

		$hlavicka = array('ID' => 'Id',
			 								'NAZEV' => 'Název',
			 								'VYBRAT' => ''
		);

    $obsah_css = array(
      'ID' 			=> 'a_c',
    	'VYBRAT' => 'a_c'
    );

    $hlavicka_css = array(
      'ID' 		=> 'a_c',
    	'NAZEV'	=> 'a_c'
    );
    $this->thCss($hlavicka_css);

    $html .= $this->getTable($table_id, $hlavicka, $zaznamy,
      array('class' => 'seznam', 'summary' => $this->controller_name ),
      $obsah_css, array('ID', 'VYBRAT'), $hlavicka_css, 'Položku vyberete kliknutím  na příslušný řádek', $filtry, false
    );
		$html .= '<br /><div class="a_c"><input type="button" value="Zrušit" onclick="parent.closeLOV();"></div>
			<div class="strankovani">'.$strankovani.'</div></form>';

  	echo $html;
  	die;
	}
}
?>