source: sipes/0.3-modules/ente_planificador_term_fields/ente_planificador_term_fields.module @ 60aa084

stableversion-3.0
Last change on this file since 60aa084 was 2437304, checked in by lhernandez <lhernandez@…>, 8 años ago

se realizo la correción para la importación de la ubicacion

  • Propiedad mode establecida a 100644
File size: 5.8 KB
Línea 
1<?php
2/**
3 * @file
4 * Module file for the Term Fields Ente Planificador module
5 */
6
7/**
8 * Agregando la opcion para la importación de los campos
9 */
10function ente_planificador_term_fields_form_alter(&$form, $form_state, $form_id) {
11  if ($form_id == 'ente_planificador_admin_settings') {
12    // list of terms.
13    $terms = array(
14     '0' => t('Ninguno'),
15     'geo_ve' => t('Entidades de Venezuela'),
16    );
17    $form['ubicacion'] = array(
18      '#type' => 'fieldset',
19      '#title' => t('Terminos de Ubicación'),
20    );
21    $form['ubicacion']['terminos'] = array(
22      '#type' => 'select',
23      '#title' => t('Terminos a Actualizar'),
24      '#options' => $terms,
25      '#description' => t('Seleccione la Lista de terminos a importar.'),
26    );
27    $vocabularies = taxonomy_get_vocabularies();
28    $availables_vocabulary = array();
29    if (count($vocabularies)) {
30      foreach ($vocabularies as $vocabulary) {
31        $availables_vocabulary[$vocabulary->vid] = check_plain($vocabulary->name);
32      }
33    }
34    $vocabulary = taxonomy_vocabulary_load(variable_get('ente_ubicacion_vocabulary', 0));
35    $availables_vocabulary[0] = t('Ninguno');
36    ksort($availables_vocabulary);
37    $form['ubicacion']['vocabulary'] = array(
38      '#type' => 'select',
39      '#title' => t('Vocabulario'),
40      '#options' => $availables_vocabulary,
41     '#description' => t('Seleccione el vocabulario en el que se han de importar los terminos.'),
42    );
43    $form['ubicacion']['submit'] = array(
44      '#type'  => 'submit',
45      '#value' => t('Guardar Terminos'),
46      '#submit' => array('ente_planificador_term_fields_import_form_submit'),
47    );
48  }
49}
50
51/**
52 * Process import country taxonomies into of drupal
53 */
54function ente_planificador_term_fields_import_form_submit($form, &$form_state) {
55  module_load_include('php', 'ente_planificador_term_fields', '/includes/ente_planificador_geo_ve.inc');
56  if (!(empty($form_state['values']['terminos']))) {
57    if ($form_state['values']['terminos'] == 'geo_ve') {
58      $entidades = lista_entidades();
59      $vocabulary = taxonomy_vocabulary_load($form_state['values']['vocabulary']);
60      _batch_ente_planificador_term_fields_states_export($entidades, $vocabulary);
61    }
62  }
63}
64
65/**
66 * Batch add setting batch to import country taxonomies into of drupal
67 */
68function _batch_ente_planificador_term_fields_states_export($entidades, $vocabulary = 0) {
69  module_load_include('php', 'ente_planificador_term_fields', '/includes/ente_planificador_geo_ve.inc');
70  $entidades = lista_entidades(); 
71  if (empty($vocabulary)) {
72    return FALSE;
73  }
74  $batch = array(
75    'title' => t('Importando Entidades...'),
76    'operations' => array(),
77    'init_message' => t('Comenzando a importar los terminos'),
78    'progress_message' => t('Procesando @current Entidades de @total.'),
79    'error_message' => t('Ocurrio un error durante el proceso'),
80    'finished' => '_ente_planificador_term_fields_import_finished',
81  );
82  foreach ($entidades as $id => $title) {
83    $batch['operations'][] = array('_ente_planificador_term_fields_import_bacth', array($id, $vocabulary));
84  }
85  batch_set($batch);
86  batch_process('admin/settings/ente_planificador'); // The path to redirect to when done.
87}
88
89/**
90 * Batch callback term taxonomy into the Drupal
91 */
92function _ente_planificador_term_fields_import_bacth($id_entidad, $vocabulary, &$context) {
93  module_load_include('php', 'ente_planificador_term_fields', '/includes/ente_planificador_geo_ve.inc');
94  $entidad = lista_entidades($id_entidad);
95
96  $term_e = array(
97    'name' => t($entidad),
98    'description' => '',
99    'parent' => array(0),
100    'vid' => $vocabulary->vid,
101    'fields' => array(
102      'codigo_geo_localizacion' =>  array(
103        'value' => $id_entidad,
104      ),
105    ),
106  );
107  taxonomy_save_term($term_e);
108  $municipios = lista_municipios($id_entidad);
109  if (count($municipios)) {
110    // agregando los municipios
111    $countm = 0;
112    foreach ($municipios as $id_e => $value_m) {
113      $term_m = array(
114        'name' => t($value_m[1]),
115        'description' => '',
116        'parent' => array($term_e['tid']),
117        'vid' => $vocabulary->vid,
118        'fields' => array(
119          'codigo_geo_localizacion' =>  array(
120            'value' => $value_m[0]
121          ),
122        ),
123      );
124     taxonomy_save_term($term_m);
125     $countm++;
126     $parroquias = lista_parroquias($value_m[0]);
127     if (count($parroquias)) {
128       $countp = 0;
129       foreach ($parroquias as $id_m => $value_p) {
130         // agregando las parroquias
131         $term_p = array(
132           'name' => t($value_p[1]),
133           'description' => '',
134           'parent' => array($term_m['tid']),
135           'vid' => $vocabulary->vid,
136           'fields' => array(
137             'codigo_geo_localizacion' =>  array(
138               'value' => $value_p[0]
139             ),
140           ),
141         );
142         taxonomy_save_term($term_p);
143         $countp++;
144       }
145      }
146    }
147  }
148  $context['message'] = t('Se Esta agregando los Municipios y Parroquias del Estado: @entidad.', array('@entidad' => $entidad));
149  $context['results'][] = t('La Entidad @name fue agregado @count Municipios y @countp Parroquias.', array('@name' => $entidad, '@count' => $countm, '@countp' => $countp));
150}
151
152/**
153 * Batch 'finished' callback
154 */
155function _ente_planificador_term_fields_import_finished($success, $results, $operations) {
156  if ($success) {
157    // Here we do something meaningful with the results.
158    $message = t('%count Entidades procesadas: !items', array('%count' => count($results), '!items' => theme('item_list', array('items' => $results))));
159  }
160  else {
161    // An error occurred.
162    // $operations contains the operations that remained unprocessed.
163    $error_operation = reset($operations);
164    $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)));
165  }
166  drupal_set_message($message);
167}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.