Finish add conditional groups

This commit is contained in:
osaajani 2019-11-28 01:55:11 +01:00
parent f4bbfa0152
commit 47b26e3cd2
15 changed files with 273 additions and 67 deletions

View File

@ -109,26 +109,18 @@ namespace controllers\internals;
* Gets the user's contacts that respects a condition
* @param int $id_user : User id
* @param string $condition : Condition string to verify
* @return bool
* @return array
*/
public function get_contacts_for_condition_and_user (int $id_user, string $condition) : bool
public function get_contacts_for_condition_and_user (int $id_user, string $condition) : array
{
$internal_contacts = new Contacts($this->bdd);
$internal_contacts = new Contact($this->bdd);
$contacts = $internal_contacts->gets_for_user($id_user);
$ruler = new Ruler();
foreach ($contacts as $key => $contact)
{
if ($contact['datas'] != null)
{
$contact['datas'] = json_decode($contact['datas']);
}
else
{
$contact['datas'] = new \stdClass();
}
$contact['datas'] = json_decode($contact['datas']);
$contact = (object) $contact;
$datas = ['contact' => $contact];

View File

@ -68,7 +68,7 @@ namespace controllers\internals;
* @param string $datas : Contact datas
* @return mixed bool|int : False if cannot create contact, id of the new contact else
*/
public function create($id_user, $number, $name, ?string $datas = null)
public function create($id_user, $number, $name, $datas)
{
$contact = [
'id_user' => $id_user,
@ -99,7 +99,7 @@ namespace controllers\internals;
* @param ?string $datas : Contact datas
* @return int : number of modified rows
*/
public function update_for_user(int $id_user, int $id, string $number, string $name, ?string $datas)
public function update_for_user(int $id_user, int $id, string $number, string $name, string $datas)
{
$contact = [
'number' => $number,

View File

@ -47,8 +47,6 @@ use Symfony\Component\ExpressionLanguage\ExpressionFunction;
}
catch (\Exception $e)
{
echo "Error : ";
echo $e->getMessage();
return false;
}
}

View File

@ -35,9 +35,10 @@ namespace controllers\internals;
* @param array $numbers : Numbers to send message to
* @param array $contacts_ids : Contact ids to send message to
* @param array $groups_ids : Group ids to send message to
* @param array $conditional_group_ids : Conditional Groups ids to send message to
* @return bool : false on error, new id on success
*/
public function create (int $id_user, $at, string $text, ?string $origin = null, bool $flash = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [])
public function create (int $id_user, $at, string $text, ?string $origin = null, bool $flash = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [])
{
$scheduled = [
'id_user' => $id_user,
@ -96,6 +97,18 @@ namespace controllers\internals;
$this->get_model()->insert_scheduled_group_relation($id_scheduled, $group_id);
}
$internal_conditional_group = new ConditionalGroup($this->bdd);
foreach ($conditional_group_ids as $conditional_group_id)
{
$find_group = $internal_conditional_group->get_for_user($id_user, $conditional_group_id);
if (!$find_group)
{
continue;
}
$this->get_model()->insert_scheduled_conditional_group_relation($id_scheduled, $conditional_group_id);
}
return $id_scheduled;
}
@ -112,9 +125,10 @@ namespace controllers\internals;
* @param array $numbers : Numbers to send message to
* @param array $contacts_ids : Contact ids to send message to
* @param array $groups_ids : Group ids to send message to
* @param array $conditional_group_ids : Conditional Groups ids to send message to
* @return bool : false on error, new id on success
*/
public function update_for_user (int $id_user, int $id_scheduled, $at, string $text, ?string $origin = null, bool $flash = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [])
public function update_for_user (int $id_user, int $id_scheduled, $at, string $text, ?string $origin = null, bool $flash = false, array $numbers = [], array $contacts_ids = [], array $groups_ids = [], array $conditional_group_ids = [])
{
$scheduled = [
'id_user' => $id_user,
@ -141,6 +155,7 @@ namespace controllers\internals;
$this->get_model()->delete_scheduled_numbers($id_scheduled);
$this->get_model()->delete_scheduled_contact_relations($id_scheduled);
$this->get_model()->delete_scheduled_group_relations($id_scheduled);
$this->get_model()->delete_scheduled_conditional_group_relations($id_scheduled);
foreach ($numbers as $number)
{
@ -170,6 +185,18 @@ namespace controllers\internals;
$this->get_model()->insert_scheduled_group_relation($id_scheduled, $group_id);
}
$internal_conditional_group = new ConditionalGroup($this->bdd);
foreach ($conditional_group_ids as $conditional_group_id)
{
$find_group = $internal_conditional_group->get_for_user($id_user, $conditional_group_id);
if (!$find_group)
{
continue;
}
$this->get_model()->insert_scheduled_conditional_group_relation($id_scheduled, $conditional_group_id);
}
return true;
}
@ -219,4 +246,15 @@ namespace controllers\internals;
{
return $this->get_model()->get_groups($id_scheduled);
}
/**
* Return conditional groups for a scheduled message
* @param int $id_scheduled : Scheduled id
* @return array
*/
public function get_conditional_groups(int $id_scheduled)
{
return $this->get_model()->get_conditional_groups($id_scheduled);
}
}

View File

@ -178,4 +178,67 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('ConditionalGroup', 'list'));
}
/**
* Try to get the preview of contacts for a conditionnal group
* @param string $_POST['condition'] : Condition to apply
* @return json string
*/
public function contacts_preview ()
{
$return = [
'success' => false,
'result' => 'Une erreur inconnue est survenue.',
];
$condition = $_POST['condition'] ?? false;
if (!$condition)
{
$return['result'] = 'Vous devez renseigner une condition.';
echo json_encode($return);
return false;
}
$internal_ruler = new \controllers\internals\Ruler();
$valid_condition = $internal_ruler->validate_condition($condition, ['contact' => (object) ['datas' => (object) null]]);
if (!$valid_condition)
{
$return['result'] = 'Syntaxe de la condition invalide.';
echo json_encode($return);
return false;
}
$contacts = $this->internal_conditional_group->get_contacts_for_condition_and_user($_SESSION['user']['id'], $condition);
if (!$contacts)
{
$return['result'] = 'Aucun contact dans le groupe.';
echo json_encode($return);
return false;
}
$contacts_name = [];
foreach ($contacts as $contact)
{
$contacts_name[] = $contact['name'];
}
$return['result'] = "Contacts du groupe : " . implode(', ', $contacts_name);
$return['success'] = true;
echo json_encode($return);
return true;
}
/**
* Return the list of groups as JSON
*/
public function json_list()
{
header('Content-Type: application/json');
echo json_encode($this->internal_conditional_group->list_for_user($_SESSION['user']['id']));
}
}

View File

@ -148,27 +148,19 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('Contact', 'add'));
}
$clean_datas = null;
if ($datas)
$clean_datas = [];
foreach ($datas as $key => $value)
{
$clean_datas = [];
foreach ($datas as $key => $value)
if ($value === "")
{
if ($value === "")
{
continue;
}
$key = mb_ereg_replace('[\W]', '', $key);
$clean_datas[$key] = (string) $value;
continue;
}
$key = mb_ereg_replace('[\W]', '', $key);
$clean_datas[$key] = (string) $value;
}
$clean_datas = $clean_datas ?: null;
if ($clean_datas)
{
$clean_datas = json_encode($clean_datas);
}
$clean_datas = json_encode($clean_datas);
if (!$this->internal_contact->create($id_user, $number, $name, $clean_datas))
{
@ -210,7 +202,7 @@ namespace controllers\publics;
$name = $contact['name'] ?? false;
$number = $contact['number'] ?? false;
$id_user = $_SESSION['user']['id'];
$datas = $contact['datas'] ?? null;
$datas = $contact['datas'] ?? [];
if (!$name || !$number)
{
@ -223,28 +215,18 @@ namespace controllers\publics;
continue;
}
$clean_datas = null;
if ($datas)
$clean_datas = [];
foreach ($datas as $key => $value)
{
$clean_datas = [];
foreach ($datas as $key => $value)
if ($value === "")
{
if ($value === "")
{
continue;
}
$key = mb_ereg_replace('[\W]', '', $key);
$clean_datas[$key] = (string) $value;
continue;
}
}
$clean_datas = $clean_datas ?: null;
if ($clean_datas)
{
$clean_datas = json_encode($clean_datas);
}
$key = mb_ereg_replace('[\W]', '', $key);
$clean_datas[$key] = (string) $value;
}
$clean_datas = json_encode($clean_datas);
$nb_contacts_update += (int) $this->internal_contact->update_for_user($id_user, $id_contact, $number, $name, $clean_datas);
}

View File

@ -129,6 +129,7 @@ namespace controllers\publics;
$scheduleds[$key]['numbers'] = [];
$scheduleds[$key]['contacts'] = [];
$scheduleds[$key]['groups'] = [];
$scheduleds[$key]['conditional_groups'] = [];
$numbers = $this->internal_scheduled->get_numbers($scheduled['id']);
foreach ($numbers as $number)
@ -147,6 +148,12 @@ namespace controllers\publics;
{
$scheduleds[$key]['groups'][] = (int) $group['id'];
}
$conditional_groups = $this->internal_scheduled->get_conditional_groups($scheduled['id']);
foreach ($conditional_groups as $conditional_group)
{
$scheduleds[$key]['conditional_groups'][] = (int) $conditional_group['id'];
}
}
$this->render('scheduled/edit', [
@ -183,6 +190,7 @@ namespace controllers\publics;
$numbers = $_POST['numbers'] ?? [];
$contacts = $_POST['contacts'] ?? [];
$groups = $_POST['groups'] ?? [];
$conditional_groups = $_POST['conditional_groups'] ?? [];
if (empty($text))
{
@ -212,7 +220,7 @@ namespace controllers\publics;
$numbers[$key] = $number;
}
if (!$numbers && !$contacts && !$groups)
if (!$numbers && !$contacts && !$groups && !$conditional_groups)
{
\FlashMessage\FlashMessage::push('danger', 'Vous devez renseigner au moins un destinataire pour le Sms.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
@ -226,7 +234,7 @@ namespace controllers\publics;
}
$scheduled_id = $this->internal_scheduled->create($id_user, $at, $text, $origin, $flash, $numbers, $contacts, $groups);
$scheduled_id = $this->internal_scheduled->create($id_user, $at, $text, $origin, $flash, $numbers, $contacts, $groups, $conditional_groups);
if (!$scheduled_id)
{
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer le Sms.');
@ -269,6 +277,7 @@ namespace controllers\publics;
$numbers = $scheduled['numbers'] ?? [];
$contacts = $scheduled['contacts'] ?? [];
$groups = $scheduled['groups'] ?? [];
$conditional_groups = $scheduled['conditional_groups'] ?? [];
$scheduled = $this->internal_scheduled->get($id_scheduled);
if (!$scheduled || $scheduled['id_user'] !== $id_user)
@ -305,7 +314,7 @@ namespace controllers\publics;
$numbers[$key] = $number;
}
if (!$numbers && !$contacts && !$groups)
if (!$numbers && !$contacts && !$groups && !$conditional_groups)
{
$all_update_ok = false;
@ -319,7 +328,7 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
$success = $this->internal_scheduled->update_for_user($id_user, $id_scheduled, $at, $text, $origin, $flash, $numbers, $contacts, $groups);
$success = $this->internal_scheduled->update_for_user($id_user, $id_scheduled, $at, $text, $origin, $flash, $numbers, $contacts, $groups, $conditional_groups);
if (!$success)
{
$all_update_ok = false;

View File

@ -62,10 +62,7 @@ namespace controllers\publics;
return false;
}
if ($contact['datas'])
{
$contact['datas'] = json_decode($contact['datas'], true);
}
$contact['datas'] = json_decode($contact['datas'], true);
$datas = [
'contact' => $contact,

View File

@ -66,7 +66,7 @@ CREATE TABLE IF NOT EXISTS contact
id_user INT NOT NULL,
name VARCHAR(100) NOT NULL,
number VARCHAR(20) NOT NULL,
datas TEXT DEFAULT NULL,
datas TEXT NOT NULL,
CHECK (JSON_VALID(datas)),
PRIMARY KEY (id),
FOREIGN KEY (id_user) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE,
@ -124,6 +124,16 @@ CREATE TABLE IF NOT EXISTS scheduled_group
FOREIGN KEY (id_group) REFERENCES `group` (id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS scheduled_conditional_group
(
id INT NOT NULL AUTO_INCREMENT,
id_scheduled INT NOT NULL,
id_conditional_group INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (id_scheduled) REFERENCES scheduled (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (id_conditional_group) REFERENCES `conditional_group` (id) ON DELETE CASCADE ON UPDATE CASCADE
);
CREATE TABLE IF NOT EXISTS scheduled_number
(
id INT NOT NULL AUTO_INCREMENT,

View File

@ -55,6 +55,19 @@ namespace models;
$params = ['id_scheduled' => $id_scheduled];
return $this->_run_query($query, $params);
}
/**
* Return conitional groups for a scheduled message
* @param int $id_scheduled : Scheduled id
* @return array
*/
public function get_conditional_groups(int $id_scheduled)
{
$query = 'SELECT * FROM `conditional_group` WHERE id IN (SELECT id_conditional_group FROM scheduled_conditional_group WHERE id_scheduled = :id_scheduled)';
$params = ['id_scheduled' => $id_scheduled];
return $this->_run_query($query, $params);
}
/**
@ -96,6 +109,19 @@ namespace models;
}
/**
* Insert a relation between a scheduled and a conditional group
* @param int $id_scheduled : Scheduled id
* @param int $id_conditional_group : Group id
* @return mixed (bool|int) : False on error, new row id else
*/
public function insert_scheduled_conditional_group_relation(int $id_scheduled, int $id_conditional_group)
{
$success = $this->_insert('scheduled_conditional_group', ['id_scheduled' => $id_scheduled, 'id_conditional_group' => $id_conditional_group]);
return ($success ? $this->_last_id() : false);
}
/**
* Delete numbers for a scheduled
* @param int $id_scheduled : Scheduled id
@ -127,6 +153,17 @@ namespace models;
{
return $this->_delete('scheduled_group', ['id_scheduled' => $id_scheduled]);
}
/**
* Delete conditional group scheduled relations for a scheduled
* @param int $id_scheduled : Scheduled id
* @return mixed int : Number of deleted rows
*/
public function delete_scheduled_conditional_group_relations(int $id_scheduled)
{
return $this->_delete('scheduled_conditional_group', ['id_scheduled' => $id_scheduled]);
}
/**

View File

@ -88,6 +88,8 @@
'delete' => '/conditional_group/delete/{csrf}/',
'edit' => '/conditional_group/edit/',
'update' => '/conditional_group/update/{csrf}/',
'contacts_preview' => '/conditional_group/preview/',
'json_list' => '/conditional_groups.json/',
],
'Received' => [

View File

@ -48,9 +48,13 @@
<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>
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/>
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.datas.gender == 'male'"/>
<div class="scheduled-preview-container">
<a class="btn btn-info preview-button" href="#">Prévisualiser les contacts</a>
</div>
</div>
<a class="btn btn-danger" href="<?php echo \descartes\Router::url('ConditionalGroup', 'list'); ?>">Annuler</a>
<input type="submit" class="btn btn-success" value="Enregistrer le groupe" />
@ -62,5 +66,46 @@
</div>
</div>
</div>
<div class="modal fade" tabindex="-1" id="preview-text-modal">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<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">Prévisualisation des contacts</h4>
</div>
<div class="modal-body">
<pre></pre>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
jQuery(document).ready(function()
{
jQuery('body').on('click', '.preview-button', function (e)
{
e.preventDefault();
var condition = jQuery(this).parents('.form-group').find('input').val();
var datas = {
'condition' : condition,
};
jQuery.ajax({
type: "POST",
url: HTTP_PWD + '/conditional_group/preview/',
data: datas,
success: function (datas) {
jQuery('#preview-text-modal').find('.modal-body pre').text(datas.result);
jQuery('#preview-text-modal').modal({'keyboard': true});
},
dataType: 'json'
});
});
});
</script>
<?php
$this->render('incs/footer');

View File

@ -61,10 +61,12 @@
</li>
<li <?php echo $page == 'groupes' ? 'class="active"' : ''; ?>>
<a href="<?php echo \descartes\Router::url('Group', 'list'); ?>"><i class="fa fa-fw fa-group"></i> Groupes</a>
</li>
<li <?php echo $page == 'conditional_groupes' ? 'class="active"' : ''; ?>>
<a href="<?php echo \descartes\Router::url('ConditionalGroup', 'list'); ?>"><i class="fa fa-fw fa-random"></i> Groupes Conditionnels</a>
</li>
</li>
<?php if ($_SESSION['user']['settings']['conditional_group'] ?? false) { ?>
<li <?php echo $page == 'conditional_groupes' ? 'class="active"' : ''; ?>>
<a href="<?php echo \descartes\Router::url('ConditionalGroup', 'list'); ?>"><i class="fa fa-fw fa-random"></i> Groupes Conditionnels</a>
</li>
<?php } ?>
</ul>
</li>
<li>

View File

@ -81,6 +81,12 @@
<label>Groupes cibles</label>
<input class="add-groupes form-control" name="groups[]"/>
</div>
<?php if ($_SESSION['user']['settings']['conditional_group'] ?? false) { ?>
<div class="form-group">
<label>Groupes conditionnels cibles</label>
<input class="add-conditional-groups form-control" name="conditional_groups[]"/>
</div>
<?php } ?>
<?php if ($_SESSION['user']['settings']['sms_flash']) { ?>
<div class="form-group">
<label>Envoyer comme un SMS Flash : </label>
@ -149,6 +155,15 @@
});
});
jQuery('.add-conditional-groups').each(function()
{
jQuery(this).magicSuggest({
data: '<?php echo \descartes\Router::url('ConditionalGroup', 'json_list'); ?>',
valueField: 'id',
displayField: 'name',
});
});
jQuery('body').on('click', '.remove-scheduleds-number', function(e)
{
jQuery(this).parents('.scheduleds-number-groupe').remove();

View File

@ -84,6 +84,12 @@
<label>Groupes cibles</label>
<input class="add-groupes form-control" name="scheduleds[<?php $this->s($scheduled['id']); ?>][groups][]" value="<?php $this->s(json_encode($scheduled['groups'])); ?>" />
</div>
<?php if ($_SESSION['user']['settings']['conditional_group'] ?? false) { ?>
<div class="form-group">
<label>Groupes conditionnels cibles</label>
<input class="add-conditional-groups form-control" name="scheduleds[<?php $this->s($scheduled['id']); ?>][conditional_groups][]" value="<?php $this->s(json_encode($scheduled['conditional_groups'])); ?>" />
</div>
<?php } ?>
<?php if ($_SESSION['user']['settings']['sms_flash']) { ?>
<div class="form-group">
<label>Envoyer comme un SMS Flash : </label>
@ -159,6 +165,16 @@
});
});
jQuery('.add-conditional-groups').each(function()
{
jQuery(this).magicSuggest({
data: '<?php echo \descartes\Router::url('ConditionalGroup', 'json_list'); ?>',
valueField: 'id',
displayField: 'name',
});
});
jQuery('body').on('click', '.remove-scheduleds-number', function(e)
{
jQuery(this).parents('.scheduleds-number-groupe').remove();