You are here

function hashtags_entity_presave in Hashtags 7

Same name and namespace in other branches
  1. 8 hashtags.module \hashtags_entity_presave()

Implementation of hook_entity_presave().

File

./hashtags.module, line 99

Code

function hashtags_entity_presave($entity, $entity_type) {
  if ($entity_type == 'comment') {
    return;
  }

  // hashtags generation proccess can be prevented if define
  // hashtags_ignore property
  if (isset($entity->hashtags_ignore) && $entity->hashtags_ignore) {
    return;
  }
  $field_name = variable_get('hashtags_terms_field', 'field_hashtags');
  $vid = variable_get('hashtags_vocabulary', 3);
  $wrapper = entity_metadata_wrapper($entity_type, $entity);
  $bundle = $wrapper
    ->getBundle();

  // Check if field_hashtags field exists
  // for current entity
  if (!_hashtags_is_field_exists($field_name, $entity_type, $bundle)) {
    return;
  }
  $entity_id = $wrapper
    ->getIdentifier();

  // for new entity - setup entity_id = 0 and save zero
  // value to hashtags_index table; and then update all
  // rows with entity_id = 0  inside hook_entity_insert()
  // with generated entity_id because we don't have access
  // to entity_id from hook_entity_presave()
  $entity_id = empty($entity_id) ? 0 : $entity_id;

  // 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($wrapper, $entity_type, $bundle);

  // 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_entity_tags($entity_id, $entity_type);

  //_hashtags_get_prev_terms($wrapper);

  // 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);
  }
  if ($entity_id) {

    // 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);

      // 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($wrapper, $entity_type);
}