Changeset 69 for trunk/index.php


Ignore:
Timestamp:
Feb 28, 2016, 10:54:30 AM (8 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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/index.php

    r67 r69  
    11<?php
    2 include('Global.php');
    32
    4 class MainView
    5 {
    6   var $Months;
    7   var $GrafTimeRanges;
     3include_once('Application/Core.php');
    84
    9   function __construct($Database)
    10   {
    11     $this->Database = &$Database;
    12     $this->Months = array('', 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září',
    13       'Říjen', 'Listopad', 'Prosinec');
    14 
    15     $this->GrafTimeRanges = array(
    16       'hour' => array(
    17         'caption' => 'Hodina',
    18         'period' => 3600,
    19       ),
    20       'day' => array(
    21         'caption' => 'Den',
    22         'period' => 3600 * 24,
    23       ),
    24       'week' => array(
    25         'caption' => 'Týden',
    26         'period' => 3600 * 24 * 7,
    27       ),
    28       'month' => array(
    29         'caption' => 'Měsíc',
    30         'period' => 3600 * 24 * 30,
    31       ),
    32       'year' => array(
    33         'caption' => 'Rok',
    34         'period' => 3600 * 24 * 365,
    35       ),
    36       'years' => array(
    37         'caption' => 'Desetiletí',
    38         'period' => 3600 * 24 * 365 * 10,
    39       ),
    40     );
    41   }
    42 
    43   function EditTime($Time)
    44   {
    45     $Output = '<form style="display: inline;" action="?Operation=SetTime&amp;Time='.$Time.'" method="post">';
    46     $TimeParts = getdate($_SESSION[$Time]);
    47 
    48     // Day selection
    49     $Output .= '<select name="Day">';
    50     for($I = 1; $I < 32; $I++)
    51     {
    52       if($I == $TimeParts['mday']) $Selected = ' selected="1"'; else $Selected = '';
    53       $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
    54     }
    55     $Output .= '</select>. ';
    56 
    57     // Month selection
    58     $Output .= '<select name="Month">';
    59     foreach($this->Months as $Index => $Month)
    60     {
    61       if($Index == $TimeParts['mon']) $Selected = ' selected="1"'; else $Selected = '';
    62       if($Index > 0) $Output .= '<option value="'.$Index.'"'.$Selected.'>'.$Month.'</option>';
    63     }
    64     $Output .= '</select>. ';
    65 
    66     // Year selection
    67     $Output .= '<select name="Year">';
    68     for($I = 2000; $I <= date("Y"); $I++)
    69     {
    70       if($I == $TimeParts['year']) $Selected = ' selected="1"'; else $Selected = '';
    71       $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
    72     }
    73     $Output .= '</select> &nbsp;&nbsp; ';
    74 
    75     // Hour selection
    76     $Output .= '<select name="Hour">';
    77     for($I = 0; $I < 24; $I++)
    78     {
    79       if($I == $TimeParts['hours']) $Selected = ' selected="1"'; else $Selected = '';
    80       $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
    81     }
    82     $Output .= '</select> : ';
    83 
    84     // Minute selection
    85     $Output .= '<select name="Minute">';
    86     for($I = 0; $I < 60; $I++)
    87     {
    88       if($I == $TimeParts['minutes']) $Selected = ' selected="1"'; else $Selected = '';
    89       $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
    90     }
    91     $Output .= '</select> ';
    92 
    93     $Output .= '<input type="submit" value="Nastavit">';
    94     $Output .= '</form>';
    95     $Output .= ' <form style="display: inline;" action="?Operation=SetTimeNow&amp;Time='.$Time.'" method="post">';
    96     $Output .= '<input type="submit" value="Aktuální čas">';
    97     $Output .= '</form>';
    98 
    99     return($Output);
    100   }
    101 
    102   /* Produce table with available measures */
    103   function ShowMeasureTable()
    104   {
    105     $PrefixMultiplier = new PrefixMultiplier();
    106     $Output = '<table border="1" cellspacing="0" cellpadding="2" style="font-size: small; margin: 0px auto;">';
    107     $Output .= '<tr><th>Měřená veličina</th><th>Poslední hodnota</th><th>Čas posledního měření</th><th>Interpolace</th><th>Poznámky</th>';
    108     if(array_key_exists('Debug', $_GET)) $Output .= '<th>Počet položek</th><th>Čas vykonání</th>';
    109     $Output .= '</tr>';
    110     $Result = $this->Database->select('measure', '*', '(Enabled=1) AND ((PermissionView="all") OR (PermissionView="'.gethostbyaddr($_SERVER['REMOTE_ADDR']).'")) ORDER BY Description');
    111     while($Measure = $Result->fetch_array())
    112     {
    113       $StopWatchStart = GetMicrotime();
    114       if(array_key_exists('Debug', $_GET))
    115       {
    116         $DbResult = $this->Database->select($Measure['DataTable'], 'COUNT(*)', 'measure='.$Measure['Id']);
    117         $RowCount = $DbResult->fetch_array();
    118         $RowCount = $RowCount[0];
    119       }
    120       $Result2 = $this->Database->select($Measure['DataTable'], 'time, avg', '(measure='.$Measure['Id'].') AND (level=0) ORDER BY time DESC LIMIT 1');
    121       if($Result2->num_rows > 0)
    122       {
    123         $Row = $Result2->fetch_array();
    124         $LastMeasureTime = date('j.n.Y G:i:s', MysqlDateTimeToTime($Row['time']));
    125         $LastMeasureValue = $PrefixMultiplier->Add($Row['avg'], $Measure['Unit']);
    126       } else {
    127         $LastMeasureTime = '&nbsp;';
    128         $LastMeasureValue = '&nbsp;';
    129       }
    130       if($Measure['Continuity'] == 1) $Interpolate = 'Ano'; else $Interpolate = 'Ne';
    131       if($Measure['Info'] == '') $Measure['Info'] = '&nbsp;';
    132       $GenerationTime = floor((GetMicrotime() - $StopWatchStart) * 1000  ) / 1000;
    133       $Output .= '<tr><td style="text-align: left"><a href="?Measure='.$Measure['Id'].'&amp;Differential=0">'.$Measure['Description'].'</a></td><td align="center">'.$LastMeasureValue.'</td><td align="center">'.$LastMeasureTime.'</td><td align="center">'.$Interpolate.'</td><td>'.$Measure['Info'].'</td>';
    134       if(array_key_exists('Debug', $_GET)) $Output .= '<td>'.$RowCount.'</td><td>'.$GenerationTime.'</td>';
    135       $Output .= '</tr>';
    136     }
    137     $Output .= '</table>';
    138     return($Output);
    139   }
    140 
    141   function ShowGraph()
    142   {
    143     $Output = '<strong>Graf:</strong><br>';
    144     $Output .= '<img alt="Graf" src="Graph.php?Measure='.$_SESSION['Measure'].'&amp;From='.
    145       $_SESSION['TimeStart'].'&amp;To='.$_SESSION['TimeEnd'].
    146       '&amp;Width=750&amp;Height=200&amp;Differential='.
    147       $_SESSION['Differential'].'" width="750" height="200"><br>';
    148     $Output .= '<a href="?Measure='.$_SESSION['Measure'].'&amp;TimeStart='.
    149       $_SESSION['TimeStart'].'&amp;TimeEnd='.$_SESSION['TimeEnd'].
    150       '&amp;TimeSpecify=1&amp;Differential='.$_SESSION['Differential'].
    151       '">Odkaz na vybraný graf</a><br/>';
    152     return($Output);
    153   }
    154 
    155   function ShowTimeRange()
    156   {
    157     $Output = '';
    158     if($_SESSION['TimeSpecify'] == 0)
    159     {
    160       $_SESSION['TimeEnd'] = time() - 60;
    161       $_SESSION['TimeStart'] = $_SESSION['TimeEnd'] - $this->GrafTimeRanges[$_SESSION['Period']]['period'];
    162     }
    163 
    164     if(!array_key_exists('Operation', $_GET)) $_GET['Operation'] = '';
    165     switch($_GET['Operation'])
    166     {
    167       case 'SetTime':
    168         if(array_key_exists('Time', $_GET) and array_key_exists('Month', $_POST) and array_key_exists('Day', $_POST) and
    169         array_key_exists('Year', $_POST) and array_key_exists('Hour', $_POST) and array_key_exists('Minute', $_POST))
    170         {
    171           if(($_GET['Time'] == 'TimeStart') or ($_GET['Time'] == 'TimeEnd'))
    172           {
    173             $_SESSION[$_GET['Time']] = mktime($_POST['Hour'], $_POST['Minute'], 0, $_POST['Month'],
    174                 $_POST['Day'], $_POST['Year']);
    175             $$_GET['Time'] = $_SESSION[$_GET['Time']];
    176           }
    177         }
    178         break;
    179       case 'SetTimeNow':
    180         if(array_key_exists('Time', $_GET))
    181         {
    182           if(($_GET['Time'] == 'TimeStart') or ($_GET['Time'] == 'TimeEnd'))
    183           {
    184             $_SESSION[$_GET['Time']] = time();
    185             $$_GET['Time'] = $_SESSION[$_GET['Time']];
    186           }
    187         }
    188         break;
    189 
    190     }
    191     $Output .= '<strong>Časový úsek:</strong><br>';
    192 
    193     // Show graph time range menu
    194     if($_SESSION['TimeSpecify'] == 0)
    195     {
    196       $Output .= 'Délka úseku: ';
    197       foreach($this->GrafTimeRanges as $Index => $Item)
    198         $Output .= '<a href="?Period='.$Index.'">'.$Item['caption'].'</a>&nbsp;';
    199       $Output .= '<br>';
    200       $Output .= '<a href="?TimeSpecify=1">Přesnější nastavení...</a><br>';
    201     } else {
    202       $Output .= '<table cellspacing="0" cellpadding="2" border="0" style="margin: 0px auto;">';
    203       $Output .= '<tr><td>Počátek:</td><td>'.$this->EditTime('TimeStart').'</td></tr>';
    204       $Output .= '<tr><td>Konec:</td><td>'.$this->EditTime('TimeEnd').'</td></tr>';
    205       $Output .= '</table>';
    206       $Output .= '<a href="?TimeSpecify=0">Jednoduché nastavení...</a><br>';
    207     }
    208     $Output .= '<br/>';
    209     return($Output);
    210 
    211   }
    212 
    213   function Show()
    214   {
    215     global $Config;
    216 
    217     $Debug = 0;
    218     foreach($Config['DefaultVariables'] as $Index => $Variable)
    219     {
    220       if(!array_key_exists($Index, $_SESSION)) $_SESSION[$Index] = $Variable;
    221       if(array_key_exists($Index, $_GET)) $_SESSION[$Index] = $_GET[$Index];
    222       if(array_key_exists($Index, $_POST)) $_SESSION[$Index] = $_POST[$Index];
    223       //$$Index = $_SESSION[$Index];
    224     }
    225 
    226     $Output = '<div style="text-align: center"><div class="Title">'.$Config['Web']['Title'].'</div>';
    227 
    228     $Result = $this->Database->select('measure', 'Id', '(`Enabled`=1) AND '.
    229         '((`PermissionView`="all") OR (`PermissionView`="'.gethostbyaddr($_SERVER['REMOTE_ADDR']).'")) '.
    230         'AND (`Id`='.($_SESSION['Measure'] * 1).')');
    231     if($Result->num_rows == 0)
    232       $_SESSION['Measure'] = $Config['DefaultVariables']['Measure'];
    233 
    234     $Output .= $this->ShowTimeRange();
    235     $Output .= $this->ShowGraph();
    236     $Output .= '<br/>';
    237     $Output .= $this->ShowMeasureTable();
    238     $Output .= '</div>';
    239 
    240     ShowPage($Output);
    241   }
    242 }
    243 
    244 $Application = new Application();
    245 $Application->Start();
    246 $MainView = new MainView($Application->Database);
    247 $MainView->Show();
     5$Core = new Core();
     6$Core->Run();
Note: See TracChangeset for help on using the changeset viewer.