'hidden', '#value'=>$ref ); return confirm_form( $form, "Voulez vous (ré-)importer les codes postaux des communes ?", 'admin/config/xref', t('This action cannot be undone.')."Afin d'accelerer le processus, il faut désactiver le module Corresponding entity reference", t('Run process'), t('Cancel') ); } else drupal_goto('admin/config/xref'); } function xref_commune_set_code_postal_form_submit($form, &$form_state){ //TODO : le batch est super lent à cause de la conf CER ===> voir si pas possible de faire un update directement en SQL $operations = array(); $operations[] = array('xref_commune_set_code_postal_batch', array('ref'=>$form_state['values']['ref'])); if(!empty($operations)){ $batch = array( 'operations' => $operations, 'finished' => 'xref_commune_set_code_postal_finish', // 'title' => t('Processing Batch'), 'init_message' => t('Batch is starting.'), 'progress_message' => t('Processed @current out of @total.'), 'error_message' => t('XREF Batch has encountered an error.'), 'file' => drupal_get_path('module', 'xref') . '/inc/xref.commune.inc', ); batch_set($batch); //Starting the Batch batch_process('admin/config/xref'); } } function xref_commune_set_code_postal_batch($ref, &$context){ //Initialisation du batch if (!isset($context['sandbox']['progress'])) { if( module_exists('cer')) { module_disable(array('cer')); } //Get list of id $result = db_query( " SELECT nid, title FROM {node} WHERE status=1 AND type = :type ORDER BY nid ", array( ':type'=>$GLOBALS['referentiels'][$ref]['drupal']['type'] )); $data = $result->fetchAll(); $context['sandbox']['max'] = count($data); $context['results']['data'] = array(); $context['results']['data']['list'] = $data; unset($data); //Set the storage field $context['results']['data']['cp_field'] = $GLOBALS['referentiels'][$ref]['services']['codes_postaux']['cp_field']; $context['results']['data']['insee_field'] = $GLOBALS['referentiels'][$ref]['services']['codes_postaux']['insee_field']; //Get codes postaux in CVS file $cp_str = explode("\n", file_get_contents(drupal_get_path('module', 'xref').'/data/laposte_hexasmal.csv')); $cp =array(); foreach($cp_str as $str){ $row = explode(';',trim($str)); if($row[0]!='' && $row[2]!=''){ $cp[$row[0]] = $row[2]; } } $context['results']['data']['cp'] = $cp; unset($cp); $context['sandbox']['progress']=0; } //Process $i=0; $j= $context['sandbox']['progress']; $message = ''; $insee_field = $context['results']['data']['insee_field']; $cp_field = $context['results']['data']['cp_field']; while($i<25 && $context['sandbox']['progress']<$context['sandbox']['max']){ $info = $context['results']['data']['list'][$i+$j]; if($node = node_load($info->nid)){ //Si on a un code INSEE if(isset($node->{$insee_field}) && $node->{$insee_field}['und'][0]['value']!=''){ //On regarde s'il existe dans la table des codes postaux $cd_insee = $node->{$insee_field}['und'][0]['value']; if(isset($context['results']['data']['cp'][$cd_insee])){ $node->{$cp_field}['und'][0]['value'] = $context['results']['data']['cp'][$cd_insee]; node_save($node); } } } $message .= '
  • '.l($info->title.' [nid : '.$info->nid.']', 'node/'.$info->nid).'
  • '; $context['sandbox']['progress']++; $i++; } $context['message'] = '

    Import codes postaux "'.$ref.'"

    ------- attention : module CER désactivé !!!
    '.$context['sandbox']['progress'].' / '.$context['sandbox']['max'].'
    '; //Condition d'arret if ($context['sandbox']['progress'] != $context['sandbox']['max']) { $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; } } //Fonction de fin de process function xref_commune_set_code_postal_finish($success, $results, $operations){ //reactivation du module CER module_enable(array('cer')); if ($success) { // Here we do something meaningful with the results. //$message = t('@count items successfully processed:', array('@count' => count($results['log']))); //$message .= theme('item_list', array('items' => $results['log'])); //drupal_set_message($message); drupal_set_message("xref_commune_set_code_postal_finish"); } else { // An error occurred. // $operations contains the operations that remained unprocessed. $error_operation = reset($operations); $message = t('An error occurred while processing %error_operation with arguments: @arguments', array('%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE))); drupal_set_message($message, 'error'); } }