<commit_before><?php

/* PHP port of tag helpers from Rails

url_helper.rb
  url_for           OK
  link_to           
  link_image_to       DEPRECATED
  link_to_unless_current    
  link_to_unless        
  link_to_if          
  mail_to           
  current_page        

*/

// +---------------------------------------------------------------------------+
// | This file is part of the SymFony Framework project.                        |
// | Copyright (c) 2004, 2005 <NAME>.                                          |
// +---------------------------------------------------------------------------+

/**
 *
 * @package   sf_runtime
 * @subpackage helper
 *
 * @author    <NAME> (<EMAIL>)
 *  (c) <NAME>
 * @since     1.0.0
 * @version   $Id: UrlHelper.php 531 2005-10-18 09:43:40Z fabien $
 */

function url_for($url)
{
  return sfContext::getInstance()->getController()->genUrl(null, $url);
}
 
/*
    # Creates a link tag of the given +name+ using an URL created by the set of +options+. See the valid options in
    # link:classes/ActionController/Base.html#M000021. It's also possible to pass a string instead of an options hash to
    # get a link tag that just points without consideration. If nil is passed as a name, the link itself will become the name.
    # The html_options have a special feature for creating javascript confirm alerts where if you pass :confirm => 'Are you sure?',
    # the link will be guarded with a JS popup asking that question. If the user accepts, the link is processed, otherwise not.
    #
    # Example:
    #   link_to "Delete this page", { :action => "destroy", :id => @page.id }, :confirm => "Are you sure?"
*/
function link_to($name = '', $options = '', $html_options = array(), $parameters_for_method_reference = array())
{
  $html_options = _parse_attributes($html_options);

  $html_options = _convert_confirm_option_to_javascript($html_options);
  if (!is_array($options) && preg_match('/^http/', $options))
  {
    $html_options['href'] = $options;
    if (!strlen($name)) $name = $html_options['href'];
    return content_tag('a', $name, $html_options);
  }
  else
  {
    $html_options['href'] = url_for($options, $parameters_for_method_reference);
    if (!strlen($name)) $name = $html_options['href'];
    return content_tag('a', $name, $html_options);
  }
}

function link_to_if($condition, $name = '', $options = '', $html_options = array(), $parameters_for_method_reference = array())
{
  if ($condition)
  {
    return link_to($name, $options, $html_options, $parameters_for_method_reference);
  }
  else
  {
    if (isset($html_options['tag']))
    {
      $tag = $html_options['tag'];
      unset($html_options['tag']);
    }
    else
    {
      $tag = 'span';
    }

    return content_tag($tag, $name, $html_options);
  }
}

function link_to_unless($condition, $name = '', $options = '', $html_options = array(), $parameters_for_method_reference = array())
{
  return link_to_if(!$condition, $name, $options, $html_options, $parameters_for_method_reference);
}

function _convert_confirm_option_to_javascript($html_options)
{
  if (isset($html_options['confirm']))
  {
    $confirm = preg_replace("/'/", "\\'", $html_options['confirm']);
    unset($html_options['confirm']);
    $html_options['onclick'] = "return confirm('$confirm');";
  }

  return $html_options;
}

function mail_to($email, $encode = true)
{
  if ($encode)
  {
    $email = _encodeText($email);
    return content_tag('a', $email, array('href' => _encodeText('mailto:').$email));
  }
  else
  {
    return content_tag('a', $email, array('href' => 'mailto:'.$email));
  }
}

function _encodeText($text)
{
  $encoded_text = '';

  for ($i = 0; $i < strlen($text); $i++)
  {
    $char = $text{$i};
    $r = rand(0, 100);

    # roughly 10% raw, 45% hex, 45% dec
    # '@' *must* be encoded. I insist.
    if ($r > 90 && $char != '@')
    {
      $encoded_text .= $char;
    }
    else if ($r < 45)
    {
      $encoded_text .= '&#x'.dechex(ord($char)).';';
    }
    else<commit_msg>added button_to helper and post support for link_to<commit_after><?php

/* PHP port of tag helpers from Rails

url_helper.rb
  url_for           OK
  link_to           
  link_image_to       DEPRECATED
  link_to_unless_current    
  link_to_unless        
  link_to_if          
  mail_to           
  current_page        

*/

// +---------------------------------------------------------------------------+
// | This file is part of the SymFony Framework project.                        |
// | Copyright (c) 2004, 2005 <NAME>.                                          |
// +---------------------------------------------------------------------------+

/**
 *
 * @package   sf_runtime
 * @subpackage helper
 *
 * @author    <NAME> (<EMAIL>)
 *  (c) <NAME>
 * @since     1.0.0
 * @version   $Id: UrlHelper.php 531 2005-10-18 09:43:40Z fabien $
 */

function url_for($url)
{
  return sfContext::getInstance()->getController()->genUrl(null, $url);
}
 
/*
    # Creates a link tag of the given +name+ using an URL created by the set of +options+. See the valid options in
    # link:classes/ActionController/Base.html#M000021. It's also possible to pass a string instead of an options hash to
    # get a link tag that just points without consideration. If nil is passed as a name, the link itself will become the name.
    # The html_options have a special feature for creating javascript confirm alerts where if you pass :confirm => 'Are you sure?',
    # the link will be guarded with a JS popup asking that question. If the user accepts, the link is processed, otherwise not.
    #
    # Example:
    #   link_to "Delete this page", { :action => "destroy", :id => @page.id }, :confirm => "Are you sure?"
*/
function link_to($name = '', $options = '', $html_options = array(), $parameters_for_method_reference = array())
{
  $html_options = _parse_attributes($html_options);

  $html_options = _convert_options_to_javascript($html_options);

  $html_options['href'] = url_for($options, $parameters_for_method_reference);
  if (!strlen($name))
  {
    $name = $html_options['href'];
  }

  return content_tag('a', $name, $html_options);
}

function link_to_if($condition, $name = '', $options = '', $html_options = array(), $parameters_for_method_reference = array())
{
  if ($condition)
  {
    return link_to($name, $options, $html_options, $parameters_for_method_reference);
  }
}

function link_to_unless($condition, $name = '', $options = '', $html_options = array(), $parameters_for_method_reference = array())
{
  return link_to_if(!$condition, $name, $options, $html_options, $parameters_for_method_reference);
}

function button_to($name, $target, $options = array())
{
  $html_options = _convert_options($options);
  $html_options['value']   = $name;

  if (isset($html_options['post']) && $html_options['post'])
  {
    $html_options['type'] = 'submit';
    unset($html_options['post']);
    $html_options = _convert_options_to_javascript($html_options);

    return form_tag($target, array('method' => 'post', 'class' => 'button_to')).tag('input', $html_options).'</form>';
  }
  else
  {
    $html_options['type']    = 'button';
    $html_options['onclick'] = "document.location.href='".url_for($target)."';";
    $html_options = _convert_options_to_javascript($html_options);

    return tag('input', $html_options);
  }
}

function mail_to($email, $encode = true)
{
  if ($encode)
  {
    $email = _encodeText($email);

    return content_tag('a', $email, array('href' => _encodeText('mailto:').$email));
  }
  else
  {
    return content_tag('a', $email, array('href' => 'mailto:'.$email));
  }
}

function _convert_options_to_javascript($html_options)
{
  // confirm
  $confirm = isset($html_options['confirm']) ? $html_options['confirm'] : '';
  unset($html_options['confirm']);

  // popup
  $popup = isset($html_options['popup']) ? $html_options['popup'] : '';
  unset($html_options['popup']);

  // post
  $post = isset($html_options['post']) ? $html_options['post'] : '';
  unset($html_options['post']);

  $onclick = isset($html_options['onclick']) ? $html_options['onclick'] : '';

  if ($popup && $post)
  {
    throw new sfConfigurationException('You can\'t use "popup" and "post" together');
  }
  else if ($confirm && $popup)
  {
    $html_options['onclick'] = $onclick.'if ('._confirm_javascript_function($confirm).') { '._popup_javascript_function($popup).' };return false;';
  }
  else if ($confirm && $post)
  {
    $html_options['onclick'] = $onclick.'if ('._confirm_javascript_function($confirm).') { '._post_javascript_function().' };return false;';
  }
  else if ($confirm)
  {
    if ($onclick)
    {
      $html_options['onclick'] = 'if ('._confirm_javascript_function($confirm).') {'.$onclick.'}';
    }
    else
    {
      $html_options['onclick'] = 'return '._confirm_javascript_function($confirm).';';
    }
  }
  else if ($post)
  {
    $html_options['onclick'] = $onclick._post_javascript_function().'return false;';
  }
  else if ($popup)
  {
    $html_options['onclick'] = $onclick._popup_javascript_function($popup).'return false;';
  }

  return $html_options;
}

function _confirm_javascript_function($confirm)
{
  return "confirm('".escape_javascript($confirm)."')";
}

function _popup_javascript_function($popup)
{
  return is_array($popup) ? "window.open(this.href,'".$popup[0]."','".$popup[-1]."');" : "window.open(this.href);";
}

function _post_javascript_function()
{
  return "f = document.createElement('form'); document.body.appendChild(f); f.method = 'POST'; f.action = this.href; f.submit();";
}

function _encodeText($text)
{
  $encoded_text = '';

  for ($i = 0; $i < strlen($text); $i++)
  {
    $char = $text{$i};
    $r = rand(0, 100);

    # roughly 10% raw, 45% hex, 45% dec
    # '@' *must* be encoded. I insist.
    if ($r > 90 && $char != '@')
    {
      $encoded_text .= $char;
    }
    else if ($r < 45)
    {
      $encoded_text .= '&#x'.dechex(ord($char)).';';
    }
    else
{'fraction_non_alphanumeric': 0.1926163723916533, 'fraction_numerical': 0.010032102728731942, 'mean_word_length': 3.080638559148588, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5503604', 'n_tokens_mistral': 3273, 'n_tokens_neox': 3158, 'n_words': 737}