You are here

function glossify_nodeapi in Glossify 6

Same name and namespace in other branches
  1. 5 glossify.module \glossify_nodeapi()
  2. 6.3 glossify.module \glossify_nodeapi()

Implementation of hook_nodeapi().

File

./glossify.module, line 156

Code

function glossify_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'load':
      $glossify_keywords = implode(', ', _fetch_keywords($node->nid, 'internal'));
      $glossify_override = db_result(db_query('SELECT alternate FROM {glossify} WHERE nid = %d', $node->nid));
      return array(
        'glossify_keywords' => $glossify_keywords,
        'glossify_override' => $glossify_override,
      );
      break;
    case 'presave':
      $configurations = variable_get('glossify_configurations', array());
      $clear_filter_cache = array();
      foreach ($configurations as $config_name => $configuration) {
        $clear_filter_cache = in_array($node->type, array_merge($configuration['to'], $configuration['from']));
      }
      if ($clear_filter_cache) {
        cache_clear_all('*', 'cache_filter', TRUE);
      }
      break;
    case 'insert':
      $configurations = variable_get('glossify_configurations', array());
      $keywords = array();
      $affected_content_types = array();
      $html_body = str_get_html($node->body);
      $html_teaser = str_get_html($node->teaser);
      foreach ($configurations as $configuration) {
        if (in_array($node->type, $configuration['to'])) {
          if ($configuration['methods']['use_title']) {
            _keyword_table('insert', $node->nid, 'title', $node->language, $node->title);
            $keywords[$node->title] = $node->title;
          }
          if ($configuration['methods']['use_internal'] && !empty($node->glossify_keywords)) {
            foreach (array_map('trim', explode(',', $node->glossify_keywords)) as $keyword) {
              _keyword_table('insert', $node->nid, 'internal', $node->language, $keyword, $node->glossify_override);
              $keywords[$keyword] = $keyword;
            }
          }
          if ($configuration['methods']['use_cck']) {
            $fieldname = $configuration['methods']['keyword_field'];
            $override = $configuration['methods']['override_field'];
            foreach (array_map('trim', explode(',', $node->{$fieldname}[0]['value'])) as $keyword) {
              _keyword_table('insert', $node->nid, 'internal', $node->language, $keyword, $node->{$override}[0]['value']);
              $keywords[$keyword] = $keyword;
            }
          }
          if ($configuration['methods']['use_taxonomy']) {
            foreach (array_map('trim', explode(',', $node->taxonomy['tags'][$configuration['methods']['vocabulary']])) as $keyword) {
              _keyword_table('insert', $node->nid, 'internal', $node->language, $keyword);
              $keywords[$keyword] = $keyword;
            }
          }
          $affected_content_types = array_merge($affected_content_types, $configuration['from']);
        }
      }
      break;
    case 'update':
      $configurations = variable_get('glossify_configurations', array());
      $keywords = array();
      $affected_content_types = array();
      $html_body = str_get_html($node->body);
      $html_teaser = str_get_html($node->teaser);
      foreach ($configurations as $config_name => $configuration) {
        if (in_array($node->type, $configuration['to'])) {
          if ($configuration['methods']['use_title']) {
            if (!db_result(db_query("SELECT gtid FROM {glossify} WHERE nid = %d AND method = '%s'", $node->nid, 'title'))) {
              _keyword_table('insert', $node->nid, 'title', $node->language, $node->title);
              $keywords[$node->title] = $node->title;
            }
            elseif (!db_result(db_query("SELECT gtid FROM {glossify} WHERE nid = %d AND method = '%s' AND term = '%s'", $node->nid, 'title', $node->title))) {
              $keywords = array_merge($keywords, _keyword_table('update', $node->nid, 'title', $node->language, $node->title));
              $keywords[$node->title] = $node->title;
            }
          }
          if ($configuration['methods']['use_internal']) {
            $keywords = array_merge($keywords, _fetch_affected_keywords($node, 'internal'));
          }
          if ($configuration['methods']['use_cck']) {
            $keywords = array_merge($keywords, _fetch_affected_keywords($node, 'cck', $configuration['methods']['keyword_field'], $configuration['methods']['override_field']));
          }
          if ($configuration['methods']['use_taxonomy']) {
            foreach ($configuration['methods']['vocabulary'] as $vocabulary) {
              $keywords = array_merge($keywords, _fetch_affected_keywords($node, 'taxonomy', $vocabulary));
            }
          }
          $affected_content_types[$config_name] = array_merge($affected_content_types, $configuration['from']);
        }
      }
      break;
    case 'view':
      $configurations = variable_get('glossify_configurations', array());
      $term_definition_list = '';
      $referenced_terms = array();
      foreach ($configurations as $config_name => $configuration) {
        if (in_array($node->type, $configuration['from'])) {
          $possible_keywords = _fetch_possible_keywords($configuration, $node->nid);
          if (in_array('reference', $configuration['style'])) {
            foreach ($possible_keywords as $term_title => $path) {
              if ($configuration['break']) {
                if (preg_match('/\\b' . $term_title . '\\b/', $node->body)) {
                  if (!in_array($path, $referenced_terms)) {
                    $term_definition_list .= theme('glossify_term', $path, 'reference');
                    $referenced_terms[] = $path;
                  }
                }
              }
              else {
                if (preg_match('/' . $term_title . '/', $node->body)) {
                  if (!in_array($path, $referenced_terms)) {
                    $term_definition_list .= theme('glossify_term', $path, 'reference');
                    $referenced_terms[] = $path;
                  }
                }
              }
            }
          }
          if (in_array('hovertip', $configuration['style'])) {
            foreach ($possible_keywords as $term_title => $path) {
              if ($configuration['break']) {
                if (preg_match('/\\b' . $term_title . '\\b/', $node->body)) {
                  $node->content['glossify_hovertip'][$term_title] = array(
                    '#value' => theme('glossify_term', $path, 'hovertip'),
                    '#weight' => 10,
                  );
                  $node->content['glossify_hovertip']['#weight'] = 10;
                }
              }
              else {
                if (preg_match('/' . $term_title . '/', $node->body)) {
                  $node->content['glossify_hovertip'][$term_title] = array(
                    '#value' => theme('glossify_term', $path, 'hovertip'),
                    '#weight' => 10,
                  );
                  $node->content['glossify_hovertip']['#weight'] = 10;
                }
              }
            }
          }
        }
      }
      if ($term_definition_list !== '') {
        $node->content['glossify_reference'] = array(
          '#weight' => '10',
          '#value' => theme('glossify_reference_section', $term_definition_list),
        );
      }
      break;
  }
}