| Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Html/ |
| Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Html/TfHtml.php |
<?php
/*
* @package Tech Fry Library
* @license https://www.gnu.org/licenses/gpl-3.0.en.html
*/
namespace TechFry\Library\View\Html;
defined('_JEXEC') or die;
use Joomla\CMS\HTML\HTMLHelper;
// Depreciated: use THtml, THead, TBody Class
class TfHtml
{
public static function open_tag($tag, $attribs = '', $bool = '')
{
$attributes = array();
// If $attribs is string, then it is class attribute value
if ($attribs && \is_string($attribs))
{
$attributes['class'] = $attribs;
}
elseif (\is_array($attribs))
{
$attributes = $attribs;
}
$output = '<' . $tag;
// Remove spaces from class attribute
if (isset($attributes['class']) && $attributes['class'])
{
$attributes['class'] = trim(preg_replace('/\s+/', ' ', $attributes['class']));
}
if (!empty($attributes))
{
foreach ($attributes as $k => $v)
{
// Replace underscore with dashes for aria and data attributes
$k = str_replace('_', '-', $k);
if (trim($v))
{
$output .= ' ' . $k . '="' . $v . '"';
}
}
}
// $bool - boolean attributes can be string or array
if ($bool)
{
if (\is_array($bool))
{
foreach ($bool as $b)
{
$output .= ' ' . $b;
}
}
else
{
$output .= ' ' . $bool;
}
}
$output .= '>' . "\n";
return $output;
}
public static function close_tag($tag)
{
return '</' . $tag . '>' . "\n";
}
// Element - opening tag, closing tag, content, attributes
public static function get_element($content, $tag = 'div', $attribs = '', $bool = '')
{
if ($content == '')
{
return;
}
$output = self::open_tag($tag, $attribs, $bool);
$output .= $content . "\n";
$output .= self::close_tag($tag);
return $output;
}
// Paragraph
public static function get_p($content, $attribs = '')
{
return self::get_element($content, 'p', $attribs);
}
// Headings
public static function get_heading($text, $level = 'h1', $attribs = '')
{
return self::get_element($text, $level, $attribs);
}
public static function get_h1($content, $attribs = '')
{
return self::get_element($content, 'h1', $attribs);
}
public static function get_h2($content, $attribs = '')
{
return self::get_element($content, 'h2', $attribs);
}
public static function get_h3($content, $attribs = '')
{
return self::get_element($content, 'h3', $attribs);
}
public static function get_h4($content, $attribs = '')
{
return self::get_element($content, 'h4', $attribs);
}
public static function get_h5($content, $attribs = '')
{
return self::get_element($content, 'h5', $attribs);
}
public static function get_h6($content, $attribs = '')
{
return self::get_element($content, 'h6', $attribs);
}
// Links
public static function get_a($text, $href = '#', $attribs = [], $bool = '')
{
if ($text == '')
{
return;
}
$array = array(
'href' => $href,
'title' => $text,
);
$attribs = array_merge($array, $attribs);
return self::get_element($text, 'a', $attribs, $bool);
}
// Image
public static function get_img($src, $alt = '', $attribs = array())
{
return self::get_image($src, $alt, $attribs);
}
public static function get_image($src, $alt = '', $attribs = array())
{
// $attribs - width, height, title
if ($src == '')
{
return;
}
// For Absolute URLs
if (isset(parse_url($src)['host']))
{
$image_url = $src;
}
else
{
$image = HTMLHelper::cleanImageURL($src);
$image_url = $image->url;
$src = $image_url;
}
$image_info = getimagesize($image_url);
if (!$image_info)
{
return;
}
$array = array(
'src' => $src,
'alt' => $alt,
);
$attribs = array_merge($array, $attribs);
return self::open_tag('img', $attribs);
}
// Table
public static function get_table($rows = [], $attribs = [], $theads = [], $caption = '')
{
if (empty($rows) && empty($theads))
{
return;
}
//$class = $style ? ' class="' . $style . '"' : '';
//$output = '<table' . $class . '>';
$output = self::open_tag('table', $attribs);
if ($caption)
{
$output .= '<caption>' . $caption . '</caption>';
}
if (!empty($theads))
{
$output .= '<thead><tr>';
foreach ($theads as $thead)
{
$output .= '<th>' . $thead . '</th>';
}
$output .= '</tr></thead>';
}
$output .= '<tbody>';
if ($rows)
{
foreach ($rows as $row)
{
$output .= '<tr>';
$row = is_array($row) ? $row : (array) $row;
foreach ($row as $col)
{
$output .= '<td>' . $col . '</td>';
}
$output .= '</tr>';
}
}
$output .= '</tbody>';
$output .= '</table>';
return $output;
}
// Unordered List
public static function get_ul($items, $list_style = '', $item_style = '')
{
if (empty($items))
{
return;
}
$list_class = $list_style ? ' class="' . trim($list_style) . '"' : '';
$item_class = $item_style ? ' class="' . trim($item_style) . '"' : '';
$output = '<ul'. $list_class . '>';
foreach ($items as $item)
{
$output .= '<li' . $item_class . '>';
$output .= $item;
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
// Ordered List
public static function get_ol($items, $list_style = '', $item_style = '')
{
if (empty($items))
{
return;
}
$list_class = $list_style ? ' class="' . trim($list_style) . '"' : '';
$item_class = $item_style ? ' class="' . trim($item_style) . '"' : '';
$output = '<ol'. $list_class . '>';
foreach ($items as $item)
{
$output .= '<li' . $item_class . '>';
$output .= $item;
$output .= '</li>';
}
$output .= '</ol>';
return $output;
}
public static function open_div($attribs = '', $bool = '')
{
return self::open_tag('div', $attribs, $bool);
}
public static function close_div()
{
return self::close_tag('div');
}
public static function get_div($content, $attribs = '', $bool = '')
{
return self::get_element($content, 'div', $attribs, $bool);
}
public static function open_span($attribs = '', $bool = '')
{
return self::open_tag('span', $attribs, $bool);
}
public static function close_span()
{
return self::close_tag('span');
}
public static function get_span($content, $attribs = '', $bool = '')
{
return self::get_element($content, 'span', $attribs, $bool);
}
// Title tag in head
public static function get_title($title)
{
return self::get_element($title, 'title');
}
public static function get_favicon($href = '')
{
// <link rel="icon" href="favicon.ico" type="image/x-icon" />
if (empty($href))
{
return;
}
$attribs = array(
'rel' => 'icon',
'href' => $href,
'type' => 'image/x-icon',
);
return self::open_tag('link', $attribs);
}
public static function get_stylesheet($href = '')
{
// <link rel="stylesheet" href="my-css-file.css" />
if (empty($href))
{
return;
}
$attribs = array(
'rel' => 'stylesheet',
'href' => $href,
'type' => 'text/css',
);
return self::open_tag('link', $attribs);
}
public static function get_script($src = '')
{
// <script src="my-js-file.js" defer></script>
if (empty($src))
{
return;
}
$output = '<script src="' . $src . '" defer></script>';
return $output;
}
public static function get_js($code, $attribs = '')
{
$output = self::get_element($code, 'script', $attribs);
return $output;
}
public static function get_style($code)
{
$output = '<style>';
$output .= $code;
$output .= '</style>' . "\n";
return $output;
}
// CSS Code
public static function get_css($selector, $defs = array())
{
$output = '';
if (!empty($defs))
{
foreach ($defs as $k => $v)
{
if ($v != '')
{
$output .= $k . ':' . $v . ';';
}
}
}
if ($output)
{
return $selector . ' {' . $output . '}' . "\n";
}
return;
}
// Meta tag in head - author, description, generator
public static function get_meta($name, $content)
{
$attribs = array(
'name' => $name,
'content' => $content,
);
return self::open_tag('meta', $attribs);
}
public static function get_responsive()
{
return self::get_meta('viewport', 'width=device-width, initial-scale=1.0');
}
public static function get_charset($charset = 'UTF-8')
{
return self::open_tag('meta', array('charset' => $charset));
}
// Video
public static function get_video($src, $attribs = array(), $bool = '')
{
// $attribs - width, height, poster
// $bool - controls, autoplay, loop, muted
if ($src == '')
{
return;
}
$output = self::open_tag('video', $attribs, $bool);
$output .= self::close_tag('video');
return $output;
}
// Audio
public static function get_audio($src, $attribs = array(), $bool = '')
{
// $attribs
// $bool - controls, autoplay, loop, muted
if ($src == '')
{
return;
}
$output = self::open_tag('audio', $attribs, $bool);
$output .= self::close_tag('audio');
return $output;
}
}