source: sipes/0.3-modules/sipesonapre/sipesonapre.module @ f74cf58

stableversion-3.0
Last change on this file since f74cf58 was 1271edc, checked in by planificacion <planificacion@…>, 8 años ago

Se agregaron funciones para la gestion de servicios, correccion de errores y filtrado de proyectos por workflow

  • Propiedad mode establecida a 100644
File size: 10.1 KB
Línea 
1<?php
2  /**
3  * Sistema Integral de Planificación y Presupuesto (SIPP)
4  * @file sipesonapre.module
5  * Drupal part Module to Sistema Integral de Planificación y Presupuesto (SIPP)
6  * Copyright 2013 Sistema Automatizado para la Planificación Estratégico-Situacional en la Administración Pública Venezolana (CENDITEL)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  * @author Cenditel Merida - PHD. Juan Vizcarrondo
23  * @date 2016-08-15 // (a&#241;o-mes-dia)
24  * @version 0.2 // (0.1)
25  *
26  */
27function sipesonapre_form_alter(&$form, $form_state, $form_id) {
28  if ($form_id == 'proyectos_operativos_admin_ae_settings') {
29    $node_type = content_types('accion_especifica');
30    $fields = $node_type['fields'];
31    $accion_especifica_number_fields = variable_get('accion_especifica_number_fields', 4);
32    $fuentes_id = array();
33    $k = 1;
34    $form['sipesonapre'] = array(
35      '#type' => 'fieldset',
36      '#title' => t('ONAPRE (Códigos)'),
37      '#collapsible' => TRUE,
38      '#collapsed' => FALSE,
39      '#weight' => -1,
40    );
41    $form['sipesonapre']['onapre_encode_ISO_8859_1'] = array(
42      '#type' => 'checkbox',
43      '#title' => t('Convertir ISO-8859-1'),
44      '#default_value' => variable_get('onapre_encode_ISO_8859_1', FALSE),
45    );
46    if (module_exists('workflow')) {
47      $wid = workflow_get_workflow_for_type('proyectos_operativos');
48      if ($wid){
49        $states = workflow_get_states($wid);
50        if (count ($states)) {
51          $form['sipesonapre']['onapre_workflow_export'] = array(
52            '#type' => 'select',
53            '#title' => t('Estado aprobado'),
54            '#default_value' => variable_get('onapre_workflow_export', array()),
55            '#description' => t('Mostrar solo los proyectos con estos estados. Si no selecciona ninguna opción se mostraran los proyectos sin importar su estado'),
56            '#options' => $states,
57            '#multiple' => TRUE,
58          );
59        }
60      }
61    }
62
63    if ($accion_especifica_number_fields > 0) {
64      for($i = 0; $i < $accion_especifica_number_fields; $i++) {
65        $arreglo_financiamiento['financiamiento_' . $i] = variable_get('accion_especifica_financiamiento_label_' . $i, '') ? variable_get('accion_especifica_financiamiento_label_' . $i, '') : '';
66        $fuentes = variable_get('accion_especifica_financiamiento_options_' . $i, array());
67        foreach($fuentes as $id) {
68          if ($id) {
69            $form['sipesonapre']['onapre_' . $id] = array(
70              '#type' => 'textfield',
71              '#title' => $fields[$id]['widget']['label'],
72              '#required' => TRUE,
73              '#default_value' => variable_get('onapre_' . $id, ''),
74            );
75          }
76        }
77      }
78    }
79  }
80}
81 
82function _sipesonapre_get_fuentes($nid = 0, $type = 'table') {
83  $cell_separator = ",";
84  $row_separator = "\n";
85
86  $node_type = content_types('accion_especifica');
87  $fields = $node_type['fields'];
88  $accion_especifica_number_fields = variable_get('accion_especifica_number_fields', 4);
89  $fuentes_id = array();
90  $k = 1;
91  $codigos = array();
92  if ($accion_especifica_number_fields > 0) {
93    for($i = 0; $i < $accion_especifica_number_fields; $i++) {
94      $arreglo_financiamiento['financiamiento_' . $i] = variable_get('accion_especifica_financiamiento_label_' . $i, '') ? variable_get('accion_especifica_financiamiento_label_' . $i, '') : '';
95      $fuentes = variable_get('accion_especifica_financiamiento_options_' . $i, array());
96      foreach($fuentes as $id) {
97        if ($id) {
98          $fuentes_id['campo_' . $k . '_total'] = $id;
99          $codigos[$id] = variable_get('onapre_' . $id, '');
100          $k++;
101        }
102      }
103    }
104  }
105  $estados = variable_get('onapre_workflow_export', array());
106  $nestados = count($estados);
107  $select =array();
108  $select['node_nid'] = 'node.nid AS node_nid';
109  //Se agrega la cantidad de AE
110  $select['node_node_data_field_proyecto_accion_esp_nid'] = 'COUNT(node_node_data_field_proyecto_accion_esp.nid) AS node_node_data_field_proyecto_accion_esp_nid';
111  $from = array();
112  $from[] = '{node} AS node';
113  $join = array(); 
114  $join[] = 'LEFT JOIN {content_field_proyecto_accion_esp} AS node_data_field_proyecto_accion_esp ON node.vid = node_data_field_proyecto_accion_esp.vid'; 
115  $join[] = 'LEFT JOIN {node} AS node_node_data_field_proyecto_accion_esp ON node_data_field_proyecto_accion_esp.field_proyecto_accion_esp_nid = node_node_data_field_proyecto_accion_esp.nid';
116  $join[] = 'LEFT JOIN {content_type_accion_especifica} AS node_node_data_field_proyecto_accion_esp_node_data_field_accion_esp_transferencias ON node_node_data_field_proyecto_accion_esp.vid = node_node_data_field_proyecto_accion_esp_node_data_field_accion_esp_transferencias.vid';
117  if ($nestados) {
118    $join[] = 'LEFT JOIN {workflow_node} AS westados ON node.vid = westados.nid';
119  }
120  $where = array();
121  $where[] = "node.type in ('proyectos_operativos')";
122  if ($nestados) {
123    $where[] = "westados.sid in (" . implode(', ', $estados) . ")";
124  }
125  $groupby = array();
126  $groupby[] = 'node_nid';
127  $i = 0;
128  foreach ($fuentes_id as $id) {
129    $select['campo_' . $i . 'total'] = 'SUM(node_node_data_field_proyecto_accion_esp_node_data_field_accion_esp_transferencias.' . $id . '_value) AS campo_' . $i . '_total';
130    $i++;
131  }
132  $sql = 'SELECT ' . implode(', ', $select) . ' FROM ' . implode(', ', $from) . ' ' . implode (' ', $join) . ' WHERE ' . implode(' AND ', $where) . ' GROUP BY ' . implode(', ', $groupby);
133  $result = db_query($sql);
134  if ($type == 'table') {
135    $header = array(
136      array('data' => t('PROY_PROY_ID')),
137      array('data' => t('FUENTE_ID')),
138      array('data' => t('MONTO')),
139    );
140    $rows = array();
141  }
142  elseif ($type == 'cvs') {
143    $cadena = '"PROY_PROY_ID"' . $cell_separator . '"FUENTE_ID"' . $cell_separator . '"MONTO"' . $row_separator;
144  }
145  elseif ($type == 'data') {
146    $arreglo = array();
147  }
148  while ($data = db_fetch_array($result)) {
149    $fuentes_fin = array();
150    $nid = $data['node_nid'];
151    foreach ($data as $id => $valor) {
152      if ($valor && isset($fuentes_id[$id])) {
153        if (!isset($fuentes_fin[$codigos[$fuentes_id[$id]]])) {
154          $fuentes_fin[$codigos[$fuentes_id[$id]]] = 0;
155        }
156        $fuentes_fin[$codigos[$fuentes_id[$id]]] += $valor;
157      }
158    }
159    if ($type == 'table') {
160      $row_nid = array('data' => $nid,);
161    }
162    elseif ($type == 'cvs') {
163      $texto_base = '"' . $nid . '"' . $cell_separator;
164    }
165    elseif ($type == 'data') {
166      $datos_other = array();
167    }
168    foreach($fuentes_fin as $id => $valor) {
169      if ($type == 'table') {
170        $row = array();
171        $row[] = $row_nid;
172        $row[] = array('data' => $id,);
173        $row[] = array('data' => $valor,);
174        $rows[] = $row;
175      }
176      elseif ($type == 'cvs') {
177        $cadena .= $texto_base . '"' . $id . '"' . $cell_separator . '"' . $valor . '"' . $row_separator;
178      }
179      elseif ($type == 'data') {
180        $item = new stdClass();
181        $item->PROY_PROY_ID = $nid;
182        $item->FUENTE_ID = $id;
183        $item->MONTO = $valor;
184        $arreglo[] = $item;
185      }
186    }
187  }
188  if ($type == 'table') {
189
190    if (empty($rows)) {
191      $rows[] = array(array('data' => t('No existen datos almacenados en el sistema.'), 'colspan' => 3));
192    }
193    return theme('table', $header, $rows, array('id' => 'fuentes_proy'));
194  }
195  elseif ($type == 'cvs') {
196    $filename = 'fuentes_proyectos_' . time() . '.csv';
197    if (variable_get('onapre_encode_ISO_8859_1', FALSE)) {
198      header('Content-type: text/plain; charset=utf-8');
199      $cadena = utf8_decode ($cadena);
200    }
201    else {
202      header('Content-type: text/plain');
203    }
204    header('Content-Disposition: attachment; filename="' . $filename . '"');
205    print $cadena;
206    exit();
207  }
208  elseif ($type == 'data') {
209    return $arreglo;
210  }
211}
212/*
213 * Implementation of hook_menu()
214 */
215function sipesonapre_menu() {
216  $items = array();
217  $items['onapre/proyectos/fuentes'] = array(
218    'title' => 'Fuentes Proyectos ONAPRE',
219    'page callback' => '_sipesonapre_get_fuentes',
220    'page arguments' => array('0', 'table'),
221    'access arguments' => array('consultar onapre'),
222    'type' => MENU_CALLBACK,
223  );
224  $items['onapre/proyectos/fuentes/cvs'] = array(
225    'title' => 'Fuentes Proyectos ONAPRE CVS',
226    'page callback' => '_sipesonapre_get_fuentes',
227    'page arguments' => array('0', 'cvs'),
228    'access arguments' => array('consultar onapre'),
229    'type' => MENU_CALLBACK,
230  );
231  return $items;
232}
233/*
234 * Implementation of hook_perm
235 */
236function sipesonapre_perm() {
237  return array(
238    'consultar onapre',
239  );
240}
241
242/**
243 * Implementation of hook_services_resources().
244 */
245function sipesonapre_services_resources() {
246  $resources['sipesonapre'] = array();
247  $resources['sipesonapre']['retrieve'] = array(
248    'help' => 'Retrieves Fuentes Proyectos ONAPRE.',
249    'callback' => 'sipesonapre_retrieve',
250    'access callback' => 'sipesonapre_access',
251    'access arguments' => array('view'),
252    'access arguments append' => TRUE,
253  );
254
255  return $resources;
256}
257
258/**
259 * Check the access permission to a given views.
260 *
261 * @param $op
262 *  String. The operation that's going to be performed.
263 * @param $args
264 *  Array. The arguments that will be passed to the callback.
265 * @return
266 *  Boolean. TRUE if the user is allowed to load the given view.
267 */
268function sipesonapre_access($op = 'view') {
269  return user_access('consultar onapre');
270}
271
272/**
273 * Callback for retrieving fuentes onapre.
274 *
275 *
276 * @return
277 *  Array. The views return.
278 */
279function sipesonapre_retrieve() {
280  $result = _sipesonapre_get_fuentes($nid = 0, 'data');
281  return $result;
282}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.