1 | <?php |
---|
2 | /** |
---|
3 | * Sistema Automatizado para la Planificación Estratégico-Situacional en la Administración Pública Venezolana |
---|
4 | * @file cck_plan_fields_simple.module |
---|
5 | * Drupal part Module to code ente planificador module |
---|
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 - Msc. Juan Vizcarrondo |
---|
23 | * @date 2013-08-03 // (año-mes-dia) |
---|
24 | * @version 0.2 // (0.2) |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | /** |
---|
29 | * Implementation of hook_field_info(). |
---|
30 | */ |
---|
31 | function cck_plan_fields_simple_field_info() { |
---|
32 | return array( |
---|
33 | 'cck_plan_fields_simple_field' => array( |
---|
34 | 'label' => t('Plan line field simple'), |
---|
35 | 'description' => t('Store items operative plan data in the database.'), |
---|
36 | ), |
---|
37 | ); |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * Implementation of hook_field_settings(). |
---|
42 | */ |
---|
43 | function cck_plan_fields_simple_field_settings($op, $field) { |
---|
44 | switch ($op) { |
---|
45 | case 'form': |
---|
46 | $form = array(); |
---|
47 | $types = array( |
---|
48 | 'int' => t('Integer'), |
---|
49 | 'float' => t('Floar'), |
---|
50 | ); |
---|
51 | $form['cck_plan_fields_simple_type'] = array( |
---|
52 | '#type' => 'select', |
---|
53 | '#title' => t('Type'), |
---|
54 | '#default_value' => !empty($field['cck_plan_fields_simple_type']) ? $field['cck_plan_fields_simple_type'] : 'int', |
---|
55 | '#options' => $types, |
---|
56 | ); |
---|
57 | $form['cck_plan_fields_simple_titulo'] = array( |
---|
58 | '#type' => 'textfield', |
---|
59 | '#title' => t('Titulo del total'), |
---|
60 | //'#required' => TRUE, |
---|
61 | '#weight' => -9, |
---|
62 | '#default_value' => !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : '', |
---|
63 | ); |
---|
64 | return $form; |
---|
65 | case 'save': |
---|
66 | $save_settings = array( |
---|
67 | 'cck_plan_fields_simple_type', |
---|
68 | 'cck_plan_fields_simple_titulo', |
---|
69 | ); |
---|
70 | return $save_settings; |
---|
71 | case 'database columns': |
---|
72 | $type_select = !empty($field['cck_plan_fields_simple_type']) ? $field['cck_plan_fields_simple_type'] : 'int'; |
---|
73 | $types = array( |
---|
74 | 'int' => t('Integer'), |
---|
75 | 'float' => t('Floar'), |
---|
76 | ); |
---|
77 | $type_select = isset($types[$type_select]) ? $type_select : 'int'; |
---|
78 | for ($i = 0; $i < 12; $i++) { |
---|
79 | $field_id = $i ? 'value_' . $i : 'value'; |
---|
80 | if ($type_select == 'int') { |
---|
81 | $columns[$field_id] = array( |
---|
82 | 'type' => $type_select, |
---|
83 | 'not null' => FALSE, |
---|
84 | 'sortable' => TRUE, |
---|
85 | 'views' => TRUE, |
---|
86 | ); |
---|
87 | } |
---|
88 | else { |
---|
89 | $columns[$field_id] = array( |
---|
90 | 'type' => 'numeric', |
---|
91 | 'size' => 'normal', |
---|
92 | 'not null' => TRUE, |
---|
93 | 'default' => 0, |
---|
94 | 'precision' => 32, |
---|
95 | 'scale' => 0, |
---|
96 | ); |
---|
97 | } |
---|
98 | |
---|
99 | } |
---|
100 | return $columns; |
---|
101 | case 'views data': |
---|
102 | $data = content_views_field_views_data($field); |
---|
103 | $db_info = content_database_info($field); |
---|
104 | $table_alias = content_views_tablename($field); |
---|
105 | $ftypes = array( |
---|
106 | 'value' => t('January'), |
---|
107 | 'value_1' => t('February'), |
---|
108 | 'value_2' => t('March'), |
---|
109 | 'value_3' => t('April'), |
---|
110 | 'value_4' => t('May'), |
---|
111 | 'value_5' => t('June'), |
---|
112 | 'value_6' => t('July'), |
---|
113 | 'value_7' => t('August'), |
---|
114 | 'value_8' => t('September'), |
---|
115 | 'value_9' => t('Octuber'), |
---|
116 | 'value_10' => t('November'), |
---|
117 | 'value_11' => t('December'), |
---|
118 | ); |
---|
119 | foreach ($ftypes as $ftype => $label) { |
---|
120 | $copy = $data[$table_alias][$field['field_name'] . $ftype]; |
---|
121 | $copy['title'] = t($label); |
---|
122 | $copy['filter']['handler'] = 'content_handler_filter_many_to_one'; |
---|
123 | $copy['filter']['numeric'] = TRUE; |
---|
124 | unset($copy['field'], $copy['argument'], $copy['sort']); |
---|
125 | $data[$table_alias][$field['field_name'] . $ftype . '_many_to_one'] = $copy; |
---|
126 | $data[$table_alias][$field['field_name'] . $ftype]['argument']['handler'] = 'content_handler_argument_many_to_one'; |
---|
127 | if ($ftype != 'description') { |
---|
128 | $data[$table_alias][$field['field_name'] . $ftype]['argument']['numeric'] = TRUE; |
---|
129 | } |
---|
130 | } |
---|
131 | return $data; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Implementation of hook_content_is_empty(). |
---|
137 | */ |
---|
138 | function cck_plan_fields_simple_content_is_empty($item, $field) { |
---|
139 | $flat = TRUE; |
---|
140 | foreach (array_keys($field['columns']) as $ftype) { |
---|
141 | if (!empty($item[$ftype])) { |
---|
142 | return FALSE; |
---|
143 | } |
---|
144 | } |
---|
145 | return $flat; |
---|
146 | } |
---|
147 | |
---|
148 | /** |
---|
149 | * Implementation of hook_field_formatter_info(). |
---|
150 | */ |
---|
151 | function cck_plan_fields_simple_field_formatter_info() { |
---|
152 | $formatters = array( |
---|
153 | 'default' => array( |
---|
154 | 'label' => t('Default'), |
---|
155 | 'multiple values' => CONTENT_HANDLE_CORE, |
---|
156 | 'field types' => array('cck_plan_fields_simple_field'), |
---|
157 | ), |
---|
158 | 'single_line' => array( |
---|
159 | 'label' => t('Single Line'), |
---|
160 | 'multiple values' => CONTENT_HANDLE_CORE, |
---|
161 | 'field types' => array('cck_plan_fields_simple_field'), |
---|
162 | ), |
---|
163 | ); |
---|
164 | $ftypes = array( |
---|
165 | 'value' => t('January'), |
---|
166 | 'value_1' => t('February'), |
---|
167 | 'value_2' => t('March'), |
---|
168 | 'value_3' => t('April'), |
---|
169 | 'value_4' => t('May'), |
---|
170 | 'value_5' => t('June'), |
---|
171 | 'value_6' => t('July'), |
---|
172 | 'value_7' => t('August'), |
---|
173 | 'value_8' => t('September'), |
---|
174 | 'value_9' => t('Octuber'), |
---|
175 | 'value_10' => t('November'), |
---|
176 | 'value_11' => t('December'), |
---|
177 | ); |
---|
178 | foreach ($ftypes as $value => $label) { |
---|
179 | $formatters['single_line_' . $value] = array( |
---|
180 | 'label' => t('Single Line ') . $label, |
---|
181 | 'multiple values' => CONTENT_HANDLE_CORE, |
---|
182 | 'field types' => array('cck_plan_fields_simple_field'), |
---|
183 | ); |
---|
184 | } |
---|
185 | return $formatters; |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * Implementation of hook_theme(). |
---|
190 | */ |
---|
191 | function cck_plan_fields_simple_theme() { |
---|
192 | return array( |
---|
193 | 'cck_plan_fields_simple_formatter_default' => array( |
---|
194 | 'arguments' => array('element'), |
---|
195 | ), |
---|
196 | 'cck_plan_fields_simple_formatter_single_line' => array( |
---|
197 | 'arguments' => array('element'), |
---|
198 | ), |
---|
199 | 'cck_plan_fields_simple_formatter_single_line_value' => array( |
---|
200 | 'arguments' => array('form' => NULL), |
---|
201 | 'function' => 'theme_cck_plan_fields_simple_formatter_generic', |
---|
202 | ), |
---|
203 | 'cck_plan_fields_simple_formatter_single_line_value1' => array( |
---|
204 | 'arguments' => array('form' => NULL), |
---|
205 | 'function' => 'theme_cck_plan_fields_simple_formatter_generic', |
---|
206 | ), |
---|
207 | 'cck_plan_fields_simple_formatter_single_line_value2' => array( |
---|
208 | 'arguments' => array('form' => NULL), |
---|
209 | 'function' => 'theme_cck_plan_fields_simple_formatter_generic', |
---|
210 | ), |
---|
211 | 'cck_plan_fields_simple_formatter_single_line_value3' => array( |
---|
212 | 'arguments' => array('form' => NULL), |
---|
213 | 'function' => 'theme_cck_plan_fields_simple_formatter_generic' |
---|
214 | ), |
---|
215 | 'cck_plan_fields_simple_formatter_single_line_value4' => array( |
---|
216 | 'arguments' => array('form' => NULL), |
---|
217 | 'function' => 'theme_cck_plan_fields_simple_formatter_generic', |
---|
218 | ), |
---|
219 | 'cck_plan_fields_simple_formatter_single_line_value5' => array( |
---|
220 | 'arguments' => array('form' => NULL), |
---|
221 | 'function' => 'theme_cck_plan_fields_simple_formatter_generic', |
---|
222 | ), |
---|
223 | 'cck_plan_fields_simple_formatter_single_line_value6' => array( |
---|
224 | 'arguments' => array('form' => NULL), |
---|
225 | 'function' => 'theme_cck_plan_fields_simple_formatter_generic', |
---|
226 | ), |
---|
227 | 'cck_plan_fields_simple_formatter_single_line_value7' => array( |
---|
228 | 'arguments' => array('form' => NULL), |
---|
229 | 'function' => 'theme_cck_plan_fields_simple_formatter_generic', |
---|
230 | ), |
---|
231 | 'cck_plan_fields_simple_formatter_single_line_value8' => array( |
---|
232 | 'arguments' => array('form' => NULL), |
---|
233 | 'function' => 'theme_cck_plan_fields_simple_formatter_generic', |
---|
234 | ), |
---|
235 | 'cck_plan_fields_simple_formatter_single_line_value9' => array( |
---|
236 | 'arguments' => array('form' => NULL), |
---|
237 | 'function' => 'theme_cck_plan_fields_simple_formatter_generic', |
---|
238 | ), |
---|
239 | 'cck_plan_fields_simple_formatter_single_line_value10' => array( |
---|
240 | 'arguments' => array('form' => NULL), |
---|
241 | 'function' => 'theme_cck_plan_fields_simple_formatter_generic', |
---|
242 | ), |
---|
243 | 'cck_plan_fields_simple_formatter_single_line_value11' => array( |
---|
244 | 'arguments' => array('form' => NULL), |
---|
245 | 'function' => 'theme_cck_plan_fields_simple_formatter_generic', |
---|
246 | ), |
---|
247 | 'cck_plan_fields_simple_table' => array( |
---|
248 | 'arguments' => array('form' => NULL), |
---|
249 | ), |
---|
250 | ); |
---|
251 | } |
---|
252 | |
---|
253 | /** |
---|
254 | * Proxy theme function for cck_plan_fields_simple formatters. |
---|
255 | */ |
---|
256 | function theme_cck_plan_fields_simple_formatter_generic($element) { |
---|
257 | $output = ''; |
---|
258 | $format_number = array( |
---|
259 | 'decimals' => variable_get('proyectos_operativos_number_decimals', 0), |
---|
260 | 'dec_point' => variable_get('proyectos_operativos_number_dec_point', ','), |
---|
261 | 'thousands_sep' => variable_get('proyectos_operativos_number_thousands_sep', '.'), |
---|
262 | ); |
---|
263 | $flag = explode('single_line_', $element['#formatter']); |
---|
264 | $ftypes = array( |
---|
265 | 'value' => t('January'), |
---|
266 | 'value_1' => t('February'), |
---|
267 | 'value_2' => t('March'), |
---|
268 | 'value_3' => t('April'), |
---|
269 | 'value_4' => t('May'), |
---|
270 | 'value_5' => t('June'), |
---|
271 | 'value_6' => t('July'), |
---|
272 | 'value_7' => t('August'), |
---|
273 | 'value_8' => t('September'), |
---|
274 | 'value_9' => t('Octuber'), |
---|
275 | 'value_10' => t('November'), |
---|
276 | 'value_11' => t('December'), |
---|
277 | ); |
---|
278 | $output = '<strong>'. $ftypes[$flag[1]] .': </strong>'. number_format($element['#item'][$flag[1]], $format_number['decimals'], $format_number['dec_point'], $format_number['thousands_sep']); |
---|
279 | return $output; |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | * theme_cck_plan_fields_formatter_default(). |
---|
284 | * default formatter theme |
---|
285 | */ |
---|
286 | function theme_cck_plan_fields_simple_formatter_default($element) { |
---|
287 | $output = ''; |
---|
288 | $ftypes = array( |
---|
289 | 'value' => t('January'), |
---|
290 | 'value_1' => t('February'), |
---|
291 | 'value_2' => t('March'), |
---|
292 | 'value_3' => t('April'), |
---|
293 | 'value_4' => t('May'), |
---|
294 | 'value_5' => t('June'), |
---|
295 | 'value_6' => t('July'), |
---|
296 | 'value_7' => t('August'), |
---|
297 | 'value_8' => t('September'), |
---|
298 | 'value_9' => t('Octuber'), |
---|
299 | 'value_10' => t('November'), |
---|
300 | 'value_11' => t('December'), |
---|
301 | ); |
---|
302 | $format_number = array( |
---|
303 | 'decimals' => variable_get('proyectos_operativos_number_decimals', 0), |
---|
304 | 'dec_point' => variable_get('proyectos_operativos_number_dec_point', ','), |
---|
305 | 'thousands_sep' => variable_get('proyectos_operativos_number_thousands_sep', '.'), |
---|
306 | ); |
---|
307 | $ftypes['total'] = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Total'); |
---|
308 | $headers = array(); |
---|
309 | $rows = array(); |
---|
310 | $row = array(); |
---|
311 | $empty = TRUE; |
---|
312 | $total = 0; |
---|
313 | foreach ($ftypes as $value => $label) { |
---|
314 | $headers[] = array('data' => $label); |
---|
315 | if (!empty($element['#item'][$value])) { |
---|
316 | $empty = FALSE; |
---|
317 | } |
---|
318 | $total += $element['#item'][$value]; |
---|
319 | if ($value != 'total') { |
---|
320 | $row[] = array('data' => number_format($element['#item'][$value], $format_number['decimals'], $format_number['dec_point'], $format_number['thousands_sep'])); |
---|
321 | } |
---|
322 | } |
---|
323 | if ($empty) { |
---|
324 | return ''; |
---|
325 | } |
---|
326 | $row[] = array('data' => number_format($total, $format_number['decimals'], $format_number['dec_point'], $format_number['thousands_sep'])); |
---|
327 | $rows[] = $row; |
---|
328 | return theme('table', $headers, $rows); |
---|
329 | } |
---|
330 | |
---|
331 | /** |
---|
332 | * theme_cck_plan_fields_formatter_single_line(). |
---|
333 | * display line items products in a single line |
---|
334 | */ |
---|
335 | function theme_cck_plan_fields_simple_formatter_single_line($element) { |
---|
336 | $output = ''; |
---|
337 | // If all fields are hidden, return '' |
---|
338 | if (empty($element)) { |
---|
339 | return $output; |
---|
340 | } |
---|
341 | $ftypes = array( |
---|
342 | 'value' => t('January'), |
---|
343 | 'value_1' => t('February'), |
---|
344 | 'value_2' => t('March'), |
---|
345 | 'value_3' => t('April'), |
---|
346 | 'value_4' => t('May'), |
---|
347 | 'value_5' => t('June'), |
---|
348 | 'value_6' => t('July'), |
---|
349 | 'value_7' => t('August'), |
---|
350 | 'value_8' => t('September'), |
---|
351 | 'value_9' => t('Octuber'), |
---|
352 | 'value_10' => t('November'), |
---|
353 | 'value_11' => t('December'), |
---|
354 | ); |
---|
355 | $format_number = array( |
---|
356 | 'decimals' => variable_get('proyectos_operativos_number_decimals', 0), |
---|
357 | 'dec_point' => variable_get('proyectos_operativos_number_dec_point', ','), |
---|
358 | 'thousands_sep' => variable_get('proyectos_operativos_number_thousands_sep', '.'), |
---|
359 | ); |
---|
360 | foreach ($ftypes as $value => $label) { |
---|
361 | $output .= ' <strong>'. $label .': </strong>'. number_format($element['#item'][$value], $format_number['decimals'], $format_number['dec_point'], $format_number['thousands_sep']); |
---|
362 | } |
---|
363 | return '<div class="items-plan-field">'. $output .'</div>'; |
---|
364 | } |
---|
365 | |
---|
366 | /** |
---|
367 | * Implementation of hook_widget_info(). |
---|
368 | */ |
---|
369 | function cck_plan_fields_simple_widget_info() { |
---|
370 | return array( |
---|
371 | 'cck_plan_fields_simple_elements' => array( |
---|
372 | 'label' => t('Items Plan Field Simple'), |
---|
373 | 'field types' => array('cck_plan_fields_simple_field'), |
---|
374 | 'multiple values' => CONTENT_HANDLE_CORE, |
---|
375 | 'callbacks' => array('default value' => CONTENT_CALLBACK_DEFAULT), |
---|
376 | ), |
---|
377 | ); |
---|
378 | } |
---|
379 | |
---|
380 | /** |
---|
381 | * Implementation of hook_widget_settings(). |
---|
382 | */ |
---|
383 | function cck_plan_fields_simple_widget_settings($op, $widget) { |
---|
384 | switch ($op) { |
---|
385 | case 'form': |
---|
386 | $form = array(); |
---|
387 | $form['min'] = array( |
---|
388 | '#type' => 'textfield', |
---|
389 | '#title' => t('Minimum'), |
---|
390 | '#element_validate' => array('_element_validate_number'), |
---|
391 | '#default_value' => is_numeric($widget['min']) ? $widget['min'] : '', |
---|
392 | ); |
---|
393 | $form['max'] = array( |
---|
394 | '#type' => 'textfield', |
---|
395 | '#title' => t('Maximum'), |
---|
396 | '#element_validate' => array('_element_validate_number'), |
---|
397 | '#default_value' => is_numeric($widget['max']) ? $widget['max'] : '', |
---|
398 | ); |
---|
399 | return $form; |
---|
400 | break; |
---|
401 | case 'save': |
---|
402 | return array( |
---|
403 | 'min', |
---|
404 | 'max', |
---|
405 | ); |
---|
406 | break; |
---|
407 | } |
---|
408 | } |
---|
409 | |
---|
410 | function theme_cck_plan_fields_simple_table($form = array()) { |
---|
411 | $headers = array(); |
---|
412 | $rows = array(); |
---|
413 | $row = array(); |
---|
414 | $ftypes = array( |
---|
415 | 'value' => t('Ene'), |
---|
416 | 'value_1' => t('Feb'), |
---|
417 | 'value_2' => t('Mar'), |
---|
418 | 'value_3' => t('Abr'), |
---|
419 | 'value_4' => t('May'), |
---|
420 | 'value_5' => t('Jun'), |
---|
421 | 'value_6' => t('Jul'), |
---|
422 | 'value_7' => t('Aug'), |
---|
423 | 'value_8' => t('Sep'), |
---|
424 | 'value_9' => t('Oct'), |
---|
425 | 'value_10' => t('Nov'), |
---|
426 | 'value_11' => t('Dic'), |
---|
427 | ); |
---|
428 | $ftypes['total'] = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Total'); |
---|
429 | $backgrounds = array( |
---|
430 | 'value' => 'black', |
---|
431 | 'value_1' => 'blue', |
---|
432 | 'value_2' => 'white', |
---|
433 | 'value_3' => 'blue', |
---|
434 | 'value_4' => 'white', |
---|
435 | 'value_5' => 'blue', |
---|
436 | 'value_6' => 'white', |
---|
437 | 'value_7' => 'blue', |
---|
438 | 'value_8' => 'white', |
---|
439 | 'value_9' => 'blue', |
---|
440 | 'value_10' => 'white', |
---|
441 | 'value_11' => 'blue', |
---|
442 | ); |
---|
443 | if (isset($form['#title']) && $form['#title']) { |
---|
444 | $field = str_replace('_', '-', $form['#field_name']); |
---|
445 | $output .= '<div id="edit-' . $field . '-0-value-wrapper" class="form-item"><label for="edit-' . $field . '-0-value">' . $form['#title'] . '</label>'; |
---|
446 | } |
---|
447 | $output .= '<div style="width:2050px; float:none">'; |
---|
448 | foreach ($ftypes as $ftype => $label) { |
---|
449 | $output .= '<div style="width:150px;float:left;margin-left:5px">' . drupal_render($form[$ftype]) . '</div>'; |
---|
450 | } |
---|
451 | $output .= drupal_render($form); |
---|
452 | $output .= '</div>'; |
---|
453 | if (isset($form['#title']) && $form['#title']) { |
---|
454 | $output .= '</div>'; |
---|
455 | } |
---|
456 | return $output; |
---|
457 | } |
---|
458 | |
---|
459 | /** |
---|
460 | * Implementation of hook_widget(). |
---|
461 | */ |
---|
462 | function cck_plan_fields_simple_widget(&$form, &$form_state, $field, $items, $delta = 0) { |
---|
463 | $cck_plan_fields_simple_path = drupal_get_path('module', 'cck_plan_fields_simple'); |
---|
464 | drupal_add_js($cck_plan_fields_simple_path . '/js/cck_plan_fields.js'); |
---|
465 | $field_name = $field['field_name']; |
---|
466 | $element = array(); |
---|
467 | $ftypes = array( |
---|
468 | 'value' => t('Ene'), |
---|
469 | 'value_1' => t('Feb'), |
---|
470 | 'value_2' => t('Mar'), |
---|
471 | 'value_3' => t('Abr'), |
---|
472 | 'value_4' => t('May'), |
---|
473 | 'value_5' => t('Jun'), |
---|
474 | 'value_6' => t('Jul'), |
---|
475 | 'value_7' => t('Aug'), |
---|
476 | 'value_8' => t('Sep'), |
---|
477 | 'value_9' => t('Oct'), |
---|
478 | 'value_10' => t('Nov'), |
---|
479 | 'value_11' => t('Dic'), |
---|
480 | ); |
---|
481 | $ftypes['total'] = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Total'); |
---|
482 | $total = 0; |
---|
483 | $class = $field_name . '_' . $delta . '_field'; |
---|
484 | for ($i = 0; $i < 12; $i++) { |
---|
485 | $class1 = ' ' . $field_name . '_m' . $i . '_field'; |
---|
486 | $field_id = $i ? 'value_' . $i : 'value'; |
---|
487 | $field_id_form = $i ? 'value-' . $i : 'value'; |
---|
488 | $element[$field_id] = array( |
---|
489 | '#type' => 'textfield', |
---|
490 | '#default_value' => isset($items[$delta][$field_id]) && $items[$delta][$field_id] ? $items[$delta][$field_id] : 0, |
---|
491 | '#size' => 15, |
---|
492 | '#attributes' => array('class' => $class . $class1 . ' number', 'onchange' => "suma('$class')"), |
---|
493 | ); |
---|
494 | $total += $element[$field_id]['#default_value']; |
---|
495 | if ($delta == 0) { |
---|
496 | $element[$field_id]['#title'] = $ftypes[$field_id]; |
---|
497 | } |
---|
498 | } |
---|
499 | $element['total'] = array( |
---|
500 | '#type' => 'textfield', |
---|
501 | '#default_value' => number_format($total, 0, '.', ''), |
---|
502 | '#size' => 15, |
---|
503 | '#attributes' => array('class' => $class . '_total totales'), |
---|
504 | ); |
---|
505 | if ($delta == 0) { |
---|
506 | $element['total']['#title'] = $ftypes['total']; |
---|
507 | } |
---|
508 | $element['#theme'] = 'cck_plan_fields_simple_table'; |
---|
509 | if (empty($element['#element_validate'])) { |
---|
510 | $element['#element_validate'] = array(); |
---|
511 | } |
---|
512 | array_unshift($element['#element_validate'], 'cck_plan_fields_simple_validate'); |
---|
513 | $form_state['#field_info'][$element['#field_name']] = $form['#field_info'][$field_name]; |
---|
514 | // Used so that hook_field('validate') knows where to |
---|
515 | // flag an error in deeply nested forms. |
---|
516 | if (empty($form['#parents'])) { |
---|
517 | $form['#parents'] = array(); |
---|
518 | } |
---|
519 | $element['_error_element'] = array( |
---|
520 | '#type' => 'value', |
---|
521 | '#value' => implode('][', array_merge($form['#parents'], array('value'))), |
---|
522 | ); |
---|
523 | return $element; |
---|
524 | } |
---|
525 | |
---|
526 | /** |
---|
527 | * FAPI validation of an individual element. |
---|
528 | */ |
---|
529 | function cck_plan_fields_simple_validate($element, &$form_state) { |
---|
530 | $field_name = $element['#field_name']; |
---|
531 | if (!isset($element['_remove']['#post'][$field_name][$element['#delta']]['_remove'])) { |
---|
532 | $type_name = $element['#type_name']; |
---|
533 | $field = content_fields($field_name, $type_name); |
---|
534 | $min = is_numeric($field['widget']['min']); |
---|
535 | $max = is_numeric($field['widget']['max']); |
---|
536 | $cck_plan_fields_simple_path = drupal_get_path('module', 'cck_plan_fields_simple'); |
---|
537 | drupal_add_js($cck_plan_fields_simple_path . '/js/cck_plan_fields.js'); |
---|
538 | $flag = FALSE; |
---|
539 | foreach ($element['#columns'] as $ftype) { |
---|
540 | if (!empty($element[$ftype]['#value'])) { |
---|
541 | $error_field = implode('][', $element['#parents']) .'][' . $ftype; |
---|
542 | if (!is_numeric($element[$ftype]['#value'])) { |
---|
543 | form_set_error($error_field, t('Amount should be a number in %field.', array('%field' => t($field['widget']['label'])))); |
---|
544 | } |
---|
545 | elseif ($field['cck_plan_fields_simple_type'] == 'int') { |
---|
546 | $start = $element[$ftype]['#value']; |
---|
547 | $value = preg_replace('@[^-0-9]@', '', $start); |
---|
548 | if ($start != $value) { |
---|
549 | form_set_error($error_field, t('Only numbers are allowed in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label'])))); |
---|
550 | } |
---|
551 | } |
---|
552 | elseif ($min && $field['widget']['min'] > $element[$ftype]['#value']) { |
---|
553 | form_set_error($error_field, t('Amount should be greater %number in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label'])))); |
---|
554 | } |
---|
555 | elseif ($max && $field['widget']['max'] < $element[$ftype]['#value']) { |
---|
556 | form_set_error($error_field, t('Amount should be litter %number in %field.', array('%number' => $field['widget']['max'], '%field' => t($field['widget']['label'])))); |
---|
557 | } |
---|
558 | $flag = TRUE; |
---|
559 | } |
---|
560 | } |
---|
561 | if (!$flag) { |
---|
562 | return; |
---|
563 | } |
---|
564 | } |
---|
565 | } |
---|
566 | |
---|
567 | |
---|
568 | /** |
---|
569 | * Implementation of hook_feeds_node_processor_targets_alter(). |
---|
570 | * |
---|
571 | * @see FeedsNodeProcessor::getMappingTargets() |
---|
572 | */ |
---|
573 | function cck_plan_fields_simple_feeds_node_processor_targets_alter(&$targets, $content_type) { |
---|
574 | $info = content_types($content_type); |
---|
575 | $fields = array(); |
---|
576 | if (isset($info['fields']) && count($info['fields'])) { |
---|
577 | foreach ($info['fields'] as $field_name => $field) { |
---|
578 | if (in_array($field['type'], array('cck_plan_fields_simple_field'))) { |
---|
579 | $fields[$field_name] = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name; |
---|
580 | } |
---|
581 | } |
---|
582 | } |
---|
583 | $ftypes = array( |
---|
584 | 'value' => t('January'), |
---|
585 | 'value_1' => t('February'), |
---|
586 | 'value_2' => t('March'), |
---|
587 | 'value_3' => t('April'), |
---|
588 | 'value_4' => t('May'), |
---|
589 | 'value_5' => t('June'), |
---|
590 | 'value_6' => t('July'), |
---|
591 | 'value_7' => t('August'), |
---|
592 | 'value_8' => t('September'), |
---|
593 | 'value_9' => t('Octuber'), |
---|
594 | 'value_10' => t('November'), |
---|
595 | 'value_11' => t('December'), |
---|
596 | ); |
---|
597 | |
---|
598 | foreach ($fields as $k => $name) { |
---|
599 | foreach($ftypes as $id => $month) { |
---|
600 | $targets[$k . ':' . $id] = array( |
---|
601 | 'name' => check_plain($name) . '(' . $month . ')', |
---|
602 | 'callback' => 'cck_plan_fields_simple_feeds_set_target', |
---|
603 | 'description' => t('The part @month_name of @name field of the node.', array('@name' => $name, '@month_name' => $month)), |
---|
604 | ); |
---|
605 | } |
---|
606 | } |
---|
607 | } |
---|
608 | |
---|
609 | function cck_plan_fields_simple_feeds_set_target($node, $target, $value) { |
---|
610 | list($field_name, $sub_field) = explode(':', $target); |
---|
611 | $ftypes = array( |
---|
612 | 'value' => t('January'), |
---|
613 | 'value_1' => t('February'), |
---|
614 | 'value_2' => t('March'), |
---|
615 | 'value_3' => t('April'), |
---|
616 | 'value_4' => t('May'), |
---|
617 | 'value_5' => t('June'), |
---|
618 | 'value_6' => t('July'), |
---|
619 | 'value_7' => t('August'), |
---|
620 | 'value_8' => t('September'), |
---|
621 | 'value_9' => t('Octuber'), |
---|
622 | 'value_10' => t('November'), |
---|
623 | 'value_11' => t('December'), |
---|
624 | ); |
---|
625 | $field_default = array(); |
---|
626 | foreach($ftypes as $id => $month) { |
---|
627 | $field_default[$id] = 0; |
---|
628 | } |
---|
629 | $field = isset($node->$field_name) ? $node->$field_name : $field_default; |
---|
630 | // Handle multiple value fields. |
---|
631 | if (is_array($value)) { |
---|
632 | $i = 0; |
---|
633 | foreach ($value as $v) { |
---|
634 | if (!is_array($v) && !is_object($v)) { |
---|
635 | $field[$i][$sub_field] = $v; |
---|
636 | } |
---|
637 | $i++; |
---|
638 | } |
---|
639 | } |
---|
640 | else { |
---|
641 | $field[0][$sub_field] = $value; |
---|
642 | } |
---|
643 | $node->$field_name = $field; |
---|
644 | } |
---|