You are here

private function FeedsExcelParser::getItems in Feeds Excel 6

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

Retrieve Items from Spredsheet.

1 call to FeedsExcelParser::getItems()
FeedsExcelParser::parse in ./ExcelParser.inc
Implementation of FeedsParser::parse().

File

./ExcelParser.inc, line 135

Class

FeedsExcelParser
Parses a given file as a Excel file.

Code

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

  // Run every sheet.
  foreach ($sheets as $sheet) {

    // 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;
}