content_profile.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // Functionality depends on node_import and CCK.
  3. if (module_exists('node_import') && module_exists('content')) {
  4. // Load the required API files.
  5. include_once('./' . drupal_get_path('module', 'node_import') . '/node_import.api.php');
  6. include_once('./' . drupal_get_path('module', 'node_import') . '/node_import.inc');
  7. /**
  8. * Implementation of hook_user_import_form_field_match(). Add supported Content Profile fields into our dropdown list.
  9. */
  10. function content_profile_user_import_form_field_match() {
  11. $options = array();
  12. $options['content_profile'] = array();
  13. $field_options = array();
  14. $contentprofile_types = content_profile_get_types();
  15. foreach ($contentprofile_types as $type => $data) {
  16. $fields = node_import_fields('node:' . $type, TRUE);
  17. // Give fields a more descriptive title.
  18. foreach (array_keys($fields) as $key) {
  19. if (strstr($key, 'cck:field_')) {
  20. $field_options["$type $key"] = t('Content Profile: (!type) !key ', array('!key' => $fields[$key]['title'], '!type' => $type));
  21. }
  22. }
  23. // skip merge if there are no fields on the content type
  24. if (!empty($field_options)) {
  25. $options['content_profile'] = array_merge($options['content_profile'], $field_options);
  26. }
  27. }
  28. /* We do not support taxonomy yet */
  29. return $options;
  30. }
  31. /**
  32. * Implementation of hook_user_import_form_update_user().
  33. */
  34. function content_profile_user_import_form_update_user() {
  35. $form['content_profile'] = array('title' => t('Content Profile'), 'description' => t('Affected: fields in Content Profile nodes.'));
  36. return $form;
  37. }
  38. /**
  39. * Implementation of hook_user_import_data().
  40. */
  41. function content_profile_user_import_data($settings, $update_setting, $column_settings, $module, $field_id, $data, $column_id) {
  42. if ($module != 'content_profile') return;
  43. return trim($data[$column_id]);
  44. }
  45. /**
  46. * Implementation of hook_user_import_after_save().
  47. */
  48. function content_profile_user_import_after_save($settings, $account, $password, $fields, $updated, $update_setting_per_module) {
  49. if (!is_array($fields['content_profile'])) return;
  50. // check if it's an existing user and if content profile is to be updated
  51. if ($updated && $update_setting_per_module['content_profile'] == UPDATE_NONE) return;
  52. // arrange values by content type
  53. foreach ($fields['content_profile'] as $column_id => $column_data) {
  54. if (!empty($column_data)) {
  55. $keys = explode(' ', $column_id);
  56. $contentprofile[$keys[0]][$keys[1]] = $column_data;
  57. }
  58. }
  59. $contentprofile_types = content_profile_get_types();
  60. // process each $content profile
  61. foreach ($contentprofile_types as $type => $configuration) {
  62. content_profile_user_import_node($type, $contentprofile, $account, $fields, $updated, $update_setting_per_module['content_profile']);
  63. }
  64. return;
  65. }
  66. /**
  67. * callback to create or update a node if appropriate
  68. */
  69. function content_profile_user_import_node($type, $content_profile, $account, $fields, $updated, $update_setting) {
  70. if (empty($content_profile[$type])) return; // pass only those, which are used
  71. $errors = array();
  72. $title_empty = time();
  73. if ($updated) {
  74. $node = node_load(array('type' => $type, 'uid' => $account->uid));
  75. }
  76. if (empty($node)) {
  77. $node = new stdClass();
  78. $node->type = $type;
  79. $node->status = 1;
  80. $node->title = $title_empty;
  81. }
  82. // Assign the mapped fields to the $node.
  83. foreach ($content_profile[$type] as $column_id => $column_data) {
  84. $field_data = explode(':', $column_id);
  85. $field_name = !empty($field_data[1]) ? $field_data[1] : $column_id;
  86. $field_key = !empty($field_data[2]) ? $field_data[2] : 'value';
  87. $field_value = array(0 => array($field_key => $column_data[0]));
  88. if (!$updated) {
  89. $node->{$field_name} = $field_value;
  90. }
  91. elseif ($updated && $update_setting == UPDATE_ADD) {
  92. $current_content = content_format($field_name, $node->{$field_name}[0], 'default', $node);
  93. if (empty($current_content) && !empty($column_data[0])) {
  94. $node->{$field_name} = $field_value;
  95. }
  96. }
  97. elseif ($updated && $update_setting == UPDATE_REPLACE) {
  98. $node->{$field_name} = $field_value;
  99. }
  100. }
  101. // not actually checking for errors at the moment, but lete's leave this in for when we do
  102. if (empty($errors)) {
  103. $node->uid = $account->uid;
  104. $node->name = $account->name;
  105. // Assign a default title if one has not already been mapped.
  106. if (!isset($node->title) || empty($node->title) || $node->title == $title_empty) {
  107. $node->title = $node->name;
  108. }
  109. $node = node_submit($node);
  110. // make sure author is not changed when submited (hapens if existing node)
  111. $node->uid = $account->uid;
  112. $node->name = $account->name;
  113. node_save($node);
  114. if (!$node->nid) {
  115. drupal_set_message(t('Unknown error on saving the node: %node_data! Check watchdog logs for details.', array('%node_data' => "$node->title ($node->type)")), 'error');
  116. }
  117. }
  118. else {
  119. /**
  120. * @todo report errors
  121. */
  122. }
  123. }
  124. }
  125. else {
  126. drupal_set_message(t('Please enable %module module!', array('%module' => 'node_import')));
  127. }