uso de AHAH y webservices en un formulario

<php>
<?php
// $Id$
/**
*
* simple menu
*
*
*/

function agrovoc_ws_menu() {

$items = array();

$items['agrovoc_ws'] = array(
'title' => 'AGROVOC WS',
'description' => 'Web service',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'drupal_get_form',
'page arguments' => array('agrovoc_ws_form'),
'access callback' => TRUE,
);

$items['agrovoc_ws/terms'] = array(
'title' => 'AGROVOC Data',
'description' => 'Data',
'type' => MENU_CALLBACK,
'page callback' => 'agrovoc_ws_form_get_data',
'access callback' => TRUE,
);

return $items;

}

/**
*
* simple interface for setup search
*
*
*/

function agrovoc_ws_form(&$form_state) {

//agrovoc_ws_get_data_from_ws

$p = array('Los Destellos', 'Cuarteto Continental');
// $q = array('Lizandro Meza', 'Elmer Yaipén');

if(isset($form_state['values']['get_terms'])) {
$q = agrovoc_ws_get_data_from_ws($form_state['storage']);
}

$default = !isset($form_state['values']['get_terms']) ? $p : $q ;

$form = array();
$form['#cache'] = TRUE;
$form['#tree']= TRUE;

$form['language'] = array(
'#title' => t('Language'),
'#type' => 'select',
'#description' => t('Select language'),
'#options' => array('English' => t('English'), 'French' => t('French'), 'Spanish' => t('Spanish')),
'#multiple' => FALSE,
'#weight' => 10,
);

$form['search_string'] = array(
'#title' => t('Keyword'),
'#type' => 'textfield',
'#description' => t('Input words'),
'#size' => 30,
'#weight' => 20,
);

$form['search_mode'] = array(
'#title' => t('Search mode'),
'#description' => t('Select search mode'),
'#type' => 'select',
'#multiple' => FALSE,
'#options' => array('containing' => t('containing'), 'starting with' => t('starting with'), 'exact match' => t('exact match')),
'#weight' => 30,
);

$form['terms'] = array(
'#prefix' => '<div id="agrovoc_terms">',
'#suffix' => '</div>',
'#weight' => 35,
);

$form['terms']['select1'] = array(
'#title' => t('Show results'),
'#description' => t('Items to select'),
'#type' => 'select',
'#options' => $default,
'#weight' => 40,
'#multiple' => TRUE,
);

$form['get_terms'] = array(
'#type' => 'hidden',
'#default_value' => !isset($form_state['values']['get_terms']) ? FALSE : TRUE ,
);

$form['add'] = array(
'#title' => t('Add'),
'#description' => t('Add items'),
'#type' => 'button',
'#weight' => 45,
'#value' => t('Add'),
);

$form['select2'] = array(
'#title' => t('Selected'),
'#description' => t('Selected Items'),
'#type' => 'select',
'#options' => array(),
'#weight' => 50,
'#multiple' => TRUE,
);

$form['remove'] = array(
'#title' => t('Remove'),
'#description' => t('Remove items'),
'#type' => 'button',
'#weight' => 47,
'#value' => t('Remove'),
);

$form['get_data'] = array(
'#type' => 'submit',
'#value' => t('Traer data'),
'#weight' => 32,
'#submit' => array('agrovoc_ws_form_submit_data'),
'#ahah' => array(
'path' => 'agrovoc_ws/terms',
'wrapper' => 'agrovoc_terms',
'method' => 'replace',
'efect' => 'fade',
),
);

$form['ready'] = array(
'#type' => 'submit',
'#value' => t('Listo'),
'#weight' => 70,
'#submit' => array('agrovoc_ws_form_submit'),
);

return $form;

}

/*
function agrovoc_ws_form_validate($form, &$form_state) {

}

*/

/**
* a esta funcion se mandan los valores del formulario cuando se envia. Siempre, siempre
*
*
*/

function agrovoc_ws_form_submit_data ($form, &$form_state) {

unset($form_state['submit_handlers']);

$get_terms = $_GET['q'] == 'agrovoc_ws/terms' ? TRUE : FALSE;

$form_state['values']['get_terms'] = $get_terms;

$form_state['storage']['values'] = $form_state['values'];

#dsm($form_state);

}

function agrovoc_ws_form_submit($form, &$form_state) {

$form_state['storage']['values2'] = $form_state['values'];
$form_state['values']['select2'] = array();

dsm($form_state);

}
/**
*
* send search parameters to agrovoc webservices
*
*
*/

function agrovoc_ws_form_get_data() {

$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);

$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;

drupal_process_form($form_id, $form, $form_state); //procesa el submit

$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);

unset($form['terms']['#prefix'], $form['terms']['#suffix']);
$output = drupal_render($form['terms']['select1']);

// Final rendering callback.
print drupal_json(array('status' => TRUE, 'data' => $output));

}

/**
* este form_state no tiene nada que ver con el verdadero
*
*/

function agrovoc_ws_get_data_from_ws($form_state) {

$client = new SoapClient(null, array('location' => 'http://www.fao.org/webservices/AgrovocWS&#039;, 'uri' => 'AgrovocWS'));

$search_mode = $form_state['values']['search_mode'];

$search_string = $form_state['values']['search_string'];

$params = array('searchString' => $search_string, 'searchmode' =>$search_mode, 'separator' => ',');

$result = $client->__soapCall('simpleSearchByMode2', $params);

return agrovoc_ws_get_list_values($result);

}

/**
* @result element fetched from AgrovocWS
*
*
*/

function agrovoc_ws_get_list_values($result) {

$result = trim($result,'[]');

$full_list = split(',', $result);

$number_items = array();

$number_items['name'] = array_pop($full_list);
$number_items['value'] = array_pop($full_list);

$item_list = array();
#dsm($full_list);

for ($i = 0; $i <= count($full_list)-1; $i+=3) {

$item_list[$i]['id'] = $full_list[$i];
$item_list[$i]['name'] = $full_list[$i+1];
$item_list[$i]['language'] = $full_list[$i+2];

}

foreach($item_list as $term => $values) {

$select_options[$values['id']] = $values['name'];

}

return $select_options;

}

/**
*
* load php object or arrays from buffer
* it's useful for example in load objects to textarea
*
*/

function agrovoc_ws_get_buffer($result) {

ob_start();
print_r(agrovoc_ws_get_list_values($result));
$content = ob_get_contents();
ob_end_clean();

return $content;

}

function agrovoc_ws_init() {

drupal_add_js("
$().ready(function() {
$('#edit-add').click(function() {
return !$('#edit-terms-select1 option:selected').remove().appendTo('#edit-select2');
});
$('#edit-remove').click(function() {
return !$('#edit-select2 option:selected').remove().appendTo('#edit-terms-select1');
});
});","inline");

}

</php>

otra variante más simple!

--------------------------------------------------------------

<php>
<?php
// $Id$
/**
*
* simple menu
*
*
*/

function agrovoc_ws_menu() {

$items = array();

$items['agrovoc_ws'] = array(
'title' => 'AGROVOC WS',
'description' => 'Web service',
'type' => MENU_NORMAL_ITEM,
'page callback' => 'drupal_get_form',
'page arguments' => array('agrovoc_ws_form'),
'access callback' => TRUE,
);

$items['agrovoc_ws/terms'] = array(
'title' => 'AGROVOC Data',
'description' => 'Data',
'type' => MENU_CALLBACK,
'page callback' => 'agrovoc_ws_form_get_data',
'access callback' => TRUE,
);

return $items;

}

/**
*
* simple interface for setup search
*
*
*/

function agrovoc_ws_form(&$form_state) {

dsm($form);

if (isset($form_state['values']['language']) && isset($form_state['values']['search_string']) && isset($form_state['values']['search_mode'])) {

$options = agrovoc_ws_get_data_from_ws($form_state);

}

else {

$options = array();

}

$form = array();
$form['#cache'] = TRUE;

$form['language'] = array(
'#title' => t('Language'),
'#type' => 'select',
'#description' => t('Select language'),
'#options' => array('English' => t('English'), 'French' => t('French'), 'Spanish' => t('Spanish')),
'#multiple' => FALSE,
'#weight' => 10,
);

$form['search_string'] = array(
'#title' => t('Keyword'),
'#type' => 'textfield',
'#description' => t('Input words'),
'#size' => 30,
'#weight' => 20,
);

$form['search_mode'] = array(
'#title' => t('Search mode'),
'#description' => t('Select search mode'),
'#type' => 'select',
'#multiple' => FALSE,
'#options' => array('containing' => t('containing'), 'starting with' => t('starting with'), 'exact match' => t('exact match')),
'#weight' => 30,
);

$form['terms'] = array(
'#prefix' => '<div id="agrovoc_terms">',
'#suffix' => '</div>',
'#weight' => 35,
);

$form['terms']['select1'] = array(
'#title' => t('Show results'),
'#description' => t('Items to select'),
'#type' => 'select',
'#options' => $options,
'#weight' => 40,
'#multiple' => TRUE,
);

$form['add'] = array(
'#title' => t('Add'),
'#description' => t('Add items'),
'#type' => 'button',
'#weight' => 45,
'#value' => t('Add'),
);

$form['select2'] = array(
'#title' => t('Selected'),
'#description' => t('Selected Items'),
'#type' => 'select',
'#options' => array(),
'#weight' => 50,
'#multiple' => TRUE,
);

$form['remove'] = array(
'#title' => t('Remove'),
'#description' => t('Remove items'),
'#type' => 'button',
'#weight' => 47,
'#value' => t('Remove'),
);

/**
* ahah callback
* event by default is 'click' if is button
* event by default is 'change' if is select
*/
$form['get_data'] = array(
'#type' => 'submit',
'#value' => t('Traer data'),
'#weight' => 32,
'#ahah' => array(
'path' => 'agrovoc_ws/terms',
'wrapper' => 'agrovoc_terms',
'efect' => 'fade',
),
);

$form['ready'] = array(
'#type' => 'submit',
'#value' => t('Listo'),
'#weight' => 70,
'#submit' => array('agrovoc_ws_form_submit'),
);

return $form;

}

function agrovoc_ws_form_submit($form, &$form_state) {

/*
$form_state['storage']['values2'] = $form_state['values'];
$form_state['values']['select2'] = array();
*/

//dsm($form_state);

}
/**
*
* send search parameters to agrovoc webservices
*
*
*/

function agrovoc_ws_form_get_data() {

$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);

$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
//dsm($form);

unset($form['select1']['#value']); // para volver a cero los valores por defecto de este campo.

drupal_process_form($form_id, $form, $form_state); //procesa el submit
//dsm($form);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);

unset($form['terms']['#prefix'], $form['terms']['#suffix']);
$output = drupal_render($form['terms']['select1']);

// Final rendering callback.
print drupal_json(array('status' => TRUE, 'data' => $output));

}

/**
* este form_state no tiene nada que ver con el verdadero
*
*/

function agrovoc_ws_get_data_from_ws($form_state) {

$client = new SoapClient(null, array('location' => 'http://www.fao.org/webservices/AgrovocWS&#039;, 'uri' => 'AgrovocWS'));

$search_mode = $form_state['values']['search_mode'];

$search_string = $form_state['values']['search_string'];

$params = array('searchString' => $search_string, 'searchmode' =>$search_mode, 'separator' => ',');

$result = $client->__soapCall('simpleSearchByMode2', $params);

return agrovoc_ws_get_list_values($result);

}

/**
* @result element fetched from AgrovocWS
*
*
*/

function agrovoc_ws_get_list_values($result) {

$result = trim($result,'[]');

$full_list = split(',', $result);

$number_items = array();

$number_items['name'] = array_pop($full_list);
$number_items['value'] = array_pop($full_list);

$item_list = array();
#dsm($full_list);

for ($i = 0; $i <= count($full_list)-1; $i+=3) {

$item_list[$i]['id'] = $full_list[$i];
$item_list[$i]['name'] = $full_list[$i+1];
$item_list[$i]['language'] = $full_list[$i+2];

}

foreach($item_list as $term => $values) {

$select_options[$values['id']] = $values['name'];

}

return $select_options;

}

/**
*
* load php object or arrays from buffer
* it's useful for example in load objects to textarea
*
*/

function agrovoc_ws_get_buffer($result) {

ob_start();
print_r(agrovoc_ws_get_list_values($result));
$content = ob_get_contents();
ob_end_clean();

return $content;

}

function agrovoc_ws_init() {

drupal_add_js("
$().ready(function() {
$('#edit-add').click(function() {
return !$('#edit-terms-select1 option:selected').remove().appendTo('#edit-select2');
});
$('#edit-remove').click(function() {
return !$('#edit-select2 option:selected').remove().appendTo('#edit-terms-select1');
});
});","inline");

}

</php>

Etiquetas