profile.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * Implementation of hook_user_import_form_field_match().
  4. */
  5. function profile_user_import_form_field_match() {
  6. module_load_include('inc', 'user_import', 'user_import.admin');
  7. $fields = _user_import_profile('fid', 'title');
  8. $options['profile'] = $fields;
  9. return $options;
  10. }
  11. /**
  12. * Implementation of hook_user_import_form_update_user().
  13. */
  14. function profile_user_import_form_update_user() {
  15. $form['profile'] = array('title' => t('Profile'), 'description' => t('Affected: Profile fields.'));
  16. return $form;
  17. }
  18. /**
  19. * Implementation of hook_user_import_data().
  20. */
  21. function profile_user_import_data($settings, $update_setting, $column_settings, $module, $field_id, $data, $column_id) {
  22. if ($module != 'profile') return;
  23. return trim($data[$column_id]);
  24. }
  25. /**
  26. * Implementation of hook_user_import_after_save().
  27. */
  28. function profile_user_import_after_save($settings, $account, $password, $fields, $updated, $update_setting_per_module) {
  29. // get all fields
  30. $profile_fields = profile_get_fields();
  31. $data = $old_data = unserialize($account->data);
  32. foreach ($profile_fields as $field) {
  33. profile_user_import_save_profile($field, $account->uid, $fields['profile'][$field->fid][0], $updated, $update_setting_per_module['profile'], $data);
  34. }
  35. // data column in the user table needs to be updated
  36. if ($data != $old_data) {
  37. db_update('users')
  38. ->fields(array('data' => serialize($data)))
  39. ->condition('uid', $account->uid)
  40. ->execute();
  41. }
  42. return;
  43. }
  44. function profile_user_import_save_profile($field, $uid, $value, $updated, $update_setting, &$data) {
  45. // when the profile field is displayed on the registration form an empty value is automatically saved by the Profile module
  46. $exists = db_query_range('SELECT value FROM {profile_values} WHERE fid = :fid AND uid = :uid', 0, 1, array(':fid' => $field->fid, ':uid' => $uid))->fetchField();
  47. user_import_profile_date($value, $field->type);
  48. if ($updated) {
  49. switch ($update_setting) {
  50. case UPDATE_NONE:
  51. return;
  52. case UPDATE_ADD:
  53. if (empty($value) || (!empty($exists) && $exists != '')) return;
  54. case UPDATE_REPLACE:
  55. if (empty($value) && $update_setting == UPDATE_REPLACE) {
  56. db_query("DELETE FROM {profile_values} WHERE fid = :fid AND uid = :uid", array(':fid' => $field->fid, ':uid' => $uid));
  57. unset($data[$field->name]);
  58. return;
  59. }
  60. if ((empty($exists) && $exists != '') || $exists === FALSE) {
  61. $id = db_insert('profile_values')
  62. ->fields(array(
  63. 'fid' => $field->fid,
  64. 'uid' => $uid,
  65. 'value' => $value,
  66. ))
  67. ->execute();
  68. }
  69. else {
  70. $id = db_update('profile_values')
  71. ->fields(array(
  72. 'value' => $value,
  73. ))
  74. ->condition('fid', $field->fid)
  75. ->condition('uid', $uid)
  76. ->execute();
  77. }
  78. $data[$field->name] = $value;
  79. return;
  80. }
  81. }
  82. else {
  83. if (empty($value)) return;
  84. if ((empty($exists) && $exists != '') || $exists === FALSE) {
  85. $id = db_insert('profile_values')
  86. ->fields(array('fid' => $field->fid, 'uid' => $uid, 'value' => $value))
  87. ->execute();
  88. }
  89. else {
  90. db_update('profile_values')
  91. ->fields(array('value' => $value))
  92. ->condition('fid', $field->fid)
  93. ->condition('uid', $uid)
  94. ->limit(1)
  95. ->execute();
  96. $data[$field->name] = $value;
  97. }
  98. }
  99. return;
  100. }
  101. /**
  102. *
  103. */
  104. function profile_get_fields() {
  105. static $fields = array();
  106. // Avoid making more than one database call for profile info.
  107. if (empty($fields)) {
  108. $results = db_query('SELECT * FROM {profile_field}');
  109. foreach ($results as $row) {
  110. $fields[] = $row;
  111. }
  112. }
  113. return $fields;
  114. }
  115. /**
  116. * Convert date into format that Profile module expects:
  117. * a:3:{s:5:"month";s:1:"1";s:3:"day";s:1:"1";s:4:"year";s:4:"1976";}
  118. */
  119. function user_import_profile_date(&$value, $field_type) {
  120. if ($field_type != "date" || empty($value)) {
  121. return;
  122. }
  123. $date = explode('/', $value);
  124. if (!is_array($date)) {
  125. return;
  126. }
  127. // Get seleceted format.
  128. $date_format = variable_get('user_import_profile_date_format', 'MM/DD/YYYY');
  129. if ($date_format == 'MM/DD/YYYY') {
  130. list($month, $day, $year) = explode('/', $value);
  131. }
  132. else {
  133. if ($date_format == 'DD/MM/YYYY') {
  134. list($day, $month, $year) = explode('/', $value);
  135. }
  136. else {
  137. if ($date_format == 'YYYY/MM/DD') {
  138. list($year, $month, $day) = explode('/', $value);
  139. }
  140. else {
  141. if ($date_format == 'YYYY/DD/MM') {
  142. list($year, $day, $month) = explode('/', $value);
  143. }
  144. }
  145. }
  146. }
  147. // Leading zeros cause a problem so remove them.
  148. if (substr($day, 0, 1) == '0') {
  149. $day = substr($day, 1, 1);
  150. }
  151. if (substr($month, 0, 1) == '0') {
  152. $month = substr($month, 1, 1);
  153. }
  154. $value = serialize(array('month' => $month, 'day' => $day, 'year' => $year));
  155. }