ACC SHELL

Path : /www/hosting/oltv.cz/www/_class/
File Upload :
Current File : /www/hosting/oltv.cz/www/_class/log__navstevy.php

<?php
/**
* @author Filip Štencl
* @since 29. 09. 2015
*/

class log__navstevy extends base {
   const TABLE_NAME = 'log__navstevy';

   private $id = null;
   private $caszalozeni = null;
   private $caszmeny = null;
	private $typ = null;
   private $pomid = null;
   private $visitor_referer = null;
   private $visitor_ip = null;
   private $visitor_user_agent = null;
   private $chyba = null;

   public function __construct($id = null) {
      if (!is_null($id) && intval($id) != 0) { // Pokud již položka existuje, zpřístupní její vlastnosti
         $result = dibi::query('SELECT * FROM ['.self::TABLE_NAME.'] WHERE id=%i',intval($id));
         if($result->count() > 0) {
            $row = $result->fetch();
            $this->id = $row->id;
            $this->caszalozeni = $row->caszalozeni;
            $this->caszmeny = $row->caszmeny;
				$this->typ = $row->typ;
            $this->pomid = $row->pomid;
            $this->visitor_referer = $row->visitor_referer;
            $this->visitor_ip = $row->visitor_ip;
            $this->visitor_user_agent = $row->visitor_user_agent;
         } else { notification::infoBox_error("Požadovaná položka nebyla nalezena"); set404(); }
      }
   }

   public function getId() { return $this->id; }
   public function getCaszalozeni() { return $this->caszalozeni; }
   public function getCaszmeny() { return $this->caszmeny; }
	public function getTyp() { return $this->typ; }
   public function getPomid() { return $this->pomid; }
   public function getVisitor_referer() { return $this->visitor_referer; }
   public function getVisitor_ip() { return $this->visitor_ip; }
   public function getVisitor_user_agent() { return $this->visitor_user_agent; }

   public function setId($id) { $this->id = $id; }
   protected function getChyba() { return $this->chyba; }
   protected function checkData($data) {
      if(array_key_exists("id",$data)) {if(intval($data['id']) > 0) { $this->id = intval($data['id']); } else { $this->chyba .= "ID je v nesprávném formátu<br />"; return false; }}
      return true;
   }

	public static function log($typ, $pomid) {
		// kontrola, zdali v session již nemám návštěvu...
		if (!isset($_SESSION["navstivene_url"][$typ]) || (isset($_SESSION["navstivene_url"][$typ]) && !in_array(trim($pomid),$_SESSION["navstivene_url"][$typ]))) {
			$_SESSION["navstivene_url"][$typ][] = $pomid;

			$arr = array(
				 "typ" => $typ,
				 "pomid" => $pomid,
				 "visitor_referer" => @$_SERVER['HTTP_REFERER'],
				 "visitor_ip" => @$_SERVER['REMOTE_ADDR'],
				 "visitor_user_agent" => @$_SERVER['HTTP_USER_AGENT'],
			);
			dibi::query("INSERT INTO [".self::TABLE_NAME."]",$arr);
		}
		return true;
	}

   public static function ulozDoTabulek() {
      // projdu groupnutou db a počty uložím do pole $arr[typ][pomid][pocet]
      $result = dibi::query("SELECT count(1) as pocet, pomid, typ FROM [".self::TABLE_NAME."] GROUP BY typ, pomid");
      foreach($result as $row) { $arr[$row->typ][$row->pomid] = $row->pocet; }

      // procházím jednotlivé typy z pole $arr
      foreach($arr as $typ => $arr_typ) {
         if (count(dibi::query("SHOW TABLES LIKE %s",$typ)) > 0) {
            $desribe = dibi::query("DESCRIBE [".$typ."]");
            foreach($desribe as $sloupec) { // pokud má tabulka sloupec návštěvy, tak ukládám...
               if ($sloupec['Field'] == "navstevy") {
                  $arr_ids = array();
                  $sql = array();
                  $sql[] = "UPDATE [".$typ."] SET navstevy = CASE [id] ";
                  foreach($arr_typ as $pomid => $item) {
                     $arr_ids[] = $pomid;
                     $sql[] = "WHEN ".$pomid." THEN ".$item." ";
                  }
                  $sql[] = " END WHERE [id] IN %in";
                  $sql[] = $arr_ids;
                  dibi::query($sql);
               }
            }
         }
      }

      return true;
   }

}

ACC SHELL 2018