first import

This commit is contained in:
Bachir Soussi Chiadmi
2015-04-08 11:40:19 +02:00
commit 1bc61b12ad
8435 changed files with 1582817 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
<?php
/**
* @file
* On behalf implementation of Feeds mapping API for date
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsNodeProcessor::getMappingTargets().
*
* @todo Only provides "end date" target if field allows it.
*/
function date_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
$info = field_info_field($name);
if (in_array($info['type'], array('date', 'datestamp', 'datetime'))) {
$targets[$name . ':start'] = array(
'name' => t('@name: Start', array('@name' => $instance['label'])),
'callback' => 'date_feeds_set_target',
'description' => t('The start date for the @name field. Also use if mapping both start and end.', array('@name' => $instance['label'])),
'real_target' => $name,
);
$targets[$name . ':end'] = array(
'name' => t('@name: End', array('@name' => $instance['label'])),
'callback' => 'date_feeds_set_target',
'description' => t('The end date for the @name field.', array('@name' => $instance['label'])),
'real_target' => $name,
);
}
}
}
/**
* Implements hook_feeds_set_target().
*
* @param $node
* The target node.
* @param $field_name
* The name of field on the target node to map to.
* @param $feed_element
* The value to be mapped. Should be either a (flexible) date string
* or a FeedsDateTimeElement object.
*
* @todo Support array of values for dates.
*/
function date_feeds_set_target($source, $entity, $target, $feed_element) {
list($field_name, $sub_field) = explode(':', $target, 2);
if (!($feed_element instanceof FeedsDateTimeElement)) {
if (is_array($feed_element)) {
$feed_element = $feed_element[0];
}
if ($sub_field == 'end') {
$feed_element = new FeedsDateTimeElement(NULL, $feed_element);
}
else {
$feed_element = new FeedsDateTimeElement($feed_element, NULL);
}
}
$feed_element->buildDateField($entity, $field_name);
}

View File

@@ -0,0 +1,94 @@
<?php
/**
* @file
* On behalf implementation of Feeds mapping API for file.module and
* image.module.
*
* Does actually not include mappers for field types defined in fields module
* (because there aren't any) but mappers for all fields that contain their
* value simply in $entity->fieldname['und'][$i]['value'].
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsNodeProcessor::getMappingTargets().
*/
function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
$info = field_info_field($name);
if (in_array($info['type'], array('file', 'image'))) {
$targets[$name] = array(
'name' => check_plain($instance['label']),
'callback' => 'file_feeds_set_target',
'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
);
}
}
}
/**
* Callback for mapping. Here is where the actual mapping happens.
*
* When the callback is invoked, $target contains the name of the field the
* user has decided to map to and $value contains the value of the feed item
* element the user has picked as a source.
*/
function file_feeds_set_target($source, $entity, $target, $value) {
if (empty($value)) {
return;
}
module_load_include('inc', 'file');
// Make sure $value is an array of objects of type FeedsEnclosure.
if (!is_array($value)) {
$value = array($value);
}
foreach ($value as $k => $v) {
if (!($v instanceof FeedsEnclosure)) {
if (is_string($v)) {
$value[$k] = new FeedsEnclosure($v, file_get_mimetype($v));
}
else {
unset($value[$k]);
}
}
}
if (empty($value)) {
return;
}
// Determine file destination.
// @todo This needs review and debugging.
list($entity_id, $vid, $bundle_name) = entity_extract_ids($entity->feeds_item->entity_type, $entity);
$instance_info = field_info_instance($entity->feeds_item->entity_type, $target, $bundle_name);
$info = field_info_field($target);
$data = array();
if (!empty($entity->uid)) {
$data[$entity->feeds_item->entity_type] = $entity;
}
$destination = file_field_widget_uri($info, $instance_info, $data);
// Populate entity.
$i = 0;
$field = isset($entity->$target) ? $entity->$target : array();
foreach ($value as $v) {
try {
$file = $v->getFile($destination);
}
catch (Exception $e) {
watchdog_exception('Feeds', $e, nl2br(check_plain($e)));
}
if ($file) {
$field['und'][$i] = (array)$file;
$field['und'][$i]['display'] = 1; // @todo: Figure out how to properly populate this field.
if ($info['cardinality'] == 1) {
break;
}
$i++;
}
}
$entity->{$target} = $field;
}

View File

@@ -0,0 +1,76 @@
<?php
/**
* @file
* On behalf implementation of Feeds mapping API for link.module.
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsProcessor::getMappingTargets()
*/
function link_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
$info = field_info_field($name);
if ($info['type'] == 'link_field') {
if (array_key_exists('url', $info['columns'])) {
$targets[$name . ':url'] = array(
'name' => t('@name: URL', array('@name' => $instance['label'])),
'callback' => 'link_feeds_set_target',
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
'real_target' => $name,
);
}
if (array_key_exists('title', $info['columns'])) {
$targets[$name . ':title'] = array(
'name' => t('@name: Title', array('@name' => $instance['label'])),
'callback' => 'link_feeds_set_target',
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
'real_target' => $name,
);
}
}
}
}
/**
* Callback for mapping. Here is where the actual mapping happens.
*
* When the callback is invoked, $target contains the name of the field the
* user has decided to map to and $value contains the value of the feed item
* element the user has picked as a source.
*/
function link_feeds_set_target($source, $entity, $target, $value) {
if (empty($value)) {
return;
}
// Handle non-multiple value fields.
if (!is_array($value)) {
$value = array($value);
}
// Iterate over all values.
list($field_name, $column) = explode(':', $target);
$info = field_info_field($field_name);
$field = isset($entity->$field_name) ? $entity->$field_name : array();
$delta = 0;
foreach ($value as $v) {
if ($info['cardinality'] == $delta) {
break;
}
if (is_object($v) && ($v instanceof FeedsElement)) {
$v = $v->getValue();
}
if (is_scalar($v)) {
$field['und'][$delta][$column] = $v;
$delta++;
}
}
$entity->$field_name = $field;
}

View File

@@ -0,0 +1,74 @@
<?php
/**
* @file
* On behalf implementation of Feeds mapping API for number.module.
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsProcessor::getMappingTargets()
*/
function number_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
$numeric_types = array(
'list_integer',
'list_float',
'list_boolean',
'number_integer',
'number_decimal',
'number_float',
);
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
$info = field_info_field($name);
if (in_array($info['type'], $numeric_types)) {
$targets[$name] = array(
'name' => check_plain($instance['label']),
'callback' => 'number_feeds_set_target',
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
);
}
}
}
/**
* Callback for mapping numerics.
*
* Ensure that $value is a numeric to avoid database errors.
*/
function number_feeds_set_target($source, $entity, $target, $value) {
// Do not perform the regular empty() check here. 0 is a valid value. That's
// really just a performance thing anyway.
if (!is_array($value)) {
$value = array($value);
}
$info = field_info_field($target);
// Iterate over all values.
$field = isset($entity->$target) ? $entity->$target : array('und' => array());
// Allow for multiple mappings to the same target.
$delta = count($field['und']);
foreach ($value as $v) {
if ($info['cardinality'] == $delta) {
break;
}
if (is_object($v) && ($v instanceof FeedsElement)) {
$v = $v->getValue();
}
if (is_numeric($v)) {
$field['und'][$delta]['value'] = $v;
$delta++;
}
}
$entity->$target = $field;
}

View File

@@ -0,0 +1,120 @@
<?php
/**
* @file
* On behalf implementation of Feeds mapping API for path.module.
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsNodeProcessor::getMappingTargets().
*/
function path_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
switch ($entity_type) {
case 'node':
case 'taxonomy_term':
case 'user':
$targets['path_alias'] = array(
'name' => t('Path alias'),
'description' => t('URL path alias of the node.'),
'callback' => 'path_feeds_set_target',
'summary_callback' => 'path_feeds_summary_callback',
'form_callback' => 'path_feeds_form_callback',
);
break;
}
}
/**
* Callback for mapping. Here is where the actual mapping happens.
*
* When the callback is invoked, $target contains the name of the field the
* user has decided to map to and $value contains the value of the feed item
* element the user has picked as a source.
*/
function path_feeds_set_target($source, $entity, $target, $value, $mapping) {
if (empty($value)) {
$value = '';
}
// Path alias cannot be multi-valued, so use the first value.
if (is_array($value)) {
$value = $value[0];
}
$entity->path = array();
$entity_type = $source->importer->processor->entityType();
list($id, , ) = entity_extract_ids($entity_type, $entity);
if ($id) {
$uri = entity_uri($entity_type, $entity);
// Check for existing aliases.
if ($path = path_load($uri['path'])) {
$entity->path = $path;
}
}
$entity->path['pathauto'] = FALSE;
// Allow pathauto to set the path alias if the option is set, and this value
// is empty.
if (!empty($mapping['pathauto_override']) && !$value) {
$entity->path['pathauto'] = TRUE;
}
else {
$entity->path['alias'] = ltrim($value, '/');
}
}
/**
* Mapping configuration summary for path.module.
*
* @param $mapping
* Associative array of the mapping settings.
* @param $target
* Array of target settings, as defined by the processor or
* hook_feeds_processor_targets_alter().
* @param $form
* The whole mapping form.
* @param $form_state
* The form state of the mapping form.
*
* @return
* Returns, as a string that may contain HTML, the summary to display while
* the full form isn't visible.
* If the return value is empty, no summary and no option to view the form
* will be displayed.
*/
function path_feeds_summary_callback($mapping, $target, $form, $form_state) {
if (!module_exists('pathauto')) {
return;
}
if (empty($mapping['pathauto_override'])) {
return t('Do not allow Pathauto if empty.');
}
else {
return t('Allow Pathauto if empty.');
}
}
/**
* Settings form callback.
*
* @return
* The per mapping configuration form. Once the form is saved, $mapping will
* be populated with the form values.
*/
function path_feeds_form_callback($mapping, $target, $form, $form_state) {
return array(
'pathauto_override' => array(
'#type' => 'checkbox',
'#title' => t('Allow Pathauto to set the alias if the value is empty.'),
'#default_value' => !empty($mapping['pathauto_override']),
),
);
}

View File

@@ -0,0 +1,35 @@
<?php
/**
* @file
* On behalf implementation of Feeds mapping API for user profiles.
*/
/**
* Implements hook_feeds_processor_target_alter().
*/
function profile_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
if ($entity_type != 'user') {
return;
}
$categories = profile_user_categories();
foreach ($categories as $category) {
foreach (_profile_get_fields($category['name']) as $record) {
$targets[$record->name] = array(
'name' => t('Profile: @name', array('@name' => $record->title)),
'description' => t('Profile: @name', array('@name' => $record->title)),
'callback' => 'profile_feeds_set_target',
);
}
}
}
/**
* Set the user profile target after import.
*/
function profile_feeds_set_target($source, $entity, $target, $value, $mapping) {
$entity->$target = $value;
}

View File

@@ -0,0 +1,186 @@
<?php
/**
* @file
* Mapper that exposes a node's taxonomy vocabularies as mapping targets.
*/
/**
* Implements hook_feeds_parser_sources_alter().
*
* @todo: Upgrade to 7.
*/
function taxonomy_feeds_parser_sources_alter(&$sources, $content_type) {
if (!empty($content_type)) {
foreach (taxonomy_get_vocabularies($content_type) as $vocabulary) {
$sources['parent:taxonomy:' . $vocabulary->machine_name] = array(
'name' => t('Feed node: Taxonomy: @vocabulary', array('@vocabulary' => $vocabulary->name)),
'description' => t('Taxonomy terms from feed node in given vocabulary.'),
'callback' => 'taxonomy_feeds_get_source',
);
}
}
}
/**
* Callback, returns taxonomy from feed node.
*/
function taxonomy_feeds_get_source(FeedsSource $source, FeedsParserResult $result, $key) {
if ($node = node_load($source->feed_nid)) {
$terms = taxonomy_feeds_node_get_terms($node);
$vocabularies = taxonomy_vocabulary_load_multiple(array(), array('machine_name' => str_replace('parent:taxonomy:', '', $key)));
$vocabulary = array_shift($vocabularies);
$result = array();
foreach ($terms as $tid => $term) {
if ($term->vid == $vocabulary->vid) {
$result[] = new FeedsTermElement($term);
}
}
return $result;
}
}
/**
* Implements hook_feeds_processor_targets_alter().
*/
function taxonomy_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
$info = field_info_field($name);
if ($info['type'] == 'taxonomy_term_reference') {
$targets[$name] = array(
'name' => check_plain($instance['label']),
'callback' => 'taxonomy_feeds_set_target',
'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
);
}
}
}
/**
* Callback for mapping. Here is where the actual mapping happens.
*
* @todo Do not create new terms for non-autotag fields.
*/
function taxonomy_feeds_set_target($source, $entity, $target, $terms) {
if (empty($terms)) {
return;
}
// Handle non-multiple values.
if (!is_array($terms)) {
$terms = array($terms);
}
$info = field_info_field($target);
// See http://drupal.org/node/881530
if (isset($info['settings']['allowed_values'][0]['vocabulary'])) {
$vocabulary = taxonomy_vocabulary_machine_name_load($info['settings']['allowed_values'][0]['vocabulary']);
}
else {
$vocabulary = taxonomy_vocabulary_load($info['settings']['allowed_values'][0]['vid']);
}
$i = 0;
$entity->$target = isset($entity->$target) ? $entity->$target : array();
foreach ($terms as $term) {
$tid = 0;
if ($term instanceof FeedsTermElement) {
$tid = $term->tid;
}
elseif (is_numeric($term)) {
$tid = $term;
}
elseif (is_string($term)) {
$tid = taxonomy_term_check_term($term, $vocabulary->vid);
}
if ($tid) {
$entity->{$target}['und'][$i]['tid'] = $tid;
}
if ($info['cardinality'] == 1) {
break;
}
$i++;
}
}
/**
* Find all terms associated with the given node, within one vocabulary.
*/
function taxonomy_feeds_node_get_terms($node, $key = 'tid') {
$terms = &drupal_static(__FUNCTION__);
if (!isset($terms[$node->nid][$key])) {
// Get tids from all taxonomy_term_reference fields.
$tids = array();
$fields = field_info_fields();
foreach ($fields as $field_name => $field) {
if ($field['type'] == 'taxonomy_term_reference' && field_info_instance('node', $field_name, $node->type)) {
if (($items = field_get_items('node', $node, $field_name)) && is_array($items)) {
$tids = array_merge($tids, array_map('_taxonomy_extract_tid', $items));
}
}
}
// Load terms and cache them in static var.
$curr_terms = taxonomy_term_load_multiple($tids);
$terms[$node->nid][$key] = array();
foreach ($curr_terms as $term) {
$terms[$node->nid][$key][$term->$key] = $term;
}
}
return $terms[$node->nid][$key];
}
/**
* Helper function used in taxonomy_feeds_node_get_terms(). Extracts
* tid from array item returned by field_get_items().
*
* @param $item tid information in a form of single element array (key == 'tid', value == tid we're looking for)
*
* @return tid extracted from $item.
*
* @see taxonomy_feeds_node_get_terms()
* @see field_get_items()
*/
function _taxonomy_extract_tid($item) {
return $item['tid'];
}
/**
* Checks whether a term identified by name and vocabulary exists. Creates a
* new term if it does not exist.
*
* @param $name
* A term name.
* @param $vid
* A vocabulary id.
*
* @return
* A term id.
*/
function taxonomy_term_check_term($name, $vid) {
$name = trim($name);
$term = taxonomy_term_lookup_term($name, $vid);
if (empty($term)) {
$term = new stdClass();
$term->name = $name;
$term->vid = $vid;
taxonomy_term_save($term);
return $term->tid;
}
return $term->tid;
}
/**
* Looks up a term, assumes SQL storage backend.
*/
function taxonomy_term_lookup_term($name, $vid) {
return db_select('taxonomy_term_data', 'td')
->fields('td', array('tid', 'name'))
->condition('name', $name)
->condition('vid', $vid)
->execute()
->fetchObject();
}

View File

@@ -0,0 +1,79 @@
<?php
/**
* @file
* On behalf implementation of Feeds mapping API for text.module.
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsProcessor::getMappingTargets()
*/
function text_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
$text_types = array(
'list_text',
'text',
'text_long',
'text_with_summary',
);
foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
$info = field_info_field($name);
if (in_array($info['type'], $text_types)) {
$targets[$name] = array(
'name' => check_plain($instance['label']),
'callback' => 'text_feeds_set_target',
'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
);
}
}
}
/**
* Callback for mapping text fields.
*/
function text_feeds_set_target($source, $entity, $target, $value) {
if (empty($value)) {
return;
}
if (!is_array($value)) {
$value = array($value);
}
if (isset($source->importer->processor->config['input_format'])) {
$format = $source->importer->processor->config['input_format'];
}
$info = field_info_field($target);
// Iterate over all values.
$field = isset($entity->$target) ? $entity->$target : array('und' => array());
// Allow for multiple mappings to the same target.
$delta = count($field['und']);
foreach ($value as $v) {
if ($info['cardinality'] == $delta) {
break;
}
if (is_object($v) && ($v instanceof FeedsElement)) {
$v = $v->getValue();
}
if (is_scalar($v)) {
$field['und'][$delta]['value'] = $v;
if (isset($format)) {
$field['und'][$delta]['format'] = $format;
}
$delta++;
}
}
$entity->$target = $field;
}