Cloudlog/system/helpers/html_helper.php

392 lines
9.1 KiB
PHP
Raw Permalink Normal View History

<?php
2011-04-25 15:24:01 +00:00
/**
* CodeIgniter
*
* An open source application development framework for PHP
2011-04-25 15:24:01 +00:00
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014 - 2019, British Columbia Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package CodeIgniter
* @author EllisLab Dev Team
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
* @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 1.0.0
2011-04-25 15:24:01 +00:00
* @filesource
*/
defined('BASEPATH') OR exit('No direct script access allowed');
2011-04-25 15:24:01 +00:00
/**
* CodeIgniter HTML Helpers
*
* @package CodeIgniter
* @subpackage Helpers
* @category Helpers
* @author EllisLab Dev Team
* @link https://codeigniter.com/userguide3/helpers/html_helper.html
2011-04-25 15:24:01 +00:00
*/
// ------------------------------------------------------------------------
if ( ! function_exists('heading'))
{
/**
* Heading
*
* Generates an HTML heading tag.
*
* @param string content
* @param int heading level
* @param string
* @return string
*/
function heading($data = '', $h = '1', $attributes = '')
2011-04-25 15:24:01 +00:00
{
return '<h'.$h._stringify_attributes($attributes).'>'.$data.'</h'.$h.'>';
2011-04-25 15:24:01 +00:00
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('ul'))
{
/**
* Unordered List
*
* Generates an HTML unordered list from an single or multi-dimensional array.
*
* @param array
* @param mixed
* @return string
*/
2011-04-25 15:24:01 +00:00
function ul($list, $attributes = '')
{
return _list('ul', $list, $attributes);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('ol'))
{
/**
* Ordered List
*
* Generates an HTML ordered list from an single or multi-dimensional array.
*
* @param array
* @param mixed
* @return string
*/
2011-04-25 15:24:01 +00:00
function ol($list, $attributes = '')
{
return _list('ol', $list, $attributes);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('_list'))
{
/**
* Generates the list
*
* Generates an HTML ordered list from an single or multi-dimensional array.
*
* @param string
* @param mixed
* @param mixed
* @param int
* @return string
*/
function _list($type = 'ul', $list = array(), $attributes = '', $depth = 0)
2011-04-25 15:24:01 +00:00
{
// If an array wasn't submitted there's nothing to do...
if ( ! is_array($list))
{
return $list;
}
// Set the indentation based on the depth
$out = str_repeat(' ', $depth)
// Write the opening list tag
.'<'.$type._stringify_attributes($attributes).">\n";
2011-04-25 15:24:01 +00:00
// Cycle through the list elements. If an array is
// encountered we will recursively call _list()
static $_last_list_item = '';
foreach ($list as $key => $val)
{
$_last_list_item = $key;
$out .= str_repeat(' ', $depth + 2).'<li>';
2011-04-25 15:24:01 +00:00
if ( ! is_array($val))
{
$out .= $val;
}
else
{
$out .= $_last_list_item."\n"._list($type, $val, '', $depth + 4).str_repeat(' ', $depth + 2);
2011-04-25 15:24:01 +00:00
}
$out .= "</li>\n";
}
// Set the indentation for the closing tag and apply it
return $out.str_repeat(' ', $depth).'</'.$type.">\n";
2011-04-25 15:24:01 +00:00
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('img'))
{
/**
* Image
*
* Generates an <img /> element
*
* @param mixed
* @param bool
* @param mixed
* @return string
*/
function img($src = '', $index_page = FALSE, $attributes = '')
2011-04-25 15:24:01 +00:00
{
if ( ! is_array($src) )
{
$src = array('src' => $src);
}
// If there is no alt attribute defined, set it to an empty string
if ( ! isset($src['alt']))
{
$src['alt'] = '';
}
$img = '<img';
foreach ($src as $k => $v)
2011-04-25 15:24:01 +00:00
{
if ($k === 'src' && ! preg_match('#^(data:[a-z,;])|(([a-z]+:)?(?<!data:)//)#i', $v))
2011-04-25 15:24:01 +00:00
{
if ($index_page === TRUE)
{
$img .= ' src="'.get_instance()->config->site_url($v).'"';
2011-04-25 15:24:01 +00:00
}
else
{
$img .= ' src="'.get_instance()->config->base_url($v).'"';
2011-04-25 15:24:01 +00:00
}
}
else
{
$img .= ' '.$k.'="'.$v.'"';
2011-04-25 15:24:01 +00:00
}
}
return $img._stringify_attributes($attributes).' />';
2011-04-25 15:24:01 +00:00
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('doctype'))
{
/**
* Doctype
*
* Generates a page document type declaration
*
* Examples of valid options: html5, xhtml-11, xhtml-strict, xhtml-trans,
* xhtml-frame, html4-strict, html4-trans, and html4-frame.
* All values are saved in the doctypes config file.
*
* @param string type The doctype to be generated
* @return string
*/
function doctype($type = 'html5')
2011-04-25 15:24:01 +00:00
{
static $doctypes;
2011-04-25 15:24:01 +00:00
if ( ! is_array($doctypes))
2011-04-25 15:24:01 +00:00
{
if (file_exists(APPPATH.'config/doctypes.php'))
{
include(APPPATH.'config/doctypes.php');
}
if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php'))
2011-04-25 15:24:01 +00:00
{
include(APPPATH.'config/'.ENVIRONMENT.'/doctypes.php');
}
if (empty($_doctypes) OR ! is_array($_doctypes))
{
$doctypes = array();
2011-04-25 15:24:01 +00:00
return FALSE;
}
$doctypes = $_doctypes;
2011-04-25 15:24:01 +00:00
}
return isset($doctypes[$type]) ? $doctypes[$type] : FALSE;
2011-04-25 15:24:01 +00:00
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('link_tag'))
{
/**
* Link
*
* Generates link to a CSS file
*
* @param mixed stylesheet hrefs or an array
* @param string rel
* @param string type
* @param string title
* @param string media
* @param bool should index_page be added to the css path
* @return string
*/
2011-04-25 15:24:01 +00:00
function link_tag($href = '', $rel = 'stylesheet', $type = 'text/css', $title = '', $media = '', $index_page = FALSE)
{
$CI =& get_instance();
$link = '<link ';
if (is_array($href))
{
foreach ($href as $k => $v)
2011-04-25 15:24:01 +00:00
{
if ($k === 'href' && ! preg_match('#^([a-z]+:)?//#i', $v))
2011-04-25 15:24:01 +00:00
{
if ($index_page === TRUE)
{
$link .= 'href="'.$CI->config->site_url($v).'" ';
}
else
{
$link .= 'href="'.$CI->config->base_url($v).'" ';
2011-04-25 15:24:01 +00:00
}
}
else
{
$link .= $k.'="'.$v.'" ';
2011-04-25 15:24:01 +00:00
}
}
}
else
{
if (preg_match('#^([a-z]+:)?//#i', $href))
2011-04-25 15:24:01 +00:00
{
$link .= 'href="'.$href.'" ';
}
elseif ($index_page === TRUE)
{
$link .= 'href="'.$CI->config->site_url($href).'" ';
}
else
{
$link .= 'href="'.$CI->config->base_url($href).'" ';
2011-04-25 15:24:01 +00:00
}
$link .= 'rel="'.$rel.'" type="'.$type.'" ';
if ($media !== '')
2011-04-25 15:24:01 +00:00
{
$link .= 'media="'.$media.'" ';
}
if ($title !== '')
2011-04-25 15:24:01 +00:00
{
$link .= 'title="'.$title.'" ';
}
}
return $link."/>\n";
2011-04-25 15:24:01 +00:00
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('meta'))
{
/**
* Generates meta tags from an array of key/values
*
* @param array
* @param string
* @param string
* @param string
* @return string
*/
2011-04-25 15:24:01 +00:00
function meta($name = '', $content = '', $type = 'name', $newline = "\n")
{
// Since we allow the data to be passes as a string, a simple array
// or a multidimensional one, we need to do a little prepping.
if ( ! is_array($name))
{
$name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
}
elseif (isset($name['name']))
2011-04-25 15:24:01 +00:00
{
// Turn single array into multidimensional
$name = array($name);
2011-04-25 15:24:01 +00:00
}
$allowed_types = array('charset', 'http-equiv', 'name', 'property');
2011-04-25 15:24:01 +00:00
$str = '';
foreach ($name as $meta)
{
// This is to preserve BC with pre-3.1 versions where only
// 'http-equiv' (default) and 'name' were supported.
if (isset($meta['type']))
{
if ($meta['type'] === 'equiv')
{
$meta['type'] = 'http-equiv';
}
elseif ( ! in_array($meta['type'], $allowed_types, TRUE))
{
$meta['type'] = 'name';
}
}
2011-04-25 15:24:01 +00:00
$type = isset($meta['type']) ? $meta['type'] : 'name';
$name = isset($meta['name']) ? $meta['name'] : '';
$content = isset($meta['content']) ? $meta['content'] : '';
$newline = isset($meta['newline']) ? $meta['newline'] : "\n";
$str .= '<meta '.$type.'="'.$name.($type === 'charset' ? '' : '" content="'.$content).'" />'.$newline;
2011-04-25 15:24:01 +00:00
}
return $str;
}
}