source: sipes/0.3-modules/ente_planificador_term_fields/ente_planificador_term_fields.module @ d0d42a3

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

se actualizo la funcion de administracion

  • Propiedad mode establecida a 100644
File size: 5.9 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  $edit = 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  $term_e = $edit;
108  $term_e = taxonomy_save_term($term_e);
109  $municipios = lista_municipios($id_entidad);
110  if (count($municipios)) {
111    // agregando los municipios
112    $countm = 0;
113    foreach ($municipios as $id_e => $value_m) {
114      $edit = array(
115        'name' => t($value_m[1]),
116        'description' => '',
117        'parent' => array($term_e['tid']),
118        'vid' => $vocabulary->vid,
119        'fields' => array(
120          'codigo_geo_localizacion' =>  array(
121            'value' => $value_m[0]
122          ),
123        ),
124      );
125     $term_m = $edit;
126     $term_m  = taxonomy_save_term($term_m);
127     $countm++;
128     $parroquias = lista_parroquias($value_m[0]);
129     if (count($parroquias)) {
130       $countp = 0;
131       foreach ($parroquias as $id_m => $value_p) {
132         // agregando las parroquias
133         $edit = array(
134           'name' => t($value_p[1]),
135           'description' => '',
136           'parent' => array($term_m['tid']),
137           'vid' => $vocabulary->vid,
138           'fields' => array(
139             'codigo_geo_localizacion' =>  array(
140               'value' => $value_p[0]
141             ),
142           ),
143         );
144         $term_p = $edit;
145         $term_p = taxonomy_save_term($term_p);
146         $countp++;
147       }
148      }
149    }
150  }
151  $context['message'] = t('Se Esta agregando los Municipios y Parroquias del Estado: @entidad.', array('@entidad' => $entidad));
152  $context['results'][] = t('La Entidad @name fue agregado @count Municipios y @countp Parroquias.', array('@name' => $entidad, '@count' => $countm, '@countp' => $countp));
153}
154
155/**
156 * Batch 'finished' callback
157 */
158function _ente_planificador_term_fields_import_finished($success, $results, $operations) {
159  if ($success) {
160    // Here we do something meaningful with the results.
161    $message = t('%count Entidades procesadas: !items', array('%count' => count($results), '!items' => theme('item_list', array('items' => $results))));
162  }
163  else {
164    // An error occurred.
165    // $operations contains the operations that remained unprocessed.
166    $error_operation = reset($operations);
167    $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)));
168  }
169  drupal_set_message($message);
170}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.