You are here

function quotes_import in Quotes 7

Inserts quotes data into db tables and handles %id variable in quotes title.

Parameters

array $node: As an import $node->quotes_import_format defines what type of import is required, either tab delimited or fortune format. The body of the node is then parsed by calling _quotes_pasre_import which splits the body as needed returning the data to import.

If called from quotes_insert() then this function serves to save a single quote to the database.

This function also renames the title if set to %id to the new nodes nid value.

Upon completion, user is returned to the quotes page on imports.

2 calls to quotes_import()
quotes_insert in ./quotes.module
Implements hook_insert().
quotes_node_presave in ./quotes.module
Form submit presave handler for importing quotes.

File

./quotes.module, line 511
The quotes module allows users to maintain a list of quotes that can be displayed in any number of administrator-defined quote blocks.

Code

function quotes_import($node) {

  // Is it a single node or an import?
  switch ($node->quotes_import_format) {
    case 'text':
    case 'fortune':
      $quotes_found = _quotes_parse_import($node, FALSE);
      foreach ($quotes_found as $count => $quote) {

        // Create a new node object.
        $newnode = (object) NULL;
        $newnode->type = "quotes";
        $newnode->title = $node->title;
        $newnode->uid = $node->uid;
        $newnode->created = strtotime("now");
        $newnode->changed = strtotime("now");
        $newnode->status = 1;
        $newnode->comment = 1;
        $newnode->promote = 1;
        $newnode->moderate = 0;
        $newnode->sticky = 0;
        $newnode->is_new = 1;
        $newnode->quotes_import_format = 'single';
        $newnode->body[$node->language][0]['value'] = filter_xss($quote->body);
        if (!isset($newnode->body[$node->language][0]['safe_value'])) {
          $newnode->body[$node->language][0]['safe_value'] = check_markup($quote->body);
        }

        // Create the teaser if required.
        $teaser = text_summary($quote->body, isset($quote->format) ? $quote->format : filter_format_default());

        // We do not want a teaser or summary in a quote usually.
        // $node->body[$node->language][0]['summary'] = $teaser;
        $newnode->body[$node->language][0]['format'] = $quote->format;
        $newnode->quotes_author = $quote->quotes_author;
        $newnode->quotes_citation = $quote->quotes_citation;
        if (isset($node->quotes_promote)) {
          $newnode->quotes_promote = $node->quotes_promote;
        }
        else {
          $newnode->quotes_promote = 0;
        }

        // Write the imported quote.
        node_save($newnode);
        $node == $newnode;
        continue;
      }

      // Done with the import, return directly to quotes page.
      drupal_set_message(t('@count quotes imported.', array(
        '@count' => $count + 1,
      )));
      drupal_goto('quotes');
      break;
    case 'single':

      // Save the author, citation, and promote values into our table.
      $aid = _quotes_handle_author($node->quotes_author);
      db_insert('quotes')
        ->fields(array(
        'nid' => (int) $node->nid,
        'vid' => (int) $node->vid,
        'aid' => (int) $aid,
        'citation' => $node->quotes_citation,
        'promote' => (int) $node->quotes_promote,
      ))
        ->execute();

      // Replace %id  title variable with nid.
      if (strpos($node->title, '%id') !== FALSE) {
        $node->title = str_replace('%id', $node->nid, $node->title);
        db_update('node')
          ->fields(array(
          'title' => $node->title,
        ))
          ->condition('vid', $node->vid)
          ->execute();
        db_update('node_revision')
          ->fields(array(
          'title' => $node->title,
        ))
          ->condition('vid', $node->vid)
          ->execute();
      }
  }
}