You are here

function glossify_nodeapi in Glossify 6.3

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

Implementation of hook_nodeapi().

File

./glossify.module, line 173

Code

function glossify_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'delete':

      // omerida, 9/30/2010: I think it'd be quicker to
      // remove from the db directly (DELETE FROM {glossify} where nid=%d
      // but I figured it'd be safer to stick with the module's api
      // and use _keyword_table function.
      $configurations = variable_get('glossify_configurations', array());
      foreach ($configurations as $config_name => $configuration) {
        if (isset($configuration['to'][$node->type])) {
          $enabled_styles = array_filter($configuration['style']);
          $methods = $configuration['methods'];
          if (isset($methods['use_title'])) {
            _keyword_table('delete', $node->nid, 'title', $config_name, $node->language);
          }
          if (isset($methods['use_internal'])) {
            _keyword_table('delete', $node->nid, 'internal', $config_name, $node->language);
          }
          if (isset($methods['use_cck'])) {
            _keyword_table('delete', $node->nid, 'cck', $config_name, $node->language);
          }
          if (isset($methods['use_taxonomy'])) {
            _keyword_table('delete', $node->nid, 'taxonomy', $config_name, $node->language);
          }
        }
      }
      break;
    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 $config_name => $configuration) {
        if (in_array($node->type, $configuration['to'])) {
          if ($configuration['methods']['use_title']) {
            _keyword_table('insert', $node->nid, 'title', $config_name, $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', $config_name, $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, 'cck', $config_name, $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, 'taxonomy', $config_name, $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' AND configuration = '%s'", $node->nid, 'title', $config_name))) {
              _keyword_table('insert', $node->nid, 'title', $config_name, $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' AND configuration = '%s'", $node->nid, 'title', $node->title, $config_name))) {
              $keywords = array_merge($keywords, _keyword_table('update', $node->nid, 'title', $config_name, $node->language, $node->title));
              $keywords[$node->title] = $node->title;
            }
          }
          if ($configuration['methods']['use_internal']) {
            $keywords = array_merge($keywords, _fetch_affected_keywords($node, 'internal', $config_name));
          }
          if ($configuration['methods']['use_cck']) {
            $keywords = array_merge($keywords, _fetch_affected_keywords($node, 'cck', $config_name, $configuration['methods']['keyword_field'], $configuration['methods']['override_field']));
          }
          if ($configuration['methods']['use_taxonomy']) {
            $keywords = array_merge($keywords, _fetch_affected_keywords($node, 'taxonomy', $config_name, $configuration['methods']['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'])) {
          $enabled_styles = array_filter($configuration['style']);
          $possible_keywords = _fetch_possible_keywords($config_name, $configuration, $node->nid);
          if (in_array('reference', $enabled_styles)) {
            foreach ($possible_keywords as $kwd) {
              list($term_title, $path) = $kwd;
              if (preg_match(_glossify_regex($configuration, $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', $enabled_styles)) {
            foreach ($possible_keywords as $kwd) {
              list($term_title, $path) = $kwd;
              if (preg_match(_glossify_regex($configuration, $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;
                $node->content['glossify_all_hovertips']['#value'] .= $node->content['glossify_hovertip'][$term_title]['#value'];
              }
            }
          }
        }
      }
      if ($term_definition_list !== '') {
        $node->content['glossify_reference'] = array(
          '#weight' => '10',
          '#value' => theme('glossify_reference_section', $term_definition_list),
        );
      }

      // Process node body if set to 'non-filter' mode. Currently the $node->format is irrelevant.
      $glossify_mode = variable_get('glossify_process_mode', GLOSSIFY_USE_FILTER);
      if ($glossify_mode != GLOSSIFY_USE_FILTER) {
        $node->content['body']['#value'] = glossify_filter('process', 0, $node->format, $node->content['body']['#value']);
      }
      break;
  }
}