-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprofile_entity.module
More file actions
executable file
·133 lines (130 loc) · 4.22 KB
/
profile_entity.module
File metadata and controls
executable file
·133 lines (130 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Module to convert core profile to user entity fields
*/
/**
* Implements hook_menu().
*/
function profile_entity_menu() {
$item['admin/config/people/profile-migration'] = array(
'title' => 'Migrate Core Profile to User Entity',
'description' => 'Migrate core profile to user entities with fields',
'page callback' => 'drupal_get_form',
'page arguments' => array('profile_entity_export'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $item;
}
/**
* Export start form
* @form
* form elements array.
*/
function profile_entity_export($form) {
$form['start'] = array(
'#type' => 'submit',
'#title' => 'Start',
'#value' => 'Start'
);
return $form;
}
/**
* Export Start Form Submit
* @form
* form elements array
* @form_state
* form submitted values
*/
function profile_entity_export_submit($form, &$form_state) {
$batch = array(
'title' => t('Exporting'),
'operations' => array(
array('profile_entity_quick_migrate', array()),
),
'finished' => 'profile_entity_batch_finished',
'file' => 'profile_entity.module',
);
batch_set($batch);
}
/**
* Exporting Data in batch process
* @context
* context array with batch operation values
*/
function profile_entity_quick_migrate(&$context) {
$result = db_query("select * from {profile_field}");
$lists = array();
foreach ($result as $record) {
$lists[$record->fid]['value'] = 'field_'.$record->name;
$lists[$record->fid]['type'] = $record->type;
}
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_node'] = 0;
$context['sandbox']['max'] = db_query("SELECT COUNT(DISTINCT u.uid) FROM {users} u inner join {profile_value} pv on pv.uid = u.uid")->fetchField();
}
$limit = 50;
$query = db_select('users', 'u');
$query->join('profile_value', 'v', 'u.uid = v.uid');
$query->fields('u',array('uid'));
$query->condition('u.uid', $context['sandbox']['current_node'], '>');
$query->orderBy('uid');
$query->range(0, $limit);
$result = $query->execute();
foreach ($result as $record) {
$user = user_load($record->uid);
$edit = array();
$profile = db_query("select * from {profile_value} where uid = :uid", array(":uid" => $user->uid));
foreach ($profile as $profile_values) {
if ($lists[$profile_values->fid]['type'] == 'selection') {
$select_arr = unserialize($profile_values->value);
if (count($select_arr) > 1) {
$i = 0;
foreach ($select_arr as $key => $value) {
$edit[$lists[$profile_values->fid]['value']]['und'][$i] = array('value' => $value);
$i++;
}
}
elseif (is_array($select_arr)) {
$edit[$lists[$profile_values->fid]['value']]['und'][0]['value'] = $select_arr[0];
}
else {
$edit[$lists[$profile_values->fid]['value']]['und'][0]['value'] = $profile_values->value;
}
}
else if ($lists[$profile_values->fid]['type'] == 'date') {
$date_arr = unserialize($profile_values->value);
$edit[$lists[$profile_values->fid]['value']]['und'][0]['value'] = $date_arr['year'] .'-'. $date_arr['month'] .'-'. $date_arr['day'];
}
else {
$edit[$lists[$profile_values->fid]['value']]['und'][0]['value'] = $profile_values->value;
}
}
$user = user_save($user, $edit);
$context['results'][] = $user->uid . ' : ' . check_plain($user->name);
$context['sandbox']['progress']++;
$context['sandbox']['current_node'] = $user->uid;
$context['message'] = $context['sandbox']['progress'] .' Done';
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
/**
* Batch operation finished callback redirect to form page
*/
function profile_entity_batch_finished($success, $results, $operations) {
if ($success) {
$message = format_plural(count($results), 'One user processed.', '@count users processed.');
}
else {
$message = t('Finished with an error.');
}
drupal_set_message($message);
foreach ($results as $result) {
$items[] = t('Loaded user %title.', array('%title' => $result));
}
$_SESSION['my_batch_results'] = $items;
drupal_goto('admin/config/people/profile-migration');
}