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