source: sipes/0.3-modules/cck_plan_fields/cck_plan_fields.module @ b9d4e2e

stable
Last change on this file since b9d4e2e was f9bf786, checked in by Sipes Apn <root@…>, 7 años ago

se corrigio el error presentado al momento de eliminar un valor

  • Propiedad mode establecida a 100755
File size: 26.1 KB
Línea 
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.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&#241;o-mes-dia)
24  * @version 0.2 // (0.2)
25  *
26  */
27
28/**
29* Implementation of hook_field_info().
30*/
31function cck_plan_fields_field_info() {
32  return array(
33    'cck_plan_fields_field' => array(
34      'label' => t('Plan line field'),
35      'description' => t('Store items operative plan data in the database.'),
36    ),
37  );
38}
39
40/**
41* Implementation of hook_field_settings().
42*/
43function cck_plan_fields_field_settings($op, $field) {
44  switch ($op) {
45    case 'form':
46      $form = array();
47      $vocabularies = taxonomy_get_vocabularies();
48      $vocabulary_options = array();
49      foreach($vocabularies as $vocabulary) {
50        $vocabulary_options[$vocabulary->vid] = $vocabulary->name;
51      }
52      $form['vid'] = array(
53        '#type' => 'select',
54        '#title' => t('Taxonomy'),
55        '#default_value' => !empty($field['vid']) ? $field['vid'] : '',
56        '#options' => $vocabulary_options,
57      );
58      if (!empty($field['vid'])) {
59        $vid = $field['vid'];
60        //$terms = taxonomy_get_tree($vid);
61        $tree = taxonomy_get_tree($vid);
62        $options = array();
63        if ($tree) {
64          foreach ($tree as $term) {
65              $choice = new stdClass();
66              $choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name);
67              $options[] = $choice;
68          }
69        }
70        $form['vtid'] = array(
71          '#type' => 'select',
72          '#default_value' => isset($field['vtid']) ? $field['vtid'] : array(),
73          '#options' => $options,
74          '#multiple' => TRUE,
75          );
76      }
77      $types = array(
78        'int' => t('Integer'),
79        'float' => t('Float'),
80      );
81      $form['cck_plan_fields_simple_type'] = array(
82        '#type' => 'select',
83        '#title' => t('Type'),
84        '#default_value' => !empty($field['cck_plan_fields_simple_type']) ? $field['cck_plan_fields_simple_type'] : 'int',
85        '#options' => $types,
86      );
87      $form['cck_plan_fields_simple_titulo'] = array(
88        '#type' => 'textfield',
89        '#title' => t('Titulo del total'),
90        //'#required' => TRUE,
91        '#weight' => -9,
92        '#default_value' => !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : '',
93      );
94      return $form;
95    case 'save':
96      $save_settings = array(
97        'vid',
98        'vtid',
99        'cck_plan_fields_simple_type',
100        'cck_plan_fields_simple_titulo',
101      );
102      return $save_settings;
103    case 'database columns':
104      $columns['tid'] = array(
105        'type' => 'int',
106        'not null' => FALSE,
107        'sortable' => TRUE,
108        'views' => TRUE,
109      );
110      for ($i = 0; $i < 12; $i++) {
111        $field_id = $i ? 'value_' . $i : 'value';
112        $columns[$field_id] = array(
113          'type' => 'numeric',
114          'size' => 'normal',
115          'not null' => TRUE,
116          'default' => 0,
117          'precision' => 32,
118          'scale' => 0,
119        );
120      }
121      return $columns;
122    case 'views data':
123      $data = content_views_field_views_data($field);
124      $db_info = content_database_info($field);
125      $table_alias = content_views_tablename($field);
126      $ftypes = array(
127        'tid' => t('Plan Account'),
128        'value' => t('January'),
129        'value_1' => t('February'),
130        'value_2' => t('March'),
131        'value_3' => t('April'),
132        'value_4' => t('May'),
133        'value_5' => t('June'),
134        'value_6' => t('July'),
135        'value_7' => t('August'),
136        'value_8' => t('September'),
137        'value_9' => t('Octuber'),
138        'value_10' => t('November'),
139        'value_11' => t('December'),
140      );
141      foreach ($ftypes as $ftype => $label) {
142        $copy = $data[$table_alias][$field['field_name'] . $ftype];
143        $copy['title'] = t($label);
144        $copy['filter']['handler'] = 'content_handler_filter_many_to_one';
145        $copy['filter']['numeric'] = TRUE;
146        unset($copy['field'], $copy['argument'], $copy['sort']);
147        $data[$table_alias][$field['field_name'] . $ftype . '_many_to_one'] = $copy;
148        $data[$table_alias][$field['field_name'] . $ftype]['argument']['handler'] = 'content_handler_argument_many_to_one';
149        if ($ftype != 'description') {
150          $data[$table_alias][$field['field_name'] . $ftype]['argument']['numeric'] = TRUE;
151        }
152      }
153      return $data;
154  }
155}
156
157/**
158 * Implementation of hook_content_is_empty().
159 */
160function cck_plan_fields_content_is_empty($item, $field) {
161  $flat = TRUE;
162  foreach (array_keys($field['columns']) as $ftype) {
163    if ($ftype != 'tid' && !empty($item[$ftype])) {
164      $flat = FALSE;
165    }
166  }
167  return $flat;
168}
169
170/**
171 * Implementation of hook_field_formatter_info().
172 */
173function cck_plan_fields_field_formatter_info() {
174  $formatters = array(
175    'default' => array(
176      'label'  => t('Default'),
177      'multiple values' => CONTENT_HANDLE_CORE,
178      'field types'  => array('cck_plan_fields_field'),
179    ),
180    'single_line' => array(
181      'label'  => t('Single Line'),
182      'multiple values' => CONTENT_HANDLE_CORE,
183      'field types'  => array('cck_plan_fields_field'),
184    ),
185  );
186  $ftypes = array(
187    'tid' => t('Partidas'),
188    'value' => t('January'),
189    'value_1' => t('February'),
190    'value_2' => t('March'),
191    'value_3' => t('April'),
192    'value_4' => t('May'),
193    'value_5' => t('June'),
194    'value_6' => t('July'),
195    'value_7' => t('August'),
196    'value_8' => t('September'),
197    'value_9' => t('Octuber'),
198    'value_10' => t('November'),
199    'value_11' => t('December'),
200  );
201  foreach ($ftypes as $value => $label) {
202    $formatters['single_line_' . $value] = array(
203      'label'  => t('Single Line ') . $label,
204      'multiple values' => CONTENT_HANDLE_CORE,
205      'field types'  => array('cck_plan_fields_field'),
206    );
207  }
208  return $formatters;
209}
210
211/**
212 * Implementation of hook_theme().
213 */
214function cck_plan_fields_theme() {
215  return array(
216    // Shows address in the default view: Multilines
217    'cck_plan_fields_formatter_default' => array(
218      'arguments' => array('element'),
219    ),
220    // Shows address in only one line
221    'cck_plan_fields_formatter_single_line' => array(
222      'arguments' => array('element'),
223    ),
224    'cck_plan_fields_formatter_single_line_tid' => array(
225      'arguments' => array('form' => NULL),
226      'function' => 'theme_cck_plan_fields_formatter_generic',
227    ),
228    'cck_plan_fields_formatter_single_line_value' => array(
229      'arguments' => array('form' => NULL),
230      'function' => 'theme_cck_plan_fields_formatter_generic',
231    ),
232    'cck_plan_fields_formatter_single_line_value1' => array(
233      'arguments' => array('form' => NULL),
234      'function' => 'theme_cck_plan_fields_formatter_generic',
235    ),
236    'cck_plan_fields_formatter_single_line_value2' => array(
237      'arguments' => array('form' => NULL),
238      'function' => 'theme_cck_plan_fields_formatter_generic',
239    ),
240    'cck_plan_fields_formatter_single_line_value3' => array(
241      'arguments' => array('form' => NULL),
242      'function' => 'theme_cck_plan_fields_formatter_generic'
243    ),
244    'cck_plan_fields_formatter_single_line_value4' => array(
245      'arguments' => array('form' => NULL),
246      'function' => 'theme_cck_plan_fields_formatter_generic',
247    ),
248    'cck_plan_fields_formatter_single_line_value5' => array(
249      'arguments' => array('form' => NULL),
250      'function' => 'theme_cck_plan_fields_formatter_generic',
251    ),
252    'cck_plan_fields_formatter_single_line_value6' => array(
253      'arguments' => array('form' => NULL),
254      'function' => 'theme_cck_plan_fields_formatter_generic',
255    ),
256    'cck_plan_fields_formatter_single_line_value7' => array(
257      'arguments' => array('form' => NULL),
258      'function' => 'theme_cck_plan_fields_formatter_generic',
259    ),
260    'cck_plan_fields_formatter_single_line_value8' => array(
261      'arguments' => array('form' => NULL),
262      'function' => 'theme_cck_plan_fields_formatter_generic',
263    ),
264    'cck_plan_fields_formatter_single_line_value9' => array(
265      'arguments' => array('form' => NULL),
266      'function' => 'theme_cck_plan_fields_formatter_generic',
267    ),
268    'cck_plan_fields_formatter_single_line_value10' => array(
269      'arguments' => array('form' => NULL),
270      'function' => 'theme_cck_plan_fields_formatter_generic',
271    ),
272    'cck_plan_fields_formatter_single_line_value11' => array(
273      'arguments' => array('form' => NULL),
274      'function' => 'theme_cck_plan_fields_formatter_generic',
275    ),
276    'cck_plan_fields_table' => array(
277      'arguments' => array('form' => NULL),
278    ),
279  );
280}
281
282/**
283 * Proxy theme function for cck_plan_fields formatters.
284 */
285function theme_cck_plan_fields_formatter_generic($element) {
286  $output = '';
287  $flag = explode('single_line_', $element['#formatter']);
288  $ftypes = array(
289    'tid' => t('Partidas'),
290    'value' => t('January'),
291    'value_1' => t('February'),
292    'value_2' => t('March'),
293    'value_3' => t('April'),
294    'value_4' => t('May'),
295    'value_5' => t('June'),
296    'value_6' => t('July'),
297    'value_7' => t('August'),
298    'value_8' => t('September'),
299    'value_9' => t('Octuber'),
300    'value_10' => t('November'),
301    'value_11' => t('December'),
302  );
303  if ($flag[1] == 'tid') {
304    $term = taxonomy_get_term($element['#item'][$flag[1]]);
305    // If this term's vocabulary supports localization.
306    if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) {
307      $term->name = tt("taxonomy:term:$term->tid:name", $term->name);
308    }
309    $output = '<strong>'. $ftypes[$flag[1]] .': </strong>'. check_plain($term->name);
310  }
311  else {
312    $output = '<strong>'. $ftypes[$flag[1]] .': </strong>'. number_format($element['#item'][$flag[1]], 2, '.', ',');
313  }
314  return $output;
315}
316
317/**
318 * theme_cck_plan_fields_formatter_default().
319 * default formatter theme
320 */
321function theme_cck_plan_fields_formatter_default($element) {
322  $output = '';
323  // If all fields are hidden, return ''
324  if (empty($element['#item']['tid'])) {
325    return $output;
326  }
327  $format_number = array(
328    'decimals' => variable_get('proyectos_operativos_number_decimals', 0),
329    'dec_point' => variable_get('proyectos_operativos_number_dec_point', ','),
330    'thousands_sep' => variable_get('proyectos_operativos_number_thousands_sep', '.'),
331  );
332  $ftypes = array(
333    'tid' => t('Partidas'),
334    'value' => t('January'),
335    'value_1' => t('February'),
336    'value_2' => t('March'),
337    'value_3' => t('April'),
338    'value_4' => t('May'),
339    'value_5' => t('June'),
340    'value_6' => t('July'),
341    'value_7' => t('August'),
342    'value_8' => t('September'),
343    'value_9' => t('Octuber'),
344    'value_10' => t('November'),
345    'value_11' => t('December'),
346  );
347  $ftypes['total'] = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Total');
348  $headers = array();
349  $rows = array();
350  $row = array();
351  $total = 0;
352  foreach ($ftypes as $value => $label) {
353    $headers[] = array('data' => $label);
354    if ($value != 'tid') {
355      if ($value != 'total') {
356        $total += $element['#item'][$value];
357        $row[] = array('data' => number_format($element['#item'][$value], $format_number['decimals'], $format_number['dec_point'], $format_number['thousands_sep']));
358      }
359      else {
360        $row[] = array('data' => $total);
361      }
362    }
363    else {
364      $term = taxonomy_get_term($element['#item'][$value]);
365      // If this term's vocabulary supports localization.
366      if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) {
367        $term->name = tt("taxonomy:term:$term->tid:name", $term->name);
368      }
369      $row[] = array('data' => $term->name);
370    }
371  }
372  $rows[] = $row;
373  return theme('table', $headers, $rows);
374}
375
376/**
377 * theme_cck_plan_fields_formatter_single_line().
378 * display line items products in a single line
379 */
380function theme_cck_plan_fields_formatter_single_line($element) {
381  $output = '';
382  // If all fields are hidden, return ''
383  if (empty($element)) {
384    return $output;
385  }
386  $format_number = array(
387    'decimals' => variable_get('proyectos_operativos_number_decimals', 0),
388    'dec_point' => variable_get('proyectos_operativos_number_dec_point', ','),
389    'thousands_sep' => variable_get('proyectos_operativos_number_thousands_sep', '.'),
390  );
391  $ftypes = array(
392    'tid' => t('Partidas'),
393    'value' => t('January'),
394    'value_1' => t('February'),
395    'value_2' => t('March'),
396    'value_3' => t('April'),
397    'value_4' => t('May'),
398    'value_5' => t('June'),
399    'value_6' => t('July'),
400    'value_7' => t('August'),
401    'value_8' => t('September'),
402    'value_9' => t('Octuber'),
403    'value_10' => t('November'),
404    'value_11' => t('December'),
405  );
406  foreach ($ftypes as $value => $label) {
407    if ($value != 'tid') {
408      $output .= ' <strong>'. $label .': </strong>'. number_format($element['#item'][$value], $format_number['decimals'], $format_number['dec_point'], $format_number['thousands_sep']);
409    }
410    else {
411      $term = taxonomy_get_term($element['#item'][$value]);
412      // If this term's vocabulary supports localization.
413      if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) {
414        $term->name = tt("taxonomy:term:$term->tid:name", $term->name);
415      }
416      $output .= '<strong>'. $label .': </strong>'. check_plain($term->name);
417    }
418  }
419  return '<div class="items-plan-field">'. $output .'</div>';
420}
421
422/**
423 * Implementation of hook_widget_info().
424 */
425function cck_plan_fields_widget_info() {
426  return array(
427    'cck_plan_fields_elements' => array(
428      'label'           => t('Items Plan Field'),
429      'field types'     => array('cck_plan_fields_field'),
430      'multiple values' => CONTENT_HANDLE_CORE,
431      'callbacks'       => array('default value' => CONTENT_CALLBACK_DEFAULT),
432    ),
433  );
434}
435
436/**
437* Implementation of hook_widget_settings().
438*/
439function cck_plan_fields_widget_settings($op, $widget) {
440  switch ($op) {
441    case 'form':
442      $form = array();
443      $form['min'] = array(
444        '#type' => 'textfield',
445        '#title' => t('Minimum'),
446        '#element_validate' => array('_element_validate_number'),
447        '#default_value' => is_numeric($widget['min']) ? $widget['min'] : '',
448      );
449      $form['max'] = array(
450        '#type' => 'textfield',
451        '#title' => t('Maximum'),
452        '#element_validate' => array('_element_validate_number'),
453        '#default_value' => is_numeric($widget['max']) ? $widget['max'] : '',
454      );
455      return $form;
456    break;
457    case 'save':
458      return array(
459        'min',
460        'max',
461      );
462    break;
463  }
464}
465
466/**
467* Implementation of theme_cck_plan_fields_table().
468*/
469function theme_cck_plan_fields_table($form = array()) {
470  $headers = array();
471  $rows = array();
472  $row = array();
473  $ftypes = array(
474    'tid' => t('Account'),
475    'value' => t('Ene'),
476    'value_1' => t('Feb'),
477    'value_2' => t('Mar'),
478    'value_3' => t('Abr'),
479    'value_4' => t('May'),
480    'value_5' => t('Jun'),
481    'value_6' => t('Jul'),
482    'value_7' => t('Aug'),
483    'value_8' => t('Sep'),
484    'value_9' => t('Oct'),
485    'value_10' => t('Nov'),
486    'value_11' => t('Dic'),
487  );
488  $ftypes['total'] = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Total');
489  $backgrounds = array(
490    'tid' => 'blue',
491    'value' => 'black',
492    'value_1' => 'blue',
493    'value_2' => 'white',
494    'value_3' => 'blue',
495    'value_4' => 'white',
496    'value_5' => 'blue',
497    'value_6' => 'white',
498    'value_7' => 'blue',
499    'value_8' => 'white',
500    'value_9' => 'blue',
501    'value_10' => 'white',
502    'value_11' => 'blue',
503  );
504  if (isset($form['#title']) && $form['#title']) {
505    $field = str_replace('_', '-', $form['#field_name']);
506    $output .= '<div id="edit-' . $field . '-0-value-wrapper" class="form-item"><label for="edit-' . $field . '-0-value">' . $form['#title'] . '</label>';
507  }
508  $output = '<div style="width:2150px">';
509  foreach ($ftypes as $ftype => $label) {
510    if($ftype != 'tid') {
511      $output .= '<div style="width:150px;float:left;margin-left:5px">' . drupal_render($form[$ftype]) . '</div>';
512    }
513    else {
514      $output .= '<div style="width:130px;float:left;margin-left:5px">' . drupal_render($form[$ftype]) . '</div>';
515    }
516  }
517  $output .= drupal_render($form);
518  $output .= '</div>';
519  if (isset($form['#title']) && $form['#title']) {
520    $output .= '</div>';
521  }
522  return $output;
523}
524
525/**
526 * Implementation of hook_widget().
527 */
528function cck_plan_fields_widget(&$form, &$form_state, $field, $items, $delta = 0) {
529  $cck_plan_fields_path = drupal_get_path('module', 'cck_plan_fields');
530  drupal_add_js($cck_plan_fields_path . '/js/cck_plan_fields.js');
531  $field_name = $field['field_name'];
532  $element = array();
533  $ftypes = array(
534    'tid' => t('Partida'),
535    'value' => t('Ene'),
536    'value_1' => t('Feb'),
537    'value_2' => t('Mar'),
538    'value_3' => t('Abr'),
539    'value_4' => t('May'),
540    'value_5' => t('Jun'),
541    'value_6' => t('Jul'),
542    'value_7' => t('Aug'),
543    'value_8' => t('Sep'),
544    'value_9' => t('Oct'),
545    'value_10' => t('Nov'),
546    'value_11' => t('Dic'),
547  );
548  $ftypes['total'] = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Total');
549  $class = $field_name . '_' . $delta . '_field';
550  $vid = $field['vid'];
551  //$terms = taxonomy_get_tree($vid);
552  $tree = taxonomy_get_tree($vid);
553  $options = array();
554  if ($tree) {
555    foreach ($tree as $term) {
556      if (isset($field['vtid'][$term->tid])) {
557        $choice = new stdClass();
558        $choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name);
559        $options[] = $choice;
560      }
561    }
562  }
563  $element['tid'] = array(
564    '#type' => 'select',
565    '#default_value' => isset($items[$delta]['tid']) ? $items[$delta]['tid'] : NULL,
566    '#options' => $options,
567  );
568  if ($delta == 0) {
569    $element['tid']['#title'] = $ftypes['tid'];
570  }
571  $total = 0;
572  for ($i = 0; $i < 12; $i++) {
573    $class1 = ' ' . $field_name . '_m' . $i . '_field' . ' ' . $field_name . '_dato_field';
574    $field_id = $i ? 'value_' . $i : 'value';
575    $field_id_form = $i ? 'value-' . $i : 'value';
576    $element[$field_id] = array(
577      '#type' => 'textfield',
578      '#default_value' => isset($items[$delta][$field_id]) && $items[$delta][$field_id] ? $items[$delta][$field_id] : 0,
579      '#size' => 15,
580      '#attributes' => array('class' => $class . $class1 . ' number', 'onchange' => "suma('$class');sumatexto('" . $field_name . '_dato_field' . "');sumatexto('" . $field_name . '_m' . $i . '_field' . "');"),
581    );
582    $total += $element[$field_id]['#default_value'];
583    if ($delta == 0) {
584      $element[$field_id]['#title'] = $ftypes[$field_id];
585    }
586  }
587  $element['total'] = array(
588    '#type' => 'textfield',
589    '#default_value' => number_format($total, 0, '.', ''),
590    '#size' => 15,
591    '#attributes' => array('class' => $class . '_total totales'),
592  );
593  if ($delta == 0) {
594    $element['total']['#title'] = $ftypes['total'];
595  }
596  $element['#theme'] = 'cck_plan_fields_table';
597  if (empty($element['#element_validate'])) {
598    $element['#element_validate'] = array();
599  }
600  array_unshift($element['#element_validate'], 'cck_plan_fields_validate');
601  $form_state['#field_info'][$element['#field_name']] = $form['#field_info'][$field_name];
602  // Used so that hook_field('validate') knows where to
603  // flag an error in deeply nested forms.
604  if (empty($form['#parents'])) {
605    $form['#parents'] = array();
606  }
607  $element['_error_element'] = array(
608    '#type' => 'value',
609    '#value' => implode('][', array_merge($form['#parents'], array('value'))),
610  );
611  return $element;
612}
613
614/**
615 * FAPI validation of an individual element.
616 */
617function cck_plan_fields_validate($element, &$form_state) {
618  $field_name = $element['#field_name'];
619  if (!isset($element['_remove']['#post'][$field_name][$element['#delta']]['_remove'])) {
620    $type_name = $element['#type_name'];
621    $field = content_fields($field_name, $type_name);
622    $min = is_numeric($field['widget']['min']);
623    $max = is_numeric($field['widget']['max']);
624    $cck_plan_fields_path = drupal_get_path('module', 'cck_plan_fields');
625    drupal_add_js($cck_plan_fields_path . '/js/cck_plan_fields.js');
626    $flag = FALSE;
627    foreach ($element['#columns'] as $ftype) {
628      if ($ftype != 'tid' && !empty($element[$ftype]['#value'])) {
629        $error_field = implode('][', $element['#parents']) .'][' . $ftype;
630        if (!is_numeric($element[$ftype]['#value'])) {
631          form_set_error($error_field, t('Amount should be a number in %field.', array('%field' => t($field['widget']['label']))));
632        }
633        elseif ($field['cck_plan_fields_simple_type'] == 'int') {
634          $start = $element[$ftype]['#value'];
635          $value = preg_replace('@[^-0-9]@', '', $start);
636          if ($start != $value) {
637            form_set_error($error_field, t('Only numbers are allowed in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label']))));
638          }
639        }
640        elseif ($min && $field['widget']['min'] > $element[$ftype]['#value']) {
641          form_set_error($error_field, t('Amount should be greater %number in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label']))));
642        }
643        elseif ($max && $field['widget']['max'] < $element[$ftype]['#value']) {
644          form_set_error($error_field, t('Amount should be litter %number in %field.', array('%number' => $field['widget']['max'], '%field' => t($field['widget']['label']))));
645        }
646        $flag = TRUE;
647      }
648    }
649    if (!$flag) {
650      return;
651    }
652    if ($element['#delta']) {
653      $current_taxonomy = $element['tid']['#value'];
654      for($i = 0; $i < $element['#delta']; $i++) {
655        if ($current_taxonomy == $form_state['values'][$field_name][$i]['tid']) {
656          $error_field = implode('][', $element['#parents']) .'][tid';
657          form_set_error($error_field, t('Account can not be repeat in %field.', array('%field' => t($field['widget']['label']))));
658        }
659      }
660    }
661  }
662}
663
664
665/**
666 * Implementation of hook_feeds_node_processor_targets_alter().
667 *
668 * @see FeedsNodeProcessor::getMappingTargets()
669 */
670function cck_plan_fields_feeds_node_processor_targets_alter(&$targets, $content_type) {
671  $info = content_types($content_type);
672  $fields = array();
673  if (isset($info['fields']) && count($info['fields'])) {
674    foreach ($info['fields'] as $field_name => $field) {
675      if (in_array($field['type'], array('cck_plan_fields_field'))) {
676        $fields[$field_name] = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name;
677      }
678    }
679  }
680  $ftypes = array(
681    'tid' => t('Taxonomy'),
682    'value' => t('January'),
683    'value_1' => t('February'),
684    'value_2' => t('March'),
685    'value_3' => t('April'),
686    'value_4' => t('May'),
687    'value_5' => t('June'),
688    'value_6' => t('July'),
689    'value_7' => t('August'),
690    'value_8' => t('September'),
691    'value_9' => t('Octuber'),
692    'value_10' => t('November'),
693    'value_11' => t('December'),
694  );
695
696  foreach ($fields as $k => $name) {
697    foreach($ftypes as $id => $month) {
698      $targets[$k . ':' . $id] = array(
699        'name' => check_plain($name) . '(' . $month . ')',
700        'callback' => 'cck_plan_fields_feeds_set_target',
701        'description' => t('The part @month_name of @name field of the node.', array('@name' => $name, '@month_name' => $month)),
702      );
703    }
704  }
705}
706
707function cck_plan_fields_feeds_set_target($node, $target, $value) {
708  $ftypes = array(
709    'tid' => t('Taxonomy'),
710    'value' => t('January'),
711    'value_1' => t('February'),
712    'value_2' => t('March'),
713    'value_3' => t('April'),
714    'value_4' => t('May'),
715    'value_5' => t('June'),
716    'value_6' => t('July'),
717    'value_7' => t('August'),
718    'value_8' => t('September'),
719    'value_9' => t('Octuber'),
720    'value_10' => t('November'),
721    'value_11' => t('December'),
722  );
723  $field_default = array();
724  foreach($ftypes as $id => $month) {
725    $field_default[$id] = 0;
726  }
727
728  list($field_name, $sub_field) = explode(':', $target);
729  $field = isset($node->$field_name) ? $node->$field_name : $field_default;
730  // Handle multiple value fields.
731  $field = content_fields($field_name, $node->type);
732  if (is_array($value)) {
733    $i = 0;
734    foreach ($value as $v) {
735      if (!is_array($v) && !is_object($v)) {
736        if ($sub_field == 'tid') {
737          $terms_found = content_taxonomy_get_term_by_name_vid($v, $field['vid']);
738          if ($terms_found->tid) {
739            $field[$i][$sub_field] = $terms_found->tid;
740          }
741        }
742        elseif(isset($field[$i]['tid']) && $field[$i]['tid']) {
743          $field[$i][$sub_field] = $v;
744        }
745      }
746      $i++;
747    }
748  }
749  else {
750    if ($sub_field == 'tid') {
751      $terms_found = content_taxonomy_get_term_by_name_vid($value, $field['vid']);
752      if ($terms_found->tid) {
753        $field[$i][$sub_field] = $terms_found->tid;
754      }
755    }
756    elseif(isset($field[0]['tid']) && $field[0]['tid']) {
757      $field[$i][$sub_field] = $value;
758    }
759    $field[0][$sub_field] = $value;
760  }
761  $node->$field_name = $field;
762}
763
764function cck_plan_fields_get_term_by_name_vid($name, $vid) {
765  $db_result = db_query(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t WHERE LOWER(t.name) = LOWER('%s') AND vid=%d", 't', 'tid'), trim($name), $vid);
766  $result = array();
767  $term = db_fetch_object($db_result);
768  if($term) {
769    return $term->tid;
770  }
771  return FALSE;
772}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.