### Eclipse Workspace Patch 1.0 #P feeds Index: mappers/location.inc =================================================================== RCS file: mappers/location.inc diff -N mappers/location.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ mappers/location.inc 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,129 @@ + 0) { + $location_fields= _location_feeds_fields(); + + // We need the collect settings + $fields = $settings['form']['fields']; + // locpick is a compound field. So split it + $fields['locpick][user_latitude'] = $fields['locpick']; + $fields['locpick][user_longitude'] = $fields['locpick']; + unset($fields['locpick']); + + foreach ($fields as $field => $values) { + if (! $values['collect']) { + unset($location_fields[$field]); + } + } + + _location_feeds_fill_targets(&$targets, 'location', 'locations', $location_fields); + } + + // location_cck.module logic + $info = content_types($content_type); + if (isset($info['fields']) && count($info['fields'])) { + foreach ($info['fields'] as $field_name => $field) { + $location_fields= _location_feeds_fields(); + if (in_array($field['type'], array('location'))) { + $name = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name; + _location_feeds_fill_targets(&$targets, 'location_cck', $name, $location_fields); + } + } + } +} + +/** + * Helper function to add target fields + * + * @param $targets + * @param $module + * Module name providing the field. + * @param $field_name + * Field name where values are supposed to get stored to. + * @param $sub_fields + * Location elements for the given $field_name + */ +function _location_feeds_fill_targets(&$targets, $module, $field_name, $sub_fields) { + foreach ($sub_fields as $sub_field => $value) { + $targets[ $field_name . ':' . $sub_field] = array( + 'name' => $module . " module: " . $field_name . '.' . t('!label', array('!label' => $value)), + 'callback' => 'location_feeds_set_target', + 'description' => t('The !label for the location of the node.', array('!label' => $sub_field)), + ); + } +} + +/** + * Helper function to get to manage target fields + * + * @return array of key/value field name/field label + */ +function _location_feeds_fields() { + static $fields; + if (isset($fields)) { + return $fields; + } + + $fields= location_field_names(TRUE); + unset($fields['locpick']); + $fields['locpick][user_latitude'] = t("Latitude"); + $fields['locpick][user_longitude'] = t("Longitude"); + + // province_name / country_name / map_link are display fields + unset($fields['province_name']); + unset($fields['country_name']); + unset($fields['map_link']); + + return $fields; +} + +/** + * Implementation of feed_set_target + * + * @param object $node + * @param string $target + * When targeting sub arrays the '][' is used to drill down. + * Note that this implementation is lazy ... we assume just depth=2 + * @param $value + * @return object + */ +function location_feeds_set_target($node, $target, $value) { + list($field_name, $sub_field) = split(':', $target); + if (strpos($sub_field, '][') > 0) { + // TODO : make this algorithm recursive. + list($sub_field, $last_field) = split('\]\[', $sub_field, 2); + } + + if (!is_array($value)) { + $value = array($value); + } + + foreach ($value as $i => $val) { + if (isset($last_field)) { + $node->{$field_name}[$i][$sub_field][$last_field] = $val; + } + else { + $node->{$field_name}[$i][$sub_field] = $val; + } + } + return $node; +}