You are here

oa_discussion.install in Open Atrium Discussion 7.2

File

oa_discussion.install
View source
<?php

/**
 * @file
 * oa_discussion.install
 */

/**
 * Implements hook_install().
 */
function oa_discussion_install() {
  oa_discussion_install_section_type();
}

/**
 * Create the Discussion Section taxonomy term.
 */
function oa_discussion_install_section_type() {
  $params = array(
    'description' => 'Allows users to create <em>Discussion posts</em> and displays a list of topics.',
    'node_options' => array(
      'oa_discussion_post',
    ),
    'layout' => 'node:oa_section:oa_section_discussion',
    'icon' => 'fa fa-comments',
  );
  oa_core_create_term('section_type', 'Discussion Section', $params);
}

/**
 * Update the Discussion section term.
 */
function oa_discussion_update_7002() {
  oa_discussion_install_section_type();
}

/**
 * Update discussion widgets for new pane settings.
 */
function oa_discussion_update_7103() {
  $result = db_query("SELECT pid, configuration FROM {panels_pane} where subtype = 'open_atrium_discussions-discussion_topics' and configuration not like '%og_group_ref_target_id%'");
  foreach ($result as $row) {
    $config = unserialize($row->configuration);
    $config['exposed']['og_group_ref_target_id'] = '';
    $config['exposed']['og_group_ref_target_id_mine'] = 0;
    $config['exposed']['og_subspaces_view_all'] = 1;
    $config['exposed']['og_subspaces_view_parent'] = 0;
    $config['exposed']['oa_section_ref_target_id'] = '';
    $config = serialize($config);
    db_update('panels_pane')
      ->fields(array(
      'configuration' => $config,
    ))
      ->condition('pid', $row->pid)
      ->execute();
  }
}

/**
 * Convert discussion replies to comments. This is batched and may take a while, and will run twice per reply.
 */
function oa_discussion_update_7104(&$sandbox) {

  // Only run if currently enabled.
  if (!module_exists('oa_discussion')) {
    return;
  }
  if (!isset($sandbox['progress'])) {
    cache_clear_all('ctools_plugin_files:', 'cache', TRUE);
    $enable = array(
      'oa_comment',
    );
    module_enable($enable);
    if (!module_exists('oa_comment')) {
      drupal_set_message(t('Unable to enable Open Atrium comments, please enable manually then rerun this update.'), 'error');
      return;
    }
    $sandbox['progress'] = 0;

    // We'll -1 to disregard the uid 0...
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT uid) FROM {users}')
      ->fetchField() - 1;
    $query = new EntityFieldQuery();
    $result = $query
      ->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'oa_discussion_post')
      ->fieldCondition('oa_parent', 'target_id', '0', '>')
      ->propertyOrderBy('created', 'ASC')
      ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT')
      ->count()
      ->execute();
    $sandbox['max'] = $result * 2;
    $sandbox['updated'] = array();
  }
  if (function_exists('oa_notification_skip')) {
    oa_notification_skip(TRUE);
  }
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'oa_discussion_post')
    ->fieldCondition('oa_parent', 'target_id', 'NULL', '!=')
    ->propertyOrderBy('created', 'ASC')
    ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT')
    ->range(0, 20);
  if ($sandbox['updated']) {
    $query
      ->entityCondition('entity_id', array_keys($sandbox['updated']), 'NOT IN');
  }
  $result = $query
    ->execute();
  $nids = !empty($result['node']) ? array_keys($result['node']) : array();
  $migrated = 0;
  $deleted = 0;
  for ($i = 0; $i < 20; $i++) {
    if ($nids && ($nid = array_shift($nids)) && ($node = node_load($nid))) {

      // Find the highest up reply that hasn't been processed so we can find
      // parent comment up correctly.
      $items = field_get_items('node', $node, 'oa_parent');
      $parent_node = NULL;
      $pid = 0;
      do {
        if ($parent_node && empty($sandbox['updated'][$parent_node->nid])) {
          $node = $parent_node;
        }
        elseif (!$pid && !empty($sandbox['updated'][$items[0]['target_id']])) {
          $pid = $sandbox['updated'][$items[0]['target_id']];
        }

        // Parent node will eventually end up being the discussion node.
        $parent_node = node_load($items[0]['target_id']);
      } while ($parent_node && ($items = field_get_items('node', $parent_node, 'oa_parent')) && !empty($items[0]['target_id']));

      // Enable comments on parent node.
      if (!$pid && $parent_node && $parent_node->comment != COMMENT_NODE_OPEN) {
        $parent_node->comment = COMMENT_NODE_OPEN;
        if (function_exists('oa_messages_skip')) {
          oa_messages_skip(TRUE);
        }
        node_save($parent_node);
        if (function_exists('oa_messages_skip')) {
          oa_messages_skip(FALSE);
        }
      }
      $comment = array(
        'status' => $node->status ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED,
        'name' => $node->name,
        'uid' => $node->uid,
        'comment_body' => $node->body,
        // Comment subject is short.
        'subject' => mb_substr($node->title, 0, 64),
        'nid' => $parent_node->nid,
        'pid' => $pid,
        'cid' => NULL,
        'created' => $node->created,
        'changed' => $node->changed,
      );

      // Preverse any same named fields as is.
      foreach (array_keys(field_info_instances('comment', 'comment_node_oa_discussion_post')) as $field_name) {
        if (!empty($node->{$field_name})) {
          $comment[$field_name] = $node->{$field_name};
        }
      }

      // Add attachments as paragraphs.
      if ($attachments = field_get_items('node', $node, 'field_oa_media')) {
        $item['entity'] = entity_create('paragraphs_item', array(
          'bundle' => 'paragraph_media',
          'field_name' => 'field_oa_related',
        ));
        $item['entity']->field_oa_media[LANGUAGE_NONE] = $attachments;
        $comment['field_oa_related'][LANGUAGE_NONE][] = $item;
      }
      $comment = (object) $comment;
      comment_save($comment);
      $migrated++;
      $sandbox['updated'][$node->nid] = $comment->cid;
    }
    elseif (!$nids && !empty($sandbox['updated'])) {
      $nid = key($sandbox['updated']);
      unset($sandbox['updated'][$nid]);
      if (function_exists('oa_messages_skip')) {
        oa_messages_skip(TRUE);
      }
      node_delete($nid);
      $deleted++;
    }
    else {
      break;
    }
  }
  if ($migrated && !$deleted) {
    drupal_set_message(t('Migrated @migrated replies (@count/@max)', array(
      '@migrated' => $migrated,
      '@count' => count($sandbox['updated']),
      '@max' => $sandbox['max'],
    )));
  }
  if ($deleted) {
    drupal_set_message(t('Deleted @deleted migrated replies (@count/@max)', array(
      '@deleted' => $deleted,
      '@count' => $sandbox['max'] - count($sandbox['updated']),
      '@max' => $sandbox['max'],
    )));
  }
  $sandbox['#finished'] = !$nids && empty($sandbox['updated']) ? 1 : (!$nids ? ($sandbox['max'] - count($sandbox['updated'])) / $sandbox['max'] : count($sandbox['updated']) / $sandbox['max']);

  // Delete the field no longer in use.
  if ($sandbox['#finished'] == 1) {
    field_delete_field('oa_parent');
  }
}

/**
 * Update discussion widgets to use new comment widgets.
 */
function oa_discussion_update_7105() {
  db_query("UPDATE {panels_pane} SET subtype = 'oa_comment_topics-comment_topics' WHERE subtype = 'open_atrium_discussions-discussion_topics'");
  db_query("UPDATE {panels_pane} SET subtype = 'oa_comment_topics-comment_user_topics_replied' WHERE subtype = 'open_atrium_discussions-discussion_user_reply'");
  db_query("UPDATE {panels_pane} SET subtype = 'oa_comment_topics-comment_user_topics' WHERE subtype = 'open_atrium_discussions-discussion_user'");
}

/**
 * Update icon to use font-awesome
 */
function oa_discussion_update_7106() {
  $term = taxonomy_get_term_by_name('Discussion Section', 'section_type');
  if ($term) {
    $term = current($term);
    if (!empty($term->field_oa_icon_class[LANGUAGE_NONE][0]['value'])) {
      if ($term->field_oa_icon_class[LANGUAGE_NONE][0]['value'] == 'icon-comments') {
        $term->field_oa_icon_class[LANGUAGE_NONE][0]['value'] = 'fa fa-comments';
        taxonomy_term_save($term);
      }
    }
  }
}

Functions

Namesort descending Description
oa_discussion_install Implements hook_install().
oa_discussion_install_section_type Create the Discussion Section taxonomy term.
oa_discussion_update_7002 Update the Discussion section term.
oa_discussion_update_7103 Update discussion widgets for new pane settings.
oa_discussion_update_7104 Convert discussion replies to comments. This is batched and may take a while, and will run twice per reply.
oa_discussion_update_7105 Update discussion widgets to use new comment widgets.
oa_discussion_update_7106 Update icon to use font-awesome