Development

Changeset 6151

You must first sign up to be able to contribute.

Changeset 6151

Show
Ignore:
Timestamp:
11/23/07 18:35:29 (6 years ago)
Author:
KRavEN
Message:

Added in quote_except function to make writing classes easier.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfExtjs2Plugin/trunk/config/config.php

    r6147 r6151  
    11<?php 
    22sfConfig::set('sf_extjs2_version', 'v0.23'); 
     3 
     4# 
     5# array values that don't need quotes 
     6# 
     7$quoteExcept = array('value' => array('true', 'false', 'new Ext.', 'function', 'Ext.'), 
     8                     'key' => array('renderer', 'store', 'defaults', 'plugins', 'cm', 'ds', 'view', 'tbar', 'bbar')); 
     9sfConfig::set('sf_extjs2_quote_except', $quoteExcept); 
     10 
    311# 
    412# adapters 
     
    1220      'adapter/jquery/ext-jquery-adapter.js' 
    1321    ), 
    14    'prototype' => array( 
     22        'prototype' => array( 
    1523      'adapter/prototype/prototype.js', 
    1624      'adapter/prototype/scriptaculous.js?load=effects.js', 
    1725      'adapter/prototype/ext-prototype-adapter.js' 
    18    ), 
    19    'yui' => array( 
     26        ), 
     27        'yui' => array( 
    2028      'adapter/yui/yui-utilities.js', 
    2129      'adapter/yui/ext-yui-adapter.js' 
    2230    ), 
    23    'ext' => array( 
     31        'ext' => array( 
    2432      'adapter/ext/ext-base.js' 
    2533    ) 
    2634  ) 
    2735); 
     36 
    2837# 
    2938# themes 
     
    3645  ) 
    3746); 
     47 
    3848# 
    3949# base directories 
     
    4252sfConfig::set('sf_extjs2_css_dir', '/sfExtjs2Plugin/resources/css/'); 
    4353sfConfig::set('sf_extjs2_images_dir', '/sfExtjs2Plugin/resources/images/'); 
     54 
    4455# 
    4556# spacer gif 
    4657# 
    4758sfConfig::set('sf_extjs2_spacer', '/sfExtjs2Plugin/resources/images/default/s.gif'); 
     59 
    4860# 
    4961# attributes which must handled as array 
    5062# 
    51 sfConfig::set('sf_extjs2_list_attributes', array('items', 'tbar', 'buttons')); 
    52 
    53 # mapping plugin method against class  
     63sfConfig::set('sf_extjs2_list_attributes', array('items', 'tbar', 'buttons', 'defaults', 'plugins', 'cm', 'ds', 'view', 'tbar', 'bbar')); 
     64 
     65
     66# mapping plugin method against class 
    5467# 
    5568sfConfig::set('classes', 
    5669  array( 
    5770    // data 
    58     'JsonReader'   => 'Ext.data.JsonReader', 
    59     'Store'        => 'Ext.data.Store', 
    60     'HttpProxy'    => 'Ext.data.HttpProxy', 
     71    'JsonReader'   => 'Ext.data.JsonReader', 
     72    'Store'        => 'Ext.data.Store', 
     73    'HttpProxy'    => 'Ext.data.HttpProxy', 
     74 
    6175    // widgets 
    6276    'BoxComponent' => 'Ext.BoxComponent', 
    6377    'Button'       => 'Ext.Button', 
    64     'GridPanel'    => 'Ext.grid.GridPanel', 
    65     'ColumnModel'  => 'Ext.grid.ColumnModel', 
     78    'GridPanel'    => 'Ext.grid.GridPanel', 
     79    'ColumnModel' => 'Ext.grid.ColumnModel', 
    6680    'Panel'        => 'Ext.Panel', 
    6781    'TabPanel'     => 'Ext.TabPanel', 
     
    7387    'TimeField'    => 'Ext.form.TimeField', 
    7488    'HtmlEditor'   => 'Ext.form.HtmlEditor', 
    75     'Menu'        => 'Ext.menu.Menu', 
    76     'Item'        => 'Ext.menu.Item'     
     89    'Menu'        => 'Ext.menu.Menu', 
     90    'Item'         => 'Ext.menu.Item' 
    7791  ) 
    7892); 
     
    178192); 
    179193 
    180  
    181194sfConfig::set('Ext.FormPanel', 
    182195  array( 
  • plugins/sfExtjs2Plugin/trunk/lib/helper/sfExtjs2Helper.php

    r6147 r6151  
    77 * @version          0.0.23 
    88 * @last modified    11.22.2007 Kubens: 
    9  *                     - Added parameter support for custom methods  
    10  *                     - Added features to create application  
    11  *                     - Added parameter support for Ext.object constructors 
    12  *                   11.17.2007 Kubens: 
    13  *                     - Added features to create custom classes and custom methods 
    14  *                    11.12.2007 Kubens:  
    15  *                     - Fixed loading order of adapters. If adapters are used then it is important to load  
    16  *                       adapters and coresponding files before ext-all.js 
    17  *                     - Overworked: load method. Adapters and themes are setuped in config.php 
    18  *                     - Overworked: constructor. If no adapter or theme is passed then default 
    19  *                       settings from config.php will used  
    20  *                    11.07.2007 KRavEN:  
    21  *                     - Fixed the adapter includes to load all required files in the correct order 
     9 *                                         - Added parameter support for custom methods 
     10 *                                         - Added features to create application 
     11 *                                         - Added parameter support for Ext.object constructors 
     12 *                                     11.17.2007 Kubens: 
     13 *                                         - Added features to create custom classes and custom methods 
     14 *                                      11.12.2007    Kubens: 
     15 *                                         - Fixed loading order of adapters. If adapters are used then it is important to load 
     16 *                                             adapters and coresponding files before ext-all.js 
     17 *                                       - Overworked: load method. Adapters and themes are setuped in config.php 
     18 *                                         - Overworked: constructor. If no adapter or theme is passed then default 
     19 *                                             settings from config.php will used 
     20 *                                      11.07.2007 KRavEN: 
     21 *                                         - Fixed the adapter includes to load all required files in the correct order 
    2222 *                      moved ext-base into adapters, pass ext as adapter for standalone 
    2323 *                      changed all javascript to load first so they will come before files specified in view.yml 
    24  *                   07.15.2007 Kubens:  
    25  *                     - created 
     24 *                   07.15.2007 Kubens: 
     25 *                                         - created 
    2626 */ 
    2727class sfExtjs2Plugin { 
     
    4848    { 
    4949      $object = sfConfig::get($classes[$class]); 
    50     return sfExtjs2Plugin::getExtObject($object['class'], $parameters[0]); 
     50        return sfExtjs2Plugin::getExtObject($object['class'], $parameters[0]); 
    5151    } 
    5252  } 
     
    5555   * Renders javascript source for Ext.Object 
    5656   * Example usage: 
    57    *  
     57   * 
    5858   * Syntax A = short form without any options 
    5959   * 
     
    7474   *         ( 
    7575   *           'name'       => 'string',      // option to render Javascript variable 
    76    *           'attributes' => array         // attributes for Ext constructor 
     76   *           'attributes' => array                    // attributes for Ext constructor 
    7777   *           ( 
    7878   *             'id'       => 'id', 
     
    8484   *               ) 
    8585   *           ) 
    86    *           'parameters' => array 
    87    *          
    88    *              'parameter1',  
    89    *             'parameter2' 
    90    *          
     86   *                     'parameters' => array 
     87   *                    
     88   *                          'parameter1', 
     89   *                         'parameter2' 
     90   *                    
    9191   *         ); 
    9292   * 
     
    9696   * 
    9797   */ 
    98   public static function getExtObject($class, $attributes = array()) 
    99   {     
     98  public static function getExtObject($class, $attribs = array()) 
     99  { 
    100100    # parameters for constructor 
    101101    $parameters = array(); 
    102     if (is_array($attributes) && array_key_exists('parameters', $attributes)) 
    103     { 
    104       $parameters = $attributes['parameters']; 
    105       unset($attributes['parameters']); 
    106     } 
    107      
     102    if (is_array($attribs) && array_key_exists('parameters', $attribs)) 
     103    { 
     104      $parameters = $attribs['parameters']; 
     105      unset($attribs['parameters']); 
     106    } 
     107 
     108    #make it easier on us so not so much escaping in the attibutes setup 
     109    foreach ($attribs as $key => $value) 
     110    { 
     111      #enables us to have empty keys removed 
     112      if(is_null($value)) continue; 
     113 
     114      #quotes everything not in the quote_except value list 
     115      if(!is_array($value)) 
     116      { 
     117        $attributes[$key] = (sfExtjs2Plugin::quote_except($key, $value) ? '\''.$value.'\'' : $value); 
     118      } 
     119      else 
     120      { 
     121        $attributes[$key] = $value; 
     122      } 
     123    } 
     124 
    108125    # syntax A is a shortform of syntax B 
    109126    # if syntax A is used then convert syntax A to syntax B 
     
    121138      if (is_array($attributes) && array_key_exists($attribute, $attributes['attributes'])) 
    122139      { 
    123         $attributes['attributes'][$attribute] = sprintf('[%s]', implode(',',$attributes['attributes'][$attribute])); 
     140        $attStr = implode(',',$attributes['attributes'][$attribute]); 
     141        $attributes['attributes'][$attribute] = (sfExtjs2Plugin::quote_except($attribute, null) ? '['.$attStr.']' : '{'.$attStr.'}'); 
     142        //$attributes['attributes'][$attribute] = sprintf('[%s]', implode(',',$attributes['attributes'][$attribute])); 
    124143      } 
    125144    } 
     
    127146    // get source of component 
    128147    $source = call_user_func(array('sfExtjs2Plugin', 'getExtObjectComponent'), $attributes['attributes'], sfConfig::get($class), $parameters); 
    129      
     148 
    130149    // if 'name' is assigned then we must render 
    131150    // either a Javascript variable or an attribute of this 
     
    153172   * @param array attributes 
    154173   * @param array config 
    155    * @param array parameters  
     174   * @param array parameters 
    156175   * @return string source of Ext component 
    157176   * 
     
    161180    $attributes = sfExtjs2Plugin::_build_attributes($attributes, $config['attributes']); 
    162181    $attributes = sprintf('%s', $attributes != '' ? '{'.$attributes.'}' : ''); 
    163         
     182 
    164183    $parameters = implode(',', $parameters); 
    165184    $parameters = sprintf('%s%s', $attributes != '' && $parameters != '' ? ',' : '', $parameters); 
    166      
     185 
    167186    $source = sprintf( 
    168                 'new %s (%s%s)',  
    169                 $config['class'],  
    170                 $attributes,  
     187                'new %s (%s%s)', 
     188                $config['class'], 
     189                $attributes, 
    171190                $parameters 
    172              ); 
     191                            ); 
    173192 
    174193    return $source; 
     
    176195 
    177196  /** 
    178    * add sources for css and js  
     197   * add sources for css and js 
    179198   * files to response 
    180199   * 
     
    183202  { 
    184203    $response = sfContext::getInstance()->getResponse(); 
    185      
     204 
    186205    // add javascript sources for adapter 
    187     $adapters = sfConfig::get('sf_extjs2_adapters', array());  
     206    $adapters = sfConfig::get('sf_extjs2_adapters', array()); 
    188207    foreach ($adapters[$this->adapter] as $file) 
    189208    { 
     
    192211 
    193212    // add javascript sources for ext all 
    194     $response->addJavascript(sfConfig::get('sf_extjs2_js_dir'). 'ext-all.js', 'last'); 
    195      
     213    $response->addJavascript(sfConfig::get('sf_extjs2_js_dir'). 'ext-all.js', 'first'); 
     214 
    196215    // add css sources for ext all 
    197     $response->addStylesheet(sfConfig::get('sf_extjs2_css_dir'). 'ext-all.css', 'last'); 
     216    $response->addStylesheet(sfConfig::get('sf_extjs2_css_dir'). 'ext-all.css', 'first'); 
    198217 
    199218    // add css sources for theme 
    200     $themes = sfConfig::get('sf_extjs2_themes', array());  
     219    $themes = sfConfig::get('sf_extjs2_themes', array()); 
    201220    foreach ($themes[$this->theme] as $file) 
    202221    { 
    203       $response->addStylesheet(sfConfig::get('sf_extjs2_css_dir'). $file, 'last'); 
     222      $response->addStylesheet(sfConfig::get('sf_extjs2_css_dir'). $file, 'first'); 
    204223    } 
    205224  } 
     
    233252   * writes opening class tag 
    234253   * 
    235    * @param string namespace  
     254   * @param string namespace 
    236255   * @param string classname 
    237256   * @param string extend 
     
    243262    $source = ''; 
    244263 
    245     // write namespace directive  
    246     // prevent double output of namespace directive   
     264    // write namespace directive 
     265    // prevent double output of namespace directive 
    247266    if ($this->namespace !== $namespace) 
    248267    { 
     
    250269      $source .= sprintf("Ext.namespace('%s')%s", $namespace, sfExtjs2Plugin::LBR_SM); 
    251270    } 
    252      
     271 
    253272    // write class tag 
    254273    $source .= sprintf("%s.%s = Ext.extend(%s, {%s", $namespace, $classname, $extend, sfExtjs2Plugin::LBR); 
     
    267286  /** 
    268287   * writes closing class tag 
    269    *  
     288   * 
    270289   * @return source 
    271290   */ 
     
    274293    $source  = ''; 
    275294    $source .= sprintf("})%s", sfExtjs2Plugin::LBR_SM); 
    276   
     295 
    277296    echo $source; 
    278297  } 
     
    280299  /** 
    281300   * writes begining application tag 
    282    *  
     301   * 
    283302   * @param attributes['name'] 
    284303   * @param attributes['private'] 
    285304   * @param attributes['public'] 
    286    * @return source  
     305   * @return source 
    287306   */ 
    288307  public function beginApplication($attributes = array()) 
     
    295314      { 
    296315        $sourcePrivate .= sprintf('%svar %s = %s;', sfExtjs2Plugin::LBR, $key, $value); 
    297       }     
     316      } 
    298317    } 
    299318 
     
    308327        $sourcePublic .= sprintf('%s%s: %s%s', sfExtjs2Plugin::LBR, $key, $value, $i < count($attributes['public']) ? ',' : '' ); 
    309328      } 
    310     }     
    311      
     329    } 
     330 
    312331    // write application syntax 
    313332    $source = ''; 
    314333    $source = sprintf( 
    315       'var %s = function() { %s%sreturn {%s %s',  
    316       $attributes['name'],  
     334      'var %s = function() { %s%sreturn {%s %s', 
     335      $attributes['name'], 
    317336      $sourcePrivate, 
    318337      $sourcePrivate != '' ? sfExtjs2Plugin::LBR : '', 
     
    320339      $sourcePublic != '' ? sfExtjs2Plugin::LBR : '' 
    321340    ); 
    322   
    323     echo $source; 
    324   } 
    325    
     341 
     342    echo $source; 
     343  } 
     344 
    326345  /** 
    327346   * writes closing application tag 
    328    *  
     347   * 
    329348   * @return source 
    330    */  
     349   */ 
    331350  public function endApplication() 
    332351  { 
    333352    $source  = ''; 
    334353    $source .= sprintf("}}()%s", sfExtjs2Plugin::LBR_SM); 
    335   
    336     echo $source; 
    337   } 
    338    
     354 
     355    echo $source; 
     356  } 
     357 
    339358  /** 
    340359   * returns output of evaled php code 
    341    *   
     360   * 
    342361   * @param array matches 
    343362   * @return string source 
     
    345364  public static function methodEvalPHP ($matches) 
    346365  { 
    347     $source = str_replace( array('<?php', '<?', '?>'), '', $matches[0]);  
     366    $source = str_replace( array('<?php', '<?', '?>'), '', $matches[0]); 
    348367    ob_start(); 
    349368    eval($source); 
     
    363382  { 
    364383    $source = is_array($attributes) && array_key_exists('source', $attributes) ? $attributes['source'] : $attributes; 
    365     $source = preg_replace_callback(  
    366                 '/(\<\?php)(.*?)(\?>)/si',  
    367                 array('self', 'methodEvalPHP'),       
     384    $source = preg_replace_callback( 
     385                '/(\<\?php)(.*?)(\?>)/si', 
     386                array('self', 'methodEvalPHP'), 
    368387                $source 
    369388              ); 
    370389    $source = sprintf("function (%s) { %s }", is_array($attributes) && array_key_exists('parameters', $attributes) ? $attributes['parameters'] : '', $source); 
    371                
    372     return $source;               
    373   } 
    374  
    375   /** 
    376    * returns source of custom class  
     390 
     391    return $source; 
     392  } 
     393 
     394  /** 
     395   * returns source of custom class 
    377396   * 
    378397   * @param string classname 
     
    419438    return $attributes; 
    420439  } 
    421     
     440 
     441  private static function quote_except($key, $value) 
     442  { 
     443    $quoteExcept = sfConfig::get('sf_extjs2_quote_except'); 
     444 
     445    if (is_int($key)) 
     446    { 
     447      return false; 
     448    } 
     449    else 
     450    { 
     451      foreach ($quoteExcept['key'] as $except) 
     452      { 
     453        if ($key == $except) 
     454        { 
     455          return false; 
     456        } 
     457      } 
     458    } 
     459 
     460    if (is_int($value)) 
     461    { 
     462      return false; 
     463    } 
     464    else 
     465    { 
     466      foreach ($quoteExcept['value'] as $except) 
     467      { 
     468        if (substr($value, 0, strlen($except)) == $except) 
     469        { 
     470          return false; 
     471        } 
     472      } 
     473    } 
     474 
     475    return true; 
     476  } 
     477 
    422478} 
    423479