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

/**
 *
 * @version    5.0.25
 * @category   Goweb
 * @package    Goweb_Form_Element_FormDynamicOptions
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2010 (c) Goweb (http://www.goweb.pt)
 */
class Goweb_View_Helper_FormDynamicOptions extends Zend_View_Helper_FormText {

    /**
     * Generates a 'dynamic options' element.
     *
     * @access public
     *
     * @param string|array $name If a string, the element name.  If an
     * array, all other parameters are ignored, and the array elements
     * are used in place of added parameters.
     *
     * @param mixed $value The element value.
     *
     * @param array $attribs Attributes for the element tag.
     *
     * @return string The element XHTML.
     */
    public function formDynamicOptions($name, $value = null, $attribs = null){
        $return = '';
        $newLine = '';
        $defaults = isset($attribs['defaults']) ? $attribs['defaults'] : null;
        $subFormType = isset($attribs['subFormType']) ? $attribs['subFormType'] : 'radio';
        $grupos = isset($attribs['grupos']) ? $attribs['grupos'] : null;
        unset($attribs['defaults']);
        unset($attribs['subFormType']);
        unset($attribs['grupos']);
        if (isset($attribs['options']) && is_array($attribs['options']) && count($attribs['options']) > 0){
            // XHTML or HTML end tag?
            $endTag = ' />';
            if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
                $endTag= '>';
            }
            $newLine .= '<tr>';
            if (in_array($subFormType , array('radio'))){
                $newLine .= '<td>';
                $newLine .= $this->formText($name . '[text_newline_' . $name . ']', null, $attribs);
                $newLine .= '</td>';
            }
            if (isset($grupos) && $grupos && is_array($grupos) && count($grupos) > 0){
                $count = 0;
                foreach ($grupos as $grupo){
                    foreach ($attribs['options'] as $option){
                        $newLine .= '<td>';
                        $newLine .= '<input type="' . $subFormType . '" name="' . $name . '[option_newline_' . $name . '_' . $count . ']"';
                        if (in_array($subFormType , array('radio'))){
                            $newLine .= ' value="' . $option . '"'; 
                        }
                        $newLine .= $endTag;
                        $newLine .= '</td>';
                    }
                    reset($attribs['options']);
                    $count ++;
                }
                reset($grupos);
            } else {
                foreach ($attribs['options'] as $option){
                    $newLine .= '<td>';
                    $newLine .= '<input type="' . $subFormType . '" name="' . $name . '[option_newline_' . $name . ']"';
                    if (in_array($subFormType , array('radio'))){
                        $newLine .= ' value="' . $option . '"'; 
                    }
                    $newLine .= $endTag;
                    $newLine .= '</td>';
                }
                reset($attribs['options']);
            }
            $newLine .= '<td>';
            $newLine .= '<div class="remove_line" onclick="$(this.parentNode.parentNode).remove();">';
            $newLine .= translate('Remover linha');
            $newLine .= '</div>';
            $newLine .= '</td>';
            $newLine .= '</tr>';
            $return .= '<div class="add_line" onclick="$(\'table_lines_' . $name . '\').down(\'tbody\').insert(newLine_' . $name . '.replaceAll(\'newline_' . $name . '\', nextLine_' . $name . ')); nextLine_' . $name . '++; if (tabbar) {tabbar.cells(tabbar.getActiveTab()).autoSize();};">' . "\n";
            $return .= translate('Adicionar linha') . "\n";
            $return .= '</div>' . "\n";
            $return .= '<table id="table_lines_' . $name . '">' . "\n";
            $return .= '<tr>' . "\n";
            if (isset($grupos) && $grupos && is_array($grupos) && count($grupos) > 0){
                if (in_array($subFormType , array('radio'))){
                    $return .= '<td>&nbsp;</td>' . "\n";
                }
                foreach ($grupos as $grupo){
                    $return .= '<td colspan="' . count($attribs['options']) . '">';
                    $return .= $grupo;
                    $return .= '</td>';
                }
                reset($grupos);
                if (in_array($subFormType , array('radio'))){
                    $return .= '</tr>' . "\n";
                    $return .= '<tr>' . "\n";
                }
            }
            if (isset($grupos) && $grupos && is_array($grupos) && count($grupos) > 0){
                if (in_array($subFormType , array('radio'))){
                    $return .= '<td>&nbsp;</td>' . "\n";
                
                    foreach ($grupos as $grupo){
                        foreach ($attribs['options'] as $option){
                            $return .= '<td class="dynamic_options">' . $option . '</td>' . "\n";
                        }
                        reset($attribs['options']);
                    }
                }
                reset($grupos);
                
            } else {
                if (in_array($subFormType , array('radio'))){
                    $return .= '<td>&nbsp;</td>' . "\n";
                    foreach ($attribs['options'] as $option){
                        $return .= '<td class="dynamic_options">' . $option . '</td>' . "\n";
                    }
                }
                reset($attribs['options']);
            }
            $return .= '</tr>' . "\n";
            if (!$value && isset($defaults) && $defaults){
                $count = 0;
                $value = array();
                foreach ($defaults as $itemValue){
                    $value['text_' . $count] = $itemValue;
                    if (isset($grupos) && $grupos && is_array($grupos) && count($grupos) > 0){
                        $count2 = 0;
                        foreach ($grupos as $grupo){
                            $value['option_' . $count . '_' . $count2] = '';
                            $count2++;
                        }
                        reset($grupos);
                    }
                    $value['option_' . $count] = '';
                    $count++;
                }
                reset($defaults);
            }
            if ($value && is_array($value)){
                
                $count = 0;
                
                if (isset($grupos) && $grupos && is_array($grupos) && count($grupos) > 0){
                    $max = count($value) / count($grupos);
                } else {
                    $max = count($value) / 2;
                }
                $max = ceil($max);
                foreach ($value as $itemKey => $itemValue){
                    if (in_array($subFormType , array('radio')) && strpos($itemKey, 'option_') === 0){
                        continue;
                    }
                    if ($count >= $max) break;
                    while ((in_array($subFormType , array('radio')) && !isset($value['text_' . $count])) && $count <= $max){
                        $count++;
                    }
                    
                    if (in_array($subFormType , array('radio')) && !isset($value['text_' . $count])){
                        $count++;
                        continue;
                    }
                    
                    $return .= '<tr>' . "\n";
                    if (in_array($subFormType , array('radio'))){
                        $return .= '<td>';
                        $attribs['id'] = $name . '_' . $count;
                        $return .= $this->formText($name . '[text_' . $count . ']', $value['text_' . $count], $attribs);
                        $return .= '</td>';
                    }
                    if (isset($grupos) && $grupos && is_array($grupos) && count($grupos) > 0){
                        $count2 = 0;
                        foreach ($grupos as $grupo){
                            foreach ($attribs['options'] as $option){
                                $return .= '<td>';
                                $return .= '<input type="' . $subFormType . '" name="' . $name . '[option_' . $count . '_' . $count2 . ']"';
                                if (in_array($subFormType , array('radio'))){
                                    $return .= ' value="' . $option . '"';
                                } else if (isset($value['option_' . $count . '_' . $count2]) && $value['option_' . $count . '_' . $count2]){
                                    $return .= ' value="' . $value['option_' . $count . '_' . $count2] . '"';
                                }
                                if (in_array($subFormType , array('radio')) && isset($value['option_' . $count . '_' . $count2]) && $option == $value['option_' . $count . '_' . $count2]){
                                    $return .= ' checked="checked"';
                                }
                                $return .= $endTag;
                                $return .= '</td>';
                            }
                            reset($attribs['options']);
                            $count2++;
                        }
                        reset($grupos);
                    } else {
                        foreach ($attribs['options'] as $option){
                            $return .= '<td>';
                            $return .= '<input type="' . $subFormType . '" name="' . $name . '[option_' . $count . ']"';
                            if (in_array($subFormType , array('radio'))){
                                $return .= ' value="' . $option . '"';
                            } else if (isset($value['option_' . $count]) && $value['option_' . $count]){
                                $return .= ' value="' . $value['option_' . $count] . '"';
                            }
                            if (in_array($subFormType , array('radio')) && isset($value['option_' . $count]) && $option == $value['option_' . $count]){
                                $return .= ' checked="checked"';
                            }
                            $return .= $endTag;
                            $return .= '</td>';
                        }
                        reset($attribs['options']);
                    }
                    $count++;
                    $return .= '<td>';
                    $return .= '<div class="remove_line" onclick="$(this.parentNode.parentNode).remove();">';
                    $return .= translate('Remover linha');
                    $return .= '</div>';
                    $return .= '</td>';
                    $return .= '</tr>' . "\n";
                }
                
                $count++;
            } else {
                $return .= str_replace('newline_' . $name . '', '0', $newLine);
            }

            
            $return .= '</table>' . "\n";
            $return .= '<script type="text/javascript">' . "\n";
            $return .= '    //<![CDATA[' . "\n";
            $next = isset($count) ? --$count : (isset($max) ? $max : 1);
            $return .= 'var nextLine_' . $name . ' = \'' . $next . '\';' . "\n";
            $return .= 'var newLine_' . $name . ' = \'' . $newLine . '\';' . "\n";
            $return .= '    //]]>' . "\n";
            $return .= '</script>' . "\n";
            

        } else {
            throw new Goweb_Exception('Parametro "options" nao existe');
        }
        return $return;
    }
}
