Icon class


General News

Icon::Get() returns the "img src" tag or "<i></i>" tags for an icon, depending on configuration (see the fake $_CONF['use_image_icons'] variable). CSS icons can come from Uikit or Fontawesome since I've noticed that non-Uikit themes include Fontawesome fonts. Not sure if this is still true.

Personally I like switching all to the CSS icons which have the advantage of taking color attributes like "danger", "warning", "grey", etc.

<?php


class Icon
{
&nbsp; &nbsp; private static $images = array(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'add', 'edit', 'copy', 'delete',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'list', 'mail', 'group', 'user',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'greyuser','check', 'greycheck',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'cross', 'disk', 'accept', 'addchild',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'update', 'wrench', 'info', 'greyinfo',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'blank'
&nbsp; &nbsp; &nbsp; &nbsp; );
&nbsp; &nbsp; private static $icons = array(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'add' &nbsp; &nbsp; &nbsp; => 'plus-circle',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'edit' &nbsp; &nbsp; &nbsp;=> 'edit',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'copy' &nbsp; &nbsp; &nbsp;=> 'copy',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'delete' &nbsp; &nbsp;=> 'remove',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'list' &nbsp; &nbsp; &nbsp;=> 'list',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'mail' &nbsp; &nbsp; &nbsp;=> 'enveloope-o',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'group' &nbsp; &nbsp; => 'group',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'user' &nbsp; &nbsp; &nbsp;=> 'user',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'greyuser' &nbsp;=> 'user',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'check' &nbsp; &nbsp; => 'check',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'greycheck' => 'check',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'cross' &nbsp; &nbsp; => 'plus',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'disk' &nbsp; &nbsp; &nbsp;=> 'save',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'accept' &nbsp; &nbsp;=> 'check',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'addchild' &nbsp;=> 'user-plus',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'update' &nbsp; &nbsp;=> 'save',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'wrench' &nbsp; &nbsp;=> 'wrench',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'info' &nbsp; &nbsp; &nbsp;=> 'info-circle',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'greyinfo' &nbsp;=> 'info-circle',
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'blank' &nbsp; &nbsp; => '',
&nbsp; &nbsp; );
&nbsp; &nbsp; private static $prefix = NULL;

&nbsp; &nbsp; /**
&nbsp; &nbsp; * &nbsp; Get a glFusion image icon
&nbsp; &nbsp; *
&nbsp; &nbsp; * &nbsp; @param &nbsp;string &nbsp;$name &nbsp; Name of icon, following the iconset prefix
&nbsp; &nbsp; * &nbsp; @param &nbsp;array &nbsp; $attrs &nbsp;Optional attributes to embed in the icon HTML
&nbsp; &nbsp; * &nbsp; @return string &nbsp; &nbsp; &nbsp;Full <img .../> tag for the icon
&nbsp; &nbsp; */
&nbsp; &nbsp; public static function getImage($name, $attrs = array())
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; global $_CONF, $_IMAGE_TYPE, $LANG_ADMIN, $_SYSTEM;

&nbsp; &nbsp; &nbsp; &nbsp; if (!in_array($name, self::$icons)) $name = 'blank';

&nbsp; &nbsp; &nbsp; &nbsp; $icon_url = "{$_CONF['layout_url']}/images/admin/$name.$_IMAGE_TYPE";
&nbsp; &nbsp; &nbsp; &nbsp; if (!isset($attrs['alt'])) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $alt = isset($attrs['title']) ? $attrs['title'] : $LANG_ADMIN[$name];
&nbsp; &nbsp; &nbsp; &nbsp; } else {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $alt = $attrs['alt'];
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; return COM_createImage($icon_url, $alt, $attrs);
&nbsp; &nbsp; }


&nbsp; &nbsp; /**
&nbsp; &nbsp; * &nbsp; Get a CSS icon from Uikit or Fontawesom
&nbsp; &nbsp; *
&nbsp; &nbsp; * &nbsp; @param &nbsp;string &nbsp;$name &nbsp; Name of icon, following the iconset prefix
&nbsp; &nbsp; * &nbsp; @param &nbsp;array &nbsp; $attrs &nbsp;Optional attributes to embed in the icon HTML
&nbsp; &nbsp; * &nbsp; @return string &nbsp; &nbsp; &nbsp;Full <i...></i> tag for the icon
&nbsp; &nbsp; */
&nbsp; &nbsp; public static function getCSS($name, $attrs = array())
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; global $LANG_ADMIN, $_SYSTEM;

&nbsp; &nbsp; &nbsp; &nbsp; if (self::$prefix === NULL) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self::$prefix = ($_SYSTEM['framework'] == 'uikit') ? 'uk-icon uk-' : 'fa fa-';
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; $attr_str = '';
&nbsp; &nbsp; &nbsp; &nbsp; if (!isset($attrs['alt'])) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attrs['alt'] = isset($attrs['title']) ? $attrs['title'] : $LANG_ADMIN[$name];
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; if (isset(self::$icons[$name])) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name = self::$icons[$name];
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; foreach ($attrs as $attr_name=>$attr_value) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attr_str .= ' ' . $attr_name . '="' . $attr_value . '"';
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; $icon = '<i class="' . self::$prefix . $name . '"' . $attr_str . '></i>';
&nbsp; &nbsp; &nbsp; &nbsp; return $icon;
&nbsp; &nbsp; }


&nbsp; &nbsp; /**
&nbsp; &nbsp; * &nbsp; Get the image or CSS icon depending on configuration.
&nbsp; &nbsp; *
&nbsp; &nbsp; * &nbsp; @param &nbsp;string &nbsp;$name &nbsp; Name of icon, following the iconset prefix
&nbsp; &nbsp; * &nbsp; @param &nbsp;array &nbsp; $attrs &nbsp;Optional attributes to embed in the icon HTML
&nbsp; &nbsp; * &nbsp; @return string &nbsp; &nbsp; &nbsp;Full <i...></i> tag for the icon
&nbsp; &nbsp; */
&nbsp; &nbsp; public static function Get($name, $attrs = array())
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; global $_CONF;

&nbsp; &nbsp; &nbsp; &nbsp; if ($_CONF['use_image_icons']) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return self::getImage($name, $attrs);
&nbsp; &nbsp; &nbsp; &nbsp; } else {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return self::getCSS($name, $attrs);
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }

}

?>

<pre>
&lt;?php


class Icon
{
&nbsp; &nbsp; private static $images = array(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;add&#39;, &#39;edit&#39;, &#39;copy&#39;, &#39;delete&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;list&#39;, &#39;mail&#39;, &#39;group&#39;, &#39;user&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;greyuser&#39;,&#39;check&#39;, &#39;greycheck&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;cross&#39;, &#39;disk&#39;, &#39;accept&#39;, &#39;addchild&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;update&#39;, &#39;wrench&#39;, &#39;info&#39;, &#39;greyinfo&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;blank&#39;
&nbsp; &nbsp; &nbsp; &nbsp; );
&nbsp; &nbsp; private static $icons = array(
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;add&#39; &nbsp; &nbsp; &nbsp; =&gt; &#39;plus-circle&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;edit&#39; &nbsp; &nbsp; &nbsp;=&gt; &#39;edit&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;copy&#39; &nbsp; &nbsp; &nbsp;=&gt; &#39;copy&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;delete&#39; &nbsp; &nbsp;=&gt; &#39;remove&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;list&#39; &nbsp; &nbsp; &nbsp;=&gt; &#39;list&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;mail&#39; &nbsp; &nbsp; &nbsp;=&gt; &#39;enveloope-o&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;group&#39; &nbsp; &nbsp; =&gt; &#39;group&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;user&#39; &nbsp; &nbsp; &nbsp;=&gt; &#39;user&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;greyuser&#39; &nbsp;=&gt; &#39;user&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;check&#39; &nbsp; &nbsp; =&gt; &#39;check&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;greycheck&#39; =&gt; &#39;check&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;cross&#39; &nbsp; &nbsp; =&gt; &#39;plus&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;disk&#39; &nbsp; &nbsp; &nbsp;=&gt; &#39;save&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;accept&#39; &nbsp; &nbsp;=&gt; &#39;check&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;addchild&#39; &nbsp;=&gt; &#39;user-plus&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;update&#39; &nbsp; &nbsp;=&gt; &#39;save&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;wrench&#39; &nbsp; &nbsp;=&gt; &#39;wrench&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;info&#39; &nbsp; &nbsp; &nbsp;=&gt; &#39;info-circle&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;greyinfo&#39; &nbsp;=&gt; &#39;info-circle&#39;,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &#39;blank&#39; &nbsp; &nbsp; =&gt; &#39;&#39;,
&nbsp; &nbsp; );
&nbsp; &nbsp; private static $prefix = NULL;

&nbsp; &nbsp; /**
&nbsp; &nbsp; * &nbsp; Get a glFusion image icon
&nbsp; &nbsp; *
&nbsp; &nbsp; * &nbsp; @param &nbsp;string &nbsp;$name &nbsp; Name of icon, following the iconset prefix
&nbsp; &nbsp; * &nbsp; @param &nbsp;array &nbsp; $attrs &nbsp;Optional attributes to embed in the icon HTML
&nbsp; &nbsp; * &nbsp; @return string &nbsp; &nbsp; &nbsp;Full &lt;img .../&gt; tag for the icon
&nbsp; &nbsp; */
&nbsp; &nbsp; public static function getImage($name, $attrs = array())
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; global $_CONF, $_IMAGE_TYPE, $LANG_ADMIN, $_SYSTEM;

&nbsp; &nbsp; &nbsp; &nbsp; if (!in_array($name, self::$icons)) $name = &#39;blank&#39;;

&nbsp; &nbsp; &nbsp; &nbsp; $icon_url = &quot;{$_CONF[&#39;layout_url&#39;]}/images/admin/$name.$_IMAGE_TYPE&quot;;
&nbsp; &nbsp; &nbsp; &nbsp; if (!isset($attrs[&#39;alt&#39;])) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $alt = isset($attrs[&#39;title&#39;]) ? $attrs[&#39;title&#39;] : $LANG_ADMIN[$name];
&nbsp; &nbsp; &nbsp; &nbsp; } else {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $alt = $attrs[&#39;alt&#39;];
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; return COM_createImage($icon_url, $alt, $attrs);
&nbsp; &nbsp; }


&nbsp; &nbsp; /**
&nbsp; &nbsp; * &nbsp; Get a CSS icon from Uikit or Fontawesom
&nbsp; &nbsp; *
&nbsp; &nbsp; * &nbsp; @param &nbsp;string &nbsp;$name &nbsp; Name of icon, following the iconset prefix
&nbsp; &nbsp; * &nbsp; @param &nbsp;array &nbsp; $attrs &nbsp;Optional attributes to embed in the icon HTML
&nbsp; &nbsp; * &nbsp; @return string &nbsp; &nbsp; &nbsp;Full &lt;i...&gt;&lt;/i&gt; tag for the icon
&nbsp; &nbsp; */
&nbsp; &nbsp; public static function getCSS($name, $attrs = array())
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; global $LANG_ADMIN, $_SYSTEM;

&nbsp; &nbsp; &nbsp; &nbsp; if (self::$prefix === NULL) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self::$prefix = ($_SYSTEM[&#39;framework&#39;] == &#39;uikit&#39;) ? &#39;uk-icon uk-&#39; : &#39;fa fa-&#39;;
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; $attr_str = &#39;&#39;;
&nbsp; &nbsp; &nbsp; &nbsp; if (!isset($attrs[&#39;alt&#39;])) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attrs[&#39;alt&#39;] = isset($attrs[&#39;title&#39;]) ? $attrs[&#39;title&#39;] : $LANG_ADMIN[$name];
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; if (isset(self::$icons[$name])) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name = self::$icons[$name];
&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; foreach ($attrs as $attr_name=&gt;$attr_value) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $attr_str .= &#39; &#39; . $attr_name . &#39;=&quot;&#39; . $attr_value . &#39;&quot;&#39;;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; $icon = &#39;&lt;i class=&quot;&#39; . self::$prefix . $name . &#39;&quot;&#39; . $attr_str . &#39;&gt;&lt;/i&gt;&#39;;
&nbsp; &nbsp; &nbsp; &nbsp; return $icon;
&nbsp; &nbsp; }


&nbsp; &nbsp; /**
&nbsp; &nbsp; * &nbsp; Get the image or CSS icon depending on configuration.
&nbsp; &nbsp; *
&nbsp; &nbsp; * &nbsp; @param &nbsp;string &nbsp;$name &nbsp; Name of icon, following the iconset prefix
&nbsp; &nbsp; * &nbsp; @param &nbsp;array &nbsp; $attrs &nbsp;Optional attributes to embed in the icon HTML
&nbsp; &nbsp; * &nbsp; @return string &nbsp; &nbsp; &nbsp;Full &lt;i...&gt;&lt;/i&gt; tag for the icon
&nbsp; &nbsp; */
&nbsp; &nbsp; public static function Get($name, $attrs = array())
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; global $_CONF;

&nbsp; &nbsp; &nbsp; &nbsp; if ($_CONF[&#39;use_image_icons&#39;]) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return self::getImage($name, $attrs);
&nbsp; &nbsp; &nbsp; &nbsp; } else {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return self::getCSS($name, $attrs);
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }

}

?&gt;</pre>
 

Share It!

Comments are closed