Skip to content

Commit

Permalink
Editing - JS events lizmapeditionfeaturecreated & lizmapeditionfeatur…
Browse files Browse the repository at this point in the history
…emodified return the feature primary key(s) value(s)
  • Loading branch information
mdouchin committed Nov 2, 2023
1 parent f96bd5b commit 6e2c715
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 21 deletions.
47 changes: 39 additions & 8 deletions assets/src/legacy/edition.js
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,6 @@ OpenLayers.Geometry.pointOnSegment = function(point, segment) {
layerId: editionLayer['id'],
featureId: featureId
}, function(data){

// Activate some controls
if( !editCtrls )
return false;
Expand Down Expand Up @@ -1375,7 +1374,7 @@ OpenLayers.Geometry.pointOnSegment = function(point, segment) {
var formContainer = $('#edition-form-container');
formContainer.html(data);
// the new form
var form = $('#edition-form-container form');
var form = $('#edition-form-container form#jforms_view_edition');

// Response contains a form
if ( form.length != 0 ) {
Expand Down Expand Up @@ -1570,27 +1569,59 @@ OpenLayers.Geometry.pointOnSegment = function(point, segment) {

// Else it means no form has been sent back
// We consider it was a successful edition with no option to reopen the form
// Or an error has been raised and sent back
if ( form.length == 0 ) {
controls['edition'].deactivate();
controls['edition'].activate();
var layerId = editionLayer['id'];
let layerId = editionLayer['id'];
let returnedMessage = '';
let pkVals = null;

// Get the newly created or updated feature primary key(s) value(s)
if ( data != '' ) {
// Get the hidden fields values
const hiddenForm = document.querySelector('#edition-form-container form.liz_close_feature_form');
if (hiddenForm) {
const closeFeaturePkValsJson = hiddenForm.querySelector('input#liz_close_feature_pk_vals').value;

// Parse the JSON containing the primary key(s) value(s)
try {
pkVals = JSON.parse(closeFeaturePkValsJson);
hiddenForm.parentNode.removeChild(hiddenForm);
} catch (e) {
console.error(e);
}
}

// Get the Jelix message if the response data contains one
// Only if not already taken from the hidden form
// For example when having the error "Feature not editable"
// TODO: we should not rely on this content to known when to display a message
if (data.includes('<ul class="jelix-msg">')) {
returnedMessage = data;
}
}

// Trigger event
var ev = 'lizmapeditionfeaturecreated';
if( editionType == 'modifyFeature' )
if( editionType == 'modifyFeature' ) {
ev = 'lizmapeditionfeaturemodified';
}
lizMap.events.triggerEvent(ev,
{ 'layerId': layerId}
{ 'layerId': layerId, 'primaryKeys': pkVals}
);

// Redraw layers
redrawLayers( layerId );

// Deactivate edition
finishEdition();

// Display message via JS
if ( data != '' )
addEditionMessage( data, 'info', true);
// Display the message in JS
if ( returnedMessage != '' ) {
// Display the message
addEditionMessage( returnedMessage, 'info', true);
}

}

Expand Down
29 changes: 26 additions & 3 deletions lizmap/modules/lizmap/controllers/edition.classic.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ public function saveFeature()
}
}

// Some errors where encoutered
// Some errors where encountered
if (!$check || !$pkvals) {
// Redirect to the display action
$rep->params['status'] = '1';
Expand All @@ -716,6 +716,11 @@ public function saveFeature()
// Redirect to the close action
$rep->action = 'lizmap~edition:closeFeature';

// Add the generated primary keys for the newly created feature
if (is_array($pkvals)) {
$rep->params['pkVals'] = json_encode($pkvals);
}

return $rep;
}

Expand All @@ -738,6 +743,7 @@ public function saveFeature()

return $rep;
}

// REOPEN THE FORM FOR THE EDITED FEATURE
// If there is a single integer primary key
// This is the featureid, we can redirect to the edition form
Expand Down Expand Up @@ -778,10 +784,15 @@ public function saveFeature()
/**
* Form close : destroy it and display a message.
*
* This function does not use the serviceAnswer method
* which depends on jMessage and cannot be used to return
* a more complex HTML fragment with hidden data.
*
* @urlparam string $repository Lizmap Repository
* @urlparam string $project Name of the project
* @urlparam string $layerId Qgis id of the layer
* @urlparam integer $featureId Id of the feature.
* @urlparam string $pkVals A JSON representation of the primary key values of the new or modified feature.
*
* @return jResponseHtmlFragment confirmation message that the form has been saved
*/
Expand All @@ -796,9 +807,21 @@ public function closeFeature()
jForms::destroy('view~edition', $this->featureId);

// Return html fragment response
jMessage::add(jLocale::get('view~edition.form.data.saved'), 'success');
/** @var jResponseHtmlFragment $rep */
$rep = $this->getResponse('htmlfragment');
$tpl = new jTpl();

return $this->serviceAnswer();
$closeFeatureMessage = jLocale::get('view~edition.form.data.saved');
$pkValsJson = $this->param('pkVals', '');

$tpl->assign('closeFeatureMessage', $closeFeatureMessage);
$tpl->assign('pkValsJson', $pkValsJson);
$content = $tpl->fetch('view~edition_close_feature_data');
$rep->addContent($content);

jMessage::clearAll();

return $rep;
}

/**
Expand Down
21 changes: 11 additions & 10 deletions lizmap/modules/lizmap/lib/Form/QgisForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,10 @@ public function getFormControlName($name)
}

/**
* @param $ref
* Get the storage path of QGIS form control
* for a given form input.
*
* @param string $ref Name of the form input
*
* @return null|string[]
*/
Expand Down Expand Up @@ -765,7 +768,6 @@ public function saveToDb($feature = null, $modifiedControls = array())
throw new \Exception('Save to database can\'t be done for the layer "'.$this->layer->getName().'"!');
}

$cnx = $this->layer->getDatasourceConnection();
// Update or Insert
$updateAction = false;
$insertAction = false;
Expand Down Expand Up @@ -929,14 +931,14 @@ protected function getParsedValue($ref, $geometryColumn)
}

switch ($this->formControls[$ref]->fieldDataType) {
case 'geometry':
try {
$value = $this->layer->getGeometryAsSql($value);
} catch (\Exception $e) {
$form->setErrorOn($geometryColumn, $e->getMessage());
case 'geometry':
try {
$value = $this->layer->getGeometryAsSql($value);
} catch (\Exception $e) {
$form->setErrorOn($geometryColumn, $e->getMessage());

return false;
}
return false;
}

break;

Expand Down Expand Up @@ -1238,7 +1240,6 @@ protected function fillControlFromUniqueValues($fieldName, $formControl)
*/
private function fillControlFromValueRelationLayer($fieldName, $formControl)
{

// required
if (array_key_exists('notNull', $formControl->valueRelationData)
and $formControl->valueRelationData['notNull']
Expand Down
8 changes: 8 additions & 0 deletions lizmap/modules/view/templates/edition_close_feature_data.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<ul class="jelix-msg">
<li class="jelix-msg-item-success">{$closeFeatureMessage}</li>
</ul>

<form class="liz_close_feature_form" style="display: none;">
<input type="hidden" id="liz_close_feature_message" name="liz_close_feature_message" value="{$closeFeatureMessage}">
<input type="hidden" id="liz_close_feature_pk_vals" name="liz_close_feature_pk_vals" value="{$pkValsJson|eschtml}">
</form>

0 comments on commit 6e2c715

Please sign in to comment.