phpDocumentor Projax
[ class tree: Projax ] [ index: Projax ] [ all elements ]

Source for file Prototype.php

Documentation is available at Prototype.php

  1. <?php  
  2.  
  3. /**
  4.  * Projax
  5.  *
  6.  * An open source set of php helper classes for prototype and script.aculo.us.
  7.  *
  8.  * @package        Projax
  9.  * @author        Vikas Patial
  10.  * @copyright    Copyright (c) 2006, ngcoders.
  11.  * @license        http://www.gnu.org/copyleft/gpl.html
  12.  * @link        http://www.ngcoders.com
  13.  * @since        Version 0.2
  14.  * @filesource
  15.  */
  16.  
  17. class Prototype extends JavaScript  {
  18.     
  19.     
  20.     
  21.     var $CALLBACKS     =      array('uninitialized',
  22.                             'loading',
  23.                             'loaded',
  24.                             'interactive',
  25.                             'complete',
  26.                             'failure',
  27.                             'success');
  28.                             
  29.                             
  30.     var $AJAX_OPTIONS = array('before',
  31.                                'after',
  32.                                'condition',
  33.                                'url',
  34.                                'asynchronous',
  35.                                'method',
  36.                                'insertion',
  37.                                'position',
  38.                                'form',
  39.                                'with',
  40.                                'update',
  41.                                'script',
  42.                                'uninitialized',
  43.                                'loading',
  44.                                'loaded',
  45.                                'interactive',
  46.                                'complete',
  47.                                'failure',
  48.                                'success');                    
  49.  
  50.     function evaluate_remote_response(){
  51.         return 'eval(request.responseText)';        
  52.     }
  53.  
  54.     function form_remote_tag($options)
  55.     {
  56.         $options['form'true;
  57.         return '<form action="'.$options['url'].'" onsubmit="'.$this->remote_function($options).'; return false;" method="'.(isset($options['method'])?$options['method']:'post').'"  >';            
  58.     }
  59.     
  60.     
  61.     function link_to_remote($name,$options=null,$html_options=null)
  62.     {
  63.         return $this->link_to_function($name,$this->remote_function($options),$html_options);
  64.     }
  65.  
  66.     function observe_field($field_id,$options=null)
  67.     {
  68.         if (isset($options['frequency']&& $options['frequency']{
  69.             return $this->_build_observer('Form.Element.Observer',$field_id,$options);
  70.         else {
  71.             return $this->_build_observer('Form.Element.EventObserver',$field_id,$options);
  72.         }
  73.     }
  74.     
  75.     
  76.     function observe_form($form_id,$options null)
  77.     {
  78.         if (isset($options['frequency'])) {
  79.             return $this->_build_observer('Form.Observer',$form_id,$options);
  80.         else {
  81.             return $this->_build_observer('Form.EventObserver',$form_id,$options);
  82.         }
  83.                 
  84.     }
  85.     
  86.     function periodically_call_remote($options=null{
  87.         
  88.         $frequency=(isset($options['frequency']))?$options['frequency']:10;
  89.         $code 'new PeriodicalExecuter(function() {'.$this->remote_function($options).'},'.$frequency.')';
  90.         return $code;
  91.     }
  92.     
  93.     function remote_function($options)
  94.     {
  95.     
  96.         $javascript_options $this->_options_for_ajax($options);
  97.         
  98.         $update ='';
  99.  
  100.         ifisset($options['update']&& is_array($options['update']) )    
  101.         {
  102.             $update=(isset($options['update']['success']))?'success: '.$options['update']['success']:'';
  103.             $update.=(empty($update))?'':",";
  104.             $update.=(isset($options['update']['failure']))?'failure: '.$options['update']['failure']:'';
  105.         else {
  106.             $update.=(isset($options['update']))?$options['update']:'';
  107.         }
  108.         
  109.         $ajax_function=(empty($update))?'new Ajax.Request(':'new Ajax.Updater(\''.$update.'\',';
  110.         
  111.         $ajax_function.="'".$options['url']."'";
  112.         $ajax_function.=",".$javascript_options.')';
  113.         
  114.         $ajax_function=(isset($options['before']))?  $options['before'].';'.$ajax_function $ajax_function;
  115.         $ajax_function=(isset($options['after']))?  $ajax_function.';'.$options['after'$ajax_function;
  116.         $ajax_function=(isset($options['condition']))'if ('.$options['condition'].') {'.$ajax_function.'}' $ajax_function;
  117.         $ajax_function=(isset($options['confirm'])) 'if ( confirm(\''.$options['confirm'].'\' ) ) { '.$ajax_function.' } ':$ajax_function;
  118.         
  119.         return $ajax_function;
  120.             
  121.     }
  122.     
  123.     function submit_to_remote($name,$value,$options{
  124.         
  125.         if(isset($options['with'])){
  126.             $options['with'="Form.serialize(this.form)";
  127.         }
  128.         
  129.         return '<input type="button" onclick="'.$this->remote_function($options).'" name="'.$name.'" value ="'.$value.'" />';
  130.             
  131.     }
  132.     
  133.     function update_element_function($element_id,$options=null,$block)
  134.     {
  135.         $content=(isset($options['content']))?$options['content']:'';
  136.         $content=$this->escape($content);
  137.         
  138.         
  139.     }
  140.     
  141.     function  update_page($block)
  142.     {
  143.         
  144.     }
  145.     
  146.     function update_page_tag($block)
  147.     {
  148.         return $this->tag($block);
  149.     }
  150.     
  151.     
  152.     /////////////////////////////////////////////////////////////////////////////////////
  153.     //                             Private functions 
  154.     /////////////////////////////////////////////////////////////////////////////////////
  155.     
  156.     
  157.     function _build_callbacks($options)
  158.     {
  159.         $callbacks=array();
  160.         foreach ($options as $callback=>$code{
  161.             if (in_array($callback,$this->CALLBACKS)) {
  162.                             $name 'on'.ucfirst($callback);
  163.                             $callbacks[$name]='function(request){'.$code.'}';
  164.                             
  165.                         }            
  166.         }
  167.         return $callbacks;
  168.     }
  169.     
  170.     
  171.     function _build_observer($klass,$name,$options=null)
  172.     {
  173.         if (isset($options['with']&& !strstr($options['with'],'=') ) {
  174.             $options['with''\''.$options['with'].'=\' + value';
  175.         }else if (isset($options['with']&& isset($options['update'])) {
  176.             $options['with''value';
  177.         }
  178.         
  179.  
  180.         $callback $options['function'$options['function'$this->remote_function($options);
  181.         
  182.         $javascript "new $klass('$name', ";
  183.         $javascript.= (isset($options['frequency']))?$options['frequency'].', ':'';
  184.         $javascript.= 'function (element,value) { ';
  185.         $javascript.= $callback;
  186.         $javascript.=  (isset($options['on']))?', '.$options['on']:'';
  187.         $javascript.= '})';
  188.         
  189.         return $javascript;
  190.         
  191.     }
  192.     
  193.     function _method_option_to_s($method)
  194.     {
  195.         return (strstr($method,"'"))?$method:"'$method'";
  196.     }
  197.     
  198.     function _options_for_ajax($options)
  199.     {
  200.         $js_options(is_array($options))?$this->_build_callbacks($options):array();
  201.         
  202.  
  203.         if (isset($options['type']&& $option['type']=='synchronous')    $js_options['asynchronous'='false';
  204.         
  205.         if (isset($options['method'])) $js_options['method']    $this->_method_option_to_s($options['method']);
  206.         
  207.         if (isset($options['position'])) $js_options['insertion']    'Insertion.'.ucfirst($options['position']);
  208.         
  209.         $js_options['evalScripts'= isset($options['script'])?$options['script']:'true';
  210.         
  211.         if (isset($options['form'])) {
  212.             $js_options['parameters']='Form.serialize(this)';
  213.         }elseif (isset($options['parameters'])){
  214.             $js_options['parameters']='Form.serialize(\''.$options['submit'].'\')';
  215.         }elseif (isset($options['with'])) {
  216.             $js_options['parameters']$options['with'];
  217.         }
  218.         
  219.         return $this->_options_for_javascript($js_options);
  220.     }
  221.     
  222.     /////////////////////////////////////////////////////////////////////////////////////
  223.     //                            Mergerd Javascript Generator helpers
  224.     /////////////////////////////////////////////////////////////////////////////////////
  225.     
  226.  
  227.     function dump($javascript)
  228.     {
  229.         echo $javascript;
  230.     }
  231.  
  232.     function ID($id,$extend=null)
  233.     {
  234.         return "$('$id')".(!empty($extend))?'.'.$extend.'()':'';
  235.     }
  236.  
  237.     function alert($message)
  238.     {
  239.         return $this->call('alert',$message);
  240.     }
  241.  
  242.     function assign($variable,$value)
  243.     {
  244.         return "$variable = $value;";
  245.     }
  246.  
  247.     function call($function $args null)
  248.     {
  249.         $arg_str='';
  250.         if (is_array($args)) {
  251.             foreach ($args as $arg){
  252.                 if(!empty($arg_str))$arg_str.=', ';
  253.                 ifis_string($arg)) {
  254.                     $arg_str.="'$arg'";
  255.                 else {
  256.                     $arg_str.=$arg;
  257.                 }
  258.             }
  259.         else {
  260.             if (is_string($args)) {
  261.                 $arg_str.="'$args'";
  262.             else {
  263.                 $arg_str.=$args;
  264.             }
  265.         }
  266.  
  267.         return "$function($arg_str)";
  268.     }
  269.  
  270.     function delay($seconds=1,$script='')
  271.     {
  272.         return "setTimeout( function() { $script } , ".($seconds*1000)." )";
  273.     }
  274.  
  275.     function hide($id)
  276.     {
  277.         return $this->call('Element.hide',$id);
  278.     }
  279.  
  280.     function insert_html($position,$id,$options_for_render=null)
  281.     {
  282.         $args=array_merge(array($id),(is_array($options_for_render)?$options_for_render:array($options_for_render)));
  283.         return $this->call('new Insertion.'.ucfirst($position),$args);
  284.     }
  285.  
  286.     function redirect_to($location)
  287.     {
  288.         return $this->assign('window.location.href',$location);
  289.     }
  290.  
  291.     function remove($id)
  292.     {
  293.         if (is_array($id)){
  294.  
  295.             $arr_str='';
  296.             foreach ($id as $obj){
  297.                 if(!empty($arg_str))$arg_str.=', ';
  298.                 $arg_str.="'$arg'";
  299.             }
  300.             return "$A[$arg_str].each(Element.remove)";
  301.         }else {
  302.             return "Element.remove('$id')";
  303.         }
  304.     }
  305.  
  306.     function replace($id,$options_for_render)
  307.     {
  308.         $args=array_merge(array($id),(is_array($options_for_render)?$options_for_render:array($options_for_render)));
  309.         return $this->call('Element.replace',$args);
  310.     }
  311.  
  312.     function replace_html($id,$options_for_render)
  313.     {
  314.         $args=array_merge(array($id),(is_array($options_for_render)?$options_for_render:array($options_for_render)));
  315.         return $this->call('Element.update',$args);
  316.     }
  317.  
  318.     function select($pattern,$extend=null)
  319.     {
  320.         return "$$('$pattern')".(!empty($extend))?'.'.$extend:'';
  321.     }
  322.  
  323.     function show($id)
  324.     {
  325.         return $this->call('Element.show',$id);
  326.     }
  327.  
  328.     function toggle($id)
  329.     {
  330.         return $this->call('Element.toggle',$id);
  331.     }
  332.  
  333.     
  334. }
  335.  
  336. ?>

Documentation generated on Sun, 05 Apr 2009 23:01:30 +0100 by phpDocumentor 1.4.1