You are here

function quotes_insert in Quotes 6

Same name and namespace in other branches
  1. 5 quotes.module \quotes_insert()
  2. 7 quotes.module \quotes_insert()

Implementation of hook_insert().

1 call to quotes_insert()
quotes_update in ./quotes.module
Implementation of hook_update().

File

./quotes.module, line 386
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_insert($node) {

  // This inserts the quote-specific information into the quotes
  // tables and also handles the %id variable in the node title.
  switch ($node->quotes_format) {
    case 'text':
    case 'fortune':
      $quotes_found = _quotes_parse_import($node, FALSE);
      foreach ($quotes_found as $count => $quote) {
        $teaser = node_teaser($quote->body, isset($quote->format) ? $quote->format : NULL);
        if ($count == 0) {

          // We'll set the base node correctly from the first quote.
          $first = drupal_clone($node);
          $node->quotes_format = $first->quotes_format = 'single';
          $node->body = $first->body = $quote->body;
          $node->teaser = $first->teaser = $teaser;
          $node->quotes_author = $first->quotes_author = $quote->quotes_author;
          $node->quotes_citation = $first->quotes_citation = $quote->quotes_citation;

          // Write back the updated info.
          node_save($node);

          // When we end this function, the corrected information will be saved for us.
          continue;
        }
        else {

          // It's not the first one, so we will make a copy and update the information.
          // Then we'll save the temporary node.
          $temp = drupal_clone($first);
          $temp->quotes_format = 'single';

          // Make node_save create a new nid.
          unset($temp->nid, $temp->vid, $temp->created);
          $temp->body = $quote->body;
          $temp->teaser = $teaser;
          $temp->quotes_author = $quote->quotes_author;
          $temp->quotes_citation = $quote->quotes_citation;

          // Okay, let's get it saved.
          node_save($temp);
        }
      }

      // End foreach.
      // Note that we don't "break" so the first quote falls into saving the extra stuff.
      drupal_set_message(check_plain(t('!count quotes imported.', array(
        '!count' => $count + 1,
      ))));
    case 'single':

      // Save the author, citation, and promote values into our table.
      $aid = _quotes_handle_author($node->quotes_author);
      db_query("INSERT INTO {quotes} (nid, vid, aid, citation, promote) VALUES (%d, %d, '%d', '%s', %d)", $node->nid, $node->vid, $aid, $node->quotes_citation, $node->quotes_promote);

      // replace %id variable in title
      if (strpos($node->title, '%id') !== FALSE) {
        $node->title = str_replace('%id', $node->nid, $node->title);
        db_query("UPDATE {node} SET title = '%s' WHERE vid = %d", $node->title, $node->vid);
        db_query("UPDATE {node_revisions} SET title = '%s' WHERE vid = %d", $node->title, $node->vid);
      }
  }

  // End switch.
}