You are here

public function XMLExporter::compile in Loft Data Grids 6.2

Same name and namespace in other branches
  1. 7.2 vendor/aklump/loft_data_grids/src/AKlump/LoftDataGrids/XMLExporter.php \AKlump\LoftDataGrids\XMLExporter::compile()

Build $this->output in prep for export/save

This is the main method to be extended for the different exporters

Overrides Exporter::compile

File

vendor/aklump/loft_data_grids/src/AKlump/LoftDataGrids/XMLExporter.php, line 21

Class

XMLExporter
Class XMLExporter

Namespace

AKlump\LoftDataGrids

Code

public function compile($page_id = NULL) {
  $data = $this
    ->getData()
    ->get();
  $xml = new \SimpleXMLElement('<data/>');
  $pages = $this
    ->getData()
    ->get();
  if ($page_id && array_key_exists($page_id, $pages)) {
    $pages = array(
      $pages[$page_id],
    );
  }
  foreach ($pages as $page_id => $data) {
    $page = $xml
      ->addChild('page');
    $page
      ->addAttribute('id', $page_id);
    foreach ($data as $id => $data_set) {
      $set = $page
        ->addChild('record');
      $set
        ->addAttribute('id', $id);
      foreach ($data_set as $key => $value) {

        // make sure the key is in good format
        $key = preg_replace('/[^a-z0-9_-]/', '_', strtolower($key));

        // Wrap cdata as needed
        if (strstr($value, '<') || strstr($value, '&')) {
          $value = '<![CDATA[' . $value . ']]>';
        }
        $set
          ->addChild($key, $value);
      }
    }
  }
  $this->output = $xml
    ->asXML();
  $this->output = str_replace('&lt;![CDATA[', '<![CDATA[', $this->output);
  $this->output = str_replace(']]&gt;</', ']]></', $this->output);
}