<?php
/**
 * Goweb Sitemanager 5 Framework
 *
 * @version    5.1.3
 * @category   Goweb
 * @package    Goweb_View
 * @subpackage Helper
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2010 (c) Goweb (http://www.goweb.pt)
 */

goweb::loadClass('Zend_View_Helper_FormText');

/**
 * Helper to generate a 'text' elements for time separated by hour and minute.
 *
 * @version    5.1.3
 * @category   Goweb
 * @package    Goweb_View
 * @subpackage Helper
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2010 (c) Goweb (http://www.goweb.pt)
 */
class Goweb_View_Helper_FormTime extends Zend_View_Helper_FormText {
    /**
     * Generates 'text' elements for time separated by hour and minute.
     *
     * @access public
     *
     * @param string $name the element name
     *
     * @param mixed $value The element value.
     *
     * @param array $attribs Attributes for the element tag.
     *
     * @return string The element XHTML.
     */
    public function formTime($name, $value = null, $attribs = null){
    	Goweb::loadJavascript('/admin/media/js/utils.js');
        Goweb::loadJavascript('/media/js/prototype/goweb/time.js');
        $arrayName = '';
        if (strstr($name, ']')){
            preg_match('/\[(.*)]/', $name, $matches);
            if (isset($matches[0])){
                $arrayName = $matches[0];
                $name = str_replace($arrayName, '', $name);
            }
        }
        $attribs['id'] = $this->_normalizeId($name . $arrayName);
        if ($value){ 
            $valueTmp = explode(':', $value) ;
            if (!isset($valueTmp[0])) $valueTmp[0] = '00';
            if (!isset($valueTmp[1])) $valueTmp[1] = '00';
            if (strlen($valueTmp[0]) == 1) $valueTmp[0] = '0' . $valueTmp[0];
            if (strlen($valueTmp[1]) == 1) $valueTmp[1] = '0' . $valueTmp[1];
            if ($valueTmp && strlen($value) != 5){
                $value = $valueTmp[0] . ':' . $valueTmp[1];
            }
        }
        $return = '';#$this->_hidden($name . $arrayName, $value, $attribs);
		$attribs['onkeyup'] = 'gowebUpdateTime("'.$this->_normalizeId($name . $arrayName).'");';
		$attribs['id'] = $this->_normalizeId($name . $arrayName).'_hour';
		$attribs['size'] = 2;
		$attribs['onkeypress'] = 'return restrictCharacters(this, event, digitsOnly);';
		$attribs['onblur'] = 'if (restrictHour("'.$attribs['id'].'")) { gowebUpdateTime("'.$this->_normalizeId($name . $arrayName).'"); }';
		$attribs['maxlength'] = 2;
		$return .= '<span class="hour">';
        $return .= $this->formText($name . $arrayName.'[hour]', $value && isset($valueTmp[0])? $valueTmp[0] : $value, $attribs);
        $return .= '</span>';
        $return .= '<span class="time_separator">';
        $return .= '&nbsp;:&nbsp;';
        $return .= '</span>';
        $attribs['id'] = $this->_normalizeId($name . $arrayName).'_minute';
        $attribs['onblur'] = 'if (restrictMinute("'.$attribs['id'].'")) { gowebUpdateTime("'.$this->_normalizeId($name . $arrayName).'"); }';
        $return .= '<span class="minute">';
        $return .= $this->formText($name . $arrayName.'[minute]', $value && isset($valueTmp[1])? $valueTmp[1] : $value, $attribs);
        $return .= '</span>';
        return $return;
    }
}
