You are here

public function PdfTemplate::drawTable in Views PDF 7

Same name and namespace in other branches
  1. 6 views_pdf_template.php \PdfTemplate::drawTable()
  2. 7.3 views_pdf_template.php \PdfTemplate::drawTable()
  3. 7.2 views_pdf_template.php \PdfTemplate::drawTable()

This method draws a table on the PDF.

File

./views_pdf_template.php, line 646
PDF Class to generate PDFs with native PHP. This class based on FPDF and FPDI.

Class

PdfTemplate
The main class to generate the PDF.

Code

public function drawTable(&$view, $options) {
  $rows = $view->result;
  $columns = $view->field;
  $pageDim = $this
    ->getPageDimensions();

  // Set draw point to the indicated position:
  if (empty($options['position']['x'])) {
    $options['position']['x'] = 0;
  }
  if (empty($options['position']['y'])) {
    $options['position']['y'] = 0;
  }
  if (!empty($this->views_header)) {
    $y = $this->y;
    $x = $this->x;
  }
  elseif (isset($options['position']['last_writing_position']) && $options['position']['last_writing_position']) {
    $y = $options['position']['y'] + $this->y;
    $x = $options['position']['x'] + $this->x;
  }
  else {
    $y = $options['position']['y'];
    $x = $options['position']['x'];
  }
  if (isset($options['position']['width']) && !empty($options['position']['width'])) {
    $width = $options['position']['width'];
  }
  else {
    $width = $pageDim['wk'] - $this->rMargin - $x;
  }
  $sumWidth = 0;
  $numberOfColumnsWithoutWidth = 0;

  // Set the definitiv width of a column
  foreach ($columns as $id => $columnName) {
    if (isset($options['info'][$id]['position']['width']) && !empty($options['info'][$id]['position']['width'])) {
      $sumWidth += $options['info'][$id]['position']['width'];
    }
    else {
      $numberOfColumnsWithoutWidth++;
    }
  }
  if ($numberOfColumnsWithoutWidth > 0) {
    $defaultColumnWidth = ($width - $sumWidth) / $numberOfColumnsWithoutWidth;
  }
  else {
    $defaultColumnWidth = 0;
  }

  // Print header:
  $rowX = $x;
  $page = $this
    ->getPage();
  if ($page == 0) {
    $this
      ->addPage();
    $page = $this
      ->getPage();
  }
  if (!isset($options['position']['row_height']) || empty($options['position']['row_height'])) {
    $options['position']['row_height'] = 0;
  }
  foreach ($columns as $id => $column) {
    if (!empty($column->options['exclude'])) {
      continue;
    }
    if (!is_array($options['info'][$id])) {
      $options['info'][$id] = array();
    }
    $options['info'][$id] += array(
      'header_style' => array(),
      'body_style' => array(),
    );
    $options['info'][$id]['header_style'] += array(
      'position' => array(),
      'text' => array(),
      'render' => array(),
    );
    $options['info'][$id]['header_style']['position'] += array(
      'corner' => 'top_left',
      'x' => NULL,
      'y' => NULL,
      'object' => '',
      'width' => NULL,
      'height' => NULL,
    );
    $options['info'][$id]['header_style']['text'] += array(
      'font_family' => 'default',
      'font_style' => '',
    );
    $options['info'][$id]['header_style']['text'] += array(
      'eval_before' => '',
      'eval_after' => '',
    );
    $options['info'][$id]['body_style'] += array(
      'position' => array(),
      'text' => array(),
      'render' => array(),
    );
    $options['info'][$id]['body_style']['position'] += array(
      'corner' => 'top_left',
      'x' => NULL,
      'y' => NULL,
      'object' => '',
      'width' => NULL,
      'height' => NULL,
    );
    $options['info'][$id]['body_style']['text'] += array(
      'font_family' => 'default',
      'font_style' => '',
    );
    $options['info'][$id]['body_style']['text'] += array(
      'eval_before' => '',
      'eval_after' => '',
    );
    $headerOptions = $options['info'][$id]['header_style'];
    if (isset($options['info'][$id]['position']['width']) && !empty($options['info'][$id]['position']['width'])) {
      $headerOptions['position']['width'] = $options['info'][$id]['position']['width'];
    }
    else {
      $headerOptions['position']['width'] = $defaultColumnWidth;
    }
    $headerOptions['position']['object'] = 'last_position_without_reset';
    $this
      ->SetY($y);
    $this
      ->SetX($x);
    $this
      ->setPage($page);
    $this
      ->renderRow($x, $y, $column->options['label'], $headerOptions, $view, $id, FALSE);
    $x += $headerOptions['position']['width'];
  }
  $rowY = $this->y;
  if (!isset($options['position']['row_height']) || empty($options['position']['row_height'])) {
    $options['position']['row_height'] = 0;
  }
  foreach ($rows as $row) {
    $x = $rowX;

    // Get the page dimensions
    $pageDim = $this
      ->getPageDimensions();
    if ($rowY + $this->bMargin + $options['position']['row_height'] > $pageDim['hk']) {
      $rowY = $this->tMargin;
      $this
        ->addPage();
    }
    if ($this->lastWritingPage != $this
      ->getPage()) {
      $rowY = $this->y;

      // $rowY - $pageDim['hk']
    }
    $y = $rowY;
    $page = $this
      ->getPage();
    foreach ($columns as $id => $column) {
      if (!empty($column->options['exclude']) && is_object($view->field[$id])) {

        // Render the element, but dont print the output. This
        // is required to allow the use of tokens in other fields.
        $view->field[$id]
          ->theme($row);
        continue;
      }
      $bodyOptions = $options['info'][$id]['body_style'];
      if (isset($options['info'][$id]['position']['width']) && !empty($options['info'][$id]['position']['width'])) {
        $bodyOptions['position']['width'] = $options['info'][$id]['position']['width'];
      }
      else {
        $bodyOptions['position']['width'] = $defaultColumnWidth;
      }
      $bodyOptions['position']['object'] = 'last_position';
      $this
        ->setPage($page);
      $this
        ->SetY($y);
      $this
        ->SetX($x);
      $bodyOptions['position']['height'] = 0;
      $this
        ->renderRow($x, $y, $row, $bodyOptions, $view, $id, FALSE);
      $x += $bodyOptions['position']['width'];

      // If the cell is writting over the row, we need to adjust the
      // row y position.
      if ($rowY + $options['position']['row_height'] < $this->y) {
        $rowY = $this->y - $options['position']['row_height'];
      }
    }
    $rowY += $options['position']['row_height'];
    $view->row_index++;
  }
  $this
    ->SetY($rowY + $options['position']['row_height'], false);
}