<?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;
  global $prefix;
  
  $MenuItemName = getMenuItemVarFromID ($MenuItemID, 'MenuItemName');
  
  $SQLQuery = "SELECT * FROM ".$prefix."menu
    WHERE MenuItemParent='".$MenuItemName."' LIMIT 1";
  $Result = mysqli_query($db, $SQLQuery);
  if ( $Array=mysqli_fetch_assoc($Result) )
  {
    return $Array['MenuItemID'];
  }
  return false;
}

function doubleMenuItemPresent ( $MenuItemKind, $MenuItemName )
{
  global $db;
  global $prefix;

  if ( $MenuItemKind AND $MenuItemName )
  {
    $SQLQuery = "SELECT count(*) as `number` FROM ".$prefix."menu
      WHERE MenuItemKind='".$MenuItemKind."' AND MenuItemName='".$MenuItemName."'";
    $Result = mysqli_query($db, $SQLQuery);
    $Array = mysqli_fetch_array($Result);
    if ($Array['number']==0) return false;
    else return true;
  }
  else return false;
}
function getAmountSubMenus ()
{
  global $db;
  global $prefix;

  $SQLQuery = "SELECT count(*) as `number` FROM ".$prefix."menu WHERE MenuItemKind='Menu'";
  $Result = mysqli_query($db, $SQLQuery);
  $Array = mysqli_fetch_array($Result);
  return $Array['number'];
}

function td ( $MenuItemParent )
{
  global $db;
  global $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++)
  {
    $SQLQuery = "SELECT * FROM ".$prefix."menu WHERE MenuItemParent='".$MenuItemParent."' AND MenuItemSequence=".$i;
    $Result = mysqli_query($db, $SQLQuery);
    $Array = mysqli_fetch_assoc($Result);
    $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?submenue=cms&amp;content=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?submenue=cms&amp;content=addMenuItem&amp;job=edit&amp;MenuItemID='.$MenuItemID.'&amp;MenuItemParent='.$MenuItemParent.'">
                        <img src="images/edit.gif" 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?submenue=cms&amp;content=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?submenue=cms&amp;content=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;
  global $prefix;

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

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

  $SQLQuery = "UPDATE ".$prefix."menu SET MenuItemSequence = -1 WHERE MenuItemSequence=".$MenuItemSequence."+1 AND MenuItemParent='".$MenuItemParent."' LIMIT 1";
  $Result = mysqli_query($db, $SQLQuery);
  if ($Result)
  {
    $SQLQuery = "UPDATE ".$prefix."menu SET MenuItemSequence = MenuItemSequence+1 WHERE MenuItemSequence=".$MenuItemSequence." AND MenuItemParent='".$MenuItemParent."' LIMIT 1";
    $Result = mysqli_query($db, $SQLQuery);
    if ($Result)
    {
      $SQLQuery = "UPDATE ".$prefix."menu SET MenuItemSequence = ".$MenuItemSequence." WHERE MenuItemSequence=-1 AND MenuItemParent='".$MenuItemParent."' LIMIT 1";
      $Result = mysqli_query($db, $SQLQuery);
      if ($Result)
      {
        return true;
      }
      else return false;
    }
    else return false;
  }
  else return false;
}

function moveUpMenuItem ($MenuItemSequence, $MenuItemParent)
{
  global $db;
  global $prefix;

  $SQLQuery = "UPDATE ".$prefix."menu SET MenuItemSequence = -1 WHERE MenuItemSequence=".$MenuItemSequence."-1 AND MenuItemParent='".$MenuItemParent."' LIMIT 1";
  $Result = mysqli_query($db, $SQLQuery);
  if ($Result)
  {
    $SQLQuery = "UPDATE ".$prefix."menu SET MenuItemSequence = MenuItemSequence-1 WHERE MenuItemSequence=".$MenuItemSequence." AND MenuItemParent='".$MenuItemParent."' LIMIT 1";
    $Result = mysqli_query($db, $SQLQuery);
    if ($Result)
    {
      $SQLQuery = "UPDATE ".$prefix."menu SET MenuItemSequence = ".$MenuItemSequence." WHERE MenuItemSequence=-1 AND MenuItemParent='".$MenuItemParent."' LIMIT 1";
      $Result = mysqli_query($db, $SQLQuery);
      if ($Result)
      {
        return true;
      }
      else return false;
    }
    else return false;
  }
  else return false;
}

function getMenuItemVarFromID ($MenuItemID, $Var)
{
  global $db;
  global $prefix;

  $SQLQuery = "SELECT ".$Var." FROM ".$prefix."menu
    WHERE MenuItemID='".$MenuItemID."' LIMIT 1";
  $Result = mysqli_query($db, $SQLQuery);
  if ( mysqli_num_rows($Result)==1)
  {
    $temp=mysqli_fetch_assoc($Result);
    return $temp[$Var];
  }
  else return false;
}
function del ( $MenuItemID )
{
  global $prefix;
  global $db;
  
  $currentSequence = getMenuItemVarFromID($MenuItemID, 'MenuItemSequence');
  $MenuItemParent = getMenuItemVarFromID($MenuItemID, 'MenuItemParent');
  $SQLQuery = "DELETE FROM ".$prefix."menu WHERE MenuItemID = '$MenuItemID'";
  echo $MenuItemParent;
  $Result = mysqli_query($db, $SQLQuery);
  if ($Result)
  {
    // Dekrementieren
    $SQLQuery = "UPDATE ".$prefix."menu SET
      MenuItemSequence = MenuItemSequence-1
      WHERE MenuItemSequence>".$currentSequence."
        AND MenuItemParent='".$MenuItemParent."'";
    echo $SQLQuery.'<br />';
    $Result = mysqli_query($db, $SQLQuery);
    if ($Result) 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 $prefix;
  global $db;

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

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

  $SQLQuery = "UPDATE ".$prefix."menu SET
    MenuItemKind = '".$MenuItemKind."',
    MenuItemName = '".$MenuItemName."',
    MenuItemFile = '".$MenuItemFile."'
    WHERE MenuItemID = '".$MenuItemID."'
    LIMIT 1";
  $Result = mysqli_query($db, $SQLQuery);
  if ($Result)
  {
    if ( $MenuItemKind=='Menu' )
    {
      $SQLQuery = "UPDATE ".$prefix."menu SET
        MenuItemParent = '".$MenuItemName."',
        MenuItemFile = '".$MenuItemFile."'
        WHERE MenuItemParent = '".$currentMenuItemName."'
        LIMIT 1";
      $Result = mysqli_query($db, $SQLQuery);
      if ($Result) return true;
      else return false;
    }
    return true;
  }
  else return false;
}

function addMenuItem ( $MenuItemKind, $MenuItemName, $MenuItemParent, $MenuItemFile )
  {
    global $prefix;
    global $db;
    // 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";
    $SQLQuery = "INSERT INTO ".$prefix."menu (MenuItemKind, MenuItemName, MenuItemSequence, MenuItemParent, MenuItemFile)
      VALUES ( '$MenuItemKind', '$MenuItemName', $MenuItemSequence, '$MenuItemParent', '$MenuItemFile' )";
    $Result = mysqli_query($db, $SQLQuery);
    if ($Result)
    {
      return true;
    }
    else return false;
}

function getAmountSubMenuItems ($MenuItemParent)
  {
    global $prefix;
    global $db;
    $SQLQuery = "SELECT count(*) as number FROM ".$prefix."menu WHERE MenuItemParent='".$MenuItemParent."'";
    $Result = mysqli_query($db, $SQLQuery);
    $row = mysqli_fetch_array($Result);
    return $row['number'];
  }

function getAmountMenuItems ()
  {
    global $prefix;
    global $db;

    $SQLQuery = "SELECT count(*) as number FROM ".$prefix."menu";
    $Result = mysqli_query($db, $SQLQuery);
    $row = mysqli_fetch_array($Result);
    return $row['number'];
  }
?>
