new: [sighting:add] Ability to provide filtering parameters when adding sightings for specific values

Fix #7669
pull/7707/head
mokaddem 2021-08-18 14:36:34 +02:00
parent ec2cb29fe0
commit 3770ae6798
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
3 changed files with 24 additions and 11 deletions

View File

@ -68,9 +68,10 @@ class SightingsController extends AppController
}
$type = isset($this->request->data['type']) ? $this->request->data['type'] : '0';
$source = isset($this->request->data['source']) ? trim($this->request->data['source']) : '';
$filters = !empty($this->request->data['filters']) ? $this->request->data['filters'] : false;
}
if (!$error) {
$result = $this->Sighting->saveSightings($id, $values, $timestamp, $this->Auth->user(), $type, $source, false, true);
$result = $this->Sighting->saveSightings($id, $values, $timestamp, $this->Auth->user(), $type, $source, false, true, false, $filters);
}
if (!is_numeric($result)) {
$error = $result;

View File

@ -643,7 +643,7 @@ class Sighting extends AppModel
return $this->attachOrgToSightings($sightings, $user, $forSync);
}
public function saveSightings($id, $values, $timestamp, $user, $type = false, $source = false, $sighting_uuid = false, $publish = false, $saveOnBehalfOf = false)
public function saveSightings($id, $values, $timestamp, $user, $type = false, $source = false, $sighting_uuid = false, $publish = false, $saveOnBehalfOf = false, $filters=[])
{
if (!in_array($type, array(0, 1, 2))) {
return 'Invalid type, please change it before you POST 1000000 sightings.';
@ -677,18 +677,27 @@ class Sighting extends AppModel
if (!is_array($values)) {
$values = array($values);
}
foreach ($values as $value) {
foreach (array('value1', 'value2') as $field) {
$conditions['OR'][] = array(
'LOWER(Attribute.' . $field . ') LIKE' => strtolower($value)
);
if (empty($filters)) {
foreach ($values as $value) {
foreach (array('value1', 'value2') as $field) {
$conditions['OR'][] = array(
'LOWER(Attribute.' . $field . ') LIKE' => strtolower($value)
);
}
}
}
}
$attributes = $this->Attribute->fetchAttributesSimple($user, [
'conditions' => $conditions,
'fields' => ['Attribute.id', 'Attribute.event_id'],
]);
$attributes = [];
if (empty($filters)) {
$attributes = $this->Attribute->fetchAttributesSimple($user, [
'conditions' => $conditions,
'fields' => ['Attribute.id', 'Attribute.event_id'],
]);
} else {
$filters['value'] = $values;
$params = $this->Attribute->restSearch($user, 'json', $filters, true);
$attributes = $this->Attribute->fetchAttributes($user, $params);
}
if (empty($attributes)) {
return 'No valid attributes found that match the criteria.';
}

View File

@ -29,6 +29,9 @@
'div' => array('style' => 'width:120px !important;'),
'label' => false
));
echo $this->Form->input('filters', array(
'placeholder' => __('{ "to_ids": 1, "tags": ["tlp:white"] }'),
));
?>
<span id="submitButton" role="button" tabindex="0" aria-label="<?php echo __('Add sighting');?>" title="<?php echo __('Add sighting');?>" class="btn btn-primary" onClick="submitPopoverForm('<?php echo h($id);?>', 'addSighting', '<?php echo h($event_id); ?>')"><?php echo __('Add');?></span>
<div class="input clear"></div>