Index: trunk/Application.php
===================================================================
--- trunk/Application.php	(revision 66)
+++ trunk/Application.php	(revision 67)
@@ -3,7 +3,9 @@
 class Application
 {
+  var $Database;
+
   function Start()
   {
-    global $Config, $Database;
+    global $Config, $DatabaseRevision;
 
     $FileName = dirname(__FILE__).'/config.php';
@@ -19,11 +21,11 @@
     $ErrorHandler->Start();
 
-    $Database = new Database();
-    $Database->Connect($Config['Database']['Host'], $Config['Database']['User'],
+    $this->Database = new Database();
+    $this->Database->Connect($Config['Database']['Host'], $Config['Database']['User'],
       $Config['Database']['Password'], $Config['Database']['Database']);
-    $Database->Prefix = $Config['Database']['Prefix'];
-    $Database->charset($Config['Database']['Charset']);
-    $Database->ShowSQLError = $Config['Web']['ShowSQLError'];
-    $Database->ShowSQLQuery = $Config['Web']['ShowSQLQuery'];
+    $this->Database->Prefix = $Config['Database']['Prefix'];
+    $this->Database->charset($Config['Database']['Charset']);
+    $this->Database->ShowSQLError = $Config['Web']['ShowSQLError'];
+    $this->Database->ShowSQLQuery = $Config['Web']['ShowSQLQuery'];
 
     // SQL injection hack protection
@@ -31,4 +33,11 @@
     foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);
 
+    // Check database persistence structure
+    $UpdateManager = new UpdateManager();
+    $UpdateManager->Database = $this->Database;
+    $UpdateManager->Revision = $DatabaseRevision;
+    if(!$UpdateManager->IsInstalled()) die('Systém vyžaduje instalaci databáze.');
+    if(!$UpdateManager->IsUpToDate()) die('Systém vyžaduje aktualizaci databáze.');
+
   }
 }
Index: trunk/Global.php
===================================================================
--- trunk/Global.php	(revision 66)
+++ trunk/Global.php	(revision 67)
@@ -7,4 +7,5 @@
 include_once('Measure.php');
 include_once('Application.php');
+include_once('Update.php');
 
 function HumanDate($Time)
Index: trunk/Graph.php
===================================================================
--- trunk/Graph.php	(revision 66)
+++ trunk/Graph.php	(revision 67)
@@ -218,4 +218,4 @@
 $Application = new Application();
 $Application->Start();
-$Graph = new MeasureGraph($Database);
+$Graph = new MeasureGraph($this->Database);
 $Graph->Render();
Index: trunk/Update.php
===================================================================
--- trunk/Update.php	(revision 67)
+++ trunk/Update.php	(revision 67)
@@ -0,0 +1,71 @@
+<?php
+
+class UpdateManager
+{
+  var $Revision;
+  var $Trace;
+  var $VersionTable;
+  var $Database;
+  var $InstallMethod;
+
+  function __construct()
+  {
+    $this->Revision = 0;
+    $this->Trace = array();
+    $this->VersionTable = 'SystemVersion';
+    $this->InstallMethod = 'FullInstall';
+  }
+
+  function GetDbVersion()
+  {
+    $DbResult = $this->Database->select($this->VersionTable, '*', '`Id`=1');
+    $Version = $DbResult->fetch_assoc();
+    return($Version['Revision']);
+  }
+
+  function IsInstalled()
+  {
+    $DbResult = $this->Database->query('SHOW TABLES LIKE "'.$this->VersionTable.'"');
+    return($DbResult->num_rows > 0);
+  }
+
+  function IsUpToDate()
+  {
+    return($this->Revision <= $this->GetDbVersion());
+  }
+
+  function Update()
+  {
+    $DbRevision = $this->GetDbVersion();
+    $Output = 'Počáteční revize databáze: '.$DbRevision.'<br/>';
+    while($this->Revision > $DbRevision)
+    {
+      $TraceItem = $this->Trace[$DbRevision];
+      $Output .= 'Aktualizace na verzi: '.$TraceItem['Revision'].'<br/>';
+      $RevUpdate = $TraceItem['Function'];
+      $RevUpdate($this);
+      $DbRevision = $TraceItem['Revision'];
+      $this->Database->query('UPDATE `'.$this->VersionTable.'` SET `Revision`= '.$TraceItem['Revision'].' WHERE `Id`=1');
+    }
+    return($Output);
+  }
+
+  function Install()
+  {
+    $InstallMethod = $this->InstallMethod;
+    $InstallMethod($this);
+    $this->Update();
+  }
+
+  function Uninstall()
+  {
+
+  }
+
+  function Execute($Query)
+  {
+    echo($Query.'<br/>');
+    flush();
+    return($this->Database->query($Query));
+  }
+}
Index: trunk/UpdateTrace.php
===================================================================
--- trunk/UpdateTrace.php	(revision 67)
+++ trunk/UpdateTrace.php	(revision 67)
@@ -0,0 +1,51 @@
+<?php
+
+class UpdateTrace
+{
+  var $Manager;
+
+  function FullInstall($Manager)
+  {
+    $this->Manager->Execute('CREATE TABLE IF NOT EXISTS `SystemVersion` (
+  `Id` int(11) NOT NULL AUTO_INCREMENT,
+  `Revision` int(11) NOT NULL,
+  PRIMARY KEY (`Id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
+    $this->Manager->Execute('INSERT INTO `SystemVersion` (`Id`, `Version`) VALUES (NULL, 65);');
+  }
+
+  function UpdateTo67($Manager)
+  {
+    $this->Manager->Execute('RENAME TABLE `measure` TO `Measure`;');
+    $this->Manager->Execute('CREATE TABLE IF NOT EXISTS `Permission` (
+  `Id` int(11) NOT NULL AUTO_INCREMENT,
+  `Measure` int(11) NOT NULL,
+  `Address` varchar(255) NOT NULL,
+  `Operation` varchar(255) NOT NULL,
+  PRIMARY KEY (`Id`),
+  KEY `Measure` (`Measure`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
+    $this->Manager->Execute('ALTER TABLE `Permission` ADD FOREIGN KEY (`Measure`) '.
+    'REFERENCES `Measure`(`Id`) ON DELETE RESTRICT ON UPDATE RESTRICT;');
+    $DbResult = $this->Manager->Execute('SELECT `Id`, `PermissionAdd` FROM `Measure`');
+    while($DbRow = $DbResult->fetch_assoc())
+    {
+	  $this->Manager->Execute('INSERT INTO `Permission` (`Measure`, `Operation`) VALUES ('.
+	    $DbRow['Id'].', "add");');
+	}
+    $DbResult = $this->Manager->Execute('SELECT `Id`, `PermissionView` FROM `Measure`');
+    while($DbRow = $DbResult->fetch_assoc())
+    {
+	  $this->Manager->Execute('INSERT INTO `Permission` (`Measure`, `Operation`) VALUES ('.
+	    $DbRow['Id'].', "view");');
+	}
+	//$this->Manager->Execute('');
+  }
+
+  function __construct()
+  {
+    $this->Updates = array(
+      65 => array('Revision' => 67, 'Function' => 'UpdateTo67'),
+    );
+  }
+}
Index: trunk/Version.php
===================================================================
--- trunk/Version.php	(revision 66)
+++ trunk/Version.php	(revision 67)
@@ -1,4 +1,10 @@
 <?php
 
-$Revision = 65;
-$ReleaseTime = strtotime('2016-01-01');
+// Subversion revision have to be updated always to next revision number
+// before commiting new changes.
+// Database revision have to be updated if database structure is changed
+// and system will need database update.
+
+$Revision = 67;
+$DatabaseRevision = 67;
+$ReleaseTime = strtotime('2016-01-03');
Index: trunk/add.php
===================================================================
--- trunk/add.php	(revision 66)
+++ trunk/add.php	(revision 67)
@@ -10,5 +10,5 @@
   $MeasureId = addslashes($_GET['MeasureId']);
   $Value = addslashes($_GET['Value']);
-  $Measure = new Measure($Database);
+  $Measure = new Measure($Application->Database);
   $Measure->Load($MeasureId);
   if(gethostbyaddr($_SERVER['REMOTE_ADDR']) == $Measure->Data['PermissionAdd'])
Index: trunk/admin.php
===================================================================
--- trunk/admin.php	(revision 66)
+++ trunk/admin.php	(revision 67)
@@ -5,65 +5,72 @@
 $Application->Start();
 
-if(array_key_exists('Operation', $_GET)) $Operation = $_GET['Operation']; else $Operation = '';
-switch($Operation)
+class AdminPage
 {
-  case 'Add':
-    $Output = ShowAdd();
-    break;
-  case 'Delete':
-    $Output = ShowDelete();
-    break;
-  case 'Edit':
-    $Output = ShowEdit();
-    break;
-  case 'RebuildCache':
-    $Output = ShowRebuildCache();
-    break;
-  default:
-    $Output = ShowNone();
+  function __construct($Database)
+  {
+    $this->Database = &$Database;
+  }
+
+  function Show()
+  {
+    if(array_key_exists('Operation', $_GET)) $Operation = $_GET['Operation']; else $Operation = '';
+    switch($Operation)
+    {
+      case 'Add':
+        $Output = $this->ShowAdd();
+        break;
+      case 'Delete':
+        $Output = $this->ShowDelete();
+        break;
+      case 'Edit':
+        $Output = $this->ShowEdit();
+        break;
+      case 'RebuildCache':
+        $Output = $this->ShowRebuildCache();
+        break;
+      default:
+        $Output = $this->ShowNone();
+    }
+    ShowPage($Output);
+  }
+
+  function ShowNone()
+  {
+    $Table = array(
+      'Header' => array('Název veličiny', 'Operace'),
+      'Rows' => array(),
+    );
+
+    $Result = $this->Database->select('measure', '*', '1 ORDER BY Description');
+    while($Measure = $Result->fetch_array())
+    {
+      array_push($Table['Rows'], array($Measure['Description'], MakeLink('?Operation=Edit&amp;MeasureId='.$Measure['Id'], 'Editovat').' '.MakeLink('?Operation=Delete&amp;MeasureId='.$Measure['Id'], 'Odstranit').' '.MakeLink('?Operation=RebuildCache&amp;MeasureId='.$Measure['Id'], 'Přestavět cache')));
+    }
+    $Output = '<h3>Seznam měření</h3>'.Table($Table).MakeLink('?Operation=Add', 'Přidat');
+    return($Output);
+  }
+
+  function ShowEdit()
+  {
+    $DbResult = $this->Database->select('measure', '*', 'Id='.addslashes($_GET['MeasureId']));
+    $Values = array();
+    $Values = $DbResult->fetch_array();
+    return($this->ShowEditTable('Measure', $Values));
+  }
+
+  function ShowAdd()
+  {
+    $Values = array();
+    return($this->ShowEditTable('Measure', $Values));
+  }
+
+  function ShowRebuildCache()
+  {
+    echo("Vytvařím novou cache...<br>");
+    $DbResult = $this->Database->select('measure', '*', 'Id='.addslashes($_GET['MeasureId']));
+    $Measure = $DbResult->fetch_array();
+
+    $Measure->RebuildMeasureCache();
+    echo('Dokončeno<br>');
+  }
 }
-ShowPage($Output);
-
-function ShowNone()
-{
-  global $Database;
-  $Table = array(
-    'Header' => array('Název veličiny', 'Operace'),
-    'Rows' => array(),
-  );
-
-  $Result = $Database->select('measure', '*', '1 ORDER BY Description');
-  while($Measure = $Result->fetch_array())
-  {
-    array_push($Table['Rows'], array($Measure['Description'], MakeLink('?Operation=Edit&amp;MeasureId='.$Measure['Id'], 'Editovat').' '.MakeLink('?Operation=Delete&amp;MeasureId='.$Measure['Id'], 'Odstranit').' '.MakeLink('?Operation=RebuildCache&amp;MeasureId='.$Measure['Id'], 'Přestavět cache')));
-  }
-  $Output = '<h3>Seznam měření</h3>'.Table($Table).MakeLink('?Operation=Add', 'Přidat');
-  return($Output);
-}
-
-function ShowEdit()
-{
-  global $Database;
-  $DbResult = $Database->select('measure', '*', 'Id='.addslashes($_GET['MeasureId']));
-  $Values = array();
-  $Values = $DbResult->fetch_array();
-  return(ShowEditTable('Measure', $Values));
-}
-
-function ShowAdd()
-{
-  global $Database;
-  $Values = array();
-  return(ShowEditTable('Measure', $Values));
-}
-
-function ShowRebuildCache()
-{
-  global $Database;
-  echo("Vytvařím novou cache...<br>");
-  $DbResult = $Database->select('measure', '*', 'Id='.addslashes($_GET['MeasureId']));
-  $Measure = $DbResult->fetch_array();
-
-  RebuildMeasureCache($Measure);
-  echo('Dokončeno<br>');
-}
Index: trunk/index.php
===================================================================
--- trunk/index.php	(revision 66)
+++ trunk/index.php	(revision 67)
@@ -244,4 +244,4 @@
 $Application = new Application();
 $Application->Start();
-$MainView = new MainView($Database);
+$MainView = new MainView($Application->Database);
 $MainView->Show();
