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

Qgis form refactor #1844

Merged
merged 5 commits into from
Oct 9, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ tests/qgis-projects/*
!tests/qgis-projects/.empty
assets/node_modules/
tests/js/node_modules/
lizmap/modules/lizmap/forms/*.form.json
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
12 changes: 12 additions & 0 deletions lizmap/modules/lizmap/lib/App/JelixContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,16 @@ public function getFullUrl($selector, $params = array())
{
return \jUrl::getFull($selector, $params);
}

/**
* Stores a json encoded object in a file located in var/log
* Not really a Jelix Context but I'm using it a lot and it seems like a goos place for it.
*
* @param string $file The name of the file where to put the json
* @param mixed $object The object to encode
*/
public function debugObject($file, $object)
{
file_put_contents(\jApp::varPath().'/log/'.$file.'.log', json_encode($object, JSON_PRETTY_PRINT));
}
}
136 changes: 15 additions & 121 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 All @@ -79,7 +79,6 @@ public function __construct($layer, $form, $featureId, $loginFilteredOverride, A
throw new \Exception('The layer "'.$layer->getName().'" is not an editable vector layer!');
}

$this->appContext = $appContext;
//Get the fields info
$dbFieldsInfo = $layer->getDbFieldsInfo();
// verifying db fields info
Expand All @@ -95,134 +94,29 @@ public function __construct($layer, $form, $featureId, $loginFilteredOverride, A
$this->form = $form;
$this->featureId = $featureId;
$this->loginFilteredOverride = $loginFilteredOverride;

$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
Loading