You are here

function community_tags_delete_tags_batch_process in Community Tags 6.2

Same name and namespace in other branches
  1. 6 community_tags.batch.inc \community_tags_delete_tags_batch_process()
  2. 7 community_tags.batch.inc \community_tags_delete_tags_batch_process()

Batch process for deleting tags and optionally their corresponding node terms.

1 string reference to 'community_tags_delete_tags_batch_process'
community_tags_delete_all_form_submit in ./community_tags.admin.inc
Submit handler for community_tags_delete_all_form().

File

./community_tags.batch.inc, line 13
community_tags.batch.inc

Code

function community_tags_delete_tags_batch_process($vid, $content_type, $counts, $mode, &$context) {
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_node'] = 0;
    $context['sandbox']['max'] = $counts->node_count;
  }

  // get all the next lot of nodes to update. Note the range starts at 0 because the processed ones don't get returned by the query. duh.
  $result = db_query_range("SELECT n.nid, n.uid, count(distinct ct.tid) term_count, group_concat(ct.tid) tids, count(*) tag_count\n     FROM {community_tags} ct\n     INNER JOIN {term_data} td ON td.tid = ct.tid AND td.vid = %d\n     INNER JOIN {node} n ON n.nid = ct.nid AND n.type = '%s'\n     LEFT JOIN {term_node} tn ON tn.nid = ct.nid AND tn.tid = ct.tid\n     GROUP BY n.nid", $vid, $content_type, 0, 100);
  $initial_progress = $context['sandbox']['progress'];
  $source = $mode & COMMUNITY_TAGS_OPMODE_DELETE_ORPHAN_TERMS ? 'community_tags:purge:sync' : 'community_tags:purge:no_sync';
  while ($row = db_fetch_object($result)) {

    // get the tids from the query results
    $tids = explode(',', $row->tids);

    // delete community tags
    $tags = array();
    foreach ($tids as $tid) {
      $tags[$tid] = (object) array(
        'tid' => $tid,
        'vid' => $vid,
      );
    }
    $node = (object) array(
      'nid' => $row->nid,
      'type' => $content_type,
    );
    community_tags_remove_tags($node, NULL, $tags, $source);
    $context['sandbox']['progress'] += 1;
    $context['sandbox']['tag_count'] += $row->tag_count;
  }

  // Inform the batch engine that we are not finished,
  // and provide an estimation of the completion level we reached.
  // guard against not reaching the max!
  // nothing added - something changed whilst processing - finish early
  if ($context['sandbox']['progress'] != $context['sandbox']['max'] && $initial_progress != $context['sandbox']['progress']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
  else {
    $context['sandbox']['progress'] = $context['sandbox']['max'];
    $context['results'][] = t('Deleted %tag_count community tags from %node_count nodes.', array(
      '%tag_count' => $context['sandbox']['tag_count'],
      '%node_count' => $context['sandbox']['progress'],
    ));
  }
}