phpDocumentor GloryLands
DOM
[ class tree: GloryLands ] [ index: GloryLands ] [ all elements ]

Source for file gldom_element.php

Documentation is available at gldom_element.php

  1. <?php
  2. /**
  3.   * GloryLands DOM Structure - Element Class
  4.   *
  5.   * <pre>
  6.   * GloryLands, a Web-Based, Massive Multiplayer Online RPG/Strategy Game
  7.   * Copyright (C) 2008-09  John Haralampidis <jïhnys2[at]gmail.cïm>
  8.   *
  9.   * This program is free software: you can redistribute it and/or modify
  10.   * it under the terms of the GNU General Public License as published by
  11.   * the Free Software Foundation, either version 3 of the License, or
  12.   * (at your option) any later version.
  13.   *
  14.   * This program is distributed in the hope that it will be useful,
  15.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.   * GNU General Public License for more details.
  18.   *
  19.   * You should have received a copy of the GNU General Public License
  20.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21.   *
  22.   * For any help/suggestions or troubleshooting you can see the
  23.   * project community website at <http://www.glorylands.gr>
  24.   * </pre>
  25.   *
  26.   * @license GNU/GPLv3 GNU General Public License version 3
  27.   * @package GloryLands
  28.   * @subpackage DOM
  29.   * @author John Haralampidis <jïhnys2[at]gmail.cïm>
  30.   * @copyright Copyright (C) 2007-2009, John Haralampidis
  31.   * @version 1.0
  32.   */
  33.  
  34. /**
  35.   * Basic DOM Element
  36.   *
  37.   * This class provides the basic class that implements
  38.   * the DOM element.
  39.   *
  40.   * @subpackage DOM
  41.   * @version 1.0
  42.   */
  43. class GLDOMElement extends GLDataset {
  44.     
  45.     public $children;
  46.     public $parent;
  47.     public $next;
  48.     public $previous;
  49.     public $root;
  50.     
  51.     public $classname;
  52.     public $id;
  53.     
  54.     public function __construct($class=NULL$id=NULL{
  55.         parent::__construct();
  56.         
  57.         $this->children = array();
  58.         if ($class != NULL$this->classname = $class;
  59.         if ($id != NULL$this->id = $id;
  60.         $this->root = GLDOM::$stage;
  61.     }
  62.     
  63.     public function add_child(&$object{
  64.         // Add the child and find it's ID
  65.         $this->children[&$object;
  66.         $id sizeof($this->children)-1;
  67.         
  68.         // Assign parent
  69.         $object->parent $this;
  70.         $object->root &$this->root;
  71.         $object->next false;
  72.         
  73.         // Calculate next/previous
  74.         if ($id == 0{
  75.             $object->previous false;
  76.         else {
  77.             $object->previous &$this->children[$id-1];
  78.             $object->previous->next $object;
  79.         }
  80.         
  81.         // Return the object
  82.         return $object;
  83.     }
  84.     
  85.     public function remove_child($objid{
  86.         // Calculate the object id
  87.         if (!is_numeric($objid)) {
  88.             $objid array_search($objid$this->vars);
  89.         }
  90.         
  91.         // Make sure we have a valid id
  92.         if (($objid 0)  || ($objid === false)) return false;
  93.         
  94.         // Unset parent
  95.         $this->vars[$objid]->parent false;
  96.         
  97.         // Calculate previous/next
  98.         if ($this->vars[$objid]->previous !== false{
  99.             $this->vars[$objid]->previous->next &$this->vars[$objid]->next;
  100.         
  101.         
  102.         // Unbind previous/next
  103.         $this->vars[$objid]->previous false;
  104.         $this->vars[$objid]->next false;
  105.         $this->vars[$objid]->root false;
  106.     }
  107.  
  108.     /**
  109.       * Return all the matching elements
  110.       * 
  111.       * Find a child using the following search pattern simmilar
  112.       * to CSS selector on HTML DOM:
  113.       *
  114.       *  [<classs>].[<id>] : Both Class and ID can be ommited. If ommited,
  115.       *                         everything will be selected
  116.       *
  117.       * @param    string    $search    The search pattern
  118.       * @param    int        $count    [Optional] The maximum entries to return
  119.       * @return    array            Return the child objects that match the criteria
  120.       *
  121.       */
  122.     public function find_children($search,$count=0{
  123.         
  124.         // Extract class/id from search string
  125.         if (strstr($search,'.')) {
  126.             $parts explode('.'$search);
  127.             if ($parts[0== ''{
  128.                 $class false;
  129.                 $name $parts[0];
  130.             else {
  131.                 $class $parts[0];
  132.                 $name $parts[1];
  133.             }
  134.         else {
  135.             $class $search;
  136.             $name false;
  137.         }
  138.         
  139.         // Search strings
  140.         $res array();
  141.         foreach ($c as $this->children{
  142.             $match true;
  143.             if ($class !== false$match $match && ($c->classname == $class);  
  144.             if ($name !== false$match $match && ($c->id == $name)
  145.             if ($match$res[$c
  146.             
  147.             // Check for maximum elements
  148.             if (($count != 0&& (sizeof($res>= $count)) break;
  149.         }
  150.         
  151.         // Return the arrays
  152.         return $res;
  153.         
  154.     }
  155.  
  156.     /**
  157.       * Return the first matching element
  158.       * 
  159.       * Find a child using the following search pattern simmilar
  160.       * to CSS selector on HTML DOM:
  161.       *
  162.       *  [<classs>].[<id>][@<index>]
  163.       *
  164.       *  Both Class and ID can be ommited. If ommited, everything will be selected.
  165.       *  Also, in contrast with find_children, you can specify a specific child
  166.       *  instead of the first. <index> is 1-based.
  167.       *
  168.       * @param    string    $search    The search pattern
  169.       * @return    array|bool           Return the child objects that match the criteria
  170.       */
  171.     public function find_child($search{
  172.     
  173.         // Locate the index
  174.         $max_items 1;
  175.         $index false;
  176.         if (strstr($search'@')) {
  177.             $pars explode('@'$search);
  178.             $search $parts[0];
  179.             $index = (int)$parts[1];
  180.             if ($index != 0{
  181.                 $max_items $index;                
  182.             }
  183.         }
  184.         
  185.         // Perform the search
  186.         $items $this->find_children($search,1);
  187.         if (sizeof($items1{
  188.             return false;
  189.         else {
  190.             if ($index{
  191.                 if (isset($items[$index])) {
  192.                     return $items[$index];
  193.                 else {
  194.                     return false;
  195.                 }
  196.             else {
  197.                 return $items[0];
  198.             }
  199.         }
  200.         
  201.     }
  202.  
  203. }
  204.  
  205. ?>

Documentation generated on Tue, 13 Oct 2009 23:49:02 +0300 by phpDocumentor 1.4.1