<commit_before><?php
/**
 * Various helpers to build forms, based on the FormHelper in the early version
 * of symfony (that seems to be absent from sf 1.4).
 *
 * Repopulating forms:
 *   The text input, checkbox, radio and textarea elements can be repopulated.
 *   For these elements, the value argument is a default value. When the field is repopulated,
 *   the get/post value is used in place of the default value.
 *   
 *   Checkbox and radios must use the array name syntax otherwise the default values are ignored.
 * 
 *   If an error exists in the request with the name matching one of the element's name,
 *   a css class "error" is added.
 * 
 * @author     <NAME>
 * @copyright  Code based on Symfony php framework, by <NAME> (www.symfony-project.org)
 */

/**
 * Returns a formatted set of <option> tags based on optional <i>$options</i> array variable.
 *
 * The options_for_select helper is usually called in conjunction with the select_tag helper, as it is relatively
 * useless on its own. By passing an array of <i>$options</i>, the helper will automatically generate <option> tags
 * using the array key as the value and the array value as the display title. Additionally the options_for_select tag is
 * smart enough to detect nested arrays as <optgroup> tags.  If the helper detects that the array value is an array itself,
 * it creates an <optgroup> tag with the name of the group being the key and the contents of the <optgroup> being the array.
 *
 * <b>Options:</b><commit_msg>(FormHelper.php): add method to output form with key/value data<commit_after><?php
/**
 * Various helpers to build forms, based on the FormHelper in the early version
 * of symfony (that seems to be absent from sf 1.4).
 * 
 * Methods:
 *   form_with_data()     Create a <form> with hidden inputs from key/values, and url.
 * 
 *   options_for_select()
 *   select_tag()
 *   input_tag()
 *   input_hidden_tag()
 *   input_password_tag()
 *   textarea_tag()
 *   checkbox_tag()
 *   radiobutton_tag()
 *   submit_tag()
 *   label_for()
 * 
 *
 * Repopulating forms:
 * 
 *   The text input, checkbox, radio and textarea elements can be repopulated.
 *   For these elements, the value argument is a default value. When the field is repopulated,
 *   the get/post value is used in place of the default value.
 *   
 *   Checkbox and radios must use the array name syntax otherwise the default values are ignored.
 * 
 *   If an error exists in the request with the name matching one of the element's name,
 *   a css class "error" is added.
 * 
 * @author     <NAME>
 * @copyright  Code based on Symfony php framework, by <NAME> (www.symfony-project.org)
 */

/**
 * Create a FORM with hidden inputs to submit manually or via a
 *  button that can be passed as $content.
 *
 * @param string $action       absolute or relative url (use url_for())
 * @param array  $data         the form data, encoded as <input type="hidden" ...>
 * @param array  $html_options html options for the <form> tag
 * @param string $content      optional content (eg. a button to submit)
 * @param mixed  $url
 *
 * @return string
 */
function form_with_data($url, $data = [], $html_options = [], $content = '')
{
  // set some defaults
  $html_options = array_merge([
    'method' => 'post',
  ], $html_options);

  $html_options['action'] = $url;

  $inputs = [];
  foreach ($data as $key => $val)
  {
    $inputs[] = input_hidden_tag($key, $val);
  }

  // (make the html output readable)
  $html = tag('form', $html_options, true)
    ."\n".implode("\n", $inputs)
    ."\n".$content
    ."\n</form>\n";

  return $html;
}


/**
 * Returns a formatted set of <option> tags based on optional <i>$options</i> array variable.
 *
 * The options_for_select helper is usually called in conjunction with the select_tag helper, as it is relatively
 * useless on its own. By passing an array of <i>$options</i>, the helper will automatically generate <option> tags
 * using the array key as the value and the array value as the display title. Additionally the options_for_select tag is
 * smart enough to detect nested arrays as <optgroup> tags.  If the helper detects that the array value is an array itself,
 * it creates an <optgroup> tag with the name of the group being the key and the contents of the <optgroup> being the array.
 *
 * <b>Options:</b>
{'fraction_non_alphanumeric': 0.09628261371507735, 'fraction_numerical': 0.0009235742322789194, 'mean_word_length': 3.4385245901639343, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '218455', 'n_tokens_mistral': 1261, 'n_tokens_neox': 1224, 'n_words': 576}