You are here

function hashtags_node_presave in Hashtags 7.2

Implementation of hook_node_presave().

File

./hashtags.module, line 68

Code

function hashtags_node_presave($node) {
  $vid = variable_get('hashtags_vocabulary', '');
  $voc = taxonomy_vocabulary_load($vid);

  // bug http://drupal.org/node/1363538
  $hashtags = array();
  if (!empty($node->body['und'][0]['value'])) {
    $hashtags = hashtags_get_tags($node->body['und'][0]['value']);
  }
  $hashtags = hashtags_get_tags($node->body['und'][0]['value']);
  $field_name = variable_get('hashtags_terms_field', '');

  // we have 3 types of hashtags in Body field:
  // New,
  // Removed,
  // Updated - not touched
  // Field contains tags and hashtags - we should synhronize Body and Field hashtags
  $all_hashtags_in_body = array();
  $new_hashtags_in_body = array();
  $removed_hashtags_in_body = array();
  $steady_hashtags_in_body = array();
  $all_hashtags_names_in_field = array();

  // fill up all arrays with data
  if (isset($node->{$field_name}['und']) && sizeof($node->{$field_name}['und'])) {
    foreach ($node->{$field_name}['und'] as $tag) {

      //foreach($node->field_tags['und'] as $tag) {
      if ($tag['name'][0] == '#') {
        $all_hashtags_in_body[$tag['name']] = $tag['tid'];
        $all_hashtags_names_in_field[$tag['name']] = $tag['name'];
      }
    }
  }

  // define what hashtags should to be added/removed for this node
  foreach ($hashtags as $name) {

    // this tag belongs to this node ?
    if (in_array($name, $all_hashtags_names_in_field)) {
      $steady_hashtags_in_body[$name] = $all_hashtags_in_body[$name];
      unset($all_hashtags_in_body[$name], $all_hashtags_names_in_field[$name]);
    }
    else {
      $new_hashtags_in_body[$name] = $name;
    }
  }

  // Removed hashtags
  $removed_hashtags_in_body = array_values($all_hashtags_in_body);

  //watchdog('new:', '<pre>'.print_r($new_hashtags_in_body, TRUE).'</pre>');

  //watchdog('upd:', '<pre>'.print_r($steady_hashtags_in_body, TRUE).'</pre>');

  //watchdog('del:', '<pre>'.print_r($removed_hashtags_in_body, TRUE).'</pre>');
  $tags_on_remove = array();
  $i = 0;

  // clear all hashtags from Tags list
  if (isset($node->{$field_name}['und']) && sizeof($node->{$field_name}['und'])) {
    foreach ($node->{$field_name}['und'] as $tag) {
      if ($tag['name'][0] == '#') {
        $tags_on_remove[] = $i;
      }
      ++$i;
    }
    foreach ($tags_on_remove as $tag_index) {
      unset($node->{$field_name}['und'][$tag_index]);
    }
    $node->{$field_name}['und'] = array_values($node->{$field_name}['und']);
  }
  $hashtag_instance = array();
  $hashtag_instance['vid'] = $vid;

  // fill up Tags list with hashtags
  foreach ($hashtags as $name) {
    $tt = taxonomy_get_term_by_name($name);
    if (in_array($name, $new_hashtags_in_body)) {

      // if this is a new hashtag - we need to create it in db
      if ($tt == array()) {
        $edit = array(
          'name' => t($name),
          'description' => '',
          'parent' => array(
            0,
          ),
          'vid' => $vid,
        );
        $term = (object) $edit;
        taxonomy_term_save($term);
      }
      $tt = taxonomy_get_term_by_name($name);
      $tid = key($tt);
    }
    else {
      $tid = $steady_hashtags_in_body[$name];
    }
    $instance = array();
    $instance['tid'] = $tt[$tid]->tid;
    $instance['vid'] = $tt[$tid]->vid;
    $instance['name'] = $tt[$tid]->name;
    $instance['vocabulary_machine_name'] = $tt[$tid]->vocabulary_machine_name;
    if (isset($tt[$tid]->rdf_mapping)) {
      $instance['rdf_mapping'] = $tt[$tid]->rdf_mapping;
    }
    $node->{$field_name}['und'][] = $instance;
  }
}