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

stable
Last change on this file since b9d4e2e was f74cf58, checked in by José Puentes <jpuentes@…>, 5 años ago

se actualizo el valor por defecto del campo

  • Propiedad mode establecida a 100755
File size: 24.7 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_combine_term.module
5  * Drupal part Module to code ente planificador module
6  * Copyright 2017 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 2017-03-24 // (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_combine_term_field_info() {
32  return array(
33    'cck_plan_fields_combine_term' => array(
34      'label' => t('Combine term and num line field'),
35      'description' => t('Store items operative combine term data in the database.'),
36    ),
37  );
38}
39
40/**
41* Implementation of hook_field_settings().
42*/
43function cck_plan_fields_combine_term_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      $list = array();
53      $vids = array();
54      for($i = 0; $i <= 11; $i++) {
55        $id = $i > 0? 'vid_' . $i : 'vid';
56        $vids[$i] = isset($field[$id]) && !empty($field[$id]) ? $field[$id] : '';
57        $list[$i] = ($i + 1);
58      }
59      $count_vocabulary = array();
60      for($i = 0; $i < 10; $i++) {
61        $count_vocabulary[$i] = ($i + 1);
62      }
63      $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0;
64      $form['count_vocabulary'] = array(
65        '#title' => t('Count Term fields'),
66        '#type' => 'select',
67        '#default_value' => $count_vocabulary_default,
68        '#options' => $count_vocabulary,
69      );
70      for($i = 0; $i <= $count_vocabulary_default; $i++) {
71        $idx = $i > 0? 'vid_' . $i : 'vid';
72        $aux = $i + 1;
73        $default_vid =  isset($field[$idx]) && !empty($field[$idx]) ? $field[$idx] : FALSE;
74        $form[$idx] = array(
75          '#type' => 'select',
76          '#title' => t('Taxonomy %i', array('%i' => $aux)),
77          '#default_value' => $default_vid,
78          '#options' => $vocabulary_options,
79        );
80        if ($default_vid) {
81          $tree = taxonomy_get_tree($default_vid);
82          $options = array();
83          if ($tree) {
84            foreach ($tree as $term) {
85              $choice = new stdClass();
86              $choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name);
87              $options[] = $choice;
88            }
89          }
90          $idx1 = $i > 0? 'vtid_' . $i : 'vtid';
91          $default_vtid =  isset($field[$idx1]) && !empty($field[$idx1]) ? $field[$idx1] : array();
92          $form[$idx1] = array(
93            '#title' => t('Term to select %i', array('%i' => $aux)),
94            '#type' => 'select',
95            '#default_value' => $default_vtid,
96            '#options' => $options,
97            '#multiple' => TRUE,
98          );
99          $idx1 = $i > 0? 'cck_plan_fields_simple_titulo_term_' . $i : 'cck_plan_fields_simple_titulo_term';
100          $form[$idx1] = array(
101            '#type' => 'textfield',
102            '#title' => t('Titulo del Termino de la taxonomia %i', array('%i' => $aux)),
103            '#default_value' => isset($field[$idx1]) && !empty($field[$idx1]) ? $field[$idx1] : t('Term'),
104          );
105          $id = $i > 0? 'combine_weight_' . $i : 'combine_weight';
106          $form[$id] = array(
107            '#type' => 'textfield',
108            '#title' => t('Weight %i', array('%i' => $aux)),
109            '#default_value' => isset($field[$id])? $field[$id] : 0,
110            '#size' => 3,
111            '#element_validate' => array('_element_validate_number'),
112          );
113        }
114      }
115      $types = array(
116        'int' => t('Integer'),
117        'float' => t('Float'),
118      );
119      $form['cck_plan_fields_simple_type'] = array(
120        '#type' => 'select',
121        '#title' => t('Type'),
122        '#default_value' => !empty($field['cck_plan_fields_simple_type']) ? $field['cck_plan_fields_simple_type'] : 'int',
123        '#options' => $types,
124      );
125      $form['cck_plan_fields_simple_titulo'] = array(
126        '#type' => 'textfield',
127        '#title' => t('Titulo del monto'),
128        //'#required' => TRUE,
129        '#default_value' => !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Monto'),
130      );
131      $form['combine_weight_value'] = array(
132        '#type' => 'textfield',
133        '#title' => t('Weight value'),
134        '#default_value' => isset($field['combine_weight_value'])? $field['combine_weight_value'] : 0,
135        '#size' => 3,
136        '#element_validate' => array('_element_validate_number'),
137      );
138      return $form;
139    case 'save':
140      $save_settings = array(
141        'cck_plan_fields_simple_type',
142        'cck_plan_fields_simple_titulo',
143        'count_vocabulary',
144        'combine_weight_value',
145      );
146      $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0;
147      for($i = 0; $i <= $count_vocabulary_default; $i++) {
148        $idx = $i > 0? 'vid_' . $i : 'vid';
149        $save_settings[] = $idx;
150        $idx1 = $i > 0? 'vtid_' . $i : 'vtid';
151        $save_settings[] = $idx1;
152        $idx1 = $i > 0? 'cck_plan_fields_simple_titulo_term_' . $i : 'cck_plan_fields_simple_titulo_term';
153        $save_settings[] = $idx1;
154        $id = $i > 0? 'combine_weight_' . $i : 'combine_weight';
155        $save_settings[] = $id;
156      }
157      return $save_settings;
158    case 'database columns':
159      $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0;
160      for($i = 0; $i <= $count_vocabulary_default; $i++) {
161        $idx = $i > 0? 'tid_' . $i : 'tid';
162        $columns[$idx] = array(
163          'type' => 'int',
164          'not null' => FALSE,
165          'sortable' => TRUE,
166          'views' => TRUE,
167        );
168      }
169      $columns['value'] = array(
170        'type' => 'numeric',
171        'size' => 'normal',
172        'not null' => TRUE,
173        'default' => 0,
174        'precision' => 32,
175        'scale' => 0,
176      );
177      return $columns;
178    case 'views data':
179      $data = content_views_field_views_data($field);
180      $db_info = content_database_info($field);
181      $table_alias = content_views_tablename($field);
182      $titulo_monto = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Monto');
183      $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0;
184      $ftypes = array(
185        'value' => $titulo_monto,
186      );
187      for($i = 0; $i <= $count_vocabulary_default; $i++) {
188        $idx = $i > 0? 'tid_' . $i : 'tid';
189        $idx1 = $i > 0? 'cck_plan_fields_simple_titulo_term_' . $i : 'cck_plan_fields_simple_titulo_term';
190        $ftypes[$idx] = !isset($field[$idx1]) && empty($field[$idx1]) ? $field[$idx1] : t('Term');
191      }
192
193      foreach ($ftypes as $ftype => $label) {
194        $copy = $data[$table_alias][$field['field_name'] . $ftype];
195        $copy['title'] = t($label);
196        $copy['filter']['handler'] = 'content_handler_filter_many_to_one';
197        $copy['filter']['numeric'] = TRUE;
198        unset($copy['field'], $copy['argument'], $copy['sort']);
199        $data[$table_alias][$field['field_name'] . $ftype . '_many_to_one'] = $copy;
200        $data[$table_alias][$field['field_name'] . $ftype]['argument']['handler'] = 'content_handler_argument_many_to_one';
201        if ($ftype != 'description') {
202          $data[$table_alias][$field['field_name'] . $ftype]['argument']['numeric'] = TRUE;
203        }
204      }
205      return $data;
206  }
207}
208
209/**
210 * Implementation of hook_content_is_empty().
211 */
212function cck_plan_fields_combine_term_content_is_empty($item, $field) {
213  $flat = TRUE;
214  //check term
215  $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0;
216  for($i = 0; $i <= $count_vocabulary_default; $i++) {
217    $idxt = $i > 0? 'tid_' . $i : 'tid';
218    if (!empty($item[$idxt]) && $item[$idxt]) {
219      $flat = FALSE;
220    }
221
222  }
223  if ($flat) {
224    return $flat;
225  }
226  //check value data
227  $flat = TRUE;
228  foreach (array_keys($field['columns']) as $ftype) {
229    if (!empty($item['value'])) {
230      $flat = FALSE;
231    }
232  }
233  return $flat;
234}
235
236/**
237 * Implementation of hook_field_formatter_info().
238 */
239function cck_plan_fields_combine_term_field_formatter_info() {
240  $formatters = array(
241    'default' => array(
242      'label'  => t('Default'),
243      'multiple values' => CONTENT_HANDLE_CORE,
244      'field types'  => array('cck_plan_fields_combine_term'),
245    ),
246  );
247  return $formatters;
248}
249
250/**
251 * Implementation of hook_theme().
252 */
253function cck_plan_fields_combine_term_theme() {
254  return array(
255    // Shows address in the default view: Multilines
256    'cck_plan_fields_combine_term_formatter_default' => array(
257      'arguments' => array('element'),
258    ),
259    'cck_plan_fields_combine_term_table' => array(
260      'arguments' => array('form' => NULL),
261    ),
262  );
263}
264
265/**
266 * Proxy theme function for cck_plan_fields formatters.
267 */
268function theme_cck_plan_fields_combine_term_formatter_generic($element) {
269  $output = '';
270  $flag = explode('single_line_', $element['#formatter']);
271  $field_name = $element['#field_name'];
272  $type_name = $element['#type_name'];
273  $field = content_fields($field_name, $type_name);
274  $ftypes = array(
275    'tid' => $field['cck_plan_fields_simple_titulo_term'],
276    'value' => $field['cck_plan_fields_simple_titulo'],
277  );
278  if ($flag[1] == 'tid') {
279    $term = taxonomy_get_term($element['#item'][$flag[1]]);
280    // If this term's vocabulary supports localization.
281    if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) {
282      $term->name = tt("taxonomy:term:$term->tid:name", $term->name);
283    }
284    $output = '<strong>'. $ftypes[$flag[1]] .': </strong>'. check_plain($term->name);
285  }
286  else {
287    $output = '<strong>'. $ftypes[$flag[1]] .': </strong>'. number_format($element['#item'][$flag[1]], 2, '.', ',');
288  }
289  return $output;
290}
291
292function cck_plan_fields_combine_term_weight($field) {
293  $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0;
294  for($i = 0; $i <= $count_vocabulary_default; $i++) {
295    $idxt = $i > 0? 'tid_' . $i : 'tid';
296    if (!empty($element['#item'][$idxt])) {
297      $empty = FALSE;
298      continue;
299    }
300  }
301  if ($empty) {
302    return '';
303  }
304  $weights = array();
305  $weights[] = array(
306    'key' => 'value',
307    'title' => !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Monto'),
308    'weight' => isset($field['combine_weight_value'])? $field['combine_weight_value'] : 0,
309  );
310  $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0;
311  for($i = 0; $i <= $count_vocabulary_default; $i++) {
312    $idx1 = $i > 0? 'cck_plan_fields_simple_titulo_term_' . $i : 'cck_plan_fields_simple_titulo_term';
313    $id = $i > 0? 'combine_weight_' . $i : 'combine_weight';
314    $idx = $i > 0? 'vid_' . $i : 'vid';
315    $idxtid = $i > 0? 'vtid_' . $i : 'vtid';
316    $idxt = $i > 0? 'tid_' . $i : 'tid';
317    $weights[] = array(
318      'key' => $idxt,
319      'title' => !empty($field[$idx1]) ? $field[$idx1] : t('Term'),
320      'weight' => isset($field[$id])? $field[$id] : 0,
321      'vid' => $idx,
322      'vtid' => $idxtid,
323    );
324  }
325  uasort($weights, 'combine_term_sort_weight');
326  return $weights;
327}
328
329/**
330 * theme_cck_plan_fields_formatter_default().
331 * default formatter theme
332 */
333function theme_cck_plan_fields_combine_term_formatter_default($element) {
334  $field_name = $element['#field_name'];
335  $type_name = $element['#type_name'];
336  $field = content_fields($field_name, $type_name);
337  $output = '';
338  // If all fields are hidden, return ''
339  $empty = TRUE;
340  $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0;
341  for($i = 0; $i <= $count_vocabulary_default; $i++) {
342    $idxt = $i > 0? 'tid_' . $i : 'tid';
343    if (!empty($element['#item'][$idxt])) {
344      $empty = FALSE;
345      continue;
346    }
347  }
348  if ($empty) {
349    return '';
350  }
351  $weights = cck_plan_fields_combine_term_weight($field);
352  $i = 0;
353  $items = array();
354  foreach($weights as $value) {
355    if ($value['key'] == 'value') {
356      $numero = number_format($element['#item'][$value['key']], $format_number['decimals'], $format_number['dec_point'], $format_number['thousands_sep']);
357      $items[] = '<div><div class="field-label-inline-first">' . $value['title'] . ':</div> ' . $numero . '</div>';
358    }
359    else {
360      if (isset($element['#item'][$value['key']]) && !empty($element['#item'][$value['key']]) && $element['#item'][$value['key']]) {
361        $term = taxonomy_get_term($element['#item'][$value['key']]);
362        if ($term) {
363          if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) {
364            $term->name = tt("taxonomy:term:$term->tid:name", $term->name);
365          }
366          $items[] = '<div><div class="field-label-inline-first">' . $value['title'] . ':</div> ' . $term->name . '</div>';
367        }
368      }
369    }
370  }
371  return implode ('', $items);
372}
373
374/**
375 * Implementation of hook_widget_info().
376 */
377function cck_plan_fields_combine_term_widget_info() {
378  return array(
379    'cck_plan_fields_combine_term' => array(
380      'label'           => t('Items combine term Field'),
381      'field types'     => array('cck_plan_fields_combine_term'),
382      'multiple values' => CONTENT_HANDLE_CORE,
383      'callbacks'       => array('default value' => CONTENT_CALLBACK_DEFAULT),
384    ),
385  );
386}
387
388/**
389* Implementation of hook_widget_settings().
390*/
391function cck_plan_fields_combine_term_widget_settings($op, $widget) {
392  switch ($op) {
393    case 'form':
394      //$form = array();
395      $form['min'] = array(
396        '#type' => 'textfield',
397        '#title' => t('Minimum'),
398        '#element_validate' => array('_element_validate_number'),
399        '#default_value' => is_numeric($widget['min']) ? $widget['min'] : '',
400      );
401      $form['max'] = array(
402        '#type' => 'textfield',
403        '#title' => t('Maximum'),
404        '#element_validate' => array('_element_validate_number'),
405        '#default_value' => is_numeric($widget['max']) ? $widget['max'] : '',
406      );
407      return $form;
408    break;
409    case 'save':
410      return array(
411        'min',
412        'max',
413        'weight_combine',
414      );
415    break;
416  }
417}
418
419
420
421function combine_term_sort_weight($a, $b) {
422  $a_weight = is_array($a) && isset($a['weight']) ? $a['weight'] : 0;
423  $b_weight = is_array($b) && isset($b['weight']) ? $b['weight'] : 0;
424  if ($a_weight == $b_weight) {
425    return 0;
426  }
427  return $a_weight < $b_weight ? -1 : 1;
428}
429
430
431/**
432* Implementation of theme_cck_plan_fields_combine_term_table().
433*/
434function theme_cck_plan_fields_combine_term_table($form = array()) {
435  $field = content_fields($form['#field_name'], $form['#type_name']);
436  $headers = array();
437  $rows = array();
438  $row = array();
439  $ftypes = array(
440    'value' => t('Cantidad'),
441  );
442  $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0;
443  for($i = 0; $i <= $count_vocabulary_default; $i++) {
444    $idx1 = $i > 0? 'cck_plan_fields_simple_titulo_term_' . $i : 'cck_plan_fields_simple_titulo_term';
445    $idx = $i > 0? 'vid_' . $i : 'vid';
446    $ftypes[$idx] = isset($field[$idx1]) && !empty($field[$idx1]) ? $field[$idx1] : t('Term');
447  }
448  if (isset($form['#title']) && $form['#title']) {
449    $field = str_replace('_', '-', $form['#field_name']);
450    $output .= '<div id="edit-' . $field . '-0-value-wrapper" class="form-item"><label for="edit-' . $field . '-0-value">' . $form['#title'] . '</label>';
451  }
452  $output = '<div">';
453  foreach ($ftypes as $ftype => $label) {
454    $output .= '<div style="float:left;margin-left:5px">' . drupal_render($form[$ftype]) . '</div>';
455  }
456  $output .= drupal_render($form);
457  $output .= '</div>';
458  if (isset($form['#title']) && $form['#title']) {
459    $output .= '</div>';
460  }
461  return $output;
462}
463
464/**
465 * Implementation of hook_widget().
466 */
467function cck_plan_fields_combine_term_widget(&$form, &$form_state, $field, $items, $delta = 0) {
468  $field_name = $field['field_name'];
469  $element = array();
470  $weights = cck_plan_fields_combine_term_weight($field);
471  $i = 0;
472  foreach($weights as $value) {
473    if ($value['key'] == 'value') {
474      $class = $field_name . '_' . $delta . '_field';
475      $class1 = ' ' . $field_name . '_m' . $i . '_field' . ' ' . $field_name . '_dato_field';
476      $field_id = $i ? 'value_' . $i : 'value';
477      $field_id_form = $i ? 'value-' . $i : 'value';
478
479      $default_value = isset($items[$delta][$value['key']]) && $items[$delta][$value['key']] ? $items[$delta][$value['key']] : 0;
480      if ($field['cck_plan_fields_simple_type'] == 'float') {
481        $default_value = isset($field['cck_plan_fields_simple_decimal']) ? str_replace('.', $field['cck_plan_fields_simple_decimal'], $default_value) : $default_value;
482      }
483
484      $element[$value['key']] = array(
485        '#type' => 'textfield',
486        //'#default_value' => isset($items[$delta][$value['key']]) && $items[$delta][$value['key']] ? $items[$delta][$value['key']] : 0,
487        '#default_value' => $default_value,
488        '#size' => 15,
489        '#attributes' => array('class' => $class . $class1 . ' number', 'onchange' => "suma('$class');sumatexto('" . $field_name . '_dato_field' . "');sumatexto('" . $field_name . '_m' . $i . '_field' . "');"),
490      );
491    }
492    else {
493      $tree = taxonomy_get_tree($field[$value['vid']]);
494      $options = array();
495      if ($tree) {
496        foreach ($tree as $term) {
497          if (isset($field[$value['vtid']][$term->tid])) {
498            $choice = new stdClass();
499            $choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name);
500            $options[] = $choice;
501          }
502        }
503      }
504      $element[$value['key']] = array(
505        '#type' => 'select',
506        '#default_value' => isset($items[$delta][$value['key']]) ? $items[$delta][$value['key']] : NULL,
507        '#options' => $options,
508      );
509
510    }
511    $element[$value['key']]['#title'] = $value['title'];
512  }
513  array_unshift($element['#element_validate'], 'cck_plan_fields_combine_term_validate');
514  $form_state['#field_info'][$element['#field_name']] = $form['#field_info'][$field_name];
515  // Used so that hook_field('validate') knows where to
516  // flag an error in deeply nested forms.
517  if (empty($form['#parents'])) {
518    $form['#parents'] = array();
519  }
520  $element['_error_element'] = array(
521    '#type' => 'value',
522    '#value' => implode('][', array_merge($form['#parents'], array('value'))),
523  );
524  return $element;
525}
526
527/**
528 * FAPI validation of an individual element.
529 */
530function cck_plan_fields_combine_term_validate($element, &$form_state) {
531  $field_name = $element['#field_name'];
532  if (!isset($element['_remove']['#post'][$field_name][$element['#delta']]['_remove'])) {
533    $type_name = $element['#type_name'];
534    $field = content_fields($field_name, $type_name);
535    $min = is_numeric($field['widget']['min']);
536    $max = is_numeric($field['widget']['max']);
537    $cck_plan_fields_path = drupal_get_path('module', 'cck_plan_fields');
538    drupal_add_js($cck_plan_fields_path . '/js/cck_plan_fields.js');
539    $flag = FALSE;
540    foreach ($element['#columns'] as $ftype) {
541      if ($ftype != 'tid' && !empty($element[$ftype]['#value'])) {
542        $error_field = implode('][', $element['#parents']) .'][' . $ftype;
543        if (!is_numeric($element[$ftype]['#value'])) {
544          form_set_error($error_field, t('Amount should be a number in %field.', array('%field' => t($field['widget']['label']))));
545        }
546        elseif ($field['cck_plan_fields_simple_type'] == 'int') {
547          $start = $element[$ftype]['#value'];
548          $value = preg_replace('@[^-0-9]@', '', $start);
549          if ($start != $value) {
550            form_set_error($error_field, t('Only numbers are allowed in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label']))));
551          }
552        }
553        elseif ($min && $field['widget']['min'] > $element[$ftype]['#value']) {
554          form_set_error($error_field, t('Amount should be greater %number in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label']))));
555        }
556        elseif ($max && $field['widget']['max'] < $element[$ftype]['#value']) {
557          form_set_error($error_field, t('Amount should be litter %number in %field.', array('%number' => $field['widget']['max'], '%field' => t($field['widget']['label']))));
558        }
559        $flag = TRUE;
560      }
561    }
562    if (!$flag) {
563      return;
564    }
565    //inicio desde el primer elemento
566    if ($element['#delta']) {
567      $current_taxonomy = $element['tid']['#value'];
568      for($i = 0; $i < $element['#delta']; $i++) {
569        if ($current_taxonomy == $form_state['values'][$field_name][$i]['tid'] && !isset($element['_remove']['#post'][$field_name][$i]['_remove'])) {
570          $error_field = implode('][', $element['#parents']) .'][tid';
571          form_set_error($error_field, t('@type_term can not be repeat in %field.', array('%field' => t($field['widget']['label']), '@type_term' => $field['cck_plan_fields_simple_titulo_term'])));
572          return;
573        }
574      }
575    }
576  }
577}
578
579/**
580 * Implementation of hook_feeds_node_processor_targets_alter().
581 *
582 * @see FeedsNodeProcessor::getMappingTargets()
583 */
584function cck_plan_fields_combine_term_feeds_node_processor_targets_alter(&$targets, $content_type) {
585  $info = content_types($content_type);
586  $fields = array();
587  if (isset($info['fields']) && count($info['fields'])) {
588    foreach ($info['fields'] as $field_name => $field) {
589      if (in_array($field['type'], array('cck_plan_fields_field'))) {
590        $fields[$field_name] = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name;
591      }
592    }
593  }
594  $ftypes = array(
595    'tid' => t('Term'),
596    'value' => t('Cantidad'),
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_combine_term_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/**
610 * Implementation of cck_plan_fields_combine_term_feeds_set_target().
611 * Target callback function in Feeds
612 */
613function cck_plan_fields_combine_term_feeds_set_target($node, $target, $value) {
614  $ftypes = array(
615    'tid' => t('Term'),
616    'value' => t('Cantidad'),
617  );
618  $field_default = array();
619  foreach($ftypes as $id => $month) {
620    $field_default[$id] = 0;
621  }
622  list($field_name, $sub_field) = explode(':', $target);
623  $field = isset($node->$field_name) ? $node->$field_name : $field_default;
624  // Handle multiple value fields.
625  $field = content_fields($field_name, $node->type);
626  if (is_array($value)) {
627    $i = 0;
628    foreach ($value as $v) {
629      if (!is_array($v) && !is_object($v)) {
630        if ($sub_field == 'tid') {
631          $terms_found = content_taxonomy_get_term_by_name_vid($v, $field['vid']);
632          if ($terms_found->tid) {
633            $field[$i][$sub_field] = $terms_found->tid;
634          }
635        }
636        elseif(isset($field[$i]['tid']) && $field[$i]['tid']) {
637          $field[$i][$sub_field] = $v;
638        }
639      }
640      $i++;
641    }
642  }
643  else {
644    if ($sub_field == 'tid') {
645      $terms_found = cck_plan_fields_combine_term_get_term_by_name_vid($value, $field['vid']);
646      if ($terms_found->tid) {
647        $field[$i][$sub_field] = $terms_found->tid;
648      }
649    }
650    elseif(isset($field[0]['tid']) && $field[0]['tid']) {
651      $field[$i][$sub_field] = $value;
652    }
653    $field[0][$sub_field] = $value;
654  }
655  $node->$field_name = $field;
656}
657
658/**
659 * Implementation of cck_plan_fields_combine_term_get_term_by_name_vid().
660 * Get term from name and vocabulary
661 */
662function cck_plan_fields_combine_term_get_term_by_name_vid($name, $vid) {
663  $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);
664  $result = array();
665  $term = db_fetch_object($db_result);
666  if($term) {
667    return $term->tid;
668  }
669  return FALSE;
670}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.