Skip to content

Commit

Permalink
Include DataSet structure and optionally data in the Layout Export.
Browse files Browse the repository at this point in the history
  • Loading branch information
dasgarner committed Jun 10, 2016
1 parent 0ff6bb9 commit b20eda3
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 7 deletions.
33 changes: 29 additions & 4 deletions lib/Controller/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Xibo\Entity\Widget;
use Xibo\Exception\AccessDeniedException;
use Xibo\Exception\LibraryFullException;
use Xibo\Factory\DataSetFactory;
use Xibo\Factory\LayoutFactory;
use Xibo\Factory\MediaFactory;
use Xibo\Factory\ModuleFactory;
Expand Down Expand Up @@ -94,6 +95,9 @@ class Layout extends Base
*/
private $mediaFactory;

/** @var DataSetFactory */
private $dataSetFactory;

/**
* Set common dependencies.
* @param LogServiceInterface $log
Expand All @@ -112,8 +116,9 @@ class Layout extends Base
* @param UserGroupFactory $userGroupFactory
* @param TagFactory $tagFactory
* @param MediaFactory $mediaFactory
* @param DataSetFactory $dataSetFactory
*/
public function __construct($log, $sanitizerService, $state, $user, $help, $date, $config, $session, $userFactory, $resolutionFactory, $layoutFactory, $moduleFactory, $permissionFactory, $userGroupFactory, $tagFactory, $mediaFactory)
public function __construct($log, $sanitizerService, $state, $user, $help, $date, $config, $session, $userFactory, $resolutionFactory, $layoutFactory, $moduleFactory, $permissionFactory, $userGroupFactory, $tagFactory, $mediaFactory, $dataSetFactory)
{
$this->setCommonDependencies($log, $sanitizerService, $state, $user, $help, $date, $config);

Expand All @@ -126,6 +131,7 @@ public function __construct($log, $sanitizerService, $state, $user, $help, $date
$this->userGroupFactory = $userGroupFactory;
$this->tagFactory = $tagFactory;
$this->mediaFactory = $mediaFactory;
$this->dataSetFactory = $dataSetFactory;
}

/**
Expand Down Expand Up @@ -757,8 +763,7 @@ function grid()
// Export Button
$layout->buttons[] = array(
'id' => 'layout_button_export',
'linkType' => '_self', 'external' => true,
'url' => $this->urlFor('layout.export', ['id' => $layout->layoutId]),
'url' => $this->urlFor('layout.export.form', ['id' => $layout->layoutId]),
'text' => __('Export')
);

Expand Down Expand Up @@ -1150,6 +1155,26 @@ public function status($layoutId)
}
}

/**
* Export Form
* @param $layoutId
*/
public function exportForm($layoutId)
{
// Get the layout
$layout = $this->layoutFactory->getById($layoutId);

// Check Permissions
if (!$this->getUser()->checkViewable($layout))
throw new AccessDeniedException();

// Render the form
$this->getState()->template = 'layout-form-export';
$this->getState()->setData([
'layout' => $layout
]);
}

/**
* @param int $layoutId
*/
Expand All @@ -1165,7 +1190,7 @@ public function export($layoutId)
throw new AccessDeniedException();

$fileName = $this->getConfig()->GetSetting('LIBRARY_LOCATION') . 'temp/export_' . $layout->layout . '.zip';
$layout->toZip($fileName);
$layout->toZip($this->dataSetFactory, $fileName, ['includeData' => ($this->getSanitizer()->getCheckbox('includeData')== 1)]);

if (ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
Expand Down
6 changes: 5 additions & 1 deletion lib/Entity/DataSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ class DataSet implements \JsonSerializable
public $isLookup = 0;

private $permissions = [];
private $columns = [];

/**
* @var DataSetColumn array
*/
public $columns = [];

private $countLast = 0;

Expand Down
36 changes: 35 additions & 1 deletion lib/Entity/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use Xibo\Exception\NotFoundException;
use Xibo\Factory\CampaignFactory;
use Xibo\Factory\DataSetFactory;
use Xibo\Factory\LayoutFactory;
use Xibo\Factory\MediaFactory;
use Xibo\Factory\ModuleFactory;
Expand Down Expand Up @@ -977,10 +978,17 @@ public function toXlf()

/**
* Export the Layout as a ZipArchive
* @param DataSetFactory $dataSetFactory
* @param string $fileName
* @param array $options
*/
public function toZip($fileName)
public function toZip($dataSetFactory, $fileName, $options = [])
{
$options = array_merge([
'includeData' => false
], $options);

// We export to a ZIP file
$zip = new \ZipArchive();
$result = $zip->open($fileName, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
if ($result !== true)
Expand Down Expand Up @@ -1067,6 +1075,32 @@ public function toZip($fileName)
// Add the mappings file to the ZIP
$zip->addFromString('mapping.json', json_encode($mappings));

// Handle any DataSet structures
$dataSets = [];

foreach ($this->getWidgets() as $widget) {
/** @var Widget $widget */
if ($widget->type == 'datasetview' || $widget->type == 'ticker') {
$dataSetId = $widget->getOptionValue('dataSetId', 0);

if ($dataSetId != 0) {
// Export the structure for this dataSet
$dataSet = $dataSetFactory->getById($dataSetId);
$dataSet->load();

// Are we also looking to export the data?
if ($options['includeData']) {
$dataSet->data = $dataSet->getData();
}

$dataSets[] = $dataSet;
}
}
}

// Add the mappings file to the ZIP
$zip->addFromString('dataSet.json', json_encode($dataSets, JSON_PRETTY_PRINT));

$zip->close();
}

Expand Down
3 changes: 2 additions & 1 deletion lib/Middleware/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,8 @@ public static function registerControllersWithDi($app)
$container->permissionFactory,
$container->userGroupFactory,
$container->tagFactory,
$container->mediaFactory
$container->mediaFactory,
$container->dataSetFactory
);
});

Expand Down
1 change: 1 addition & 0 deletions lib/routes-web.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
$app->get('/layout/form/delete/:id', '\Xibo\Controller\Layout:deleteForm')->name('layout.delete.form');
$app->get('/layout/form/retire/:id', '\Xibo\Controller\Layout:retireForm')->name('layout.retire.form');
$app->get('/layout/form/upgrade/:id', '\Xibo\Controller\Layout:upgradeForm')->name('layout.upgrade.form');
$app->get('/layout/form/export/:id', '\Xibo\Controller\Layout:exportForm')->name('layout.export.form');

//
// regions
Expand Down
38 changes: 38 additions & 0 deletions views/layout-form-export.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{#
/*
* Spring Signage Ltd - http://www.springsignage.com
* Copyright (C) 2016 Spring Signage Ltd
* (layout-form-export.twig)
*/
#}

{% extends "form-base.twig" %}
{% import "forms.twig" as forms %}

{% block formTitle %}
{% trans "Export" %}
{% endblock %}

{% block formButtons %}
{% trans "Cancel" %}, XiboDialogClose()
{% trans "Yes" %}, $("#layoutExportForm").submit()
{% endblock %}

{% block callBack %}layoutExportFormOpen{% endblock %}

{% block formHtml %}
<div class="row">
<div class="col-md-12">
<form id="layoutExportForm" class="form-horizontal" method="get" action="{{ urlFor("layout.export", {id: layout.layoutId}) }}">
{% set layoutName = layout.layout %}
{% set helpText %}{% trans %}You have selected {{ layoutName }} for export. A ZIP file will be downloaded which contains the layout, its widgets and media. It will also contain the structure for associated DataSets.{% endtrans %}{% endset %}
{{ forms.message(helpText) }}

{% set title %}{% trans "Include DataSet data?" %}{% endset %}
{% set helpText %}{% trans "Do you want to include the DataSet data?" %}{% endset %}
{{ forms.checkbox("includeData", title, 0, helpText) }}

</form>
</div>
</div>
{% endblock %}
6 changes: 6 additions & 0 deletions views/layout-page.twig
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@
});
});
}
function layoutExportFormOpen() {
$("#layoutExportForm").submit(function(e) {
XiboDialogClose();
});
}
</script>
{% endblock %}

Expand Down

0 comments on commit b20eda3

Please sign in to comment.