View source
<?php
$plugin = array(
'name' => t('Group visibility'),
'description' => t('Upgrade group visibility (e.g. open, moderated, closed) from Organic groups 6.'),
'access callback' => 'og_migrate_7000_access',
'migrate callback' => 'og_ui_og_migrate_upgrade_group_visibility',
'dependencies' => array(
'upgrade_group',
),
);
function og_ui_og_migrate_upgrade_group_visibility(&$context) {
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['last'] = 0;
$context['sandbox']['last_vid'] = 0;
$context['sandbox']['max'] = 0;
$group_types = array();
foreach (node_type_get_types() as $type) {
if (og_is_group_type('node', $type->type)) {
$group_types[] = $type->type;
og_create_field(OG_DEFAULT_ACCESS_FIELD, 'node', $type->type);
}
}
if ($group_types) {
$query = db_select('d6_og', 'og');
$context['sandbox']['max'] = $query
->countQuery()
->execute()
->fetchField();
}
if (!$context['sandbox']['max']) {
$context['finished'] = 1;
return;
}
}
$batch_size = variable_get('og_batch_size', 200);
$query = db_select('node_revision', 'nr');
$query
->innerJoin('node', 'n', 'n.vid = nr.vid');
$query
->innerJoin('d6_og', 'og', 'n.nid = og.nid');
$query
->fields('nr', array(
'nid',
'vid',
))
->fields('n', array(
'type',
'created',
))
->fields('og', array(
'og_selective',
'upgrade_id',
))
->condition('nr.vid', $context['sandbox']['last_vid'], '>')
->condition('og.upgrade_id', $context['sandbox']['last'], '>')
->orderBy('nr.vid', 'ASC')
->range(0, $batch_size);
$revisions = $query
->execute();
$nids = array();
foreach ($revisions as $revision) {
$node = (object) array(
'nid' => $revision->nid,
'vid' => $revision->vid,
'type' => $revision->type,
);
$nids[$revision->nid] = $revision->og_selective;
$node->{OG_DEFAULT_ACCESS_FIELD}[LANGUAGE_NONE][0]['value'] = 1;
field_attach_update('node', $node);
$context['sandbox']['last_vid'] = $revision->vid;
$context['sandbox']['last'] = $revision->upgrade_id;
$context['sandbox']['progress']++;
}
if ($nids) {
foreach ($nids as $nid => $state) {
if ($group = og_get_group('node', $nid)) {
$anon_permissions = array();
$auth_permissions = array(
'unsubscribe' => TRUE,
);
switch ($state) {
case 0:
$anon_permissions = array(
'subscribe' => FALSE,
'subscribe without approval' => TRUE,
);
break;
case 1:
$anon_permissions = array(
'subscribe' => TRUE,
'subscribe without approval' => FALSE,
);
break;
case 2:
$anon_permissions = array(
'subscribe' => FALSE,
'subscribe without approval' => FALSE,
);
break;
case 3:
$anon_permissions = array(
'subscribe' => FALSE,
'subscribe without approval' => FALSE,
);
$auth_permissions = array(
'unsubscribe' => FALSE,
);
break;
}
$roles = og_roles_override($group->gid);
$roles = array_flip($roles);
og_role_change_permissions($roles[OG_ANONYMOUS_ROLE], $anon_permissions);
og_role_change_permissions($roles[OG_AUTHENTICATED_ROLE], $auth_permissions);
}
}
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}