You are here

function _quotes_parse_import in Quotes 6

Same name and namespace in other branches
  1. 5 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.

4 calls to _quotes_parse_import()
quotes_insert in ./quotes.module
Implementation of hook_insert().
quotes_validate in ./quotes.node.inc
Implementation of hook_validate().
quotes_validate in ./quotes.module
Implementation of hook_validate().
quotes_view in ./quotes.module
Implementation of hook_view().

File

./quotes.module, line 1603
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_parse_import($node, $set_errors = FALSE) {
  $quotes = array();

  // This lets us dump a % if it's on the end of the author.
  $trim_chars = " \t\n\r\0\v%";
  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. "@found"', array(
            '!num' => count($quotes) + 1,
            '@found' => $quote[0],
          )));
        }
        break;
      }
      if (!isset($quote[1])) {
        $quote[1] = '';
      }
      if (!isset($quote[2])) {
        $quote[2] = '';
      }

      // Check for length of author
      if (drupal_strlen($quote[1]) > 254) {
        form_set_error('body', t('Parse error on quote !num. "@found"', array(
          '!num' => count($quotes) + 1,
          '@found' => $quote[0],
        )));
      }
      else {
        $quotes[] = (object) array(
          'body' => trim(check_markup($quote[0])),
          'quotes_author' => trim(check_plain($quote[1])),
          'quotes_citation' => trim(check_markup($quote[2])),
          'format' => $node->format,
        );
      }
    }
  }
  elseif ($node->quotes_format == 'fortune') {
    foreach (preg_split('<\\n+%+\\n+>', str_replace("\t", '    ', preg_replace('<(?:\\r\\n?|\\n)>', "\n", $node->body))) as $this_quote) {
      if (preg_match('<^(?:(?:(.*)\\n+\\s*--\\s*(.*?)))$>s', $this_quote, $matches)) {
        $aquote = explode("--", $matches[0]);
        if (!isset($aquote[1])) {
          $aquote[1] = '';
        }
        if (!isset($aquote[2])) {
          $aquote[2] = '';
        }
        if (drupal_strlen($aquote[1]) > 254) {
          form_set_error('body', t('Parse error on quote !num. "@found"', array(
            '!num' => count($quotes) + 1,
            '@found' => $aquote[0],
          )));
        }
        else {
          $quotes[] = (object) array(
            'body' => trim(check_markup($aquote[0])),
            'quotes_author' => trim(check_plain($aquote[1])),
            'quotes_citation' => trim(check_markup($aquote[2])),
            'format' => $node->format,
          );
        }
      }
    }
  }
  elseif ($set_errors) {
    form_set_error('quotes_format', t('Please select a valid import format. (!format)', array(
      '!format' => $node->quotes_format,
    )));
  }
  return $quotes;
}