<commit_before><?php
/**
 * HTML/Tag functions.
 *
 * @package     Creovel
 * @subpackage  Helpers
 * @license     http://creovel.org/license MIT License
 * @since       Class available since Release 0.2.0
**/

/**
 * Base function used to create the different types of HTML tags.
 *
 * @param string $name Tag name
 * @param array $html_options Associative array of attributes
 * @param string $content
 * @return string
 * @author <NAME>
 **/
function create_html_element($name, $html_options = null, $content = null)
{
    $name = strtolower(trim($name));
    $no_end_tag = false;
    
    if ($name == 'state') {
        print_obj($html_options, 1);
    }
    
    // set flag for tags with no ends
    switch ($name) {
        case 'meta':
        case 'link':
        case 'input':
        case 'br':
        case 'img':
            $no_end_tag = true;
            break;
    }
    
    return "<{$name}" .
            (($attr_str = html_options_str($html_options)) ? ' ' . $attr_str : '') .
            ($no_end_tag ? ' />' : ">{$content}</{$name}>" );
}

/**
 * Creates a string of html tag attributes.
 *
 * @param array $html_options Assoicative array of attributes
 * @return string
 * @author <NAME>
 **/
function html_options_str($html_options)
{
    $options_str = '';
    if (count($html_options)){
        // lowercase all attributes
        foreach ($html_options as $attribute => $value) {
            $html_options[strtolower($attribute)] = $value;
        }
        
        // add confirm pop up
        if (isset($html_options['confirm'])) {
            $msg = str_replace("'", "\'", htmlentities($html_options['confirm']));
            if (isset($html_options['onclick'])) {
                $html_options['onclick'] =
                    "if ( !window.confirm('{$msg}') ) return false; " .
                    $html_options['onclick'];
            }
            unset($html_options['confirm']);
        }
        
        // create options string foreach valid option set
        foreach ($html_options as $attribute => $value) {
            $options_str .= ' ' . $attribute . '="' . $value .'"';
        }
    }
    
    return trim($options_str);
}

/**
 * Returns a stylesheets include tag.
 *
 * @param string $url Relative stylesheet path
 * @param string $media Stylesheet type default set to "screen"
 * @return string
 * @author <NAME>
 **/
function stylesheet_include_tag($url, $media = 'screen')
{
    $html_options = array(
        'rel' => 'stylesheet',
        'type' => 'text/css',
        'media' => $media,
        'href' => $url
        );
    if (is_array($media)) {
        $html_options = array_merge($html_options, $media);
        $html_options['media'] = isset($media['media']) ? $media['media'] : 'screen';
    }
    
    $return = '';
    if (is_array($url)) foreach ($url as $path) {
        $file = in_string('.css', $path) ? $path : CSS_URL . $path . '.css';
        $return .= stylesheet_include_tag($file, $media) . "\n";
    }
    return $return ? $return : create_html_element('link', $html_options);
}

/**
 * Returns a javascript script tag with $script for contents.
 *
 * @param string $script
 * @return string
 * @author <NAME>
 **/
function javascript_tag($script = '', $html_options = array())
{
    $html_options['type'] = 'text/javascript';
    return create_html_element('script', $html_options, $script);
}

/**
 * Returns a javascript include script tag.
 *
 * @param string $url Relative stylesheet path
 * @return string
 * @author <NAME>
 **/
function javascript_include_tag($url, $html_options = array())
{
    $return = '';
    if (is_array($url)) foreach ($url as $path) {
        $return .= javascript_include_tag(
                    $path ? JAVASCRIPT_URL . $path . '.js' : '',
                    $html_options) . "\n";
    }
    if ($url) $html_options['src'] = $url;
    return $return ? $return : javascript_tag('', $html_options);
}


/**
 * Creates a anchor link for lazy programmers.
 *
 * <code>
 * <?=link_to('Edit', 'agent', 'edit', $this->agent->id, array('class' => 'classname', 'target' => '_blank'))?>
 * </code>
 *
 * @param string $link_title Defaults to "Goto"
 * @param string $controller
 * @param string $action Optional
 * @param mixed $id Optional ID or an associative array of parameters
 * @param array $html_options
 * @return void
 * @author <NAME>
 **/
function link_to($link_title = 'Goto', $controller = '', $action = '', $id = '', $html_options = null)
{
    // set href
    $html_options['href'] = $html_options['href']
                            ? $html_options['href']
                            : url_for($controller, $action, $id, $html_options['https']);
    // if action is array merge it with html_options
    if (is_array($action)) $html_options = array_merge((array) $action, (array) $html_options);
    return create_html_element('a', $html_options, $link_title);
}

/**
 * Creates a anchor link for lazy programmers.
 *
 * <code>
 * <?=link_to_url('Edit', 'http://creovel.org', array('class' => 'classname', 'target' => '_blank'))?>
 * </code>
 *
 * @param string $link_title Defaults to "Goto"
 * @param string $url
 * @param array $html_options
 * @return void
 * @author <NAME>
 **/
function link_to_url($link_title = 'Goto', $url = '#', $html_options = null)
{
	$html_options['href'] = $url;
	return link_to($link_title, null, null, null, $html_options);
}

/**
 * Creates a anchor link for lazy programmers.
 *
 * <code>
 * <?=link_to_google_maps('Directions', '21 Jump Street Los Angeles, CA 90001', array( 'class' => 'classname', 'name' => 'top'))?><commit_msg>HTML helper updates.<commit_after><?php
/**
 * HTML/Tag functions.
 *
 * @package     Creovel
 * @subpackage  Helpers
 * @license     http://creovel.org/license MIT License
 * @since       Class available since Release 0.2.0
**/

/**
 * Base function used to create the different types of HTML tags.
 *
 * @param string $name Tag name
 * @param array $html_options Associative array of attributes
 * @param string $content
 * @return string
 * @author <NAME>
 **/
function create_html_element($name, $html_options = null, $content = null)
{
    $name = strtolower(trim($name));
    $no_end_tag = false;
    
    // set flag for tags with no ends
    switch ($name) {
        case 'meta':
        case 'link':
        case 'input':
        case 'br':
        case 'img':
            $no_end_tag = true;
            break;
    }
    
    return "<{$name}" .
            (($attr_str = html_options_str($html_options)) ? ' ' . $attr_str : '') .
            ($no_end_tag ? ' />' : ">{$content}</{$name}>" );
}

/**
 * Creates a string of html tag attributes.
 *
 * @param array $html_options Assoicative array of attributes
 * @return string
 * @author <NAME>
 **/
function html_options_str($html_options)
{
    $options_str = '';
    if (count($html_options)){
        // lowercase all attributes
        foreach ($html_options as $attribute => $value) {
            $html_options[strtolower($attribute)] = $value;
        }
        
        // add confirm pop up
        if (isset($html_options['confirm'])) {
            $msg = str_replace("'", "\'", htmlentities($html_options['confirm']));
            $onclick = "if (!window.confirm('{$msg}')) return false;";
            if (isset($html_options['onclick'])) {
                $html_options['onclick'] = $onclick . " " .$html_options['onclick'];
            } else {
                $html_options['onclick'] = $onclick;
            }
            unset($html_options['confirm']);
        }
        
        // create options string foreach valid option set
        foreach ($html_options as $attribute => $value) {
            $options_str .= ' ' . $attribute . '="' . $value .'"';
        }
    }
    
    return trim($options_str);
}

/**
 * Returns a stylesheets include tag.
 *
 * @param string $url Relative stylesheet path
 * @param string $media Stylesheet type default set to "screen"
 * @return string
 * @author <NAME>
 **/
function stylesheet_include_tag($url, $media = 'screen')
{
    $html_options = array(
        'rel' => 'stylesheet',
        'type' => 'text/css',
        'media' => $media,
        'href' => $url
        );
    if (is_array($media)) {
        $html_options = array_merge($html_options, $media);
        $html_options['media'] = isset($media['media']) ? $media['media'] : 'screen';
    }
    
    $return = '';
    if (is_array($url)) foreach ($url as $path) {
        $file = in_string('.css', $path) ? $path : CSS_URL . $path . '.css';
        $return .= stylesheet_include_tag($file, $media) . "\n";
    }
    return $return ? $return : create_html_element('link', $html_options);
}

/**
 * Returns a javascript script tag with $script for contents.
 *
 * @param string $script
 * @return string
 * @author <NAME>
 **/
function javascript_tag($script = '', $html_options = array())
{
    $html_options['type'] = 'text/javascript';
    return create_html_element('script', $html_options, $script);
}

/**
 * Returns a javascript include script tag.
 *
 * @param string $url Relative stylesheet path
 * @return string
 * @author <NAME>
 **/
function javascript_include_tag($url, $html_options = array())
{
    $return = '';
    if (is_array($url)) foreach ($url as $path) {
        $return .= javascript_include_tag(
                    $path ? JAVASCRIPT_URL . $path . '.js' : '',
                    $html_options) . "\n";
    }
    if ($url) $html_options['src'] = $url;
    return $return ? $return : javascript_tag('', $html_options);
}


/**
 * Creates a anchor link for lazy programmers.
 *
 * <code>
 * <?=link_to('Edit', 'agent', 'edit', $this->agent->id, array('class' => 'classname', 'target' => '_blank'))?>
 * </code>
 *
 * @param string $link_title Defaults to "Goto"
 * @param string $controller
 * @param string $action Optional
 * @param mixed $id Optional ID or an associative array of parameters
 * @param array $html_options
 * @return void
 * @author <NAME>
 **/
function link_to($link_title = 'Goto', $controller = '', $action = '', $id = '', $html_options = null)
{
    // set href
    $html_options['href'] = @$html_options['href']
                            ? $html_options['href']
                            : url_for($controller, $action, $id, @$html_options['https']);
    // if action is array merge it with html_options
    if (is_array($action)) $html_options = array_merge((array) $action, (array) $html_options);
    return create_html_element('a', $html_options, $link_title);
}

/**
 * Creates a anchor link for lazy programmers.
 *
 * <code>
 * <?=link_to_url('Edit', 'http://creovel.org', array('class' => 'classname', 'target' => '_blank'))?>
 * </code>
 *
 * @param string $link_title Defaults to "Goto"
 * @param string $url
 * @param array $html_options
 * @return void
 * @author <NAME>
 **/
function link_to_url($link_title = 'Goto', $url = '#', $html_options = null)
{
	$html_options['href'] = $url;
	return link_to($link_title, null, null, null, $html_options);
}

/**
 * Creates a anchor link for lazy programmers.
 *
 * <code>
 * <?=link_to_google_maps('Directions', '21 Jump Street Los Angeles, CA 90001', array( 'class' => 'classname', 'name' => 'top'))?>
{'fraction_non_alphanumeric': 0.16851520572450804, 'fraction_numerical': 0.0018783542039355994, 'mean_word_length': 2.792740841248304, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7198242', 'n_tokens_mistral': 3524, 'n_tokens_neox': 3466, 'n_words': 899}