/[drupal]/contributions/modules/migrator/migrator.admin-pages.inc
ViewVC logotype

Contents of /contributions/modules/migrator/migrator.admin-pages.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Mon Apr 28 18:30:53 2008 UTC (18 months, 4 weeks ago) by sun
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +2 -1 lines
File MIME type: text/x-php
Added variable to switch db connection only when necessary.
1 <?php
2 // $Id: migrator.admin-pages.inc,v 1.2 2008/04/28 18:24:43 sun Exp $
3
4 /**
5 * @file
6 * Migrator remote system settings and item mappings.
7 */
8
9 /**
10 * System settings form for remote system.
11 */
12 function migrator_settings_system() {
13 $form = array();
14 $options = array();
15 $systems = glob(drupal_get_path('module', 'migrator') .'/*', GLOB_ONLYDIR);
16 foreach ($systems as $option) {
17 if (file_exists($option .'/system.inc')) {
18 $key = preg_replace('/[^a-zA-Z0-9]/', '', basename($option)); // obsolete?
19 $options[$key] = drupal_ucfirst(basename($option));
20 }
21 }
22 $form['system'] = array(
23 '#type' => 'select',
24 '#title' => t('Remote system'),
25 '#options' => $options,
26 '#default_value' => migrator_system(),
27 );
28
29 $options_features = array();
30 foreach (migrator_system_features() as $feature => $item) {
31 $options_features[$feature] = drupal_ucfirst($feature);
32 }
33 $form['features'] = array(
34 '#type' => 'checkboxes',
35 '#title' => t('Data to migrate'),
36 '#options' => $options_features,
37 '#default_value' => variable_get('migrator_migrate_features', array()),
38 );
39
40 $form['submit'] = array(
41 '#type' => 'submit',
42 '#value' => t('Save'),
43 );
44 return $form;
45 }
46
47 function migrator_settings_system_submit($form_id, $form_values) {
48 if ($form_values['op'] == t('Save')) {
49 variable_set('migrator_system', $form_values['system']);
50 variable_set('migrator_migrate_features', $form_values['features']);
51 }
52 }
53
54 /**
55 * Settings form for remote system database connection.
56 */
57 function migrator_settings_database() {
58 $form = array();
59 $db = parse_url(variable_get('migrator_db', ''));
60 $form['db'] = array(
61 '#type' => 'fieldset',
62 '#title' => t('Database connection'),
63 );
64 $form['db']['scheme'] = array(
65 '#type' => 'radios',
66 '#title' => t('Database interface'),
67 '#options' => array('mysql' => 'MySQL', 'mysqli' => 'MySQLi'),
68 '#default_value' => $GLOBALS['db_type'], // was: $db['scheme'],
69 '#disabled' => TRUE,
70 '#description' => t('Due to the database abstraction design in Drupal 5 and 6, only the current default database interface is supported.'),
71 );
72 $form['db']['host'] = array(
73 '#type' => 'textfield',
74 '#title' => t('Host'),
75 '#default_value' => !empty($db['host']) ? $db['host'] : 'localhost',
76 '#required' => TRUE,
77 );
78 $form['db']['path'] = array(
79 '#type' => 'textfield',
80 '#title' => t('Database'),
81 '#default_value' => substr($db['path'], 1),
82 '#required' => TRUE,
83 );
84 $form['db']['user'] = array(
85 '#type' => 'textfield',
86 '#title' => t('Username'),
87 '#default_value' => $db['user'],
88 '#required' => TRUE,
89 );
90 $form['db']['pass'] = array(
91 '#type' => 'textfield',
92 '#title' => t('Password'),
93 '#default_value' => $db['pass'],
94 '#required' => TRUE,
95 );
96 $form['db']['db_prefix'] = array(
97 '#type' => 'textfield',
98 '#title' => t('Table prefix'),
99 '#default_value' => variable_get('migrator_db_prefix', 'jos_'),
100 );
101 $form[] = array(
102 '#type' => 'submit',
103 '#value' => t('Save'),
104 );
105 return $form;
106 }
107
108 function migrator_settings_database_submit($form_id, $form_values) {
109 $url = $form_values['scheme'] .'://'. $form_values['user'] .':'. $form_values['pass'] .'@'. $form_values['host'] .'/'. $form_values['path'];
110 variable_set('migrator_db', $url);
111 variable_set('migrator_db_is_default', (is_array($GLOBALS['db_url']) ? $GLOBALS['db_url']['default'] == $url : $GLOBALS['db_url'] == $url));
112 variable_set('migrator_db_prefix', $form_values['db_prefix']);
113 }
114
115 /**
116 * Mapping form for remote system user roles.
117 */
118 function migrator_settings_roles() {
119 migrator_check_db();
120
121 $remote_roles = migrator_get('roles');
122 $drupal_roles = user_roles(TRUE);
123 $mappings = migrator_map_get('role');
124
125 $form = array();
126 $form['object'] = array(
127 '#type' => 'value',
128 '#value' => 'role',
129 );
130 $form['#mapping_type'] = 'select';
131 migrator_mapping_form($form, $remote_roles, $drupal_roles, $mappings);
132 return $form;
133 }
134
135 function migrator_settings_roles_submit($form_id, $form_values) {
136 migrator_map_set('role', $form_values['mappings'], TRUE);
137 }
138
139 /**
140 * Mapping form for remote system user roles.
141 */
142 function migrator_settings_users() {
143 migrator_check_db();
144
145 $mappings = migrator_map_get('user');
146 $remote_users = array();
147 $drupal_users = array();
148 $ops = array();
149
150 if (count($mappings)) {
151 $where = migrator_key('user') .' IN ('. implode(',', array_fill(0, count($mappings), '%d')) .')';
152 $remote_users = migrator_get('users', $where, array_keys($mappings));
153 foreach ($mappings as $rid => $uid) {
154 $remote_users[$rid] = $remote_users[$rid]['name'];
155 $user = user_load(array('uid' => $uid));
156 $drupal_users[$uid] = theme('username', $user);
157 $ops[$rid] = l(t('Delete'), "admin/settings/migrator/delete/user/$rid");
158 }
159 }
160
161 $form = array();
162 $form['object'] = array(
163 '#type' => 'value',
164 '#value' => 'user',
165 );
166
167 $form['remote_user'] = array(
168 '#type' => 'textfield',
169 '#size' => 30,
170 '#maxlength' => 60,
171 // #name and #parents required for drupal_render().
172 '#name' => 'remote_user',
173 '#parents' => array('remote_user'),
174 // Attach autocomplete behaviour. (id required)
175 '#autocomplete_path' => 'admin/settings/migrator/autocomplete/user',
176 '#id' => 'remote-user',
177 );
178 $form['drupal_user'] = array(
179 '#type' => 'textfield',
180 '#size' => 30,
181 '#maxlength' => 60,
182 // #name and #parents required for drupal_render().
183 '#name' => 'drupal_user',
184 '#parents' => array('drupal_user'),
185 // Attach autocomplete behaviour. (id required)
186 '#autocomplete_path' => 'user/autocomplete',
187 '#id' => 'drupal-user',
188 );
189 $form['submit'] = array(
190 '#type' => 'submit',
191 '#value' => t('Add'),
192 );
193 $remote_users[-1] = drupal_render($form['remote_user']);
194 $drupal_users[-1] = drupal_render($form['drupal_user']);
195 $mappings[-1] = -1;
196 $ops[-1] = drupal_render($form['submit']);
197
198 migrator_mapping_form($form, $remote_users, $drupal_users, $mappings, $ops);
199 return $form;
200 }
201
202 function migrator_settings_users_submit($form_id, $form_values) {
203 $remote_user = migrator_get('uid', $form_values['remote_user']);
204 $drupal_user = user_load(array('name' => $form_values['drupal_user']));
205
206 migrator_map_set('user', array($remote_user => $drupal_user->uid));
207 }
208
209 /**
210 * Generate a remote/local item mapping form in a table.
211 *
212 * If $form['#mapping_type'] is set to 'select', this function assumes a
213 * 1:1 relationship between remote and local objects. Practical examples:
214 * - User roles: Each remote role needs to be assigned to a Drupal role.
215 *
216 * @param &$form
217 * A form structure, passed by reference.
218 * @param $remote
219 * An associative array containing remote system objects (id => name).
220 * @param $local
221 * An associative array containing Drupal system objects (id => name),
222 * or a callback function to invoke with the mapping value.
223 * @param $mappings
224 * An associative array containing id mappings for the objects above.
225 * @param $ops
226 * An associative array containing operations for each row (optional).
227 * If empty, Migrator automatically adds a save button to the form.
228 *
229 * @see migrator_settings_roles(), migrator_settings_users()
230 */
231 function migrator_mapping_form(&$form, $remote, $local, $mappings, $ops = NULL) {
232 $form['#theme'] = 'migrator_mapping_form';
233 $i = 0;
234 foreach ($remote as $rid => $name) {
235 $form['rows'][$i][0][] = array(
236 '#value' => $remote[$rid],
237 );
238 if ($form['#mapping_type'] == 'select') {
239 $form['rows'][$i][1][$rid] = array(
240 '#type' => 'select',
241 '#options' => $local,
242 '#default_value' => isset($mappings[$rid]) ? $mappings[$rid] : NULL,
243 '#parents' => array('mappings', $rid),
244 );
245 }
246 else {
247 $form['rows'][$i][1][] = array(
248 '#value' => $local[$mappings[$rid]],
249 );
250 }
251 if (!is_null($ops)) {
252 $form['rows'][$i][2][] = array(
253 '#value' => isset($ops[$rid]) ? $ops[$rid] : '',
254 );
255 }
256 $i++;
257 }
258 if (is_null($ops)) {
259 $form[] = array(
260 '#type' => 'submit',
261 '#value' => t('Save'),
262 );
263 }
264 }
265
266 /**
267 * Theme a mapping table containing configured remote/local item mappings.
268 *
269 * @see migrator_mapping_form()
270 */
271 function theme_migrator_mapping_form(&$form) {
272 $output = '';
273 if (count($form['rows'])) {
274 $header = array();
275 $object = $form['object']['#value'];
276 $header[] = t('Remote @object', array('@object' => $object));
277 $header[] = t('Drupal @object', array('@object' => $object));
278 if (isset($form['rows'][0][2])) {
279 $header[] = t('Operations');
280 }
281 $rows = array();
282 foreach ($form['rows'] as $i => $row) {
283 if (is_int($i)) {
284 $rows[] = array(
285 drupal_render($form['rows'][$i][0]),
286 drupal_render($form['rows'][$i][1]),
287 isset($form['rows'][$i][2]) ? drupal_render($form['rows'][$i][2]) : NULL,
288 );
289 }
290 }
291 $output .= theme('table', $header, $rows);
292 }
293 $output .= drupal_render($form);
294 return $output;
295 }
296

  ViewVC Help
Powered by ViewVC 1.1.2