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

stable
Last change on this file since 37f87b2 was f8d6cf8, checked in by José Gregorio Puentes <jpuentes@…>, 6 años ago

se agrego el cambio para permitir cantidad y taxonomias en lo referente a los tipos de monedas

  • Propiedad mode establecida a 100755
File size: 24.3 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      $element[$value['key']] = array(
479        '#type' => 'textfield',
480        '#default_value' => isset($items[$delta][$value['key']]) && $items[$delta][$value['key']] ? $items[$delta][$value['key']] : 0,
481        '#size' => 15,
482        '#attributes' => array('class' => $class . $class1 . ' number', 'onchange' => "suma('$class');sumatexto('" . $field_name . '_dato_field' . "');sumatexto('" . $field_name . '_m' . $i . '_field' . "');"),
483      );
484    }
485    else {
486      $tree = taxonomy_get_tree($field[$value['vid']]);
487      $options = array();
488      if ($tree) {
489        foreach ($tree as $term) {
490          if (isset($field[$value['vtid']][$term->tid])) {
491            $choice = new stdClass();
492            $choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name);
493            $options[] = $choice;
494          }
495        }
496      }
497      $element[$value['key']] = array(
498        '#type' => 'select',
499        '#default_value' => isset($items[$delta][$value['key']]) ? $items[$delta][$value['key']] : NULL,
500        '#options' => $options,
501      );
502
503    }
504    $element[$value['key']]['#title'] = $value['title'];
505  }
506  array_unshift($element['#element_validate'], 'cck_plan_fields_combine_term_validate');
507  $form_state['#field_info'][$element['#field_name']] = $form['#field_info'][$field_name];
508  // Used so that hook_field('validate') knows where to
509  // flag an error in deeply nested forms.
510  if (empty($form['#parents'])) {
511    $form['#parents'] = array();
512  }
513  $element['_error_element'] = array(
514    '#type' => 'value',
515    '#value' => implode('][', array_merge($form['#parents'], array('value'))),
516  );
517  return $element;
518}
519
520/**
521 * FAPI validation of an individual element.
522 */
523function cck_plan_fields_combine_term_validate($element, &$form_state) {
524  $field_name = $element['#field_name'];
525  if (!isset($element['_remove']['#post'][$field_name][$element['#delta']]['_remove'])) {
526    $type_name = $element['#type_name'];
527    $field = content_fields($field_name, $type_name);
528    $min = is_numeric($field['widget']['min']);
529    $max = is_numeric($field['widget']['max']);
530    $cck_plan_fields_path = drupal_get_path('module', 'cck_plan_fields');
531    drupal_add_js($cck_plan_fields_path . '/js/cck_plan_fields.js');
532    $flag = FALSE;
533    foreach ($element['#columns'] as $ftype) {
534      if ($ftype != 'tid' && !empty($element[$ftype]['#value'])) {
535        $error_field = implode('][', $element['#parents']) .'][' . $ftype;
536        if (!is_numeric($element[$ftype]['#value'])) {
537          form_set_error($error_field, t('Amount should be a number in %field.', array('%field' => t($field['widget']['label']))));
538        }
539        elseif ($field['cck_plan_fields_simple_type'] == 'int') {
540          $start = $element[$ftype]['#value'];
541          $value = preg_replace('@[^-0-9]@', '', $start);
542          if ($start != $value) {
543            form_set_error($error_field, t('Only numbers are allowed in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label']))));
544          }
545        }
546        elseif ($min && $field['widget']['min'] > $element[$ftype]['#value']) {
547          form_set_error($error_field, t('Amount should be greater %number in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label']))));
548        }
549        elseif ($max && $field['widget']['max'] < $element[$ftype]['#value']) {
550          form_set_error($error_field, t('Amount should be litter %number in %field.', array('%number' => $field['widget']['max'], '%field' => t($field['widget']['label']))));
551        }
552        $flag = TRUE;
553      }
554    }
555    if (!$flag) {
556      return;
557    }
558    //inicio desde el primer elemento
559    if ($element['#delta']) {
560      $current_taxonomy = $element['tid']['#value'];
561      for($i = 0; $i < $element['#delta']; $i++) {
562        if ($current_taxonomy == $form_state['values'][$field_name][$i]['tid'] && !isset($element['_remove']['#post'][$field_name][$i]['_remove'])) {
563          $error_field = implode('][', $element['#parents']) .'][tid';
564          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'])));
565          return;
566        }
567      }
568    }
569  }
570}
571
572/**
573 * Implementation of hook_feeds_node_processor_targets_alter().
574 *
575 * @see FeedsNodeProcessor::getMappingTargets()
576 */
577function cck_plan_fields_combine_term_feeds_node_processor_targets_alter(&$targets, $content_type) {
578  $info = content_types($content_type);
579  $fields = array();
580  if (isset($info['fields']) && count($info['fields'])) {
581    foreach ($info['fields'] as $field_name => $field) {
582      if (in_array($field['type'], array('cck_plan_fields_field'))) {
583        $fields[$field_name] = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name;
584      }
585    }
586  }
587  $ftypes = array(
588    'tid' => t('Term'),
589    'value' => t('Cantidad'),
590  );
591  foreach ($fields as $k => $name) {
592    foreach($ftypes as $id => $month) {
593      $targets[$k . ':' . $id] = array(
594        'name' => check_plain($name) . '(' . $month . ')',
595        'callback' => 'cck_plan_fields_combine_term_feeds_set_target',
596        'description' => t('The part @month_name of @name field of the node.', array('@name' => $name, '@month_name' => $month)),
597      );
598    }
599  }
600}
601
602/**
603 * Implementation of cck_plan_fields_combine_term_feeds_set_target().
604 * Target callback function in Feeds
605 */
606function cck_plan_fields_combine_term_feeds_set_target($node, $target, $value) {
607  $ftypes = array(
608    'tid' => t('Term'),
609    'value' => t('Cantidad'),
610  );
611  $field_default = array();
612  foreach($ftypes as $id => $month) {
613    $field_default[$id] = 0;
614  }
615  list($field_name, $sub_field) = explode(':', $target);
616  $field = isset($node->$field_name) ? $node->$field_name : $field_default;
617  // Handle multiple value fields.
618  $field = content_fields($field_name, $node->type);
619  if (is_array($value)) {
620    $i = 0;
621    foreach ($value as $v) {
622      if (!is_array($v) && !is_object($v)) {
623        if ($sub_field == 'tid') {
624          $terms_found = content_taxonomy_get_term_by_name_vid($v, $field['vid']);
625          if ($terms_found->tid) {
626            $field[$i][$sub_field] = $terms_found->tid;
627          }
628        }
629        elseif(isset($field[$i]['tid']) && $field[$i]['tid']) {
630          $field[$i][$sub_field] = $v;
631        }
632      }
633      $i++;
634    }
635  }
636  else {
637    if ($sub_field == 'tid') {
638      $terms_found = cck_plan_fields_combine_term_get_term_by_name_vid($value, $field['vid']);
639      if ($terms_found->tid) {
640        $field[$i][$sub_field] = $terms_found->tid;
641      }
642    }
643    elseif(isset($field[0]['tid']) && $field[0]['tid']) {
644      $field[$i][$sub_field] = $value;
645    }
646    $field[0][$sub_field] = $value;
647  }
648  $node->$field_name = $field;
649}
650
651/**
652 * Implementation of cck_plan_fields_combine_term_get_term_by_name_vid().
653 * Get term from name and vocabulary
654 */
655function cck_plan_fields_combine_term_get_term_by_name_vid($name, $vid) {
656  $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);
657  $result = array();
658  $term = db_fetch_object($db_result);
659  if($term) {
660    return $term->tid;
661  }
662  return FALSE;
663}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.