source: sipes/0.3-modules/cck_plan_fields/cck_plan_fields_simple.module @ 5c31d5d

stableversion-3.0
Last change on this file since 5c31d5d was 5d26291, checked in by lhernandez <lhernandez@…>, 8 años ago

se modifico el separador

  • Propiedad mode establecida a 100755
File size: 20.9 KB
Línea 
1<?php
2
3/**
4* Implementation of hook_field_info().
5*/
6function cck_plan_fields_simple_field_info() {
7  return array(
8    'cck_plan_fields_simple_field' => array(
9      'label' => t('Plan line field simple'),
10      'description' => t('Store items operative plan data in the database.'),
11    ),
12  );
13}
14
15/**
16* Implementation of hook_field_settings().
17*/
18function cck_plan_fields_simple_field_settings($op, $field) {
19  switch ($op) {
20    case 'form':
21      $form = array();
22      $types = array(
23        'int' => t('Integer'),
24        'float' => t('Floar'),
25      );
26      $form['cck_plan_fields_simple_type'] = array(
27        '#type' => 'select',
28        '#title' => t('Type'),
29        '#default_value' => !empty($field['cck_plan_fields_simple_type']) ? $field['cck_plan_fields_simple_type'] : 'int',
30        '#options' => $types,
31      );
32      $form['cck_plan_fields_simple_titulo'] = array(
33        '#type' => 'textfield',
34        '#title' => t('Titulo del total'),
35        //'#required' => TRUE,
36        '#weight' => -9,
37        '#default_value' => !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : '',
38      );
39      return $form;
40    case 'save':
41      $save_settings = array(
42        'cck_plan_fields_simple_type',
43        'cck_plan_fields_simple_titulo',
44      );
45      return $save_settings;
46    case 'database columns':
47      $type_select = !empty($field['cck_plan_fields_simple_type']) ? $field['cck_plan_fields_simple_type'] : 'int';
48      $types = array(
49        'int' => t('Integer'),
50        'float' => t('Floar'),
51      );
52      $type_select = isset($types[$type_select]) ? $type_select : 'int';
53      for ($i = 0; $i < 12; $i++) {
54        $field_id = $i ? 'value_' . $i : 'value';
55        if ($type_select == 'int') {
56          $columns[$field_id] = array(
57            'type' => $type_select,
58            'not null' => FALSE,
59            'sortable' => TRUE,
60            'views' => TRUE,
61          );
62        }
63        else {
64          $columns[$field_id] = array(
65            'type' => 'numeric',
66            'size' => 'normal',
67            'not null' => TRUE,
68            'default' => 0,
69            'precision' => 32,
70            'scale' => 0,
71          );
72        }
73
74      }
75      return $columns;
76    case 'views data':
77      $data = content_views_field_views_data($field);
78      $db_info = content_database_info($field);
79      $table_alias = content_views_tablename($field);
80      $ftypes = array(
81        'value' => t('January'),
82        'value_1' => t('February'),
83        'value_2' => t('March'),
84        'value_3' => t('April'),
85        'value_4' => t('May'),
86        'value_5' => t('June'),
87        'value_6' => t('July'),
88        'value_7' => t('August'),
89        'value_8' => t('September'),
90        'value_9' => t('Octuber'),
91        'value_10' => t('November'),
92        'value_11' => t('December'),
93      );
94      foreach ($ftypes as $ftype => $label) {
95        $copy = $data[$table_alias][$field['field_name'] . $ftype];
96        $copy['title'] = t($label);
97        $copy['filter']['handler'] = 'content_handler_filter_many_to_one';
98        $copy['filter']['numeric'] = TRUE;
99        unset($copy['field'], $copy['argument'], $copy['sort']);
100        $data[$table_alias][$field['field_name'] . $ftype . '_many_to_one'] = $copy;
101        $data[$table_alias][$field['field_name'] . $ftype]['argument']['handler'] = 'content_handler_argument_many_to_one';
102        if ($ftype != 'description') {
103          $data[$table_alias][$field['field_name'] . $ftype]['argument']['numeric'] = TRUE;
104        }
105      }
106      return $data;
107  }
108}
109
110/**
111 * Implementation of hook_content_is_empty().
112 */
113function cck_plan_fields_simple_content_is_empty($item, $field) {
114  $flat = TRUE;
115  foreach (array_keys($field['columns']) as $ftype) {
116    if (!empty($item[$ftype])) {
117      return FALSE;
118    }
119  }
120  return $flat;
121}
122
123/**
124 * Implementation of hook_field_formatter_info().
125 */
126function cck_plan_fields_simple_field_formatter_info() {
127  $formatters = array(
128    'default' => array(
129      'label'  => t('Default'),
130      'multiple values' => CONTENT_HANDLE_CORE,
131      'field types'  => array('cck_plan_fields_simple_field'),
132    ),
133    'single_line' => array(
134      'label'  => t('Single Line'),
135      'multiple values' => CONTENT_HANDLE_CORE,
136      'field types'  => array('cck_plan_fields_simple_field'),
137    ),
138  );
139  $ftypes = array(
140    'value' => t('January'),
141    'value_1' => t('February'),
142    'value_2' => t('March'),
143    'value_3' => t('April'),
144    'value_4' => t('May'),
145    'value_5' => t('June'),
146    'value_6' => t('July'),
147    'value_7' => t('August'),
148    'value_8' => t('September'),
149    'value_9' => t('Octuber'),
150    'value_10' => t('November'),
151    'value_11' => t('December'),
152  );
153  foreach ($ftypes as $value => $label) {
154    $formatters['single_line_' . $value] = array(
155      'label'  => t('Single Line ') . $label,
156      'multiple values' => CONTENT_HANDLE_CORE,
157      'field types'  => array('cck_plan_fields_simple_field'),
158    );
159  }
160  return $formatters;
161}
162
163/**
164 * Implementation of hook_theme().
165 */
166function cck_plan_fields_simple_theme() {
167  return array(
168    'cck_plan_fields_simple_formatter_default' => array(
169      'arguments' => array('element'),
170    ),
171    'cck_plan_fields_simple_formatter_single_line' => array(
172      'arguments' => array('element'),
173    ),
174    'cck_plan_fields_simple_formatter_single_line_value' => array(
175      'arguments' => array('form' => NULL),
176      'function' => 'theme_cck_plan_fields_simple_formatter_generic',
177    ),
178    'cck_plan_fields_simple_formatter_single_line_value1' => array(
179      'arguments' => array('form' => NULL),
180      'function' => 'theme_cck_plan_fields_simple_formatter_generic',
181    ),
182    'cck_plan_fields_simple_formatter_single_line_value2' => array(
183      'arguments' => array('form' => NULL),
184      'function' => 'theme_cck_plan_fields_simple_formatter_generic',
185    ),
186    'cck_plan_fields_simple_formatter_single_line_value3' => array(
187      'arguments' => array('form' => NULL),
188      'function' => 'theme_cck_plan_fields_simple_formatter_generic'
189    ),
190    'cck_plan_fields_simple_formatter_single_line_value4' => array(
191      'arguments' => array('form' => NULL),
192      'function' => 'theme_cck_plan_fields_simple_formatter_generic',
193    ),
194    'cck_plan_fields_simple_formatter_single_line_value5' => array(
195      'arguments' => array('form' => NULL),
196      'function' => 'theme_cck_plan_fields_simple_formatter_generic',
197    ),
198    'cck_plan_fields_simple_formatter_single_line_value6' => array(
199      'arguments' => array('form' => NULL),
200      'function' => 'theme_cck_plan_fields_simple_formatter_generic',
201    ),
202    'cck_plan_fields_simple_formatter_single_line_value7' => array(
203      'arguments' => array('form' => NULL),
204      'function' => 'theme_cck_plan_fields_simple_formatter_generic',
205    ),
206    'cck_plan_fields_simple_formatter_single_line_value8' => array(
207      'arguments' => array('form' => NULL),
208      'function' => 'theme_cck_plan_fields_simple_formatter_generic',
209    ),
210    'cck_plan_fields_simple_formatter_single_line_value9' => array(
211      'arguments' => array('form' => NULL),
212      'function' => 'theme_cck_plan_fields_simple_formatter_generic',
213    ),
214    'cck_plan_fields_simple_formatter_single_line_value10' => array(
215      'arguments' => array('form' => NULL),
216      'function' => 'theme_cck_plan_fields_simple_formatter_generic',
217    ),
218    'cck_plan_fields_simple_formatter_single_line_value11' => array(
219      'arguments' => array('form' => NULL),
220      'function' => 'theme_cck_plan_fields_simple_formatter_generic',
221    ),
222    'cck_plan_fields_simple_table' => array(
223      'arguments' => array('form' => NULL),
224    ),
225  );
226}
227
228/**
229 * Proxy theme function for cck_plan_fields_simple formatters.
230 */
231function theme_cck_plan_fields_simple_formatter_generic($element) {
232  $output = '';
233  $format_number = array(
234    'decimals' => variable_get('proyectos_operativos_number_decimals', 0),
235    'dec_point' => variable_get('proyectos_operativos_number_dec_point', ','),
236    'thousands_sep' => variable_get('proyectos_operativos_number_thousands_sep', '.'),
237  );
238  $flag = explode('single_line_', $element['#formatter']);
239  $ftypes = array(
240    'value' => t('January'),
241    'value_1' => t('February'),
242    'value_2' => t('March'),
243    'value_3' => t('April'),
244    'value_4' => t('May'),
245    'value_5' => t('June'),
246    'value_6' => t('July'),
247    'value_7' => t('August'),
248    'value_8' => t('September'),
249    'value_9' => t('Octuber'),
250    'value_10' => t('November'),
251    'value_11' => t('December'),
252  );
253  $output = '<strong>'. $ftypes[$flag[1]] .': </strong>'. number_format($element['#item'][$flag[1]], $format_number['decimals'], $format_number['dec_point'], $format_number['thousands_sep']);
254  return $output;
255}
256
257/**
258 * theme_cck_plan_fields_formatter_default().
259 * default formatter theme
260 */
261function theme_cck_plan_fields_simple_formatter_default($element) {
262  $output = '';
263  $ftypes = array(
264    'value' => t('January'),
265    'value_1' => t('February'),
266    'value_2' => t('March'),
267    'value_3' => t('April'),
268    'value_4' => t('May'),
269    'value_5' => t('June'),
270    'value_6' => t('July'),
271    'value_7' => t('August'),
272    'value_8' => t('September'),
273    'value_9' => t('Octuber'),
274    'value_10' => t('November'),
275    'value_11' => t('December'),
276  );
277  $format_number = array(
278    'decimals' => variable_get('proyectos_operativos_number_decimals', 0),
279    'dec_point' => variable_get('proyectos_operativos_number_dec_point', ','),
280    'thousands_sep' => variable_get('proyectos_operativos_number_thousands_sep', '.'),
281  );
282  $ftypes['total'] = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Total');
283  $headers = array();
284  $rows = array();
285  $row = array();
286  $empty = TRUE;
287  $total = 0; 
288  foreach ($ftypes as $value => $label) {
289    $headers[] = array('data' => $label);
290    if (!empty($element['#item'][$value])) {
291      $empty = FALSE;
292    }
293    $total += $element['#item'][$value];
294    if ($value != 'total') {
295      $row[] = array('data' => number_format($element['#item'][$value], $format_number['decimals'], $format_number['dec_point'], $format_number['thousands_sep']));
296    }
297  }
298  if ($empty) {
299    return '';
300  }
301  $row[] = array('data' => number_format($total, $format_number['decimals'], $format_number['dec_point'], $format_number['thousands_sep']));
302  $rows[] = $row;
303  return theme('table', $headers, $rows);
304}
305
306/**
307 * theme_cck_plan_fields_formatter_single_line().
308 * display line items products in a single line
309 */
310function theme_cck_plan_fields_simple_formatter_single_line($element) {
311  $output = '';
312  // If all fields are hidden, return ''
313  if (empty($element)) {
314    return $output;
315  }
316  $ftypes = array(
317    'value' => t('January'),
318    'value_1' => t('February'),
319    'value_2' => t('March'),
320    'value_3' => t('April'),
321    'value_4' => t('May'),
322    'value_5' => t('June'),
323    'value_6' => t('July'),
324    'value_7' => t('August'),
325    'value_8' => t('September'),
326    'value_9' => t('Octuber'),
327    'value_10' => t('November'),
328    'value_11' => t('December'),
329  );
330  $format_number = array(
331    'decimals' => variable_get('proyectos_operativos_number_decimals', 0),
332    'dec_point' => variable_get('proyectos_operativos_number_dec_point', ','),
333    'thousands_sep' => variable_get('proyectos_operativos_number_thousands_sep', '.'),
334  );
335  foreach ($ftypes as $value => $label) {
336    $output .= ' <strong>'. $label .': </strong>'. number_format($element['#item'][$value], $format_number['decimals'], $format_number['dec_point'], $format_number['thousands_sep']);
337  }
338  return '<div class="items-plan-field">'. $output .'</div>';
339}
340
341/**
342 * Implementation of hook_widget_info().
343 */
344function cck_plan_fields_simple_widget_info() {
345  return array(
346    'cck_plan_fields_simple_elements' => array(
347      'label'           => t('Items Plan Field Simple'),
348      'field types'     => array('cck_plan_fields_simple_field'),
349      'multiple values' => CONTENT_HANDLE_CORE,
350      'callbacks'       => array('default value' => CONTENT_CALLBACK_DEFAULT),
351    ),
352  );
353}
354
355/**
356* Implementation of hook_widget_settings().
357*/
358function cck_plan_fields_simple_widget_settings($op, $widget) {
359  switch ($op) {
360    case 'form':
361      $form = array();
362      $form['min'] = array(
363        '#type' => 'textfield',
364        '#title' => t('Minimum'),
365        '#element_validate' => array('_element_validate_number'),
366        '#default_value' => is_numeric($widget['min']) ? $widget['min'] : '',
367      );
368      $form['max'] = array(
369        '#type' => 'textfield',
370        '#title' => t('Maximum'),
371        '#element_validate' => array('_element_validate_number'),
372        '#default_value' => is_numeric($widget['max']) ? $widget['max'] : '',
373      );
374      return $form;
375    break;
376    case 'save':
377      return array(
378        'min',
379        'max',
380      );
381    break;
382  }
383}
384
385function theme_cck_plan_fields_simple_table($form = array()) {
386  $headers = array();
387  $rows = array();
388  $row = array();
389  $ftypes = array(
390    'value' => t('Ene'),
391    'value_1' => t('Feb'),
392    'value_2' => t('Mar'),
393    'value_3' => t('Abr'),
394    'value_4' => t('May'),
395    'value_5' => t('Jun'),
396    'value_6' => t('Jul'),
397    'value_7' => t('Aug'),
398    'value_8' => t('Sep'),
399    'value_9' => t('Oct'),
400    'value_10' => t('Nov'),
401    'value_11' => t('Dic'),
402  );
403  $ftypes['total'] = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Total');
404  $backgrounds = array(
405    'value' => 'black',
406    'value_1' => 'blue',
407    'value_2' => 'white',
408    'value_3' => 'blue',
409    'value_4' => 'white',
410    'value_5' => 'blue',
411    'value_6' => 'white',
412    'value_7' => 'blue',
413    'value_8' => 'white',
414    'value_9' => 'blue',
415    'value_10' => 'white',
416    'value_11' => 'blue',
417  );
418  if (isset($form['#title']) && $form['#title']) {
419    $field = str_replace('_', '-', $form['#field_name']);
420    $output .= '<div id="edit-' . $field . '-0-value-wrapper" class="form-item"><label for="edit-' . $field . '-0-value">' . $form['#title'] . '</label>';
421  }
422  $output .= '<div style="width:2050px; float:none">';
423  foreach ($ftypes as $ftype => $label) {
424    $output .= '<div style="width:150px;float:left;margin-left:5px">' . drupal_render($form[$ftype]) . '</div>';
425  }
426  $output .= drupal_render($form);
427  $output .= '</div>';
428  if (isset($form['#title']) && $form['#title']) {
429    $output .= '</div>';
430  }
431  return $output;
432}
433
434/**
435 * Implementation of hook_widget().
436 */
437function cck_plan_fields_simple_widget(&$form, &$form_state, $field, $items, $delta = 0) {
438  $cck_plan_fields_simple_path = drupal_get_path('module', 'cck_plan_fields_simple');
439  drupal_add_js($cck_plan_fields_simple_path . '/js/cck_plan_fields.js');
440  $field_name = $field['field_name'];
441  $element = array();
442  $ftypes = array(
443    'value' => t('Ene'),
444    'value_1' => t('Feb'),
445    'value_2' => t('Mar'),
446    'value_3' => t('Abr'),
447    'value_4' => t('May'),
448    'value_5' => t('Jun'),
449    'value_6' => t('Jul'),
450    'value_7' => t('Aug'),
451    'value_8' => t('Sep'),
452    'value_9' => t('Oct'),
453    'value_10' => t('Nov'),
454    'value_11' => t('Dic'),
455  );
456  $ftypes['total'] = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Total');
457  $total = 0;
458  $class = $field_name . '_' . $delta . '_field';
459  for ($i = 0; $i < 12; $i++) {
460    $class1 = ' ' . $field_name . '_m' . $i . '_field';
461    $field_id = $i ? 'value_' . $i : 'value';
462    $field_id_form = $i ? 'value-' . $i : 'value';
463    $element[$field_id] = array(
464      '#type' => 'textfield',
465      '#default_value' => isset($items[$delta][$field_id]) && $items[$delta][$field_id] ? $items[$delta][$field_id] : 0,
466      '#size' => 15,
467      '#attributes' => array('class' => $class . $class1 . ' number', 'onchange' => "suma('$class')"),
468    );
469    $total += $element[$field_id]['#default_value'];
470    if ($delta == 0) {
471      $element[$field_id]['#title'] = $ftypes[$field_id];
472    }
473  }
474  $element['total'] = array(
475    '#type' => 'textfield',
476    '#default_value' => number_format($total, 0, '.', ''),
477    '#size' => 15,
478    '#attributes' => array('class' => $class . '_total totales'),
479  );
480  if ($delta == 0) {
481    $element['total']['#title'] = $ftypes['total'];
482  }
483  $element['#theme'] = 'cck_plan_fields_simple_table';
484  if (empty($element['#element_validate'])) {
485    $element['#element_validate'] = array();
486  }
487  array_unshift($element['#element_validate'], 'cck_plan_fields_simple_validate');
488  $form_state['#field_info'][$element['#field_name']] = $form['#field_info'][$field_name];
489  // Used so that hook_field('validate') knows where to
490  // flag an error in deeply nested forms.
491  if (empty($form['#parents'])) {
492    $form['#parents'] = array();
493  }
494  $element['_error_element'] = array(
495    '#type' => 'value',
496    '#value' => implode('][', array_merge($form['#parents'], array('value'))),
497  );
498  return $element;
499}
500
501/**
502 * FAPI validation of an individual element.
503 */
504function cck_plan_fields_simple_validate($element, &$form_state) {
505  $field_name = $element['#field_name'];
506  $type_name = $element['#type_name'];
507  $field = content_fields($field_name, $type_name);
508  $min = is_numeric($field['widget']['min']);
509  $max = is_numeric($field['widget']['max']);
510  $cck_plan_fields_simple_path = drupal_get_path('module', 'cck_plan_fields_simple');
511  drupal_add_js($cck_plan_fields_simple_path . '/js/cck_plan_fields.js');
512  $flag = FALSE;
513  foreach ($element['#columns'] as $ftype) {
514    if (!empty($element[$ftype]['#value'])) {
515      $error_field = implode('][', $element['#parents']) .'][' . $ftype;
516      if (!is_numeric($element[$ftype]['#value'])) {
517        form_set_error($error_field, t('Amount should be a number in %field.', array('%field' => t($field['widget']['label']))));
518      }
519      elseif ($field['cck_plan_fields_simple_type'] == 'int') {
520        $start = $element[$ftype]['#value'];
521        $value = preg_replace('@[^-0-9]@', '', $start);
522        if ($start != $value) {
523          form_set_error($error_field, t('Only numbers are allowed in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label']))));
524        }
525      }
526      elseif ($min && $field['widget']['min'] > $element[$ftype]['#value']) {
527        form_set_error($error_field, t('Amount should be greater %number in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label']))));
528      }
529      elseif ($max && $field['widget']['max'] < $element[$ftype]['#value']) {
530        form_set_error($error_field, t('Amount should be litter %number in %field.', array('%number' => $field['widget']['max'], '%field' => t($field['widget']['label']))));
531      }
532      $flag = TRUE;
533    }
534  }
535  if (!$flag) {
536    return;
537  }
538  $type_name = $element['#type_name'];
539  $field = content_fields($field_name, $type_name);
540  $field_key = $element['#columns'][0];
541}
542
543
544/**
545 * Implementation of hook_feeds_node_processor_targets_alter().
546 *
547 * @see FeedsNodeProcessor::getMappingTargets()
548 */
549function cck_plan_fields_simple_feeds_node_processor_targets_alter(&$targets, $content_type) {
550  $info = content_types($content_type);
551  $fields = array();
552  if (isset($info['fields']) && count($info['fields'])) {
553    foreach ($info['fields'] as $field_name => $field) {
554      if (in_array($field['type'], array('cck_plan_fields_simple_field'))) {
555        $fields[$field_name] = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name;
556      }
557    }
558  }
559  $ftypes = array(
560    'value' => t('January'),
561    'value_1' => t('February'),
562    'value_2' => t('March'),
563    'value_3' => t('April'),
564    'value_4' => t('May'),
565    'value_5' => t('June'),
566    'value_6' => t('July'),
567    'value_7' => t('August'),
568    'value_8' => t('September'),
569    'value_9' => t('Octuber'),
570    'value_10' => t('November'),
571    'value_11' => t('December'),
572  );
573
574  foreach ($fields as $k => $name) {
575    foreach($ftypes as $id => $month) {
576      $targets[$k . ':' . $id] = array(
577        'name' => check_plain($name) . '(' . $month . ')',
578        'callback' => 'cck_plan_fields_simple_feeds_set_target',
579        'description' => t('The part @month_name of @name field of the node.', array('@name' => $name, '@month_name' => $month)),
580      );
581    }
582  }
583}
584
585function cck_plan_fields_simple_feeds_set_target($node, $target, $value) {
586  list($field_name, $sub_field) = explode(':', $target);
587  $ftypes = array(
588    'value' => t('January'),
589    'value_1' => t('February'),
590    'value_2' => t('March'),
591    'value_3' => t('April'),
592    'value_4' => t('May'),
593    'value_5' => t('June'),
594    'value_6' => t('July'),
595    'value_7' => t('August'),
596    'value_8' => t('September'),
597    'value_9' => t('Octuber'),
598    'value_10' => t('November'),
599    'value_11' => t('December'),
600  );
601  $field_default = array();
602  foreach($ftypes as $id => $month) {
603    $field_default[$id] = 0;
604  }
605  $field = isset($node->$field_name) ? $node->$field_name : $field_default;
606  // Handle multiple value fields.
607  if (is_array($value)) {
608    $i = 0;
609    foreach ($value as $v) {
610      if (!is_array($v) && !is_object($v)) {
611        $field[$i][$sub_field] = $v;
612      }
613      $i++;
614    }
615  }
616  else {
617    $field[0][$sub_field] = $value;
618  }
619  $node->$field_name = $field;
620}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.