Changeset 13 for base.php


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ů.
File:
1 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'],
Note: See TracChangeset for help on using the changeset viewer.