Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude hidden elements from exports. (#1728) #1731

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions apps/qubit/modules/clipboard/actions/exportAction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ClipboardExportAction extends DefaultEditAction
'includeAllLevels',
'includeDigitalObjects',
'includeDrafts',
'includeNonVisibleElements',
];

private $choices = [];
Expand Down Expand Up @@ -80,6 +81,19 @@ public function execute($request)
$this->digitalObjectsAvailable = true;
}

// Determine if there are non-visible elements that should be hidden
$this->nonVisibleElementsIncluded = false;
$defTemplate = sfConfig::get(('app_default_template_'.strtolower($this->objectType)));

foreach (sfConfig::getAll() as $setting => $value) {
if (
(false !== strpos($setting, ('app_element_visibility_'.$defTemplate)))
&& (0 == sfConfig::get($setting))
) {
$this->nonVisibleElementsIncluded = true;
}
}

// Show export options panel if:
// information object type
// or, if actor type and digital objects are on the clipboard
Expand Down Expand Up @@ -110,6 +124,13 @@ public function execute($request)
&& $this->context->user->isAuthenticated()
&& 'on' == $request->getParameter('includeDrafts');

// Get field includeNonVisibleElements if:
// options enabled
// and, user is authenticated
$this->nonVisibleElementsIncluded = $this->showOptions
&& $this->context->user->isAdministrator()
&& 'on' == $request->getParameter('includeNonVisibleElements');

parent::execute($request);

$this->response->addJavaScript('exportOptions', 'last');
Expand Down Expand Up @@ -160,6 +181,7 @@ public function execute($request)
'public' => !$this->draftsIncluded,
'objectType' => $this->objectType,
'levels' => $levelsOfDescription,
'nonVisibleElementsIncluded' => $this->nonVisibleElementsIncluded,
];

$msg = ('xml' == $this->formatType) ? 'XML export' : 'CSV export';
Expand Down Expand Up @@ -428,6 +450,28 @@ protected function addField($name)
$this->form->setDefault('includeDrafts', true);
}

break;
// Enable field includeNonVisibleElements if:
// information object type
// and, user is authenticated
case 'includeNonVisibleElements':
if (
'informationObject' == $this->objectType
&& $this->context->user->isAdministrator()
) {
$this->form->setWidget(
'includeNonVisibleElements',
new sfWidgetFormInputCheckbox(
['label' => __('Include non-visible elements')]
)
);

$this->helpMessages[] = __(
'Choosing "Include non-visible elements" will include those not marked as Visible Elements.'
);
$this->form->setDefault('includeNonVisibleElements', false);
}

break;

default:
Expand Down
2 changes: 1 addition & 1 deletion js/exportOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@

onObjectTypeChange: function ()
{

var url = window.location.href.split('?')[0] + '?type=';
var type = this.$type.val().trim();
switch (type)
Expand Down
84 changes: 77 additions & 7 deletions lib/flatfile/QubitFlatfileExport.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class QubitFlatfileExport
protected $rowsPerFile = 1000; // how many rows until creating new export file

protected $separatorChar = '|'; // character to use when imploding arrays to a single value
protected $params;
protected $nonVisibleElementsIncluded;

/*
* Constructor
Expand Down Expand Up @@ -243,6 +245,10 @@ public function exportResource(&$resource)
$this->loadResourceSpecificConfiguration(get_class($resource));
}

if (!$this->params['nonVisibleElementsIncluded']) {
$this->getHiddenVisibleElementCsvHeaders();
}

$this->resource = $resource;

// If writing to a directory, generate filename periodically to keep each
Expand All @@ -269,6 +275,13 @@ public function exportResource(&$resource)
}
}

$this->prepareRowFromResource();

if (!empty($this->nonVisibleElementsIncluded)) {
// Remove elements from $this->columnNames that are in $this->nonVisibleElementsIncluded
$this->columnNames = array_diff($this->columnNames, $this->nonVisibleElementsIncluded);
}

// If file doesn't yet exist, write headers
if (!file_exists($filePath)) {
$this->appendRowToCsvFile($filePath, $this->columnNames);
Expand All @@ -279,9 +292,8 @@ public function exportResource(&$resource)
Qubit::clearClassCaches();
}

$this->prepareRowFromResource();

// Write row to file and initialize row
$this->row = array_slice($this->row, 0, count($this->columnNames));
$this->appendRowToCsvFile($filePath, $this->row);
$this->row = array_fill(0, count($this->columnNames), null);
++$this->rowsExported;
Expand All @@ -296,24 +308,82 @@ public function prepareRowFromResource()
foreach ($this->columnNames as $index => $column) {
$value = $this->row[$index];

// If row value hasn't been set to anything, attempt to get resource property
if (null === $value) {
// If row value hasn't been set to anything and element is not hidden,
// attempt to get resource property
if (null === $value && !in_array($column, $this->nonVisibleElementsIncluded)) {
if (in_array($column, $this->standardColumns)) {
$value = $this->resource->{$column};
} elseif (($sourceColumn = array_search($column, $this->columnMap)) !== false) {
$value = $this->resource->{$sourceColumn};
} elseif (isset($this->propertyMap[$column])) {
$value = $this->resource->getPropertyByName($this->propertyMap[$column])->__toString();
}
}

// Add column value (imploding if necessary)
$this->row[$index] = $this->content($value);
// Add column value (imploding if necessary)
$this->row[$index] = $this->content($value);
} else {
// Unset hidden elements columns
unset($this->row[$index]);
}
}

$this->modifyRowBeforeExport();
}

/**
* Get parameters to determine hidden elements.
*
* @param array parameters for CSV export
* @param mixed $params
*/
public function setParams($params)
{
$this->params = $params;
}

/**
* Get list of hidden elements.
*/
public function getHiddenVisibleElementCsvHeaders()
{
$nonVisibleElementsIncluded = [];
$nonVisibleElements = [];

if (!$this->params['nonVisibleElementsIncluded']) {
$template = sfConfig::get('app_default_template_'.strtolower($this->params['objectType']));

// Get list of elements hidden from settings
foreach (sfConfig::getAll() as $setting => $value) {
if (
(false !== strpos($setting, ('app_element_visibility_'.$template)))
&& (!strpos($setting, ('__source')))
&& (0 == sfConfig::get($setting))
) {
array_push($nonVisibleElements, $setting);
}
}

if (!empty($nonVisibleElements)) {
$mapPath = sfConfig::get('sf_lib_dir').DIRECTORY_SEPARATOR.'job/visibleElementsHeaderMap.yml';
$headers = sfYaml::load($mapPath);

// Get xml/csv headers to remove
foreach ($nonVisibleElements as $e) {
$prefix = 'app_element_visibility_';
$element = str_replace($prefix, '', $e);

if (array_key_exists($element, $headers)) {
foreach ($headers[$element]['csv'] as $ele) {
array_push($nonVisibleElementsIncluded, $ele);
}
}
}
}
}

$this->nonVisibleElementsIncluded = $nonVisibleElementsIncluded;
}

/*
* Custom configuration logic
*
Expand Down
3 changes: 3 additions & 0 deletions lib/job/arInformationObjectCsvExportJob.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ protected function exportResource($resource, $path)
*/
protected function exportDataAndDigitalObject($resource, $path)
{
// Pass parameters to QubitFlatfileExport to determine hidden visible elements
$this->csvWriter->setParams($this->params);

// Append resource metadata to CSV file
$this->csvWriter->exportResource($resource);

Expand Down
64 changes: 64 additions & 0 deletions lib/job/arInformationObjectXmlExportJob.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ protected function exportResource($resource, $path)
}
}

$nonVisibleElementsIncluded = $this->getHiddenVisibleElementXmlHeaders();

// Remove hidden visible elements from xml
if (!empty($nonVisibleElementsIncluded) && !$this->params['nonVisibleElementsIncluded']) {
foreach ($nonVisibleElementsIncluded as $element) {
// Determine if opening tag has additional elements that needs to
// be removed for closing tag
// ex. <odd type="descriptionIdentifier">
$parts = explode(' ', $element, 2);

// Set regular expression to match xml headers
if ('' != $parts[1]) {
$pattern = '/<'.$element.'.*?<\/'.$parts[0].'>/s';
} else {
$pattern = '/<'.$element.'.*?<\/'.$element.'>/s';
}

$xml = preg_replace($pattern, '', $xml);
}
}

$filename = exportBulkBaseTask::generateSortableFilename(
$resource,
'xml',
Expand All @@ -109,4 +130,47 @@ protected function exportResource($resource, $path)
++$this->itemsExported;
$this->logExportProgress();
}

/**
* Get list of hidden elements.
*
* @return array hidden elements
*/
protected function getHiddenVisibleElementXmlHeaders()
{
$nonVisibleElementsIncluded = [];
$nonVisibleElements = [];

$template = sfConfig::get('app_default_template_'.strtolower($this->params['objectType']));

// Get list of elements hidden from settings
foreach (sfConfig::getAll() as $setting => $value) {
if (
(false !== strpos($setting, ('app_element_visibility_'.$template)))
&& (!strpos($setting, ('__source')))
&& (0 == sfConfig::get($setting))
) {
array_push($nonVisibleElements, $setting);
}
}

if (!empty($nonVisibleElements)) {
$mapPath = sfConfig::get('sf_lib_dir').DIRECTORY_SEPARATOR.'job/visibleElementsHeaderMap.yml';
$headers = sfYaml::load($mapPath);

// Get xml/csv headers to remove
foreach ($nonVisibleElements as $e) {
$prefix = 'app_element_visibility_';
$element = str_replace($prefix, '', $e);

if (array_key_exists($element, $headers)) {
foreach ($headers[$element]['xml'] as $ele) {
array_push($nonVisibleElementsIncluded, $ele);
}
}
}
}

return $nonVisibleElementsIncluded;
}
}
Loading
Loading