Index: migrate_extras.module =================================================================== --- migrate_extras.module (revision 91) +++ migrate_extras.module (working copy) @@ -16,6 +16,7 @@ 'filefield' => array('status' => FALSE), 'location' => array('status' => FALSE), 'privatemsg' => array('status' => FALSE), + 'flag' => array('status' => FALSE), ), ); return $api; Index: flag.migrate.inc =================================================================== --- flag.migrate.inc (revision 0) +++ flag.migrate.inc (revision 0) @@ -0,0 +1,132 @@ + 'Flag'); + return $types; +} + +/** + * Implementation of hook_migrate_fields(). + */ +function flag_migrate_fields_flag($type) { + + $fields = array( + 'flag_name' => t('Flag name'), + 'content_id' => t('Source content id'), + 'uid' => t('Source user id'), + 'timestamp' => t('UNIX timestamp'), + ); + + return $fields; +} + +/** + * Implementation of hook_migrate_prepare(). + */ +/* +function flag_migrate_prepare_flag(&$ur, $tblinfo, $row) { + //not used. +} +*/ + +/** + * Implementation of hook_migrate_import(). + */ +function flag_migrate_import_flag($tblinfo, $row) { + + foreach ($tblinfo->fields as $destfield => $values) { + if ($values['srcfield'] && $row->$values['srcfield']) { + $flag_content->$destfield = $row->$values['srcfield']; + } + else { + $flag_content->$destfield = $values['default_value']; + } + } + + timer_start('prepare_flag'); + $errors = migrate_destination_invoke_all('prepare_flag', $flag_content, $tblinfo, $row); + timer_stop('prepare_flag'); + + + $success = TRUE; + foreach ($errors as $error) { + if ($error['level'] != MIGRATE_MESSAGE_INFORMATIONAL) { + $success = FALSE; + break; + } + } + + $flag = flag_get_flag($flag_content->flag_name); + if (empty($flag)) { + $errors[] = migrate_message(t('No matching flag name found.')); + $success = FALSE; + } + + if ($success) { + timer_start('save_flag'); + $ret = FALSE; + $new_nid = _migrate_xlat_get_new_id('node', $flag_content->content_id); + $new_uid = _migrate_xlat_get_new_id('user', $flag_content->uid); + if ($new_nid && $new_uid) { + $account = user_load($new_uid); + $ret = flag('flag', $flag_content->flag_name, $new_nid, $account); + if (!$ret) { + $errors[] = migrate_message(t('Failed to import flag.')); + } + else { + // Find out dest fcid. + $flag_content->fcid = db_result(db_query("SELECT fcid FROM {flag_content} WHERE content_id = %d AND uid = %d AND fid = %d AND content_type = '%s'", $new_nid, $new_uid, $flag->fid, $flag->content_type)); + } + } + else { + $errors[] = migrate_message(t('No matching node or user found.')); + } + timer_stop('save_flag'); + + // Call completion hooks, for any processing which needs to be done after node_save. + timer_start('flag completion hooks'); + $errors = array_merge($errors, migrate_destination_invoke_all('complete_flag', $flag_content, $tblinfo, $row)); + timer_stop('flag completion hooks'); + + $sourcekey = $tblinfo->sourcekey; + + // Map the flagged entry. + if ($flag_content->fcid) { + $sourcekey = $tblinfo->sourcekey; + migrate_add_mapping($tblinfo->mcsid, $row->$sourcekey, $flag_content->fcid); + } + } + return $errors; +} + +/** + * Implementation of hook_migrate_complete(). + */ +function flag_migrate_complete_flag(&$flag, $tblinfo, $row) { + // Set the timestamp to the original. + if ($flag->timestamp) { + db_query("UPDATE {flag_content} fc SET fc.timestamp = %d WHERE fcid = %d", $flag->timestamp, $flag->fcid); + } +} + +/** + * Implementation of hook_migrate_delete(). + */ +function flag_migrate_delete_flag($fcid) { + $record = db_fetch_object(db_query("SELECT f.name AS flag_name, fc.* FROM {flag_content} fc, {flags} f WHERE f.fid = fc.fid AND fcid = %d", $fcid)); + $account = user_load($record->uid); + $ret = flag('unflag', $record->flag_name, $record->content_id, $account); +} + +