You are here

public function Xls::encode in Excel Serialization 8

File

src/Encoder/Xls.php, line 53

Class

Xls
Adds XLS encoder support for the Serialization API.

Namespace

Drupal\xls_serialization\Encoder

Code

public function encode($data, $format, array $context = []) {
  switch (gettype($data)) {
    case 'array':

      // Nothing to do.
      break;
    case 'object':
      $data = (array) $data;
      break;
    default:
      $data = [
        $data,
      ];
      break;
  }
  try {

    // Instantiate a new excel object.
    $xls = new Spreadsheet();
    $xls
      ->setActiveSheetIndex(0);
    $sheet = $xls
      ->getActiveSheet();

    // Set headers.
    $this
      ->setHeaders($sheet, $data, $context);

    // Set the data.
    $this
      ->setData($sheet, $data);

    // Set the width of every column with data in it to AutoSize.
    $this
      ->setColumnsAutoSize($sheet);
    if (isset($context['views_style_plugin'])) {
      if (isset($context['views_style_plugin']->options['xls_settings'])) {
        $this
          ->setSettings($context['views_style_plugin']->options['xls_settings']);

        // Set any metadata passed in via the context.
        if (isset($context['views_style_plugin']->options['xls_settings']['metadata'])) {
          $this
            ->setMetaData($xls
            ->getProperties(), $context['views_style_plugin']->options['xls_settings']['metadata']);
        }
      }
      if (!empty($context['views_style_plugin']->view)) {

        /** @var \Drupal\views\ViewExecutable $view */
        $view = $context['views_style_plugin']->view;

        // Set the worksheet title based on the view title within the context.
        if (!empty($view
          ->getTitle())) {
          $sheet
            ->setTitle($this
            ->validateWorksheetTitle($view
            ->getTitle()));
        }

        // Set the header row of the worksheet to bold.
        if ($view
          ->getDisplay()
          ->getOption('header_bold') == 1) {
          $this
            ->setHeaderRowBold($sheet);
        }

        // Set the header row of the worksheet to italic.
        if ($view
          ->getDisplay()
          ->getOption('header_italic') == 1) {
          $this
            ->setHeaderRowItalic($sheet);
        }

        // Set the background color of the header row of the worksheet.
        if ($view
          ->getDisplay()
          ->getOption('header_background_color') != '') {
          $this
            ->setHeaderRowBackgroundColor($sheet, $view
            ->getDisplay()
            ->getOption('header_background_color'));
        }

        // Conditional formatting.
        for ($i = 0; $i <= 4; $i++) {
          $current_conditional_formatting_base_field = $view
            ->getDisplay()
            ->getOption('conditional_formatting_base_field_' . $i);
          if ($current_conditional_formatting_base_field !== NULL && $current_conditional_formatting_base_field !== 'Select a field') {
            $headers = $this
              ->extractHeaders($data, $context);
            $conditional_formatting_base_field[$i] = $current_conditional_formatting_base_field;
            $field_label_or_name[$i] = $this
              ->getViewFieldLabel($view, $conditional_formatting_base_field[$i]);
            $base_field_column_letter[$i] = $this
              ->getColumnLetterFromFieldName($headers, $field_label_or_name[$i]);
            $operator[$i] = $this
              ->getOperatorFromSelectIndex($view
              ->getDisplay()
              ->getOption('conditional_formatting_operator_' . $i));
            $compare_to[$i] = $view
              ->getDisplay()
              ->getOption('conditional_formatting_compare_to_' . $i);
            $rgb_background_color[$i] = $view
              ->getDisplay()
              ->getOption('conditional_formatting_background_color_' . $i);
            $conditional_styles[] = $this
              ->setConditionalFormat($base_field_column_letter[$i], $operator[$i], $compare_to[$i], $rgb_background_color[$i]);
          }
        }
        if (isset($conditional_styles)) {
          $this
            ->setConditionalFormating($sheet, $conditional_styles);
        }
      }
    }
    $writer = IOFactory::createWriter($xls, $this->xlsFormat);

    // @todo utilize a temporary file perhaps?
    // @todo This should also support batch processing.
    // @see http://stackoverflow.com/questions/9469779/how-do-i-write-my-excel-spreadsheet-into-a-variable-using-phpexcel
    ob_start();
    $writer
      ->save('php://output');
    return ob_get_clean();
  } catch (\Exception $e) {
    throw new InvalidDataTypeException($e
      ->getMessage(), $e
      ->getCode(), $e);
  }
}