Zend - The PHP Company




Miscellaneous

Add Code


LinkMgr  

Type: class
Added by: Ritter_Runkel
Entered: 03/06/2001
Last modified: 06/12/2000
Rating: - (fewer than 3 votes)
Views: 4830
Useful class to transfer variables through an url.


<?php

/**
  This class can be used to transfer variables through url's.
  
  At LinkMgr you can register global variables, to transfer
  these in a link. Use registerParameter($aParameter); to register 
  such global variables. To get all registered variables use 
  createParameterArray();. To create a link with all registered 
  global variables use createLink($aHref, $aParameterArray); with 
  $aParameterArray=null, otherwise $aParameterArray will be taken 
  to build url. You can set a default href with setHRef($aHRef);.
  Also useful is the index key handling. Use this to get unique 
  index keys for you global arrays. Set a current index key if 
  you want to read transfered global arrays in seperated modules.
  
  NOTE: Use LinkMgr::getInstance(); to get an instance.
  
  NOTE: The LinkMgr works only with normal 
        variables and arrays with a depth of one.

  TIP:  To save registered parameters in seperated modules 
        (instead of a central initialization point), use 
        session_register(...) with your LinkMgr instance.

  @author   danilo@psychatris.de
  @version  0.90; 05/31/2001
 */
class LinkMgr {

  
# static functions
  
  /**
    Gets the singleton instance. Use the one and only.
    
    @return The singleton instance of the LinkMgr.
   */
  
function &getInstance() {
    global 
$_SingletonLinkMgr;

    
# check and create singleton instance
    
if(!$_SingletonLinkMgr)
      
$_SingletonLinkMgr=new LinkMgr();

    
# return singleton instance
    
return $_SingletonLinkMgr;
  }

  
# class variables
  
var $href$paramArray=array(), $indexKey;
  
  
# class functions
  
  /**
    Creates an index key for the given parameter array.
    If $aParameterArray equals null the parameter array 
    of registered variables will be taken. 
    
    @param $aParameterArray The array of 
      parameters to create an index key for.
    @return A new index key for the array.
   */
  
function createIndexKey($aParameterArray=null) {

    
# check parameter array
    
if(!$aParameterArray)
      
$aParameterArray=$this->createParameterArray;

    
# init index key
    
$indexKey=0;

    
# check parameter array
    
if($aParameterArray && is_array($aParameterArray)) {
    
      
# loop through all given parameters
      
$keyArray=array_keys($aParameterArray);
      for(
$i=0;$i<count($keyArray);$i++) {
  
        
# get array of parameter
        
$array=&$aParameterArray[$keyArray[$i]];
  
        
# look up for array indizes
        
if(is_array($array)) {
          
$keyArray2=array_keys($array);
  
          
# compare to get greatest index
          
for($j=0;$j<count($keyArray2);$j++)
            if(
$indexKey<$keyArray2[$j]) $indexKey=$keyArray2[$j];
        }
      }
    }
    
    
# return incremented index key
    
return ++$indexKey;
  }

  
/**
    Creates a link.
    
    @param $aHRef The href.
    @param $aParameterArray The array of parameters to transfer.
    @return A link with parameters to transfer.
   */
  
function createLink($aHRef$aParameterArray=null) {

    
# init variables
    
$char="?";

    
# check href
    
if(!$aHRef$aHRef=$this->href;

    
# check parameter array
    
if(!$aParameterArray)
      
$aParameterArray=$this->createParameterArray();
    
    
# loop through all given parameters
    
$keyArray1=array_keys($aParameterArray);
    for(
$i1=0;$i1<count($keyArray1);$i1++) {

      
# get variable
      
$variable=&$aParameterArray[$keyArray1[$i1]];
      
      
# check variable
      
if($variable) {

        
# check for array
        
if(is_array($variable)) {
  
          
# loop through sub-array
          
$keyArray2=array_keys($variable);
          for(
$i2=0;$i2<count($keyArray2);$i2++)
            
$aHRef=$aHRef.$char.$keyArray1[$i1]."[".$keyArray2[$i2]."]=".$variable[$keyArray2[$i2]];
        }
        else 
$aHRef=$aHRef.$char.$keyArray1[$i1]."=".$variable;
      }

      
# set char to parameter seperator
      
$char="&";
    }
    
    return 
$aHRef;
  }

  
/**
    Creates a parameter array of registered variables.
    
    @return An paramter array of all registered variables.
      Use this to manipulate it and create a link.
   */
  
function createParameterArray() {

    
# loop through all registered parameters
    
$keyArray=array_keys($this->paramArray);
    for(
$i=0;$i<count($keyArray);$i++) {

      
# get paramter
      
$param=$this->paramArray[$keyArray[$i]];

      
# make global
      
global $$param;

      
# check if parameter exist and add to array
      
if($$param$array[$param]=$$param;
    }

    return 
$array;
  }

  
/**
    Gets the currently setted index key.
    
    @return The currently setted index key.
   */
  
function getIndexKey() {
    return 
$this->indexKey;
  }
  
  
/**
    Registers a global variable as parameter.
    
    @param $aParameter The name og a global variable to register.
   */
  
function registerParameter($aParameter) {
    if(!
in_array($aParameter$this->paramArray))
      
$this->paramArray[]=$aParameter;
  }
  
  
/**
    Sets the default href.
    
    @param The new default href.
   */
  
function setHRef($aHRef) {
    
$this->$href=$aHRef;
  }

  
/**
    Sets the index key.
    
    @param The new index key.
   */
  
function setIndexKey($anIndexKey) {
    
$this->indexKey=$anIndexKey;
  }
}

?>


Usage Example


will follow...


Rate This Script





Search



This Category All Categories