Changeset 13


Ignore:
Timestamp:
Oct 13, 2008, 1:56:05 PM (16 years ago)
Author:
george
Message:
  • Přidáno: Automatické zaznamenávání historie operací nad seznamy do tabulky SystemLog.
  • Upraveno: Nová třída pro databázovou obsluhu seznamů oddělená od souboru obstarávajícího komunikaci pomocí webu.
  • Opraveno: Chybná podmínka zobrazování skrytých políček formulářů.
  • Přidáno: Zobrazování chyb databázových povelů.
Files:
3 added
6 edited

Legend:

Unmodified
Added
Removed
  • base.php

    r11 r13  
    11<?php
    2 
    3 // Include type definitions
    4 include('types/Enumeration.php');
    5 include('types/Boolean.php');
    6 include('types/Integer.php');
    7 include('types/String.php');
    8 include('types/PointerOneToMany.php');
    9 include('types/PointerOneToOne.php');
    10 include('types/Date.php');
    11 include('types/Password.php');
    12 include('types/Float.php');
    13 include('types/Hyperlink.php');
    14 include('types/Hidden.php');
    15 
    16 // Include list events definitions
    17 include('lists/SystemList.php');
    18 include('lists/SystemListItem.php');
    192
    203function ShowList($List, $Column = '', $ColumnValue = 0, $Title = '')
     
    4427    $FullListLink = '';
    4528  }
     29
     30  // Handle ordering
     31  if(array_key_exists('OrderColumn', $_GET))
     32  {
     33    if($_SESSION['OrderColumn'] == $_GET['OrderColumn']) // Same column => reverse orded
     34      $_SESSION['OrderDirection'] = ($_SESSION['OrderDirection'] + 1) % 2;
     35    if($_SESSION['OrderTable'] != $List['TableName']) // Different table => set ascending order
     36      $_SESSION['OrderDirection'] = 0;
     37    $_SESSION['OrderColumn'] = $_GET['OrderColumn'];
     38    $_SESSION['OrderTable'] = $List['TableName'];
     39  }
     40  if(array_key_exists('OrderColumn', $_SESSION) and ($_SESSION['OrderTable'] == $List['TableName']))
     41  {
     42    $OrderDirection = array('ASC', 'DESC');
     43    $Order = 'ORDER BY t1.`'.$_SESSION['OrderColumn'].'` '.$OrderDirection[$_SESSION['OrderDirection']];
     44  } else $Order = '';
     45
     46  if(array_key_exists('Page', $_GET)) $Page = $_GET['Page']; else $Page = 0;
     47  //$Where .= ' AND (ValidFromTime <= NOW()) AND ((ValidToTime >= NOW()) OR (ValidToTime IS NULL))';
     48  $Where .= ' '.$Order;
     49  $DbResult = $Database->query('SELECT COUNT(t2.Id) AS Count FROM (SELECT DISTINCT(ItemId) as Id FROM `'.$List['TableName'].'` as t1 WHERE 1'.$Where.') as t2');
     50  $DbRow = $DbResult->fetch_row();
     51  $TotalItemCount = $DbRow[0];
     52
     53  $DbResult = $Database->query('SELECT t1.* FROM `'.$List['TableName'].'` AS t1 LEFT JOIN `'.$List['TableName'].'` AS t2 ON t1.ItemId=t2.ItemId AND t1.Id < t2.Id WHERE (t2.ItemId IS NULL)'.$Where.' LIMIT '.($Page * $Config['Web']['ItemsPerPage']).', '.$Config['Web']['ItemsPerPage']);
     54  while($DbRow = $DbResult->fetch_assoc())
     55  {
     56    $Output .= '<tr>';
     57    foreach($List['Items'] as $Index => $Item)
     58    {
     59      if($Item['VisibleInList'] == 1)
     60      {
     61        $ItemDefinition = array('Name' => $Index, 'Value' => $DbRow[$Index], 'SourceTable' => $List['TableName'], 'SourceItemId' => $DbRow['Id'], 'Type' => $Item['Type']);
     62        $Output .= '<td>'.ExecuteTypeEvent($Item['Type'], 'OnView', $ItemDefinition).'</td>';
     63      }
     64    }
     65    $Output .= '<td><a href="?Action=ViewItem&amp;Table='.$List['TableName'].'&amp;Item='.$DbRow['Id'].'">Zobrazit</a> <a href="?Action=EditItem&amp;Table='.$List['TableName'].'&amp;Item='.$DbRow['Id'].'">Editovat</a> <a href="?Action=DeleteItem&amp;Table='.$List['TableName'].'&amp;Item='.$DbRow['Id'].'">Smazat</a></td></tr>';
     66  }
     67  $Output .= '</table>';
     68  $PageList = PagesList($Page, $TotalItemCount);
     69  if(strlen($PageList) != 0) $Output .= $PageList.'<br />';
     70  $Output .= ' <a href="?Action=AddItem&amp;Table='.$List['TableName'].$ColumnSelection.'">Přidat</a> '.$FullListLink;
     71  return($Output);
     72}
     73
     74function ShowHistory($List, $Id, $Title = '')
     75{
     76  global $Database, $Types, $Config;
     77
     78  if($Title == '') $Output = '<div>'.$List['Title'].'</div>';
     79    else $Output = '<div>'.$Title.'</div>';
     80  $Output .= '<table class="WideTable"><tr>';
     81  foreach($List['Items'] as $Item)
     82  {
     83    if($Item['VisibleInList'] == 1)
     84      $Output .= '<th><a href="?OrderColumn='.$Item['Name'].'">'.$Item['TextBefore'].'</a></th>';
     85  }
     86  $Output .= '<th><a href="?OrderColumn=Author">Autor</a></th><th><a href="?OrderColumn=CreationTime">Čas vytvoření</a></th><th>Akce</th></tr>';
     87
     88  $Where = ' AND (ItemId='.$Id.')';
    4689
    4790  // Handle ordering
     
    69112  $TotalItemCount = $DbRow[0];
    70113
    71   $DbResult = $Database->query('SELECT t1.* FROM `'.$List['TableName'].'` AS t1 LEFT JOIN `'.$List['TableName'].'` AS t2 ON t1.ItemId=t2.ItemId AND t1.Id < t2.Id WHERE (t2.ItemId IS NULL)'.$Where.' LIMIT '.($Page * $Config['Web']['ItemsPerPage']).', '.$Config['Web']['ItemsPerPage']);
    72   //echo($Database->LastQuery.'<br>');
    73   while($DbRow = $DbResult->fetch_array())
    74   {
    75     $Output .= '<tr>';
    76     foreach($List['Items'] as $Index => $Item)
    77     {
    78       if($Item['VisibleInList'] == 1)
    79       {
    80         $ItemDefinition = array('Name' => $Index, 'Value' => $DbRow[$Index], 'SourceTable' => $List['TableName'], 'SourceItemId' => $DbRow['Id'], 'Type' => $Item['Type']);
    81         $Output .= '<td>'.ExecuteTypeEvent($Item['Type'], 'OnView', $ItemDefinition).'</td>';
    82       }
    83     }
    84     $Output .= '<td><a href="?Action=ViewItem&amp;Table='.$List['TableName'].'&amp;Item='.$DbRow['Id'].'">Zobrazit</a> <a href="?Action=EditItem&amp;Table='.$List['TableName'].'&amp;Item='.$DbRow['Id'].'">Editovat</a> <a href="?Action=DeleteItem&amp;Table='.$List['TableName'].'&amp;Item='.$DbRow['Id'].'">Smazat</a></td></tr>';
    85   }
    86   $Output .= '</table>';
    87   $Output .= PagesList($Page, $TotalItemCount);
    88   $Output .= '<a href="?Action=AddItem&amp;Table='.$List['TableName'].$ColumnSelection.'">Přidat</a> '.$FullListLink;
    89   return($Output);
    90 }
    91 
    92 function ShowHistory($List, $Id, $Title = '')
    93 {
    94   global $Database, $Types, $Config;
    95 
    96   if($Title == '') $Output = '<div>'.$List['Title'].'</div>';
    97     else $Output = '<div>'.$Title.'</div>';
    98   $Output .= '<table class="WideTable"><tr>';
    99   foreach($List['Items'] as $Item)
    100   {
    101     if($Item['VisibleInList'] == 1)
    102       $Output .= '<th><a href="?OrderColumn='.$Item['Name'].'">'.$Item['TextBefore'].'</a></th>';
    103   }
    104   $Output .= '<th><a href="?OrderColumn=Author">Autor</a></th><th><a href="?OrderColumn=CreationTime">Čas vytvoření</a></th><th>Akce</th></tr>';
    105 
    106   $Where = ' AND (ItemId='.$Id.')';
    107 
    108   // Handle ordering
    109   if(array_key_exists('OrderColumn', $_GET))
    110   {
    111     if($_SESSION['OrderColumn'] == $_GET['OrderColumn']) // Same column => reverse orded
    112       $_SESSION['OrderDirection'] = ($_SESSION['OrderDirection'] + 1) % 2;
    113     if($_SESSION['OrderTable'] != $List['TableName']) // Different table => set ascending order
    114       $_SESSION['OrderDirection'] = 0;
    115     $_SESSION['OrderColumn'] = $_GET['OrderColumn'];
    116     $_SESSION['OrderTable'] = $List['TableName'];
    117   }
    118   if(array_key_exists('OrderColumn', $_SESSION) and ($_SESSION['OrderTable'] == $List['TableName']))
    119   {
    120     $OrderDirection = array('ASC', 'DESC');
    121     $Order = 'ORDER BY t1.`'.$_SESSION['OrderColumn'].'` '.$OrderDirection[$_SESSION['OrderDirection']];
    122   } else $Order = '';
    123 
    124   if(array_key_exists('Page', $_GET)) $Page = $_GET['Page']; else $Page = 0;
    125   //$Where .= ' AND (ValidFromTime <= NOW()) AND ((ValidToTime >= NOW()) OR (ValidToTime IS NULL))';
    126   $Where .= ' '.$Order;
    127   $DbResult = $Database->query('SELECT COUNT(t2.Id) AS Count FROM (SELECT DISTINCT(ItemId) as Id FROM `'.$List['TableName'].'` as t1 WHERE 1'.$Where.') as t2');
    128   //echo($Database->LastQuery.'<br>');
    129   $DbRow = $DbResult->fetch_array();
    130   $TotalItemCount = $DbRow[0];
    131 
    132114  $DbResult = $Database->query('SELECT * FROM `'.$List['TableName'].'` AS t1 WHERE 1'.$Where.' LIMIT '.($Page * $Config['Web']['ItemsPerPage']).', '.$Config['Web']['ItemsPerPage']);
    133115  //echo($Database->LastQuery.'<br>');
     
    156138  $Output .= '</table>';
    157139  $Output .= PagesList($Page, $TotalItemCount);
    158   $Output .= '<a href="?Action=ShowList&amp;Table='.$List['TableName'].'">Celkový seznam</a>';
     140  $Output .= ' <a href="?Action=ShowList&amp;Table='.$List['TableName'].'">Celkový seznam</a>';
    159141  return($Output);
    160142}
     
    197179function ShowEditItemFinish($List, $Id)
    198180{
    199   global $Database, $Types, $System;
     181  global $Database, $Types, $System, $LogActionType;
    200182
    201183  $DbResult = $Database->select($List['TableName'], '*', 'Id='.$Id);
     
    219201  );
    220202  $Form->LoadValuesFromForm();
    221 
    222   $DbResult = $Database->select($List['TableName'], 'ItemId', 'Id='.$Id);
    223   $DbRow = $DbResult->fetch_assoc();
    224   $Form->Values['ItemId'] = $DbRow['ItemId'];
    225   $Form->Values['Author'] = $System->Modules['User']->User['Id'];
    226   $Form->Values['CreationTime'] = 'NOW()';
    227   $Form->Values['ValidTimeFrom'] = 'NOW()';
    228   $Database->insert($List['TableName'], $Form->Values);
    229   //echo($Database->LastQuery);
     203  $System->Modules['DatabaseList']->EditItem($List['TableName'], $Form->Values, $Id);
     204  $NewId = $Database->insert_id;
     205  $System->Modules['Log']->Add($List['Id'], $NewId, $LogActionType['Edit']);
    230206  $Output = 'Změny uloženy.';
    231   $Output .= ShowViewItem($List, $Database->insert_id);
     207  $Output .= ShowViewItem($List, $NewId);
    232208  $Form->Values['Column'] = $_POST['Column'];
    233209  ExecuteListEvent($List['TableName'], 'OnEdit', $Form->Values);
     
    277253function ShowAddItemFinish($List)
    278254{
    279   global $Database, $Types, $System;
     255  global $Database, $Types, $System, $LogActionType;
    280256
    281257  $DefinitionItems = array();
     
    294270    }
    295271  }
    296   $DefinitionItems[] = array('Name' => 'Column', 'Type' => 'Hidden', 'Caption' => '', 'Value' => $Column);
    297272
    298273  $Form = new Form();
     
    304279  );
    305280  $Form->LoadValuesFromForm();
    306   $Form->Values['Author'] = $System->Modules['User']->User['Id'];
    307   $Form->Values['CreationTime'] = 'NOW()';
    308   $Form->Values['ValidTimeFrom'] = 'NOW()';
    309   $DbResult = $Database->select($List['TableName'], 'MAX(ItemId)');
    310   $DbRow = $DbResult->fetch_row();
    311   $AutoincrementId = $DbRow[0];
    312   $Form->Values['ItemId'] = $AutoincrementId + 1;
    313   $Database->insert($List['TableName'], $Form->Values);
    314   //echo($Database->LastQuery);
     281  $System->Modules['DatabaseList']->AddItem($List['TableName'], $Form->Values);
     282  $NewId = $Database->insert_id;
     283  $System->Modules['Log']->Add($List['Id'], $NewId, $LogActionType['Add']);
    315284  $Output = 'Položka přidána';
    316   $Output .= ShowViewItem($List, $Database->insert_id);
     285  $Output .= ShowViewItem($List, $NewId);
    317286  $Form->Values['Column'] = $_POST['Column'];
    318287  ExecuteListEvent($List['TableName'], 'OnAdd', $Form->Values);
     
    368337function ShowDeleteItem($List, $Id)
    369338{
    370   global $Database, $Lists;
    371 
    372   $Database->update($List['TableName'], 'Id='.$Id, array('DeletionTime' => 'NOW()'));
     339  global $Database, $Lists, $System, $LogActionType;
     340
     341  $System->Modules['Log']->Add($List['Id'], $Id, $LogActionType['Delete']);
     342  $System->Modules['DatabaseList']->DeleteItem($List['TableName'], $Id);
    373343  $Output = 'Položka smazána.';
    374344  $Output .= ShowList($Lists[$_SESSION['Table']], $Lists[$_SESSION['Column']], $Lists[$_SESSION['ColumnValue']]).'<br /><br />';
     345  ExecuteListEvent($List['TableName'], 'OnDelete', $Id);
    375346  return($Output);
    376347}
     
    396367
    397368  $PermanentVar = array('Table', 'Item', 'Column', 'ColumnValue', 'Action');
     369  if($_GET['Table'] != $_SESSION['Table'])
     370  {
     371    $_SESSION['Column'] = '';
     372    $_SESSION['ColumnValue'] = '';
     373  }
    398374  foreach($PermanentVar as $Var)
    399375  {
     
    446422  {
    447423    $Items = array();
    448     $DbResult2 = $Database->select('SystemListItem', '`Name`, `TextBefore`, `TextAfter`, `Type`, `Default`, `Help`, `Required`, `Editable`, `VisibleInList`, `VisibleInPointer`', 'List='.$DbRow['Id']);
     424    $DbResult2 = $Database->select('SystemListItem', '`Id`, `Name`, `TextBefore`, `TextAfter`, `Type`, `Default`, `Help`, `Required`, `Editable`, `VisibleInList`, `VisibleInPointer`', 'List='.$DbRow['Id']);
    449425    while($DbRow2 = $DbResult2->fetch_assoc())
    450426    {
     
    452428    }
    453429    $List = array(
     430     'Id' => $DbRow['Id'],
    454431     'TableName' => $DbRow['TableName'],
    455432     'Title' => $DbRow['Title'],
  • database.php

    r9 r13  
    22
    33// Extended database class
    4 // Date: 2007-07-19
     4// Date: 2008-10-13
    55
    66class Database extends mysqli
     
    88  var $Prefix = '';
    99  var $LastQuery = '';
    10  
     10  var $ShowError = 0;
     11
    1112  function query($Query)
    1213  {
    1314    $this->LastQuery = $Query;
    14     return(parent::query($Query));
     15    $DbResult = parent::query($Query);
     16    if(($this->ShowError == TRUE) and ($this->error != ''))
     17    {
     18      echo('<strong>Database error:</strong> '.$this->error.'<br /><strong>Query:</strong> '.$Query.'<br />');
     19      //echo('<pre>'); print_r(debug_backtrace()); echo('</pre>');
     20    }
     21    return($DbResult);
    1522  }
    1623
    1724  function select($Table, $What = '*', $Condition = 1)
    1825  {
    19     $this->LastQuery = "SELECT ".$What." FROM `".$this->Prefix.$Table."` WHERE ".$Condition;
    20     return($this->query($this->LastQuery));
     26    return($this->query("SELECT ".$What." FROM `".$this->Prefix.$Table."` WHERE ".$Condition));
    2127  }
    2228
    2329  function delete($Table, $Condition)
    2430  {
    25     $this->LastQuery = "DELETE FROM `".$this->Prefix.$Table."` WHERE ".$Condition;
    26     $this->query($this->LastQuery);
     31    $this->query("DELETE FROM `".$this->Prefix.$Table."` WHERE ".$Condition);
    2732  }
    28  
     33
    2934  function insert($Table, $Data)
    3035  {
     
    3540      $Value = strtr($Value, '"', '\"');
    3641      $Name .= ',`'.$Key.'`';
    37             if($Value == 'NOW()') $Values .= ",".$Value;
    38               else $Values .= ",'".$Value."'";
     42      if($Value == 'NOW()') $Values .= ",".$Value;
     43        else $Values .= ",'".$Value."'";
    3944    }
    4045    $Name = substr($Name, 1);
    4146    $Values = substr($Values, 1);
    42     $this->LastQuery = 'INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')';
    43     $this->query($this->LastQuery);
     47    $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
    4448  }
    45  
     49
    4650  function update($Table, $Condition, $Data)
    4751  {
     
    4953    foreach($Data as $Key => $Value)
    5054    {
    51             $Value = strtr($Value, '"', '\"');
     55      $Value = strtr($Value, '"', '\"');
    5256      if($Value != 'NOW()') $Value = "'".$Value."'";
    5357      $Values .= ", ".$Key."=".$Value;
    5458    }
    55     $Values = substr($Values, 2); 
    56     $this->LastQuery = 'UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')';
    57     $this->query($this->LastQuery);
     59    $Values = substr($Values, 2);
     60    $this->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
    5861  }
    59  
     62
    6063  function replace($Table, $Data)
    6164  {
     
    7275    $Values = substr($Values, 1);
    7376    //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br>');
    74     $this->LastQuery = 'REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')';
    75     $this->query($this->LastQuery);
    76     //echo($this->error().'<br>');
     77    $this->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
    7778  }
    78  
     79
    7980  function charset($Charset)
    8081  {
    81     $this->LastQuery = 'SET CHARACTER SET '.$Charset;
    82     $this->query($this->LastQuery);
     82    $this->query('SET CHARACTER SET '.$Charset);
    8383  }
    8484
  • forms.php

    r11 r13  
    2828  function ShowEditBlock($Context = '')
    2929  {
    30     global $Database, $Types;
     30    global $Database, $Types, $TypeNames;
    3131
    3232    $Table = array(
     
    3737    foreach($this->Definition['Items'] as $Item)
    3838    {
    39       if($Item['Type'] != 'Hidden')
     39      if($Item['Type'] != $TypeNames['Hidden'])
    4040      {
    4141        if(!array_key_exists($Index, $this->Values) and isset($Item['Value'])) $this->Values[$Index] = $Item['Value'];
     
    4949  function ShowHiddenBlock($Context = '')
    5050  {
    51     global $Database, $Types;
     51    global $Database, $Types, $TypeNames;
    5252
    5353    $Output = '';
     
    5555    foreach($this->Definition['Items'] as $Item)
    5656    {
    57       if($Item['Type'] == 'Hidden')
     57      if($Item['Type'] == $TypeNames['Hidden'])
    5858      {
    5959        if(!array_key_exists($Item['Name'], $this->Values) and isset($Item['Value'])) $this->Values[$Item['Name']] = $Item['Value'];
  • global.php

    r9 r13  
    1212$Database->Prefix = $Config['Database']['Prefix'];
    1313$Database->charset($Config['Database']['Charset']);
     14$Database->ShowError = TRUE;
    1415include_once('module.php');
    1516include_once('page.php');
    1617include_once('forms.php');
     18include_once('types/include.php');
     19include_once('lists/include.php');
    1720include_once('base.php');
    1821
     
    3942$System = new System();
    4043$System->Config = $Config;
     44include_once('database_list.php');
     45$System->AddModule(new DatabaseList());
    4146include_once('log.php');
    4247$System->AddModule(new Log());
  • lists/SystemListItem.php

    r10 r13  
    55  global $Database;
    66
    7   print_r($Parameters);
     7  //print_r($Parameters);
    88  $DbResult = $Database->select('SystemType', 'DbDataType', 'Id='.$Parameters['Type']);
    99  //echo($Database->LastQuery);
    1010  $DbRow = $DbResult->fetch_assoc();
    1111  $DbResult = $Database->select('SystemList', 'TableName', 'Id='.$Parameters[$Parameters['Column']]);
    12   echo($Database->LastQuery);
     12  //echo($Database->LastQuery);
    1313  $DbRow2 = $DbResult->fetch_assoc();
    1414  $Database->query('ALTER TABLE `'.$DbRow2['TableName'].'` ADD `'.$Parameters['Name'].'` '.$DbRow['DbDataType'].' NOT NULL ;');
    15   echo($Database->LastQuery);
     15  //echo($Database->LastQuery);
    1616}
    1717
  • log.php

    r8 r13  
    11<?php
     2
     3$LogActionType = array('Add' => 0, 'Edit' => 1, 'Delete' => 2);
    24
    35class Log extends Module
    46{
    5   var $Dependencies = array('User');
     7  var $Dependencies = array('User', 'DatabaseList');
     8  var $TableName = 'SystemLog';
    69
    7   function NewRecord($Module, $Operation, $Value = '')
     10  function Add($List, $Item, $Action)
    811  {
    9     $this->Database->insert('is`.`Log', array('Time' => 'NOW()', 'User' => $this->System->Modules['User']->User['Id'], 'Module' => $Module, 'Operation' => $Operation, 'Value' => $Value));
    10     //echo($this->Database->LastQuery);
     12    $Values = array('User' => $this->System->Modules['User']->User['Id'], 'List' => $List, 'Item' => $Item, 'Action' => $Action);
     13    $this->System->Modules['DatabaseList']->AddItem($this->TableName, $Values);
    1114  }
    1215
Note: See TracChangeset for help on using the changeset viewer.