'Importar Entes Planificadores', 'page callback' => 'drupal_get_form', 'page arguments' => array('ente_planificador_importar_form'), 'access arguments' => array('importar entes planificadores'), 'type' => MENU_CALLBACK, ); $items['ente_planificador_importar/execute'] = array( 'title' => 'Importar entes', 'page callback' => '_ente_planificador_importar_execute', 'page arguments' => array(2), 'access arguments' => array('importar entes planificadores'), 'type' => MENU_CALLBACK, ); $items['ente_planificador_importar/terminos'] = array( 'title' => 'Importar Terminos', 'page callback' => 'drupal_get_form', 'page arguments' => array('ente_planificador_importar_terminos_form'), 'access arguments' => array('importar entes planificadores'), 'type' => MENU_CALLBACK, ); $items['ente_planificador_importar/terminos/execute'] = array( 'title' => 'Importar terminos', 'page callback' => '_ente_planificador_importar_terminos_execute', 'page arguments' => array(3), 'access arguments' => array('importar entes planificadores'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implementation of ente_planificador_importar_terminos_form(). * Form to load cvs file in term import */ function ente_planificador_importar_terminos_form() { $form = array(); // If this #attribute is not present, upload will fail on submit $form['#attributes']['enctype'] = 'multipart/form-data'; $form['file_import_entes'] = array( '#title' => t('Terminos a importar'), '#type' => 'file', '#description' => t('Archivo cvs con el contenido de los terminos a importar'), ); $vocabularies = taxonomy_get_vocabularies(); $vocabulary_options = array(); foreach($vocabularies as $vocabulary) { $vocabulary_options[$vocabulary->vid] = $vocabulary->name; } $form['vid'] = array( '#type' => 'select', '#title' => t('Taxonomy'), '#default_value' => !empty($field['vid']) ? $field['vid'] : '', '#options' => $vocabulary_options, ); $options = array(); for($i = 0;$i < 11; $i++) { if (!$i) { $options[$i] = t('N/A'); } else { $options[$i] = t('Fila') . ' ' . $i; } } $form['nombre'] = array( '#type' => 'select', '#title' => t('Nombre'), '#options' => $options, '#description' => t("La fila donde se encuentra el nombre del termino."), '#required' => TRUE, ); $ids = array( 'code' => t('Código'), ); foreach ($ids as $id =>$text) { $form[$id] = array( '#type' => 'select', '#title' => $text, '#options' => $options, ); } $form['encabezado'] = array( '#title' => 'Primera columna como encabezado', '#type' => 'checkbox', '#return_value' => '1', ); $form['submit'] = array( '#type' => 'submit', '#value' => 'Submit', ); return $form; } /** * Implementation of ente_planificador_importar_terminos_form_submit(). * Submit cvs file in term import */ function ente_planificador_importar_terminos_form_submit($form, &$form_state) { global $user; $validators = array('file_validate_extensions' => array('csv')); // Check for a new uploaded file. $file = file_save_upload('file_import_entes', $validators); if (isset($file)) { // File upload was attempted. if ($file) { // Put the temporary file in form_values so we can save it on submit. $csv = $file->filepath; $file = fopen($csv, 'r'); $time = time(); $i = 1; $datas = array(); while (($data = fgetcsv($file)) !== FALSE) { $term = array( 'vid' => $form_state['values']['vid'], // Voacabulary ID 'name' => check_plain($data[$form_state['values']['nombre'] - 1]), // Term Name 'fields' => array( 'unidad_codigo1' => check_plain($data[$form_state['values']['code'] - 1]), ), ); if (!($form_state['values']['encabezado'] && $i == 1)) { $datas[] = $term; } $i++; } $_SESSION['ente_planificador_importar_terminos'][$time] = $datas; $form_state['redirect'] = 'ente_planificador_importar/terminos/execute/' . $time; } else { // File upload failed. form_set_error('file_import_entes', t('The term file could not be uploaded.')); } } } /** * Implementation of _ente_planificador_importar_terminos_execute(). * Batch 'execute' callback */ function _ente_planificador_importar_terminos_execute($execute_id){ if ($execute_id && isset($_SESSION['ente_planificador_importar_terminos'][$execute_id]) && is_array($_SESSION['ente_planificador_importar_terminos'][$execute_id])) { $batch = array( 'title' => t('Importing terminos ...', array('@format' => $format)), 'operations' => array(), 'init_message' => t('Commencing'), 'progress_message' => t('Processed @current out of @total.'), 'error_message' => t('An error occurred during processing'), 'finished' => '_batch_ente_planificador_importar_term_finished', ); $datas = $_SESSION['ente_planificador_importar_terminos'][$execute_id]; unset($_SESSION['ente_planificador_importar_terminos'][$execute_id]); $cantidad = count($datas); $i = 1; foreach($datas as $data) { $batch['operations'][] = array('_batch_ente_planificador_importar_term', array($data, $i, $cantidad)); $i++; } batch_set($batch); batch_process('ente_planificador_importar/terminos'); } else { drupal_set_message(t('Ocurrio un error al intentar importar los entes planificadores')); drupal_goto('ente_planificador_importar/terminos'); } } /** * Implementation of _batch_ente_planificador_importar_term_finished(). * Batch 'finished' callback */ function _batch_ente_planificador_importar_term_finished($success, $results, $operations) { if ($success) { // Here we do something meaningful with the results. $message = t('!count_webform terminos processed', array('!count_webform' => count($results))); } 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); } /** * Implementation of _batch_ente_planificador_importar_term(). * display term saved (used in _ente_planificador_importar_terminos_execute) * Batch run callback */ function _batch_ente_planificador_importar_term($term, $current = 0, $cantidad, &$context) { taxonomy_save_term($term); $context['results'][] = 'term importado: ' . $term->name . '
' . print_r($term, 1) . '
'; $context['sandbox']['progress']++; $context['sandbox']['current'] = $current; //$context['sandbox']['max'] = $cantidad; $context['message'] = t('Now processing ente: %submission', array('%submission' => $term->name)); if ($context['sandbox']['progress'] >= $context['sandbox']['max']) { $context['finished'] = 1; } else { $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; } } /** * Implementation of ente_planificador_importar_form(). * Form to load cvs file in entes import */ function ente_planificador_importar_form() { $form = array(); // If this #attribute is not present, upload will fail on submit $form['#attributes']['enctype'] = 'multipart/form-data'; $form['file_import_entes'] = array( '#title' => t('Entes a importar'), '#type' => 'file', '#description' => t('Archivo cvs con el contenido de los entes planificadores a importar'), ); $options = array(); for($i = 0;$i < 11; $i++) { if (!$i) { $options[$i] = t('N/A'); } else { $options[$i] = t('Fila') . ' ' . $i; } } $form['titulo'] = array( '#type' => 'select', '#title' => t('Titulo'), '#options' => $options, '#description' => t("La fila donde se encuentra el nombre del ente."), '#required' => TRUE, ); $form['level'] = array( '#type' => 'select', '#title' => t('Nivel'), '#options' => $options, '#description' => t("La fila donde se encuentra el nivel de jerarquia del ente."), '#required' => TRUE, ); $form['father'] = array( '#type' => 'select', '#title' => t('Ente padre'), '#options' => $options, '#description' => t("La fila donde se encuentra el ente padre del ente."), '#required' => TRUE, ); $form['sector'] = array( '#type' => 'select', '#title' => t('Sector'), '#options' => $options, '#description' => t("La fila donde se encuentra el sector del ente."), ); $form['acronimo'] = array( '#type' => 'select', '#title' => t('Acrónimo'), '#options' => $options, '#description' => t("La fila donde se encuentra el acrónimo del ente."), ); $form['encabezado'] = array( '#title' => 'Primera columna como encabezado', '#type' => 'checkbox', '#return_value' => '1', ); $form['submit'] = array( '#type' => 'submit', '#value' => 'Submit', ); return $form; } /** * Implementation of ente_planificador_importar_form_submit(). * Submit cvs file in entes import */ function ente_planificador_importar_form_submit($form, &$form_state) { global $user; $validators = array('file_validate_extensions' => array('csv')); // Check for a new uploaded file. $file = file_save_upload('file_import_entes', $validators); if (isset($file)) { // File upload was attempted. if ($file) { // Put the temporary file in form_values so we can save it on submit. $csv = $file->filepath; $file = fopen($csv, 'r'); $time = time(); $i = 1; while (($data = fgetcsv($file)) !== FALSE) { $ente = new stdClass(); if ($form_state['values']['encabezado']) $ente->type = 'ente_planificador'; $ente->title = check_plain($data[$form_state['values']['titulo'] - 1]); $ente->father = $data[$form_state['values']['father'] - 1]; if ($data[$form_state['values']['sector']]) { $ente->sector = $data[$form_state['values']['sector'] - 1]; } if ($data[$form_state['values']['acronimo']]) { $ente->import_acronimo = check_plain($data[$form_state['values']['acronimo'] - 1]); // $ente->ente_acronimo[0]['value'] = $ente->import_acronimo; } $ente->status = 1; $ente->tipo = $data[$form_state['values']['level']]; $ente->promote = 0; $ente->promote = 0; $ente->sticky = 0; $ente->uid = $user->uid; $ente->revision = 1; $texto = t('Agregado el ente @nombre_ente', array('@nombre_ente' => $ente->title)); $ente->log = $texto; if (!($form_state['values']['encabezado'] && $i == 1)) { $datas[] = $ente; } $i++; } $_SESSION['ente_planificador_importar'][$time] = $datas; $form_state['redirect'] = 'ente_planificador_importar/execute/' . $time; } else { // File upload failed. form_set_error('file_import_entes', t('The entes file could not be uploaded.')); } } } /** * Implementation of _ente_planificador_importar_execute(). * Batch 'execute' callback */ function _ente_planificador_importar_execute($execute_id){ if ($execute_id && isset($_SESSION['ente_planificador_importar'][$execute_id]) && is_array($_SESSION['ente_planificador_importar'][$execute_id])) { $batch = array( 'title' => t('Importing entes planificadores ...', array('@format' => $format)), 'operations' => array(), 'init_message' => t('Commencing'), 'progress_message' => t('Processed @current out of @total.'), 'error_message' => t('An error occurred during processing'), 'finished' => '_batch_ente_planificador_importar_finished', ); $datas = $_SESSION['ente_planificador_importar'][$execute_id]; unset($_SESSION['ente_planificador_importar'][$execute_id]); $cantidad = count($datas); $i = 1; foreach($datas as $data) { $batch['operations'][] = array('_batch_ente_planificador_importar', array($data, $i, $cantidad)); $i++; } batch_set($batch); batch_process('ente_planificador_importar'); } else { drupal_set_message(t('Ocurrio un error al intentar importar los entes planificadores')); drupal_goto('ente_planificador_importar'); } } /** * Implementation of _ente_planificador_importar_usuarios(). * Obtiene la cantidad de usuarios por tipo de ente */ function _ente_planificador_importar_usuarios($tipo = 0) { $cant_usuarios = array(); $cant_usuarios[1] = array( 'for' => 0, 'enl' => 5, 'sup' => 5, ); $cant_usuarios[2] = array( 'for' => 0, 'enl' => 3, 'sup' => 3, ); $cant_usuarios[3] = array( 'for' => 10, 'enl' => 2, 'sup' => 1, ); $cant_usuarios[4] = array( 'for' => 3, 'enl' => 0, 'sup' => 1, ); return $cant_usuarios[$tipo]; } /** * Implementation of _batch_ente_planificador_importar(). * Save entes (used in _ente_planificador_importar_execute) * Batch run callback */ function _batch_ente_planificador_importar($ente, $current = 0, $cantidad, &$context) { //$context['results'][] = 'ente importado: ' . $ente->title; $father = $ente->father; //Se consulta la jerarquia del ente padre $desc = _ente_planificador_hierarchical_get_descbyacronimo($ente->father); $count = count($desc); if ($count) { foreach($desc as $idfathers) { if ($idfathers['father']) { $ente->ente_planificador_hierarchical[$idfathers['level']] = $idfathers['father']; $ente->{'ente_planificador_hierarchical_father_' . $idfathers['level']} = $idfathers['father']; } else { $ente->ente_planificador_hierarchical[$idfathers['level']] = $idfathers['nid']; $ente->{'ente_planificador_hierarchical_father_' . $idfathers['level']} = $idfathers['nid']; } } $ente->{'ente_planificador_hierarchical_father_' . ($count + 1)} = 0; } $level = count($ente->ente_planificador_hierarchical[$idfathers['level']]); if ($level) { $level++; //Se ajusta el nivel if ($ente->tipo != $level) { $ente->tipo = $level; } $acronimo = isset($ente->import_acronimo)? $ente->import_acronimo : 0; //$context['results'][] = 'ente importado:
' . print_r($ente, 1) . '
'; node_save($ente); $falla = $ente->nid ? '' : 'FALLA'; $context['results'][] = 'ente importado:
' . print_r($ente, 1) . $falla . '
'; if ($ente && $ente->nid) { //Se ingresa el acronimo si existe if ($acronimo) { db_query("UPDATE {ente_planificador_hierarchical} SET acronimo = '%s' WHERE nid = %d", $acronimo, $ente->nid); } else { $acronimo = 'ente_' . $acronimo; } //se crean los usuarios segun el tipo $roles_type = _ente_planificador_hierarchical_get_roles_type($ente->tipo); $cant_usuarios = _ente_planificador_importar_usuarios($ente->tipo); foreach ($roles_type as $rol => $text) { $array = array(); $array['roles'] = array(); $array['roles'][DRUPAL_AUTHENTICATED_RID] = DRUPAL_AUTHENTICATED_RID; $rol_tipo = variable_get('ente_planificador_hierarchical_rol_' . $rol . '_' . $ente->tipo, 2); //existe el rol para el usuario if ($rol_tipo && $cant_usuarios[$rol]) { for ($i = 0; $i < $cant_usuarios[$rol]; $i++) { $array['roles'][$rol_tipo] = $rol_tipo; $array['name'] = $acronimo . '_' . $rol . '_' . ($i + 1); $array['pass'] = $acronimo . '_' . $rol . '_' . ($i + 1); $array['mail'] = $rol . '@' . 'ente_' . $acronimo . '.com'; $array['status'] = 1; $array['entes'] = $ente->nid; user_save(NULL, $array); } } } } } //$context['results'][] = $father . 'ente importado: ' . $ente->title . '
' . print_r($ente, 1) . '
'; $context['sandbox']['progress']++; $context['sandbox']['current'] = $current; //$context['sandbox']['max'] = $cantidad; $context['message'] = t('Now processing ente: %submission', array('%submission' => $ente->title)); if ($context['sandbox']['progress'] >= $context['sandbox']['max']) { $context['finished'] = 1; } else { $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; } } /** * Implementation of _batch_ente_planificador_importar_finished(). * Batch 'finished' callback */ function _batch_ente_planificador_importar_finished($success, $results, $operations) { if ($success) { // Here we do something meaningful with the results. $message = t('!count_webform entes processed', array('!count_webform' => print_r($results, TRUE))); } 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); } /** * Implementation of _ente_planificador_hierarchical_get_descbyacronimo(). * Obtiene los padres de un ente por el acronimo o nombre */ function _ente_planificador_hierarchical_get_descbyacronimo($acronimo = '') { $fathers = array(); //get fathers if ($acronimo) { //Se verifica que el arconimo exista $query = db_query("SELECT * FROM {ente_planificador_hierarchical} WHERE acronimo = '%s'", $acronimo); $father_acronimo = db_fetch_object($query); if (!$father_acronimo || !$father_acronimo->acronimo) { $query = db_query("SELECT h.* FROM {node} AS n INNER JOIN {ente_planificador_hierarchical} AS h ON h.nid = n.nid WHERE n.title = '%s'", $acronimo); $father_acronimo = db_fetch_object($query); } if ($father_acronimo && $father_acronimo->nid) { $query = db_query('SELECT * FROM {ente_planificador_hierarchical} WHERE nid = %d', $father_acronimo->nid); while ($father = db_fetch_object($query)) { $fathers[] = array( 'title' => $father->title, 'nid' => $father->nid, 'father' => $father->father, 'level' => $father->level, 'title' => $father->acronimo, ); } } } return $fathers; }