AJAX
[ class tree: AJAX ] [ index: AJAX ] [ all elements ]

Source for file Action.php

Documentation is available at Action.php

  1. <?php
  2. /**
  3.  * OO AJAX Implementation for PHP, contains HTML_AJAX_Action
  4.  *
  5.  * @category   HTML
  6.  * @package    AJAX
  7.  * @author     Elizabeth Smith <auroraeosrose@gmail.com>
  8.  * @copyright  2005-2006 Elizabeth Smith
  9.  * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  10.  * @version    Release: @package_version@
  11.  */
  12.  
  13. /**
  14.  * Require the response class and json serializer
  15.  */
  16. require_once 'HTML/AJAX/Response.php';
  17. require_once 'HTML/AJAX/Serializer/JSON.php';
  18.  
  19. /**
  20.  * Helper class to eliminate the need to write javascript functions to deal with data
  21.  *
  22.  * This class creates information that can be properly serialized and used by
  23.  * the haaction serializer which eliminates the need for php users to write javascript
  24.  * for dealing with the information returned by an ajax method - instead the javascript
  25.  * is basically created for them
  26.  *
  27.  * @version   $Id: Action.php 537 2006-08-12 01:48:12Z emsmith $
  28.  */
  29. {
  30.  
  31.     /**
  32.      * Content type for the HAA response
  33.      *
  34.      * goofy but unique content type to tell the javascript which deserializer to use
  35.      * overrides HTML_AJAX_Response
  36.      *
  37.      * @var string 
  38.      * @access public
  39.      */
  40.     var $contentType = 'application/html_ajax_action';
  41.  
  42.     /**
  43.      * An array holding all the actions for the class
  44.      *
  45.      * these have numeric keys and each new action is added on the end, remember
  46.      * these are executed in the order added
  47.      *
  48.      * @var array 
  49.      * @access private
  50.      */
  51.     var $_actions;
  52.  
  53.     /**
  54.      * Prepends data to the attribute identified by id
  55.      *
  56.      * The data will be added to the beginning of the attribute identified by the id
  57.      * sent, id must be unique
  58.      *
  59.      * $response->prependAttr('myid', 'class', 'red');
  60.      * $response->prependAttr('myid', array('class' => 'red', 'innerHTML' => 'this is an error'));
  61.      *
  62.      * @param   string   $id    id for a specific item on the page <div id="myid"></div>
  63.      * @param   string|array  $attribute    either an array of attribute/data pairs or a string attribute name
  64.      * @param   mixed   $data    should be NULL if attribute is an array, otherwise data you wish to set the attribute to
  65.      * @return  void 
  66.      * @access public
  67.      */
  68.     function prependAttr($id$attribute$data = NULL)
  69.     {
  70.         if(!is_null($data))
  71.         {
  72.             $attribute = array($attribute => $data);
  73.         }
  74.         $this->_actions[= array(
  75.             'action' => 'prepend',
  76.             'id' => $id,
  77.             'attributes' => $attribute,
  78.             'data' => $data,
  79.         );
  80.         return;
  81.     }
  82.  
  83.     /**
  84.      * Appends data to the attribute identified by id
  85.      *
  86.      * The data will be added to the end of the attribute identified by the id
  87.      * sent, id must be unique
  88.      *
  89.      * $response->appendAttr('myid', 'class', 'red');
  90.      * $response->appendAttr('myid', array('class' => 'red', 'innerHTML' => 'this is an error'));
  91.      *
  92.      * @param   string   $id    id for a specific item on the page <div id="myid"></div>
  93.      * @param   string|array  $attribute    either an array of attribute/data pairs or a string attribute name
  94.      * @param   mixed   $data    should be NULL if attribute is an array, otherwise data you wish to set the attribute to
  95.      * @return  void 
  96.      * @access public
  97.      */
  98.     function appendAttr($id$attribute$data = NULL)
  99.     {
  100.         if(!is_null($data))
  101.         {
  102.             $attribute = array($attribute => $data);
  103.         }
  104.         $this->_actions[= array(
  105.             'action' => 'append',
  106.             'id' => $id,
  107.             'attributes' => $attribute,
  108.         );
  109.         return;
  110.     }
  111.  
  112.     /**
  113.      * Assigns data to the attribute identified by id overwriting any previous values
  114.      *
  115.      * The data will be assigned to the attribute identified by the id
  116.      * sent, id must be unique
  117.      *
  118.      * $response->assignAttr('myid', 'class', 'red');
  119.      * $response->assignAttr('myid', array('class' => 'red', 'innerHTML' => 'this is an error'));
  120.      *
  121.      * @param   string   $id    id for a specific item on the page <div id="myid"></div>
  122.      * @param   string|array  $attribute    either an array of attribute/data pairs or a string attribute name
  123.      * @param   mixed   $data    should be NULL if attribute is an array, otherwise data you wish to set the attribute to
  124.      * @return  void 
  125.      * @access public
  126.      */
  127.     function assignAttr($id$attribute$data = NULL)
  128.     {
  129.         if(!is_null($data))
  130.         {
  131.             $attribute = array($attribute => $data);
  132.         }
  133.         $this->_actions[= array(
  134.             'action' => 'assign',
  135.             'id' => $id,
  136.             'attributes' => $attribute,
  137.         );
  138.         return;
  139.     }
  140.  
  141.     /**
  142.      * Deletes or assigns a value of an empty string to an attribute
  143.      *
  144.      * You may send either a single attribute or an array of attributes to clear
  145.      *
  146.      * $response->clearAttr('myid', 'class');
  147.      * $response->clearAttr('myid', array('class', 'innerHTML'));
  148.      *
  149.      * @param   string   $id    id for a specific item on the page <div id="myid"></div>
  150.      * @param   string|array  $attribute    either an array of attribute/data pairs or a string attribute name
  151.      * @return  void 
  152.      * @access public
  153.      */
  154.     function clearAttr($id$attribute)
  155.     {
  156.         if(!is_array($attribute))
  157.         {
  158.             $attribute = array($attribute);
  159.         }
  160.         $this->_actions[= array(
  161.             'action' => 'clear',
  162.             'id' => $id,
  163.             'attributes' => $attribute,
  164.         );
  165.         return;
  166.     }
  167.  
  168.     /**
  169.      * create a dom node via javascript
  170.      *
  171.      * higher level dom manipulation - creates a new node to insert into the dom
  172.      * You can control where the new node is inserted with two things, the insertion
  173.      * type and the id/  The type should be append, prepend, insertBefore, or insertAfter
  174.      *
  175.      * The id is a sibling node - like a div in the same div you want to add more to
  176.      * If you choose to append or prepend a node it will be placed at the beginning
  177.      * or end of the node with the id you send. If you choose insertBefore or
  178.      * InsertAfter it will be put right before or right after the node you specified.
  179.      * You can send an array of attributes to apply to the new node as well,
  180.      * so you don't have to create it and then assign Attributes.
  181.      *
  182.      * $response->createNode('myid', 'div');
  183.      * $response->createNode('submit', 'input',
  184.      *   array('id' => 'key',
  185.      *         'name' => 'key',
  186.      *         'type' => 'hidden',
  187.      *         'value' => $id),
  188.      *   'insertBefore');
  189.      *
  190.      * @param   string   $id    id for a specific item on the page <div id="myid"></div>
  191.      * @param   string   $tag    html node to create
  192.      * @param   array   $attributes    array of attribute -> data to fill the node with
  193.      * @return  void 
  194.      * @access public
  195.      */
  196.     function createNode($id$tag$attributes$type 'append')
  197.     {
  198.         $types = array('append''prepend''insertBefore''insertAfter');
  199.         if(!in_array($type$types))
  200.         {
  201.             $type 'append';
  202.         }
  203.         settype($attributes'array');
  204.         $this->_actions[= array(
  205.             'action' => 'create',
  206.             'id' => $id,
  207.             'tag' => $tag,
  208.             'attributes' => $attributes,
  209.             'type' => $type,
  210.         );
  211.         return;
  212.     }
  213.  
  214.     /**
  215.      * Replace a dom node via javascript
  216.      *
  217.      * higher level dom manipulation - replaces one node with another
  218.      * This can be used to replace a div with a form for inline editing
  219.      * use innerHtml attribute to change inside text
  220.      *
  221.      * $response->replaceNode('myid', 'div', array('innerHTML' => 'loading complete'));
  222.      * $response->replaceNode('mydiv', 'form', array('innerHTML' => $form));
  223.      *
  224.      * @param   string   $id    id for a specific item on the page <div id="myid"></div>
  225.      * @param   string   $tag    html node to create
  226.      * @param   array   $attributes    array of attribute -> data to fill the node with
  227.      * @return  void 
  228.      * @access public
  229.      */
  230.     function replaceNode($id$tag$attributes)
  231.     {
  232.         settype($attributes'array');
  233.         $this->_actions[= array(
  234.             'action' => 'replace',
  235.             'id' => $id,
  236.             'tag' => $tag,
  237.             'attributes' => $attributes,
  238.         );
  239.         return;
  240.     }
  241.  
  242.     /**
  243.      * Delete a dom node via javascript
  244.      *
  245.      * $response->removeNode('myid');
  246.      * $response->removeNode(array('mydiv', 'myform'));
  247.      *
  248.      * @param   string   $id    id for a specific item on the page <div id="myid"></div>
  249.      * @return  void 
  250.      * @access public
  251.      */
  252.     function removeNode($id)
  253.     {
  254.         $this->_actions[= array(
  255.             'action' => 'remove',
  256.             'id' => $id,
  257.         );
  258.         return;
  259.     }
  260.  
  261.     /**
  262.      * Send a string to a javascript eval
  263.      *
  264.      * This will send the data right to the eval javascript function, it will NOT
  265.      * allow you to dynamically add a javascript function for use later on because
  266.      * it is constrined by the eval function
  267.      *
  268.      * @param   string   $data   string to pass to the alert javascript function
  269.      * @return  void 
  270.      * @access public
  271.      */
  272.     function insertScript($data)
  273.     {
  274.         $this->_actions[= array(
  275.             'action' => 'script',
  276.             'data' => $data,
  277.         );
  278.         return;
  279.     }
  280.  
  281.     /**
  282.      * Send a string to a javascript alert
  283.      *
  284.      * This will send the data right to the alert javascript function
  285.      *
  286.      * @param   string   $data   string to pass to the alert javascript function
  287.      * @return  void 
  288.      * @access public
  289.      */
  290.     function insertAlert($data)
  291.     {
  292.         $this->_actions[= array(
  293.             'action' => 'alert',
  294.             'data' => $data,
  295.         );
  296.         return;
  297.     }
  298.  
  299.     /**
  300.      * Returns the serialized content of the response class
  301.      *
  302.      * we actually use the json serializer underneath, so we send the actions array
  303.      * to the json serializer and return the data
  304.      *
  305.      * @return  string   serialized response content
  306.      * @access public
  307.      */
  308.     function getPayload()
  309.     {
  310.         $serializer = new HTML_AJAX_Serializer_JSON();
  311.         return $serializer->serialize($this->_actions);
  312.     }
  313.  
  314.     /**
  315.      * Adds all the actions from one response object to another, feature request
  316.      * #6635 at pear.php.net
  317.      *
  318.      * @param   object   $instance    referenced HTML_AJAX_Action object
  319.      * @return  array 
  320.      * @access public
  321.      */
  322.     function combineActions(&$instance)
  323.     {
  324.         $this->_actions array_merge($this->_actions$instance->retrieveActions());
  325.     }
  326.  
  327.     /**
  328.      * to follow proper property access we need a way to retrieve the private
  329.      * actions array
  330.      *
  331.      * @return  array 
  332.      * @access public
  333.      */
  334.     function retrieveActions()
  335.     {
  336.         return $this->_actions;
  337.     }
  338. }
  339. ?>

Documentation generated on Sat, 05 May 2007 18:00:06 -0400 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.