Fix for ahah add elements (array) in form

  1. <?php
  2. /**
  3.  * Menu-callback PARA JAVASCRIPT PSEUDO-AJAX
  4.  */
  5. function simulador_js()
  6. {
  7.         ######################################################
  8.         $cache = cache_get('form_'.$_POST['form_build_id'], 'cache_form');
  9.         
  10.         $node = (object)$_POST;
  11.         $form = $cache->data;
  12.         ######################################################
  13.        
  14.         // DECLARA QUE É UM ARRAY DE ELEMENTOS
  15.         $new_obj['arr_prd']['#tree']  = TRUE;
  16.         $new_obj['arr_de']['#tree']   = TRUE;
  17.         $new_obj['arr_ate']['#tree']  = TRUE;
  18.         $new_obj['arr_taxa']['#tree'] = TRUE;
  19.        
  20.         $prod   = $_POST['preconf_prod'];
  21.         $de             = $_POST['preconf_de'];
  22.         $ate    = $_POST['preconf_ate'];
  23.         $taxa   = $_POST['preconf_taxa'];
  24.        
  25.         // Validando
  26.         if (empty($prod) || empty($de) || empty($ate) || empty($taxa))
  27.         {
  28.                 echo drupal_to_js(array(
  29.                         'status' => FALSE, 'data' =>
  30.                         '<div class="messages error">
  31.                                Preencha os campos: "Produto", "De", "Ate" e "Taxa"!
  32.                         </div>'
  33.                         )
  34.                 );
  35.                 exit();
  36.         }
  37.        
  38.         // Itens ta tabela
  39.         $itens  = array();
  40.         $img_del        = drupal_get_path('module', 'simulador') . '/del.png';
  41.        
  42.         $post_de  = '';
  43.         $key        = '0';
  44.  
  45.         // Pegando os valores ja existentes no form
  46.         if (isset($node->arr_prd))
  47.         {
  48.                 if (count($node->arr_prd) > 0)
  49.                 {
  50.                         foreach ($node->arr_prd as $key=>$post_prod)
  51.                         {
  52.                                 $post_de        = $node->arr_de[$key];
  53.                                 $post_ate       = $node->arr_ate[$key];
  54.                                 $post_taxa      = $node->arr_taxa[$key];
  55.                                
  56.                                 $itens[] = array(
  57.                                         $post_prod,
  58.                                         $post_de,
  59.                                         $post_ate,
  60.                                         $post_taxa,
  61.                                         drupal_render($form_state['del['.$key.']'] = array(
  62.                                                         '#type'                 => 'image_button',
  63.                                                         '#name'                 => 'del['.$key.']',
  64.                                                         '#id'                   => 'del_'.$key,
  65.                                                         '#submit'               => FALSE,
  66.                                                         '#src'          => $img_del,
  67.                                                         '#attributes'   => array('OnClick'=>'delLn("'.$key.'"); return false'),
  68.                                                 )
  69.                                         )
  70.                                 );
  71.  
  72.                                 $new_obj['arr_prd'][$key] = array(
  73.                                         '#type'                 => 'hidden',
  74.                                         '#name'                 => 'arr_prd['.$key.']',
  75.                                         '#id'           => 'arr_prd_'.$key,
  76.                                         '#value'                => $post_prod,
  77.                                         '#default_value'=> $post_prod,
  78.                                 );     
  79.                        
  80.                                 $new_obj['arr_de'][$key] = array(
  81.                                         '#type'         => 'hidden',
  82.                                         '#name'                 => 'arr_de['.$key.']',
  83.                                         '#id'           => 'arr_de_'.$key,
  84.                                         '#value'                => $post_de,
  85.                                         '#default_value'=> $post_de,
  86.                                 );
  87.                                
  88.                                 $new_obj['arr_ate'][$key] = array(
  89.                                         '#type'         => 'hidden',
  90.                                         '#name'                 => 'arr_ate['.$key.']',
  91.                                         '#id'           => 'arr_ate_'.$key,
  92.                                         '#value'                => $post_ate,
  93.                                         '#default_value'=> $post_ate,
  94.                                 );
  95.                                
  96.                                 $new_obj['arr_taxa'][$key] = array(
  97.                                         '#type'         => 'hidden',
  98.                                         '#name'                 => 'arr_taxa['.$key.']',
  99.                                         '#id'           => 'arr_taxa_'.$key,
  100.                                         '#value'                => $post_taxa,
  101.                                         '#default_value'=> $post_taxa,
  102.                                 );
  103.                         }
  104.                 }
  105.         }
  106.         $key++;
  107.  
  108.         // Adiciona os novos valores
  109.         $itens[] = array(
  110.                 $prod,
  111.                 $de,
  112.                 $ate,
  113.                 $taxa,
  114.                 drupal_render($form_state['del['.$key.']'] = array(
  115.                                 '#type'                 => 'image_button',
  116.                                 '#name'                 => 'del['.$key.']',
  117.                                 '#id'                   => 'del_'.$key,
  118.                                 '#submit'               => FALSE,
  119.                                 '#src'          => $img_del,
  120.                                 '#attributes'   => array('OnClick'=>'delLn("'.$key.'"); return false'),
  121.                         )
  122.                 )
  123.         );
  124.        
  125.         $new_obj['arr_prd'][$key] = array(
  126.                 '#type'                 => 'hidden',
  127.                 '#name'                 => 'arr_prd['.$key.']',
  128.                 '#id'           => 'arr_prd_'.$key,
  129.                 '#value'                => $prod,
  130.                 '#default_value'=> $prod,
  131.         );     
  132.  
  133.         $new_obj['arr_de'][$key] = array(
  134.                 '#type'         => 'hidden',
  135.                 '#name'                 => 'arr_de['.$key.']',
  136.                 '#id'           => 'arr_de_'.$key,
  137.                 '#value'                => $de,
  138.                 '#default_value'=> $de,
  139.         );
  140.        
  141.         $new_obj['arr_ate'][$key] = array(
  142.                 '#type'         => 'hidden',
  143.                 '#name'                 => 'arr_ate['.$key.']',
  144.                 '#id'           => 'arr_ate_'.$key,
  145.                 '#value'                => $ate,
  146.                 '#default_value'=> $ate,
  147.         );
  148.        
  149.         $new_obj['arr_taxa'][$key] = array(
  150.                 '#type'         => 'hidden',
  151.                 '#name'                 => 'arr_taxa['.$key.']',
  152.                 '#id'           => 'arr_taxa_'.$key,
  153.                 '#default_value'=> $taxa,
  154.                 '#value'                => $taxa,
  155.         );
  156.        
  157.         $cache->data += $new_obj;
  158.         cache_set('form_'. $_POST['form_build_id'], $cache->data, 'cache_form', CACHE_PERMANENT);
  159.  
  160.         $header = array(t('Produto'), t('De'), t('Até'), t('Taxa'), t('Del'));
  161.         $output = theme('table', $header, $itens) . drupal_render($new_obj);
  162.        
  163.         // Não usar drupal_json() pois causa problemas em alguns navegadores.
  164.         print drupal_to_js(array('status' => TRUE, 'data' => $output));
  165.         exit;
  166. }
  167. ?>
  168. by Romeu Godoi - romeu.godoi@gmail.com