Add dynamic deletion of contacts

This commit is contained in:
osaajani 2021-04-24 00:10:23 +02:00
parent bd57db4fdd
commit 8315817388
6 changed files with 120 additions and 3 deletions

View File

@ -442,3 +442,21 @@ footer img
{
margin-bottom: 30px;
}
/**
* Contacts conditional deletion
*/
.conditional-deletion-preview-container
{
margin-top: 10px;
margin-bottom: 20px;
text-align: right;
}
.conditional-deletion-preview-container label,
.conditional-deletion-preview-container select
{
display: inline-block;
width: auto;
}

View File

@ -234,7 +234,17 @@ namespace controllers\publics;
$contacts_name[] = $contact['name'];
}
$return['result'] = 'Contacts du groupe : ' . implode(', ', $contacts_name);
$visible_names = array_slice($contacts_name, 0, 5);
$how_many_more = count($contacts_name) - count($visible_names);
$result_text = 'Contacts du groupe : ' . implode(', ', $visible_names);
if ($how_many_more > 0)
{
$result_text .= ", et $how_many_more autres.";
}
$return['result'] = $result_text;
$return['success'] = true;
echo json_encode($return);

View File

@ -18,6 +18,7 @@ namespace controllers\publics;
{
private $internal_contact;
private $internal_event;
private $internal_conditional_group;
/**
* Cette fonction est appelée avant toute les autres :
@ -31,6 +32,7 @@ namespace controllers\publics;
$this->internal_contact = new \controllers\internals\Contact($bdd);
$this->internal_event = new \controllers\internals\Event($bdd);
$this->internal_conditional_group = new \controllers\internals\ConditionalGroup($bdd);
\controllers\internals\Tool::verifyconnect();
}
@ -83,6 +85,40 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
/**
* This function will delete a list of contacts depending on a condition
*
* @param string $_POST['condition'] : Condition to use to delete contacts
* @param mixed $csrf
*
* @return boolean;
*/
public function conditional_delete($csrf)
{
if (!$this->verify_csrf($csrf))
{
\FlashMessage\FlashMessage::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
$condition = $_POST['condition'] ?? false;
if (!$condition)
{
\FlashMessage\FlashMessage::push('danger', 'Vous devez fournir une condition !');
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
$contacts_to_delete = $this->internal_conditional_group->get_contacts_for_condition_and_user($_SESSION['user']['id'], $condition);
foreach ($contacts_to_delete as $contact)
{
$this->internal_contact->delete_for_user($_SESSION['user']['id'], $contact['id']);
}
return $this->redirect(\descartes\Router::url('Contact', 'list'));
}
/**
* Cette fonction retourne la page d'ajout d'un contact.

View File

@ -43,6 +43,7 @@
'update' => '/contact/update/{csrf}/',
'import' => '/contact/import/{csrf}/',
'export' => '/contact/export/{format}/',
'conditional_delete' => '/contact/conditional_delete/{csrf}/',
'json_list' => '/contacts.json/',
],

View File

@ -48,7 +48,7 @@
<div class="form-group">
<label>Condition</label>
<p class="italic small help">
Les conditions vous permettent de définir dynamiquement les contacts qui appartiennent au groupe en utilisant leurs données additionnelles. Pour plus d'informations consultez la documentation relative à <a href="#">l'utilisation des groupes conditionnels.</a><br/>
Les conditions vous permettent de définir dynamiquement les contacts qui appartiennent au groupe en utilisant leurs données additionnelles. Pour plus d'informations consultez la documentation relative à <a href="https://documentation.raspisms.fr/users/groups_and_contacts/conditionnals_groups.html" target="_blank">l'utilisation des groupes conditionnels.</a><br/>
Vous pouvez prévisualiser les contacts qui feront parti du groupe en cliquant sur le bouton <b>"Prévisualiser les contacts"</b>.
</p>
<input class="form-control" name="condition" placeholder="Ex : contact.gender == 'male'" value="<?php $this->s($_SESSION['previous_http_post']['condition'] ?? '') ?>"/>

View File

@ -14,7 +14,8 @@
<div class="col-lg-12">
<h1 class="page-header">
Dashboard <small>Contacts</small>
<a class="btn btn-info float-right" id="btn-export" href="#"><span class="fa fa-upload"></span> Exporter la liste des contacts</a>
<a class="btn btn-info float-right" id="btn-conditional-deletion" href="#"><span class="fa fa-trash-o"></span> Supprimer une liste dynamique de contacts</a>
<a class="btn btn-info float-right" id="btn-export" href="#" style="margin-right: 10px;"><span class="fa fa-upload"></span> Exporter la liste des contacts</a>
<a class="btn btn-info float-right" id="btn-import" href="#" style="margin-right: 10px;"><span class="fa fa-download"></span> Importer une liste de contacts</a>
</h1>
<ol class="breadcrumb">
@ -112,6 +113,32 @@
</div>
</div>
</div>
<div class="modal fade" tabindex="-1" id="conditional-deletion-modal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<form action="<?php $this->s(\descartes\Router::url('Contact', 'conditional_delete', ['csrf' => $_SESSION['csrf']])); ?>" method="POST" enctype="multipart/form-data">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">Supprimer des contacts de façon conditionnelle</h4>
</div>
<div class="modal-body text-center">
<div class="form-group">
<p>Vous pouvez supprimer une liste dynamique de contacts, construite selon des règles basées sur les données de ces contacts. Pour plus d'informations consultez la documentation relative à <a href="https://documentation.raspisms.fr/users/groups_and_contacts/conditionnals_groups.html" target="_blank">l'utilisation des groupes conditionnels.</a><br/></p>
<input class="form-control" name="condition" placeholder="Ex : contact.gender == 'male'" required/>
<div class="conditional-deletion-preview-container">
<div class="conditional-deletion-preview-text"></div>
<a class="btn btn-info conditional-deletion-preview-button" href="#">Prévisualiser les contacts qui seront supprimés</a>
</div>
</div>
</div>
<div class="modal-footer">
<a type="button" class="btn btn-danger" data-dismiss="modal">Annuler</a>
<button class="btn btn-default btn-confirm" type="submit"><span class="fa fa-trash-o"></span> Supprimer les contacts</button>
</div>
</form>
</div>
</div>
</div>
<script>
jQuery(document).ready(function()
{
@ -126,6 +153,31 @@ jQuery(document).ready(function()
jQuery('#export-modal').modal({'keyboard': true});
});
jQuery('body').on('click', '#btn-conditional-deletion', function ()
{
jQuery('#conditional-deletion-modal').modal({'keyboard': true});
});
jQuery('body').on('click', '.conditional-deletion-preview-button', function (e)
{
e.preventDefault();
var condition = jQuery(this).parents('.form-group').find('input').val();
var data = {
'condition' : condition,
};
jQuery.ajax({
type: "POST",
url: HTTP_PWD + '/conditional_group/preview/',
data: data,
success: function (data) {
jQuery('.conditional-deletion-preview-text').text(data.result);
},
dataType: 'json'
});
});
//Datatable
jQuery('.datatable').DataTable({