Changeset 9 for base.php


Ignore:
Timestamp:
Oct 11, 2008, 11:06:21 PM (16 years ago)
Author:
george
Message:
  • Přidáno: Částečná podpora pro historii všech záznamů. Editace vytváří nový záznam. Zobrazení historie dané položky.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • base.php

    r8 r9  
    2727  $Output .= '<th>Akce</th></tr>';
    2828
     29  $Where = '';
     30
    2931  if(($Column != '') and ($ColumnValue != 0))
    3032  {
    31     $Where = $Column.'='.$ColumnValue;
     33    $Where .= ' AND (t1.'.$Column.'='.$ColumnValue.')';
    3234    $ColumnSelection = '&amp;Column='.$Column.'&amp;ColumnValue='.$ColumnValue;
    3335    $FullListLink = '<a href="?Action=ViewList&amp;Table='.$List['TableName'].'">Celkový seznam</a>';
    34   } else
    35   {
    36     $Where = '1';
     36  } else
     37  {
    3738    $ColumnSelection = '';
    3839    $FullListLink = '';
     
    5253  {
    5354    $OrderDirection = array('ASC', 'DESC');
    54     $Order = 'ORDER BY `'.$_SESSION['OrderColumn'].'` '.$OrderDirection[$_SESSION['OrderDirection']];
     55    $Order = 'ORDER BY t1.`'.$_SESSION['OrderColumn'].'` '.$OrderDirection[$_SESSION['OrderDirection']];
    5556  } else $Order = '';
    5657
     
    5859  //$Where .= ' AND (ValidFromTime <= NOW()) AND ((ValidToTime >= NOW()) OR (ValidToTime IS NULL))';
    5960  $Where .= ' '.$Order;
    60   $DbResult = $Database->select($List['TableName'], 'COUNT(*)', $Where);
    61   //echo($Database->LastQuery);
     61  $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');
     62  //echo($Database->LastQuery.'<br>');
    6263  $DbRow = $DbResult->fetch_array();
    6364  $TotalItemCount = $DbRow[0];
    6465
    65   $DbResult = $Database->select($List['TableName'], '*', $Where.' LIMIT '.($Page * $Config['Web']['ItemsPerPage']).', '.$Config['Web']['ItemsPerPage']);
     66  $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']);
     67  //echo($Database->LastQuery.'<br>');
    6668  while($DbRow = $DbResult->fetch_array())
    6769  {
     
    8789}
    8890
     91function ShowHistory($List, $Id, $Title = '')
     92{
     93  global $Database, $Types, $Config;
     94
     95  if($Title == '') $Output = '<div>'.$List['Title'].'</div>';
     96    else $Output = '<div>'.$Title.'</div>';
     97  $Output .= '<table class="WideTable"><tr>';
     98  foreach($List['Items'] as $Item)
     99  {
     100    if($Item['VisibleInList'] == 1)
     101      $Output .= '<th><a href="?OrderColumn='.$Item['Name'].'">'.$Item['TextBefore'].'</a></th>';
     102  }
     103  $Output .= '<th>Akce</th></tr>';
     104
     105  $Where = ' AND (ItemId='.$Id.')';
     106
     107  // Handle ordering
     108  if(array_key_exists('OrderColumn', $_GET))
     109  {
     110    if($_SESSION['OrderColumn'] == $_GET['OrderColumn']) // Same column => reverse orded
     111      $_SESSION['OrderDirection'] = ($_SESSION['OrderDirection'] + 1) % 2;
     112    if($_SESSION['OrderTable'] != $List['TableName']) // Different table => set ascending order
     113      $_SESSION['OrderDirection'] = 0;
     114    $_SESSION['OrderColumn'] = $_GET['OrderColumn'];
     115    $_SESSION['OrderTable'] = $List['TableName'];
     116  }
     117  if(array_key_exists('OrderColumn', $_SESSION) and ($_SESSION['OrderTable'] == $List['TableName']))
     118  {
     119    $OrderDirection = array('ASC', 'DESC');
     120    $Order = 'ORDER BY t1.`'.$_SESSION['OrderColumn'].'` '.$OrderDirection[$_SESSION['OrderDirection']];
     121  } else $Order = '';
     122
     123  if(array_key_exists('Page', $_GET)) $Page = $_GET['Page']; else $Page = 0;
     124  //$Where .= ' AND (ValidFromTime <= NOW()) AND ((ValidToTime >= NOW()) OR (ValidToTime IS NULL))';
     125  $Where .= ' '.$Order;
     126  $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');
     127  //echo($Database->LastQuery.'<br>');
     128  $DbRow = $DbResult->fetch_array();
     129  $TotalItemCount = $DbRow[0];
     130
     131  $DbResult = $Database->query('SELECT * FROM `'.$List['TableName'].'` WHERE 1'.$Where.' LIMIT '.($Page * $Config['Web']['ItemsPerPage']).', '.$Config['Web']['ItemsPerPage']);
     132  //echo($Database->LastQuery.'<br>');
     133  while($DbRow = $DbResult->fetch_array())
     134  {
     135    $Output .= '<tr>';
     136    foreach($List['Items'] as $Index => $Item)
     137    {
     138      if($Item['VisibleInList'] == 1)
     139      {
     140        $ItemType = explode(':', $Item['Type']);
     141        $Type = $Types[$ItemType[0]];
     142        $ItemDefinition = array('Name' => $Index, 'Value' => $DbRow[$Index], 'SourceTable' => $List['TableName'], 'SourceItemId' => $DbRow['Id'], 'Type' => $Item['Type']);
     143        if(is_callable($Type['CallbackView'])) $Value = $Type['CallbackView']($Type, $ItemDefinition);
     144        else $Value = $Type['CallbackView'];
     145        $Output .= '<td>'.$Value.'</td>';
     146      }
     147    }
     148    $Output .= '<td><a href="?Action=ViewItem&amp;Table='.$List['TableName'].'&amp;Item='.$DbRow['Id'].'">Zobrazit</a> </td></tr>';
     149  }
     150  $Output .= '</table>';
     151  $Output .= PagesList($Page, $TotalItemCount);
     152  $Output .= '<a href="?Action=AddItem&amp;Table='.$List['TableName'].$ColumnSelection.'">Přidat</a> '.$FullListLink;
     153  return($Output);
     154}
     155
    89156function ShowEditItem($List, $Id)
    90157{
    91158  global $Database, $Types;
    92159
    93   $Output = '<form action="?Action=EditItemFinish&amp;Item='.$Id.'" method="post"><table class="WideTable">';
    94   $Output .= '<tr><th>Jméno položky</th><th>Hodnota</th></tr>';
    95160  $DbResult = $Database->select($List['TableName'], '*', 'Id='.$Id);
    96161  while($DbRow = $DbResult->fetch_array())
    97162  {
     163    $DefinitionItems = array();
    98164    foreach($List['Items'] as $Index => $Item)
    99165    {
     
    101167      if($ItemType[0] != 'PointerOneToMany')
    102168      {
    103         $Type = $Types[$ItemType[0]];
    104         $ItemDefinition = array('Name' => $Index, 'Value' => $DbRow[$Index], 'SourceTable' => $List['TableName'], 'SourceItemId' => $DbRow['Id'], 'Type' => $Item['Type']);
    105         if(is_callable($Type['CallbackEdit'])) $Value = $Type['CallbackEdit']($Type, $ItemDefinition);
    106         else $Value = $Type['CallbackEdit'];
    107         if($Item['Required'] == 1) $Required = '*'; else $Required = '';
    108         $Output .= '<tr><td>'.$Item['TextBefore'].':'.$Required.'</td><td title="'.$Item['Help'].'">'.$Value.'</td></tr>';
    109       }
    110     }
    111   }
    112   $Output .= '</table><input type="submit" value="Uložit"></form>';
     169        $DefinitionItems[] = array('Name' => $Index, 'Caption' => $Item['TextBefore'].$Required, 'Value' => $DbRow[$Index], 'SourceTable' => $List['TableName'], 'SourceItemId' => $DbRow['Id'], 'Type' => $Item['Type']);
     170      }
     171    }
     172  }
     173  $Form = new Form();
     174  $Form->Definition = array
     175  (
     176    'Title' => 'Titulek',
     177    'SubmitBuffonText' => 'Uložit',
     178    'Items' => $DefinitionItems,
     179  );
     180  $Form->OnSubmit = '?Action=EditItemFinish&amp;Item='.$Id;
     181  $Output .= $Form->ShowEditForm();
    113182  if(($Column != '') and ($ColumnValue != 0))
    114183  {
     
    118187    $Output .= '<a href="?Action=ShowList&amp;Table='.$List['TableName'].'">Celkový seznam</a>';
    119188  }
    120 
    121189  return($Output);
    122190}
     
    124192function ShowEditItemFinish($List, $Id)
    125193{
    126   global $Database, $Types;
     194  global $Database, $Types, $System;
    127195
    128196  $Values = array();
     
    135203    }
    136204  }
    137   $Database->update($List['TableName'], 'Id='.$Id, $Values);
     205  $DbResult = $Database->select($List['TableName'], 'ItemId', 'Id='.$Id);
     206  $DbRow = $DbResult->fetch_assoc();
     207  $Values['ItemId'] = $DbRow['ItemId'];
     208  $Values['Author'] = $System->Modules['User']->User['Id'];
     209  $Values['CreationTime'] = 'NOW()';
     210  $Values['ValidTimeFrom'] = 'NOW()';
     211  $Database->insert($List['TableName'], $Values);
    138212  //echo($Database->LastQuery);
    139213  $Output = 'Změny uloženy.';
     
    182256function ShowAddItemFinish($List)
    183257{
    184   global $Database, $Types;
     258  global $Database, $Types, $System;
    185259
    186260  $Values = array();
     
    193267    }
    194268  }
     269  $Values['Author'] = $System->Modules['User']->User['Id'];
     270  $Values['CreationTime'] = 'NOW()';
     271  $Values['ValidTimeFrom'] = 'NOW()';
     272  $DbResult = $Database->select($List['TableName'], 'MAX(ItemId)');
     273  $DbRow = $DbResult->fetch_row();
     274  $AutoincrementId = $DbRow[0];
     275  $Values['ItemId'] = $AutoincrementId + 1;
    195276  $Database->insert($List['TableName'], $Values);
    196277  //echo($Database->LastQuery);
     
    208289  while($DbRow = $DbResult->fetch_array())
    209290  {
    210     $Output = '<table class="WideTable">';
    211     $Output .= '<tr><th>Jméno položky</th><th>Hodnota</th></tr>';
     291    $DefinitionItems = array();
    212292    foreach($List['Items'] as $Index => $Item)
    213293    {
     
    215295      if($ItemType[0] != 'PointerOneToMany')
    216296      {
    217         $Type = $Types[$ItemType[0]];
    218         $ItemDefinition = array('Name' => $Index, 'Value' => $DbRow[$Index], 'SourceTable' => $List['TableName'], 'SourceItemId' => $DbRow['Id'], 'Type' => $Item['Type']);
    219         if(is_callable($Type['CallbackView'])) $Value = $Type['CallbackView']($Type, $ItemDefinition);
    220         else $Value = $Type['CallbackView'];
    221         $Output .= '<tr><td>'.$Item['TextBefore'].':</td><td title="'.$Item['Help'].'">'.$Value.'</td></tr>';
    222       }
    223     }
    224     $Output .= '</table>';
     297        $DefinitionItems[] = array('Name' => $Index, 'Type' => $Item['Type'], 'Caption' => $Item['TextBefore'].$Required, 'Value' => $DbRow[$Index], 'SourceTable' => $List['TableName'], 'SourceItemId' => 0);
     298      }
     299    }
     300    $Form = new Form();
     301    $Form->Definition = array
     302    (
     303      'Title' => 'Titulek',
     304      'Items' => $DefinitionItems,
     305    );
     306    $Output .= $Form->ShowReadOnlyForm();
    225307    $Output .= '<a href="?Action=EditItem&amp;Table='.$List['TableName'].'&amp;Item='.$DbRow['Id'].'">Editovat</a> ';
    226308
     
    232314      $Output .= '<a href="?Action=ShowList&amp;Table='.$List['TableName'].'">Celkový seznam</a>';
    233315    }
     316    $Output .= ' <a href="?Action=ShowHistory&amp;Table='.$List['TableName'].'&amp;Item='.$DbRow['ItemId'].'">Historie</a>';
    234317    $Output .= '<div class="line"></div>';
    235 
    236318    foreach($List['Items'] as $Index => $Item)
    237319    {
     
    306388        case 'AddItemFinish':
    307389          $Output = ShowAddItemFinish($List);
     390          break;
     391        case 'ShowHistory':
     392          $Output = ShowHistory($List, $_SESSION['Item']);
    308393          break;
    309394        case 'ShowList':
Note: See TracChangeset for help on using the changeset viewer.