Source for file gloolink.php
Documentation is available at gloolink.php
* GloryLands, a Web-Based, Massive Multiplayer Online RPG/Strategy Game
* Copyright (C) 2008-09 John Haralampidis <jïhnys2[at]gmail.cïm>
* 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 3 of the License, 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, see <http://www.gnu.org/licenses/>.
* For any help/suggestions or troubleshooting you can see the
* project community website at <http://www.glorylands.gr>
* @license GNU/GPLv3 GNU General Public License version 3
* @author John Haralampidis <jïhnys2[at]gmail.cïm>
* @copyright Copyright (C) 2007-2009, John Haralampidis
* This class provides the basic functions that implements
* the PHP-Javascript linking interface.
* It also provides the management system of the
* permanent structure storage inside the section.
static private $ObjectStack;
* @todo The session management should be performed
static public function save() {
// #@# This should be implemented using GLCache::Set()
$_SESSION['DOM'] = self::$ObjectStack;
static public function initialize($clean_start= false) {
// #@# This should be implemented using GLCache::Get()
self::$ObjectStack = array();
$_SESSION['DOM'] = array();
if (isset ($_SESSION['DOM'])) {
self::$ObjectStack = $_SESSION['DOM'];
self::$ObjectStack = array();
// =======[ PHP - JS Interaction ]================================================
foreach (self::$ObjectStack as $info) {
if ($info['id'] == $linkid) return $info['object'];
foreach (self::$ObjectStack as $info) {
if ($info['object']->classname == $class) $stack[] = $info['object'];
foreach (self::$ObjectStack as $info) {
if ($info['object']->id == $object_id) return $info['object'];
static public function register($class, $js_class) {
// Make sure $ObjectStack is defined (since we are static)
if (!self::$ObjectStack) self::$ObjectStack= array();
$name = get_class($class);
$id = sizeof(self::$ObjectStack);
// Register the object on stack
self::$ObjectStack[] = array(
// (Note that the class that called this function ($class)
// MUST assign this ID on the linkID property)
return '#GL.DOM.get('. $object->linkID. ')#';
if (is_array($structure)) {
foreach ($structure as $var => $value) {
// Calculate the variable name to use for path nesting
// If this element is an object, do some additional processing
// The object is a GLOOObject reference, return null
// for now, but register the variable for a delay-reference
// If the object is not a GLOOObject instance, extract it's
// variables, and act like an array
$structure[$var] = self::compile_structure(get_object_vars($value), $delay_ref, $path. $subvar);
// If this element is an array, perform a subl-evel structure analysis
$structure[$var] = self::compile_structure($value, $delay_ref, $path. '.'. $var);
// Now that the array is properly formatted, return the string representation
// Elseways, use json_encode to convert the structure into
// something that Javascript can handle
// Make sure $ObjectStack is defined (since we are static)
if (!self::$ObjectStack) self::$ObjectStack= array();
$script = "/* GloryLands Web-Based MMORPG v0.6 */\r\n";
$script.= "/* Object-Oriented API v0.1 Alpha */\r\n";
$script.= "/* (C) Copyright 2009 - John Haralampidis */\r\n";
$script.= "// Initialize Object Structure:\r\n";
// Start creating the object instances, recording in the same
// time the delay-mapping that should be done in order to properly set-up
foreach (self::$ObjectStack as $i => $object) {
// Prepare the variables that should be used for the initialization of the JS unit
$init_vars = array_merge($object['object']->_vars(), array(
'root' => $object['object']->root,
'parent' => $object['object']->parent,
'previous' => $object['object']->previous,
'next' => $object['object']->next,
'children' => $object['object']->children
// Write the code that should initialize the JS interface, and in the same
// time extract the delay references
$script.= 'GL.DOM.register(new '. $object['class']. '('. self::compile_structure($init_vars, $delay_ref, $object['id']. '#'). ','. $object['id']. '));'. "\r\n";
// Merk the object as synced
self::$ObjectStack[$i]['object']->synced = true;
// Compile the delay-mapping in order to implement
// the object references.
$script.= "// Perform delay-mapping of references:\r\n";
foreach ($delay_ref as $ref) {
$part = explode('#', $ref['path']);
$ref['path'] = substr($part[1],1);
if (!isset ($groups[$group])) $groups[$group]= 'with(GL.DOM.get('. $group. ')){';
$groups[$group].= $ref['path']. "=GL.DOM.get(". $ref['id']. ");";
$script.= implode("};\r\n", $groups). "};\r\n";
$script.= "$(window).addEvent('domready',function(){GL.Core.run();});\r\n";
// Return the compiled script
// =======[ Async I/O ]===========================================================
// #@# This should be implemented using GLCache::Get()
if (!isset ($_SESSION['messages'])) return array();
$msg = $_SESSION['messages'];
unset ($_SESSION['messages']);
// #@# This should be implemented using GLCache::Set()
if (!isset ($_SESSION['messages'])) $_SESSION['messages'] = array();
$_SESSION['messages'][] = array(
'id' => $linkid, 'c' => $call, 'p' => $parm
|