You are here

class DrupalTableExporter in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 includes/DrupalTableExporter.php \AKlump\LoftDataGrids\DrupalTableExporter

Class DrupalTableExporter

Hierarchy

Expanded class hierarchy of DrupalTableExporter

1 string reference to 'DrupalTableExporter'
_loft_data_grids_get_exporter_names in ./loft_data_grids.module
Return an array of Exporter classnames dynamically loaded and cached

File

src/AKlump/LoftDataGrids/DrupalTableExporter.php, line 7

Namespace

AKlump\LoftDataGrids
View source
class DrupalTableExporter extends Exporter implements DrupalExporterInterface {
  public $format;
  protected $extension = '.html';
  public function getInfo() {
    $info = parent::getInfo();
    $info = array(
      'name' => 'Drupal table formatter',
      'shortname' => 'theme_table',
      'description' => 'Export data using theme_table().',
    ) + $info;
    return $info;
  }
  public function compile($page_id = null) {
    $build = $this
      ->build($page_id);
    $this->output = drupal_render($build);
    return $this;
  }
  public function build($page_id = null) {
    $pages = $this
      ->getData()
      ->get();
    if ($page_id && array_key_exists($page_id, $pages)) {
      $pages = array(
        $pages[$page_id],
      );
    }
    $build = array();
    $page_ct = 0;
    foreach ($pages as $page_id => $data) {
      $vars['#ExportData'] = $this;
      $vars['#page_id'] = $page_id;
      $vars['attributes'] = array();
      $vars['caption'] = !empty($page_id) ? $page_id : null;
      $vars['header'] = array();
      $column_no = 1;
      foreach ($this
        ->getHeader($page_id) as $header_key => $value) {
        $header_classes = array();
        $header_classes[] = 'column-' . $column_no;
        if ($column_no === count(reset($data)) - 1) {
          $header_classes[] = 'last';
        }
        elseif ($column_no === 0) {
          $header_classes[] = 'first';
        }
        $string = preg_replace('/[^a-z0-9\\-\\.]/', '-', strtolower($header_key));
        $string = preg_replace('/^\\d/', 'c-\\0', $string);
        $header_classes[] = preg_replace('/-{2,}/', '-', $string);
        $vars['header'][] = array(
          'data' => t($value),
          'class' => $this
            ->tableClassesHandler($header_classes),
        );
        $column_no++;
      }
      $vars['rows'] = array();
      foreach (array_values($data) as $row_no => $row) {
        $row_classes = array();
        $row_classes[] = 'row-' . $row_no;
        if ($row_no === count($data) - 1) {
          $row_classes[] = 'last';
        }
        elseif ($row_no === 0) {
          $row_classes[] = 'first';
        }
        $columns = array();
        $array_keys = array_keys($row);
        foreach (array_values($row) as $column_no => $column) {
          $column_classes = array();
          $column_classes[] = 'column-' . $column_no;
          if ($column_no === count($row) - 1) {
            $column_classes[] = 'last';
          }
          elseif ($column_no === 0) {
            $column_classes[] = 'first';
          }
          $string = preg_replace('/[^a-z0-9\\-\\.]/', '-', strtolower($array_keys[$column_no]));
          $string = preg_replace('/^\\d/', 'c-\\0', $string);
          $column_classes[] = preg_replace('/-{2,}/', '-', $string);
          $columns[] = array(
            'data' => $column,
            'class' => $this
              ->tableClassesHandler($column_classes),
          );
        }
        $vars['rows'][] = array(
          'data' => $columns,
          'class' => $this
            ->tableClassesHandler($row_classes),
        );
      }
      $build['table' . ($page_ct++ > 0 ? '_' . $page_ct : '')] = array(
        '#theme' => 'table',
        '#rows' => $vars['rows'],
        '#header' => $vars['header'],
        '#attributes' => $vars['attributes'],
        '#caption' => $vars['caption'],
      );
      drupal_alter('loft_data_grids_table', $build, $this, $page_id);
    }
    return $build;
  }

  /**
   * Theme table handled classes differently across versions, so this is a
   * helper.
   *
   * @param $classes
   *
   * @return string|array
   */
  protected function tableClassesHandler($classes) {
    return $classes;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalTableExporter::$extension protected property
DrupalTableExporter::$format public property
DrupalTableExporter::build public function Return a renderable build array Overrides DrupalExporterInterface::build
DrupalTableExporter::compile public function Build the string content of $this->output and return $this for chaining. Overrides ExporterInterface::compile
DrupalTableExporter::getInfo public function Return info about this class Overrides Exporter::getInfo
DrupalTableExporter::tableClassesHandler protected function Theme table handled classes differently across versions, so this is a helper.
Exporter::$export_data protected property
Exporter::$header protected property
Exporter::$settings protected property
Exporter::addSetting public function Adds/Updates a single setting by name. Overrides ExporterInterface::addSetting
Exporter::cssSafe protected function
Exporter::dataTransform protected function Iterate over all cells and transform data as appropriate.
Exporter::export public function Export data as a string Overrides ExporterInterface::export 1
Exporter::filenameSafe protected function Return a string as a safe filename
Exporter::formatColumn public function Format a single column with format by string Overrides ExporterInterface::formatColumn 1
Exporter::getData public function Return the ExportDataInterface object Overrides ExporterInterface::getData
Exporter::getDataAsTransformedArray protected function Convert ExportData to an array transforming every cell.
Exporter::getFilename public function Get the filename Overrides ExporterInterface::getFilename
Exporter::getHeader public function Return an array each of the keys present in the data on a page. Overrides ExporterInterface::getHeader
Exporter::getSettings public function Return the settings object. Overrides ExporterInterface::getSettings
Exporter::getShowPageIds public function Return the showPageIds. Overrides ExporterInterface::getShowPageIds
Exporter::getTitle public function Overrides ExporterInterface::getTitle
Exporter::hidePageIds public function Set the exporter to hide page ids. Overrides ExporterInterface::hidePageIds
Exporter::save public function Stream as a file to the server with headers. Overrides ExporterInterface::save 1
Exporter::saveFile public function Compile and and save to a filepath. Overrides ExporterInterface::saveFile 2
Exporter::setData public function Set the export data object Overrides ExporterInterface::setData
Exporter::setFilename public function Getter/Setter for the filename Overrides ExporterInterface::setFilename
Exporter::setSettings public function Set the settings object. Overrides ExporterInterface::setSettings
Exporter::setSettingsDefault protected function Setup default values on object data. 4
Exporter::setTitle public function Set a title for the exported document Overrides ExporterInterface::setTitle 1
Exporter::showPageIds public function Set the exporter to display page ids. Overrides ExporterInterface::showPageIds
Exporter::__construct public function Constructor 5