chg: [Decaying] Improved mapping reset and started integration with the

interface
pull/5032/head
mokaddem 2019-04-04 11:17:11 +02:00
parent 3437920fab
commit 6490b8ad56
5 changed files with 110 additions and 17 deletions

View File

@ -18,4 +18,36 @@ class DecayingModelMappingController extends AppController
return $this->RestResponse->viewData($associated_types, $this->response->type());
}
public function linkAttributeTypeToModel($model_id) {
$model = $this->DecayingModelMapping->DecayingModel->checkAuthorisation($this->Auth->user(), $model_id);
if ($model == false) {
throw new MethodNotAllowedException(_('No Decaying Model with the provided ID exists, or you are not authorised to edit it.'));
}
if ($this->request->is('post')) {
if (!isset($this->request->data['DecayingModelMapping']['model_id'])) {
$this->request->data['DecayingModelMapping']['model_id'] = $model_id;
}
if (!isset($this->request->data['DecayingModelMapping']['org_id'])) {
$this->request->data['DecayingModelMapping']['org_id'] = $this->Auth->user()['org_id'];
}
if (empty($this->request->data['DecayingModelMapping']['attributetypes'])) {
throw new MethodNotAllowedException(_("The model must link to at least one attribute type"));
} else {
$decoded = json_decode($this->request->data['DecayingModelMapping']['attributetypes'], true);
if ($decoded === null) {
throw new MethodNotAllowedException(_("Invalid JSON: attribute type"));
}
$this->request->data['DecayingModelMapping']['attribute_types'] = $decoded;
unset($this->request->data['DecayingModelMapping']['attributetypes']);
}
$response = $this->DecayingModelMapping->resetMappingForModel($this->request->data['DecayingModelMapping']);
return $this->RestResponse->viewData($response, $this->response->type());
} else {
$this->set('model_id', $model_id);
}
}
}

View File

@ -106,14 +106,16 @@ class DecayingModel extends AppModel
$existing_model = $existing_models[$new_model['uuid']];
if ($force || $new_model['version'] > $existing_model['version']) {
$new_model['id'] = $existing_model['id'];
$new_model['model_id'] = $existing_model['id'];
$this->save($new_model);
$this->DecayingModelMapping->resetMappingFromDefaultModel($new_model);
$this->DecayingModelMapping->resetMappingForModel($new_model);
}
} else {
$this->create();
$this->save($new_model);
$new_model['id'] = $this->Model->id;
$this->DecayingModelMapping->resetMappingFromDefaultModel($new_model);
$new_model['model_id'] = $this->Model->id;
$this->DecayingModelMapping->resetMappingForModel($new_model);
}
}
}

View File

@ -43,20 +43,44 @@ class DecayingModelMapping extends AppModel
}
// Delete all DEFAULT mapping associated to the model and re-create them
public function resetMappingFromDefaultModel($new_model) {
// public function resetMappingFromDefaultModel($new_model) {
// $this->deleteAll(array(
// 'DecayingModelMapping.org_id' => null,
// 'model_id' => $new_model['id']
// ));
//
// foreach ($new_model['attribute_types'] as $type) {
// $this->create();
// $to_save = array(
// 'attribute_type' => $type,
// 'model_id' => $new_model['id']
// );
// $this->save($to_save);
// }
// }
public function resetMappingForModel($new_model) {
if (!isset($new_model['org_id'])) {
$new_model['org_id'] = null;
}
$this->deleteAll(array(
'DecayingModelMapping.org_id' => null,
'model_id' => $new_model['id']
'DecayingModelMapping.org_id' => $new_model['org_id'],
'model_id' => $new_model['model_id']
));
foreach ($new_model['attribute_types'] as $type) {
$this->create();
$to_save = array(
'attribute_type' => $type,
'model_id' => $new_model['id']
'model_id' => $new_model['model_id']
);
$this->save($to_save);
if (!is_null($new_model['org_id'])) {
$to_save['org_id'] = $new_model['org_id'];
}
$data[] = $to_save;
}
$this->saveMany($data, array(
'atomic' => true
));
}
public function getAssociatedTypes($user, $model_id) {
@ -72,10 +96,7 @@ class DecayingModelMapping extends AppModel
$associated_types = $decaying_model['attribute_types'];
$temp = $this->find('list', array(
'conditions' => array(
'OR' => array(
'org_id' => $user['Organisation']['id'],
'org_id' => NULL,
),
'org_id' => array($user['Organisation']['id'], NULL),
'model_id' => $model_id
),
'recursive' => -1,

View File

@ -0,0 +1,21 @@
<div class="form">
<?php echo $this->Form->create('DecayingModelMapping');?>
<fieldset>
<legend><?php echo __('Add DecayingModelMapping');?></legend>
<?php
echo $this->Form->input('model_id', array(
'hidden' => true,
'value' => $model_id
));
echo $this->Form->input('attributetypes', array());
?>
<div class="clear"></div>
</fieldset>
<?php
echo $this->Form->button(__('Add'), array('class' => 'btn btn-primary'));
echo $this->Form->end();
?>
</div>
<?php
echo $this->element('/genericElements/SideMenu/side_menu', array('menuList' => 'decayingModel', 'menuItem' => 'add'));
?>

View File

@ -340,9 +340,9 @@
}
this.fetchFormAndSubmit($clicked, type, model_id, data);
},
fetchFormAndSubmit: function($clicked, type, model_id, formData) {
fetchFormAndSubmit: function($clicked, type, model_id, formData, url) {
var that = this;
var url = "/decayingModel/";
var url = url === undefined ? "/decayingModel/" : url;
if (type == "add") {
url += type;
} else {
@ -363,7 +363,9 @@
},
success: function(data, textStatus) {
showMessage('success', 'Network has been saved');
that.refreshRow(data);
if (url == "/decayingModel/") {
that.refreshRow(data);
}
},
error: function( jqXhr, textStatus, errorThrown ){
showMessage('fail', 'Could not save network');
@ -381,10 +383,15 @@
applyModel: function(clicked) {
var $row = $(clicked).parent().parent();
var rowData = this.getDataFromRow($row);
var selected_types = this.getSelected();
var model_id = rowData.id;
var data = { 'attributetypes': selected_types };
this.fetchFormAndSubmit($(clicked), 'linkAttributeTypeToModel', model_id, data, "/decayingModelMapping/");
// TODO: Implement
},
getDataFromRow: function($row) {
var data = {};
data.id = $row.find('td.DMId').text();
data.name = $row.find('td.DMName').text();
data.description = $row.find('td.DMDescription').text();
data.parameters = {};
@ -447,6 +454,14 @@
$cb.prop('checked', force);
}
},
getSelected: function() {
var $selected_td = $('#table_attribute_type > tbody > tr.info > td:nth-child(2)');
var selected_types = [];
$selected_td.each(function() {
selected_types.push($(this).text().trim());
});
return selected_types;
},
filterTableType: function(table, searchString) {
var $table = $(table);
var $body = $table.find('tbody');
@ -506,10 +521,12 @@
return text;
},
injectData: function($form, data) {
var prefixkey = $form.attr('action').split('/')[1].ucfirst();
Object.keys(data).forEach(function(k) {
var v = data[k];
var field = k.charAt(0).toUpperCase() + k.slice(1);
$('#DecayingModel'+field).val(v);
v = Array.isArray(v) ? JSON.stringify(v) : v;
var field = k.ucfirst();
$('#'+prefixkey+field).val(v);
});
},
simpleCompareObject: function(obj1, obj2) { // recursively compare object equality on their value