<?php
/*
    CREATIVE COMMONS - Attribution-No Derivative Works 3.0 Germany:
    - You may not alter, transform, or build upon this work.
    - Dieses Werk darf nicht bearbeitet oder in anderer Weise verändert werden.
*/
function getNextSubItem($MenuItemID)
{
  
  global $DB_PREFIX;
  
  $MenuItemName = getMenuItemVarFromID ($MenuItemID, 'MenuItemName');
  
  $sql = "SELECT * FROM ".DB_PREFIX."menu
    WHERE MenuItemParent='".$MenuItemName."' LIMIT 1";
  $resultat = mysql_query($sql);
  if ( $Array=mysql_fetch_assoc($resultat) )
  {
    return $Array['MenuItemID'];
  }
  return false;
}

function doubleMenuItemPresent ( $MenuItemKind, $MenuItemName )
{
  
  global $DB_PREFIX;

  if ( $MenuItemKind AND $MenuItemName )
  {
    $sql = "SELECT count( * ) as `Anzahl` FROM ".DB_PREFIX."menu
      WHERE MenuItemKind='".$MenuItemKind."' AND MenuItemName='".$MenuItemName."'";
    $resultat = mysql_query($sql);
    $Array = mysql_fetch_array($resultat);
    if ($Array['Anzahl']==0) return false;
    else return true;
  }
  else return false;
}
function getAmountSubMenus ()
{
  
  global $DB_PREFIX;

  $sql = "SELECT count( * ) as `Anzahl` FROM ".DB_PREFIX."menu WHERE MenuItemKind='Menu'";
  $resultat = mysql_query($sql);
  $Array = mysql_fetch_array($resultat);
  return $Array['Anzahl'];
}

function td ( $MenuItemParent )
{
  
  global $DB_PREFIX;
  global $counter;
  global $REQUEST_URI;
  global $L_Edit_page;
  global $L_Edit_page;
  global $L_Add_subitem;
  global $UserRightDeleteMenuItem;
  global $UserRightEditMenuItem;
  global $L_009;
  global $L_010;

  $AmountSubMenus = getAmountSubMenus();
  $AmountSubMenuItems = getAmountSubMenuItems($MenuItemParent);
  for ($i=0; $i<=$AmountSubMenuItems-1; $i++)
  {
    $sql = "SELECT * FROM ".DB_PREFIX."menu WHERE MenuItemParent='".$MenuItemParent."' AND MenuItemSequence=".$i;
    $resultat = mysql_query($sql);
    $Array=mysql_fetch_assoc($resultat);
    $MenuItemID = $Array['MenuItemID'];
    $MenuItemKind = $Array['MenuItemKind'];
    $MenuItemName = $Array['MenuItemName'];
    $MenuItemSequence = $Array['MenuItemSequence'];
    $MenuItemParent = $Array['MenuItemParent'];
    $MenuItemFile = $Array['MenuItemFile'];

    echo '<tr>';
    if ( $UserRightDeleteMenuItem )
            {
              echo '<td>
                      <a href="index.php?submenu=cms&amp;page=delMenuItem&amp;MenuItemID='.$MenuItemID.'">
                        <img src="images/delete.gif" alt="'.$L_009.'" title="'.$L_009.'" /></a>
                    </td>';
            }
            if ( $UserRightEditMenuItem )
            {
              echo '<td>
                      <a href="index.php?submenu=cms&amp;page=addMenuItem&amp;job=edit&amp;MenuItemID='.$MenuItemID.'&amp;MenuItemParent='.$MenuItemParent.'">
                        <img src="images/edit.png" alt="'.$L_010.'" title="'.$L_010.'" /></a>
                    </td>';
            }
    echo '<td>';
    for ($j=1; $j<=$counter; $j++)
    {
      if ( $j==$counter ) echo '<img src="images/cms_menu_sub.gif" alt="" title="" />&nbsp;';
      else echo '<img src="images/blank.gif" alt="" title="" />&nbsp;';
    }
    echo $MenuItemName.'</td>';
    echo '<td>';
    if ( $MenuItemKind=='Menu' )
      echo '<a href="index.php?submenu=cms&amp;page=addMenuItem&amp;job=add&amp;MenuItemID='.$MenuItemID.'&amp;MenuItemParent='.$MenuItemName.'">
              <img src="images/cms_menu_add.gif" alt="'.$L_Add_subitem.'" title="'.$L_Add_subitem.'" /></a>';
    if ( $MenuItemKind=='Page' )
      echo '<a href="index.php?submenu=cms&amp;page=addPage&amp;file=addSubitem">
              <img src="images/cms_pages.gif" alt="'.$L_Edit_page.'" title="'.$L_Edit_page.'" /></a>';
    echo '</td>';
    echo '<td>'.$MenuItemID.'</td>';
    echo '<td>'.$MenuItemKind.'</td>';
    echo '<td>'.$MenuItemSequence.'</td>';
    echo '<td>'.$MenuItemParent.'</td>';
    echo '<td>'.$MenuItemFile.'</td>';
    echo '<td>';
    // Icon zum höher setzen
    if ( $MenuItemSequence!=0 )
    {
      echo '<form style="display:inline" method="post" action="'.$REQUEST_URI.'" accept-charset="UTF-8">
              <fieldset style="display:inline">
                <input type="hidden" name="MenuItemSequence" value="'.$MenuItemSequence.'" />
                <input type="hidden" name="MenuItemParent" value="'.$MenuItemParent.'" />
                <input title="" name="MenuItemUp" src="images/up.gif" style="border:0; width:16px;" type="image" value="'.$MenuItemSequence.'" />
              </fieldset>
            </form>';
    }
    echo '</td>';
    echo '<td>';
    // Icon zum tiefer setzen
    if ( $MenuItemSequence<$AmountSubMenuItems-1 )
    {
      echo '<form style="display:inline" method="post" action="'.$REQUEST_URI.'" accept-charset="UTF-8">
              <fieldset style="display:inline">
                <input type="hidden" name="MenuItemSequence" value="'.$MenuItemSequence.'" />
                <input type="hidden" name="MenuItemParent" value="'.$MenuItemParent.'" />
                <input title="" name="MenuItemDown" src="images/down.gif" style="border:0; width:16px;" type="image" value="'.$MenuItemSequence.'" />
              </fieldset>
            </form>';
    }
    echo '</td>';
    echo '</tr>';
    if ($MenuItemKind=='Menu') { $counter++; td($MenuItemName);$counter--;  }
    if ($MenuItemParent=='main') $counter=0;
  }
}

function addSubitem ($MenuItemID, $MenuItem, $MenuItemName)
{
  
  global $DB_PREFIX;

  echo $MenuItemID.'<br />';
  echo $MenuItem.'<br />';
  echo $MenuItemName.'<br />';

  return true;
}
function moveDownMenuItem ($MenuItemSequence, $MenuItemParent)
{
  
  global $DB_PREFIX;

  $sql = "UPDATE ".DB_PREFIX."menu SET MenuItemSequence = -1 WHERE MenuItemSequence=".$MenuItemSequence."+1 AND MenuItemParent='".$MenuItemParent."' LIMIT 1";
  $resultat = mysql_query($sql);
  if ($resultat)
  {
    $sql = "UPDATE ".DB_PREFIX."menu SET MenuItemSequence = MenuItemSequence+1 WHERE MenuItemSequence=".$MenuItemSequence." AND MenuItemParent='".$MenuItemParent."' LIMIT 1";
    $resultat = mysql_query($sql);
    if ($resultat)
    {
      $sql = "UPDATE ".DB_PREFIX."menu SET MenuItemSequence = ".$MenuItemSequence." WHERE MenuItemSequence=-1 AND MenuItemParent='".$MenuItemParent."' LIMIT 1";
      $resultat = mysql_query($sql);
      if ($resultat)
      {
        return true;
      }
      else return false;
    }
    else return false;
  }
  else return false;
}

function moveUpMenuItem ($MenuItemSequence, $MenuItemParent)
{
  
  global $DB_PREFIX;

  $sql = "UPDATE ".DB_PREFIX."menu SET MenuItemSequence = -1 WHERE MenuItemSequence=".$MenuItemSequence."-1 AND MenuItemParent='".$MenuItemParent."' LIMIT 1";
  $resultat = mysql_query($sql);
  if ($resultat)
  {
    $sql = "UPDATE ".DB_PREFIX."menu SET MenuItemSequence = MenuItemSequence-1 WHERE MenuItemSequence=".$MenuItemSequence." AND MenuItemParent='".$MenuItemParent."' LIMIT 1";
    $resultat = mysql_query($sql);
    if ($resultat)
    {
      $sql = "UPDATE ".DB_PREFIX."menu SET MenuItemSequence = ".$MenuItemSequence." WHERE MenuItemSequence=-1 AND MenuItemParent='".$MenuItemParent."' LIMIT 1";
      $resultat = mysql_query($sql);
      if ($resultat)
      {
        return true;
      }
      else return false;
    }
    else return false;
  }
  else return false;
}

function getMenuItemVarFromID ($MenuItemID, $Var)
{
  
  global $DB_PREFIX;

  $SQL = "SELECT ".$Var." FROM ".DB_PREFIX."menu
    WHERE MenuItemID='".$MenuItemID."' LIMIT 1";
  $result = mysql_query($SQL);
  if ( mysql_num_rows($result)==1)
  {
    $temp=mysql_fetch_assoc($result);
    return $temp[$Var];
  }
  else return false;
}
function del ( $MenuItemID )
{
  global $DB_PREFIX;
  
  
  $currentSequence = getMenuItemVarFromID($MenuItemID, 'MenuItemSequence');
  $MenuItemParent = getMenuItemVarFromID($MenuItemID, 'MenuItemParent');
  $sql = "DELETE FROM ".DB_PREFIX."menu WHERE MenuItemID = '$MenuItemID'";
  echo $MenuItemParent;
  $resultat = mysql_query($sql);
  if ($resultat)
  {
    // Dekrementieren
    $sql = "UPDATE ".DB_PREFIX."menu SET
      MenuItemSequence = MenuItemSequence-1
      WHERE MenuItemSequence>".$currentSequence."
        AND MenuItemParent='".$MenuItemParent."'";
    echo $sql.'<br />';
    $resultat = mysql_query($sql);
    if ($resultat) return true;
    else return false;
  }
  else return false;
}
function delMenuItem ( $MenuItemID )
{
  $MenuItemName = getMenuItemVarFromID($MenuItemID, 'MenuItemName');
  $AmountSubMenuItems = getAmountSubMenuItems($MenuItemName);
  for ($i=1; $i<=$AmountSubMenuItems; $i++)
  {
    $NextSubItem = getNextSubItem($MenuItemID);
    $MenuItemKind = getMenuItemVarFromID($NextSubItem, 'MenuItemKind');
    echo $NextSubItem.$MenuItemKind.'<br />';
    if ( $MenuItemKind=='Menu' )
    {
      delMenuItem ( $NextSubItem );
      del($NextSubItem);
    }
    else del($NextSubItem);
  }
  del($MenuItemID);
}

function editMenuItem ( $MenuItemID, $MenuItemKind, $MenuItemName, $MenuItemFile )
{
  global $DB_PREFIX;
  

  // HTML-Tags entfernen
  $MenuItemName = strip_tags($MenuItemName);
  $MenuItemFile = strip_tags($MenuItemFile);
  // Sonderzeichen umwandeln
  $MenuItemName = htmlspecialchars($MenuItemName, ENT_QUOTES);

  $currentMenuItemName = getMenuItemVarFromID($MenuItemID, 'MenuItemName');

  $sql = "UPDATE ".DB_PREFIX."menu SET
    MenuItemKind = '".$MenuItemKind."',
    MenuItemName = '".$MenuItemName."',
    MenuItemFile = '".$MenuItemFile."'
    WHERE MenuItemID = '".$MenuItemID."'
    LIMIT 1";
  $resultat = mysql_query($sql);
  if ($resultat)
  {
    if ( $MenuItemKind=='Menu' )
    {
      $sql = "UPDATE ".DB_PREFIX."menu SET
        MenuItemParent = '".$MenuItemName."',
        MenuItemFile = '".$MenuItemFile."'
        WHERE MenuItemParent = '".$currentMenuItemName."'
        LIMIT 1";
      $resultat = mysql_query($sql);
      if ($resultat) return true;
      else return false;
    }
    return true;
  }
  else return false;
}

function addMenuItem ( $MenuItemKind, $MenuItemName, $MenuItemParent, $MenuItemFile )
  {
    global $DB_PREFIX;
    // HTML-Tags entfernen
    $MenuItemName = strip_tags($MenuItemName);
    $MenuItemFile = strip_tags($MenuItemFile);
    // Sonderzeichen umwandeln
    $MenuItemName = htmlspecialchars($MenuItemName, ENT_QUOTES);
    $MenuItemSequence = getAmountSubMenuItems($MenuItemParent);
    // Leere numerische Variablen als NULL definieren
      if ( $MenuItemSequence=='' ) $MenuItemSequence="NULL";
    $sql = "INSERT INTO ".DB_PREFIX."menu (MenuItemKind, MenuItemName, MenuItemSequence, MenuItemParent, MenuItemFile)
      VALUES ( '$MenuItemKind', '$MenuItemName', $MenuItemSequence, '$MenuItemParent', '$MenuItemFile' )";
    $resultat = mysql_query($sql);
    if ($resultat)
    {
      return true;
    }
    else return false;
}

function getAmountSubMenuItems ($MenuItemParent)
  {
    global $DB_PREFIX;
    $sql = "SELECT count(*) as zahl FROM ".DB_PREFIX."menu WHERE MenuItemParent='".$MenuItemParent."'";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    return $row['zahl'];
  }

function getAmountMenuItems ()
  {
    global $DB_PREFIX;
    $sql = "SELECT count(*) as zahl FROM ".DB_PREFIX."menu";
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    return $row['zahl'];
  }
?>
