'Importar Entes Planificadores', 'page callback' => '_ente_planificador_importar_listar', 'access arguments' => array('access import entes planificadores'), 'type' => MENU_NORMAL_ITEM, ); $items['ente_planificador_importar/entes'] = array( 'title' => 'Importar Entes Planificadores', 'page callback' => 'drupal_get_form', 'page arguments' => array('ente_planificador_importar_form'), 'access arguments' => array('import 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('import term'), 'type' => MENU_CALLBACK, ); $items['ente_planificador_importar/estadoproyecto'] = array( 'title' => 'Modificar estado de poyectos', 'page callback' => 'drupal_get_form', 'page arguments' => array('ente_planificador_importar_aprobar_proyectos_form'), 'access arguments' => array('update proyecto state'), 'type' => MENU_CALLBACK, ); return $items; } /* * Implementation of _ente_planificador_importar_listar() * Muestra las opciones de importacion disponibles */ function _ente_planificador_importar_listar() { $links = array(); drupal_alter('ente_planificador_importar_links', $links); return theme('item_list', $links); } /* * Implementation of hook_ente_planificador_importar_links_alter() */ function ente_planificador_ente_planificador_importar_links_alter(&$links) { if (user_access('import entes planificadores')) { $links['entes'] = array( 'data' => l(t('Importar Entes Planificadores'), 'ente_planificador_importar/entes'), 'class' => 'ente-planificador-importar-entes', ); } if (user_access('import term')) { $links['term'] = array( 'data' => l(t('Importar terminos'), 'ente_planificador_importar/terminos'), 'class' => 'ente-planificador-importar-term', ); } if (user_access('update proyecto state')) { $links['estadoproyecto'] = array( 'data' => l(t('Actualizar estado de proyectos'), 'ente_planificador_importar/estadoproyecto'), 'class' => 'ente-planificador-importar-estado-proyecto', ); } } /** * 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(); $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', ); 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' => array( 'value' => check_plain($data[$form_state['values']['code'] - 1]), ), ), ); if (!($form_state['values']['encabezado'] && $i == 1)) { $batch['operations'][] = array('_batch_ente_planificador_importar_term', array($term)); } $i++; } batch_set($batch); batch_process('ente_planificador_importar/terminos'); } 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++; } 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, &$context) { taxonomy_save_term($term); $context['message'] = t('Now processing ente: %submission', array('%submission' => $term->name)); $context['results'][] = 'term importado: ' . $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 < 30; $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."), ); $node_type = content_types('ente_planificador'); $fields = $node_type['fields']; $campos = array(); if (count($fields)) { $form['campos'] = array( '#type' => 'fieldset', '#title' => t('campos'), ); foreach($fields as $field_id => $field) { $campos[$field_id] = $field; $form['campos'][$field_id] = array( '#type' => 'select', '#title' => $field['widget']['label'], '#options' => $options, '#description' => t("La fila donde se encuentra el campo del ente."), ); } } $form['encabezado'] = array( '#title' => 'Primera columna como encabezado', '#type' => 'checkbox', '#return_value' => '1', ); $form['#ente_fields'] = $campos; $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; $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', ); 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']] -1) { $ente->sector_social = $data[$form_state['values']['sector'] - 1]; } if ($data[$form_state['values']['acronimo']] -1) { $ente->import_acronimo = check_plain($data[$form_state['values']['acronimo'] - 1]); } //se agregan los campos if (count($form['#ente_fields'])) { foreach($form['#ente_fields'] as $field_id => $field) { if ($data[$form_state['values'][$field_id]]) { $tipo = 'value'; if ($field['type'] == 'nodereference') { $tipo = 'nid'; } if ($field['type'] == 'userreference') { $tipo = 'uid'; } if ($field['type'] == 'email') { $tipo = 'email'; } if ($field['type'] == 'link') { $tipo = 'link'; } if ($field['type'] == 'content_taxonomy') { $tipo = 'content_taxonomy'; } $ente->{$field_id}[0][$tipo] = check_plain($data[$form_state['values'][$field_id] - 1]); } } } $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)) { $batch['operations'][] = array('_batch_ente_planificador_importar', array($ente)); $datas[] = $ente; } $i++; } batch_set($batch); batch_process('ente_planificador_importar/entes'); } 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)); } 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, &$context) { $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']) { $parent = taxonomy_get_parents($ente->sector_social); $ente->sector = $ente->sector_social; $ente->ambito = key($parent); $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; $ente->sector = isset($ente->sector_social)? $ente->sector_social : 0; node_save($ente); $falla = $ente->nid ? '' : 'FALLA'; //insert sector $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 . '_' . ($i + 1) . '@' . 'ente_' . $acronimo . '.com'; $array['status'] = 1; $array['entes'] = $ente->nid; user_save(NULL, $array); } } } } } $context['message'] = t('Now processing ente: %submission', array('%submission' => $ente->title)); } /** * 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; } /** * Implementation of ente_planificador_importar_aprobar_proyectos_form(). * Form to load cvs file and setting in proyectos estate update */ function ente_planificador_importar_aprobar_proyectos_form() { $form = array(); // If this #attribute is not present, upload will fail on submit $form['#attributes']['enctype'] = 'multipart/form-data'; $form['file_import_proyectos'] = array( '#title' => t('Proyectos a aprobar'), '#type' => 'file', '#description' => t('Archivo cvs con el contenido de los proyectos a aprobar'), ); $options = array(); for($i = 0;$i < 11; $i++) { if (!$i) { $options[$i] = t('N/A'); } else { $options[$i] = t('Fila') . ' ' . $i; } } $form['sipes'] = array( '#type' => 'select', '#title' => t('Código SIPES del proyecto'), '#options' => $options, '#description' => t("La fila donde se encuentra el código SIPES del proyecto."), '#required' => TRUE, ); $estados = _proyectos_operativos_reformula_obtiene_estados(); $form['workflow'] = array( '#type' => 'select', '#title' => t('Nuevo estado del proyecto'), '#options' => $estados['states'], '#description' => t("Seleccione el nuevo estado que se colocara a los proyectos contenidos en el archivo de importación."), '#required' => TRUE, ); $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_aprobar_proyectos_form_submit(). * Submit cvs file in proyectos estate update */ function ente_planificador_importar_aprobar_proyectos_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_proyectos', $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(); $batch = array( 'title' => t('Importing proyectos ...', 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_proyecto_operativo_cambiar_estado_importar_finished', ); while (($data = fgetcsv($file)) !== FALSE) { $sipes = check_plain($data[$form_state['values']['sipes'] - 1]); if ($sipes != '') { $proyecto = array( 'sipes' => $sipes, // codigo sipes 'workflow' => $form_state['values']['workflow'], // workflow 'workflows' => $form['workflow']['#options'], // workflow options ); if (!($form_state['values']['encabezado'] && $i == 1)) { $batch['operations'][] = array('_batch_proyecto_operativo_cambiar_estado_importar', array($proyecto)); } $i++; } } batch_set($batch); batch_process('ente_planificador_importar/estadoproyecto'); } else { // File upload failed. form_set_error('file_import_proyectos', t('The proyectos file could not be uploaded.')); } } } /** * Implementation of _batch_proyecto_operativo_cambiar_estado_importar(). * Update state proyectos (used in ente_planificador_importar_aprobar_proyectos_form_submit) * Batch run callback */ function _batch_proyecto_operativo_cambiar_estado_importar($proyecto, &$context) { $context['message'] = t('Now processing proyecto: %submission', array('%submission' => check_plain($proyecto['sipes']))); $query = db_query("SELECT nid FROM {node} WHERE title = '%s'", $proyecto['sipes']); $proyecto_nid = db_fetch_object($query); if ($proyecto_nid && $proyecto_nid->nid) { $node = node_load($proyecto_nid->nid); if ($node->nid) { if ($node->_workflow != $proyecto['workflow']) { $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']])); //$node->_workflow = $proyecto['workflow']; $node->revision = 1; $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']])); $node->log = $texto; workflow_execute_transition($node, $proyecto['workflow'], $texto, TRUE); //$node->_workflow = $proyecto['workflow']; node_save($node); } else { $context['results'][] = t('El proyecto @proyecto ya se encuentra en el estado @prox ', array('@proyecto' => $proyecto['sipes'], '@prox' => $proyecto['workflows'][$proyecto['workflow']])); } } else { $context['results'][] = t('No se encontro el proyecto @proyecto', array('@proyecto' => $proyecto['sipes'])); } } else { $context['results'][] = t('No se encontro el proyecto @proyecto', array('@proyecto' => $proyecto['sipes'])); } } /** * Implementation of _batch_ente_planificador_importar_finished(). * Batch 'finished' callback */ function _batch_proyecto_operativo_cambiar_estado_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); }