array( 'label' => t('Combine term and num line field'), 'description' => t('Store items operative combine term data in the database.'), ), ); } /** * Implementation of hook_field_settings(). */ function cck_plan_fields_combine_term_field_settings($op, $field) { switch ($op) { case 'form': $form = array(); $vocabularies = taxonomy_get_vocabularies(); $vocabulary_options = array(); foreach($vocabularies as $vocabulary) { $vocabulary_options[$vocabulary->vid] = $vocabulary->name; } $list = array(); $vids = array(); for($i = 0; $i <= 11; $i++) { $id = $i > 0? 'vid_' . $i : 'vid'; $vids[$i] = isset($field[$id]) && !empty($field[$id]) ? $field[$id] : ''; $list[$i] = ($i + 1); } $count_vocabulary = array(); for($i = 0; $i < 10; $i++) { $count_vocabulary[$i] = ($i + 1); } $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0; $form['count_vocabulary'] = array( '#title' => t('Count Term fields'), '#type' => 'select', '#default_value' => $count_vocabulary_default, '#options' => $count_vocabulary, ); for($i = 0; $i <= $count_vocabulary_default; $i++) { $idx = $i > 0? 'vid_' . $i : 'vid'; $aux = $i + 1; $default_vid = isset($field[$idx]) && !empty($field[$idx]) ? $field[$idx] : FALSE; $form[$idx] = array( '#type' => 'select', '#title' => t('Taxonomy %i', array('%i' => $aux)), '#default_value' => $default_vid, '#options' => $vocabulary_options, ); if ($default_vid) { $tree = taxonomy_get_tree($default_vid); $options = array(); if ($tree) { foreach ($tree as $term) { $choice = new stdClass(); $choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name); $options[] = $choice; } } $idx1 = $i > 0? 'vtid_' . $i : 'vtid'; $default_vtid = isset($field[$idx1]) && !empty($field[$idx1]) ? $field[$idx1] : array(); $form[$idx1] = array( '#title' => t('Term to select %i', array('%i' => $aux)), '#type' => 'select', '#default_value' => $default_vtid, '#options' => $options, '#multiple' => TRUE, ); $idx1 = $i > 0? 'cck_plan_fields_simple_titulo_term_' . $i : 'cck_plan_fields_simple_titulo_term'; $form[$idx1] = array( '#type' => 'textfield', '#title' => t('Titulo del Termino de la taxonomia %i', array('%i' => $aux)), '#default_value' => isset($field[$idx1]) && !empty($field[$idx1]) ? $field[$idx1] : t('Term'), ); $id = $i > 0? 'combine_weight_' . $i : 'combine_weight'; $form[$id] = array( '#type' => 'textfield', '#title' => t('Weight %i', array('%i' => $aux)), '#default_value' => isset($field[$id])? $field[$id] : 0, '#size' => 3, '#element_validate' => array('_element_validate_number'), ); } } $types = array( 'int' => t('Integer'), 'float' => t('Float'), ); $form['cck_plan_fields_simple_type'] = array( '#type' => 'select', '#title' => t('Type'), '#default_value' => !empty($field['cck_plan_fields_simple_type']) ? $field['cck_plan_fields_simple_type'] : 'int', '#options' => $types, ); $form['cck_plan_fields_simple_titulo'] = array( '#type' => 'textfield', '#title' => t('Titulo del monto'), //'#required' => TRUE, '#default_value' => !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Monto'), ); $form['combine_weight_value'] = array( '#type' => 'textfield', '#title' => t('Weight value'), '#default_value' => isset($field['combine_weight_value'])? $field['combine_weight_value'] : 0, '#size' => 3, '#element_validate' => array('_element_validate_number'), ); return $form; case 'save': $save_settings = array( 'cck_plan_fields_simple_type', 'cck_plan_fields_simple_titulo', 'count_vocabulary', 'combine_weight_value', ); $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0; for($i = 0; $i <= $count_vocabulary_default; $i++) { $idx = $i > 0? 'vid_' . $i : 'vid'; $save_settings[] = $idx; $idx1 = $i > 0? 'vtid_' . $i : 'vtid'; $save_settings[] = $idx1; $idx1 = $i > 0? 'cck_plan_fields_simple_titulo_term_' . $i : 'cck_plan_fields_simple_titulo_term'; $save_settings[] = $idx1; $id = $i > 0? 'combine_weight_' . $i : 'combine_weight'; $save_settings[] = $id; } return $save_settings; case 'database columns': $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0; for($i = 0; $i <= $count_vocabulary_default; $i++) { $idx = $i > 0? 'tid_' . $i : 'tid'; $columns[$idx] = array( 'type' => 'int', 'not null' => FALSE, 'sortable' => TRUE, 'views' => TRUE, ); } $columns['value'] = array( 'type' => 'numeric', 'size' => 'normal', 'not null' => TRUE, 'default' => 0, 'precision' => 32, 'scale' => 0, ); return $columns; case 'views data': $data = content_views_field_views_data($field); $db_info = content_database_info($field); $table_alias = content_views_tablename($field); $titulo_monto = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Monto'); $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0; $ftypes = array( 'value' => $titulo_monto, ); for($i = 0; $i <= $count_vocabulary_default; $i++) { $idx = $i > 0? 'tid_' . $i : 'tid'; $idx1 = $i > 0? 'cck_plan_fields_simple_titulo_term_' . $i : 'cck_plan_fields_simple_titulo_term'; $ftypes[$idx] = !isset($field[$idx1]) && empty($field[$idx1]) ? $field[$idx1] : t('Term'); } foreach ($ftypes as $ftype => $label) { $copy = $data[$table_alias][$field['field_name'] . $ftype]; $copy['title'] = t($label); $copy['filter']['handler'] = 'content_handler_filter_many_to_one'; $copy['filter']['numeric'] = TRUE; unset($copy['field'], $copy['argument'], $copy['sort']); $data[$table_alias][$field['field_name'] . $ftype . '_many_to_one'] = $copy; $data[$table_alias][$field['field_name'] . $ftype]['argument']['handler'] = 'content_handler_argument_many_to_one'; if ($ftype != 'description') { $data[$table_alias][$field['field_name'] . $ftype]['argument']['numeric'] = TRUE; } } return $data; } } /** * Implementation of hook_content_is_empty(). */ function cck_plan_fields_combine_term_content_is_empty($item, $field) { $flat = TRUE; //check term $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0; for($i = 0; $i <= $count_vocabulary_default; $i++) { $idxt = $i > 0? 'tid_' . $i : 'tid'; if (!empty($item[$idxt]) && $item[$idxt]) { $flat = FALSE; } } if ($flat) { return $flat; } //check value data $flat = TRUE; foreach (array_keys($field['columns']) as $ftype) { if (!empty($item['value'])) { $flat = FALSE; } } return $flat; } /** * Implementation of hook_field_formatter_info(). */ function cck_plan_fields_combine_term_field_formatter_info() { $formatters = array( 'default' => array( 'label' => t('Default'), 'multiple values' => CONTENT_HANDLE_CORE, 'field types' => array('cck_plan_fields_combine_term'), ), ); return $formatters; } /** * Implementation of hook_theme(). */ function cck_plan_fields_combine_term_theme() { return array( // Shows address in the default view: Multilines 'cck_plan_fields_combine_term_formatter_default' => array( 'arguments' => array('element'), ), 'cck_plan_fields_combine_term_table' => array( 'arguments' => array('form' => NULL), ), ); } /** * Proxy theme function for cck_plan_fields formatters. */ function theme_cck_plan_fields_combine_term_formatter_generic($element) { $output = ''; $flag = explode('single_line_', $element['#formatter']); $field_name = $element['#field_name']; $type_name = $element['#type_name']; $field = content_fields($field_name, $type_name); $ftypes = array( 'tid' => $field['cck_plan_fields_simple_titulo_term'], 'value' => $field['cck_plan_fields_simple_titulo'], ); if ($flag[1] == 'tid') { $term = taxonomy_get_term($element['#item'][$flag[1]]); // If this term's vocabulary supports localization. if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) { $term->name = tt("taxonomy:term:$term->tid:name", $term->name); } $output = ''. $ftypes[$flag[1]] .': '. check_plain($term->name); } else { $output = ''. $ftypes[$flag[1]] .': '. number_format($element['#item'][$flag[1]], 2, '.', ','); } return $output; } function cck_plan_fields_combine_term_weight($field) { $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0; for($i = 0; $i <= $count_vocabulary_default; $i++) { $idxt = $i > 0? 'tid_' . $i : 'tid'; if (!empty($element['#item'][$idxt])) { $empty = FALSE; continue; } } if ($empty) { return ''; } $weights = array(); $weights[] = array( 'key' => 'value', 'title' => !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Monto'), 'weight' => isset($field['combine_weight_value'])? $field['combine_weight_value'] : 0, ); $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0; for($i = 0; $i <= $count_vocabulary_default; $i++) { $idx1 = $i > 0? 'cck_plan_fields_simple_titulo_term_' . $i : 'cck_plan_fields_simple_titulo_term'; $id = $i > 0? 'combine_weight_' . $i : 'combine_weight'; $idx = $i > 0? 'vid_' . $i : 'vid'; $idxtid = $i > 0? 'vtid_' . $i : 'vtid'; $idxt = $i > 0? 'tid_' . $i : 'tid'; $weights[] = array( 'key' => $idxt, 'title' => !empty($field[$idx1]) ? $field[$idx1] : t('Term'), 'weight' => isset($field[$id])? $field[$id] : 0, 'vid' => $idx, 'vtid' => $idxtid, ); } uasort($weights, 'combine_term_sort_weight'); return $weights; } /** * theme_cck_plan_fields_formatter_default(). * default formatter theme */ function theme_cck_plan_fields_combine_term_formatter_default($element) { $field_name = $element['#field_name']; $type_name = $element['#type_name']; $field = content_fields($field_name, $type_name); $output = ''; // If all fields are hidden, return '' $empty = TRUE; $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0; for($i = 0; $i <= $count_vocabulary_default; $i++) { $idxt = $i > 0? 'tid_' . $i : 'tid'; if (!empty($element['#item'][$idxt])) { $empty = FALSE; continue; } } if ($empty) { return ''; } $weights = cck_plan_fields_combine_term_weight($field); $i = 0; $items = array(); foreach($weights as $value) { if ($value['key'] == 'value') { $numero = number_format($element['#item'][$value['key']], $format_number['decimals'], $format_number['dec_point'], $format_number['thousands_sep']); $items[] = '
' . $value['title'] . ':
' . $numero . '
'; } else { if (isset($element['#item'][$value['key']]) && !empty($element['#item'][$value['key']]) && $element['#item'][$value['key']]) { $term = taxonomy_get_term($element['#item'][$value['key']]); if ($term) { if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) { $term->name = tt("taxonomy:term:$term->tid:name", $term->name); } $items[] = '
' . $value['title'] . ':
' . $term->name . '
'; } } } } return implode ('', $items); } /** * Implementation of hook_widget_info(). */ function cck_plan_fields_combine_term_widget_info() { return array( 'cck_plan_fields_combine_term' => array( 'label' => t('Items combine term Field'), 'field types' => array('cck_plan_fields_combine_term'), 'multiple values' => CONTENT_HANDLE_CORE, 'callbacks' => array('default value' => CONTENT_CALLBACK_DEFAULT), ), ); } /** * Implementation of hook_widget_settings(). */ function cck_plan_fields_combine_term_widget_settings($op, $widget) { switch ($op) { case 'form': //$form = array(); $form['min'] = array( '#type' => 'textfield', '#title' => t('Minimum'), '#element_validate' => array('_element_validate_number'), '#default_value' => is_numeric($widget['min']) ? $widget['min'] : '', ); $form['max'] = array( '#type' => 'textfield', '#title' => t('Maximum'), '#element_validate' => array('_element_validate_number'), '#default_value' => is_numeric($widget['max']) ? $widget['max'] : '', ); return $form; break; case 'save': return array( 'min', 'max', 'weight_combine', ); break; } } function combine_term_sort_weight($a, $b) { $a_weight = is_array($a) && isset($a['weight']) ? $a['weight'] : 0; $b_weight = is_array($b) && isset($b['weight']) ? $b['weight'] : 0; if ($a_weight == $b_weight) { return 0; } return $a_weight < $b_weight ? -1 : 1; } /** * Implementation of theme_cck_plan_fields_combine_term_table(). */ function theme_cck_plan_fields_combine_term_table($form = array()) { $field = content_fields($form['#field_name'], $form['#type_name']); $headers = array(); $rows = array(); $row = array(); $ftypes = array( 'value' => t('Cantidad'), ); $count_vocabulary_default = isset($field['count_vocabulary']) ? $field['count_vocabulary'] : 0; for($i = 0; $i <= $count_vocabulary_default; $i++) { $idx1 = $i > 0? 'cck_plan_fields_simple_titulo_term_' . $i : 'cck_plan_fields_simple_titulo_term'; $idx = $i > 0? 'vid_' . $i : 'vid'; $ftypes[$idx] = isset($field[$idx1]) && !empty($field[$idx1]) ? $field[$idx1] : t('Term'); } if (isset($form['#title']) && $form['#title']) { $field = str_replace('_', '-', $form['#field_name']); $output .= '
'; } $output = ''; foreach ($ftypes as $ftype => $label) { $output .= '
' . drupal_render($form[$ftype]) . '
'; } $output .= drupal_render($form); $output .= '
'; if (isset($form['#title']) && $form['#title']) { $output .= ''; } return $output; } /** * Implementation of hook_widget(). */ function cck_plan_fields_combine_term_widget(&$form, &$form_state, $field, $items, $delta = 0) { $field_name = $field['field_name']; $element = array(); $weights = cck_plan_fields_combine_term_weight($field); $i = 0; foreach($weights as $value) { if ($value['key'] == 'value') { $class = $field_name . '_' . $delta . '_field'; $class1 = ' ' . $field_name . '_m' . $i . '_field' . ' ' . $field_name . '_dato_field'; $field_id = $i ? 'value_' . $i : 'value'; $field_id_form = $i ? 'value-' . $i : 'value'; $default_value = isset($items[$delta][$value['key']]) && $items[$delta][$value['key']] ? $items[$delta][$value['key']] : 0; if ($field['cck_plan_fields_simple_type'] == 'float') { $default_value = isset($field['cck_plan_fields_simple_decimal']) ? str_replace('.', $field['cck_plan_fields_simple_decimal'], $default_value) : $default_value; } $element[$value['key']] = array( '#type' => 'textfield', //'#default_value' => isset($items[$delta][$value['key']]) && $items[$delta][$value['key']] ? $items[$delta][$value['key']] : 0, '#default_value' => $default_value, '#size' => 15, '#attributes' => array('class' => $class . $class1 . ' number', 'onchange' => "suma('$class');sumatexto('" . $field_name . '_dato_field' . "');sumatexto('" . $field_name . '_m' . $i . '_field' . "');"), ); } else { $tree = taxonomy_get_tree($field[$value['vid']]); $options = array(); if ($tree) { foreach ($tree as $term) { if (isset($field[$value['vtid']][$term->tid])) { $choice = new stdClass(); $choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name); $options[] = $choice; } } } $element[$value['key']] = array( '#type' => 'select', '#default_value' => isset($items[$delta][$value['key']]) ? $items[$delta][$value['key']] : NULL, '#options' => $options, ); } $element[$value['key']]['#title'] = $value['title']; } array_unshift($element['#element_validate'], 'cck_plan_fields_combine_term_validate'); $form_state['#field_info'][$element['#field_name']] = $form['#field_info'][$field_name]; // Used so that hook_field('validate') knows where to // flag an error in deeply nested forms. if (empty($form['#parents'])) { $form['#parents'] = array(); } $element['_error_element'] = array( '#type' => 'value', '#value' => implode('][', array_merge($form['#parents'], array('value'))), ); return $element; } /** * FAPI validation of an individual element. */ function cck_plan_fields_combine_term_validate($element, &$form_state) { $field_name = $element['#field_name']; if (!isset($element['_remove']['#post'][$field_name][$element['#delta']]['_remove'])) { $type_name = $element['#type_name']; $field = content_fields($field_name, $type_name); $min = is_numeric($field['widget']['min']); $max = is_numeric($field['widget']['max']); $cck_plan_fields_path = drupal_get_path('module', 'cck_plan_fields'); drupal_add_js($cck_plan_fields_path . '/js/cck_plan_fields.js'); $flag = FALSE; foreach ($element['#columns'] as $ftype) { if ($ftype != 'tid' && !empty($element[$ftype]['#value'])) { $error_field = implode('][', $element['#parents']) .'][' . $ftype; if (!is_numeric($element[$ftype]['#value'])) { form_set_error($error_field, t('Amount should be a number in %field.', array('%field' => t($field['widget']['label'])))); } elseif ($field['cck_plan_fields_simple_type'] == 'int') { $start = $element[$ftype]['#value']; $value = preg_replace('@[^-0-9]@', '', $start); if ($start != $value) { form_set_error($error_field, t('Only numbers are allowed in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label'])))); } } elseif ($min && $field['widget']['min'] > $element[$ftype]['#value']) { form_set_error($error_field, t('Amount should be greater %number in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label'])))); } elseif ($max && $field['widget']['max'] < $element[$ftype]['#value']) { form_set_error($error_field, t('Amount should be litter %number in %field.', array('%number' => $field['widget']['max'], '%field' => t($field['widget']['label'])))); } $flag = TRUE; } } if (!$flag) { return; } //inicio desde el primer elemento if ($element['#delta']) { $current_taxonomy = $element['tid']['#value']; for($i = 0; $i < $element['#delta']; $i++) { if ($current_taxonomy == $form_state['values'][$field_name][$i]['tid'] && !isset($element['_remove']['#post'][$field_name][$i]['_remove'])) { $error_field = implode('][', $element['#parents']) .'][tid'; 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']))); return; } } } } } /** * Implementation of hook_feeds_node_processor_targets_alter(). * * @see FeedsNodeProcessor::getMappingTargets() */ function cck_plan_fields_combine_term_feeds_node_processor_targets_alter(&$targets, $content_type) { $info = content_types($content_type); $fields = array(); if (isset($info['fields']) && count($info['fields'])) { foreach ($info['fields'] as $field_name => $field) { if (in_array($field['type'], array('cck_plan_fields_field'))) { $fields[$field_name] = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name; } } } $ftypes = array( 'tid' => t('Term'), 'value' => t('Cantidad'), ); foreach ($fields as $k => $name) { foreach($ftypes as $id => $month) { $targets[$k . ':' . $id] = array( 'name' => check_plain($name) . '(' . $month . ')', 'callback' => 'cck_plan_fields_combine_term_feeds_set_target', 'description' => t('The part @month_name of @name field of the node.', array('@name' => $name, '@month_name' => $month)), ); } } } /** * Implementation of cck_plan_fields_combine_term_feeds_set_target(). * Target callback function in Feeds */ function cck_plan_fields_combine_term_feeds_set_target($node, $target, $value) { $ftypes = array( 'tid' => t('Term'), 'value' => t('Cantidad'), ); $field_default = array(); foreach($ftypes as $id => $month) { $field_default[$id] = 0; } list($field_name, $sub_field) = explode(':', $target); $field = isset($node->$field_name) ? $node->$field_name : $field_default; // Handle multiple value fields. $field = content_fields($field_name, $node->type); if (is_array($value)) { $i = 0; foreach ($value as $v) { if (!is_array($v) && !is_object($v)) { if ($sub_field == 'tid') { $terms_found = content_taxonomy_get_term_by_name_vid($v, $field['vid']); if ($terms_found->tid) { $field[$i][$sub_field] = $terms_found->tid; } } elseif(isset($field[$i]['tid']) && $field[$i]['tid']) { $field[$i][$sub_field] = $v; } } $i++; } } else { if ($sub_field == 'tid') { $terms_found = cck_plan_fields_combine_term_get_term_by_name_vid($value, $field['vid']); if ($terms_found->tid) { $field[$i][$sub_field] = $terms_found->tid; } } elseif(isset($field[0]['tid']) && $field[0]['tid']) { $field[$i][$sub_field] = $value; } $field[0][$sub_field] = $value; } $node->$field_name = $field; } /** * Implementation of cck_plan_fields_combine_term_get_term_by_name_vid(). * Get term from name and vocabulary */ function cck_plan_fields_combine_term_get_term_by_name_vid($name, $vid) { $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); $result = array(); $term = db_fetch_object($db_result); if($term) { return $term->tid; } return FALSE; }