Allow text alternative for images to percolate all the way through template layers
[squirrelmail.git] / functions / html.php
index 6b408ff3ccfd5c7d250d449f1be0d4ef5fcca74a..3172bc5044d4c7fd855e0def212b323d61fea25e 100644 (file)
 /**
  * html.php
  *
- * Copyright (c) 1999-2005 The SquirrelMail Project Team
- * Licensed under the GNU GPL. For full terms see the file COPYING.
- *
  * The idea is to inlcude here some functions to make easier
  * the right to left implementation by "functionize" some
  * html outputs.
  *
+ * @copyright © 1999-2006 The SquirrelMail Project Team
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package squirrelmail
+ * @since 1.3.0
+ */
+
+
+/**
+ * Generates a hyperlink
+ *
+ * @param string $uri     The target link location
+ * @param string $text    The link text 
+ * @param string $target  The location where the link should 
+ *                        be opened (OPTIONAL; default not used)
+ * @param string $onclick The onClick JavaScript handler (OPTIONAL; 
+ *                        default not used)
+ * @param string $class   The CSS class name (OPTIONAL; default
+ *                        not used)
+ * @param string $id      The ID name (OPTIONAL; default not used)
+ *
+ * @return string The desired hyperlink tag.
+ *
+ * @since 1.5.2
+ *
  */
+function create_hyperlink($uri, $text, $target='', $onclick='', $class='', $id='') {
+
+    global $oTemplate;
+
+    $oTemplate->assign('uri', $uri);
+    $oTemplate->assign('text', $text);
+    $oTemplate->assign('target', $target);
+    $oTemplate->assign('onclick', $onclick);
+    $oTemplate->assign('class', $class);
+    $oTemplate->assign('id', $id);
+
+    return $oTemplate->fetch('hyperlink.tpl');
+
+}
+
 
 /**
- * Generate html tags
+ * Generates an image tag
+ *
+ * @param string $src     The image source path
+ * @param string $alt     Alternate link text (OPTIONAL; default 
+ *                        not used)
+ * @param string $width   The width the image should be shown in 
+ *                        (OPTIONAL; default not used)
+ * @param string $height  The height the image should be shown in 
+ *                        (OPTIONAL; default not used)
+ * @param string $border  The image's border attribute value 
+ *                        (OPTIONAL; default not used)
+ * @param string $class   The CSS class name (OPTIONAL; default
+ *                        not used)
+ * @param string $id      The ID name (OPTIONAL; default not used)
+ * @param string $onclick The onClick JavaScript handler (OPTIONAL;
+ *                        default not used)
+ * @param string $title   The image's title attribute value 
+ *                        (OPTIONAL; default not used)
+ * @param string $align   The image's alignment attribute value 
+ *                        (OPTIONAL; default not used)
+ * @param string $hspace  The image's hspace attribute value 
+ *                        (OPTIONAL; default not used)
+ * @param string $vspace  The image's vspace attribute value 
+ *                        (OPTIONAL; default not used)
+ * @param string $text_alternative A text replacement for the entire
+ *                                 image tag, to be used at the 
+ *                                 discretion of the template set,
+ *                                 if for some reason the image tag
+ *                                 cannot or should not be produced
+ *                                 (OPTIONAL; default not used)
+ *
+ * @return string The desired hyperlink tag.
+ *
+ * @since 1.5.2
+ *
+ */
+function create_image($src, $alt='', $width='', $height='', 
+                      $border='', $class='', $id='', $onclick='', 
+                      $title='', $align='', $hspace='', $vspace='',
+                      $text_alternative='') {
+
+    global $oTemplate;
+
+    $oTemplate->assign('src', $src);
+    $oTemplate->assign('alt', $alt);
+    $oTemplate->assign('width', $width);
+    $oTemplate->assign('height', $height);
+    $oTemplate->assign('border', $border);
+    $oTemplate->assign('class', $class);
+    $oTemplate->assign('id', $id);
+    $oTemplate->assign('onclick', $onclick);
+    $oTemplate->assign('title', $title);
+    $oTemplate->assign('align', $align);
+    $oTemplate->assign('hspace', $hspace);
+    $oTemplate->assign('vspace', $vspace);
+    $oTemplate->assign('text_alternative', $text_alternative);
+
+    return $oTemplate->fetch('image.tpl');
+
+}
+
+
+/**
+ * Generates html tags
  *
  * @param string $tag Tag to output
  * @param string $val Value between tags
  * @param string $bgcolor Back color in hexadecimal
  * @param string $xtra Extra options
  * @return string HTML ready for output
+ * @since 1.3.0
  */
 function html_tag( $tag,                // Tag to output
                        $val = '',           // Value between tags
@@ -30,155 +129,132 @@ function html_tag( $tag,                // Tag to output
                        $bgcolor = '',       // Back color
                        $xtra = '' ) {       // Extra options
 
-        GLOBAL $languages, $squirrelmail_language;
-
-        $align = strtolower( $align );
-        $bgc = '';
-        $tag = strtolower( $tag );
+    GLOBAL $languages, $squirrelmail_language;
 
-        if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
-            $dir = $languages[$squirrelmail_language]['DIR'];
-        } else {
-            $dir = 'ltr';
-        }
+    $align = strtolower( $align );
+    $bgc = '';
+    $tag = strtolower( $tag );
 
-        if ( $dir == 'ltr' ) {
-            $rgt = 'right';
-            $lft = 'left';
-        } else {
-            $rgt = 'left';
-            $lft = 'right';
-        }
+    if ( isset( $languages[$squirrelmail_language]['DIR']) ) {
+        $dir = $languages[$squirrelmail_language]['DIR'];
+    } else {
+        $dir = 'ltr';
+    }
 
-        if ( $bgcolor <> '' ) {
-            $bgc = " bgcolor=\"$bgcolor\"";
-        }
+    if ( $dir == 'ltr' ) {
+        $rgt = 'right';
+        $lft = 'left';
+    } else {
+        $rgt = 'left';
+        $lft = 'right';
+    }
 
-        switch ( $align ) {
-            case '':
-                $alg = '';
-                break;
-            case 'right':
-                $alg = " align=\"$rgt\"";
-                break;
-            case 'left':
-                $alg = " align=\"$lft\"";
-                break;
-            default:
-                $alg = " align=\"$align\"";
-                break;
-        }
+    if ( $bgcolor <> '' ) {
+        $bgc = " bgcolor=\"$bgcolor\"";
+    }
 
-        $ret = "<$tag";
+    switch ( $align ) {
+    case '':
+        $alg = '';
+        break;
+    case 'right':
+        $alg = " align=\"$rgt\"";
+        break;
+    case 'left':
+        $alg = " align=\"$lft\"";
+        break;
+    default:
+        $alg = " align=\"$align\"";
+        break;
+    }
 
-        if ( $dir <> 'ltr' ) {
-            $ret .= " dir=\"$dir\"";
-        }
-        $ret .= $bgc . $alg;
+    $ret = "<$tag";
 
-        if ( $xtra <> '' ) {
-            $ret .= " $xtra";
-        }
+    if ( $dir <> 'ltr' ) {
+        $ret .= " dir=\"$dir\"";
+    }
+    $ret .= $bgc . $alg;
 
-        if ( $val <> '' ) {
-            $ret .= ">$val</$tag>\n";
-        } else {
-            $ret .= '>'. "\n";
-        }
+    if ( $xtra <> '' ) {
+        $ret .= " $xtra";
+    }
 
-        return( $ret );
+    if ( $val <> '' ) {
+        $ret .= ">$val</$tag>\n";
+    } else {
+        $ret .= '>'. "\n";
     }
 
-    /* handy function to set url vars */
-    /* especially usefull when $url = $PHP_SELF */
-    function set_url_var($url, $var, $val=0, $link=true) {
-        $k = '';
-        $pat_a = array (
-                       '/.+(\\&'.$var.')=(.*)\\&/AU',   /* in the middle */
-                       '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
-                       '/.+(\\?'.$var.')=(.*)$/AU',     /* at front and only var */
-                       '/.+(\\&'.$var.')=(.*)$/AU'      /* at the end */
-                     );
-        preg_replace('/&amp;/','&',$url);
-        switch (true) {
-            case (preg_match($pat_a[0],$url,$regs)):
-                $k = $regs[1];
-                $v = $regs[2];
-                break;
-            case (preg_match($pat_a[1],$url,$regs)):
-                $k = $regs[1];
-                $v = $regs[2];
-                break;
-            case (preg_match($pat_a[2],$url,$regs)):
-                $k = $regs[1];
-                $v = $regs[2];
-                break;
-            case (preg_match($pat_a[3],$url,$regs)):
-                $k = $regs[1];
-                $v = $regs[2];
-                break;
-            default:
-                if ($val) {
-                    if (strpos($url,'?')) {
-                        $url .= "&$var=$val";
-                    } else {
-                        $url .= "?$var=$val";
-                    }
-                }
-                break;
-        }
+    return( $ret );
+}
+
 
-        if ($k) {
-            if ($val) {
-                $rpl = "$k=$val";
-                if ($link) {
-                    $rpl = preg_replace('/&/','&amp;',$rpl);
-                }
+/**
+ * handy function to set url vars
+ *
+ * especially useful when $url = $PHP_SELF
+ * @param string $url url that must be modified
+ * @param string $var variable name
+ * @param string $val variable value
+ * @param boolean $link controls sanitizing of ampersand in urls (since 1.3.2)
+ * @return string $url modified url
+ * @since 1.3.0
+ */
+function set_url_var($url, $var, $val=0, $link=true) {
+    $k = '';
+    $pat_a = array (
+                    '/.+(\\&'.$var.')=(.*)\\&/AU',   /* in the middle */
+                    '/.+\\?('.$var.')=(.*\\&).+/AU', /* at front, more follow */
+                    '/.+(\\?'.$var.')=(.*)$/AU',     /* at front and only var */
+                    '/.+(\\&'.$var.')=(.*)$/AU'      /* at the end */
+                    );
+    $url = str_replace('&amp;','&',$url);
+
+    // FIXME: why switch is used instead of if () or one preg_match()
+    switch (true) {
+    case (preg_match($pat_a[0],$url,$regs)):
+        $k = $regs[1];
+        $v = $regs[2];
+        break;
+    case (preg_match($pat_a[1],$url,$regs)):
+        $k = $regs[1];
+        $v = $regs[2];
+        break;
+    case (preg_match($pat_a[2],$url,$regs)):
+        $k = $regs[1];
+        $v = $regs[2];
+        break;
+    case (preg_match($pat_a[3],$url,$regs)):
+        $k = $regs[1];
+        $v = $regs[2];
+        break;
+    default:
+        if ($val) {
+            if (strpos($url,'?')) {
+                $url .= "&$var=$val";
             } else {
-                $rpl = '';
-            }
-            if( substr($v,-1)=='&' ) {
-                $rpl .= '&';
+                $url .= "?$var=$val";
             }
-            $pat = "/$k=$v/";
-            $url = preg_replace($pat,$rpl,$url);
         }
-        return $url;
+        break;
     }
 
-    /* Temporary test function to proces template vars with formatting.
-     * I use it for viewing the message_header (view_header.php) with
-     * a sort of template.
-     */
-    function echo_template_var($var, $format_ar = array() ) {
-        $frm_last = count($format_ar) -1;
-
-        if (isset($format_ar[0])) echo $format_ar[0];
-            $i = 1;
-
-        switch (true) {
-            case (is_string($var)):
-                echo $var;
-                break;
-            case (is_array($var)):
-                $frm_a = array_slice($format_ar,1,$frm_last-1);
-                foreach ($var as $a_el) {
-                    if (is_array($a_el)) {
-                        echo_template_var($a_el,$frm_a);
-                    } else {
-                        echo $a_el;
-                        if (isset($format_ar[$i])) {
-                            echo $format_ar[$i];
-                        }
-                        $i++;
-                    }
-                }
-                break;
-            default:
-                break;
+    if ($k) {
+        if ($val) {
+            $rpl = "$k=$val";
+        } else {
+            $rpl = '';
         }
-        if (isset($format_ar[$frm_last]) && $frm_last>$i ) {
-            echo $format_ar[$frm_last];
+        if( substr($v,-1)=='&' ) {
+            $rpl .= '&';
         }
+        $pat = "/$k=$v/";
+        $url = preg_replace($pat,$rpl,$url);
     }
-?>
+    if ($link) {
+        $url = str_replace('&','&amp;',$url);
+    }
+    return $url;
+}
+
+