You are here

function hashtags_comment_presave in Hashtags 7

Implements hook_comment_presave().

File

./hashtags.module, line 223

Code

function hashtags_comment_presave($comment) {
  $field_name = variable_get('hashtags_terms_field', 'field_hashtags');
  $vid = variable_get('hashtags_vocabulary', 3);
  $entity_type = 'node';
  $entity = entity_load_single($entity_type, $comment->nid);
  $entity_wrapper = entity_metadata_wrapper($entity_type, $entity);
  $bundle = $entity_wrapper
    ->getBundle();

  // Check if field_hashtags field exists
  // for current entity
  if (!_hashtags_is_field_exists($field_name, $entity_type, $bundle)) {
    return;
  }
  $comment_wrapper = entity_metadata_wrapper('comment', $comment);

  // go through every textfield/textarea that marked
  // to use hashtags and put everything to $text string for
  // further taking tags from it
  $text = _hashtags_get_tracked_text($comment_wrapper, 'comment', $comment->node_type);
  $entity_id = $entity_wrapper
    ->getIdentifier();

  // for new comment - setup cid = 0 and save zero
  // value inside hashtags_index table; and update all
  // rows with cid = 0  inside hook_comment_insert()
  // with generated cid because we don't have access
  // to cid from hook_comment_presave()
  $comment_id = empty($comment->cid) ? 0 : $comment->cid;

  // go through every textfield/textarea that marked
  // to use hashtags and put everything to $text string for
  // further taking tags from it
  // this array is going to contain 'title' => 'tid'
  // format of terms that should be attached to entity
  // (some of previous and some of new ones)
  $current_terms = array();

  // get all tags titles
  $current_terms_titles = hashtags_parse_tags($text, FALSE);

  // get 'title' => 'tid' values of previous tags
  // (only works for existing entities)
  $prev_terms = _hashtags_index_get_comment_tags($comment_id, $entity_type);

  // take the titles of previous tags
  $prev_terms_titles = array_keys($prev_terms);

  // list of terms that should be checked and
  // added to database (except previous terms)
  $terms_to_add = array_diff($current_terms_titles, $prev_terms_titles);

  // go through the title list of terms that should be
  // added to database or taken if they are existed;
  // save data into $current_terms array
  foreach ($terms_to_add as $name) {

    // create/get existing term by name & vid
    $tid = _hashtags_add_term($name, $vid);

    // add row to index
    _hashtags_index_add($entity_id, $entity_type, $tid, $comment_id);
  }
  if ($comment_id != 0) {

    // list of terms that should be checked and
    // deleted from database
    $terms_to_delete = array_diff($prev_terms_titles, $current_terms_titles);

    // go through the title list of previous terms and check if
    // they should be deleted from database (if term has only
    // one attachment to hashtags_field)
    foreach ($terms_to_delete as $name) {
      $tid = $prev_terms[$name];

      // remove row from index
      $left_hashtags = _hashtags_index_remove($entity_id, $entity_type, $tid, $comment_id);

      // when term is last in vocabulary - remove it
      if (!$left_hashtags && _hashtags_is_last_term($tid, $field_name)) {
        taxonomy_term_delete($tid);
      }
    }
  }

  // refresh Hashtags field with data from
  // hashtags_index table
  _hashtags_index_sync_tags($entity_wrapper, $entity_type, TRUE);
}