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

[Bug] Add embedded layers to keyValueConfig object #4847

Merged
merged 2 commits into from
Oct 16, 2024
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
88 changes: 47 additions & 41 deletions lizmap/modules/lizmap/lib/Project/QgisProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ class QgisProject
'customProjectVariables',
);

/**
* @var array List of embedded projects
*/
protected $qgisEmbeddedProjects = array();

/**
* @var App\AppContextInterface
*/
Expand Down Expand Up @@ -644,27 +649,24 @@ public function xpathQuery($query)
*
* @deprecated
*
* @param mixed $layerId
* @param null|array $embeddedRelationsProjects reference to associative array path(key) - QgisProject instance (value)
* @param mixed $layerId
*
* @return \SimpleXMLElement[]
*/
public function getXmlLayer($layerId, &$embeddedRelationsProjects = null)
mind84 marked this conversation as resolved.
Show resolved Hide resolved
public function getXmlLayer($layerId)
{
$layer = $this->getLayerDefinition($layerId);
if ($layer && array_key_exists('embedded', $layer) && $layer['embedded'] == 1) {
// avoid reloading the same qgis project multiple times while reading relations by checking embeddedRelationsProjects param
// If this array is null or does not contains the corresponding qgis project, then the function if forced to load a new qgis project
if ($embeddedRelationsProjects && array_key_exists($layer['projectPath'], $embeddedRelationsProjects)) {
if (array_key_exists($layer['projectPath'], $this->qgisEmbeddedProjects)) {
// use QgisProject instance already created
$qgsProj = $embeddedRelationsProjects[$layer['projectPath']];
$qgsProj = $this->qgisEmbeddedProjects[$layer['projectPath']];
} else {
// create new QgisProject instance
$qgsProj = new QgisProject(realpath(dirname($this->path).DIRECTORY_SEPARATOR.$layer['projectPath']), $this->services, $this->appContext);
// update the array, if exists
if ($embeddedRelationsProjects) {
$embeddedRelationsProjects[$layer['projectPath']] = $qgsProj;
}
$this->qgisEmbeddedProjects[$layer['projectPath']] = $qgsProj;
}

return $qgsProj->getXml()->xpath("//maplayer[id='{$layerId}']");
Expand Down Expand Up @@ -920,7 +922,6 @@ public function readLocateByLayer($locateByLayer)
*/
public function readEditionLayers($editionLayers)
{
$embeddedEditionLayers = array();
foreach ($editionLayers as $key => $obj) {
// Improve performance by getting provider directly from config
// Available for lizmap plugin >= 3.3.2
Expand All @@ -932,7 +933,7 @@ public function readEditionLayers($editionLayers)
continue;
}
// check for embedded layers
$qgisProject = $this->getEmbeddedQgisProject($obj, $embeddedEditionLayers);
$qgisProject = $this->getEmbeddedQgisProject($obj->layerId);
if ($qgisProject) {
$xml = $qgisProject->getXml();
} else {
Expand Down Expand Up @@ -961,7 +962,7 @@ public function readEditionForms($editionLayers, $proj)
$embeddedEditionLayers = array();
foreach ($editionLayers as $key => $obj) {
// check for embedded layers
$qgisProject = $this->getEmbeddedQgisProject($obj, $embeddedEditionLayers);
$qgisProject = $this->getEmbeddedQgisProject($obj->layerId);
if ($qgisProject) {
$xml = $qgisProject->getXml();
} else {
Expand All @@ -978,36 +979,33 @@ public function readEditionForms($editionLayers, $proj)
}

/**
* @param object $editionLayer
* @param array $embeddedEditionLayers
* @param string $layer
*
* @return null|QgisProject
*/
public function getEmbeddedQgisProject($editionLayer, &$embeddedEditionLayers = null)
public function getEmbeddedQgisProject($layer)
{
$qgisProject = null;
if (is_array($embeddedEditionLayers)) {
$layerDefinition = $this->getLayerDefinition($editionLayer->layerId);
if ($layerDefinition && array_key_exists('embedded', $layerDefinition) && $layerDefinition['embedded'] == 1) {
if (array_key_exists($layerDefinition['projectPath'], $embeddedEditionLayers)) {
// use QgisProject instance already created
$qgisProject = $embeddedEditionLayers[$layerDefinition['projectPath']];
$layerDefinition = $this->getLayerDefinition($layer);
if ($layerDefinition && array_key_exists('embedded', $layerDefinition) && $layerDefinition['embedded'] == 1) {
if (array_key_exists($layerDefinition['projectPath'], $this->qgisEmbeddedProjects)) {
// use QgisProject instance already created
$qgisProject = $this->qgisEmbeddedProjects[$layerDefinition['projectPath']];
} else {
// create new QgisProject instance or retreive it from cache, if any
$path = realpath(dirname($this->path).DIRECTORY_SEPARATOR.$layerDefinition['projectPath']);
$qgsMtime = filemtime($path);
$qgsCfgMtime = filemtime($path.'.cfg');
$cacheHandler = new ProjectCache($path, $qgsMtime, $qgsCfgMtime, $this->appContext);
$data = $cacheHandler->retrieveProjectData();

if ($data) {
$qgisProject = new QgisProject($path, $this->services, $this->appContext, $data['qgis']);
} else {
// create new QgisProject instance or retreive it from cache, if any
$path = realpath(dirname($this->path).DIRECTORY_SEPARATOR.$layerDefinition['projectPath']);
$qgsMtime = filemtime($path);
$qgsCfgMtime = filemtime($path.'.cfg');
$cacheHandler = new ProjectCache($path, $qgsMtime, $qgsCfgMtime, $this->appContext);
$data = $cacheHandler->retrieveProjectData();

if ($data) {
$qgisProject = new QgisProject($path, $this->services, $this->appContext, $data['qgis']);
} else {
$qgisProject = new QgisProject($path, $this->services, $this->appContext);
}

$embeddedEditionLayers[$layerDefinition['projectPath']] = $qgisProject;
$qgisProject = new QgisProject($path, $this->services, $this->appContext);
}

$this->qgisEmbeddedProjects[$layerDefinition['projectPath']] = $qgisProject;
}
}

Expand All @@ -1030,7 +1028,15 @@ public function readLayersLabeledFieldsConfig($layerIds, $proj)
// Get QGIS form fields configurations for each layer
$layersLabeledFieldsConfig = array();
foreach ($layerIds as $layerId) {
$layerXml = $this->getXmlLayer2($this->getXml(), $layerId);
$qgisProject = $this->getEmbeddedQgisProject($layerId);

if ($qgisProject) {
$xml = $qgisProject->getXml();
} else {
$xml = $this->getXml();
}

$layerXml = $this->getXmlLayer2($xml, $layerId);
if (is_null($layerXml)) {
continue;
}
Expand Down Expand Up @@ -1505,13 +1511,11 @@ protected function readRelations($xml)
if ($xmlRelations) {
// Store qgisProjects references in a key=>value array and pass it by reference along the methods that loads and validates relations.
// This avoid to reload the same QgisProject instance multiple times, if there are many "embedded relations" referencing the same (embedded) qgis project
$embeddedRelationsProjects = array();

/** @var \SimpleXMLElement $relation */
foreach ($xmlRelations[0] as $relation) {
$relationObj = $relation->attributes();

$relationField = $this->readRelationField($relation, $embeddedRelationsProjects);
$relationField = $this->readRelationField($relation);
if ($relationField === null) {
// no corresponding layer
continue;
Expand Down Expand Up @@ -1555,13 +1559,12 @@ protected function readRelations($xml)

/**
* @param \SimpleXMLElement $relationXml
* @param null|array $embeddedRelationsProjects
*/
protected function readRelationField($relationXml, &$embeddedRelationsProjects = null)
protected function readRelationField($relationXml)
{
$referencedLayerId = $relationXml->attributes()->referencedLayer;

$_referencedLayerXml = $this->getXmlLayer($referencedLayerId, $embeddedRelationsProjects);
$_referencedLayerXml = $this->getXmlLayer($referencedLayerId);
if (count($_referencedLayerXml) == 0) {
return null;
}
Expand Down Expand Up @@ -1845,6 +1848,9 @@ protected function readLayers($xml)
$layer['projectPath'] = (string) $attributes->project;
$layers[] = $layer;
}
if (!array_key_exists($projectPath, $this->qgisEmbeddedProjects)) {
$this->qgisEmbeddedProjects[$projectPath] = $embeddedProject;
}
}
}

Expand Down
37 changes: 37 additions & 0 deletions tests/end2end/playwright/embedded-form.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,43 @@ test.describe('Edition of an embedded layer', () => {
await page.locator('#dock-close').click();
});

test('Inspect keyValueConfig for embedded layers', async ({ page }) =>{
const keyValueConfig = await (page.evaluate(() =>globalThis.lizMap.keyValueConfig ));

const expectedKeyValueConfig = {
edition_layer_embed_child: [],
edition_layer_embed_line: [],
edition_layer_embed_point:{
id_ext_point: {
code_field: "id",
exp_filter: "",
label_field: "descr",
source_layer: "edition_layer_embed_child",
source_layer_id: "edition_layer_embed_child_d87f81cd_26d2_4c40_820d_676ba03ff6ab",
type: "ValueRelation"
}
}
}

await expect(keyValueConfig).toEqual(expectedKeyValueConfig);

// open attribute table
await page.locator('#button-attributeLayers').click();

let getKeyValueRequestPromise = page.waitForRequest(request => request.method() === 'POST' && request.postData()?.includes('edition_layer_embed_child') === true && request.postData()?.includes('GetFeature') === true);
await page.locator('#attribute-layer-list button.btn-open-attribute-layer[value="edition_layer_embed_point"]').click();
await getKeyValueRequestPromise;

// inspect attribute table
let table = page.locator('#attribute-layer-table-edition_layer_embed_point');
await expect(table).toHaveCount(1);

await expect(table.locator('tbody tr').nth(0).locator('td').nth(2)).toHaveText('External1');
await expect(table.locator('tbody tr').nth(1).locator('td').nth(2)).toHaveText('External2');
await expect(table.locator('tbody tr').nth(2).locator('td').nth(2)).toHaveText('');

})

test('Open embedded layer edition form', async ({ page }) => {
let editPointRequestPromise = page.waitForResponse(response => response.url().includes('editFeature'));

Expand Down
Loading