Skip to content

Commit

Permalink
Layout Import:
Browse files Browse the repository at this point in the history
 - ignore widgets for modules which don't exist in the CMS
 - DataSet don't test formulas
relates to xibosignageltd/xibo-private#384
  • Loading branch information
dasgarner committed Aug 6, 2023
1 parent a7af4c1 commit 209ec62
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
1 change: 0 additions & 1 deletion lib/Entity/DataSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,6 @@ public function save($options = [])
// Columns
if ($options['saveColumns']) {
foreach ($this->columns as $column) {
/* @var \Xibo\Entity\DataSetColumn $column */
$column->dataSetId = $this->dataSetId;
$column->save();
}
Expand Down
54 changes: 38 additions & 16 deletions lib/Entity/DataSetColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,12 @@ public function listContentArray()
* Validate
* @throws InvalidArgumentException
*/
public function validate()
public function validate($options = [])
{
$options = array_merge([
'testFormulas' => true,
], $options);

if ($this->dataSetId == 0 || $this->dataSetId == '')
throw new InvalidArgumentException(__('Missing dataSetId'), 'dataSetId');

Expand Down Expand Up @@ -273,33 +277,51 @@ public function validate()
throw new InvalidArgumentException(__('New list content value is invalid as it does not include values for existing data'), 'listcontent');
}

// if formula dataSetType is set and formula is not empty, try to execute the SQL to validate it - we're ignoring client side formulas here.
if ($this->dataSetColumnTypeId == 2 && $this->formula != '' && substr($this->formula, 0, 1) !== '$') {
try {
$formula = str_replace('[DisplayId]', 0, $this->formula);
$this->getStore()->select('SELECT * FROM (SELECT `id`, ' . $formula . ' AS `' . $this->heading . '` FROM `dataset_' . $this->dataSetId . '`) dataset WHERE 1 = 1 ', []);
} catch (\Exception $e) {
$this->getLog()->debug('Formula validation failed with following message ' . $e->getMessage());
throw new InvalidArgumentException(__('Provided formula is invalid'), 'formula');
}
// if formula dataSetType is set and formula is not empty, try to execute the SQL to validate it - we're
// ignoring client side formulas here.
if ($options['testFormulas']
&& $this->dataSetColumnTypeId == 2
&& $this->formula != ''
&& !str_starts_with($this->formula, '$')
) {
try {
$formula = str_replace('[DisplayId]', 0, $this->formula);
$this->getStore()->select('
SELECT *
FROM (
SELECT `id`, ' . $formula . ' AS `' . $this->heading . '`
FROM `dataset_' . $this->dataSetId . '`
) dataset
', []);
} catch (\Exception $e) {
$this->getLog()->debug('Formula validation failed with following message ' . $e->getMessage());
throw new InvalidArgumentException(__('Provided formula is invalid'), 'formula');
}
}
}

/**
* Save
* @param array[Optional] $options
* @param array $options
* @throws InvalidArgumentException
*/
public function save($options = [])
{
$options = array_merge(['validate' => true, 'rebuilding' => false], $options);
$options = array_merge([
'validate' => true,
'rebuilding' => false,
'testFormulas' => true,
], $options);

if ($options['validate'] && !$options['rebuilding']) {
$this->validate($options);
}

if ($options['validate'] && !$options['rebuilding'])
$this->validate();
if ($this->dataSetColumnId == 0)
if ($this->dataSetColumnId == 0) {
$this->add();
else
} else {
$this->edit($options);
}
}

/**
Expand Down
10 changes: 8 additions & 2 deletions lib/Factory/LayoutFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,7 @@ public function createFromZip(
$existingDataSet->save([
'activate' => false,
'notify' => false,
'testFormulas' => false,
]);

// Do we need to add data
Expand Down Expand Up @@ -1905,8 +1906,13 @@ public function createFromZip(

// We need one final pass through all widgets on the layout so that we can set the durations properly.
foreach ($layout->getAllWidgets() as $widget) {
$module = $this->moduleFactory->getByType($widget->type);
$widget->calculateDuration($module, $importedFromXlf);
try {
$module = $this->moduleFactory->getByType($widget->type);
$widget->calculateDuration($module, $importedFromXlf);
} catch (NotFoundException) {
// This widget does not exist in this CMS, so we can ignore this.
$this->getLog()->error('createFromZip: widget type ' . $widget->type . ' not available in this CMS');
}

// Get global stat setting of widget to set to on/off/inherit
$widget->setOptionValue('enableStat', 'attrib', $this->config->getSetting('WIDGET_STATS_ENABLED_DEFAULT'));
Expand Down

0 comments on commit 209ec62

Please sign in to comment.