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; } $form['vid'] = array( '#type' => 'select', '#title' => t('Taxonomy'), '#default_value' => !empty($field['vid']) ? $field['vid'] : '', '#options' => $vocabulary_options, ); if (!empty($field['vid'])) { $vid = $field['vid']; //$terms = taxonomy_get_tree($vid); $tree = taxonomy_get_tree($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; } } $form['vtid'] = array( '#type' => 'select', '#default_value' => isset($field['vtid']) ? $field['vtid'] : array(), '#options' => $options, '#multiple' => TRUE, ); } $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_term'] = array( '#type' => 'textfield', '#title' => t('Titulo del Termino de la taxonomia'), //'#required' => TRUE, '#weight' => -9, '#default_value' => !empty($field['cck_plan_fields_simple_titulo_term']) ? $field['cck_plan_fields_simple_titulo_term'] : t('Term'), ); $form['cck_plan_fields_simple_titulo'] = array( '#type' => 'textfield', '#title' => t('Titulo del monto'), //'#required' => TRUE, '#weight' => -9, '#default_value' => !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Monto'), ); return $form; case 'save': $save_settings = array( 'vid', 'vtid', 'cck_plan_fields_simple_type', 'cck_plan_fields_simple_titulo_term', 'cck_plan_fields_simple_titulo', ); return $save_settings; case 'database columns': $columns['tid'] = 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_term = !empty($field['cck_plan_fields_simple_titulo_term']) ? $field['cck_plan_fields_simple_titulo_term'] : t('Term'); $titulo_monto = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Monto'); $ftypes = array( 'tid' => $titulo_term, 'value' => $titulo_monto, ); 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; foreach (array_keys($field['columns']) as $ftype) { if ($ftype != 'tid' && !empty($item[$ftype])) { $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_field'), ), ); 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; } /** * 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 '' if (empty($element['#item']['tid'])) { return $output; } $format_number = array( 'decimals' => variable_get('proyectos_operativos_number_decimals', 0), 'dec_point' => variable_get('proyectos_operativos_number_dec_point', ','), 'thousands_sep' => variable_get('proyectos_operativos_number_thousands_sep', '.'), ); $headers = array(); $rows = array(); $row = array(); $total = 0; // If this term's vocabulary supports localization. $term = taxonomy_get_term($element['#item']['tid']); if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) { $term->name = tt("taxonomy:term:$term->tid:name", $term->name); } $numero = number_format($element['#item']['value'], $format_number['decimals'], $format_number['dec_point'], $format_number['thousands_sep']); return $numero . ' ' . $term->name; } /** * Implementation of hook_widget_info(). */ function cck_plan_fields_combine_term_widget_info() { return array( 'cck_plan_fields_combine_term_elements' => array( 'label' => t('Items combine term Field'), 'field types' => array('cck_plan_fields_combine_term_field'), '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', ); break; } } /** * Implementation of theme_cck_plan_fields_combine_term_table(). */ function theme_cck_plan_fields_combine_term_table($form = array()) { $headers = array(); $rows = array(); $row = array(); $ftypes = array( 'value' => t('Cantidad'), 'tid' => t('Term'), ); $backgrounds = array( 'tid' => 'blue', 'value' => 'black', ); if (isset($form['#title']) && $form['#title']) { $field = str_replace('_', '-', $form['#field_name']); $output .= '
'; } $output = ''; foreach ($ftypes as $ftype => $label) { if($ftype != 'tid') { $output .= '
' . drupal_render($form[$ftype]) . '
'; } else { $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(); $ftypes = array( 'tid' => !empty($field['cck_plan_fields_simple_titulo_term']) ? $field['cck_plan_fields_simple_titulo_term'] : t('Term'), 'value' => !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Monto'), ); $ftypes['total'] = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Total'); $class = $field_name . '_' . $delta . '_field'; $vid = $field['vid']; $tree = taxonomy_get_tree($vid); $options = array(); if ($tree) { foreach ($tree as $term) { if (isset($field['vtid'][$term->tid])) { $choice = new stdClass(); $choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name); $options[] = $choice; } } } $element['tid'] = array( '#type' => 'select', '#default_value' => isset($items[$delta]['tid']) ? $items[$delta]['tid'] : NULL, '#options' => $options, ); if ($delta == 0) { $element['tid']['#title'] = $ftypes['tid']; } for ($i = 0; $i < 1; $i++) { $class1 = ' ' . $field_name . '_m' . $i . '_field' . ' ' . $field_name . '_dato_field'; $field_id = $i ? 'value_' . $i : 'value'; $field_id_form = $i ? 'value-' . $i : 'value'; $element[$field_id] = array( '#type' => 'textfield', '#default_value' => isset($items[$delta][$field_id]) && $items[$delta][$field_id] ? $items[$delta][$field_id] : 0, '#size' => 15, '#attributes' => array('class' => $class . $class1 . ' number', 'onchange' => "suma('$class');sumatexto('" . $field_name . '_dato_field' . "');sumatexto('" . $field_name . '_m' . $i . '_field' . "');"), ); if ($delta == 0) { $element[$field_id]['#title'] = $ftypes[$field_id]; } } $element['#theme'] = 'cck_plan_fields_combine_term_table'; if (empty($element['#element_validate'])) { $element['#element_validate'] = array(); } 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']; $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; } 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']) { $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']))); } } } $type_name = $element['#type_name']; $field = content_fields($field_name, $type_name); $field_key = $element['#columns'][0]; } /** * 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; }