Skip to content

Commit

Permalink
Merge pull request #1844 from alagroy-42/qgisFormRefactor
Browse files Browse the repository at this point in the history
Qgis form refactor
  • Loading branch information
laurentj committed Oct 9, 2020
2 parents 651ff30 + a0cc73d commit bc36c12
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 280 deletions.
5 changes: 4 additions & 1 deletion lizmap/modules/lizmap/classes/qgisForm.listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*
* @license Mozilla Public License : http://www.mozilla.org/MPL/
*/

use Lizmap\Form;

class qgisFormListener extends jEventListener
{
public function onjformsPrepareToFillDynamicList($event)
Expand Down Expand Up @@ -44,6 +47,6 @@ public function onjformsPrepareToFillDynamicList($event)
return;
}

$qgisForm = new qgisForm($layer, $form, $featureId, jAcl2::check('lizmap.tools.loginFilteredLayers.override', $lrep->getKey()));
$qgisForm = new Form\QgisForm($layer, $form, $featureId, jAcl2::check('lizmap.tools.loginFilteredLayers.override', $lrep->getKey()), lizmap::getAppContext());
}
}
13 changes: 8 additions & 5 deletions lizmap/modules/lizmap/controllers/edition.classic.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*
* @license Mozilla Public License : http://www.mozilla.org/MPL/
*/

use Lizmap\Form;

class editionCtrl extends jController
{
/** @var null|lizmapProject */
Expand Down Expand Up @@ -438,7 +441,7 @@ public function editFeature()
$qgisForm = null;

try {
$qgisForm = new qgisForm($this->layer, $form, $this->featureId, $this->loginFilteredOverride);
$qgisForm = new Form\QgisForm($this->layer, $form, $this->featureId, $this->loginFilteredOverride, lizmap::getAppContext());
} catch (Exception $e) {
jMessage::add($e->getMessage(), 'error');

Expand Down Expand Up @@ -618,7 +621,7 @@ public function saveFeature()
$qgisForm = null;

try {
$qgisForm = new qgisForm($this->layer, $form, $this->featureId, $this->loginFilteredOverride);
$qgisForm = new Form\QgisForm($this->layer, $form, $this->featureId, $this->loginFilteredOverride, lizmap::getAppContext());
} catch (Exception $e) {
jMessage::add($e->getMessage(), 'error');

Expand Down Expand Up @@ -833,7 +836,7 @@ public function deleteFeature()
$qgisForm = null;

try {
$qgisForm = new qgisForm($this->layer, $form, $this->featureId, $this->loginFilteredOverride);
$qgisForm = new Form\QgisForm($this->layer, $form, $this->featureId, $this->loginFilteredOverride, lizmap::getAppContext());
} catch (Exception $e) {
jMessage::add($e->getMessage(), 'error');

Expand Down Expand Up @@ -949,7 +952,7 @@ public function saveNewFeature()
$qgisForm = null;

try {
$qgisForm = new qgisForm($this->layer, $form, $this->featureId, $this->loginFilteredOverride);
$qgisForm = new Form\QgisForm($this->layer, $form, $this->featureId, $this->loginFilteredOverride, lizmap::getAppContext());
} catch (Exception $e) {
$rep->data['success'] = false;
$rep->data['message'] = $e->getMessage();
Expand Down Expand Up @@ -1314,7 +1317,7 @@ public function getGroupVisibilities()
$lproj = lizmap::getProject($repository.'~'.$project);
$layer = $lproj->getLayer($layerId);

$qgisForm = new qgisForm($layer, $form, $featureId, jAcl2::check('lizmap.tools.loginFilteredLayers.override', $lrep->getKey()));
$qgisForm = new Form\QgisForm($layer, $form, $featureId, jAcl2::check('lizmap.tools.loginFilteredLayers.override', $lrep->getKey()), lizmap::getAppContext());

// Update form
$dependencies = $privateData['qgis_groupDependencies'];
Expand Down
133 changes: 14 additions & 119 deletions lizmap/modules/lizmap/lib/Form/QgisForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class QgisForm implements QgisFormControlsInterface
{
/**
* @var qgisMapLayer|qgisVectorLayer
* @var \qgisMapLayer|\qgisVectorLayer
*/
protected $layer;

Expand Down Expand Up @@ -63,10 +63,10 @@ class QgisForm implements QgisFormControlsInterface
/**
* qgisForm constructor.
*
* @param qgisMapLayer|qgisVectorLayer $layer
* @param \jFormsBase $form
* @param string $featureId
* @param bool $loginFilteredOverride
* @param \qgisMapLayer|\qgisVectorLayer $layer
* @param \jFormsBase $form
* @param string $featureId
* @param bool $loginFilteredOverride
*
* @throws \Exception
*/
Expand Down Expand Up @@ -97,131 +97,26 @@ public function __construct($layer, $form, $featureId, $loginFilteredOverride, A
$this->appContext = $appContext;
$this->dbFieldsInfo = $dbFieldsInfo;

$layerXml = $layer->getXmlLayer();

$edittypesXml = $layerXml->edittypes;
if ($edittypesXml && count($edittypesXml) !== 0) {
$edittypesXml = $edittypesXml[0];
} else {
$edittypesXml = null;
}

$fieldConfigurationXml = $layerXml->fieldConfiguration;
if ($fieldConfigurationXml && count($fieldConfigurationXml) !== 0) {
$fieldConfigurationXml = $fieldConfigurationXml[0];
} else {
$fieldConfigurationXml = null;
}

$categoriesXml = $layerXml->xpath('renderer-v2/categories');
if ($categoriesXml && count($categoriesXml) != 0) {
$categoriesXml = $categoriesXml[0];
} else {
$categoriesXml = null;
}

$eCapabilities = $layer->getEditionCapabilities();
$capabilities = $eCapabilities->capabilities;
$aliases = $layer->getAliasFields();
$dataFields = $dbFieldsInfo->dataFields;
$toDeactivate = array();
$toSetReadOnly = array();
$json = file_get_contents(realpath(__DIR__.'/../..').'/forms/'.$layer->getProject()->getKey().'.'.$layer->getId().'.form.json');
$formInfos = json_decode($json);
foreach ($dataFields as $fieldName => $prop) {
// get field edit type
$edittype = null;
if ($edittypesXml) {
$edittype = $edittypesXml->xpath("edittype[@name='${fieldName}']");
if ($edittype && count($edittype) != 0) {
$edittype = $edittype[0];
} else {
$edittype = null;
}
} elseif ($fieldConfigurationXml) {
$fieldConfiguration = $fieldConfigurationXml->xpath("field[@name='${fieldName}']");
if ($fieldConfiguration && count($fieldConfiguration) !== 0) {
$fieldConfiguration = $fieldConfiguration[0];
$editWidgetXml = $fieldConfiguration->editWidget;
if ($editWidgetXml && count($editWidgetXml) !== 0) {
$editWidgetXml = $editWidgetXml[0];
// type
$fieldEditType = (string) $editWidgetXml->attributes()->type;
// options
$fieldEditOptions = array();
foreach ($editWidgetXml->config as $config) {
foreach ($config->Option as $option) {
foreach ($option->Option as $opt) {
// Option with list of values
if ((string) $opt->attributes()->type === 'List') {
$values = array();
foreach ($opt->Option as $l) {
if ((string) $l->attributes()->type === 'Map') {
foreach ($l->Option as $v) {
$values[] = (object) array(
'key' => (string) $v->attributes()->name,
'value' => (string) $v->attributes()->value,
);
}
} else {
$values[] = (string) $l->attributes()->value;
}
}
$fieldEditOptions[(string) $opt->attributes()->name] = $values;
// Option with list of values as Map
} elseif ((string) $opt->attributes()->type === 'Map') {
$values = array();
foreach ($opt->Option as $v) {
$values[] = (object) array(
'key' => (string) $v->attributes()->name,
'value' => (string) $v->attributes()->value,
);
}
$fieldEditOptions[(string) $opt->attributes()->name] = $values;
// Option with string list of values
} elseif ((string) $opt->attributes()->type === 'StringList') {
$values = array();
foreach ($opt->Option as $v) {
$values[] = (string) $v->attributes()->value;
}
$fieldEditOptions[(string) $opt->attributes()->name] = $values;
// Simple option
} else {
$fieldEditOptions[(string) $opt->attributes()->name] = (string) $opt->attributes()->value;
}
}
}
}

$edittype = array(
'type' => $fieldEditType,
'options' => (object) $fieldEditOptions,
);

// editable
$editableFieldXml = $layerXml->xpath("editable/field[@name='${fieldName}']");
if ($editableFieldXml && count($editableFieldXml) !== 0) {
$editableFieldXml = $editableFieldXml[0];
$edittype['editable'] = (int) $editableFieldXml->attributes()->editable;
} else {
$edittype['editable'] = 1;
}

$edittype = (object) $edittype;
}
} else {
$edittype = null;
}
}
// get field alias
$alias = null;
if ($aliases and array_key_exists($fieldName, $aliases)) {
$alias = $aliases[$fieldName];
}

// faire qqch pour la geometry
$defaultValue = $this->getDefaultValue($fieldName);

$constraints = $this->getConstraints($fieldName);

$formControl = new qgisFormControl($fieldName, $edittype, $prop, $alias, $defaultValue, $constraints, $categoriesXml, $this->appContext);
if (property_exists($formInfos, $fieldName)) {
$formControl = new QgisFormControl($fieldName, $formInfos->{$fieldName}, $prop, $defaultValue, $constraints, $this->appContext);
} else {
// The geometry field is not present in the .XML
$formControl = new QgisFormControl($fieldName, null, $prop, null, $constraints, $this->appContext);
}

if ($formControl->isUniqueValue()) {
$this->fillControlFromUniqueValues($fieldName, $formControl);
Expand Down
93 changes: 12 additions & 81 deletions lizmap/modules/lizmap/lib/Form/QgisFormControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,23 @@ class QgisFormControl
* @param string $defaultValue the QGIS expression of the default value
* @param array $constraints the QGIS constraints
* @param object $rendererCategories simplexml object corresponding to the QGIS categories of the renderer
* @param mixed $properties
*/
public function __construct($ref, $edittype, $prop, $aliasXml = null, $defaultValue = null, $constraints = null, $rendererCategories = null, App\AppContextInterface $appContext)
public function __construct($ref, $properties, $prop, $defaultValue = null, $constraints = null, App\AppContextInterface $appContext)
{
// Set class attributes
$this->ref = $ref;
$this->fieldName = $ref;
$this->appContext = $appContext;
if (is_string($aliasXml)) {
$this->fieldAlias = $aliasXml;
} elseif ($aliasXml and is_array($aliasXml) and count($aliasXml) != 0) {
$this->fieldAlias = (string) $aliasXml[0]->attributes()->name;
} elseif ($aliasXml and count($aliasXml) != 0) {
$this->fieldAlias = $aliasXml;
}
$this->fieldDataType = $this->castDataType[strtolower($prop->type)];

$this->defaultValue = $defaultValue;
$propTab = array('edittype', 'fieldEditType', 'fieldAlias', 'widgetv2configAttr');

foreach ($propTab as $elem) {
if ($properties && property_exists($properties, $elem)) {
$this->{$elem} = $properties->{$elem};
}
}

if (!self::$qgisEdittypeMap['builded']) {
self::buildEditTypeMap();
Expand All @@ -224,79 +224,10 @@ public function __construct($ref, $edittype, $prop, $aliasXml = null, $defaultVa
$this->required = false;
}

if ($this->fieldDataType != 'geometry') {
$this->edittype = $edittype;
$this->rendererCategories = $rendererCategories;

// Get qgis edittype data
if ($this->edittype && ($this->edittype instanceof \SimpleXMLElement)) {
// New QGIS 2.4 edittypes : use widgetv2type property
if (property_exists($this->edittype->attributes(), 'widgetv2type')) {
$this->widgetv2configAttr = $this->edittype->widgetv2config->attributes();
$this->fieldEditType = (string) $this->edittype->attributes()->widgetv2type;
}
// Before QGIS 2.4
else {
$this->fieldEditType = (int) $this->edittype->attributes()->type;
}
} elseif ($this->edittype && is_object($this->edittype)) {
$this->widgetv2configAttr = $this->edittype->options;
$this->fieldEditType = $this->edittype->type;
} else {
$this->fieldEditType = 0;
}

// Get jform control type
if ($this->fieldEditType === 12) {
$useHtml = 0;
if (property_exists($this->edittype->attributes(), 'UseHtml')) {
$useHtml = (int) filter_var((string) $this->edittype->attributes()->UseHtml, FILTER_VALIDATE_BOOLEAN);
}
$markup = self::$qgisEdittypeMap[$this->fieldEditType]['jform']['markup'][$useHtml];
} elseif ($this->fieldEditType === 'TextEdit') {
$isMultiLine = false;
if (property_exists($this->widgetv2configAttr, 'IsMultiline')) {
$isMultiLine = filter_var((string) $this->widgetv2configAttr->IsMultiline, FILTER_VALIDATE_BOOLEAN);
}

if (!$isMultiLine) {
$this->fieldEditType = 'LineEdit';
$markup = self::$qgisEdittypeMap[$this->fieldEditType]['jform']['markup'];
} else {
$useHtml = 0;
if (property_exists($this->widgetv2configAttr, 'UseHtml')) {
$useHtml = (int) filter_var((string) $this->widgetv2configAttr->UseHtml, FILTER_VALIDATE_BOOLEAN);
}
$markup = self::$qgisEdittypeMap[$this->fieldEditType]['jform']['markup'][$useHtml];
}
} elseif ($this->fieldEditType === 5) {
$markup = self::$qgisEdittypeMap[$this->fieldEditType]['jform']['markup'][0];
} elseif ($this->fieldEditType === 15) {
$allowMulti = (int) filter_var((string) $this->edittype->attributes()->allowMulti, FILTER_VALIDATE_BOOLEAN);
$markup = self::$qgisEdittypeMap[$this->fieldEditType]['jform']['markup'][$allowMulti];
} elseif ($this->fieldEditType === 'Range' || $this->fieldEditType === 'EditRange') {
$markup = self::$qgisEdittypeMap[$this->fieldEditType]['jform']['markup'][0];
} elseif ($this->fieldEditType === 'SliderRange' || $this->fieldEditType === 'DialRange') {
$markup = self::$qgisEdittypeMap[$this->fieldEditType]['jform']['markup'][1];
} elseif ($this->fieldEditType === 'ValueRelation') {
$allowMulti = (int) filter_var((string) $this->widgetv2configAttr->AllowMulti, FILTER_VALIDATE_BOOLEAN);
$markup = self::$qgisEdittypeMap[$this->fieldEditType]['jform']['markup'][$allowMulti];
} elseif ($this->fieldEditType === 'DateTime') {
$markup = 'date';
$display_format = $this->widgetv2configAttr->display_format;
// Use date AND time widget id type is DateTime and we find HH
if (preg_match('#HH#i', $display_format)) {
$markup = 'datetime';
}
// Use only time if field is only time
if (preg_match('#HH#i', $display_format) and !preg_match('#YY#i', $display_format)) {
$markup = 'time';
}
} else {
$markup = self::$qgisEdittypeMap[$this->fieldEditType]['jform']['markup'];
}
} else {
if ($this->fieldDataType == 'geometry') {
$markup = 'hidden';
} else {
$markup = $properties->markup;
}

// Create the control
Expand Down
Loading

0 comments on commit bc36c12

Please sign in to comment.