Location: PHPKode > scripts > myMenuGen > mymenugen/classes/mymenugen_class.php
<?php
/*
 * MyMenuGen_class.php
 *
 * Copyright (C) 2006 Peter C Barkway
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

class myMenuObject {

// *****************************************************************
  # Object Constructor
  function myMenuObject(){
    // Tables
    $this->tableName      = 'mymenugen_pages';
    $this->tableName2     = 'mymenugen_menu_items';

    // Menu Items
    $this->menu_id        = -1;
    $this->menu_title     = '';
    $this->menu_image     = '';
    $this->menu_hr        = 'no';
    $this->position       = 0;
    $this->parent_id      = -1;
    $this->external_link  = '';

    // Page Details
    $this->defaultHomepage= 100;
    $this->page_id        = -1;
    $this->page_link      = '';
    $this->page_decription= '';

    // General
    $this->debug          = false;
    $this->rootDir        = '';
    $this->new_record     = true;
    $this->version        = '1.0d';           // current version number of this class
  } # function myMenuObject

// *****************************************************************
  function last_message($message){
    if($this->debug){ print "<p>$message</p>\n"; }
  } # function last_message($message)

// *****************************************************************
// Prints version number
  function getVersion(){
    print "<h2>myMenuGen_class.php v$this->version</h2>\n";
  } # function getVersion()

// *****************************************************************
  function quote_smart($value) {
     // Stripslashes
     if (get_magic_quotes_gpc()) {
       $value = stripslashes($value);
     }
     // Quote if not integer
     if (!is_numeric($value)) {
         $value = "'" . mysql_real_escape_string($value) . "'";
     }
     return $value;
  }

// *****************************************************************
// Fetches the value of the currently set homepage.
// If none set then return the default page, set in the constructor
// *****************************************************************
  function getHomePage() {
    $sql = "SELECT id, title, description FROM $this->tableName WHERE homepage = 'yes' LIMIT 1";
    $this->last_message("myMenuObject: Select homepage<br />$sql<br />");
    $result = mysql_query($sql) or die("Database Error:  Unable to find homepage");
    if(mysql_num_rows($result) > 0) {
      $row = mysql_fetch_object ($result);
      $this->page_id          = $row->id;
      $this->page_link        = $row->title;
      $this->page_description = $row->description;
      return $this->page_id;
    } else {
      return $this->defaultHomepage;
    }
  }

// *****************************************************************
// Set the page with the passed id to be the homepage.
// *****************************************************************
  function saveHomePage($id) {
    if(empty($id)) {
      $this->last_message("myMenuObject: No page id supplied");
      return false;
    } else {
      // Set all homepages to no
      $sql = "UPDATE $this->tableName SET homepage='no'";
      $this->last_message("myMenuObject: Set homepage field to 'No'<br />$sql<br />");
      $result = mysql_query($sql) or die("Database Error:  Unable to set hompage values to No");
      // Set homepage to yes for the page we want
      $sql = "UPDATE $this->tableName SET homepage='yes' WHERE id = '$id'";
      $this->last_message("myMenuObject: Set homepage value<br />$sql<br />");
      $result = mysql_query($sql) or die("Database Error:  Unable to set hompage value");
      return true;
    }
  }

// *****************************************************************
// Fetches the value of the currently set homepage.
// If none set then return the default page, set in the constructor
// *****************************************************************
  function getPages() {
    $pageArray = array();

    $sql = "SELECT id FROM $this->tableName WHERE id > 99 AND id < 400";
    $this->last_message("myMenuObject: Select pages<br />$sql<br />");
    $result = mysql_query($sql) or die("Database Error:  Unable to find pages");
    if(mysql_num_rows($result) > 0) {
      while ($row = mysql_fetch_object ($result)) {
        $pageArray[] = $row->id;
      }
    }
    return $pageArray;
  }

// *****************************************************************
// Fetches the value of the currently set homepage.
// If none set then return the default page, set in the constructor
// *****************************************************************
  function getPage($id) {
    $sql = "SELECT id, title, description FROM $this->tableName WHERE id = '$id' LIMIT 1";
    $this->last_message("myMenuObject: Select page details<br />$sql<br />");
    $result = mysql_query($sql) or die("Database Error:  Unable to find homepage");
    if(mysql_num_rows($result) > 0) {
      $row = mysql_fetch_object ($result);
      $this->page_id          = $row->id;
      $this->page_link        = $row->title;
      $this->page_description = $row->description;
      return true;
    }
    return false;
  }

// *****************************************************************
// Get all menu items on a particular level
// *****************************************************************
  function getThisLevel($pid) {
    $levelArray = array();

    $sql = "SELECT id FROM $this->tableName2 WHERE parent_id = $pid ORDER BY position";
    $this->last_message("myMenuObject: Select menu items for top level<br />$sql<br />");
    $result = mysql_query($sql) or die("Database Error:  Unable to find menu items for top level");
    if(mysql_num_rows($result) > 0) {
      while ($row = mysql_fetch_object ($result)) {
        $levelArray[] = $row->id;
      }
    }
    return $levelArray;
  }

// *****************************************************************
// Get menu item details
// *****************************************************************
  function getMenuItem($id) {
    $parentArray = array();

    if(empty($id)) {
      $this->last_message("myMenuObject: No page id supplied");
      return false;
    } else {
      $sql = "SELECT menu_title,
                     position,
                     parent_id,
                     page_id,
                     image,
                     hr_line,
                     $this->tableName.title,
                     external_link
              FROM $this->tableName2
              LEFT JOIN $this->tableName ON $this->tableName.id = $this->tableName2.page_id
              WHERE $this->tableName2.id = '$id' LIMIT 1";
      $this->last_message("myMenuObject: Select menu item details id:$id<br />$sql<br />".mysql_error()."<br />");
      $result = mysql_query($sql) or die("Database Error:  Unable to find menu item id: $id".mysql_error()."");
      if(mysql_num_rows($result) > 0) {
        $row = mysql_fetch_object ($result);
        $this->menu_title = $row->menu_title;
        $this->menu_image = $row->image;
        $this->menu_hr    = $row->hr_line;
        $this->position   = $row->position;
        $this->parent_id  = $row->parent_id;
        $this->page_id    = $row->page_id;
        $this->page_link  = empty($row->title)?$row->external_link:$row->title;
        return true;
      } else {
        $this->last_message("myMenuObject: Unable to find parent id:$id<br />$sql<br />");
        return false;
      }
    }
  }

// *****************************************************************
// Get parent of a child item
// *****************************************************************
  function getParentItem($id) {
    $parent_id = 0;

    if(empty($id)) {
      $this->last_message("myMenuObject: No page id supplied");
      return false;
    } else {
      $sql = "SELECT id FROM $this->tableName2 WHERE id = '$id' LIMIT 1";
      $this->last_message("myMenuObject: Select parent of child: $id<br />$sql<br />");
      $result = mysql_query($sql) or die("Database Error:  Unable to find parent of child: $id");
      if(mysql_num_rows($result) > 0) {
        $row = mysql_fetch_object ($result);
        $parent_id = $row->id;
      }
      return $parent_id;
    }
  }

// *****************************************************************
// Get all child items for a particular parent
// *****************************************************************
  function getChildItems($id) {
    $childArray = array();

    $sql = "SELECT id FROM $this->tableName2 WHERE parent_id = '$id'";
    $this->last_message("myMenuObject: Select children for id: $id<br />$sql<br />");
    $result = mysql_query($sql) or die("Database Error:  Unable to find child items for parent id: $id");
    if(mysql_num_rows($result) > 0) {
      while ($row = mysql_fetch_object ($result)) {
        $childArray[] = $row->id;
      }
    }
    return $childArray;
  }

// *****************************************************************
// Get all menu items by position order
// *****************************************************************
  function getMenuItemsOrdered() {
    $idArray = array();

    $sql = "SELECT id FROM $this->tableName2 ORDER BY position";
    $this->last_message("myMenuObject: Select all menu id<br />$sql<br />");
    $result = mysql_query($sql) or die("Database Error:  Unable to connect to menu items table");
    if(mysql_num_rows($result) > 0) {
      while ($row = mysql_fetch_object ($result)) {
        $idArray[] = $row->id;
      }
    }
    return $idArray;
  }

// *****************************************************************
// Get all menu items unordered
// *****************************************************************
  function getMenuItemsID() {
    $idArray = array();

    $sql = "SELECT id FROM $this->tableName2";
    $this->last_message("myMenuObject: Select all menu id<br />$sql<br />");
    $result = mysql_query($sql) or die("Database Error:  Unable to connect to menu items table");
    if(mysql_num_rows($result) > 0) {
      while ($row = mysql_fetch_object ($result)) {
        $idArray[] = $row->id;
      }
    }
    return $idArray;
  }

// *****************************************************************
// Test if menu item exists
// *****************************************************************
  function itemExists($id) {
    if(empty($id)) {
      $this->last_message("myMenuObject: No page id supplied");
      return false;
    } else {
      $sql = "SELECT id FROM $this->tableName2 WHERE id = '$id' LIMIT 1";
      $this->last_message("myMenuObject: Check for menu item id: $id<br />$sql<br />");
      $result = mysql_query($sql) or die("Database Error:  Unable to check for menu item id: $id");
      if(mysql_num_rows($result) > 0) {
        return true;
      } else {
        return false;
      }
    }
  }

// *****************************************************************
// Save menu item
// *****************************************************************
  function saveMenuItem() {
    $saved = 0;
    $sql = "INSERT INTO $this->tableName2
                (id,
                 menu_title,
                 position,
                 parent_id,
                 image,
                 hr_line,
                 page_id,
                 external_link
                )
            VALUES
                (".$this->quote_smart($this->menu_id).",
                 ".$this->quote_smart($this->menu_title).",
                 ".$this->quote_smart($this->position).",
                 ".$this->quote_smart($this->parent_id).",
                 ".$this->quote_smart($this->menu_image).",
                 ".$this->quote_smart($this->menu_hr).",
                 ".$this->quote_smart($this->page_id).",
                 ".$this->quote_smart($this->external_link)."
                )";
    $this->last_message("myMenuObject: Save menu item<br />$sql<br />");
    $result = mysql_query($sql) or die("Database Error:  Unable to save menu item");
  }

// *****************************************************************
// Update menu item
// *****************************************************************
  function updateMenuItem($id) {
    if(empty($id)) {
      $this->last_message("myMenuObject: No page id supplied");
      return false;
    } else {
      $sql = "UPDATE $this->tableName2 SET
                menu_title    = ".$this->quote_smart($this->menu_title).",
                position      = ".$this->quote_smart($this->position).",
                parent_id     = ".$this->quote_smart($this->parent_id).",
                image         = ".$this->quote_smart($this->menu_image).",
                hr_line       = ".$this->quote_smart($this->menu_hr).",
                page_id       = ".$this->quote_smart($this->page_id).",
                external_link = ".$this->quote_smart($this->external_link)."
              WHERE id = '$id'";
      $this->last_message("myMenuObject: Update menu item id: $id<br />$sql<br />");
      $result = mysql_query($sql) or die("Database Error:  Unable to update menu item id: $id");
      return true;
    }
  }

// *****************************************************************
// Delete menu item
// *****************************************************************
  function deleteMenuItem($id) {
    if(empty($id)) {
      $this->last_message("myMenuObject: No page id supplied");
      return false;
    } else {
      $sql = "DELETE FROM $this->tableName2 WHERE id='$id'";
      $this->last_message("myMenuObject: Delete menu item id:$id<br />$sql<br />");
      $result = mysql_query($sql) or die("Database Error:  Unable to delete menu item");
      return true;
    }
  }

// *****************************************************************
// Delete all menu items
// *****************************************************************
  function deleteAllMenuItems() {
    $sql = "TRUNCATE `$this->tableName2`";
    $this->last_message("myMenuObject: Delete menu item id:$id<br />$sql<br />");
    $result = mysql_query($sql) or die("Database Error:  Unable to delete menu item");
  }

// *****************************************************************
// Delete all menu items
// *****************************************************************
  function deleteRemovedMenuItems($idArray) {
    $existingIDArray = $tmpArray = array();
    $existingIDArray = $this->getMenuItemsID();
    if(count($idArray) > 0 && count($existingIDArray) > 0) {
      $tmpArray = array_diff($existingIDArray, $idArray);
      if(count($tmpArray) > 0) {
        foreach($tmpArray as $val) {
          $this->deleteMenuItem($val);
        }
      }
    }
  }


// *****************************************************************
// Build the menu structure as a series of li items.
// Child items will be created as ul/li items
// *****************************************************************
  function generateMenuStructure($pid = 0, $breakAfter = 0) {
    $tStr = '';

    $topLevel = array();
    $topLevel = $this->getThisLevel($pid);
    if(count($topLevel) > 0 ) {
      foreach($topLevel as $val) {
        $tStr .= $this->createItems($val, $breakAfter);
      }
      return $tStr;
    } else {
      $this->last_message("myMenuObject: Unable to determine top menu items for id: $pid");
    }
  }


// *****************************************************************
// Build the menu structure as a series of li items.
// Child items will be created as ul/li items
// *****************************************************************
  function createItems($pid, $breakAfter) {
    $tStr = '';
    if($this->getMenuItem($pid)) {
      // Determine if this item has children
      $childLevel = array();
      $childLevel = $this->getChildItems($pid);
      if(count($childLevel) > 0) { // Yes we do have child items
        $tStr .= '<li>';
        if(!empty($this->menu_image)) {
          $tStr .= '<span class="menuImg"><img src="'.$this->menu_image.'" /></span>';
        }
        $tStr .= '<a>'.$this->menu_title.'<span class="menuArrow"></span></a><ul>';
        if($this->menu_hr == 'yes' && !$breakAfter) {
          $breakAfter = 1;
        } else {
          $breakAfter = 0;
        }
        $julie = $this->generateMenuStructure($pid, $breakAfter);
        if(!empty($julie)) {
          $tStr .= $julie.'</ul>';
          if($breakAfter) {
            $tStr .= '<span class="menu_break"></span>';
          }
          $tStr .= '</li>';
        }
      } else {                    // No child items
        $tStr .= '<li>';
        if(!empty($this->menu_image)) {
          $tStr .= '<span class="menuImg"><img src="'.$this->menu_image.'" /></span>';
        }
        $tStr .= '<a href=';
        if($this->page_id == 0 && !empty($this->page_link)) {
          $tStr .= '"'.$this->page_link.'" target="_blank"';
        } else {
          $tStr .= '"'.$this->rootDir.'index.php?pid='.$this->page_id.'"';
        }
        $tStr .= '>'.$this->menu_title.'</a>';
        if($this->menu_hr == 'yes') {
          $tStr .= '<span class="menu_break"></span>';
        }
        $tStr .= '</li>';
      }
    }
    return $tStr;
  }

} # class myMenuObject
?>
Return current item: myMenuGen