You are here

function taxonomy_deploy_deploy_check_cleanup in Deploy - Content Staging 6

Implementation of hook_deploy_check_cleanup().

After dependencies are sorted out, but before items get pushed, we need to arrange the weights for our taxonomy terms such that parent terms are always pushed out before child terms. This is more difficult than one might think. I originally intended on having every term add itself as min(weight) - 1, but this doesn't always work out. For instance, a term will add its parent at a lower weight, but then a later term with the same parent might add itself at an even lower weight. What we need is to get all the terms and their dependent terms added, and then sort the weighting out. This is what we do here.

1 call to taxonomy_deploy_deploy_check_cleanup()
taxonomy_vocabulary_deploy_add_form_submit in modules/taxonomy_deploy/taxonomy_deploy.pages.inc
Submit handler for taxonomy_vocabulary_deploy_add_form().

File

modules/taxonomy_deploy/taxonomy_deploy.module, line 269
Deployment API which enables modules to deploy items between servers.

Code

function taxonomy_deploy_deploy_check_cleanup($pid) {
  if (db_result(db_query("SELECT DISTINCT module FROM {deploy_plan_items} WHERE pid = %d AND module = 'taxonomy_term'", $pid))) {

    // All that matters is that within a given vocabulary, all items are weighted
    // such that the parents are of a lower weight than the children. The actual
    // weights don't matter, and they don't need to be unique. This is the fastest
    // thing I could figure out to meet that need.
    // Get all the vids we are deploying terms for/
    $vocabularies = db_query("SELECT DISTINCT vid FROM {term_data} td INNER JOIN {deploy_plan_items} dpi ON td.tid = dpi.data WHERE dpi.module = 'taxonomy_term'");
    while ($vid = db_result($vocabularies)) {

      // Get the tree for this vid, which is a sorted array of taxonomy term objects.
      $tree = taxonomy_get_tree($vid);

      // In order for this to be useful to us we strip it down to just an array
      // of tids, still properly sorted. This involves re-iterating the tree but
      // I don't see what to do about that.
      foreach ($tree as &$value) {
        $value = $value->tid;
      }

      // Now get all the tids we're deploying from this vocabulary, and get them in
      // another array.
      $items = array();
      $tids = db_query("SELECT dpi.data FROM {deploy_plan_items} dpi INNER JOIN {term_data} td ON dpi.data = td.tid WHERE dpi.module = 'taxonomy_term' AND td.vid = %d", $vid);
      while ($item = db_result($tids)) {
        $items[] = $item;
      }

      // What we have now are two arrays - one sorted array of all tids in a vocabulary, and one
      // unsorted array of all tids in our plan. what we want is one sorted array of all tids in
      // the plan. Thank you array_intersect(). NOTE: the order of the parameters here matters
      // immensely.
      $items = array_intersect($tree, $items);

      // Array_intersect() maintains the original array's keys, which we use as our
      // weights. Again, I would love to know a better way to update these then running
      // an update query on them all individually.
      foreach ($items as $key => $item) {
        db_query("UPDATE {deploy_plan_items} SET weight = %d WHERE data = %d and module = 'taxonomy_term'", $key, $item);
      }
    }
  }
}