Location: PHPKode > scripts > DX0 > dx0/dx0/dx0loadable.inc
<?php

  if (!defined("_DX0_LOADABLE_"))
  {
    define("_DX0_LOADABLE_", 1);
  } else return;

  //
  // dx0loadable class - see README for details
  // { hide@address.com }
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  // TINA: (morosely): Oh, darling, darling, darling. I'm not much good to you am I?
  // RICK: Don't say that. Don't ever say that, you hear me?
  // TINA: It's true, though. I'm a jinx. I'm a horrible jinx. If it hadn't been for
  //   me, Scott Kincaid would have assigned you to the Buenos Aires office years ago.
  //   I spoiled all that. (Goes over to the window) I'm one of the little foxes that
  //   spoil the grapes. I feel like someone in a terribly sophisticated play. The
  //   funny part is, I'm not sophisticated. I'm not anything. I'm just me. (Turns)
  //   Oh, Rick, Rick, I'm scared. What's happened to us? I can't seem to find us
  //   anymore. I reach out and reach out and we're just not there. I'm frightened.
  //   I'm a frightened child. (Looks out window) I hate this rain. Sometimes I see
  //   me dead in it.
  // RICK (quietly): My darling, isn't that a line from "A Farewell to Arms"?
  // TINA (Turns, furious): Get out of here. Get out! Get out of here before I jump
  //   out of this window. Do you hear me?
  // RICK (grabbing her): Now you listen to me. You beautiful little moron. You
  //   adorable, childish, self-dramatizing--
  //                                            -FRANNY AND ZOOEY by J.D. Salinger
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
  //
   
  require $GLOBALS['DX0_INCLUDE_DIR'].'pickle.inc';

  $DX0_LOADED_CACHE = array();				// caches unpickled loadElements
  $DX0_SKIN_EXTENSIONS = array( 'gif', 'png', 'jpg', 'jpeg' );
  class dx0loadElement {

    function usage( $errorStr ) {
      $this->error( $errorStr . "<BR><BR>
      [dx0loadElement] USAGE:<BR>
      \$obj = new dx0loadElement( \$loadEle, \$eleName, \$properties );<BR>
      Please consult the DX0 documentation at <A HREF=\"http://deathstar.eng.utah.edu/~kroford/930/dx0/doc/\">the dx0 distribution site</A> for details.<BR><BR>" );

      return false;
    }

    function error( $errorStr ) {
      if ($GLOBALS['DX0_SUPPRESS_ERRORS']) return false;
      echo "<FONT COLOR=\"RED\"><B>ERROR:</B></FONT> " . $errorStr . "<BR>";
    }


    function dx0loadElement($loadEle, $name = "Default", $newProps = "") {
      //
      // Validate Parameters
      //
      if (!is_string($loadEle) && !is_array($loadEle)) {
        return $this->usage("The \$loadEle parameter must be a string or an array.");
      } else if (!is_string($name)) {
        return $this->usage("The \$eleName parameter must be a string.");
      } else if ($newProps != "" && !is_array($newProps)) {
        return $this->usage("The \$properties parameter must be an array.");
      }

      if (!$newProps) $newProps = array();
      $req = array("nick", "author", "description", "elements", "properties", "assigns",
        "javascript", "openTag", "closeTag", "images");
      $this->name = $name;
      $this->objAssigns = array();

      //
      // attempt to load file or array
      //
      $this->nick = $this->load($loadEle);
      if (!$this->nick) return false;

      //while (list($prop, $var) = each($GLOBALS['DX0_LOADED_CACHE'][$this->nick])) {
      //  eval("\$this->$prop = \$var;");
      //}
      while (list(,$chk) = each($req)) {
        if (!isset($GLOBALS['DX0_LOADED_CACHE'][$this->nick])) {
          $this->errlog .= "No '$chk' found in the loadable element.\n";
        }
      }
      
      $this->imageDir = "ele/" . $this->nick . "/";
      $this->default = $GLOBALS['DX0_LOADED_CACHE'][$this->nick]['properties']['default'];
      $GLOBALS['DX0_LOAD_ELEMENTS'][$this->name] = $this->name . $this->default;
      //var_dump( $GLOBALS['DX0_LOADED_CACHE'] ); exit();
      $this->assigns = $GLOBALS['DX0_LOADED_CACHE'][$this->nick]['assigns'];
      $this->setProperties($newProps);
      $this->PARTS = array();
      while (list($k, $v) = each($GLOBALS['DX0_LOADED_CACHE'][$this->nick]['elements'])) {
        eval("\$this->" . $k . "Props['name'] = '" . $this->name . $k . "';");
        eval("\$this->PARTS[\$k] = new dx0" . $v . "('" . $this->name . $k . "', \$this->" . $k . "Props);");
      } 

      //
      // identify images with proper extensions
      //
      while (list($k,$v) = each($GLOBALS['DX0_LOADED_CACHE'][$this->nick]['images'])) {
        reset($GLOBALS['DX0_SKIN_EXTENSIONS']);
        while (list(,$ext) = each($GLOBALS['DX0_SKIN_EXTENSIONS'])) {
          $rep = $this->imageDir . $this->skin . "_" . $v . "." . $ext;
          if (file_exists($GLOBALS['DX0_DIR'] . $rep))
            $this->imageList[$k] = $GLOBALS['DX0_WEB_DIR'] . $rep;
        }
      }

      $base = $this->PARTS[$this->default];
      if (is_array($this->assigns)) {
        while (list($k, $v) = each($this->assigns)) {
          $base->objAssigns[$k] = $this->interpret($v);
        }
      }
      $this->PARTS[$this->default] = $base;

      if (isset($GLOBALS['DX0_LOADED_CACHE'][$this->nick]['onload'])) {
        while (list(,$line) = each($GLOBALS['DX0_LOADED_CACHE'][$this->nick]['onload'])) {
          dx0registerFx("onload", $this->interpret($line));   
        }
      }

      return false;
    }
    
    function openTag() {
      return $this->interpret($GLOBALS['DX0_LOADED_CACHE'][$this->nick]['openTag']);
    }

    function closeTag() {
      return $this->interpret($GLOBALS['DX0_LOADED_CACHE'][$this->nick]['closeTag']);
    }

    function place($content = "") {
      return $this->openTag() . $content . $this->closeTag();
    }

    //
    // stores the unpickled file (or associative array) in the cache
    //
    function load($loadEle) {
      if (is_string($loadEle)) {
        if (!isset($GLOBALS['DX0_LOADED_CACHE'][$loadEle])) {
          $loadPath = $GLOBALS['DX0_DIR'] . "ele/" . $loadEle . "/code.ele";
          $fp = fopen($loadPath, "rb");
          if (!$fp) {
            $this->error( "File for element <B>$loadEle</B> could not be located at 
                          <B>$loadPath</B>" );
            return;
          }
          $ele = pickle_load($fp);
          $GLOBALS['DX0_JAVASCRIPT_BUFFER'] .= stripslashes($ele['javascript']);
	  unset($ele['javascript']);
	  $GLOBALS['DX0_LOADED_CACHE'][$loadEle] = $ele;
          fclose($fp);
        }
        return $loadEle;
      } else if (is_array($loadEle)) {
        if (!isset($GLOBALS['DX0_LOADED_CACHE'][$loadEle['nick']])) {
	  $GLOBALS['DX0_LOADED_CACHE'][$loadEle['nick']] = $loadEle;
          $GLOBALS['DX0_JAVASCRIPT_BUFFER'] .= $loadEle['javascript'];
	}
	return $loadEle['nick'];
      } else
        return;
    }

    function save($file) {
      $fp = fopen($file, "wb");

      pickle_dump($GLOBALS['DX0_LOADED_CACHE'][$this->nick], $fp, 1);
      fclose($fp);
    }

    //
    // propFilter combines similiar properties
    //
    function propFilter($props) {
      $rep = array ("x" => "left", "y" => "top", "z" => "z-index");
      while (list($k, $v) = each($rep)) {
        if (isset($props[$k])) {
	  $props[$v] = $props[$k];
          unset($props[$k]);
        }
      }
      return $props;
    }

    //
    // associates properties with the proper child elements
    //
    function setProperties($newProps) {
      $newProps = $this->propFilter($newProps);
      //
      // set defaults, applying properties where needed
      //
      while (list($k, $v) = each($GLOBALS['DX0_LOADED_CACHE'][$this->nick]['properties'])) {
        if ($k == "default") continue;
        if ($k == "drag") {
          if ($newProps[$k] == "on") {
            while (list($o, $od) = each($v)) {
              $this->propHandler($o, $this->name . $od);
	    }
	  }
	} else {
          while (list($o, $od) = each($v)) {
	    if (isset($newProps[$k])) {
	      if ($k == "z-index") {
	        $newProps[$k] += $od;
	      }
	      $this->propHandler($o, $newProps[$k]);
	    } else {
	      $this->propHandler($o, $od);
	    }
          }
	}
	if (isset($newProps[$k])) unset($newProps[$k]);
      }
      while (list($k, $v) = each($newProps)) {
        $this->propHandler($this->default . "." . $k, $v);
      }
    }

    function propHandler($k, $v) {
      $pp = explode(".", $k);
      if ($pp[0] == "this") {
        eval("\$this->" . $pp[1] . " = \$v;");
        // tag as an assignment.. i noticed that lots of stuff being assigned as
        // 'this' was mostly being used in the JavaScript anyways..
        $this->assigns[$pp[1]] = var_to_js($v);
      } else {
        eval("\$this->" . $pp[0] . "Props['" . $pp[1] . "'] = \$v;");
      }
    }

    //
    // interpreter for [^obj.prop^] code
    //
    function interpret($buf) {
      //
      // using ereg now.. more portable between PHP3 + 4 than strpos()
      //
      while ( ereg( "\[\^([[:alnum:]]+)\.([[:alnum:]]+)\^\]", $buf, $cmd) ) {
        $rep = $this->dealWith($cmd);
        $buf = str_replace($cmd[0], $rep, $buf);
      }
      return stripslashes($buf);
    }

    //
    // deals with the references (i.e. Block.top becomes $this->PARTS['Block']->properties['top'])
    //
    function dealWith($cmd) {
      $rep = "";
      if ($cmd[1] == "this") {
        $rep = $this->$cmd[2];
      } else if ($cmd[1] == "images") {
        if (strlen($this->skin) > 0) {
          $rep = $this->imageList[$cmd[2]];
        }
      } else {
        $obj = $this->PARTS[$cmd[1]];
        if ($cmd[2] == "openTag") {
          $rep = $obj->openTag();
        } else if ($cmd[2] == "closeTag") {  
          $rep = $obj->closeTag();
        } else {
          $rep = $obj->properties[$cmd[2]];
        }  
      }
      if (is_array($rep)) {
        $rep = array_to_js($rep);
      }
      return $rep;
    }


  }

  function var_to_js($v) {
    if (is_int($v) || ereg( "^[0-9]+$", $v ))
      return (int)$v;
    else if (is_array($v))
      return array_to_js($v);
    else
      return '"' . str_replace( '"', '\"', $v ) . '"';
  }

  function array_to_js($obj) {
    while (list($k, $v) = each($obj)) {
      $k = "\"$k\"";

      if (is_array($v)) {
        $v = array_to_js($v);
      } else if (is_string($v)) {
        $v = "\"$v\"";
      }
      $jsa[] = "$k: $v";
    }
    return "{" . implode(",", $jsa) . "}";
  }

?>
Return current item: DX0