pmnote.migrate.inc in Drupal PM (Project Management) 7.3
Migration functions for the PM Note module.
File
pmnote/includes/pmnote.migrate.inc
View source
<?php
define('PMNOTE_MIGRATE_MAX_JOB_PER_BATCH', 5);
function pmnote_migrate(&$sandbox) {
pmnote_migrate_create_fields($sandbox);
if (empty($sandbox['pmnote_current_vid_of_field_being_migrated'])) {
$sandbox['pmnote_current_vid_of_field_being_migrated'] = 0;
}
if (pmnote_migrate_field_data($sandbox) == FALSE) {
$sandbox['#finished'] = 0.5;
return;
}
db_drop_table('pmnote');
variable_del('node_options_pmnote');
variable_set('pm_permission_field_parent_reference_for_pmnote', 'pmnote_parent');
variable_set('pm_permission_node_pmnote_enabled', TRUE);
module_load_include('inc', 'pm', 'includes/pm.permission.migrate');
pm_permission_migrate_execute('pmnote');
$sandbox['#finished'] = 1;
return 'PM Note nodes have been migrated to field_api based fields successfully.';
}
function pmnote_migrate_update_could_be_performed() {
if (db_table_exists('pmnote')) {
return TRUE;
}
return FALSE;
}
function pmnote_migrate_create_fields(&$sandbox) {
module_load_include('inc', 'pmnote', 'includes/pmnote.field_base');
module_load_include('inc', 'pmnote', 'includes/pmnote.field_instance');
module_load_include('inc', 'pm', 'includes/pm.field');
$field_bases = pmnote_default_field_bases();
pm_field_bases_create_if_required($field_bases);
$field_instances = pmnote_default_field_instances();
pm_field_instances_create_if_required($field_instances);
return TRUE;
}
function pmnote_migrate_field_data(&$sandbox) {
$results = db_select('pmnote', 'pmn')
->fields('pmn')
->condition('vid', $sandbox['pmnote_current_vid_of_field_being_migrated'], '>')
->range(0, PMNOTE_MIGRATE_MAX_JOB_PER_BATCH)
->execute();
$count = 0;
foreach ($results as $pmnote) {
$count++;
$sandbox['pmnote_current_vid_of_field_being_migrated'] = $pmnote->vid;
try {
_pmnote_migrate_migrate_single_node($pmnote->nid, $pmnote->vid, $pmnote);
} catch (Exception $exc) {
watchdog('pmnote', 'See ' . __FUNCTION__ . '() ' . $exc
->getTraceAsString(), NULL, WATCHDOG_ERROR);
}
}
return empty($count);
}
function _pmnote_migrate_migrate_single_node($nid, $vid, $note) {
$node = node_load($nid, $vid);
if ($note->task_nid) {
$node->pmnote_parent[LANGUAGE_NONE][0]['target_id'] = $note->task_nid;
}
elseif ($note->project_nid) {
$node->pmnote_parent[LANGUAGE_NONE][0]['target_id'] = $note->project_nid;
}
elseif ($note->organization_nid) {
$node->pmnote_parent[LANGUAGE_NONE][0]['target_id'] = $note->organization_nid;
}
node_save($node);
return TRUE;
}
function _pmnote_get_drupal_uid_from_pmperson_nid($nid) {
$r = db_select('pmperson', 'p')
->fields('p')
->condition('p.nid', $nid)
->execute()
->fetchAssoc();
$uid = empty($r) ? FALSE : $r->user_uid;
return $uid;
}