<?php
goweb::loadClass('Zend_View_Helper_HtmlElement');
/**
 * Goweb Sitemanager 5 Framework
 *
 * @version    5.0.0
 * @category   Goweb
 * @package    Goweb_View_Helper
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2009 (c) Goweb (http://www.goweb.pt)
 */

/**
 *
 * @version    5.0.0
 * @category   Goweb
 * @package    Goweb_View_Helper_Img
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2009 (c) Goweb (http://www.goweb.pt)
 */
class Goweb_View_Helper_Img extends Zend_View_Helper_HtmlElement {
    /**
     * Generates an 'img' element.
     *
     * @access public
     *
     * @param mixed $value The source ('src="..."') for the image.
     *
     * @param array $attribs Attributes for the element tag.
     *
     * @return string The element XHTML.
     */
    public function Img($value = null, $attribs = null){
        // Determine if we should use the value or the src attribute
        if (isset($attribs['src'])) {
            $value = $attribs['src'];
            unset($attribs['src']);
        }
        list($width, $height) = @getimagesize(WWW_PATH . $value);
        if (!isset($attribs['width']) && $width) $attribs['width'] = $width;
        if (!isset($attribs['height']) && $height) $attribs['height'] = $height;
        
        $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
        $baseUrl = str_replace('/admin', '', str_replace('/index.php', '', $baseUrl));
        if (strlen($baseUrl) > 0 && strlen($value) > 0){
            if (strpos($value, 'http://') === FALSE && strpos($value, 'https://') === FALSE && strpos($value, $baseUrl) === FALSE){
                $value = $baseUrl . $value;
            }
        }
        $src = ' src="' . $this->view->escape($value) . '"';
        
        if (!isset($attribs['alt'])){
            $attribs['alt'] = basename($value);
        } else {
            $attribs['alt'] = $this->view->escapeTitle(html_entity_decode($attribs['alt'], ENT_NOQUOTES, CHARSET));
        }
        if (!isset($attribs['title'])){
            $attribs['title'] = $attribs['alt'];
        } else {
            $attribs['title'] = $this->view->escapeTitle(html_entity_decode($attribs['title'], ENT_NOQUOTES, CHARSET));
        }
        
        // XHTML or HTML end tag?
        $endTag = ' />';
        if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
            $endTag= '>';
        }

        // build the element
        $xhtml = '<img'
        . $src
        . $this->_htmlAttribs($attribs)
        . $endTag;
        return $xhtml;
    }
}
