You are here

function _quotes_parse_import in Quotes 5

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

Parses and returns the quotes contained in the provided node body.

Parameters

$node: The node object containing the data to be parsed.

$set_errors: The boolean indicating whether or not form errors should be set.

Return value

An array containing the parsed quotes as objects with properties body, quotes_author, quotes_citation, and format.

3 calls to _quotes_parse_import()
quotes_submit in ./quotes.module
Implementation of hook_submit().
quotes_validate in ./quotes.module
Implementation of hook_validate().
quotes_view in ./quotes.module
Implementation of hook_view().

File

./quotes.module, line 1528

Code

function _quotes_parse_import($node, $set_errors = FALSE) {
  $quotes = array();
  if ($node->quotes_format == 'text') {

    // The documentation shows '<tab>' and some users have actually used that string, so let's allow it.
    $node->body = str_replace('<tab>', "\t", $node->body);
    foreach (explode("\r", str_replace("\\\r", "\n", preg_replace('<(?:\\r\\n?|\\n)>', "\r", trim($node->body)))) as $quote) {
      $quote = explode("\t", $quote);
      if (count($quote) < 2 || !trim($quote[0])) {
        if ($set_errors) {
          form_set_error('body', t('Parse error on quote !num.', array(
            '!num' => count($quotes) + 1,
          )));
        }
        break;
      }
      $new_quote = drupal_clone($node);
      $new_quote->body = trim($quote[0]);
      $new_quote->quotes_author = $quote[1];
      $new_quote->quotes_citation = $quote[2];
      $quotes[] = $new_quote;
    }
  }
  elseif ($node->quotes_format == 'fortune') {
    foreach (preg_split('<\\n+%+\\n+>', str_replace("\t", '    ', preg_replace('<(?:\\r\\n?|\\n)>', "\n", $node->body))) as $quote) {
      if (preg_match('<^(?:(?:(.*)\\n+\\s*--\\s*(.*?)))$>s', $quote, $matches)) {
        if (!trim($matches[1])) {
          if ($set_errors) {
            form_set_error('body', t('Parse error on quote !num.', array(
              '!num' => count($quotes) + 1,
            )));
          }
          break;
        }
        $quotes[] = (object) array(
          'body' => trim($matches[1]),
          'quotes_author' => $matches[2],
          'quotes_citation' => NULL,
          'format' => $node->format,
        );
      }
      else {
        if (!trim($quote)) {
          if ($set_errors) {
            form_set_error('body', t('Parse error on quote !num.', array(
              '!num' => count($quotes) + 1,
            )));
          }
          break;
        }
        $quotes[] = (object) array(
          'body' => trim($quote),
          'format' => $node->format,
        );
      }
    }
  }
  elseif ($set_errors) {
    form_set_error('quotes_format', t('Please select a valid import format.'));
  }
  return $quotes;
}