Start adding conditional groups

This commit is contained in:
osaajani 2019-11-25 19:59:21 +01:00
parent 9f98fb5ae3
commit f59f7bd757
4 changed files with 175 additions and 0 deletions

View File

@ -0,0 +1,112 @@
<?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
{
protected $model = null;
/**
* Get the model for the Controller
* @return \descartes\Model
*/
protected function get_model () : \descartes\Model
{
$this->model = $this->model ?? new \models\ConditionalGroup($this->bdd);
return $this->model;
}
/**
* Create a new group for a user
* @param int $id_user : user id
* @param string $name : Group name
* @param string $condition : Condition for forming group content
* @return mixed bool|int : false on error, new group id
*/
public function create (int $id_user, string $name, string $condition)
{
$conditional_group = [
'id_user' => $id_user,
'name' => $name,
'condition' => $condition,
];
if (!$this->validate_condition($condition))
{
return false;
}
$id_group = $this->get_model()->insert($conditional_group);
if (!$id_group)
{
return false;
}
$internal_event = new Event($this->bdd);
$internal_event->create($id_user, 'CONDITIONAL_GROUP_ADD', 'Ajout du groupe conditionnel : ' . $name);
return $id_group;
}
/**
* Update a group for a user
* @param int $id_user : User id
* @param int $id_group : Group id
* @param string $name : Group name
* @param string $condition : Condition for forming group content
* @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,
];
if (!$this->validate_condition($condition))
{
return false;
}
$result = $this->get_model()->update_for_user($id_user, $id_group, $conditional_group);
if (!$result)
{
return false;
}
return true;
}
/**
* Return a group by his name for a user
* @param int $id_user : User id
* @param string $name : Group name
* @return array
*/
public function get_by_name_for_user (int $id_user, string $name)
{
return $this->get_model()->get_by_name_for_user($id_user, $name);
}
/**
* Verify if a condition string is valid (i.e we can parse it without error)
* @param string $condition : Condition string to verify
* @return bool
*/
public function validate_condition (string $condition) : bool
{
}
}

View File

@ -84,6 +84,17 @@ CREATE TABLE IF NOT EXISTS `group`
UNIQUE (id_user, name)
);
CREATE TABLE IF NOT EXISTS `conditional_group`
(
id INT NOT NULL AUTO_INCREMENT,
id_user INT NOT NULL,
name VARCHAR(100) NOT NULL,
condition TEXT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (id_user) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE (id_user, name)
);
CREATE TABLE IF NOT EXISTS group_contact
(
id INT NOT NULL AUTO_INCREMENT,

33
models/ConditionalGroup.php Executable file
View File

@ -0,0 +1,33 @@
<?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 models;
class CondiditionalGroup extends StandardModel
{
/**
* Return table name
* @return string
*/
protected function get_table_name() : string { return 'conditional_group'; }
/**
* Return a conditional group by his name for a user
* @param int $id_user : User id
* @param string $name : Group name
* @return array
*/
public function get_by_name_for_user (int $id_user, string $name)
{
return $this->_select_one($this->get_table_name(), ['id_user' => $id_user, 'name' => $name]);
}
}

View File

@ -183,6 +183,25 @@
</form>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title"><i class="fa fa-filter fa-fw"></i> Support des groupes conditionnels</h4>
</div>
<div class="panel-body">
<form action="<?php echo \descartes\Router::url('Setting', 'update', ['setting_name' => 'conditional_group', 'csrf' => $_SESSION['csrf']]); ?>" method="POST">
<div class="form-group">
<label>Groupes conditionnels activés : </label>
<select name="setting_value" class="form-control">
<option value="0">Non</option>
<option value="1" <?php echo $_SESSION['user']['settings']['conditional_group'] ? 'selected' : ''; ?>>Oui</option>
</select>
</div>
<div class="text-center">
<button class="btn btn-success">Mettre à jour les données</button>
</div>
</form>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title"><i class="fa fa-code fa-fw"></i> Support du templating</h4>