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

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

se agregaron las nuevas funcionalidades referentes al monto

  • Propiedad mode establecida a 100755
File size: 19.8 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  $flag = explode('single_line_', $element['#formatter']);
234  $ftypes = array(
235    'value' => t('January'),
236    'value_1' => t('February'),
237    'value_2' => t('March'),
238    'value_3' => t('April'),
239    'value_4' => t('May'),
240    'value_5' => t('June'),
241    'value_6' => t('July'),
242    'value_7' => t('August'),
243    'value_8' => t('September'),
244    'value_9' => t('Octuber'),
245    'value_10' => t('November'),
246    'value_11' => t('December'),
247  );
248  $output = '<strong>'. $ftypes[$flag[1]] .': </strong>'. number_format($element['#item'][$flag[1]], 2, '.', ',');
249  return $output;
250}
251
252/**
253 * theme_cck_plan_fields_formatter_default().
254 * default formatter theme
255 */
256function theme_cck_plan_fields_simple_formatter_default($element) {
257  $output = '';
258  $ftypes = array(
259    'value' => t('January'),
260    'value_1' => t('February'),
261    'value_2' => t('March'),
262    'value_3' => t('April'),
263    'value_4' => t('May'),
264    'value_5' => t('June'),
265    'value_6' => t('July'),
266    'value_7' => t('August'),
267    'value_8' => t('September'),
268    'value_9' => t('Octuber'),
269    'value_10' => t('November'),
270    'value_11' => t('December'),
271  );
272  $ftypes['total'] = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Total');
273  $headers = array();
274  $rows = array();
275  $row = array();
276  $empty = TRUE;
277  $total = 0; 
278  foreach ($ftypes as $value => $label) {
279    $headers[] = array('data' => $label);
280    if (!empty($element['#item'][$value])) {
281      $empty = FALSE;
282    }
283    $total += $element['#item'][$value];
284    if ($value != 'total') {
285      $row[] = array('data' => number_format($element['#item'][$value], 2, '.', ','));
286    }
287  }
288  if ($empty) {
289    return '';
290  }
291  $row[] = array('data' => number_format($total, 2, '.', ','));
292  $rows[] = $row;
293  return theme('table', $headers, $rows);
294}
295
296/**
297 * theme_cck_plan_fields_formatter_single_line().
298 * display line items products in a single line
299 */
300function theme_cck_plan_fields_simple_formatter_single_line($element) {
301  $output = '';
302  // If all fields are hidden, return ''
303  if (empty($element)) {
304    return $output;
305  }
306  $ftypes = array(
307    'value' => t('January'),
308    'value_1' => t('February'),
309    'value_2' => t('March'),
310    'value_3' => t('April'),
311    'value_4' => t('May'),
312    'value_5' => t('June'),
313    'value_6' => t('July'),
314    'value_7' => t('August'),
315    'value_8' => t('September'),
316    'value_9' => t('Octuber'),
317    'value_10' => t('November'),
318    'value_11' => t('December'),
319  );
320  foreach ($ftypes as $value => $label) {
321    $output .= ' <strong>'. $label .': </strong>'. number_format($element['#item'][$value], 2, '.', ',');
322  }
323  return '<div class="items-plan-field">'. $output .'</div>';
324}
325
326/**
327 * Implementation of hook_widget_info().
328 */
329function cck_plan_fields_simple_widget_info() {
330  return array(
331    'cck_plan_fields_simple_elements' => array(
332      'label'           => t('Items Plan Field Simple'),
333      'field types'     => array('cck_plan_fields_simple_field'),
334      'multiple values' => CONTENT_HANDLE_CORE,
335      'callbacks'       => array('default value' => CONTENT_CALLBACK_DEFAULT),
336    ),
337  );
338}
339
340/**
341* Implementation of hook_widget_settings().
342*/
343function cck_plan_fields_simple_widget_settings($op, $widget) {
344  switch ($op) {
345    case 'form':
346      $form = array();
347      $form['min'] = array(
348        '#type' => 'textfield',
349        '#title' => t('Minimum'),
350        '#element_validate' => array('_element_validate_number'),
351        '#default_value' => is_numeric($widget['min']) ? $widget['min'] : '',
352      );
353      $form['max'] = array(
354        '#type' => 'textfield',
355        '#title' => t('Maximum'),
356        '#element_validate' => array('_element_validate_number'),
357        '#default_value' => is_numeric($widget['max']) ? $widget['max'] : '',
358      );
359      return $form;
360    break;
361    case 'save':
362      return array(
363        'min',
364        'max',
365      );
366    break;
367  }
368}
369
370function theme_cck_plan_fields_simple_table($form = array()) {
371  $headers = array();
372  $rows = array();
373  $row = array();
374  $ftypes = array(
375    'value' => t('Ene'),
376    'value_1' => t('Feb'),
377    'value_2' => t('Mar'),
378    'value_3' => t('Abr'),
379    'value_4' => t('May'),
380    'value_5' => t('Jun'),
381    'value_6' => t('Jul'),
382    'value_7' => t('Aug'),
383    'value_8' => t('Sep'),
384    'value_9' => t('Oct'),
385    'value_10' => t('Nov'),
386    'value_11' => t('Dic'),
387  );
388  $ftypes['total'] = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Total');
389  $backgrounds = array(
390    'value' => 'black',
391    'value_1' => 'blue',
392    'value_2' => 'white',
393    'value_3' => 'blue',
394    'value_4' => 'white',
395    'value_5' => 'blue',
396    'value_6' => 'white',
397    'value_7' => 'blue',
398    'value_8' => 'white',
399    'value_9' => 'blue',
400    'value_10' => 'white',
401    'value_11' => 'blue',
402  );
403  if (isset($form['#title']) && $form['#title']) {
404    $field = str_replace('_', '-', $form['#field_name']);
405    $output .= '<div id="edit-' . $field . '-0-value-wrapper" class="form-item"><label for="edit-' . $field . '-0-value">' . $form['#title'] . '</label>';
406  }
407  $output .= '<div style="width:2050px; float:none">';
408  foreach ($ftypes as $ftype => $label) {
409    $output .= '<div style="width:150px;float:left;margin-left:5px">' . drupal_render($form[$ftype]) . '</div>';
410  }
411  $output .= drupal_render($form);
412  $output .= '</div>';
413  if (isset($form['#title']) && $form['#title']) {
414    $output .= '</div>';
415  }
416  return $output;
417}
418
419/**
420 * Implementation of hook_widget().
421 */
422function cck_plan_fields_simple_widget(&$form, &$form_state, $field, $items, $delta = 0) {
423  $cck_plan_fields_simple_path = drupal_get_path('module', 'cck_plan_fields_simple');
424  drupal_add_js($cck_plan_fields_simple_path . '/js/cck_plan_fields.js');
425  $field_name = $field['field_name'];
426  $element = array();
427  $ftypes = array(
428    'value' => t('Ene'),
429    'value_1' => t('Feb'),
430    'value_2' => t('Mar'),
431    'value_3' => t('Abr'),
432    'value_4' => t('May'),
433    'value_5' => t('Jun'),
434    'value_6' => t('Jul'),
435    'value_7' => t('Aug'),
436    'value_8' => t('Sep'),
437    'value_9' => t('Oct'),
438    'value_10' => t('Nov'),
439    'value_11' => t('Dic'),
440  );
441  $ftypes['total'] = !empty($field['cck_plan_fields_simple_titulo']) ? $field['cck_plan_fields_simple_titulo'] : t('Total');
442  $total = 0;
443  $class = $field_name . '_' . $delta . '_field';
444  for ($i = 0; $i < 12; $i++) {
445    $class1 = ' ' . $field_name . '_m' . $i . '_field';
446    $field_id = $i ? 'value_' . $i : 'value';
447    $field_id_form = $i ? 'value-' . $i : 'value';
448    $element[$field_id] = array(
449      '#type' => 'textfield',
450      '#default_value' => isset($items[$delta][$field_id]) && $items[$delta][$field_id] ? $items[$delta][$field_id] : 0,
451      '#size' => 15,
452      '#attributes' => array('class' => $class . $class1 . ' number', 'onchange' => "suma('$class')"),
453    );
454    $total += $element[$field_id]['#default_value'];
455    if ($delta == 0) {
456      $element[$field_id]['#title'] = $ftypes[$field_id];
457    }
458  }
459  $element['total'] = array(
460    '#type' => 'textfield',
461    '#default_value' => number_format($total, 0, '.', ''),
462    '#size' => 15,
463    '#attributes' => array('class' => $class . '_total totales'),
464  );
465  if ($delta == 0) {
466    $element['total']['#title'] = $ftypes['total'];
467  }
468  $element['#theme'] = 'cck_plan_fields_simple_table';
469  if (empty($element['#element_validate'])) {
470    $element['#element_validate'] = array();
471  }
472  array_unshift($element['#element_validate'], 'cck_plan_fields_simple_validate');
473  $form_state['#field_info'][$element['#field_name']] = $form['#field_info'][$field_name];
474  // Used so that hook_field('validate') knows where to
475  // flag an error in deeply nested forms.
476  if (empty($form['#parents'])) {
477    $form['#parents'] = array();
478  }
479  $element['_error_element'] = array(
480    '#type' => 'value',
481    '#value' => implode('][', array_merge($form['#parents'], array('value'))),
482  );
483  return $element;
484}
485
486/**
487 * FAPI validation of an individual element.
488 */
489function cck_plan_fields_simple_validate($element, &$form_state) {
490  $field_name = $element['#field_name'];
491  $type_name = $element['#type_name'];
492  $field = content_fields($field_name, $type_name);
493  $min = is_numeric($field['widget']['min']);
494  $max = is_numeric($field['widget']['max']);
495  $cck_plan_fields_simple_path = drupal_get_path('module', 'cck_plan_fields_simple');
496  drupal_add_js($cck_plan_fields_simple_path . '/js/cck_plan_fields.js');
497  $flag = FALSE;
498  foreach ($element['#columns'] as $ftype) {
499    if (!empty($element[$ftype]['#value'])) {
500      $error_field = implode('][', $element['#parents']) .'][' . $ftype;
501      if (!is_numeric($element[$ftype]['#value'])) {
502        form_set_error($error_field, t('Amount should be a number in %field.', array('%field' => t($field['widget']['label']))));
503      }
504      elseif ($field['cck_plan_fields_simple_type'] == 'int') {
505        $start = $element[$ftype]['#value'];
506        $value = preg_replace('@[^-0-9]@', '', $start);
507        if ($start != $value) {
508          form_set_error($error_field, t('Only numbers are allowed in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label']))));
509        }
510      }
511      elseif ($min && $field['widget']['min'] > $element[$ftype]['#value']) {
512        form_set_error($error_field, t('Amount should be greater %number in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label']))));
513      }
514      elseif ($max && $field['widget']['max'] < $element[$ftype]['#value']) {
515        form_set_error($error_field, t('Amount should be litter %number in %field.', array('%number' => $field['widget']['max'], '%field' => t($field['widget']['label']))));
516      }
517      $flag = TRUE;
518    }
519  }
520  if (!$flag) {
521    return;
522  }
523  $type_name = $element['#type_name'];
524  $field = content_fields($field_name, $type_name);
525  $field_key = $element['#columns'][0];
526}
527
528
529/**
530 * Implementation of hook_feeds_node_processor_targets_alter().
531 *
532 * @see FeedsNodeProcessor::getMappingTargets()
533 */
534function cck_plan_fields_simple_feeds_node_processor_targets_alter(&$targets, $content_type) {
535  $info = content_types($content_type);
536  $fields = array();
537  if (isset($info['fields']) && count($info['fields'])) {
538    foreach ($info['fields'] as $field_name => $field) {
539      if (in_array($field['type'], array('cck_plan_fields_simple_field'))) {
540        $fields[$field_name] = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name;
541      }
542    }
543  }
544  $ftypes = array(
545    'value' => t('January'),
546    'value_1' => t('February'),
547    'value_2' => t('March'),
548    'value_3' => t('April'),
549    'value_4' => t('May'),
550    'value_5' => t('June'),
551    'value_6' => t('July'),
552    'value_7' => t('August'),
553    'value_8' => t('September'),
554    'value_9' => t('Octuber'),
555    'value_10' => t('November'),
556    'value_11' => t('December'),
557  );
558
559  foreach ($fields as $k => $name) {
560    foreach($ftypes as $id => $month) {
561      $targets[$k . ':' . $id] = array(
562        'name' => check_plain($name) . '(' . $month . ')',
563        'callback' => 'cck_plan_fields_simple_feeds_set_target',
564        'description' => t('The part @month_name of @name field of the node.', array('@name' => $name, '@month_name' => $month)),
565      );
566    }
567  }
568}
569
570function cck_plan_fields_simple_feeds_set_target($node, $target, $value) {
571  list($field_name, $sub_field) = explode(':', $target);
572  $ftypes = array(
573    'value' => t('January'),
574    'value_1' => t('February'),
575    'value_2' => t('March'),
576    'value_3' => t('April'),
577    'value_4' => t('May'),
578    'value_5' => t('June'),
579    'value_6' => t('July'),
580    'value_7' => t('August'),
581    'value_8' => t('September'),
582    'value_9' => t('Octuber'),
583    'value_10' => t('November'),
584    'value_11' => t('December'),
585  );
586  $field_default = array();
587  foreach($ftypes as $id => $month) {
588    $field_default[$id] = 0;
589  }
590  $field = isset($node->$field_name) ? $node->$field_name : $field_default;
591  // Handle multiple value fields.
592  if (is_array($value)) {
593    $i = 0;
594    foreach ($value as $v) {
595      if (!is_array($v) && !is_object($v)) {
596        $field[$i][$sub_field] = $v;
597      }
598      $i++;
599    }
600  }
601  else {
602    $field[0][$sub_field] = $value;
603  }
604  $node->$field_name = $field;
605}
Nota: Vea TracBrowser para ayuda de uso del navegador del repositorio.