You are here

function _quotes_parse_import in Quotes 7

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

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

Parameters

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

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

Return value

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

3 calls to _quotes_parse_import()
quotes_import in ./quotes.module
Inserts quotes data into db tables and handles %id variable in quotes title.
quotes_validate in ./quotes.node.inc
Implements hook_validate().
quotes_validate in ./quotes.module
Implements hook_validate().

File

./quotes.module, line 2237
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_import_format == 'text') {

    // The documentation shows '<tab>' and some users have actually used
    // that string, so let's allow it.
    $quote_import = str_replace("<tab>", "\t", $node->body[$node->language][0]['value']);
    foreach (explode("\r", str_replace("\\\r", "\n", preg_replace('<(?:\\r\\n?|\\n)>', "\r", trim($quote_import)))) 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] = NULL;
      }
      if (!isset($quote[2])) {
        $quote[2] = NULL;
      }

      // 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($quote[0]),
          'quotes_author' => trim(check_plain($quote[1])),
          'quotes_citation' => trim(check_markup($quote[2])),
          'format' => $node->body[$node->language][0]['format'],
        );
      }
    }
  }
  elseif ($node->quotes_import_format == 'fortune') {
    foreach (preg_split('<\\n+%+\\n+>', str_replace("\t", '    ', preg_replace('<(?:\\r\\n?|\\n)>', "\n", $node->body[$node->language][0]['value']))) as $quote) {
      if (preg_match('<^(?:(?:(.*)\\n+\\s*--\\s*(.*?)))$>s', $quote, $matches)) {
        $aquote = explode("--", $matches[0]);
        if (!isset($aquote[1])) {
          $aquote[1] = NULL;
        }
        if (!isset($aquote[2])) {
          $aquote[2] = NULL;
        }
        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($aquote[0]),
            'quotes_author' => trim(check_plain($aquote[1])),
            'quotes_citation' => trim(check_markup($aquote[2])),
            'format' => $node->body[$node->language][0]['format'],
          );
        }
      }
    }
  }
  elseif ($set_errors) {
    form_set_error('quotes_import_format', t('Please select a valid import format. (!format)', array(
      '!format' => $node->quotes_import_format,
    )));
  }
  return $quotes;
}