*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
use CodeIgniter\Files\Exceptions\FileNotFoundException;
use Config\DocTypes;
use Config\Mimes;
// CodeIgniter HTML Helpers
if (! function_exists('ul')) {
/**
* Unordered List
*
* Generates an HTML unordered list from an single or
* multi-dimensional array.
*
* @param mixed $attributes HTML attributes string, array, object
*/
function ul(array $list, $attributes = ''): string
{
return _list('ul', $list, $attributes);
}
}
if (! function_exists('ol')) {
/**
* Ordered List
*
* Generates an HTML ordered list from an single or multi-dimensional array.
*
* @param mixed $attributes HTML attributes string, array, object
*/
function ol(array $list, $attributes = ''): string
{
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 mixed $list
* @param mixed $attributes string, array, object
*/
function _list(string $type = 'ul', $list = [], $attributes = '', int $depth = 0): string
{
// Set the indentation based on the depth
$out = str_repeat(' ', $depth)
// Write the opening list tag
. '<' . $type . stringify_attributes($attributes) . ">\n";
// Cycle through the list elements. If an array is
// encountered we will recursively call _list()
foreach ($list as $key => $val) {
$out .= str_repeat(' ', $depth + 2) . '
\n";
}
// Set the indentation for the closing tag and apply it
return $out . str_repeat(' ', $depth) . '' . $type . ">\n";
}
}
if (! function_exists('img')) {
/**
* Image
*
* Generates an image element
*
* @param array|string $src Image source URI, or array of attributes and values
* @param bool $indexPage Whether to treat $src as a routed URI string
* @param array|object|string $attributes Additional HTML attributes
*/
function img($src = '', bool $indexPage = false, $attributes = ''): string
{
if (! is_array($src)) {
$src = ['src' => $src];
}
if (! isset($src['src'])) {
$src['src'] = $attributes['src'] ?? '';
}
if (! isset($src['alt'])) {
$src['alt'] = $attributes['alt'] ?? '';
}
$img = ' $value) {
$img .= ' ' . $key . '="' . $value . '"';
}
// Prevent passing completed values to stringify_attributes
if (is_array($attributes)) {
unset($attributes['alt'], $attributes['src']);
}
return $img . stringify_attributes($attributes) . ' />';
}
}
if (! function_exists('img_data')) {
/**
* Image (data)
*
* Generates a src-ready string from an image using the "data:" protocol
*
* @param string $path Image source path
* @param string|null $mime MIME type to use, or null to guess
*/
function img_data(string $path, ?string $mime = null): string
{
if (! is_file($path) || ! is_readable($path)) {
throw FileNotFoundException::forFileNotFound($path);
}
// Read in file binary data
$handle = fopen($path, 'rb');
$data = fread($handle, filesize($path));
fclose($handle);
// Encode as base64
$data = base64_encode($data);
// Figure out the type (Hail Mary to JPEG)
$mime = $mime ?? Mimes::guessTypeFromExtension(pathinfo($path, PATHINFO_EXTENSION)) ?? 'image/jpg';
return 'data:' . $mime . ';base64,' . $data;
}
}
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
*/
function doctype(string $type = 'html5'): string
{
$config = new DocTypes();
$doctypes = $config->list;
return $doctypes[$type] ?? false;
}
}
if (! function_exists('script_tag')) {
/**
* Script
*
* Generates link to a JS file
*
* @param mixed $src Script source or an array
* @param bool $indexPage Should indexPage be added to the JS path
*/
function script_tag($src = '', bool $indexPage = false): string
{
$script = '';
}
}
if (! function_exists('link_tag')) {
/**
* Link
*
* Generates link to a CSS file
*
* @param mixed $href Stylesheet href or an array
* @param bool $indexPage should indexPage be added to the CSS path.
*/
function link_tag($href = '', string $rel = 'stylesheet', string $type = 'text/css', string $title = '', string $media = '', bool $indexPage = false, string $hreflang = ''): string
{
$link = '';
}
}
if (! function_exists('video')) {
/**
* Video
*
* Generates a video element to embed videos. The video element can
* contain one or more video sources
*
* @param mixed $src Either a source string or an array of sources
* @param string $unsupportedMessage The message to display if the media tag is not supported by the browser
* @param string $attributes HTML attributes
*/
function video($src, string $unsupportedMessage = '', string $attributes = '', array $tracks = [], bool $indexPage = false): string
{
if (is_array($src)) {
return _media('video', $src, $unsupportedMessage, $attributes, $tracks);
}
$video = '