Changeset 64 for trunk/index.php


Ignore:
Timestamp:
Jan 1, 2016, 2:05:17 PM (8 years ago)
Author:
chronos
Message:
  • Modified: Basic application related code moved from Global.php to separate file Application.php.
  • Modified: Main index page converted to PHP class.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/index.php

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