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ño-mes-dia) |
---|
24 | * @version 0.2 // (0.1) |
---|
25 | * |
---|
26 | */ |
---|
27 | function 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']['proyectos_operativos_perm_edit'] = 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 ($accion_especifica_number_fields > 0) { |
---|
47 | for($i = 0; $i < $accion_especifica_number_fields; $i++) { |
---|
48 | $arreglo_financiamiento['financiamiento_' . $i] = variable_get('accion_especifica_financiamiento_label_' . $i, '') ? variable_get('accion_especifica_financiamiento_label_' . $i, '') : ''; |
---|
49 | $fuentes = variable_get('accion_especifica_financiamiento_options_' . $i, array()); |
---|
50 | foreach($fuentes as $id) { |
---|
51 | if ($id) { |
---|
52 | $form['sipesonapre']['onapre_' . $id] = array( |
---|
53 | '#type' => 'textfield', |
---|
54 | '#title' => $fields[$id]['widget']['label'], |
---|
55 | '#required' => TRUE, |
---|
56 | '#default_value' => variable_get('onapre_' . $id, ''), |
---|
57 | ); |
---|
58 | } |
---|
59 | } |
---|
60 | } |
---|
61 | } |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | function _sipesonapre_get_fuentes($nid = 0, $type = 'table') { |
---|
66 | $cell_separator = ","; |
---|
67 | $row_separator = "\n"; |
---|
68 | |
---|
69 | $node_type = content_types('accion_especifica'); |
---|
70 | $fields = $node_type['fields']; |
---|
71 | $accion_especifica_number_fields = variable_get('accion_especifica_number_fields', 4); |
---|
72 | $fuentes_id = array(); |
---|
73 | $k = 1; |
---|
74 | $codigos = array(); |
---|
75 | if ($accion_especifica_number_fields > 0) { |
---|
76 | for($i = 0; $i < $accion_especifica_number_fields; $i++) { |
---|
77 | $arreglo_financiamiento['financiamiento_' . $i] = variable_get('accion_especifica_financiamiento_label_' . $i, '') ? variable_get('accion_especifica_financiamiento_label_' . $i, '') : ''; |
---|
78 | $fuentes = variable_get('accion_especifica_financiamiento_options_' . $i, array()); |
---|
79 | foreach($fuentes as $id) { |
---|
80 | if ($id) { |
---|
81 | $fuentes_id['campo_' . $k . '_total'] = $id; |
---|
82 | $codigos[$id] = variable_get('onapre_' . $id, ''); |
---|
83 | $k++; |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | $select =array(); |
---|
89 | $select['node_nid'] = 'node.nid AS node_nid'; |
---|
90 | //Se agrega la cantidad de AE |
---|
91 | $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'; |
---|
92 | $from = array(); |
---|
93 | $from[] = '{node} AS node'; |
---|
94 | $join = array(); |
---|
95 | $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'; |
---|
96 | $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'; |
---|
97 | $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'; |
---|
98 | $where = array(); |
---|
99 | $where[] = "node.type in ('proyectos_operativos')"; |
---|
100 | $groupby = array(); |
---|
101 | $groupby[] = 'node_nid'; |
---|
102 | $i = 0; |
---|
103 | foreach ($fuentes_id as $id) { |
---|
104 | $select['campo_' . $i . 'total'] = 'SUM(node_node_data_field_proyecto_accion_esp_node_data_field_accion_esp_transferencias.' . $id . '_value) AS campo_' . $i . '_total'; |
---|
105 | $i++; |
---|
106 | } |
---|
107 | $sql = 'SELECT ' . implode(', ', $select) . ' FROM ' . implode(', ', $from) . ' ' . implode (' ', $join) . ' WHERE ' . implode(' AND ', $where) . ' GROUP BY ' . implode(', ', $groupby); |
---|
108 | $result = db_query($sql); |
---|
109 | if ($type == 'table') { |
---|
110 | $header = array( |
---|
111 | array('data' => t('PROY_PROY_ID')), |
---|
112 | array('data' => t('FUENTE_ID')), |
---|
113 | array('data' => t('MONTO')), |
---|
114 | ); |
---|
115 | $rows = array(); |
---|
116 | } |
---|
117 | elseif ($type == 'cvs') { |
---|
118 | $cadena = '"PROY_PROY_ID"' . $cell_separator . '"FUENTE_ID"' . $cell_separator . '"MONTO"' . $row_separator; |
---|
119 | } |
---|
120 | elseif ($type == 'data') { |
---|
121 | $arreglo = array(); |
---|
122 | } |
---|
123 | while ($data = db_fetch_array($result)) { |
---|
124 | $fuentes_fin = array(); |
---|
125 | $nid = $data['node_nid']; |
---|
126 | foreach ($data as $id => $valor) { |
---|
127 | if ($valor && isset($fuentes_id[$id])) { |
---|
128 | if (!isset($fuentes_fin[$codigos[$fuentes_id[$id]]])) { |
---|
129 | $fuentes_fin[$codigos[$fuentes_id[$id]]] = 0; |
---|
130 | } |
---|
131 | $fuentes_fin[$codigos[$fuentes_id[$id]]] += $valor; |
---|
132 | } |
---|
133 | } |
---|
134 | if ($type == 'table') { |
---|
135 | $row_nid = array('data' => $nid,); |
---|
136 | } |
---|
137 | elseif ($type == 'cvs') { |
---|
138 | $texto_base = '"' . $nid . '"' . $cell_separator; |
---|
139 | } |
---|
140 | elseif ($type == 'data') { |
---|
141 | $datos_other = array(); |
---|
142 | } |
---|
143 | foreach($fuentes_fin as $id => $valor) { |
---|
144 | if ($type == 'table') { |
---|
145 | $row = array(); |
---|
146 | $row[] = $row_nid; |
---|
147 | $row[] = array('data' => $id,); |
---|
148 | $row[] = array('data' => $valor,); |
---|
149 | $rows[] = $row; |
---|
150 | } |
---|
151 | elseif ($type == 'cvs') { |
---|
152 | $cadena .= $texto_base . '"' . $id . '"' . $cell_separator . '"' . $valor . '"' . $row_separator; |
---|
153 | } |
---|
154 | elseif ($type == 'data') { |
---|
155 | $item = new stdClass(); |
---|
156 | $item->PROY_PROY_ID = $nid; |
---|
157 | $item->FUENTE_ID = $id; |
---|
158 | $item->MONTO = $valor; |
---|
159 | $arreglo[] = $item; |
---|
160 | } |
---|
161 | } |
---|
162 | } |
---|
163 | if ($type == 'table') { |
---|
164 | |
---|
165 | if (empty($rows)) { |
---|
166 | $rows[] = array(array('data' => t('No existen datos almacenados en el sistema.'), 'colspan' => 3)); |
---|
167 | } |
---|
168 | return theme('table', $header, $rows, array('id' => 'fuentes_proy')); |
---|
169 | } |
---|
170 | elseif ($type == 'cvs') { |
---|
171 | $filename = 'fuentes_proyectos_' . time() . '.csv'; |
---|
172 | if (variable_get('onapre_encode_ISO_8859_1', FALSE)) { |
---|
173 | header('Content-type: text/plain; charset=utf-8'); |
---|
174 | $cadena = utf8_decode ($cadena); |
---|
175 | } |
---|
176 | else { |
---|
177 | header('Content-type: text/plain'); |
---|
178 | } |
---|
179 | header('Content-Disposition: attachment; filename="' . $filename . '"'); |
---|
180 | print $cadena; |
---|
181 | exit(); |
---|
182 | } |
---|
183 | elseif ($type == 'data') { |
---|
184 | return $arreglo; |
---|
185 | } |
---|
186 | } |
---|
187 | /* |
---|
188 | * Implementation of hook_menu() |
---|
189 | */ |
---|
190 | function sipesonapre_menu() { |
---|
191 | $items = array(); |
---|
192 | $items['onapre/proyectos/fuentes'] = array( |
---|
193 | 'title' => 'Fuentes Proyectos ONAPRE', |
---|
194 | 'page callback' => '_sipesonapre_get_fuentes', |
---|
195 | 'page arguments' => array('0', 'table'), |
---|
196 | 'access arguments' => array('consultar onapre'), |
---|
197 | 'type' => MENU_CALLBACK, |
---|
198 | ); |
---|
199 | $items['onapre/proyectos/fuentes/cvs'] = array( |
---|
200 | 'title' => 'Fuentes Proyectos ONAPRE CVS', |
---|
201 | 'page callback' => '_sipesonapre_get_fuentes', |
---|
202 | 'page arguments' => array('0', 'cvs'), |
---|
203 | 'access arguments' => array('consultar onapre'), |
---|
204 | 'type' => MENU_CALLBACK, |
---|
205 | ); |
---|
206 | return $items; |
---|
207 | } |
---|
208 | /* |
---|
209 | * Implementation of hook_perm |
---|
210 | */ |
---|
211 | function sipesonapre_perm() { |
---|
212 | return array( |
---|
213 | 'consultar onapre', |
---|
214 | ); |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * Implementation of hook_services_resources(). |
---|
219 | */ |
---|
220 | function sipesonapre_services_resources() { |
---|
221 | $resources['sipesonapre'] = array(); |
---|
222 | $resources['sipesonapre']['retrieve'] = array( |
---|
223 | 'help' => 'Retrieves Fuentes Proyectos ONAPRE.', |
---|
224 | 'callback' => 'sipesonapre_retrieve', |
---|
225 | 'access callback' => 'sipesonapre_access', |
---|
226 | 'access arguments' => array('view'), |
---|
227 | 'access arguments append' => TRUE, |
---|
228 | ); |
---|
229 | |
---|
230 | return $resources; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * Check the access permission to a given views. |
---|
235 | * |
---|
236 | * @param $op |
---|
237 | * String. The operation that's going to be performed. |
---|
238 | * @param $args |
---|
239 | * Array. The arguments that will be passed to the callback. |
---|
240 | * @return |
---|
241 | * Boolean. TRUE if the user is allowed to load the given view. |
---|
242 | */ |
---|
243 | function sipesonapre_access($op = 'view') { |
---|
244 | return user_access('consultar onapre'); |
---|
245 | } |
---|
246 | |
---|
247 | /** |
---|
248 | * Callback for retrieving fuentes onapre. |
---|
249 | * |
---|
250 | * |
---|
251 | * @return |
---|
252 | * Array. The views return. |
---|
253 | */ |
---|
254 | function sipesonapre_retrieve() { |
---|
255 | $result = _sipesonapre_get_fuentes($nid = 0, 'data'); |
---|
256 | return $result; |
---|
257 | } |
---|