You are here

function community_tags_node_community_tags_tags_removed in Community Tags 6.2

Implementation of hook_community_tags_tags_removed(). Don't process if tags being removed because node is being deleted or term is being deleted as taxonomy module will take care of deleting node terms.

Apply node term synchronisation of removed tags. Will handle any set of ctags for any combination of nodes, users, and terms.

@todo - add options to syncronisation - e.g. if role x untags then rmove from node terms

File

community_tags_node/community_tags_node.module, line 202
community_tags_node.module

Code

function community_tags_node_community_tags_tags_removed($node, $user, $terms, $source, $removed_ctags) {

  // only process if this module processing flag and not a node or term delete
  if (!_community_tags_node_is_processing() && !($source == 'node:delete' || $source == 'term:delete')) {

    // remove node terms if last tag being deleted
    // group by node so we only need to save each node once.
    $ctags_by_node = community_tags_tags_group_by($removed_ctags, 'nid');
    foreach ($ctags_by_node as $nid => $ctags_for_node) {
      $node_save_required = FALSE;
      $node_to_save = _community_tags_get_node($nid);

      // now group by vid as logic depends on combination of vid and node type
      $ctags_for_node_by_vid = community_tags_tags_group_by($ctags_for_node, 'vid');
      foreach ($ctags_for_node_by_vid as $vid => $ctags_for_node_and_vid) {
        $first_ctag_for_node_and_vid = reset($ctags_for_node_and_vid);
        if (_community_tags_node_is_opmode(COMMUNITY_TAGS_NODE_OPMODE_SYNC_CT_TO_NT, $vid, $node_to_save->type)) {

          // logic is - for each term if the number of tags being removed is equal to the tag count - then remove
          // but it could be more complicated than that...
          // so group by tid
          $ctags_for_node_and_vid_by_tid = community_tags_tags_group_by($ctags_for_node_and_vid, 'tid');
          foreach ($ctags_for_node_and_vid_by_tid as $tid => $ctags_for_node_and_vid_and_tid) {

            // $ctags_for_node_and_vid_and_tid is the set of tags for this node and term for all users
            $first_ctag_for_node_and_vid_and_tid = reset($ctags_for_node_and_vid_and_tid);
            if (count($ctags_for_node_and_vid_and_tid) == $first_ctag_for_node_and_vid_and_tid->count && isset($node_to_save->taxonomy[$tid])) {

              // i.e. all tags for this term are being removed for the node - therefore delete the node term
              unset($node_to_save->taxonomy[$tid]);
              $node_save_required = TRUE;
            }
          }
        }
      }

      // if node terms were removed from this node then save.
      if ($node_save_required) {

        // setting this will prevent full CT node update processing
        $node_to_save->community_tags_op = TRUE;

        // invoke full node save pipeline - term nodes will be updated, and good stuff like search (including the Apache SOLR Integration module) will know about it.
        _community_tags_node_save($node_to_save);
      }
    }
  }
}