<?php $this->loadCss('/admin/media/css/calendar.css'); ?>
<?php $this->loadCss('/admin/media/css/dtree.css'); ?>
<?php $this->loadJavascript('/admin/media/js/dtree.js'); ?>
<?php if ((isset($combo)) && ($combo == 1)) : ?>
<script type="text/javascript">
/* <![CDATA[ */
function testarConexao(db_type, host, user, pass) {
    var pars = 'db_type=' + db_type.value + '&host=' + host.value + '&user=' + user.value + '&pass=' + pass.value;
    
    var flag = false;
    
    $('image_db_test').src = '<?php echo $baseUrl; ?>/admin/media/images/ajax_loading.gif';
    
    if (host.value == '') {
        alert('<?php echo translateAdmin('Por favor, informe o Host da Base de Dados');?>.');
        host.focus();
        flag = true;
    }
    
    if ((user.value == '') && (!flag)) {
        alert('<?php echo translateAdmin('Por favor, informe o username de acesso a Base de Dados');?>.');
        user.focus();
        flag = true;
    }
    
    if ((pass.value == '') && (!flag)) {
        alert('<?php echo translateAdmin('Por favor, informe a password de acesso a Base de Dados');?>.');
        pass.focus();
        flag = true;
    }
    
    if (flag) {
        $('image_db_test').src = '<?php echo $baseUrl; ?>/admin/media/images/icon_warning.png';
    } else {
        // chamar a funcao php para verificar a conexao com a base de dados
        var url = "<?php echo Goweb::link($baseUrl . '/form/verify_connectivity'); ?>";
        var myAjax = new Ajax.Updater('div_connection_test',
                                      url,
                                      {
                                          method: 'get',
                                          parameters: pars
                                      }
                                      );
        
    }
    
}
/* ]]> */
</script>
<?php endif; ?>

<div id="accoes_bg">
    <div id="titulo_accao"><?php echo $action; ?> <?php echo translateAdmin('de'); ?> <?php echo $section_singular; ?></div>
</div>

<div id="conteudo">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <?php if (isset($tree)) : ?>
    <td valign="top" id="tr_tree">
        <div id="tree">
            <p>P&aacute;ginas | <a href="<?php echo $tree->openAllLink(); ?>"><?php echo translateAdmin('Expandir tudo');?></a> | <a href="<?php echo $tree->closeAllLink(); ?>"><?php echo translateAdmin('Fechar tudo');?></a></p>
            <?php echo $tree->render(); ?>
        </div>
    </td>
    <?php $align = "left"; ?>
    <?php else: ?>
    <?php $align = "center"; ?>
    <?php endif; ?>
    <td align="center" valign="top" id="manutencaodisplay">
        <?php $autoCompletes = method_exists($form, 'getAutoCompletes') ? $form->getAutoCompletes() : array();?>
        <?php 
        $fieldsUpdate = array();
        foreach ($model->getCols() as $colName => $col){
            if ($col->getType() == 'ForeignKey' && $col->getParam('update')){
                if (is_array($col->getParam('update'))){
                    foreach ($col->getParam('update') as $column) $fieldsUpdate[] = $column;
                } else {
                    $fieldsUpdate[] = $col->getParam('update');
                }
            }
        } ?>
        <?php echo $form->start(); ?>
        <div class="acessibilidade">
        	<span>
            <?php if (isset($delete_link)) : ?>
            <input type="button" id="delete_link" name="delete_link" value="<?php echo translateAdmin('Remover');?>" class="form_delete" onclick="if (confirm('<?php echo translateAdmin('Tem a certeza que deseja remover o item seleccionado');?>?')) { location='<?php echo $this->link($delete_link); ?>'; } "/>
            <?php endif; ?>
            <?php echo $form->submit_and_new->input; ?>
			<?php echo $form->submit_and_continue->input; ?>
			<?php echo $form->submit->input; ?>
            </span>
        </div>
        <div id="rte">
        <table border="0" cellpadding="2">
            <?php foreach ($form->getElementsNames() as $element): ?>
                <?php if (strpos($element, '_tafel') === false && $form->{$element}->type != 'submit' && $form->{$element}->type != 'button' && $form->{$element}->type != 'hidden'): ?>
                <tr>
                    <?php if ($form->{$element}->type == 'rte'): ?>
                    <td colspan="2" align="left"><?php if ($form->{$element}->getErrorMessage()): ?>
                        <div class="error_message">
                        <?php echo $form->{$element}->getErrorMessage(); ?>
                        </div>
                        <?php endif; ?>
                        <?php echo $form->{$element}->input; ?>
                    </td>
                    <?php else: ?>
                    <td width="10%" nowrap="nowrap" bgcolor="#f0f0ee"><?php echo $form->{$element}->label; ?>:</td>
                    <td width="90%" bgcolor="#f0f0ee" align="left">
                        <?php if ($form->{$element}->getErrorMessage()): ?>
                        <div class="error_message">
                            <?php echo $form->{$element}->getErrorMessage(); ?>
                        </div>
                        <?php endif; ?>
                        
                        <?php if (is_array($form->{$element}->input)) : ?>
                            <?php foreach ($form->{$element}->input as $label => $input) : ?>
                            <?php echo $input; ?>&nbsp;<?php echo $label; ?><?php echo $this->br(); ?>
                            <?php endforeach; ?>
                        <?php else: ?>
                        <?php if (isset($fieldsUpdate) && in_array($element, $fieldsUpdate)):?>
                        <div id="div_<?php echo $element?>">
                        <?php endif;?>
                        <?php echo $form->{$element}->input; ?>
                        <?php if ($form->elementExists($element . '_tafel')):?>
                        <?php echo $form->{$element. '_tafel'}->input; ?>
                        <?php endif;?>
                        <?php if (isset($fieldsUpdate) && in_array($element, $fieldsUpdate)):?>
                        </div>
                        <?php endif;?>
                        <?php if (isset($autoCompletes[$element])):?>
                        <div id="<?php echo $element ?>_update" class="auto_completer" style="display: none;"></div>
                        <?php endif;?>
                        <?php endif; ?>
                        
                        <?php if ($model->colExists($element) && $model->getCol($element)->getType() == 'Image' && $model->{$element}): ?>
                        <?php
                           $imagem = $model->{$element};
                           $uploadPath = WWW_PATH . $model->getCol($element)->getParam('target') . '/';
                           $sizes = $model->getCol($element)->getParam('sizes');
                           $tamanho = current($sizes);
                           if (!$tamanho) $tamanho = 'small';
                           $sizes = array_reverse($sizes);
                           foreach ($sizes as $size){
                               if (file_exists($uploadPath . $size . '_' . $imagem)) {
                                  $imageSize = getimagesize($uploadPath . $size . '_' . $imagem);
                                  $imagem = $size . '_' . $imagem;
                                  break 1;
                               }
                           }
                        ?>
                        <?php if (isset($imageSize)):?>
                        <?php if(!$model->getColParam($element, 'notnull')):?>
                        <input type="checkbox" name="<?php echo $element; ?>_del" class="checkbox" />
                        <?php echo translateAdmin('Remover');?>
                        <?php endif;?>
                        <div style="padding-top:5px;">
                        <a href="" onclick="windowPopup('<?php echo $baseUrl . $model->getCol($element)->getParam('target'); ?>/<?php echo $imagem; ?>', 'Imagem', '<?php echo $imageSize[0] + 15; ?>', '<?php echo $imageSize[1] + 15; ?>'); return false;"><?php echo $this->img($model->getCol($element)->getParam('target') . '/' . $tamanho . '_' . $model->{$element}, array('border' => 0));?></a> </div>
                        <?php endif;?>
                        <?php elseif ($model->colExists($element) && $model->getCol($element)->getType() == 'File' && $model->{$element} && !$model->getColParam($element, 'notnull')): ?>
                        <div style="padding-top:5px;">
                        <input type="checkbox" name="<?php echo $element; ?>_del"  class="checkbox"/><?php echo translateAdmin('&nbsp;Remover&nbsp;&nbsp;&nbsp;');?><a href="<?php echo $baseUrl . $model->getCol($element)->getParam('target'); ?>/<?php echo $model->{$element}; ?>" target="_blank">[ <?php echo translateAdmin('Ver ficheiro');?> ]</a></div>
                        <?php endif; ?>
                        
                        <?php if ($form->{$element}->help): ?>
                        <?php echo $this->br(); ?><label class="helper"><?php echo $form->{$element}->help; ?></label>
                        <?php endif; ?>
                    </td>
                    <?php endif; ?>
                </tr>
                <?php endif; ?>
                
                <?php if ((isset($combo)) && ($element == 'db_pass')) : ?>
                <tr>
                    <td align="left" bgcolor="#f0f0ee">&nbsp;</td>
                    <td align="left" bgcolor="#f0f0ee">
                    <div id="div_connection_test">
                    <?php echo $this->img('/admin/media/images/icon_warning.png');?>&nbsp;<a href="javascript:testarConexao($('db_type'), $('db_host'), $('db_user'), $('db_pass'));" style="color:#666666;"><?php echo translateAdmin('Testar Conex&atilde;o');?></a>
                    </div>
                    </td>
                </tr>
                <?php endif; ?>
                
            <?php endforeach; ?>
           <?php if (isset($ficha) && $ficha): ?>
           <?php foreach ($ficha->getElementsNames() as $element): ?>
           <tr>
              <td width="10%" nowrap="nowrap" bgcolor="#f0f0ee"><?php echo $ficha->{$element}->label; ?>:</td>
              <td width="90%" bgcolor="#f0f0ee" align="left">
                 <?php if ($ficha->{$element}->getErrorMessage()): ?>
                 <div class="error_message">
                    <?php echo $ficha->{$element}->getErrorMessage(); ?>
                 </div>
                 <?php endif; ?>
                        
                 <?php if (is_array($ficha->{$element}->input)) : ?>
                    <?php foreach ($ficha->{$element}->input as $label => $input) : ?>
                    <?php echo $input; ?>&nbsp;<?php echo $label; ?><br />
                    <?php endforeach; ?>
                 <?php else: ?>
                    <?php echo $ficha->{$element}->input; ?>
                 <?php endif; ?>                        
              </td>
           </tr>
           <?php endforeach; ?>
           <?php endif; ?>
        </table>
        <div align="right"></div>
        <?php echo $this->br(); ?>
        <div class="acessibilidade">
        	<span>
            <?php if (isset($delete_link)) : ?>
            <input type="button" id="delete_link2" name="delete_link" value="<?php echo translateAdmin('Remover');?>" class="form_delete" onclick="if (confirm('<?php echo translateAdmin('Tem a certeza que deseja remover o item seleccionado');?>?')) { location='<?php echo $this->link($delete_link); ?>'; } "/>
            <?php endif; ?>
            <?php echo $form->submit_and_new->input; ?>
			<?php echo $form->submit_and_continue->input; ?>
			<?php echo $form->submit->input; ?>
            </span>
        </div>
        
        </div>
        <?php foreach ($form->getElementsNames() as $element): ?>
            <?php if ($form->{$element}->type == 'hidden'):?>
                <?php echo $form->{$element}->input; ?>
            <?php endif;?>
        <?php endforeach;?>
        <?php echo $form->getJsValidate()?>
        <?php if (isset($autoCompletes) && count($autoCompletes) > 0):?>
        <script type="text/javascript" language="javascript" charset="utf-8">
        // <![CDATA[
        <?php foreach ($autoCompletes as $autoComplete):?>
        <?php echo $autoComplete; ?>
        <?php endforeach;?>
        // ]]>
        </script>
        <?php endif;?>
        <?php echo $form->end(); ?>
        
    </td>
  </tr>
</table>
</div>
