[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/helper/ -> FormHelper.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
   6   * (c) 2004 David Heinemeier Hansson
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  /**
  13   * FormHelper.
  14   *
  15   * @package    symfony
  16   * @subpackage helper
  17   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  18   * @author     David Heinemeier Hansson
  19   * @version    SVN: $Id: FormHelper.php 3491 2007-02-18 09:07:59Z fabien $
  20   */
  21  
  22  /**
  23   * Returns a formatted set of <option> tags based on optional <i>$options</i> array variable.
  24   *
  25   * The options_for_select helper is usually called in conjunction with the select_tag helper, as it is relatively
  26   * useless on its own. By passing an array of <i>$options</i>, the helper will automatically generate <option> tags
  27   * using the array key as the value and the array value as the display title. Additionally the options_for_select tag is
  28   * smart enough to detect nested arrays as <optgroup> tags.  If the helper detects that the array value is an array itself,
  29   * it creates an <optgroup> tag with the name of the group being the key and the contents of the <optgroup> being the array.
  30   *
  31   * <b>Options:</b>
  32   * - include_blank  - Includes a blank <option> tag at the beginning of the string with an empty value
  33   * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value
  34   *
  35   * <b>Examples:</b>
  36   * <code>
  37   *  echo select_tag('person', options_for_select(array(1 => 'Larry', 2 => 'Moe', 3 => 'Curly')));
  38   * </code>
  39   *
  40   * <code>
  41   *  $card_list = array('VISA' => 'Visa', 'MAST' => 'MasterCard', 'AMEX' => 'American Express', 'DISC' => 'Discover');
  42   *  echo select_tag('cc_type', options_for_select($card_list, 'AMEX', array('include_custom' => '-- Select Credit Card Type --')));
  43   * </code>
  44   *
  45   * <code>
  46   *  $optgroup_array = array(1 => 'Joe', 2 => 'Sue', 'Group A' => array(3 => 'Mary', 4 => 'Tom'), 'Group B' => array(5 => 'Bill', 6 =>'Andy'));
  47   *  echo select_tag('employee', options_for_select($optgroup_array, null, array('include_blank' => true)), array('class' => 'mystyle'));
  48   * </code>
  49   *
  50   * @param  array dataset to create <option> tags and <optgroup> tags from
  51   * @param  string selected option value
  52   * @param  array  additional HTML compliant <option> tag parameters
  53   * @return string populated with <option> tags derived from the <i>$options</i> array variable
  54   * @see select_tag
  55   */
  56  function options_for_select($options = array(), $selected = '', $html_options = array())
  57  {
  58    $html_options = _parse_attributes($html_options);
  59  
  60    if (is_array($selected))
  61    {
  62      $selected = array_map('strval', array_values($selected));
  63    }
  64  
  65    $html = '';
  66  
  67    if ($value = _get_option($html_options, 'include_custom'))
  68    {
  69      $html .= content_tag('option', $value, array('value' => ''))."\n";
  70    }
  71    else if (_get_option($html_options, 'include_blank'))
  72    {
  73      $html .= content_tag('option', '', array('value' => ''))."\n";
  74    }
  75  
  76    foreach ($options as $key => $value)
  77    {
  78      if (is_array($value))
  79      {
  80        $html .= content_tag('optgroup', options_for_select($value, $selected, $html_options), array('label' => $key))."\n";
  81      }
  82      else
  83      {
  84        $option_options = array('value' => $key);
  85  
  86        if (
  87            (is_array($selected) && in_array(strval($key), $selected, true))
  88            ||
  89            (strval($key) == strval($selected))
  90           )
  91        {
  92          $option_options['selected'] = 'selected';
  93        }
  94  
  95        $html .= content_tag('option', $value, $option_options)."\n";
  96      }
  97    }
  98  
  99    return $html;
 100  }
 101  
 102  /**
 103   * Returns an HTML <form> tag that points to a valid action, route or URL as defined by <i>$url_for_options</i>.
 104   *
 105   * By default, the form tag is generated in POST format, but can easily be configured along with any additional
 106   * HTML parameters via the optional <i>$options</i> parameter. If you are using file uploads, be sure to set the 
 107   * <i>multipart</i> option to true.
 108   *
 109   * <b>Options:</b>
 110   * - multipart - When set to true, enctype is set to "multipart/form-data".
 111   *
 112   * <b>Examples:</b>
 113   *   <code><?php echo form_tag('@myroute'); ?></code>
 114   *   <code><?php echo form_tag('/module/action', array('name' => 'myformname', 'multipart' => true)); ?></code>
 115   *
 116   * @param  string valid action, route or URL
 117   * @param  array optional HTML parameters for the <form> tag
 118   * @return string opening HTML <form> tag with options
 119   */
 120  function form_tag($url_for_options = '', $options = array())
 121  {
 122    $options = _parse_attributes($options);
 123  
 124    $html_options = $options;
 125    if (!isset($html_options['method']))
 126    {
 127      $html_options['method'] = 'post';
 128    }
 129  
 130    if (_get_option($html_options, 'multipart'))
 131    {
 132      $html_options['enctype'] = 'multipart/form-data';
 133    }
 134  
 135    $html_options['action'] = url_for($url_for_options);
 136  
 137    return tag('form', $html_options, true);
 138  }
 139  
 140  /**
 141   * Returns a <select> tag, optionally comprised of <option> tags.
 142   *
 143   * The select tag does not generate <option> tags by default.  
 144   * To do so, you must populate the <i>$option_tags</i> parameter with a string of valid HTML compliant <option> tags.
 145   * Fortunately, Symfony provides a handy helper function to convert an array of data into option tags (see options_for_select). 
 146   * If you need to create a "multiple" select tag (ability to select multiple options), set the <i>multiple</i> option to true.  
 147   * Doing so will automatically convert the name field to an array type variable (i.e. name="name" becomes name="name[]").
 148   * 
 149   * <b>Options:</b>
 150   * - multiple - If set to true, the select tag will allow multiple options to be selected at once.
 151   *
 152   * <b>Examples:</b>
 153   * <code>
 154   *  $person_list = array(1 => 'Larry', 2 => 'Moe', 3 => 'Curly');
 155   *  echo select_tag('person', options_for_select($person_list, $sf_params->get('person')), array('class' => 'full'));
 156   * </code>
 157   *
 158   * <code>
 159   *  echo select_tag('department', options_for_select($department_list), array('multiple' => true));
 160   * </code>
 161   *
 162   * <code>
 163   *  echo select_tag('url', options_for_select($url_list), array('onChange' => 'Javascript:this.form.submit();'));
 164   * </code>
 165   *
 166   * @param  string field name 
 167   * @param  mixed contains a string of valid <option></option> tags, or an array of options that will be passed to options_for_select
 168   * @param  array  additional HTML compliant <select> tag parameters
 169   * @return string <select> tag optionally comprised of <option> tags.
 170   * @see options_for_select, content_tag
 171   */
 172  function select_tag($name, $option_tags = null, $options = array())
 173  {
 174    $options = _convert_options($options);
 175    $id = $name;
 176    if (isset($options['multiple']) && $options['multiple'] && substr($name, -2) !== '[]')
 177    {
 178      $name .= '[]';
 179    }
 180    if (is_array($option_tags))
 181    {
 182      $option_tags = options_for_select($option_tags);
 183    }
 184  
 185    return content_tag('select', $option_tags, array_merge(array('name' => $name, 'id' => get_id_from_name($id)), $options));
 186  }
 187  
 188  /**
 189   * Returns a <select> tag populated with all the countries in the world.
 190   *
 191   * The select_country_tag builds off the traditional select_tag function, and is conveniently populated with 
 192   * all the countries in the world (sorted alphabetically). Each option in the list has a two-character country 
 193   * code for its value and the country's name as its display title.  The country data is retrieved via the sfCultureInfo
 194   * class, which stores a wide variety of i18n and i10n settings for various countries and cultures throughout the world.
 195   * Here's an example of an <option> tag generated by the select_country_tag:
 196   *
 197   * <samp>
 198   *  <option value="US">United States</option>
 199   * </samp>
 200   *
 201   * <b>Examples:</b>
 202   * <code>
 203   *  echo select_country_tag('country', 'FR');
 204   * </code>
 205   *
 206   * @param  string field name 
 207   * @param  string selected field value (two-character country code)
 208   * @param  array  additional HTML compliant <select> tag parameters
 209   * @return string <select> tag populated with all the countries in the world.
 210   * @see select_tag, options_for_select, sfCultureInfo
 211   */
 212  function select_country_tag($name, $selected = null, $options = array())
 213  {
 214    $c = new sfCultureInfo(sfContext::getInstance()->getUser()->getCulture());
 215    $countries = $c->getCountries();
 216  
 217    if ($country_option = _get_option($options, 'countries'))
 218    {
 219      foreach ($countries as $key => $value)
 220      {
 221        if (!in_array($key, $country_option))
 222        {
 223          unset($countries[$key]);
 224        }
 225      }
 226    }
 227  
 228    asort($countries);
 229  
 230    $option_tags = options_for_select($countries, $selected, $options);
 231  
 232    return select_tag($name, $option_tags, $options);
 233  }
 234  
 235  /**
 236   * Returns a <select> tag populated with all the languages in the world (or almost).
 237   *
 238   * The select_language_tag builds off the traditional select_tag function, and is conveniently populated with 
 239   * all the languages in the world (sorted alphabetically). Each option in the list has a two or three character 
 240   * language/culture code for its value and the language's name as its display title.  The country data is 
 241   * retrieved via the sfCultureInfo class, which stores a wide variety of i18n and i10n settings for various 
 242   * countries and cultures throughout the world. Here's an example of an <option> tag generated by the select_country_tag:
 243   *
 244   * <samp>
 245   *  <option value="en">English</option>
 246   * </samp>
 247   *
 248   * <b>Examples:</b>
 249   * <code>
 250   *  echo select_language_tag('language', 'de');
 251   * </code>
 252   *
 253   * @param  string field name 
 254   * @param  string selected field value (two or threecharacter language/culture code)
 255   * @param  array  additional HTML compliant <select> tag parameters
 256   * @return string <select> tag populated with all the languages in the world.
 257   * @see select_tag, options_for_select, sfCultureInfo
 258   */
 259  function select_language_tag($name, $selected = null, $options = array())
 260  {
 261    $c = new sfCultureInfo(sfContext::getInstance()->getUser()->getCulture());
 262    $languages = $c->getLanguages();
 263  
 264    if ($language_option = _get_option($options, 'languages'))
 265    {
 266      foreach ($languages as $key => $value)
 267      {
 268        if (!in_array($key, $language_option))
 269        {
 270          unset($languages[$key]);
 271        }
 272      }
 273    }
 274  
 275    asort($languages);
 276  
 277    $option_tags = options_for_select($languages, $selected, $options);
 278  
 279    return select_tag($name, $option_tags, $options);
 280  }
 281  
 282  /**
 283   * Returns an XHTML compliant <input> tag with type="text".
 284   *
 285   * The input_tag helper generates your basic XHTML <input> tag and can utilize any standard <input> tag parameters 
 286   * passed in the optional <i>$options</i> parameter.
 287   *
 288   * <b>Examples:</b>
 289   * <code>
 290   *  echo input_tag('name');
 291   * </code>
 292   *
 293   * <code>
 294   *  echo input_tag('amount', $sf_params->get('amount'), array('size' => 8, 'maxlength' => 8));
 295   * </code>
 296   *
 297   * @param  string field name 
 298   * @param  string selected field value
 299   * @param  array  additional HTML compliant <input> tag parameters
 300   * @return string XHTML compliant <input> tag with type="text"
 301   */
 302  function input_tag($name, $value = null, $options = array())
 303  {
 304    return tag('input', array_merge(array('type' => 'text', 'name' => $name, 'id' => get_id_from_name($name, $value), 'value' => $value), _convert_options($options)));
 305  }
 306  
 307  /**
 308   * Returns an XHTML compliant <input> tag with type="hidden".
 309   *
 310   * Similar to the input_tag helper, the input_hidden_tag helper generates an XHTML <input> tag and can utilize 
 311   * any standard <input> tag parameters passed in the optional <i>$options</i> parameter.  The only difference is 
 312   * that it creates the tag with type="hidden", meaning that is not visible on the page.
 313   *
 314   * <b>Examples:</b>
 315   * <code>
 316   *  echo input_hidden_tag('id', $id);
 317   * </code>
 318   *
 319   * @param  string field name 
 320   * @param  string populated field value
 321   * @param  array  additional HTML compliant <input> tag parameters
 322   * @return string XHTML compliant <input> tag with type="hidden"
 323   */
 324  function input_hidden_tag($name, $value = null, $options = array())
 325  {
 326    $options = _parse_attributes($options);
 327  
 328    $options['type'] = 'hidden';
 329    return input_tag($name, $value, $options);
 330  }
 331  
 332  /**
 333   * Returns an XHTML compliant <input> tag with type="file".
 334   *
 335   * Similar to the input_tag helper, the input_hidden_tag helper generates your basic XHTML <input> tag and can utilize
 336   * any standard <input> tag parameters passed in the optional <i>$options</i> parameter.  The only difference is that it 
 337   * creates the tag with type="file", meaning that next to the field will be a "browse" (or similar) button. 
 338   * This gives the user the ability to choose a file from there computer to upload to the web server.  Remember, if you 
 339   * plan to upload files to your website, be sure to set the <i>multipart</i> option form_tag helper function to true 
 340   * or your files will not be properly uploaded to the web server.
 341   *
 342   * <b>Examples:</b>
 343   * <code>
 344   *  echo input_file_tag('filename', array('size' => 30));
 345   * </code>
 346   *
 347   * @param  string field name 
 348   * @param  array  additional HTML compliant <input> tag parameters
 349   * @return string XHTML compliant <input> tag with type="file"
 350   * @see input_tag, form_tag
 351   */
 352  function input_file_tag($name, $options = array())
 353  {
 354    $options = _parse_attributes($options);
 355  
 356    $options['type'] = 'file';
 357    return input_tag($name, null, $options);
 358  }
 359  
 360  /**
 361   * Returns an XHTML compliant <input> tag with type="password".
 362   *
 363   * Similar to the input_tag helper, the input_hidden_tag helper generates your basic XHTML <input> tag and can utilize
 364   * any standard <input> tag parameters passed in the optional <i>$options</i> parameter.  The only difference is that it 
 365   * creates the tag with type="password", meaning that the text entered into this field will not be visible to the end user.
 366   * In most cases it is replaced by  * * * * * * * *.  Even though this text is not readable, it is recommended that you do not 
 367   * populate the optional <i>$value</i> option with a plain-text password or any other sensitive information, as this is a 
 368   * potential security risk.
 369   *
 370   * <b>Examples:</b>
 371   * <code>
 372   *  echo input_password_tag('password');
 373   *  echo input_password_tag('password_confirm');
 374   * </code>
 375   *
 376   * @param  string field name
 377   * @param  string populated field value
 378   * @param  array  additional HTML compliant <input> tag parameters
 379   * @return string XHTML compliant <input> tag with type="password"
 380   * @see input_tag
 381   */
 382  function input_password_tag($name = 'password', $value = null, $options = array())
 383  {
 384    $options = _parse_attributes($options);
 385  
 386    $options['type'] = 'password';
 387    return input_tag($name, $value, $options);
 388  }
 389  
 390  /**
 391   * Returns a <textarea> tag, optionally wrapped with an inline rich-text JavaScript editor.
 392   *
 393   * The texarea_tag helper generates a standard HTML <textarea> tag and can be manipulated with
 394   * any number of standard HTML parameters via the <i>$options</i> array variable.  However, the 
 395   * textarea tag also has the unique capability of being transformed into a WYSIWYG rich-text editor
 396   * such as TinyMCE (http://tinymce.moxiecode.com) very easily with the use of some specific options:
 397   *
 398   * <b>Options:</b>
 399   *  - rich: A rich text editor class (for example sfRichTextEditorTinyMCE for TinyMCE).
 400   *
 401   * <b>Examples:</b>
 402   * <code>
 403   *  echo textarea_tag('notes');
 404   * </code>
 405   *
 406   * <code>
 407   *  echo textarea_tag('description', 'This is a description', array('rows' => 10, 'cols' => 50));
 408   * </code> 
 409   *
 410   * @param  string field name
 411   * @param  string populated field value
 412   * @param  array  additional HTML compliant <textarea> tag parameters
 413   *
 414   * @return string <textarea> tag optionally wrapped with a rich-text WYSIWYG editor
 415   */
 416  function textarea_tag($name, $content = null, $options = array())
 417  {
 418    $options = _parse_attributes($options);
 419  
 420    if ($size = _get_option($options, 'size'))
 421    {
 422      list($options['cols'], $options['rows']) = split('x', $size, 2);
 423    }
 424  
 425    // rich control?
 426    if ($rich = _get_option($options, 'rich', false))
 427    {
 428      if (true === $rich)
 429      {
 430        $rich = sfConfig::get('sf_rich_text_editor_class', 'TinyMCE');
 431      }
 432  
 433      // switch for backward compatibility
 434      switch ($rich)
 435      {
 436        case 'tinymce':
 437          $rich = 'TinyMCE';
 438          break;
 439        case 'fck':
 440          $rich = 'FCK';
 441          break;
 442      }
 443  
 444      $editorClass = 'sfRichTextEditor'.$rich;
 445  
 446      if (!class_exists($editorClass))
 447      {
 448        throw new sfConfigurationException(sprintf('The rich text editor "%s" does not exist.', $editorClass));
 449      }
 450  
 451      $sfEditor = new $editorClass();
 452      if (!in_array('sfRichTextEditor', class_parents($sfEditor)))
 453      {
 454        throw new sfConfigurationException(sprintf('The editor "%s" must extend sfRichTextEditor.', $editorClass));
 455      }
 456      $sfEditor->initialize($name, $content, $options);
 457  
 458      return $sfEditor->toHTML();
 459    }
 460  
 461    return content_tag('textarea', escape_once((is_object($content)) ? $content->__toString() : $content), array_merge(array('name' => $name, 'id' => get_id_from_name(_get_option($options, 'id', $name), null)), _convert_options($options)));
 462  }
 463  
 464  /**
 465   * Returns an XHTML compliant <input> tag with type="checkbox".
 466   *
 467   * When creating multiple checkboxes with the same name, be sure to use an array for the
 468   * <i>$name</i> parameter (i.e. 'name[]').  The checkbox_tag is smart enough to create unique ID's
 469   * based on the <i>$value</i> parameter like so:
 470   *
 471   * <samp>
 472   *  <input type="checkbox" name="status[]" id="status_3" value="3" />
 473   *  <input type="checkbox" name="status[]" id="status_4" value="4" />
 474   * </samp>
 475   * 
 476   * <b>Examples:</b>
 477   * <code>
 478   *  echo checkbox_tag('newsletter', 1, $sf_params->get('newsletter'));
 479   * </code>
 480   *
 481   * <code>
 482   *  echo checkbox_tag('option_a', 'yes', true, array('class' => 'style_a'));
 483   * </code>
 484   *
 485   * <code>
 486   *  // one request variable with an array of checkbox values
 487   *  echo checkbox_tag('choice[]', 1);
 488   *  echo checkbox_tag('choice[]', 2);
 489   *  echo checkbox_tag('choice[]', 3);
 490   *  echo checkbox_tag('choice[]', 4); 
 491   * </code>
 492   *
 493   * <code>
 494   *  // assuming you have Prototype.js enabled, you could do this
 495   *  echo checkbox_tag('show_tos', 1, false, array('onclick' => "Element.toggle('tos'); return false;"));
 496   * </code>
 497   *
 498   * @param  string field name 
 499   * @param  string checkbox value (if checked)
 500   * @param  bool   is the checkbox checked? (1 or 0)
 501   * @param  array  additional HTML compliant <input> tag parameters
 502   * @return string XHTML compliant <input> tag with type="checkbox"
 503   */
 504  function checkbox_tag($name, $value = '1', $checked = false, $options = array())
 505  {
 506    $html_options = array_merge(array('type' => 'checkbox', 'name' => $name, 'id' => get_id_from_name($name, $value), 'value' => $value), _convert_options($options));
 507  
 508    if ($checked)
 509    {
 510      $html_options['checked'] = 'checked';
 511    }
 512  
 513    return tag('input', $html_options);
 514  }
 515  
 516  /**
 517   * Returns an XHTML compliant <input> tag with type="radio".
 518   *
 519   * <b>Examples:</b>
 520   * <code>
 521   *  echo ' Yes '.radiobutton_tag('newsletter', 1);
 522   *  echo ' No '.radiobutton_tag('newsletter', 0); 
 523   * </code>
 524   *
 525   * @param  string field name 
 526   * @param  string radio button value (if selected)
 527   * @param  bool   is the radio button selected? (1 or 0)
 528   * @param  array  additional HTML compliant <input> tag parameters
 529   * @return string XHTML compliant <input> tag with type="radio"
 530   */
 531  function radiobutton_tag($name, $value, $checked = false, $options = array())
 532  {
 533    $html_options = array_merge(array('type' => 'radio', 'name' => $name, 'id' => get_id_from_name($name, $value), 'value' => $value), _convert_options($options));
 534  
 535    if ($checked)
 536    {
 537      $html_options['checked'] = 'checked';
 538    }
 539  
 540    return tag('input', $html_options);
 541  }
 542  
 543  /**
 544   * Returns two XHTML compliant <input> tags to be used as a free-text date fields for a date range.
 545   * 
 546   * Built on the input_date_tag, the input_date_range_tag combines two input tags that allow the user
 547   * to specify a from and to date.  
 548   * You can easily implement a JavaScript calendar by enabling the 'rich' option in the 
 549   * <i>$options</i> parameter.  This includes a button next to the field that when clicked, 
 550   * will open an inline JavaScript calendar.  When a date is selected, it will automatically
 551   * populate the <input> tag with the proper date, formatted to the user's culture setting.
 552   *
 553   * <b>Note:</b> The <i>$name</i> parameter will automatically converted to array names. 
 554   * For example, a <i>$name</i> of "date" becomes date[from] and date[to]
 555   * 
 556   * <b>Options:</b>
 557   * - rich - If set to true, includes an inline JavaScript calendar can auto-populate the date field with the chosen date
 558   * - before - string to be displayed before the input_date_range_tag
 559   * - middle - string to be displayed between the from and to tags
 560   * - after - string to be displayed after the input_date_range_tag
 561   *
 562   * <b>Examples:</b>
 563   * <code>
 564   *  $date = array('from' => '2006-05-15', 'to' => '2006-06-15');
 565   *  echo input_date_range_tag('date', $date, array('rich' => true));
 566   * </code>
 567   *
 568   * <code>
 569   *  echo input_date_range_tag('date', null, array('middle' => ' through ', 'rich' => true));
 570   * </code>
 571   *
 572   * @param  string field name 
 573   * @param  array  dates: $value['from'] and $value['to']
 574   * @param  array  additional HTML compliant <input> tag parameters
 575   * @return string XHTML compliant <input> tag with optional JS calendar integration
 576   * @see input_date_tag
 577   */
 578  function input_date_range_tag($name, $value, $options = array())
 579  {
 580    $options = _parse_attributes($options);
 581  
 582    return _get_option($options, 'before', '').
 583           input_date_tag($name.'[from]', $value['from'], $options).
 584           _get_option($options, 'middle', '').
 585           input_date_tag($name.'[to]', $value['to'], $options).
 586           _get_option($options, 'after', '');
 587  }
 588  
 589  /**
 590   * Returns an XHTML compliant <input> tag to be used as a free-text date field.
 591   * 
 592   * You can easily implement a JavaScript calendar by enabling the 'rich' option in the 
 593   * <i>$options</i> parameter.  This includes a button next to the field that when clicked, 
 594   * will open an inline JavaScript calendar.  When a date is selected, it will automatically
 595   * populate the <input> tag with the proper date, formatted to the user's culture setting. 
 596   * Symfony also conveniently offers the input_date_range_tag, that allows you to specify a to
 597   * and from date.
 598   *
 599   * <b>Options:</b>
 600   * - rich - If set to true, includes an inline JavaScript calendar can auto-populate the date field with the chosen date
 601   *
 602   * <b>Examples:</b>
 603   * <code>
 604   *  echo input_date_tag('date', null, array('rich' => true));
 605   * </code>
 606   *
 607   * @param  string field name 
 608   * @param  string date
 609   * @param  array  additional HTML compliant <input> tag parameters
 610   * @return string XHTML compliant <input> tag with optional JS calendar integration
 611   * @see input_date_range_tag
 612   */
 613  function input_date_tag($name, $value = null, $options = array())
 614  {
 615    $options = _parse_attributes($options);
 616  
 617    $context = sfContext::getInstance();
 618  
 619    $culture = _get_option($options, 'culture', $context->getUser()->getCulture());
 620  
 621    $withTime = _get_option($options, 'withtime', false);
 622  
 623    // rich control?
 624    if (!_get_option($options, 'rich', false))
 625    {
 626      use_helper('DateForm');
 627  
 628      // set culture for month tag
 629      $options['culture'] = $culture;
 630  
 631      if ($withTime)
 632      {
 633        return select_datetime_tag($name, $value, $options, isset($options['html']) ? $options['html'] : array());
 634      }
 635      else
 636      {
 637        return select_date_tag($name, $value, $options, isset($options['html']) ? $options['html'] : array());
 638      }
 639    }
 640  
 641    $pattern = _get_option($options, 'format', $withTime ? 'g' : 'd');
 642  
 643    $dateFormat = new sfDateFormat($culture);
 644  
 645    $pattern = $dateFormat->getInputPattern($pattern);
 646  
 647    // parse date
 648    if ($value === null || $value === '')
 649    {
 650      $value = '';
 651    }
 652    else
 653    {
 654      $value = $dateFormat->format($value, $pattern);
 655    }
 656  
 657    // register our javascripts and stylesheets
 658    $langFile = sfConfig::get('sf_calendar_web_dir').'/lang/calendar-'.strtolower(substr($culture, 0, 2));
 659    $jss = array(
 660      sfConfig::get('sf_calendar_web_dir').'/calendar',
 661      is_readable(sfConfig::get('sf_symfony_data_dir').'/web/'.$langFile.'.js') || is_readable(sfConfig::get('sf_web_dir').'/'.$langFile.'.js') ? $langFile : sfConfig::get('sf_calendar_web_dir').'/lang/calendar-en',
 662      sfConfig::get('sf_calendar_web_dir').'/calendar-setup',
 663    );
 664    foreach ($jss as $js)
 665    {
 666      $context->getResponse()->addJavascript($js);
 667    }
 668  
 669    // css
 670    if ($calendar_style = _get_option($options, 'css', 'skins/aqua/theme'))
 671    {
 672      $context->getResponse()->addStylesheet(sfConfig::get('sf_calendar_web_dir').'/'.$calendar_style);
 673    }
 674  
 675    // date format
 676    $date_format = $dateFormat->getPattern($pattern);
 677  
 678    // calendar date format
 679    $calendar_date_format = $date_format;
 680    $calendar_date_format = strtr($date_format, array('yyyy' => 'Y', 'yy'=>'y', 'MM' => 'm', 'M'=>'m', 'dd'=>'d', 'd'=>'e', 'HH'=>'H', 'H'=>'k', 'hh'=>'I', 'h'=>'l', 'mm'=>'M', 'ss'=>'S', 'a'=>'p'));
 681  
 682    $calendar_date_format = preg_replace('/([mdyhklspe])+/i', '%\\1', $calendar_date_format);
 683  
 684    $id_inputField = (isset($options['id']))? $options['id'] : get_id_from_name($name);
 685    $id_calendarButton = 'trigger_'.get_id_from_name($name);
 686    $js = '
 687      document.getElementById("'.$id_calendarButton.'").disabled = false;
 688      Calendar.setup({
 689        inputField : "'.$id_inputField.'",
 690        ifFormat : "'.$calendar_date_format.'",
 691        daFormat : "'.$calendar_date_format.'",
 692        button : "'.$id_calendarButton.'"';
 693    
 694    if ($withTime)
 695    {
 696      $js .= ",\n showsTime : true";
 697    }
 698  
 699    // calendar options
 700    if ($calendar_options = _get_option($options, 'calendar_options'))
 701    {
 702      $js .= ",\n".$calendar_options;
 703    }
 704  
 705    $js .= '
 706      });
 707    ';
 708  
 709    // calendar button
 710    $calendar_button = '...';
 711    $calendar_button_type = 'txt';
 712    if ($calendar_button_img = _get_option($options, 'calendar_button_img'))
 713    {
 714      $calendar_button = $calendar_button_img;
 715      $calendar_button_type = 'img';
 716    }
 717    else if ($calendar_button_txt = _get_option($options, 'calendar_button_txt'))
 718    {
 719      $calendar_button = $calendar_button_txt;
 720      $calendar_button_type = 'txt';
 721    }
 722  
 723    // construct html
 724    if (!isset($options['size']))
 725    {
 726      // educated guess about the size
 727      $options['size'] = strlen($date_format)+2;
 728    }
 729    $html = input_tag($name, $value, $options);
 730  
 731    if ($calendar_button_type == 'img')
 732    {
 733      $html .= image_tag($calendar_button, array('id' => $id_calendarButton, 'style' => 'cursor: pointer; vertical-align: middle'));
 734    }
 735    else
 736    {
 737      $html .= content_tag('button', $calendar_button, array('type' => 'button', 'disabled' => 'disabled', 'onclick' => 'return false', 'id' => $id_calendarButton));
 738    }
 739  
 740    if (_get_option($options, 'with_format'))
 741    {
 742      $html .= '('.$date_format.')';
 743    }
 744  
 745    // add javascript
 746    $html .= content_tag('script', $js, array('type' => 'text/javascript'));
 747  
 748    return $html;
 749  }
 750  
 751  /**
 752   * Returns an XHTML compliant <input> tag with type="submit".
 753   * 
 754   * By default, this helper creates a submit tag with a name of <em>commit</em> to avoid
 755   * conflicts with other parts of the framework.  It is recommended that you do not use the name
 756   * "submit" for submit tags unless absolutely necessary. Also, the default <i>$value</i> parameter
 757   * (title of the button) is set to "Save changes", which can be easily overwritten by passing a 
 758   * <i>$value</i> parameter.
 759   *
 760   * <b>Examples:</b>
 761   * <code>
 762   *  echo submit_tag();
 763   * </code>
 764   *
 765   * <code>
 766   *  echo submit_tag('Update Record');
 767   * </code>
 768   *
 769   * @param  string field value (title of submit button)
 770   * @param  array  additional HTML compliant <input> tag parameters
 771   * @return string XHTML compliant <input> tag with type="submit"
 772   */
 773  function submit_tag($value = 'Save changes', $options = array())
 774  {
 775    return tag('input', array_merge(array('type' => 'submit', 'name' => 'commit', 'value' => $value), _convert_options_to_javascript(_convert_options($options))));
 776  }
 777  
 778  /**
 779   * Returns an XHTML compliant <input> tag with type="reset".
 780   *
 781   * By default, this helper creates a submit tag with a name of <em>reset</em>.  Also, the default 
 782   * <i>$value</i> parameter (title of the button) is set to "Reset" which can be easily overwritten 
 783   * by passing a <i>$value</i> parameter.
 784   *
 785   * <b>Examples:</b>
 786   * <code>
 787   *  echo reset_tag();
 788   * </code>
 789   *
 790   * <code>
 791   *  echo reset_tag('Start Over');
 792   * </code>
 793   *
 794   * @param  string field value (title of reset button)
 795   * @param  array  additional HTML compliant <input> tag parameters
 796   * @return string XHTML compliant <input> tag with type="reset"
 797   */
 798  function reset_tag($value = 'Reset', $options = array())
 799  {
 800    return tag('input', array_merge(array('type' => 'reset', 'name' => 'reset', 'value' => $value), _convert_options($options)));
 801  }
 802  
 803  /**
 804   * Returns an XHTML compliant <input> tag with type="image".
 805   *
 806   * The submit_image_tag is very similar to the submit_tag, the only difference being that it uses an image
 807   * for the submit button instead of the browser-generated default button. The image is defined by the 
 808   * <i>$source</i> parameter and must be a valid image, either local or remote (URL). By default, this 
 809   * helper creates a submit tag with a name of <em>commit</em> to avoid conflicts with other parts of the 
 810   * framework.  It is recommended that you do not use the name "submit" for submit tags unless absolutely necessary.
 811   *
 812   * <b>Examples:</b>
 813   * <code>
 814   *  // Assuming your image is in the /web/images/ directory
 815   *  echo submit_image_tag('my_submit_button.gif');
 816   * </code>
 817   *
 818   * <code>
 819   *  echo submit_image_tag('http://mydomain.com/my_submit_button.gif');
 820   * </code>
 821   *
 822   * @param  string path to image file
 823   * @param  array  additional HTML compliant <input> tag parameters
 824   * @return string XHTML compliant <input> tag with type="image"
 825   */
 826  function submit_image_tag($source, $options = array())
 827  {
 828    if (!isset($options['alt']))
 829    {
 830      $path_pos = strrpos($source, '/');
 831      $dot_pos = strrpos($source, '.');
 832      $begin = $path_pos ? $path_pos + 1 : 0;
 833      $nb_str = ($dot_pos ? $dot_pos : strlen($source)) - $begin;
 834      $options['alt'] = ucfirst(substr($source, $begin, $nb_str));
 835    }
 836  
 837    return tag('input', array_merge(array('type' => 'image', 'name' => 'commit', 'src' => image_path($source)), _convert_options_to_javascript(_convert_options($options))));
 838  }
 839  
 840  /**
 841   * Returns a <label> tag with <i>$label</i> for the specified <i>$id</i> parameter.
 842   *
 843   * @param  string id
 844   * @param  string label or title
 845   * @param  array  additional HTML compliant <label> tag parameters
 846   * @return string <label> tag with <i>$label</i> for the specified <i>$id</i> parameter.
 847   */
 848  function label_for($id, $label, $options = array())
 849  {
 850    $options = _parse_attributes($options);
 851  
 852    return content_tag('label', $label, array_merge(array('for' => get_id_from_name($id, null)), $options));
 853  }
 854  
 855  /**
 856   * Returns a formatted ID based on the <i>$name</i> parameter and optionally the <i>$value</i> parameter.
 857   *
 858   * This function determines the proper form field ID name based on the parameters. If a form field has an
 859   * array value as a name we need to convert them to proper and unique IDs like so:
 860   * <samp>
 861   *  name[] => name (if value == null)
 862   *  name[] => name_value (if value != null)
 863   *  name[bob] => name_bob
 864   *  name[item][total] => name_item_total
 865   * </samp>
 866   *
 867   * <b>Examples:</b>
 868   * <code>
 869   *  echo get_id_from_name('status[]', '1');
 870   * </code>
 871   *
 872   * @param  string field name 
 873   * @param  string field value
 874   * @return string <select> tag populated with all the languages in the world.
 875   */
 876  function get_id_from_name($name, $value = null)
 877  {
 878    // check to see if we have an array variable for a field name
 879    if (strstr($name, '['))
 880    {
 881      $name = str_replace(array('[]', '][', '[', ']'), array((($value != null) ? '_'.$value : ''), '_', '_', ''), $name);
 882    }
 883  
 884    return $name;
 885  }
 886  
 887  
 888  /**
 889   * Converts specific <i>$options</i> to their correct HTML format
 890   *
 891   * @param  array options
 892   * @return array returns properly formatted options 
 893   */
 894  function _convert_options($options)
 895  {
 896    $options = _parse_attributes($options);
 897  
 898    foreach (array('disabled', 'readonly', 'multiple') as $attribute)
 899    {
 900      if (array_key_exists($attribute, $options))
 901      {
 902        if ($options[$attribute])
 903        {
 904          $options[$attribute] = $attribute;
 905        }
 906        else
 907        {
 908          unset($options[$attribute]);
 909        }
 910      }
 911    }
 912  
 913    return $options;
 914  }
 915  
 916  function _convert_include_custom_for_select($options, &$select_options)
 917  {
 918    if (_get_option($options, 'include_blank'))
 919    {
 920      $select_options[''] = '';
 921    }
 922    else if ($include_custom = _get_option($options, 'include_custom'))
 923    {
 924      $select_options[''] = $include_custom;
 925    }
 926  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7