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