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

[Feature] Add text widget to the edition form #4635

Merged
merged 3 commits into from
Oct 17, 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
87 changes: 85 additions & 2 deletions lizmap/modules/lizmap/classes/qgisAttributeEditorElement.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class qgisAttributeEditorElement
protected $_isGroupBox = false;
protected $_isTabPanel = false;
protected $_isRelationWidget = false;
protected $_isTextWidget = false;

protected $_isVisibilityExpressionEnabled = false;
protected $_visibilityExpression = '';
Expand All @@ -29,6 +30,7 @@ class qgisAttributeEditorElement
protected $childrenBeforeTab = array();
protected $tabChildren = array();
protected $childrenAfterTab = array();
protected $_textWidgetText = '';

public function __construct(
Lizmap\Form\QgisFormControlsInterface $formControls,
Expand Down Expand Up @@ -56,7 +58,7 @@ public function __construct(
$this->label = $getLabel;
}

$this->_isContainer = ($name != 'attributeEditorField' && $name != 'attributeEditorRelation');
$this->_isContainer = ($name != 'attributeEditorField' && $name != 'attributeEditorRelation' && $name != 'attributeEditorTextElement');
if (!$this->_isContainer) {
// Field
$this->htmlId = $parentId.'-'.$idx;
Expand All @@ -67,6 +69,13 @@ public function __construct(
$this->htmlId = $parentId.'-relation'.$idx;
$this->_isRelationWidget = true;
}
if ($name == 'attributeEditorTextElement') {
$this->htmlId = $parentId.'-text'.$idx;
$this->_isTextWidget = true;
$this->ctrlRef = $this->getName();
$stringNode = (string) $node;
$this->_textWidgetText = $stringNode;
}
} else {
// Manage containers: form, group or tab
if ($name == 'attributeEditorForm') {
Expand Down Expand Up @@ -99,6 +108,7 @@ public function __construct(
&& $name != 'attributeEditorForm'
&& $name != 'attributeEditorField'
&& $name != 'attributeEditorRelation'
&& $name != 'attributeEditorTextElement'
) {
++$childIdx;

Expand All @@ -108,7 +118,7 @@ public function __construct(

if (!$child->isContainer()) {
// Child is a Field input OR a relation widget
if ($child->getCtrlRef() !== null || $child->isRelationWidget()) {
if ($child->getCtrlRef() !== null || $child->isRelationWidget() || $child->isTextWidget()) {
if (count($this->tabChildren)) {
$this->childrenAfterTab[] = $child;
} else {
Expand Down Expand Up @@ -165,11 +175,31 @@ public function getAttribute($name)
return null;
}

/**
* Returns the _textWidgetText property value.
*
* @return string
*/
public function getTextWidgetText()
{
return $this->_textWidgetText;
}

public function isContainer()
{
return $this->_isContainer;
}

/**
* Returns the _isTextWidget property value, that is, whether the element is a text widget or not.
*
* @return bool
*/
public function isTextWidget()
{
return $this->_isTextWidget;
}

public function isGroupBox()
{
return $this->_isGroupBox;
Expand Down Expand Up @@ -266,6 +296,59 @@ public function getFields()
return $fields;
}

/**
* Returns the text widget fields configuration.
*
* @return array<mixed|string>[]
*/
public function getTextWidgetFields()
{
$fields = array();
if (!$this->hasChildren() && $this->isTextWidget() == true) {
$fields[$this->getName()] = array(
'label' => $this->getName(),
'name' => $this->getName(),
'value' => $this->getTextWidgetText(),
);

return $fields;
}

foreach ($this->getChildrenBeforeTab() as $child) {
if ($child->isGroupBox()) {
$fields = array_merge($fields, $child->getTextWidgetFields());
} else {
if ($child->isTextWidget() == true) {
$fields[$child->getName()] = array(
'label' => $child->getName(),
'name' => $child->getName(),
'value' => $child->getTextWidgetText(),
);
}
}
}

foreach ($this->getTabChildren() as $child) {
$fields = array_merge($fields, $child->getTextWidgetFields());
}

foreach ($this->getChildrenAfterTab() as $child) {
if ($child->isGroupBox()) {
$fields = array_merge($fields, $child->getTextWidgetFields());
} else {
if ($this->isTextWidget() == true) {
$fields[$child->getName()] = array(
'label' => $child->getName(),
'name' => $child->getName(),
'value' => $child->getTextWidgetText(),
);
}
}
}

return $fields;
}

public function getGroupVisibilityExpressions()
{
$expressions = array();
Expand Down
14 changes: 10 additions & 4 deletions lizmap/modules/lizmap/classes/qgisExpressionUtils.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,13 @@ public static function evaluateExpressions($layer, $expressions, $form_feature =
/**
* Request QGIS Server and the lizmap plugin to replace QGIS expressions text.
*
* @param qgisVectorLayer $layer A QGIS vector layer
* @param array $expressions The expressions text to replace
* @param qgisVectorLayer $layer A QGIS vector layer
* @param array $expressions The expressions text to replace
* @param array $form_feature A feature to add to the evaluation context
*
* @return null|object the results of expressions text replacement
*/
public static function replaceExpressionText($layer, $expressions)
public static function replaceExpressionText($layer, $expressions, $form_feature = null)
{
// Evaluate the expression by qgis
$project = $layer->getProject();
Expand All @@ -259,9 +260,14 @@ public static function replaceExpressionText($layer, $expressions)
'map' => $project->getRelativeQgisPath(),
'layer' => $layer->getName(),
'strings' => json_encode($expressions),
'features' => 'ALL',
'format' => 'GeoJSON',
);
if ($form_feature) {
$params['feature'] = json_encode($form_feature);
$params['form_scope'] = 'true';
} else {
$params['features'] = 'ALL';
}

// Request replace expression text
$json = self::request($params, $project);
Expand Down
56 changes: 56 additions & 0 deletions lizmap/modules/lizmap/lib/Form/QgisForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,45 @@ public function __construct($layer, $form, $featureId, $loginFilteredOverride, A
$privateData['qgis_groupDependencies'] = array();
}

// adding text widget fields to the form
if ($attributeEditorForm) {
$textWidgetFields = $attributeEditorForm->getTextWidgetFields();
if (count($textWidgetFields) > 0) {
foreach ($textWidgetFields as $textName => $textProp) {
// use jFormsControlOutput instance
$textCtrl = new \jFormsControlOutput($textName);
$textCtrl->label = $textProp['label'];
// add control into the form
$form->addControl($textCtrl);

// construct the from_feature array including geometry, if any, for geometry based expressions evaluation
$geom = null;
$ref = $form->getData('liz_geometryColumn');
$wkt = trim($form->getData($ref));
if ($wkt && \lizmapWkt::check($wkt)) {
$geom = \lizmapWkt::parse($wkt);
if ($geom === null) {
\jLog::log('Parsing WKT failed! '.$wkt, 'error');
}
}

$form_feature = array(
'type' => 'Feature',
'geometry' => $geom,
'properties' => $form->getAllData(),
);
// evaluate expression
$expressionT = $this->evaluateExpressionText(array($textName => $textProp['value']), $form_feature);
// expecting geojson or null
if ($expressionT && is_array($expressionT->features) && count($expressionT->features) == 1 && property_exists($expressionT->features[0], 'properties') && property_exists($expressionT->features[0]->properties, $textName)) {
$form->setData($textName, $expressionT->features[0]->properties->{$textName});
} else {
$form->setData($textName, '');
}
}
}
}

$form->getContainer()->privateData = array_merge($form->getContainer()->privateData, $privateData);
}

Expand Down Expand Up @@ -1891,4 +1930,21 @@ public function evaluateWebDavUrlExpression($fieldRef, $storageUrl, $fileName =

return null;
}

/**
* Returns the text evaluated from $expression.
*
* @param array $expression The expression to evaluate
* @param null|array $form_feature A feature to add to the evaluation context
*
* @return null|object
*/
public function evaluateExpressionText($expression, $form_feature = null)
{
return \qgisExpressionUtils::replaceExpressionText(
$this->layer,
$expression,
$form_feature
);
}
}
Loading
Loading