ManiaLib 1.0b1 API Reference

Source for file Manialink.class.php

Documentation is available at Manialink.class.php

  1. <?php
  2. /**
  3.  * @author Maxime Raoust
  4.  * @copyright 2009-2010 NADEO
  5.  * @package ManiaLib
  6.  */
  7.  
  8. /**#@+
  9.  * @ignore
  10.  */
  11. require_onceAPP_FRAMEWORK_GUI_TOOLKIT_PATH.'standard.php' );
  12. require_onceAPP_FRAMEWORK_GUI_TOOLKIT_PATH.'layouts/AbstractLayout.class.php' );
  13. /**#@-*/
  14.  
  15. /**
  16.  * GUI Toolkit
  17.  * Manialink GUI Toolkit main class
  18.  * @package ManiaLib
  19.  * @subpackage GUIToolkit
  20.  */
  21. abstract class Manialink
  22. {
  23.     /**#@+
  24.      * @ignore
  25.      */
  26.     public static $domDocument;
  27.     public static $parentNodes;
  28.     public static $parentLayouts;
  29.     public static $linksEnabled true;
  30.     protected static $dicos array();
  31.     /**#@-*/
  32.     
  33.     /**
  34.      * Loads the Manialink GUI toolkit. This should be called before doing
  35.      * anything with the toolkit.
  36.      * @param bool Whether you want to create the root "<manialink>" element in the XML
  37.      * @param int The timeout value in seconds. Use 0 if you have dynamic pages to avoid caching
  38.      */
  39.     final public static function load($createManialinkElement true$timeoutValue=0)
  40.     {
  41.         self::$domDocument new DOMDocument('1.0''utf8');
  42.         self::$parentNodes array();
  43.         self::$parentLayouts array();
  44.  
  45.         if($createManialinkElement)
  46.         {
  47.             $manialink self::$domDocument->createElement('manialink');
  48.             self::$domDocument->appendChild($manialink);
  49.             self::$parentNodes[$manialink;
  50.                 
  51.             $timeout self::$domDocument->createElement('timeout');
  52.             $manialink->appendChild($timeout);
  53.             $timeout->nodeValue $timeoutValue;
  54.         }
  55.         else
  56.         {
  57.             $frame self::$domDocument->createElement('frame');
  58.             self::$domDocument->appendChild($frame);
  59.             self::$parentNodes[$frame;
  60.         }
  61.     }
  62.  
  63.     /**
  64.      * Renders the Manialink
  65.      * @param boolean Wehther you want to return the XML instead of printing it
  66.      */
  67.     final public static function render($return false)
  68.     {
  69.         if(self::$dicos)
  70.         {
  71.             array_map(array('Manialink''includeManialink')self::$dicos);
  72.         }
  73.         if($return)
  74.         {
  75.             return self::$domDocument->saveXML();
  76.         }
  77.         else
  78.         {
  79.             header('Content-Type: text/xml; charset=utf-8');
  80.             echo self::$domDocument->saveXML();
  81.         }
  82.     }
  83.  
  84.     /**
  85.      * Creates a new Manialink frame, with an optionnal associated layout
  86.      *
  87.      * @param float X position
  88.      * @param float Y position
  89.      * @param float Z position
  90.      * @param float Scale (default is null or 1)
  91.      * @param AbstractLayout The optionnal layout associated with the frame. If
  92.      *  you pass a layout object, all the items inside the frame will be
  93.      *  positionned using constraints defined by the layout
  94.      */
  95.     final public static function beginFrame($x=0$y=0$z=0$scale=nullAbstractLayout $layout=null)
  96.     {
  97.         // Update parent layout
  98.         $parentLayout end(self::$parentLayouts);
  99.         if($parentLayout instanceof AbstractLayout)
  100.         {
  101.             // If we have a current layout, we have a container size to deal with
  102.             if($layout instanceof AbstractLayout)
  103.             {
  104.                 $ui new Spacer($layout->getSizeX()$layout->getSizeY());
  105.                 $ui->setPosition($x$y$z);
  106.  
  107.                 $parentLayout->preFilter($ui);
  108.                 $x += $parentLayout->xIndex;
  109.                 $y += $parentLayout->yIndex;
  110.                 $z += $parentLayout->zIndex;
  111.                 $parentLayout->postFilter($ui);
  112.             }
  113.         }
  114.  
  115.         // Create DOM element
  116.         $frame self::$domDocument->createElement('frame');
  117.         if($x || $y || $z)
  118.         {
  119.             $frame->setAttribute('posn'$x.' '.$y.' '.$z);
  120.         }
  121.         end(self::$parentNodes)->appendChild($frame);
  122.         if($scale)
  123.         {
  124.             $frame->setAttribute('scale'$scale);
  125.         }
  126.  
  127.         // Update stacks
  128.         self::$parentNodes[$frame;
  129.         self::$parentLayouts[$layout;
  130.     }
  131.  
  132.     /**
  133.      * Closes the current Manialink frame
  134.      */
  135.     final public static function endFrame()
  136.     {
  137.         if(!end(self::$parentNodes)->hasChildNodes())
  138.         {
  139.             end(self::$parentNodes)->nodeValue ' ';
  140.         }
  141.         array_pop(self::$parentNodes);
  142.         array_pop(self::$parentLayouts);
  143.     }
  144.     
  145.     /**
  146.      * Redirects the user to the specified Manialink
  147.      */
  148.     final public static function redirect($link$render true)
  149.     {
  150.         self::$domDocument new DOMDocument('1.0''utf8');
  151.         self::$parentNodes array();
  152.         self::$parentLayouts array();
  153.  
  154.         $redirect self::$domDocument->createElement('redirect');
  155.         $redirect->appendChild(self::$domDocument->createTextNode($link));
  156.         self::$domDocument->appendChild($redirect);
  157.         self::$parentNodes[$redirect;
  158.  
  159.         if($render)
  160.         {
  161.             if(ob_get_contents())
  162.             {
  163.                 ob_clean();
  164.             }
  165.             header('Content-Type: text/xml; charset=utf-8');
  166.             echo self::$domDocument->saveXML();
  167.             exit;
  168.         }
  169.         else
  170.         {
  171.             return self::$domDocument->saveXML();
  172.         }
  173.     }
  174.  
  175.     /**
  176.      * Append some XML code to the document
  177.      * @param string Some XML code
  178.      */
  179.     static function appendXML($XML)
  180.     {
  181.         $doc new DOMDocument();
  182.         $doc->loadXML($XML);
  183.         $node self::$domDocument->importNode($doc->firstChildtrue);
  184.         end(self::$parentNodes)->appendChild($node);
  185.     }
  186.  
  187.     /**
  188.      * Disable all Manialinks, URLs and actions of GUIElement objects as long as it is disabled
  189.      */
  190.     static function disableLinks()
  191.     {
  192.         self::$linksEnabled false;
  193.     }
  194.  
  195.     /**
  196.      * Enable links
  197.      */
  198.     static function enableLinks()
  199.     {
  200.         self::$linksEnabled true;
  201.     }
  202.     
  203.     /**
  204.      * Shortcut for including files in manialinks
  205.      */
  206.     static function includeManialink($url)
  207.     {
  208.         $ui new IncludeManialink();
  209.         $ui->setUrl($url);
  210.         $ui->save();
  211.     }
  212.     
  213.     /**
  214.      * Add a dictionnary file, will be included when rendering
  215.      */
  216.     static function addDico($url)
  217.     {
  218.         self::$dicos[$url;
  219.     }
  220. }
  221.  
  222. ?>