You are here

private function FeedsExcelParser::getItems in Feeds Excel 7

Same name and namespace in other branches
  1. 6 ExcelParser.inc \FeedsExcelParser::getItems()

Retrieve Items from Spredsheet.

Parameters

$sheet:

1 call to FeedsExcelParser::getItems()
FeedsExcelParser::parseItems in ./ExcelParser.inc
Parse items from the given reader source respecting processing limits.

File

./ExcelParser.inc, line 228

Class

FeedsExcelParser
Parses a given file as a Excel file.

Code

private function getItems($sheet) {
  $mode = $this->config['mapping_mode'];
  $items = array();

  // Only run non-empty sheets
  if (!empty($sheet['cells'])) {
    $max = array(
      'rows' => $sheet['numRows'],
      'cols' => $sheet['numCols'],
    );
    $iterative_range = new ExcelRange($this->config['iterative'], $max);
    $sheet_id = $sheet['id'];

    // Retrieve all items out of defined iterative Range.
    while ($range = $iterative_range
      ->getNextRange()) {
      $range_id = $iterative_range
        ->getCurrentRangeID();
      $cells = $sheet['cells'];
      foreach ($cells as $row => $cols) {
        if ($iterative_range
          ->isRowInCurrentRange($row)) {
          $row_offset = $iterative_range
            ->getCurrentRangeRowOffset();
          $max_rows = $iterative_range
            ->getCurrentRangeMaxRow();
          foreach ($cols as $col => $value) {
            if ($iterative_range
              ->isColInCurrentRange($col)) {
              $col_offset = $iterative_range
                ->getCurrentRangeColOffset();
              $max_cols = $iterative_range
                ->getCurrentRangeMaxRow();

              // Encode value and raw to utf8 in any case
              $value = utf8_encode($value);
              if (!isset($sheet['cellsInfo'][$row][$col]['raw'])) {
                $raw = $value;
              }
              else {
                $raw = utf8_encode($sheet['cellsInfo'][$row][$col]['raw']);
              }

              // Build cell
              $cell = array(
                'value' => $value,
                'type' => $sheet['cellsInfo'][$row][$col]['type'],
                'raw' => $raw,
                'column' => $col,
                'row' => $row,
                'x' => $col - $col_offset,
                'y' => $row - $row_offset,
              );

              // Set items dependent on
              switch ($mode) {
                case 'rows':
                  $key = "{$sheet_id}:{$range_id}:{$row}";
                  $items[$key]['cells'][$col - $col_offset] = $cell;
                  $items[$key]['row'] = $row;
                  break;
                case 'columns':
                  $key = "{$sheet_id}:{$range_id}:{$col}";
                  $items[$key]['cells'][$row - $row_offset] = $cell;
                  $items[$key]['column'] = $col;
                  break;
              }

              // Meta data for the given item
              if ($key && !isset($items[$key]['meta'])) {
                $items[$key]['meta'] = array(
                  'range_row_offset' => $row_offset,
                  'range_col_offset' => $col_offset,
                  'range_id' => $range_id,
                  'sheet_id' => $sheet_id,
                  'range_max_cols' => $max_cols,
                  'range_max_rows' => $max_rows,
                  'key' => $key,
                );
              }
            }
          }
        }
      }
    }
  }
  return $items;
}