2019-11-25 19:59:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of RaspiSMS.
|
|
|
|
*
|
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace controllers\internals;
|
|
|
|
|
|
|
|
class ConditionalGroup extends StandardController
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
protected $model;
|
2019-11-25 19:59:21 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Create a new group for a user.
|
|
|
|
*
|
|
|
|
* @param int $id_user : user id
|
|
|
|
* @param string $name : Group name
|
2019-11-25 19:59:21 +01:00
|
|
|
* @param string $condition : Condition for forming group content
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-25 19:59:21 +01:00
|
|
|
* @return mixed bool|int : false on error, new group id
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function create(int $id_user, string $name, string $condition)
|
2019-11-25 19:59:21 +01:00
|
|
|
{
|
|
|
|
$conditional_group = [
|
|
|
|
'id_user' => $id_user,
|
|
|
|
'name' => $name,
|
|
|
|
'condition' => $condition,
|
|
|
|
];
|
|
|
|
|
2019-11-27 06:25:33 +01:00
|
|
|
$internal_ruler = new Ruler();
|
2021-01-17 03:16:57 +01:00
|
|
|
$valid_condition = $internal_ruler->validate_condition($condition, ['contact' => (object) ['data' => (object) null]]);
|
2019-11-27 06:25:33 +01:00
|
|
|
if (!$valid_condition)
|
2019-11-25 19:59:21 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-25 19:59:21 +01:00
|
|
|
$id_group = $this->get_model()->insert($conditional_group);
|
|
|
|
if (!$id_group)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$internal_event = new Event($this->bdd);
|
2020-01-17 18:47:08 +01:00
|
|
|
$internal_event->create($id_user, 'CONDITIONAL_GROUP_ADD', 'Ajout du groupe conditionnel : ' . $name);
|
2019-11-25 19:59:21 +01:00
|
|
|
|
|
|
|
return $id_group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Update a group for a user.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param int $id_group : Group id
|
|
|
|
* @param string $name : Group name
|
2019-11-25 19:59:21 +01:00
|
|
|
* @param string $condition : Condition for forming group content
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-25 19:59:21 +01:00
|
|
|
* @return bool : False on error, true on success
|
|
|
|
*/
|
|
|
|
public function update_for_user(int $id_user, int $id_group, string $name, string $condition)
|
|
|
|
{
|
|
|
|
$conditional_group = [
|
|
|
|
'name' => $name,
|
|
|
|
'condition' => $condition,
|
|
|
|
];
|
2019-11-27 06:25:33 +01:00
|
|
|
|
|
|
|
$internal_ruler = new Ruler();
|
2021-01-17 03:16:57 +01:00
|
|
|
$valid_condition = $internal_ruler->validate_condition($condition, ['contact' => (object) ['data' => (object) null]]);
|
2019-11-27 06:25:33 +01:00
|
|
|
if (!$valid_condition)
|
2019-11-25 19:59:21 +01:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->get_model()->update_for_user($id_user, $id_group, $conditional_group);
|
|
|
|
if (!$result)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return a group by his name for a user.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param string $name : Group name
|
|
|
|
*
|
2019-11-25 19:59:21 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function get_by_name_for_user(int $id_user, string $name)
|
2019-11-25 19:59:21 +01:00
|
|
|
{
|
|
|
|
return $this->get_model()->get_by_name_for_user($id_user, $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Gets the user's contacts that respects a condition.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
2019-11-25 19:59:21 +01:00
|
|
|
* @param string $condition : Condition string to verify
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function get_contacts_for_condition_and_user(int $id_user, string $condition): array
|
2019-11-25 19:59:21 +01:00
|
|
|
{
|
2019-11-28 01:55:11 +01:00
|
|
|
$internal_contacts = new Contact($this->bdd);
|
2019-11-27 06:25:33 +01:00
|
|
|
$contacts = $internal_contacts->gets_for_user($id_user);
|
|
|
|
|
|
|
|
$ruler = new Ruler();
|
|
|
|
|
|
|
|
foreach ($contacts as $key => $contact)
|
|
|
|
{
|
2021-01-17 03:16:57 +01:00
|
|
|
$contact['data'] = json_decode($contact['data']);
|
2019-11-27 06:25:33 +01:00
|
|
|
$contact = (object) $contact;
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2021-01-17 03:16:57 +01:00
|
|
|
//Add metas of contact by adding contact without data
|
2020-09-26 21:34:42 +02:00
|
|
|
$metas = clone $contact;
|
2021-01-17 03:16:57 +01:00
|
|
|
$metas->data = null;
|
2021-01-14 03:32:17 +01:00
|
|
|
$metas->id_user = null;
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2021-01-17 03:16:57 +01:00
|
|
|
$data = ['contact' => $contact->data, 'contact_metas' => $metas];
|
|
|
|
$is_valid = $ruler->evaluate_condition($condition, $data);
|
2019-11-27 06:25:33 +01:00
|
|
|
if (!$is_valid)
|
|
|
|
{
|
|
|
|
unset($contacts[$key]);
|
|
|
|
}
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-27 06:25:33 +01:00
|
|
|
return $contacts;
|
2019-11-25 19:59:21 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the model for the Controller.
|
|
|
|
*/
|
2021-07-19 17:32:23 +02:00
|
|
|
protected function get_model(): \models\ConditionalGroup
|
2020-01-17 18:19:25 +01:00
|
|
|
{
|
|
|
|
$this->model = $this->model ?? new \models\ConditionalGroup($this->bdd);
|
|
|
|
|
|
|
|
return $this->model;
|
|
|
|
}
|
2019-11-25 19:59:21 +01:00
|
|
|
}
|