You are here

function apachesolr_multilingual_textfile_nodeapi in Apache Solr Multilingual 6

Same name and namespace in other branches
  1. 6.2 apachesolr_multilingual_textfile/apachesolr_multilingual_textfile.module \apachesolr_multilingual_textfile_nodeapi()

File

apachesolr_multilingual_textfile/apachesolr_multilingual_textfile.module, line 279

Code

function apachesolr_multilingual_textfile_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  if ('apachesolr_multilingual_textfile' == $node->type) {
    switch ($op) {
      case 'load':
        $node->filename = db_result(db_query("SELECT filename FROM {apachesolr_multilingual_textfiles} WHERE (nid = %d) and (vid = %d)", $node->nid, $node->vid));
        break;
      case 'presave':

        // prohibit publishing
        $node->status = 0;

        // create fix title
        $active_languages = locale_language_list();
        $node->title = $active_languages[$node->language] . ' / ' . $node->filename;
        break;
      case 'insert':
        $item = array(
          'nid' => $node->nid,
          'vid' => $node->vid,
          'filename' => $node->filename,
        );
        drupal_write_record('apachesolr_multilingual_textfiles', $item);
        break;
      case 'delete':

        // delete complete node therefore no revision control
        db_query("DELETE FROM {apachesolr_multilingual_textfiles} WHERE nid = %d", $node->nid);
        break;
      case 'update':
        $item = array(
          'nid' => $node->nid,
          'vid' => $node->vid,
          'filename' => $node->filename,
        );
        if ($node->revision) {
          drupal_write_record('apachesolr_multilingual_textfiles', $item);
        }
        else {
          drupal_write_record('apachesolr_multilingual_textfiles', $item, array(
            'nid',
            'vid',
          ));
        }
        break;
      case 'validate':
        if (!$node->language) {
          form_set_error('language', t('choose a language'));
          return;
        }
        if (!$node->filename) {
          form_set_error('filename', t('choose a filename'));
          return;
        }
        $nid = db_result(db_query("SELECT nid FROM {apachesolr_multilingual_textfiles} JOIN {node} USING (nid) WHERE language='%s' AND filename='%s'", $node->language, $node->filename));
        if ($nid && $nid != $node->nid) {
          $active_languages = locale_language_list();
          $title = $active_languages[$node->language] . ' / ' . $node->filename;
          form_set_error('title', t('node already exists: %title', array(
            '%title' => $title,
          )));
        }
        $error = apachesolr_multilingual_validate_solr_textfile($node->filename, $node->body);
        if ($error) {
          form_set_error('body', t('syntax error in apache solr textfile: %filename - %error', array(
            '%filename' => $node->filename,
            '%error' => $error,
          )));
        }
        break;
    }
  }
}