Changeset 69 for trunk/Application


Ignore:
Timestamp:
Feb 28, 2016, 10:54:30 AM (9 years ago)
Author:
chronos
Message:
  • Modified: Use object oriented approach for page drawing using Application class.
  • Added: SQL updated will be automatic using UpdateTrace.php file.
  • Added: Use generic setup page at URL /setup for SQL structure update.
  • Modified: Update Common package to newer version.
Location:
trunk/Application
Files:
3 added
3 moved

Legend:

Unmodified
Added
Removed
  • trunk/Application/Core.php

    r68 r69  
    11<?php
    22
    3 class Application
     3$ConfigFileName = dirname(__FILE__).'/../Config/Config.php';
     4if(file_exists($ConfigFileName)) include_once($ConfigFileName);
     5
     6include_once(dirname(__FILE__).'/../Global.php');
     7include_once(dirname(__FILE__).'/Version.php');
     8include_once(dirname(__FILE__).'/View.php');
     9include_once(dirname(__FILE__).'/UpdateTrace.php');
     10include_once(dirname(__FILE__).'/DefaultConfig.php');
     11
     12class Core extends Application
    413{
    5   var $Database;
     14  var $Pages;
     15  var $ShowPage;
     16  var $BaseURL;
    617
    7   function Start()
     18  function __construct()
     19  {
     20    parent::__construct();
     21    $this->Pages = array();
     22    $this->ShowPage = true;
     23    $this->BaseURL = $_SERVER['SCRIPT_NAME'];
     24    if(substr($this->BaseURL, -10, 10) == '/index.php')
     25      $this->BaseURL = substr($this->BaseURL, 0, -10);
     26  }
     27
     28  function RunCommon()
    829  {
    930    global $Config, $DatabaseRevision;
    10 
    11     $FileName = dirname(__FILE__).'/config.php';
    12     if(file_exists($FileName)) include_once($FileName);
    13     else {
    14       die('Configuration file "'.$FileName.'" not found.');
    15     }
    1631
    1732    session_start();
     
    3348    foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);
    3449
    35     // Check database persistence structure
    36     $UpdateManager = new UpdateManager();
    37     $UpdateManager->Database = $this->Database;
    38     $UpdateManager->Revision = $DatabaseRevision;
    39     if(!$UpdateManager->IsInstalled()) die('Systém vyžaduje instalaci databáze.');
    40     if(!$UpdateManager->IsUpToDate()) die('Systém vyžaduje aktualizaci databáze.');
     50    $this->Config = &$Config;
    4151
     52    // Register and start existing modules
     53    $this->Setup = new Setup($this);
     54    $this->Setup->Start();
     55    if($this->Setup->CheckState())
     56    {
     57      $this->ModuleManager->Start();
     58    }
     59  }
     60
     61  function Run()
     62  {
     63    $this->RunCommon();
     64    if($this->ShowPage)
     65    {
     66      $this->PathItems = ProcessURL();
     67      $this->ShowPage();
     68    }
     69  }
     70
     71  function ShowPage()
     72  {
     73    $this->BaseView = new BaseView($this);
     74
     75    /* @var $Page Page */
     76    $ClassName = $this->SearchPage($this->PathItems, $this->Pages);
     77    if($ClassName != '')
     78    {
     79      $Page = new $ClassName($this);
     80    } else {
     81      $Page = new PageMissing($this);
     82    }
     83    echo($this->BaseView->GetOutput($Page));
     84  }
     85
     86  function RegisterPage($Path, $Handler)
     87  {
     88    if(is_array($Path))
     89    {
     90      $Page = &$this->Pages;
     91      $LastKey = array_pop($Path);
     92      foreach($Path as $PathItem)
     93      {
     94        $Page = &$Page[$PathItem];
     95      }
     96      if(!is_array($Page)) $Page = array('' => $Page);
     97      $Page[$LastKey] = $Handler;
     98    } else $this->Pages[$Path] = $Handler;
     99  }
     100
     101  function UnregisterPage($Path)
     102  {
     103    unset($this->Pages[$Path]);
     104  }
     105
     106  function SearchPage($PathItems, $Pages)
     107  {
     108    if(count($PathItems) > 0) $PathItem = $PathItems[0];
     109      else $PathItem = '';
     110    if(array_key_exists($PathItem, $Pages))
     111    {
     112      if(is_array($Pages[$PathItem]))
     113      {
     114        array_shift($PathItems);
     115        return($this->SearchPage($PathItems, $Pages[$PathItem]));
     116      } else return($Pages[$PathItem]);
     117    } else return('');
     118  }
     119
     120  function Link($Target)
     121  {
     122    return($this->BaseURL.$Target);
    42123  }
    43124}
     125
     126class PageMissing extends Page
     127{
     128  function __construct($System)
     129  {
     130    parent::__construct($System);
     131    $this->ParentClass = '';
     132    $this->Title = T('Page not found');
     133  }
     134
     135  function Show()
     136  {
     137    Header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
     138    return('<h3 align="center">'.T('Required page not found'));
     139  }
     140}
  • trunk/Application/UpdateTrace.php

    r68 r69  
    11<?php
    22
    3 class UpdateTrace
     3function FullInstall($Manager)
    44{
    5   var $Manager;
    6 
    7   function FullInstall($Manager)
    8   {
    9     $this->Manager->Execute('CREATE TABLE IF NOT EXISTS `SystemVersion` (
     5   $Manager->Execute('CREATE TABLE IF NOT EXISTS `SystemVersion` (
    106  `Id` int(11) NOT NULL AUTO_INCREMENT,
    117  `Revision` int(11) NOT NULL,
    128  PRIMARY KEY (`Id`)
    139) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
    14     $this->Manager->Execute('INSERT INTO `SystemVersion` (`Id`, `Version`) VALUES (NULL, 65);');
    15   }
     10  $Manager->Execute('INSERT INTO `SystemVersion` (`Id`, `Revision`) VALUES (NULL, 65);');
     11  $Manager->Execute('CREATE TABLE IF NOT EXISTS `measure` (
     12  `Id` int(11) NOT NULL auto_increment,
     13  `Name` varchar(255) collate utf8_general_ci NOT NULL,
     14  `Description` varchar(255) collate utf8_general_ci NOT NULL,
     15  `Divider` int(11) NOT NULL default 1,
     16  `Unit` varchar(16) collate utf8_general_ci NOT NULL,
     17  `Continuity` tinyint(1) NOT NULL default 0,
     18  `Period` int(11) NOT NULL default 60,
     19  `OldName` varchar(32) collate utf8_general_ci NOT NULL,
     20  `PermissionView` varchar(255) collate utf8_general_ci NOT NULL default "all",
     21  `PermissionAdd` varchar(255) collate utf8_general_ci NOT NULL default "localhost.localdomain",
     22  `Info` varchar(255) collate utf8_general_ci NOT NULL,
     23  `Enabled` int(11) NOT NULL default 1,
     24  `Cumulative` int(11) NOT NULL default 0,
     25  `DataTable` varchar(32) collate utf8_general_ci NOT NULL default "data",
     26  `DataType` varchar(32) collate utf8_general_ci NOT NULL,
     27  PRIMARY KEY  (`Id`)
     28) ENGINE=InnoDB  DEFAULT CHARSET=utf8;');
     29}
    1630
    17   function UpdateTo67($Manager)
    18   {
    19     $this->Manager->Execute('RENAME TABLE `measure` TO `Measure`;');
    20     $this->Manager->Execute('CREATE TABLE IF NOT EXISTS `Permission` (
     31function UpdateTo67($Manager)
     32{
     33  $Manager->Execute('RENAME TABLE `measure` TO `Measure`;');
     34/*
     35   $Manager->Execute('CREATE TABLE IF NOT EXISTS `Permission` (
    2136  `Id` int(11) NOT NULL AUTO_INCREMENT,
    2237  `Measure` int(11) NOT NULL,
     
    2641  KEY `Measure` (`Measure`)
    2742) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
    28     $this->Manager->Execute('ALTER TABLE `Permission` ADD FOREIGN KEY (`Measure`) '.
    29     'REFERENCES `Measure`(`Id`) ON DELETE RESTRICT ON UPDATE RESTRICT;');
    30     $DbResult = $this->Manager->Execute('SELECT `Id`, `PermissionAdd` FROM `Measure`');
    31     while($DbRow = $DbResult->fetch_assoc())
    32     {
    33           $this->Manager->Execute('INSERT INTO `Permission` (`Measure`, `Operation`) VALUES ('.
     43  $Manager->Execute('ALTER TABLE `Permission` ADD FOREIGN KEY (`Measure`) '.
     44  'REFERENCES `Measure`(`Id`) ON DELETE RESTRICT ON UPDATE RESTRICT;');
     45  $DbResult = $Manager->Execute('SELECT `Id`, `PermissionAdd` FROM `Measure`');
     46  while($DbRow = $DbResult->fetch_assoc())
     47  {
     48    $Manager->Execute('INSERT INTO `Permission` (`Measure`, `Operation`) VALUES ('.
    3449            $DbRow['Id'].', "add");');
    3550        }
    36     $DbResult = $this->Manager->Execute('SELECT `Id`, `PermissionView` FROM `Measure`');
    37     while($DbRow = $DbResult->fetch_assoc())
    38     {
    39           $this->Manager->Execute('INSERT INTO `Permission` (`Measure`, `Operation`) VALUES ('.
     51  $DbResult = $Manager->Execute('SELECT `Id`, `PermissionView` FROM `Measure`');
     52  while($DbRow = $DbResult->fetch_assoc())
     53  {
     54          $Manager->Execute('INSERT INTO `Permission` (`Measure`, `Operation`) VALUES ('.
    4055            $DbRow['Id'].', "view");');
    41         }
    42         //$this->Manager->Execute('');
    4356  }
     57  */
     58}
    4459
    45   function __construct()
     60class Updates
     61{
     62  function Get()
    4663  {
    47     $this->Updates = array(
     64    return(array(
    4865      65 => array('Revision' => 67, 'Function' => 'UpdateTo67'),
    49     );
     66    ));
    5067  }
    5168}
  • trunk/Application/Version.php

    r68 r69  
    88$Revision = 67;
    99$DatabaseRevision = 67;
    10 $ReleaseTime = strtotime('2016-01-03');
     10$ReleaseTime = strtotime('2016-02-28');
Note: See TracChangeset for help on using the changeset viewer.