Conjunto 6d56596 en sipes
- Fecha y hora:
- 21/09/2021 14:05:39 (hace 3 años)
- Branches:
- stable
- Children:
- 9ac9f8d
- Parents:
- eb502b4
- Fichero:
-
- 1 editado
Leyenda
- No modificado
- Añadido
- Eliminado
-
0.3-modules/ente_planificador_importar/ente_planificador_importar.module
r546c863 r6d56596 1 1 <?php 2 3 2 /** 4 * Modulo para importar entes y terminos 3 * Modulo para importar entes y terminos 5 4 * Sistema Automatizado para la Planificación Estratégico-Situacional en la Administración Pública Venezolana 6 5 * @file ente_planificador_importar.module … … 12 11 * the Free Software Foundation; either version 2 of the License, or 13 12 * (at your option) any later version. 14 * 13 * 15 14 * This program is distributed in the hope that it will be useful, 16 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 17 * GNU General Public License for more details. 19 * 18 * 20 19 * You should have received a copy of the GNU General Public License 21 20 * along with this program; if not, write to the Free Software … … 59 58 'type' => MENU_CALLBACK, 60 59 ); 61 $items['ente_planificador_importar/ updb_proyectos'] = array(62 'title' => 'Importar Entes Planificadores',60 $items['ente_planificador_importar/politicas'] = array( 61 'title' => 'Importar Politicas Ministeriales', 63 62 'page callback' => 'drupal_get_form', 64 'page arguments' => array(' ente_planificador_importar_updb_proyectos_form'),63 'page arguments' => array('politicas_ministeriales_planificador_importar_form'), 65 64 'access arguments' => array('import entes planificadores'), 66 65 'type' => MENU_CALLBACK, … … 73 72 'type' => MENU_CALLBACK, 74 73 ); 75 $items['ente_planificador_importar/terminos/fields'] = array(76 'title' => 'Importar Campos de Terminos',77 'page callback' => 'drupal_get_form',78 'page arguments' => array('ente_planificador_importar_terminos_fields_form'),79 'access arguments' => array('import term'),80 'type' => MENU_CALLBACK,81 );82 $items['ente_planificador_importar/terminos/relations_fields'] = array(83 'title' => 'Importar Campos de Terminos',84 'page callback' => 'drupal_get_form',85 'page arguments' => array('ente_planificador_importar_terminos_relations_fields_form'),86 'access arguments' => array('import term'),87 'type' => MENU_CALLBACK,88 );89 $items['ente_planificador_importar_terminos_js_util'] = array(90 'title' => 'JS utilities',91 'page callback' => 'ente_planificador_importar_fields_js_util',92 'access arguments' => array('import term'),93 'type' => MENU_CALLBACK,94 );95 $items['ente_planificador_importar_terminos_relations_js_util'] = array(96 'title' => 'JS utilities',97 'page callback' => 'ente_planificador_importar_fields_relations_js_util',98 'access arguments' => array('import term'),99 'type' => MENU_CALLBACK,100 );101 74 $items['ente_planificador_importar/estadoproyecto'] = array( 102 75 'title' => 'Modificar estado de poyectos', … … 127 100 $links['entes'] = array( 128 101 'data' => l(t('Importar Entes Planificadores'), 'ente_planificador_importar/entes'), 129 'class' => 'ente-planificador-importar-entes', 102 'class' => 'ente-planificador-importar-entes', 130 103 ); 104 $links['politicas'] = array( 105 'data' => l(t('Importar Politicas Ministeriales'), 'ente_planificador_importar/politicas'), 106 'class' => 'ente-planificador-importar-entes', 107 ); 131 108 } 132 109 if (user_access('import term')) { 133 $links['term 1'] = array(110 $links['term'] = array( 134 111 'data' => l(t('Importar terminos'), 'ente_planificador_importar/terminos'), 135 'class' => 'ente-planificador-importar-term', 136 ); 137 $links['term2'] = array( 138 'data' => l(t('Importar Campos de terminos'), 'ente_planificador_importar/terminos/fields'), 139 'class' => 'ente-planificador-importar-term-fields', 140 ); 141 $links['term_parent'] = array( 142 'data' => l(t('Relacionar taxonomias y guardar valores en un campo del termino'), 'ente_planificador_importar/terminos/relations_fields'), 143 'class' => 'ente-planificador-importar-term-parents-fields', 112 'class' => 'ente-planificador-importar-term', 144 113 ); 145 114 } … … 147 116 $links['estadoproyecto'] = array( 148 117 'data' => l(t('Actualizar estado de proyectos'), 'ente_planificador_importar/estadoproyecto'), 149 'class' => 'ente-planificador-importar-estado-proyecto', 118 'class' => 'ente-planificador-importar-estado-proyecto', 150 119 ); 151 120 } 152 121 } 153 122 123 154 124 /** 155 125 * Implementation of ente_planificador_importar_terminos_form(). 156 * Form to load cvs file in term import 157 */ 158 function ente_planificador_importar_terminos_relations_fields_form(&$form_state) { 159 $form = array(); 160 // If this #attribute is not present, upload will fail on submit 161 162 $form['#prefix'] = "<div id='fields-ahah-taxonomy'>"; 163 $form['#suffix'] = "</div>"; 164 $options[0] = t('_none'); 165 foreach (taxonomy_get_vocabularies() as $key => $taxonomy) { 166 $options[$taxonomy->vid] = $taxonomy->name; 167 } 168 169 $form['vid'] = array( 170 '#type' => 'select', 171 '#title' => t('Taxonomia Origen'), 172 '#options' => $options, 173 '#weight' => 0, 174 '#required' => true, 175 '#default_value' => 0, 176 ); 177 178 $form['vid_taxonomy'] = array( 179 '#type' => 'select', 180 '#title' => t('Taxonomy to relations'), 181 '#options' => $options, 182 '#weight' => 1, 183 '#required' => true, 184 '#default_value' => 0, 185 '#ahah' => array( 186 'path' => 'ente_planificador_importar_terminos_relations_js_util', 187 'wrapper' => 'fields-ahah-taxonomy', 188 'method' => 'replace', 189 'progress' => array('type' => 'bar', 'message' => t('Please wait...')), 190 ), 191 ); 192 $form['submit'] = array( 193 '#type' => 'submit', 194 '#value' => 'Submit', 195 '#weight' => 10, 196 ); 197 return $form; 198 } 199 200 /** 201 * Implementation of ente_planificador_importar_terminos_form_submit(). 202 * Submit cvs file in term import 203 */ 204 function ente_planificador_importar_terminos_relations_fields_form_submit($form, &$form_state) { 205 global $user; 206 $validators = array('file_validate_extensions' => array('csv')); 207 // Check for a new uploaded file. 208 $file = file_save_upload('file_import_terms', $validators); 209 if (isset($file)) { 210 // File upload was attempted. 211 if ($file) { 212 // Put the temporary file in form_values so we can save it on submit. 213 $csv = $file->filepath; 214 $file = fopen($csv, 'r'); 215 $time = time(); 216 $i = 1; 217 $datas = array(); 218 $batch = array( 219 'title' => t('Importing terminos ...', array('@format' => $format)), 220 'operations' => array(), 221 'init_message' => t('Commencing'), 222 'progress_message' => t('Processed @current out of @total.'), 223 'error_message' => t('An error occurred during processing'), 224 'finished' => '_batch_ente_planificador_importar_term_finished', 225 ); 226 $ids = array( 227 'field1' => t('Campo 1'), 228 'field2' => t('Campo 2'), 229 'field3' => t('Campo 3'), 230 ); 231 $terms = ente_planificador_importar_terminos_relations_fields_name($form_state['values']['vid']); 232 $term_load = array(); 233 while (($data = fgetcsv($file)) !== FALSE) { 234 if (!($form_state['values']['encabezado'] && $i == 1)) { 235 $taxonomy_relations = taxonomy_vocabulary_load($form_state['values']['vid']); 236 $taxonomy = taxonomy_vocabulary_load($form_state['values']['vid_taxonomy']); 237 $name_field = trim(check_plain($data[$form_state['values']['nombre'] - 1])); 238 $name_field2 = trim(check_plain($data[$form_state['values']['nombre2'] - 1])); 239 240 if ($terms[$name_field]->tid) { 241 $term = $terms[$name_field]; 242 } 243 else if ($terms["$name_field."]->tid) { 244 $term = $terms["$name_field."]; 245 } 246 else if ($terms[$name_field2]->tid) { 247 $term = $terms[$name_field2]; 248 } 249 else if ($terms["$name_field2."]->tid) { 250 $term = $terms["$name_field2."]; 251 } 252 253 if ($term->tid) { 254 $term_load = array( 255 'vid' => $taxonomy->vid, // Voacabulary ID 256 'name' => $term->name, // Term Name 257 ); 258 taxonomy_save_term($term_load); 259 $term_load['fields'][$form_state['values']['relation']] = array( 260 'value' => $term->tid, 261 ); 262 } 263 $j = 0; 264 foreach ($ids as $id => $name) { 265 if ($form_state['values'][$id] && $data[$form_state['values'][$id] - 1]) { 266 $values = array_values($form_state['values']['fields']); 267 $term_load['fields']["$values[$j]"] = array( 268 'value' => check_plain($data[$form_state['values'][$id] - 1]), 269 ); 270 } 271 $j++; 272 } 273 $batch['operations'][] = array('_batch_ente_planificador_importar_term_relations_fields', array($term_load)); 274 } 275 $i++; 276 } 277 batch_set($batch); 278 batch_process('ente_planificador_importar/terminos/relations_fields'); 279 } 280 else { 281 // File upload failed. 282 form_set_error('file_import_terms', t('The term file could not be uploaded.')); 283 } 284 } 285 } 286 287 function ente_planificador_importar_terminos_relations_fields_name($vid) { 288 $result = db_query(db_rewrite_sql('SELECT t.tid, t.*, parent FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d ORDER BY weight, name', 't', 'tid'), $vid); 289 while ($term = db_fetch_object($result)) { 290 $terms[trim($term->name)] = $term; 291 } 292 return $terms; 293 } 294 295 /** 296 * Genera las repuestas ajax 297 */ 298 function ente_planificador_importar_fields_relations_js_util() { 299 $form_state = array('submitted' => FALSE); 300 $form_build_id = $_POST['form_build_id']; 301 // Add the new element to the stored form. Without adding the element to the 302 // form, Drupal is not aware of this new elements existence and will not 303 // process it. We retreive the cached form, add the element, and resave. 304 $form = form_get_cache($form_build_id, $form_state); 305 306 $args = $form['#parameters']; 307 $form_id = array_shift($args); 308 // We will run some of the submit handlers so we need to disable redirecting. 309 $form['#redirect'] = FALSE; 310 // We need to process the form, prepare for that by setting a few internals 311 // variables. 312 $form['#post'] = $_POST; 313 $form['#programmed'] = FALSE; 314 $form_state['post'] = $_POST; 315 316 $values = term_fields_get_fields('fields'); 317 318 if (isset($_POST['vid_taxonomy'])) { 319 $values = term_fields_get_fields('fields'); 320 $i = 1; 321 $k = 1; 322 $fields[0] = t("_none"); 323 $fields_op[0] = t("_none"); 324 foreach ($values as $value) { 325 foreach ($value->vids as $key => $vid) { 326 $taxonomy = taxonomy_vocabulary_load($vid['vid']); 327 if ($value->type == 'numeric' && $taxonomy->vid == $_POST['vid_taxonomy']) { 328 $fields_op["$value->fid"] = t('@title (Valor del Campo n@campo)', array('@title' => $value->title, '@campo' => $k)); 329 $k++; 330 } 331 else if ($taxonomy->vid == $_POST['vid_taxonomy']) { 332 $fields["$value->fid"] = t('@title (Valor del Campo n@campo)', array('@title' => $value->title, '@campo' => $i)); 333 $i++; 334 } 335 } 336 } 337 $form['relation'] = array( 338 '#type' => 'select', 339 '#title' => t('Campos Numerico a relacionar'), 340 '#options' => $fields_op, 341 '#weight' => 2, 342 '#default_value' => $fields[0], 343 ); 344 $form['fields'] = array( 345 '#type' => 'checkboxes', 346 '#title' => t('Campos a recolectar'), 347 '#options' => $fields, 348 '#weight' => 2, 349 '#default_value' => $fields[0], 350 ); 351 if (count($fields)) { 352 $form['#attributes']['enctype'] = 'multipart/form-data'; 353 $form['file_import_terms'] = array( 354 '#title' => t('Archivo (Terminos a importar)'), 355 '#type' => 'file', 356 '#description' => t('Archivo cvs con el contenido de los terminos a importar'), 357 '#weight' => 3, 358 ); 359 $options = array(); 360 for ($i = 0;$i < 11; $i++) { 361 if (!$i) { 362 $options[$i] = t('N/A'); 363 } 364 else { 365 $options[$i] = t('Fila') . ' ' . $i; 366 } 367 } 368 $form['fieldset'] = array( 369 '#title' => t('Nombres'), 370 '#type' => 'fieldset', 371 '#weight' => 4, 372 ); 373 $form['fieldset']['nombre'] = array( 374 '#type' => 'select', 375 '#title' => t('Nombre del Termino'), 376 '#options' => $options, 377 '#description' => t("La fila donde se encuentra el valor del Campo."), 378 '#required' => TRUE, 379 '#weight' => 4, 380 ); 381 $form['fieldset']['nombre2'] = array( 382 '#type' => 'select', 383 '#title' => t('Nombre del Termino 2'), 384 '#options' => $options, 385 '#description' => t("La fila donde se encuentra el valor del Campo de no presentarse un valor en el primer campo."), 386 '#required' => TRUE, 387 '#weight' => 4, 388 ); 389 $ids = array( 390 'field1' => t('Campo 1'), 391 'field2' => t('Campo 2'), 392 'field3' => t('Campo 3'), 393 ); 394 $i = 0; 395 foreach ($ids as $id =>$text) { 396 $i++; 397 $form[$id] = array( 398 '#type' => 'select', 399 '#title' => $text, 400 '#options' => $options, 401 '#weight' => 5 + $i, 402 ); 403 } 404 $form['encabezado'] = array( 405 '#title' => 'Primera columna como encabezado', 406 '#type' => 'checkbox', 407 '#return_value' => '1', 408 '#weight' => 9, 409 ); 410 } 411 } 412 form_set_cache($form_build_id, $form, $form_state); 413 $form += array( 414 '#post' => $_POST, 415 '#programmed' => FALSE, 416 ); 417 // Rebuild the form. 418 $form = form_builder($_POST['form_id'], $form, $form_state); 419 // Render the new output. 420 $new_form = array(); 421 $new_form = $form; 422 unset($new_form['#prefix'], $new_form['#suffix']); // Prevent duplicate wrappers. 423 $output = drupal_render($new_form); 424 print drupal_to_js(array('data' => $output, 'status' => true)); 425 exit(); 426 print ''; 427 exit(); 428 } 429 430 431 /** 432 * Implementation of _batch_ente_planificador_importar_term_fields(). 433 * display term saved (used in _ente_planificador_importar_terminos_execute) 434 * Batch run callback 435 */ 436 function _batch_ente_planificador_importar_term_relations_fields($term, &$context) { 437 $parents = taxonomy_get_parents($term['tid']); 438 if ($parents) { 439 $parent = array_shift($parents); 440 $parent = $parent->tid; 441 } 442 else { 443 $parent = 0; 444 } 445 $term['parent'] = $parent; 446 taxonomy_save_term($term); 447 448 //watchdog('example', 'taxonomy term <pre>@filename</pre>.', array('@filename' => print_r($term, 1)), WATCHDOG_ERROR); 449 450 $context['message'] = t('Now processing ente: %submission', array('%submission' => $term['name'])); 451 $context['results'][] = 'term importado: ' . $term['name'] . '</pre>'; 452 453 if ($context['sandbox']['progress'] >= $context['sandbox']['max']) { 454 $context['finished'] = 1; 455 } else { 456 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; 457 } 458 459 } 460 461 462 /** 463 * Update values of field to type content 464 */ 465 function ente_planificador_importar_updb_proyectos_form(&$form_state) { 466 $form = array(); 467 // If this #attribute is not present, upload will fail on submit 468 $form['#attributes']['enctype'] = 'multipart/form-data'; 469 $form['file_import_entes'] = array( 470 '#title' => t('Archivo a importar'), 471 '#type' => 'file', 472 '#description' => t('Archivo cvs con el contenido de los terminos a importar'), 473 '#weight' => -1, 474 ); 475 476 $options = array(); 477 for ($i = 0;$i < 30; $i++) { 478 if (!$i) { 479 $options[$i] = t('N/A'); 480 } 481 else { 482 $options[$i] = t('Fila') . ' ' . $i; 483 } 484 } 485 486 $delimiter_option = array(",",";"); 487 $form['delimiter']= array( 488 '#type' => 'select', 489 '#title' => t('Tipo de Delimitador'), 490 '#options' => $delimiter_option, 491 '#description' => t("Delimitador del archivo csv."), 492 '#required' => TRUE, 493 '#weight' => -1, 494 '#default_value' => ",", 495 ); 496 497 $form['field_name'] = array( 498 '#type' => 'select', 499 '#title' => t('Nombre del campo'), 500 '#options' => $options, 501 '#description' => t("La fila donde se encuentra el nombre del Campo a modificar."), 502 '#required' => TRUE, 503 '#weight' => 0, 504 '#default_value' => 1, 505 ); 506 507 $form['field_nid'] = array( 508 '#type' => 'select', 509 '#title' => t('Nid del nodo'), 510 '#options' => $options, 511 '#description' => t("La fila donde se encuentra el nid del nodo a modificar."), 512 '#required' => TRUE, 513 '#weight' => 1, 514 '#default_value' => 2, 515 ); 516 517 $form['field_vid'] = array( 518 '#type' => 'select', 519 '#title' => t('Vid del campo'), 520 '#options' => $options, 521 '#description' => t("La fila donde se encuentra el vid del nodo a modificar."), 522 '#required' => TRUE, 523 '#weight' => 2, 524 '#default_value' => 3, 525 ); 526 527 $form['type_content'] = array( 528 '#type' => 'select', 529 '#title' => t('tipo de contenido del campo'), 530 '#options' => $options, 531 '#description' => t("seleccione la columna donde se encuentre el tipo de contenido del campo."), 532 '#required' => TRUE, 533 '#weight' => 3, 534 '#default_value' => 4, 535 ); 536 537 $form['field_delta'] = array( 538 '#type' => 'select', 539 '#title' => t('Delta del campo'), 540 '#options' => $options, 541 '#description' => t("si el campo es multiple seleccione la columna donde se encuentre el delta."), 542 '#required' => TRUE, 543 '#weight' => 4, 544 ); 545 546 $form['field_tid'] = array( 547 '#type' => 'select', 548 '#title' => t('tid del campo'), 549 '#options' => $options, 550 '#description' => t("si el campo es multiple seleccione la columna donde se encuentre el tid."), 551 '#required' => TRUE, 552 '#weight' => 5, 553 ); 554 555 $form['fields'] = array( 556 '#type' => 'fieldset', 557 '#title' => t('Campos'), 558 '#weight' => 6, 559 ); 560 for ($i=0; $i<12; $i++) { 561 $form['fields']['field_value_' . $i] = array( 562 '#type' => 'select', 563 '#title' => t('Valor del campo ' . $i), 564 '#options' => $options, 565 '#description' => t("La fila donde se encuentra el valor del nodo a modificar."), 566 '#required' => TRUE, 567 '#weight' => $i, 568 '#default_value' => ($i==0) ? 7 : 0 , 569 ); 570 } 571 572 $form['encabezado'] = array( 573 '#title' => 'Primera columna como encabezado', 574 '#type' => 'checkbox', 575 '#return_value' => '1', 576 '#weight' => 7, 577 '#default_value' => TRUE, 578 ); 579 580 $form['submit'] = array( 581 '#type' => 'submit', 582 '#value' => 'Submit', 583 '#weight' => 35, 584 ); 585 return $form; 586 } 587 588 /** 589 * Implementation of function ente_planificador_importar_updb_proyectos_form(). 590 * Update values of field to type content 591 */ 592 function ente_planificador_importar_updb_proyectos_form_submit($form, &$form_state) { 593 global $user; 594 $validators = array('file_validate_extensions' => array('csv')); 595 // Check for a new uploaded file. 596 $file = file_save_upload('file_import_entes', $validators); 597 if (isset($file)) { 598 include_once('./'. drupal_get_path('module', 'content') .'/content.install'); 599 drupal_load('module', 'content'); 600 // $types = content_types_install(); 601 // if (empty($types)) { 602 // return $ret; 603 // } 604 605 // File upload was attempted. 606 if ($file) { 607 608 // Put the temporary file in form_values so we can save it on submit. 609 $csv = $file->filepath; 610 $file = fopen($csv, 'r'); 611 $time = time(); 612 $i = 1; 613 $batch = array( 614 'title' => t('Update value proyects and acciones ...', array('@format' => $format)), 615 'operations' => array(), 616 'init_message' => t('Commencing'), 617 'progress_message' => t('Processed @current out of @total.'), 618 'error_message' => t('An error occurred during processing'), 619 'finished' => '_batch_ente_planificador_importar_finished', 620 ); 621 622 //read the csv archive 623 $data_all = fread($file, filesize($csv)); 624 $fgetslines = explode("\n",$data_all); 625 $process = count($fgetslines); 626 $process = $form_state['values']['encabezado'] && $i == 1 ? $process-1 : $process ; 627 $encabezado = $form_state['values']['encabezado'] && $i == 1 ? 1 : 0 ; 628 629 for ($k=$encabezado; $k < $process; $k++) { 630 $data_line = explode("\n",$data_all); 631 $data_line_all = $data_line[$k]; 632 $delimet = $form_state['values']['delimiter'] ? ";" : "," ; 633 $data = explode($delimet,$data_line_all); 634 635 $batch['operations'][] = array('_ente_planificador_importar_updb_proyectos_importar', array( $data, $form_state['values'])); 636 } 637 638 batch_set($batch); 639 batch_process('ente_planificador_importar/updb_proyectos'); 640 } 641 else { 642 643 // File upload failed. 644 form_set_error('file_import_entes', t('The entes file could not be uploaded.')); 645 } 646 } 647 } 648 649 /** 650 * Update values of field to type content 651 * Batch run callback 652 */ 653 function _ente_planificador_importar_updb_proyectos_importar( $data, $formstate_values, &$context) { 654 655 $fields_update = trim($data[$formstate_values['field_name']-1]); 656 $field = content_fields($fields_update, $data[$formstate_values['type_content']-1]); 657 $values[] = $field; 658 $index = array(); 659 for ($i=0; $i<12; $i++) { 660 $index[] = $i === 0? 'value' : 'value_' . $i; 661 } 662 663 $rate = 100000; 664 $sql = ''; 665 foreach ($values as $field) { 666 if ($field['multiple']) { 667 $sql.=' and delta = '. $data[$formstate_values['field_delta']-1]; 668 } 669 $db_info = content_database_info($field); 670 if (array_key_exists('tid', $db_info['columns'])) { 671 $sql.= ' and ' . $db_info['columns']['tid']['column'] . ' = ' . $data[$formstate_values['field_tid']-1] ; 672 673 } 674 $table = $db_info['table']; 675 $columns = array(); 676 677 foreach($index as $key => $id) { 678 if (isset($db_info['columns'][$id])) { 679 $value = trim($data[$formstate_values['field_value_' . $key]-1]); 680 $attributes = $db_info['columns'][$id]; 681 $column = $attributes['column']; 682 683 $decimals = ($field['scale']) && is_numeric($field['scale']) ? $field['scale'] : 0; 684 if(!$value==0) { 685 686 //$columns[] = $column . ' = round(' . $value . '/' . $rate . ', ' . 8 . ')'; 687 688 // Divide monto 689 // $value_table = round($value/$rate,8); 690 691 //Directamente sin dividir montos 692 $columns[] = $column .'='.$value; 693 694 } else { 695 $columns[] = $column . ' = 0 '; 696 } 697 } 698 } 699 700 //sql que se va a pasar visto en Entradas recientes del registro 701 // $sql1 =('UPDATE {'. $db_info['table'] . '} AS tab SET '. implode(', ', $columns) . ' FROM {node} AS n WHERE n.nid = tab.nid AND n.vid = tab.vid AND n.vid = ' . $data[$formstate_values['field_vid']-1] . ' AND n.nid =' . $data[$formstate_values['field_nid']-1] . $sql ); 702 // watchdog('Test Sipes', '$decimals @filename.', array('@filename' => print_r($sql1,1)), WATCHDOG_ERROR); 703 //endsql que se va a pasar visto en Entradas recientes del registro 704 705 update_sql('UPDATE {'. $db_info['table'] . '} AS tab SET '. implode(', ', $columns) . ' FROM {node} AS n WHERE n.nid = tab.nid AND n.vid = tab.vid AND n.vid = ' . $data[$formstate_values['field_vid']-1] . ' AND n.nid =' . $data[$formstate_values['field_nid']-1] . $sql ); 706 } 707 708 $context['message'] = t('Now processing nid of node: %submission', array('%submission' => trim($data[$formstate_values['field_nid']-1]))); 709 $context['results'][] = 'Node update: ' . trim($data[$formstate_values['field_nid']-1]) . '</pre>'; 710 711 if ($context['sandbox']['progress'] >= $context['sandbox']['max']) { 712 $context['finished'] = 1; 713 } else { 714 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; 715 } 716 } 717 718 719 720 /** 721 * Implementation of ente_planificador_importar_terminos_form(). 722 * Form to load cvs file in term import 723 */ 724 function ente_planificador_importar_terminos_fields_form(&$form_state) { 725 $form = array(); 726 // If this #attribute is not present, upload will fail on submit 727 728 $form['#prefix'] = "<div id='fields-ahah-taxonomy'>"; 729 $form['#suffix'] = "</div>"; 730 $options[0] = t('_none'); 731 foreach (taxonomy_get_vocabularies() as $key => $taxonomy) { 732 $options[$taxonomy->vid] = $taxonomy->name; 733 } 734 735 $form['vid'] = array( 736 '#type' => 'select', 737 '#title' => t('Taxonomy'), 738 '#options' => $options, 739 '#weight' => 0, 740 '#required' => true, 741 '#default_value' => 0, 742 '#ahah' => array( 743 'path' => 'ente_planificador_importar_terminos_js_util', 744 'wrapper' => 'fields-ahah-taxonomy', 745 'method' => 'replace', 746 'progress' => array('type' => 'bar', 'message' => t('Please wait...')), 747 ), 748 ); 749 750 $form['submit'] = array( 751 '#type' => 'submit', 752 '#value' => 'Submit', 753 '#weight' => 10, 754 ); 755 return $form; 756 } 757 758 /** 759 * Implementation of ente_planificador_importar_terminos_form_submit(). 760 * Submit cvs file in term import 761 */ 762 function ente_planificador_importar_terminos_fields_form_submit($form, &$form_state) { 763 global $user; 764 $validators = array('file_validate_extensions' => array('csv')); 765 // Check for a new uploaded file. 766 $file = file_save_upload('file_import_terms', $validators); 767 if (isset($file)) { 768 // File upload was attempted. 769 if ($file) { 770 // Put the temporary file in form_values so we can save it on submit. 771 $csv = $file->filepath; 772 $file = fopen($csv, 'r'); 773 $time = time(); 774 $i = 1; 775 $datas = array(); 776 $batch = array( 777 'title' => t('Importing terminos ...', array('@format' => $format)), 778 'operations' => array(), 779 'init_message' => t('Commencing'), 780 'progress_message' => t('Processed @current out of @total.'), 781 'error_message' => t('An error occurred during processing'), 782 'finished' => '_batch_ente_planificador_importar_term_finished', 783 ); 784 $ids = array( 785 'field1' => t('Campo 1'), 786 'field2' => t('Campo 2'), 787 'field3' => t('Campo 3'), 788 ); 789 790 while (($data = fgetcsv($file)) !== FALSE) { 791 $taxonomy = taxonomy_vocabulary_load($form_state['values']['vid']); 792 $terms = taxonomy_get_tree($taxonomy->vid); 793 $name_field = check_plain($data[$form_state['values']['nombre'] - 1]); 794 foreach ($terms as $term) { 795 if (preg_match("/\b$name_field/i", trim($term->name))) { //not found 796 $term_load = taxonomy_get_term($term->tid); 797 $term_load = (array) $term_load; 798 $j = 0; 799 foreach ($ids as $id => $name) { 800 if ($form_state['values'][$id] && $data[$form_state['values'][$id] - 1]) { 801 $values = array_values($form_state['values']['fields']); 802 $term_load['fields']["$values[$j]"] = array( 803 'value' => check_plain($data[$form_state['values'][$id] - 1]), 804 ); 805 } 806 $j++; 807 } 808 if (!($form_state['values']['encabezado'] && $i == 1)) { 809 $batch['operations'][] = array('_batch_ente_planificador_importar_term_fields', array($term_load)); 810 } 811 } 812 } 813 $i++; 814 } 815 batch_set($batch); 816 batch_process('ente_planificador_importar/terminos/fields'); 817 } 818 else { 819 // File upload failed. 820 form_set_error('file_import_terms', t('The term file could not be uploaded.')); 821 } 822 } 823 } 824 825 /** 826 * Implementation of _batch_ente_planificador_importar_term_fields(). 827 * display term saved (used in _ente_planificador_importar_terminos_execute) 828 * Batch run callback 829 */ 830 function _batch_ente_planificador_importar_term_fields($term, &$context) { 831 $parents = taxonomy_get_parents($term['tid']); 832 if ($parents) { 833 $parent = array_shift($parents); 834 $parent = $parent->tid; 835 } 836 else { 837 $parent = 0; 838 } 839 $term['parent'] = $parent; 840 taxonomy_save_term($term); 841 842 //watchdog('example', 'taxonomy term <pre>@filename</pre>.', array('@filename' => print_r($term, 1)), WATCHDOG_ERROR); 843 844 $context['message'] = t('Now processing ente: %submission', array('%submission' => $term['name'])); 845 $context['results'][] = 'term importado: ' . $term['name'] . '</pre>'; 846 847 if ($context['sandbox']['progress'] >= $context['sandbox']['max']) { 848 $context['finished'] = 1; 849 } else { 850 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; 851 } 852 } 853 854 /** 855 * Genera las repuestas ajax 856 */ 857 function ente_planificador_importar_fields_js_util() { 858 $form_state = array('submitted' => FALSE); 859 $form_build_id = $_POST['form_build_id']; 860 // Add the new element to the stored form. Without adding the element to the 861 // form, Drupal is not aware of this new elements existence and will not 862 // process it. We retreive the cached form, add the element, and resave. 863 $form = form_get_cache($form_build_id, $form_state); 864 865 $args = $form['#parameters']; 866 $form_id = array_shift($args); 867 // We will run some of the submit handlers so we need to disable redirecting. 868 $form['#redirect'] = FALSE; 869 // We need to process the form, prepare for that by setting a few internals 870 // variables. 871 $form['#post'] = $_POST; 872 $form['#programmed'] = FALSE; 873 $form_state['post'] = $_POST; 874 875 $values = term_fields_get_fields('fields'); 876 877 if (isset($_POST['vid'])) { 878 $values = term_fields_get_fields('fields'); 879 $i = 0; 880 foreach ($values as $value) { 881 foreach ($value->vids as $key => $vid) { 882 $taxonomy = taxonomy_vocabulary_load($vid['vid']); 883 if ($taxonomy->vid == $_POST['vid']) { 884 $fields["$value->fid"] = t('@title (Valor del Campo n@campo)', array('@title' => $value->title, '@campo' => $i)); 885 } 886 $i++; 887 } 888 } 889 $form['fields'] = array( 890 '#type' => 'checkboxes', 891 '#title' => t('Campos'), 892 '#options' => $fields, 893 '#weight' => 1, 894 '#required' => true, 895 ); 896 897 if (count($fields)) { 898 $form['#attributes']['enctype'] = 'multipart/form-data'; 899 $form['file_import_terms'] = array( 900 '#title' => t('Archivo (Terminos a importar)'), 901 '#type' => 'file', 902 '#description' => t('Archivo cvs con el contenido de los terminos a importar'), 903 '#weight' => 2, 904 ); 905 $options = array(); 906 for ($i = 0;$i < 11; $i++) { 907 if (!$i) { 908 $options[$i] = t('N/A'); 909 } 910 else { 911 $options[$i] = t('Fila') . ' ' . $i; 912 } 913 } 914 $form['nombre'] = array( 915 '#type' => 'select', 916 '#title' => t('Nombre del Termino'), 917 '#options' => $options, 918 '#description' => t("La fila donde se encuentra el valor del Campo."), 919 '#required' => TRUE, 920 '#weight' => 3, 921 ); 922 $ids = array( 923 'field1' => t('Campo 1'), 924 'field2' => t('Campo 2'), 925 'field3' => t('Campo 3'), 926 ); 927 $i = 0; 928 foreach ($ids as $id =>$text) { 929 $i++; 930 $form[$id] = array( 931 '#type' => 'select', 932 '#title' => $text, 933 '#options' => $options, 934 '#weight' => 4 + $i, 935 ); 936 } 937 $form['encabezado'] = array( 938 '#title' => 'Primera columna como encabezado', 939 '#type' => 'checkbox', 940 '#return_value' => '1', 941 '#weight' => 9, 942 ); 943 } 944 } 945 form_set_cache($form_build_id, $form, $form_state); 946 $form += array( 947 '#post' => $_POST, 948 '#programmed' => FALSE, 949 ); 950 // Rebuild the form. 951 $form = form_builder($_POST['form_id'], $form, $form_state); 952 // Render the new output. 953 $new_form = array(); 954 $new_form = $form; 955 unset($new_form['#prefix'], $new_form['#suffix']); // Prevent duplicate wrappers. 956 $output = drupal_render($new_form); 957 print drupal_to_js(array('data' => $output, 'status' => true)); 958 exit(); 959 print ''; 960 exit(); 961 } 962 963 /** 964 * Implementation of ente_planificador_importar_terminos_form(). 965 * Form to load cvs file in term import 126 * Form to load cvs file in term import 966 127 */ 967 128 function ente_planificador_importar_terminos_form() { … … 986 147 ); 987 148 $options = array(); 988 for($i = 0;$i < 1 5; $i++) {149 for($i = 0;$i < 11; $i++) { 989 150 if (!$i) { 990 151 $options[$i] = t('N/A'); … … 994 155 } 995 156 } 996 for ($i=0; $i<=6; $i++) { 997 $form['fieldset_' . $i] = array( 998 '#type' => 'fieldset', 999 '#title' => t('Grupo ') . $i, 1000 ); 1001 $form['fieldset_' . $i]['search_parent_' . $i] = array( 1002 '#type' => 'select', 1003 '#title' => t('Parent ') . $i, 1004 '#options' => $options, 1005 '#description' => t("La fila donde se encuentra el parent ") . $i, 1006 '#required' => TRUE, 1007 ); 1008 $form['fieldset_' . $i]['nombre_' . $i] = array( 1009 '#type' => 'select', 1010 '#title' => t('Nombre ') . $i, 1011 '#options' => $options, 1012 '#description' => t("La fila donde se encuentra el nombre del termino ") . $i, 1013 '#required' => TRUE, 1014 ); 1015 $form['fieldset_' . $i]['description_' . $i] = array( 1016 '#type' => 'select', 1017 '#title' => t('Description ') . $i, 1018 '#options' => $options, 1019 '#description' => t("La fila donde se encuentra el description del termino ") . $i, 1020 '#required' => TRUE, 1021 ); 1022 } 1023 /* 157 $form['nombre'] = array( 158 '#type' => 'select', 159 '#title' => t('Nombre'), 160 '#options' => $options, 161 '#description' => t("La fila donde se encuentra el nombre del termino."), 162 '#required' => TRUE, 163 ); 1024 164 $ids = array( 1025 165 'code' => t('Código'), … … 1031 171 '#options' => $options, 1032 172 ); 1033 } */173 } 1034 174 $form['encabezado'] = array( 1035 175 '#title' => 'Primera columna como encabezado', … … 1046 186 /** 1047 187 * Implementation of ente_planificador_importar_terminos_form_submit(). 1048 * Submit cvs file in term import 188 * Submit cvs file in term import 1049 189 */ 1050 190 function ente_planificador_importar_terminos_form_submit($form, &$form_state) { … … 1052 192 $validators = array('file_validate_extensions' => array('csv')); 1053 193 // Check for a new uploaded file. 1054 $file = file_save_upload('file_import_entes'); 1055 194 $file = file_save_upload('file_import_entes', $validators); 1056 195 if (isset($file)) { 1057 196 // File upload was attempted. … … 1072 211 ); 1073 212 while (($data = fgetcsv($file)) !== FALSE) { 1074 for ($j=0; $j<=6; $j++) { 1075 if ($form_state['values']['nombre_' . $j] && $data[$form_state['values']['nombre_' . $j] - 1]) { 1076 $term = array( 1077 'vid' => $form_state['values']['vid'], // Voacabulary ID 1078 'name' => check_plain(trim($data[$form_state['values']['nombre_' . $j] - 1])), // Term Name 1079 'description' => check_plain($data[$form_state['values']['description_' . $j] - 1]), // Term description 1080 'search_parent' => FALSE, 1081 /*'fields' => array( 1082 'unidad_codigo1' => array( 1083 'value' => check_plain($data[$form_state['values']['code'] - 1]), 1084 ), 1085 ),*/ 1086 ); 1087 if ($form_state['values']['search_parent_' . $j] && $data[$form_state['values']['search_parent_' . $j] - 1]) { 1088 $term['search_parent'] = check_plain($data[$form_state['values']['search_parent_' . $j] - 1]); 1089 $term['term_parent'] = check_plain($data[$form_state['values']['search_parent_0']]); 1090 } 1091 if (!($form_state['values']['encabezado'] && $i == 1)) { 1092 $batch['operations'][] = array('_batch_ente_planificador_importar_term', array($term)); 1093 } 1094 } 213 $term = array( 214 'vid' => $form_state['values']['vid'], // Voacabulary ID 215 'name' => check_plain($data[$form_state['values']['nombre'] - 1]), // Term Name 216 'fields' => array( 217 'unidad_codigo1' => array( 218 'value' => check_plain($data[$form_state['values']['code'] - 1]), 219 ), 220 ), 221 ); 222 if (!($form_state['values']['encabezado'] && $i == 1)) { 223 $batch['operations'][] = array('_batch_ente_planificador_importar_term', array($term)); 1095 224 } 1096 225 $i++; … … 1109 238 /** 1110 239 * Implementation of _ente_planificador_importar_terminos_execute(). 1111 * Batch 'execute' callback 240 * Batch 'execute' callback 1112 241 */ 1113 242 function _ente_planificador_importar_terminos_execute($execute_id){ … … 1141 270 /** 1142 271 * Implementation of _batch_ente_planificador_importar_term_finished(). 1143 * Batch 'finished' callback 272 * Batch 'finished' callback 1144 273 */ 1145 274 function _batch_ente_planificador_importar_term_finished($success, $results, $operations) { … … 1160 289 * Implementation of _batch_ente_planificador_importar_term(). 1161 290 * display term saved (used in _ente_planificador_importar_terminos_execute) 1162 * Batch run callback 291 * Batch run callback 1163 292 */ 1164 293 function _batch_ente_planificador_importar_term($term, &$context) { 1165 //not create if exist 1166 if ($term['search_parent']) { 1167 $db_result = db_query(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t WHERE LOWER(t.name) = LOWER('%s') AND vid = %d", 't', 'tid'), trim($term['search_parent']), $term['vid']); 1168 $result = array(); 1169 while ($row = db_fetch_array($db_result)) { 1170 if (!empty($row)) { 1171 $parents_all = taxonomy_get_parents_all($row['tid']); 1172 foreach ($parents_all as $parent) { 1173 if ($parent->name == trim($term['search_parent']) && $parents_all[count($parents_all) - 1]->name == trim($term['term_parent'])) { 1174 $parents = array('name' => $parent->name, 'tid_parent' => $row['tid']); 1175 1176 $db_query2 = db_query(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t inner join {term_hierarchy} as h on h.tid=t.tid WHERE LOWER(t.name) = LOWER('%s') AND t.vid = %d AND h.parent = %d", 't', 'tid'), trim($term['name']), $term['vid'], $row['tid']); 1177 $rows_query = db_fetch_array($db_query2); 1178 if (!$rows_query['tid']) { 1179 $term['parent'][] = $parents['tid_parent']; 1180 taxonomy_save_term($term); 1181 } 1182 } 1183 } 1184 } 1185 } 1186 } 1187 else { 1188 $db_result = db_query(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t INNER JOIN {term_hierarchy} as h ON h.tid=t.tid WHERE LOWER(t.name) = LOWER('%s') AND t.vid = %d AND h.parent = %d", 't', 'tid'), trim($term['name']), $term['vid'], 0); 1189 $rows = db_fetch_array($db_result); 1190 1191 if (!taxonomy_get_term($rows['tid'])) { 1192 taxonomy_save_term($term); 1193 } 1194 } 1195 294 taxonomy_save_term($term); 1196 295 $context['message'] = t('Now processing ente: %submission', array('%submission' => $term->name)); 1197 296 $context['results'][] = 'term importado: ' . $term->name . '</pre>'; 297 /* 298 if ($context['sandbox']['progress'] >= $context['sandbox']['max']) { 299 $context['finished'] = 1; 300 } else { 301 $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; 302 } 303 */ 1198 304 } 1199 305 1200 306 /** 1201 307 * Implementation of ente_planificador_importar_form(). 1202 * Form to load cvs file in entes import 308 * Form to load cvs file in entes import 1203 309 */ 1204 310 function ente_planificador_importar_form() { … … 1262 368 ); 1263 369 foreach($fields as $field_id => $field) { 1264 $campos[$field_id] = $field; 370 $campos[$field_id] = $field; 1265 371 $form['campos'][$field_id] = array( 1266 372 '#type' => 'select', … … 1286 392 /** 1287 393 * Implementation of ente_planificador_importar_form_submit(). 1288 * Submit cvs file in entes import 394 * Submit cvs file in entes import 1289 395 */ 1290 396 function ente_planificador_importar_form_submit($form, &$form_state) { … … 1373 479 /** 1374 480 * Implementation of _ente_planificador_importar_execute(). 1375 * Batch 'execute' callback 481 * Batch 'execute' callback 1376 482 */ 1377 483 function _ente_planificador_importar_execute($execute_id){ … … 1404 510 /** 1405 511 * Implementation of _ente_planificador_importar_usuarios(). 1406 * Obtiene la cantidad de usuarios por tipo de ente 512 * Obtiene la cantidad de usuarios por tipo de ente 1407 513 */ 1408 514 function _ente_planificador_importar_usuarios($tipo = 0) { … … 1434 540 * Implementation of _batch_ente_planificador_importar(). 1435 541 * Save entes (used in _ente_planificador_importar_execute) 1436 * Batch run callback 542 * Batch run callback 1437 543 */ 1438 544 function _batch_ente_planificador_importar($ente, &$context) { … … 1477 583 } 1478 584 else { 1479 $acronimo = 'ente_' . $acronimo; 585 $acronimo = 'ente_' . $acronimo; 1480 586 } 1481 587 //se crean los usuarios segun el tipo … … 1507 613 /** 1508 614 * Implementation of _batch_ente_planificador_importar_finished(). 1509 * Batch 'finished' callback 615 * Batch 'finished' callback 1510 616 */ 1511 617 function _batch_ente_planificador_importar_finished($success, $results, $operations) { … … 1557 663 /** 1558 664 * Implementation of ente_planificador_importar_aprobar_proyectos_form(). 1559 * Form to load cvs file and setting in proyectos estate update 665 * Form to load cvs file and setting in proyectos estate update 1560 666 */ 1561 667 function ente_planificador_importar_aprobar_proyectos_form() { … … 1660 766 * Implementation of _batch_proyecto_operativo_cambiar_estado_importar(). 1661 767 * Update state proyectos (used in ente_planificador_importar_aprobar_proyectos_form_submit) 1662 * Batch run callback 768 * Batch run callback 1663 769 */ 1664 770 function _batch_proyecto_operativo_cambiar_estado_importar($proyecto, &$context) { … … 1671 777 if ($node->_workflow != $proyecto['workflow']) { 1672 778 $context['results'][] = t('Actualizado el proyecto @proyecto desde el estado @ant al @prox ', array('@proyecto' => $proyecto['sipes'], '@ant' => $proyecto['workflows'][$node->_workflow], '@prox' => $proyecto['workflows'][$proyecto['workflow']])); 1673 $node->_workflow = $proyecto['workflow'];779 //$node->_workflow = $proyecto['workflow']; 1674 780 $node->revision = 1; 1675 781 $texto = t('Se cambio el proyecto @nombre del estado @estado al @estado_next', array('@nombre' => $node->title, '@estado' => $proyecto['workflows'][$node->_workflow], '@estado_next' => $proyecto['workflows'][$proyecto['workflow']])); 1676 782 $node->log = $texto; 1677 $node->_workflow = $proyecto['workflow']; 783 workflow_execute_transition($node, $proyecto['workflow'], $texto, TRUE); 784 //$node->_workflow = $proyecto['workflow']; 1678 785 node_save($node); 1679 786 } … … 1693 800 /** 1694 801 * Implementation of _batch_ente_planificador_importar_finished(). 1695 * Batch 'finished' callback 802 * Batch 'finished' callback 1696 803 */ 1697 804 function _batch_proyecto_operativo_cambiar_estado_importar_finished($success, $results, $operations) { … … 1708 815 drupal_set_message($message); 1709 816 } 817 818 /** 819 * Implementation of politicas_ministeriales_planificador_importar_form(). 820 * Form to load cvs file in politicas_ministeriales import 821 */ 822 function politicas_ministeriales_planificador_importar_form() { 823 $form = array(); 824 // If this #attribute is not present, upload will fail on submit 825 $form['#attributes']['enctype'] = 'multipart/form-data'; 826 $form['file_import_entes'] = array( 827 '#title' => t('Entes a importar'), 828 '#type' => 'file', 829 '#description' => t('Archivo cvs con el contenido de los entes planificadores a importar'), 830 ); 831 $options = array(); 832 for($i = 0;$i < 30; $i++) { 833 if (!$i) { 834 $options[$i] = t('N/A'); 835 } 836 else { 837 $options[$i] = t('Fila') . ' ' . $i; 838 } 839 } 840 $form['titulo'] = array( 841 '#type' => 'select', 842 '#title' => t('Titulo'), 843 '#options' => $options, 844 '#description' => t("La fila donde se encuentra el nombre de la polÃtica."), 845 '#required' => TRUE, 846 ); 847 $vocabularies = taxonomy_get_vocabularies(); 848 $vocabulary_options = array(); 849 foreach($vocabularies as $vocabulary) { 850 $vocabulary_options[$vocabulary->vid] = $vocabulary->name; 851 } 852 $form['vid'] = array( 853 '#type' => 'select', 854 '#title' => t('Taxonomy'), 855 '#default_value' => !empty($field['vid']) ? $field['vid'] : '', 856 '#options' => $vocabulary_options, 857 ); 858 859 $form['nombre'] = array( 860 '#type' => 'select', 861 '#title' => t('Nombre'), 862 '#options' => $options, 863 '#description' => t("La fila donde se encuentra el nombre del termino."), 864 '#required' => TRUE, 865 ); 866 $node_type = content_types('politicas_ministeriales'); 867 $fields = $node_type['fields']; 868 $campos = array(); 869 if (count($fields)) { 870 $form['campos'] = array( 871 '#type' => 'fieldset', 872 '#title' => t('campos'), 873 ); 874 foreach($fields as $field_id => $field) { 875 $campos[$field_id] = $field; 876 $form['campos'][$field_id] = array( 877 '#type' => 'select', 878 '#title' => $field['widget']['label'], 879 '#options' => $options, 880 '#description' => t("La fila donde se encuentra el campo del ente."), 881 ); 882 } 883 } 884 $form['encabezado'] = array( 885 '#title' => 'Primera columna como encabezado', 886 '#type' => 'checkbox', 887 '#return_value' => '1', 888 ); 889 $form['#ente_fields'] = $campos; 890 $form['submit'] = array( 891 '#type' => 'submit', 892 '#value' => 'Submit', 893 ); 894 return $form; 895 } 896 897 /** 898 * Implementation of politicas_ministeriales_planificador_importar_form_submit(). 899 * Submit cvs file in politicas import 900 */ 901 function politicas_ministeriales_planificador_importar_form_submit($form, &$form_state) { 902 global $user; 903 904 $validators = array('file_validate_extensions' => array('csv')); 905 // Check for a new uploaded file. 906 $file = file_save_upload('file_import_entes', $validators); 907 if (isset($file)) { 908 // File upload was attempted. 909 if ($file) { 910 // Put the temporary file in form_values so we can save it on submit. 911 $csv = $file->filepath; 912 $file = fopen($csv, 'r'); 913 $time = time(); 914 $i = 1; 915 $batch = array( 916 'title' => t('Importing entes planificadores ...', array('@format' => $format)), 917 'operations' => array(), 918 'init_message' => t('Commencing'), 919 'progress_message' => t('Processed @current out of @total.'), 920 'error_message' => t('An error occurred during processing'), 921 'finished' => '_batch_ente_planificador_importar_finished', 922 ); 923 $first_term = get_first_term_pndes($form_state['values']['vid']); 924 $pndes = _get_childrens_term_pndes($first_term, $form_state['values']['vid']); 925 //$pndes = _get_parents_term_pndes($first_term, $form_state['values']['vid']); 926 927 while (($data = fgetcsv($file)) !== FALSE) { 928 $pndes_name = check_plain($data[$form_state['values']['nombre'] - 1]); 929 //$pndes_name = substr($pndes_name, 0, -1); 930 931 $tid = $pndes_name && isset($pndes[$pndes_name])? $pndes[$pndes_name] : 0; 932 933 if ($tid) { 934 $node = new stdClass(); 935 $node->type = 'politicas_ministeriales'; 936 $node->title = 'PolÃtica Ministerial ' . $pndes_name; 937 $node->body = check_plain($data[$form_state['values']['titulo'] - 1]); 938 //se agregan los campos 939 if (count($form['#ente_fields'])) { 940 foreach($form['#ente_fields'] as $field_id => $field) { 941 if ($data[$form_state['values'][$field_id]]) { 942 $tipo = 'value'; 943 if ($field['type'] == 'nodereference') { 944 $tipo = 'nid'; 945 } 946 if ($field['type'] == 'userreference') { 947 $tipo = 'uid'; 948 } 949 if ($field['type'] == 'email') { 950 $tipo = 'email'; 951 } 952 if ($field['type'] == 'link') { 953 $tipo = 'link'; 954 } 955 if ($field['type'] == 'content_taxonomy') { 956 $tipo = 'content_taxonomy'; 957 } 958 $node->{$field_id}[0][$tipo] = check_plain($data[$form_state['values'][$field_id] - 1]); 959 } 960 } 961 } 962 $node->_pndes_term = $tid; 963 $node->status = 1; 964 $node->promote = 0; 965 $node->sticky = 0; 966 $node->uid = $user->uid; 967 $node->revision = 1; 968 $texto = t('Agregado la polÃtca @nombre_ente', array('@nombre_ente' => $node->title)); 969 $node->log = $texto; 970 971 if (!($form_state['values']['encabezado'] && $i == 1)) { 972 $batch['operations'][] = array('_batch_politicas_ministeriales_importar', array($node)); 973 $datas[] = $node; 974 } 975 $i++; 976 } 977 } 978 batch_set($batch); 979 batch_process('ente_planificador_importar/politicas'); 980 } 981 else { 982 983 // File upload failed. 984 form_set_error('file_import_entes', t('The entes file could not be uploaded.')); 985 } 986 } 987 } 988 989 990 /** 991 * Implementation of _batch_politicas_ministeriales_importar(). 992 * Save entes (used in _ente_planificador_importar_execute) 993 * Batch run callback 994 */ 995 function _batch_politicas_ministeriales_importar($node, &$context) { 996 $node->field_pol_min_pndes = _get_linage_term_pndes($node->_pndes_term); 997 unset($node->_pndes_term); 998 node_save($node); 999 $context['message'] = t('Now processing ente: %submission', array('%submission' => $node->title)); 1000 } 1001 1002 /** 1003 * Implementation of get_first_term_pndes(). 1004 * Obtiene el primer elemento del PNDES 1005 */ 1006 function get_first_term_pndes($vid) { 1007 $tid = 0; 1008 $year = variable_get('proyectos_operativos_anho_creacion', 0); 1009 if ($year) { 1010 $db_result = db_query("SELECT * FROM {term_data} as t WHERE t.name <= '%s' AND t.vid = %d ORDER BY name desc", $year, $vid); 1011 $row = db_fetch_array($db_result); 1012 $tid = $row['tid']; 1013 } 1014 return $tid; 1015 } 1016 1017 /** 1018 * Implementation of _get_childrens_term_pndes(). 1019 * Obtiene los hijos de PNDES tomando como indice el nombre del termino 1020 */ 1021 function _get_childrens_term_pndes($parent, $vid) { 1022 // Retrieve the tid. 1023 $term_name = array(); 1024 //$children = taxonomy_get_children($parent, $vid, 'tid'); 1025 $children = taxonomy_get_tree($vid, $parent); 1026 1027 foreach ($children as $tid => $term) { 1028 if ($term->name) { 1029 $term_name[trim($term->name)] = $term->tid; 1030 } 1031 } 1032 return $term_name; 1033 } 1034 1035 /* 1036 * Implementation of _get_parents_term_pndes(). 1037 * Obtiene los padres de PNDES tomando como indice el nombre del termino 1038 */ 1039 function _get_parents_term_pndes($parent, $vid) { 1040 // Retrieve the tid. 1041 $term_name = array(); 1042 $parents = taxonomy_get_parents($parent, $vid); 1043 1044 foreach ($parents as $tid => $term) { 1045 if ($term->name) { 1046 $term_name[trim($term->name)] = $term->tid; 1047 } 1048 } 1049 1050 return $term_name; 1051 } 1052 1053 /** 1054 * Implementation of _get_linage_term_pndes(). 1055 * Obtiene el linage de terminos 1056 */ 1057 function _get_linage_term_pndes($tid = 0) { 1058 $terms = array_reverse(hs_taxonomy_get_parents_all($tid)); 1059 1060 foreach ($terms as $term) { 1061 $lineage[] = array('value' => $term->tid); 1062 } 1063 return $lineage; 1064 }
Nota: Vea TracChangeset
para ayuda en el uso del visor de conjuntos de cambios.