Login   Register  
PHP Classes
elePHPant
Icontem

File: Xyndravandria/Averazain/HTMLBuilder.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Mauro Di Girolamo  >  Xyndravandria Averazain  >  Xyndravandria/Averazain/HTMLBuilder.php  >  Download  
File: Xyndravandria/Averazain/HTMLBuilder.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Xyndravandria Averazain
Handle AJAX requests with PHP classes
Author: By
Last change: Alpha 0.0.0
Date: 1 year ago
Size: 13,868 bytes
 

Contents

Class file image Download
<?php
/*
=============================================================================================================================================
|   This file is part of a project released under the terms of the Xyndravandria PHP License (XyndravandriaPHPLicense.txt).                 |
|                                                                                                                                           |
|   You should be given a copy of the Xyndravandria PHP License (XyndravandriaPHPLicense.txt) within the same directory as the README.md;   |
|   if not, you can get a copy at http://Xyndravandria.ohost.de/XyndravandriaPHPLicense.txt .                                               |
|                                                                                                                                           |
|   The copyright (c) of this project is owned by Mauro Di Girolamo <maurodigirolamo@.web.de>.                                              |
============================================================================================================================================|



Xyndravandria Averazain
-----------------------
Alpha 0.0.0

Xyndravandria is the name of a collection of projects designed and developed by Mauro Di Girolamo (maurodigirolamo@web.de); he is therefore the copyright (c) owner of Xyndravandria itself and all of its projects.

Xyndravandria Averazain is released under the terms of the Xyndravandria PHP License (XyndravandriaPHPLicense.txt). You should be given a copy of the Xyndravandria PHP License (XyndravandriaPHPLicense.txt) within the same directory as the README.md; if not, you can get a copy at http://Xyndravandria.ohost.de/XyndravandriaPHPLicense.txt . There might be a release under a freer license for a later, more stable version.

The documentation is either included in ./admin_media/Documentation/ or can be read at http://Xyndravandria.ohost.de/Averazain/Documentation/.

All projects:

   Xyndravandria Averazain
   http://github.com/MauroDiGirolamo/Xyndravandria_Averazain
   PHP
   Averazain is an Ajax framework supporting also JavaScript disabled clients perfectly - including search engines like Google.
   
   Xyndravandria Dyverath
   http://github.com/MauroDiGirolamo/Xyndravandria_Dyverath
   PHP
   Dyverath is a database access wrapper.
   
   Xyndravandria Erozaver
   http://github.com/MauroDiGirolamo/Xyndravandria_Erozaver
   PHP
   Erozaver is a class extending the type hinting given by the PHP engine (additional support for basic type hinting and size constraints).
   
   Xyndravandria Mondraviel
   http://github.com/MauroDiGirolamo/Xyndravandria_Mondraviel
   PHP
   Mondraviel is a class used to separate HTML from PHP code by firstly register models - files containing place holders embedded in HTML code - and then later fill them dynamically with content by passing values for the place holders.
*/

namespace Xyndravandria\Averazain;

// TODO: File uploads.
// TODO: Remove special array usage in addional arguments?
/// The HTMLBuilder is used to generate HTML providing
/// perfect support for JavaScript disabled clients.
abstract class HTMLBuilder {
   
   /// Generates an anchor tag.
   /// @public
   /// @static
   /// @param string $IndexFile: The index file of your
   /// website.
   /// @param string $Class: The class whose method is to
   /// be called.
   /// @param string $Method: The method to be called.
   /// @param array $Arguments: Arguments to be passed to
   /// the method.
   /// @param string $InnerHTML: The inner HTML of the
   /// anchor tag.
   /// @param array $Assigments: Further assigments within
   /// the anchor tag, for instance used to set a class
   /// attribute. The indexes of the array refer to the
   /// attributes of the HTML tag.
   /// @note Both $Arguments, $InnerHTML and $Assigments
   /// are optional parameters. @n
   /// $InnerHTML is not passed or empty, the hyperlink
   /// itself will be put between the anchor tags. @n
   /// Furthermore,
   /// @verbatim HTMLBuilder::HyperLink( 'Index.php', 'Class', 'Method', array( ), 'Inner HTML' ) == HTMLBuilder::Hyperlink( 'Index.php', 'Class', 'Method', 'Inner HTML' ) @endverbatim
   /// Lastly, if you would like to pass a JavaScript
   /// expression (function call, attribute, ...) on
   /// client level as an (additional) parameter - which
   /// should therefore not be put between string quotes
   /// -, pass it as an array. Since the expression will
   /// logically <i>not<i> be added to the GET arguments
   /// of the file assigned to the target attribute of
   /// the form tag, you have to pass another value which
   /// should be passed as a GET argument. This is
   /// relevant when it comes to JavaScript disabled
   /// clients. To sum up, the array structure has to be:
   /// ( [0] => [JavaScript expression] ; [1] => [GET
   /// argument] ) @n
   /// <br><b>Technical explanation:</b>
   /// The onclick event of the anchor tag will only be
   /// called if a client does support JavaScript and in
   /// case that happens, due to the 'return false;', the
   /// client will not follow the hyperlink assigned to
   /// the href attribute after invoking the onclick
   /// event. If JavaScript is disabled, the client will
   /// normally follow the hyperlink and not invoke the
   /// onclick event.
   public static function Hyperlink( $IndexFile, $Class, $Method, $Arguments = array( ), $InnerHTML = '', array $Assigment = array( ) ) {
      //\settype( $IndexFile, 'string' );
      //\settype( $Class, 'string' );
      //\settype( $Method, 'string' );
      if( \is_string( $Arguments ) ) {
         $InnerHTML = $Arguments;
         $Arguments = array( );
      }
      if( ! \class_exists( $Class ) || ! \method_exists( $Class, 'Anchor' ) )
         throw new XyndravandriaAverazainException( 'Either $Class does not exist or $Class does not offer the Anchor( ) method.' );
      else {
         $Anchor = $Class::Anchor( $Method ); // TODO: Check whether anchor (or better: class) not found!
         list( $HashListing, $MethodListing ) = self::ArgumentListing( $Arguments );
         $AssigmentListing = self::AssigmentListing( $Assigment );
         // TODO: Correct path making (also think of already existing GET parameters; the port).
         $URL = 'http://' . $_SERVER[ 'SCRIPT_NAME' ] . '/' . $IndexFile . '?' . $Anchor . $HashListing;
         return '<a href = "' . $URL . '" onclick = "' . $Class . '.' . $Method . '( ' . $MethodListing . ' ); return false;"' . $AssigmentListing . '>' . ( empty( $InnerHTML ) ? ( $URL ) : $InnerHTML ) . '</a>';
      }
      return;
   }
   
   /// Generates a form tag.
   /// @public
   /// @static
   /// @param string $IndexPage: The index file of your
   /// website.
   /// @param string $Class: The class whose method the
   /// form data is to be passed.
   /// @param string $Method: The method of the class the
   /// form data is to be passed to.
   /// @param array $AdditionalArguments: Additional
   /// Arguments to be passed to the method.
   /// @param string $InnerHTML: The inner HTML of the
   /// form tag.
   /// @param array $Assigments: Further assigments
   /// within the form tag, for instance used to set a
   /// class attribute. The indexes of the array refer to
   /// the attributes of the HTML tag.
   /// @note Both $AdditionalArguments and $Assigments
   /// are optional parameters. @n
   /// Furthermore, $AdditionalArguments may also contain
   /// $Assigments. Whether $AdditionalArguments really
   /// contains additional arguments or assigments is
   /// determined by checking whether there is at least
   /// one string index. @n
   /// Lastly, if you would like to pass a JavaScript
   /// expression (function call, attribute, ...) on
   /// client level as an (additional) parameter - which
   /// should therefore not be put between string quotes
   /// -, pass it as an array. Since the expression will
   /// logically <i>not<i> be added to the GET arguments
   /// of the file assigned to the target attribute of
   /// the form tag, you have to pass another value which
   /// should be passed as a GET argument. This is
   /// relevant when it comes to JavaScript disabled
   /// clients. To sum up, the array structure has to be:
   /// ( [0] => [JavaScript expression] ; [1] => [GET
   /// argument] ) @n
   /// <span style = "color: green">Tip:</span> Always
   /// distribute the URL with the GET arguments and not
   /// the one with the JavaScript hash when externally
   /// linking to a page. Whenever a JavaScript enabled
   /// client requests a site via GET arguments,
   /// Averazain will automatically make the client
   /// reload the page to change the URL to the other one
   /// with JavaScript hash while also of course cutting
   /// of the GET arguments. @n
   /// <br><b>Technical explanation:</b>
   /// The onsubmit event of the form tag will only be
   /// called if a client does support JavaScript and in
   /// case that happens, due to the 'return false;', the
   /// client will not send the data to the hyperlink
   /// assigned to the action attribute after invoking
   /// the onsubmit event. If JavaScript is disabled, the
   /// client will normally send the data to the
   /// hyperlink and not invoke the onsubmit event. @n
   /// Furthermore, the AttachForm( ) method will be
   /// called in the onsubmit event to tell Averazain to
   /// also pass the form data within the Ajax request
   /// before it is sent. @n
   /// Eventually, there will be added the prefix to the
   /// name attribute of any input, select and textarea
   /// tag. This is necessary to make it possible for
   /// Averazain to later distinguish form values from
   /// method arguments in order to save them into
   /// Averazain::$FormData.
   public static function Form( $IndexFile, $Class, $Method, $InnerHTML, array $AdditionalArguments = array( ), array $Assigments = array( ) ) { // TODO: Extend behaviour for select tags (send on change?).
      //\settype( $IndexFile, 'string' );
      //\settype( $Class, 'string' );
      //\settype( $Method, 'string' );
      //\settype( $InnerHTML, 'string' );
      if( \is_string( \key( $AdditionalArguments ) ) ) {
         $Assigments = $AdditionalArguments;
         $AdditionalArguments = array( );
      }
      $Anchor = $Class::Anchor( $Method ); // TODO: Check whether anchor (or better: class) not found!
      list( $HashListing, $MethodListing ) = self::ArgumentListing( $AdditionalArguments );
      $AssigmentListing = self::AssigmentListing( $Assigments );
      foreach( array( 'input', 'select', 'textarea' ) as $Tag )
        $InnerHTML = \preg_replace( '#' . $Tag . '(.+)name(.+)=(.+)"(.+)"#U', $Tag . '$1name$2=$3"Xyndravandria->Averazain->Request->FormData->$4"', $InnerHTML ); // TODO: Optimise ; escpecially too many . ...
      return '<form action = "' . $IndexFile . '?' . $Anchor . $HashListing . '" onsubmit = "XyndravandriaAverazain.AttachForm( this ); ' . $Class . '.' . $Method . '( ' . $MethodListing . ' ); return false;" method = "post"' . $AssigmentListing . '>' . $InnerHTML . '</form>';
   }
   
   /// Creates two argument listing for the hash and
   /// JavaScript method.
   /// @private
   /// @static
   /// @param array $Arguments: The arguments to be
   /// parsed.
   /// @returns array of string
   /// @note Lastly, if you would like to pass a
   /// JavaScript expression (function call, attribute,
   /// ...) on client level as an (additional) parameter
   /// - which should therefore not be put between string
   /// quotes -, pass it as an array. Since the
   /// expression will logically <i>not<i> be added to
   /// the GET arguments of the file assigned to the
   /// target attribute of the form tag, you have to pass
   /// another value which should be passed as a GET
   /// argument. This is relevant when it comes to
   /// JavaScript disabled clients. To sum up, the array
   /// structure has to be: ( [0] => [JavaScript
   /// expression] ; [1] => [GET argument] )
   public static function ArgumentListing( array $Arguments ) {
      $HashListing = empty( $Arguments ) ? '' : ':';
      foreach( $Arguments as $Value ) {
         if( \is_array( $Value ) ) {
            if( \count( $Value ) < 2 )
               throw new XyndravandriaAverazainException( 'Invalid array passed as an element in $Arguments (less than two elements).' );
            else
               $Value = $Value[ 1 ];
         }
         $HashListing .= $Value . ',';
      }
      $HashListing = empty( $Arguments ) ? $HashListing : \substr( $HashListing, 0, \strlen( $HashListing ) - 1 );
      $MethodListing = '';
      foreach( $Arguments as $Value )
         if( \is_array( $Value ) ) {
            if( \count( $Value ) < 2 )
               throw new XyndravandriaAverazainException( 'Invalid array passed as an element in $Arguments (less than two elements).' );
            else
               $MethodListing .= ' ' . $Value[ 0 ] . ' ,';
         } elseif( \is_numeric( $Value ) )
            $MethodListing .= ' ' . $Value . ' ,';
         else
            $MethodListing .= ' \'' . \addslashes( $Value ) . '\' ,';
      $MethodListing = empty( $Arguments ) ? $MethodListing : \substr( $MethodListing, 0, \strlen( $MethodListing ) - 1 );
      return array( $HashListing, $MethodListing );
   }
      
   /// Creates an assigment listing used within a HTML
   /// tag.
   /// @private
   /// @static
   /// @param array $Assigments: The assigments as an
   /// array.
   /// @returns string
   private static function AssigmentListing( array $Assigments ) {
      $AssigmentListing = '';
      foreach( $Assigments as $Attribute => $Value )
         $AssigmentListing .= ' ' . $Attribute . ' = "' . \addslashes( $Value ) . '"';
      return $AssigmentListing;
   }
   
}
?>