1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Implementation of hook_field_info(). |
---|
5 | */ |
---|
6 | function cck_plan_fields_field_info() { |
---|
7 | return array( |
---|
8 | 'cck_plan_fields_field' => array( |
---|
9 | 'label' => t('Plan line field'), |
---|
10 | 'description' => t('Store items operative plan data in the database.'), |
---|
11 | ), |
---|
12 | ); |
---|
13 | } |
---|
14 | |
---|
15 | /** |
---|
16 | * Implementation of hook_field_settings(). |
---|
17 | */ |
---|
18 | function cck_plan_fields_field_settings($op, $field) { |
---|
19 | switch ($op) { |
---|
20 | case 'form': |
---|
21 | $form = array(); |
---|
22 | $vocabularies = taxonomy_get_vocabularies(); |
---|
23 | $vocabulary_options = array(); |
---|
24 | foreach($vocabularies as $vocabulary) { |
---|
25 | $vocabulary_options[$vocabulary->vid] = $vocabulary->name; |
---|
26 | } |
---|
27 | $traslate_labels = array( |
---|
28 | 'vid' => t('Taxonomy'), |
---|
29 | ); |
---|
30 | $form['vid'] = array( |
---|
31 | '#type' => 'select', |
---|
32 | '#title' => t('Taxonomy'), |
---|
33 | '#default_value' => !empty($field['vid']) ? $field['vid'] : '', |
---|
34 | '#options' => $vocabulary_options, |
---|
35 | ); |
---|
36 | if (!empty($field['vid'])) { |
---|
37 | $vid = $field['vid']; |
---|
38 | //$terms = taxonomy_get_tree($vid); |
---|
39 | $tree = taxonomy_get_tree($vid); |
---|
40 | $options = array(); |
---|
41 | if ($tree) { |
---|
42 | foreach ($tree as $term) { |
---|
43 | $choice = new stdClass(); |
---|
44 | $choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name); |
---|
45 | $options[] = $choice; |
---|
46 | } |
---|
47 | } |
---|
48 | $form['vtid'] = array( |
---|
49 | '#type' => 'select', |
---|
50 | '#default_value' => isset($field['vtid']) ? $field['vtid'] : array(), |
---|
51 | '#options' => $options, |
---|
52 | '#multiple' => TRUE, |
---|
53 | ); |
---|
54 | } |
---|
55 | $types = array( |
---|
56 | 'int' => t('Integer'), |
---|
57 | 'float' => t('Float'), |
---|
58 | ); |
---|
59 | $form['cck_plan_fields_simple_type'] = array( |
---|
60 | '#type' => 'select', |
---|
61 | '#title' => t('Type'), |
---|
62 | '#default_value' => !empty($field['cck_plan_fields_simple_type']) ? $field['cck_plan_fields_simple_type'] : 'int', |
---|
63 | '#options' => $types, |
---|
64 | ); |
---|
65 | return $form; |
---|
66 | case 'save': |
---|
67 | $save_settings = array( |
---|
68 | 'vid', |
---|
69 | 'vtid', |
---|
70 | 'cck_plan_fields_simple_type', |
---|
71 | ); |
---|
72 | return $save_settings; |
---|
73 | case 'database columns': |
---|
74 | $columns['tid'] = array( |
---|
75 | 'type' => 'int', |
---|
76 | 'not null' => FALSE, |
---|
77 | 'sortable' => TRUE, |
---|
78 | 'views' => TRUE, |
---|
79 | ); |
---|
80 | for ($i = 0; $i < 12; $i++) { |
---|
81 | $field_id = $i ? 'value_' . $i : 'value'; |
---|
82 | $columns[$field_id] = array( |
---|
83 | 'type' => 'numeric', |
---|
84 | 'size' => 'normal', |
---|
85 | 'not null' => TRUE, |
---|
86 | 'default' => 0, |
---|
87 | 'precision' => 32, |
---|
88 | 'scale' => 0, |
---|
89 | ); |
---|
90 | } |
---|
91 | return $columns; |
---|
92 | case 'views data': |
---|
93 | $data = content_views_field_views_data($field); |
---|
94 | $db_info = content_database_info($field); |
---|
95 | $table_alias = content_views_tablename($field); |
---|
96 | $ftypes = array( |
---|
97 | 'tid' => t('Plan Account'), |
---|
98 | 'value' => t('January'), |
---|
99 | 'value_1' => t('February'), |
---|
100 | 'value_2' => t('March'), |
---|
101 | 'value_3' => t('April'), |
---|
102 | 'value_4' => t('May'), |
---|
103 | 'value_5' => t('June'), |
---|
104 | 'value_6' => t('July'), |
---|
105 | 'value_7' => t('August'), |
---|
106 | 'value_8' => t('September'), |
---|
107 | 'value_9' => t('Octuber'), |
---|
108 | 'value_10' => t('November'), |
---|
109 | 'value_11' => t('December'), |
---|
110 | ); |
---|
111 | foreach ($ftypes as $ftype => $label) { |
---|
112 | $copy = $data[$table_alias][$field['field_name'] . $ftype]; |
---|
113 | $copy['title'] = t($label); |
---|
114 | $copy['filter']['handler'] = 'content_handler_filter_many_to_one'; |
---|
115 | $copy['filter']['numeric'] = TRUE; |
---|
116 | unset($copy['field'], $copy['argument'], $copy['sort']); |
---|
117 | $data[$table_alias][$field['field_name'] . $ftype . '_many_to_one'] = $copy; |
---|
118 | $data[$table_alias][$field['field_name'] . $ftype]['argument']['handler'] = 'content_handler_argument_many_to_one'; |
---|
119 | if ($ftype != 'description') { |
---|
120 | $data[$table_alias][$field['field_name'] . $ftype]['argument']['numeric'] = TRUE; |
---|
121 | } |
---|
122 | } |
---|
123 | return $data; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Implementation of hook_content_is_empty(). |
---|
129 | */ |
---|
130 | function cck_plan_fields_content_is_empty($item, $field) { |
---|
131 | $flat = TRUE; |
---|
132 | foreach (array_keys($field['columns']) as $ftype) { |
---|
133 | if ($ftype != 'tid' && !empty($item[$ftype])) { |
---|
134 | $flat = FALSE; |
---|
135 | } |
---|
136 | } |
---|
137 | return $flat; |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * Implementation of hook_field_formatter_info(). |
---|
142 | */ |
---|
143 | function cck_plan_fields_field_formatter_info() { |
---|
144 | $formatters = array( |
---|
145 | 'default' => array( |
---|
146 | 'label' => t('Default'), |
---|
147 | 'multiple values' => CONTENT_HANDLE_CORE, |
---|
148 | 'field types' => array('cck_plan_fields_field'), |
---|
149 | ), |
---|
150 | 'single_line' => array( |
---|
151 | 'label' => t('Single Line'), |
---|
152 | 'multiple values' => CONTENT_HANDLE_CORE, |
---|
153 | 'field types' => array('cck_plan_fields_field'), |
---|
154 | ), |
---|
155 | ); |
---|
156 | $ftypes = array( |
---|
157 | 'tid' => t('Partidas'), |
---|
158 | 'value' => t('January'), |
---|
159 | 'value_1' => t('February'), |
---|
160 | 'value_2' => t('March'), |
---|
161 | 'value_3' => t('April'), |
---|
162 | 'value_4' => t('May'), |
---|
163 | 'value_5' => t('June'), |
---|
164 | 'value_6' => t('July'), |
---|
165 | 'value_7' => t('August'), |
---|
166 | 'value_8' => t('September'), |
---|
167 | 'value_9' => t('Octuber'), |
---|
168 | 'value_10' => t('November'), |
---|
169 | 'value_11' => t('December'), |
---|
170 | ); |
---|
171 | foreach ($ftypes as $value => $label) { |
---|
172 | $formatters['single_line_' . $value] = array( |
---|
173 | 'label' => t('Single Line ') . $label, |
---|
174 | 'multiple values' => CONTENT_HANDLE_CORE, |
---|
175 | 'field types' => array('cck_plan_fields_field'), |
---|
176 | ); |
---|
177 | } |
---|
178 | return $formatters; |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * Implementation of hook_theme(). |
---|
183 | */ |
---|
184 | function cck_plan_fields_theme() { |
---|
185 | return array( |
---|
186 | // Shows address in the default view: Multilines |
---|
187 | 'cck_plan_fields_formatter_default' => array( |
---|
188 | 'arguments' => array('element'), |
---|
189 | ), |
---|
190 | // Shows address in only one line |
---|
191 | 'cck_plan_fields_formatter_single_line' => array( |
---|
192 | 'arguments' => array('element'), |
---|
193 | ), |
---|
194 | 'cck_plan_fields_formatter_single_line_tid' => array( |
---|
195 | 'arguments' => array('form' => NULL), |
---|
196 | 'function' => 'theme_cck_plan_fields_formatter_generic', |
---|
197 | ), |
---|
198 | 'cck_plan_fields_formatter_single_line_value' => array( |
---|
199 | 'arguments' => array('form' => NULL), |
---|
200 | 'function' => 'theme_cck_plan_fields_formatter_generic', |
---|
201 | ), |
---|
202 | 'cck_plan_fields_formatter_single_line_value1' => array( |
---|
203 | 'arguments' => array('form' => NULL), |
---|
204 | 'function' => 'theme_cck_plan_fields_formatter_generic', |
---|
205 | ), |
---|
206 | 'cck_plan_fields_formatter_single_line_value2' => array( |
---|
207 | 'arguments' => array('form' => NULL), |
---|
208 | 'function' => 'theme_cck_plan_fields_formatter_generic', |
---|
209 | ), |
---|
210 | 'cck_plan_fields_formatter_single_line_value3' => array( |
---|
211 | 'arguments' => array('form' => NULL), |
---|
212 | 'function' => 'theme_cck_plan_fields_formatter_generic' |
---|
213 | ), |
---|
214 | 'cck_plan_fields_formatter_single_line_value4' => array( |
---|
215 | 'arguments' => array('form' => NULL), |
---|
216 | 'function' => 'theme_cck_plan_fields_formatter_generic', |
---|
217 | ), |
---|
218 | 'cck_plan_fields_formatter_single_line_value5' => array( |
---|
219 | 'arguments' => array('form' => NULL), |
---|
220 | 'function' => 'theme_cck_plan_fields_formatter_generic', |
---|
221 | ), |
---|
222 | 'cck_plan_fields_formatter_single_line_value6' => array( |
---|
223 | 'arguments' => array('form' => NULL), |
---|
224 | 'function' => 'theme_cck_plan_fields_formatter_generic', |
---|
225 | ), |
---|
226 | 'cck_plan_fields_formatter_single_line_value7' => array( |
---|
227 | 'arguments' => array('form' => NULL), |
---|
228 | 'function' => 'theme_cck_plan_fields_formatter_generic', |
---|
229 | ), |
---|
230 | 'cck_plan_fields_formatter_single_line_value8' => array( |
---|
231 | 'arguments' => array('form' => NULL), |
---|
232 | 'function' => 'theme_cck_plan_fields_formatter_generic', |
---|
233 | ), |
---|
234 | 'cck_plan_fields_formatter_single_line_value9' => array( |
---|
235 | 'arguments' => array('form' => NULL), |
---|
236 | 'function' => 'theme_cck_plan_fields_formatter_generic', |
---|
237 | ), |
---|
238 | 'cck_plan_fields_formatter_single_line_value10' => array( |
---|
239 | 'arguments' => array('form' => NULL), |
---|
240 | 'function' => 'theme_cck_plan_fields_formatter_generic', |
---|
241 | ), |
---|
242 | 'cck_plan_fields_formatter_single_line_value11' => array( |
---|
243 | 'arguments' => array('form' => NULL), |
---|
244 | 'function' => 'theme_cck_plan_fields_formatter_generic', |
---|
245 | ), |
---|
246 | 'cck_plan_fields_table' => array( |
---|
247 | 'arguments' => array('form' => NULL), |
---|
248 | ), |
---|
249 | ); |
---|
250 | } |
---|
251 | |
---|
252 | /** |
---|
253 | * Proxy theme function for cck_plan_fields formatters. |
---|
254 | */ |
---|
255 | function theme_cck_plan_fields_formatter_generic($element) { |
---|
256 | $output = ''; |
---|
257 | $flag = explode('single_line_', $element['#formatter']); |
---|
258 | $ftypes = array( |
---|
259 | 'tid' => t('Partidas'), |
---|
260 | 'value' => t('January'), |
---|
261 | 'value_1' => t('February'), |
---|
262 | 'value_2' => t('March'), |
---|
263 | 'value_3' => t('April'), |
---|
264 | 'value_4' => t('May'), |
---|
265 | 'value_5' => t('June'), |
---|
266 | 'value_6' => t('July'), |
---|
267 | 'value_7' => t('August'), |
---|
268 | 'value_8' => t('September'), |
---|
269 | 'value_9' => t('Octuber'), |
---|
270 | 'value_10' => t('November'), |
---|
271 | 'value_11' => t('December'), |
---|
272 | ); |
---|
273 | if ($flag[1] == 'tid') { |
---|
274 | $term = taxonomy_get_term($element['#item'][$flag[1]]); |
---|
275 | // If this term's vocabulary supports localization. |
---|
276 | if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) { |
---|
277 | $term->name = tt("taxonomy:term:$term->tid:name", $term->name); |
---|
278 | } |
---|
279 | $output = '<strong>'. $ftypes[$flag[1]] .': </strong>'. check_plain($term->name); |
---|
280 | } |
---|
281 | else { |
---|
282 | $output = '<strong>'. $ftypes[$flag[1]] .': </strong>'. number_format($element['#item'][$flag[1]], 2, '.', ','); |
---|
283 | } |
---|
284 | return $output; |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | * theme_cck_plan_fields_formatter_default(). |
---|
289 | * default formatter theme |
---|
290 | */ |
---|
291 | function theme_cck_plan_fields_formatter_default($element) { |
---|
292 | $output = ''; |
---|
293 | // If all fields are hidden, return '' |
---|
294 | if (empty($element['#item']['tid'])) { |
---|
295 | return $output; |
---|
296 | } |
---|
297 | $ftypes = array( |
---|
298 | 'tid' => t('Partidas'), |
---|
299 | 'value' => t('January'), |
---|
300 | 'value_1' => t('February'), |
---|
301 | 'value_2' => t('March'), |
---|
302 | 'value_3' => t('April'), |
---|
303 | 'value_4' => t('May'), |
---|
304 | 'value_5' => t('June'), |
---|
305 | 'value_6' => t('July'), |
---|
306 | 'value_7' => t('August'), |
---|
307 | 'value_8' => t('September'), |
---|
308 | 'value_9' => t('Octuber'), |
---|
309 | 'value_10' => t('November'), |
---|
310 | 'value_11' => t('December'), |
---|
311 | 'total' => t('Total'), |
---|
312 | ); |
---|
313 | $headers = array(); |
---|
314 | $rows = array(); |
---|
315 | $row = array(); |
---|
316 | $total = 0; |
---|
317 | foreach ($ftypes as $value => $label) { |
---|
318 | $headers[] = array('data' => $label); |
---|
319 | if ($value != 'tid') { |
---|
320 | if ($value != 'total') { |
---|
321 | $total += $element['#item'][$value]; |
---|
322 | $row[] = array('data' => number_format($element['#item'][$value], 2, '.', ',')); |
---|
323 | } |
---|
324 | else { |
---|
325 | $row[] = array('data' => $total); |
---|
326 | } |
---|
327 | } |
---|
328 | else { |
---|
329 | $term = taxonomy_get_term($element['#item'][$value]); |
---|
330 | // If this term's vocabulary supports localization. |
---|
331 | if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) { |
---|
332 | $term->name = tt("taxonomy:term:$term->tid:name", $term->name); |
---|
333 | } |
---|
334 | $row[] = array('data' => $term->name); |
---|
335 | } |
---|
336 | } |
---|
337 | $rows[] = $row; |
---|
338 | return theme('table', $headers, $rows); |
---|
339 | } |
---|
340 | |
---|
341 | /** |
---|
342 | * theme_cck_plan_fields_formatter_single_line(). |
---|
343 | * display line items products in a single line |
---|
344 | */ |
---|
345 | function theme_cck_plan_fields_formatter_single_line($element) { |
---|
346 | $output = ''; |
---|
347 | // If all fields are hidden, return '' |
---|
348 | if (empty($element)) { |
---|
349 | return $output; |
---|
350 | } |
---|
351 | $ftypes = array( |
---|
352 | 'tid' => t('Partidas'), |
---|
353 | 'value' => t('January'), |
---|
354 | 'value_1' => t('February'), |
---|
355 | 'value_2' => t('March'), |
---|
356 | 'value_3' => t('April'), |
---|
357 | 'value_4' => t('May'), |
---|
358 | 'value_5' => t('June'), |
---|
359 | 'value_6' => t('July'), |
---|
360 | 'value_7' => t('August'), |
---|
361 | 'value_8' => t('September'), |
---|
362 | 'value_9' => t('Octuber'), |
---|
363 | 'value_10' => t('November'), |
---|
364 | 'value_11' => t('December'), |
---|
365 | ); |
---|
366 | foreach ($ftypes as $value => $label) { |
---|
367 | if ($value != 'tid') { |
---|
368 | $output .= ' <strong>'. $label .': </strong>'. number_format($element['#item'][$value], 2, '.', ','); |
---|
369 | } |
---|
370 | else { |
---|
371 | $term = taxonomy_get_term($element['#item'][$value]); |
---|
372 | // If this term's vocabulary supports localization. |
---|
373 | if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) { |
---|
374 | $term->name = tt("taxonomy:term:$term->tid:name", $term->name); |
---|
375 | } |
---|
376 | $output .= '<strong>'. $label .': </strong>'. check_plain($term->name); |
---|
377 | } |
---|
378 | } |
---|
379 | return '<div class="items-plan-field">'. $output .'</div>'; |
---|
380 | } |
---|
381 | |
---|
382 | /** |
---|
383 | * Implementation of hook_widget_info(). |
---|
384 | */ |
---|
385 | function cck_plan_fields_widget_info() { |
---|
386 | return array( |
---|
387 | 'cck_plan_fields_elements' => array( |
---|
388 | 'label' => t('Items Plan Field'), |
---|
389 | 'field types' => array('cck_plan_fields_field'), |
---|
390 | 'multiple values' => CONTENT_HANDLE_CORE, |
---|
391 | 'callbacks' => array('default value' => CONTENT_CALLBACK_DEFAULT), |
---|
392 | ), |
---|
393 | ); |
---|
394 | } |
---|
395 | |
---|
396 | /** |
---|
397 | * Implementation of hook_widget_settings(). |
---|
398 | */ |
---|
399 | function cck_plan_fields_widget_settings($op, $widget) { |
---|
400 | switch ($op) { |
---|
401 | case 'form': |
---|
402 | $form = array(); |
---|
403 | $form['min'] = array( |
---|
404 | '#type' => 'textfield', |
---|
405 | '#title' => t('Minimum'), |
---|
406 | '#element_validate' => array('_element_validate_number'), |
---|
407 | '#default_value' => is_numeric($widget['min']) ? $widget['min'] : '', |
---|
408 | ); |
---|
409 | $form['max'] = array( |
---|
410 | '#type' => 'textfield', |
---|
411 | '#title' => t('Maximum'), |
---|
412 | '#element_validate' => array('_element_validate_number'), |
---|
413 | '#default_value' => is_numeric($widget['max']) ? $widget['max'] : '', |
---|
414 | ); |
---|
415 | return $form; |
---|
416 | break; |
---|
417 | case 'save': |
---|
418 | return array( |
---|
419 | 'min', |
---|
420 | 'max', |
---|
421 | ); |
---|
422 | break; |
---|
423 | } |
---|
424 | } |
---|
425 | |
---|
426 | /** |
---|
427 | * Implementation of theme_cck_plan_fields_table(). |
---|
428 | */ |
---|
429 | function theme_cck_plan_fields_table($form = array()) { |
---|
430 | $headers = array(); |
---|
431 | $rows = array(); |
---|
432 | $row = array(); |
---|
433 | $ftypes = array( |
---|
434 | 'tid' => t('Account'), |
---|
435 | 'value' => t('Ene'), |
---|
436 | 'value_1' => t('Feb'), |
---|
437 | 'value_2' => t('Mar'), |
---|
438 | 'value_3' => t('Abr'), |
---|
439 | 'value_4' => t('May'), |
---|
440 | 'value_5' => t('Jun'), |
---|
441 | 'value_6' => t('Jul'), |
---|
442 | 'value_7' => t('Aug'), |
---|
443 | 'value_8' => t('Sep'), |
---|
444 | 'value_9' => t('Oct'), |
---|
445 | 'value_10' => t('Nov'), |
---|
446 | 'value_11' => t('Dic'), |
---|
447 | 'total' => t('Total'), |
---|
448 | ); |
---|
449 | $backgrounds = array( |
---|
450 | 'tid' => 'blue', |
---|
451 | 'value' => 'black', |
---|
452 | 'value_1' => 'blue', |
---|
453 | 'value_2' => 'white', |
---|
454 | 'value_3' => 'blue', |
---|
455 | 'value_4' => 'white', |
---|
456 | 'value_5' => 'blue', |
---|
457 | 'value_6' => 'white', |
---|
458 | 'value_7' => 'blue', |
---|
459 | 'value_8' => 'white', |
---|
460 | 'value_9' => 'blue', |
---|
461 | 'value_10' => 'white', |
---|
462 | 'value_11' => 'blue', |
---|
463 | ); |
---|
464 | if (isset($form['#title']) && $form['#title']) { |
---|
465 | $field = str_replace('_', '-', $form['#field_name']); |
---|
466 | $output .= '<div id="edit-' . $field . '-0-value-wrapper" class="form-item"><label for="edit-' . $field . '-0-value">' . $form['#title'] . '</label>'; |
---|
467 | } |
---|
468 | $output = '<div style="width:2150px">'; |
---|
469 | foreach ($ftypes as $ftype => $label) { |
---|
470 | if($ftype != 'tid') { |
---|
471 | $output .= '<div style="width:150px;float:left;margin-left:5px">' . drupal_render($form[$ftype]) . '</div>'; |
---|
472 | } |
---|
473 | else { |
---|
474 | $output .= '<div style="width:130px;float:left;margin-left:5px">' . drupal_render($form[$ftype]) . '</div>'; |
---|
475 | } |
---|
476 | } |
---|
477 | $output .= drupal_render($form); |
---|
478 | $output .= '</div>'; |
---|
479 | if (isset($form['#title']) && $form['#title']) { |
---|
480 | $output .= '</div>'; |
---|
481 | } |
---|
482 | return $output; |
---|
483 | } |
---|
484 | |
---|
485 | /** |
---|
486 | * Implementation of hook_widget(). |
---|
487 | */ |
---|
488 | function cck_plan_fields_widget(&$form, &$form_state, $field, $items, $delta = 0) { |
---|
489 | $cck_plan_fields_path = drupal_get_path('module', 'cck_plan_fields'); |
---|
490 | drupal_add_js($cck_plan_fields_path . '/js/cck_plan_fields.js'); |
---|
491 | $field_name = $field['field_name']; |
---|
492 | $element = array(); |
---|
493 | $ftypes = array( |
---|
494 | 'tid' => t('Partida'), |
---|
495 | 'value' => t('Ene'), |
---|
496 | 'value_1' => t('Feb'), |
---|
497 | 'value_2' => t('Mar'), |
---|
498 | 'value_3' => t('Abr'), |
---|
499 | 'value_4' => t('May'), |
---|
500 | 'value_5' => t('Jun'), |
---|
501 | 'value_6' => t('Jul'), |
---|
502 | 'value_7' => t('Aug'), |
---|
503 | 'value_8' => t('Sep'), |
---|
504 | 'value_9' => t('Oct'), |
---|
505 | 'value_10' => t('Nov'), |
---|
506 | 'value_11' => t('Dic'), |
---|
507 | 'total' => t('Total'), |
---|
508 | ); |
---|
509 | $class = $field_name . '_' . $delta . '_field'; |
---|
510 | $vid = $field['vid']; |
---|
511 | //$terms = taxonomy_get_tree($vid); |
---|
512 | $tree = taxonomy_get_tree($vid); |
---|
513 | $options = array(); |
---|
514 | if ($tree) { |
---|
515 | foreach ($tree as $term) { |
---|
516 | if (isset($field['vtid'][$term->tid])) { |
---|
517 | $choice = new stdClass(); |
---|
518 | $choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name); |
---|
519 | $options[] = $choice; |
---|
520 | } |
---|
521 | } |
---|
522 | } |
---|
523 | $element['tid'] = array( |
---|
524 | '#type' => 'select', |
---|
525 | '#default_value' => isset($items[$delta]['tid']) ? $items[$delta]['tid'] : NULL, |
---|
526 | '#options' => $options, |
---|
527 | ); |
---|
528 | if ($delta == 0) { |
---|
529 | $element['tid']['#title'] = $ftypes['tid']; |
---|
530 | } |
---|
531 | $total = 0; |
---|
532 | for ($i = 0; $i < 12; $i++) { |
---|
533 | $class1 = ' ' . $field_name . '_m' . $i . '_field' . ' ' . $field_name . '_dato_field'; |
---|
534 | $field_id = $i ? 'value_' . $i : 'value'; |
---|
535 | $field_id_form = $i ? 'value-' . $i : 'value'; |
---|
536 | $element[$field_id] = array( |
---|
537 | '#type' => 'textfield', |
---|
538 | '#default_value' => isset($items[$delta][$field_id]) && $items[$delta][$field_id] ? $items[$delta][$field_id] : 0, |
---|
539 | '#size' => 15, |
---|
540 | '#attributes' => array('class' => $class . $class1 . ' number', 'onchange' => "suma('$class');sumatexto('" . $field_name . '_dato_field' . "');sumatexto('" . $field_name . '_m' . $i . '_field' . "');"), |
---|
541 | ); |
---|
542 | $total += $element[$field_id]['#default_value']; |
---|
543 | if ($delta == 0) { |
---|
544 | $element[$field_id]['#title'] = $ftypes[$field_id]; |
---|
545 | } |
---|
546 | } |
---|
547 | $element['total'] = array( |
---|
548 | '#type' => 'textfield', |
---|
549 | '#default_value' => number_format($total, 0, '.', ''), |
---|
550 | '#size' => 15, |
---|
551 | '#attributes' => array('class' => $class . '_total totales'), |
---|
552 | ); |
---|
553 | if ($delta == 0) { |
---|
554 | $element['total']['#title'] = $ftypes['total']; |
---|
555 | } |
---|
556 | $element['#theme'] = 'cck_plan_fields_table'; |
---|
557 | if (empty($element['#element_validate'])) { |
---|
558 | $element['#element_validate'] = array(); |
---|
559 | } |
---|
560 | array_unshift($element['#element_validate'], 'cck_plan_fields_validate'); |
---|
561 | $form_state['#field_info'][$element['#field_name']] = $form['#field_info'][$field_name]; |
---|
562 | // Used so that hook_field('validate') knows where to |
---|
563 | // flag an error in deeply nested forms. |
---|
564 | if (empty($form['#parents'])) { |
---|
565 | $form['#parents'] = array(); |
---|
566 | } |
---|
567 | $element['_error_element'] = array( |
---|
568 | '#type' => 'value', |
---|
569 | '#value' => implode('][', array_merge($form['#parents'], array('value'))), |
---|
570 | ); |
---|
571 | return $element; |
---|
572 | } |
---|
573 | |
---|
574 | /** |
---|
575 | * FAPI validation of an individual element. |
---|
576 | */ |
---|
577 | function cck_plan_fields_validate($element, &$form_state) { |
---|
578 | |
---|
579 | $field_name = $element['#field_name']; |
---|
580 | $type_name = $element['#type_name']; |
---|
581 | $field = content_fields($field_name, $type_name); |
---|
582 | $min = is_numeric($field['widget']['min']); |
---|
583 | $max = is_numeric($field['widget']['max']); |
---|
584 | $cck_plan_fields_path = drupal_get_path('module', 'cck_plan_fields'); |
---|
585 | drupal_add_js($cck_plan_fields_path . '/js/cck_plan_fields.js'); |
---|
586 | $flag = FALSE; |
---|
587 | foreach ($element['#columns'] as $ftype) { |
---|
588 | if ($ftype != 'tid' && !empty($element[$ftype]['#value'])) { |
---|
589 | $error_field = implode('][', $element['#parents']) .'][' . $ftype; |
---|
590 | if (!is_numeric($element[$ftype]['#value'])) { |
---|
591 | form_set_error($error_field, t('Amount should be a number in %field.', array('%field' => t($field['widget']['label'])))); |
---|
592 | } |
---|
593 | elseif ($field['cck_plan_fields_simple_type'] == 'int') { |
---|
594 | $start = $element[$ftype]['#value']; |
---|
595 | $value = preg_replace('@[^-0-9]@', '', $start); |
---|
596 | if ($start != $value) { |
---|
597 | form_set_error($error_field, t('Only numbers are allowed in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label'])))); |
---|
598 | } |
---|
599 | } |
---|
600 | elseif ($min && $field['widget']['min'] > $element[$ftype]['#value']) { |
---|
601 | form_set_error($error_field, t('Amount should be greater %number in %field.', array('%number' => $field['widget']['min'], '%field' => t($field['widget']['label'])))); |
---|
602 | } |
---|
603 | elseif ($max && $field['widget']['max'] < $element[$ftype]['#value']) { |
---|
604 | form_set_error($error_field, t('Amount should be litter %number in %field.', array('%number' => $field['widget']['max'], '%field' => t($field['widget']['label'])))); |
---|
605 | } |
---|
606 | $flag = TRUE; |
---|
607 | } |
---|
608 | } |
---|
609 | if (!$flag) { |
---|
610 | return; |
---|
611 | } |
---|
612 | if ($element['#delta']) { |
---|
613 | $current_taxonomy = $element['tid']['#value']; |
---|
614 | for($i = 0; $i < $element['#delta']; $i++) { |
---|
615 | if ($current_taxonomy == $form_state['values'][$field_name][$i]['tid']) { |
---|
616 | $error_field = implode('][', $element['#parents']) .'][tid'; |
---|
617 | form_set_error($error_field, t('Account can not be repeat in %field.', array('%field' => t($field['widget']['label'])))); |
---|
618 | } |
---|
619 | } |
---|
620 | } |
---|
621 | $type_name = $element['#type_name']; |
---|
622 | $field = content_fields($field_name, $type_name); |
---|
623 | $field_key = $element['#columns'][0]; |
---|
624 | } |
---|
625 | |
---|
626 | |
---|
627 | /** |
---|
628 | * Implementation of hook_feeds_node_processor_targets_alter(). |
---|
629 | * |
---|
630 | * @see FeedsNodeProcessor::getMappingTargets() |
---|
631 | */ |
---|
632 | function cck_plan_fields_feeds_node_processor_targets_alter(&$targets, $content_type) { |
---|
633 | $info = content_types($content_type); |
---|
634 | $fields = array(); |
---|
635 | if (isset($info['fields']) && count($info['fields'])) { |
---|
636 | foreach ($info['fields'] as $field_name => $field) { |
---|
637 | if (in_array($field['type'], array('cck_plan_fields_field'))) { |
---|
638 | $fields[$field_name] = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name; |
---|
639 | } |
---|
640 | } |
---|
641 | } |
---|
642 | $ftypes = array( |
---|
643 | 'tid' => t('Taxonomy'), |
---|
644 | 'value' => t('January'), |
---|
645 | 'value_1' => t('February'), |
---|
646 | 'value_2' => t('March'), |
---|
647 | 'value_3' => t('April'), |
---|
648 | 'value_4' => t('May'), |
---|
649 | 'value_5' => t('June'), |
---|
650 | 'value_6' => t('July'), |
---|
651 | 'value_7' => t('August'), |
---|
652 | 'value_8' => t('September'), |
---|
653 | 'value_9' => t('Octuber'), |
---|
654 | 'value_10' => t('November'), |
---|
655 | 'value_11' => t('December'), |
---|
656 | ); |
---|
657 | |
---|
658 | foreach ($fields as $k => $name) { |
---|
659 | foreach($ftypes as $id => $month) { |
---|
660 | $targets[$k . ':' . $id] = array( |
---|
661 | 'name' => check_plain($name) . '(' . $month . ')', |
---|
662 | 'callback' => 'cck_plan_fields_feeds_set_target', |
---|
663 | 'description' => t('The part @month_name of @name field of the node.', array('@name' => $name, '@month_name' => $month)), |
---|
664 | ); |
---|
665 | } |
---|
666 | } |
---|
667 | } |
---|
668 | |
---|
669 | function cck_plan_fields_feeds_set_target($node, $target, $value) { |
---|
670 | $ftypes = array( |
---|
671 | 'tid' => t('Taxonomy'), |
---|
672 | 'value' => t('January'), |
---|
673 | 'value_1' => t('February'), |
---|
674 | 'value_2' => t('March'), |
---|
675 | 'value_3' => t('April'), |
---|
676 | 'value_4' => t('May'), |
---|
677 | 'value_5' => t('June'), |
---|
678 | 'value_6' => t('July'), |
---|
679 | 'value_7' => t('August'), |
---|
680 | 'value_8' => t('September'), |
---|
681 | 'value_9' => t('Octuber'), |
---|
682 | 'value_10' => t('November'), |
---|
683 | 'value_11' => t('December'), |
---|
684 | ); |
---|
685 | $field_default = array(); |
---|
686 | foreach($ftypes as $id => $month) { |
---|
687 | $field_default[$id] = 0; |
---|
688 | } |
---|
689 | |
---|
690 | list($field_name, $sub_field) = explode(':', $target); |
---|
691 | $field = isset($node->$field_name) ? $node->$field_name : $field_default; |
---|
692 | // Handle multiple value fields. |
---|
693 | $field = content_fields($field_name, $node->type); |
---|
694 | if (is_array($value)) { |
---|
695 | $i = 0; |
---|
696 | foreach ($value as $v) { |
---|
697 | if (!is_array($v) && !is_object($v)) { |
---|
698 | if ($sub_field == 'tid') { |
---|
699 | $terms_found = content_taxonomy_get_term_by_name_vid($v, $field['vid']); |
---|
700 | if ($terms_found->tid) { |
---|
701 | $field[$i][$sub_field] = $terms_found->tid; |
---|
702 | } |
---|
703 | } |
---|
704 | elseif(isset($field[$i]['tid']) && $field[$i]['tid']) { |
---|
705 | $field[$i][$sub_field] = $v; |
---|
706 | } |
---|
707 | } |
---|
708 | $i++; |
---|
709 | } |
---|
710 | } |
---|
711 | else { |
---|
712 | if ($sub_field == 'tid') { |
---|
713 | $terms_found = content_taxonomy_get_term_by_name_vid($value, $field['vid']); |
---|
714 | if ($terms_found->tid) { |
---|
715 | $field[$i][$sub_field] = $terms_found->tid; |
---|
716 | } |
---|
717 | } |
---|
718 | elseif(isset($field[0]['tid']) && $field[0]['tid']) { |
---|
719 | $field[$i][$sub_field] = $value; |
---|
720 | } |
---|
721 | $field[0][$sub_field] = $value; |
---|
722 | } |
---|
723 | $node->$field_name = $field; |
---|
724 | } |
---|
725 | |
---|
726 | function cck_plan_fields_get_term_by_name_vid($name, $vid) { |
---|
727 | $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); |
---|
728 | $result = array(); |
---|
729 | $term = db_fetch_object($db_result); |
---|
730 | if($term) { |
---|
731 | return $term->tid; |
---|
732 | } |
---|
733 | return FALSE; |
---|
734 | } |
---|