mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-20 16:37:48 +02:00
add contact data enrichment && add templating on messages
This commit is contained in:
parent
50c7e4298c
commit
9f98fb5ae3
16 changed files with 833 additions and 88 deletions
|
@ -47,6 +47,17 @@ namespace controllers\internals;
|
|||
{
|
||||
return $this->get_model()->get_by_name_and_user($id_user, $name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return all contacts of a user.
|
||||
* @param int $id_user : user id
|
||||
* @return array
|
||||
*/
|
||||
public function gets_for_user (int $id_user)
|
||||
{
|
||||
return $this->get_model()->gets_for_user($id_user);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
@ -54,14 +65,16 @@ namespace controllers\internals;
|
|||
* @param int $id_user : User id
|
||||
* @param string $number : Contact number
|
||||
* @param string $name : Contact name
|
||||
* @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)
|
||||
public function create($id_user, $number, $name, $datas)
|
||||
{
|
||||
$contact = [
|
||||
'id_user' => $id_user,
|
||||
'number' => $number,
|
||||
'name' => $name,
|
||||
'datas' => $datas,
|
||||
];
|
||||
|
||||
$result = $this->get_model()->insert($contact);
|
||||
|
@ -83,15 +96,17 @@ namespace controllers\internals;
|
|||
* @param int $id : Contact id
|
||||
* @param string $number : Contact number
|
||||
* @param string $name : Contact name
|
||||
* @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)
|
||||
public function update_for_user(int $id_user, int $id, string $number, string $name, ?string $datas)
|
||||
{
|
||||
$datas = [
|
||||
$contact = [
|
||||
'number' => $number,
|
||||
'name' => $name,
|
||||
'datas' => $datas,
|
||||
];
|
||||
|
||||
return $this->get_model()->update_for_user($id_user, $id, $datas);
|
||||
return $this->get_model()->update_for_user($id_user, $id, $contact);
|
||||
}
|
||||
}
|
||||
|
|
87
controllers/internals/Templating.php
Executable file
87
controllers/internals/Templating.php
Executable file
|
@ -0,0 +1,87 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Templating questions relative class
|
||||
* Not a standard controller as it's not linked to a model in any way
|
||||
*/
|
||||
class Templating
|
||||
{
|
||||
/**
|
||||
* Twig environment
|
||||
*/
|
||||
private $sandbox;
|
||||
|
||||
public function __construct ()
|
||||
{
|
||||
$tags = [
|
||||
'if',
|
||||
'for',
|
||||
'apply',
|
||||
];
|
||||
|
||||
$filters = [
|
||||
'abs', 'capitalize', 'country_name', 'currency_name',
|
||||
'currency_symbol', 'date', 'date_modify', 'default',
|
||||
'first', 'format', 'format_currency', 'format_datetime',
|
||||
'format_number', 'join', 'keys', 'language_name',
|
||||
'last', 'length', 'locale_name', 'lower', 'number_format',
|
||||
'replace', 'reverse', 'round', 'slice',
|
||||
'sort', 'spaceless', 'split', 'timezone_name',
|
||||
'title', 'trim', 'upper', 'url_encode',
|
||||
];
|
||||
|
||||
$methods = [];
|
||||
$properties = [];
|
||||
$functions = [
|
||||
'date', 'max', 'min', 'random',
|
||||
'range',
|
||||
];
|
||||
|
||||
$policy = new \Twig\Sandbox\SecurityPolicy($tags, $filters, $methods, $properties, $functions);
|
||||
$this->sandbox = new \Twig\Extension\SandboxExtension($policy, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Render a string as a twig template
|
||||
* @param string $template : Template string
|
||||
* @param array $datas : Datas to pass to the template
|
||||
* @return array : keys, success, error, result
|
||||
*/
|
||||
public function render (string $template, array $datas = [])
|
||||
{
|
||||
try
|
||||
{
|
||||
$loader = new \Twig\Loader\ArrayLoader([
|
||||
'template' => $template,
|
||||
]);
|
||||
|
||||
$twig = new \Twig\Environment($loader);
|
||||
$result = $twig->render('template', $datas);
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'result' => $result,
|
||||
];
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return [
|
||||
'success' => false,
|
||||
'result' => $e->getMessage(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -99,6 +99,14 @@ namespace controllers\publics;
|
|||
return $this->redirect(\descartes\Router::url('Contact', 'list'));
|
||||
}
|
||||
|
||||
foreach ($contacts as &$contact)
|
||||
{
|
||||
if ($contact['datas'])
|
||||
{
|
||||
$contact['datas'] = json_decode($contact['datas']);
|
||||
}
|
||||
}
|
||||
|
||||
$this->render('contact/edit', [
|
||||
'contacts' => $contacts,
|
||||
]);
|
||||
|
@ -123,6 +131,7 @@ namespace controllers\publics;
|
|||
$name = $_POST['name'] ?? false;
|
||||
$number = $_POST['number'] ?? false;
|
||||
$id_user = $_SESSION['user']['id'];
|
||||
$datas = empty($_POST['datas']) ? null : $_POST['datas'];
|
||||
|
||||
if (!$name || !$number)
|
||||
{
|
||||
|
@ -139,7 +148,25 @@ namespace controllers\publics;
|
|||
return $this->redirect(\descartes\Router::url('Contact', 'add'));
|
||||
}
|
||||
|
||||
if (!$this->internal_contact->create($id_user, $number, $name))
|
||||
$clean_datas = null;
|
||||
if ($datas)
|
||||
{
|
||||
$clean_datas = [];
|
||||
foreach ($datas as $key => $value)
|
||||
{
|
||||
if ($value === "")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$key = mb_ereg_replace('[\W]', '', $key);
|
||||
$clean_datas[$key] = (string) $value;
|
||||
}
|
||||
|
||||
$clean_datas = json_encode($clean_datas);
|
||||
}
|
||||
|
||||
if (!$this->internal_contact->create($id_user, $number, $name, $clean_datas))
|
||||
{
|
||||
\FlashMessage\FlashMessage::push('danger', 'Impossible de créer ce contact.');
|
||||
|
||||
|
@ -168,15 +195,54 @@ namespace controllers\publics;
|
|||
return $this->redirect(\descartes\Router::url('Contact', 'list'));
|
||||
}
|
||||
|
||||
$nb_contacts_update = 0;
|
||||
foreach ($_POST['contacts'] as $contact)
|
||||
if (!array($_POST['contacts']))
|
||||
{
|
||||
$nb_contacts_update += (int) $this->internal_contact->update_for_user($_SESSION['user']['id'], $contact['id'], $contact['number'], $contact['name']);
|
||||
return $this->redirect(\descartes\Router::url('Contact', 'list'));
|
||||
}
|
||||
|
||||
$nb_contacts_update = 0;
|
||||
foreach ($_POST['contacts'] as $id_contact => $contact)
|
||||
{
|
||||
$name = $contact['name'] ?? false;
|
||||
$number = $contact['number'] ?? false;
|
||||
$id_user = $_SESSION['user']['id'];
|
||||
$datas = empty($contact['datas']) ? null : $contact['datas'];
|
||||
|
||||
if (!$name || !$number)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$number = \controllers\internals\Tool::parse_phone($number);
|
||||
if (!$number)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$clean_datas = null;
|
||||
if ($datas)
|
||||
{
|
||||
$clean_datas = [];
|
||||
foreach ($datas as $key => $value)
|
||||
{
|
||||
if ($value === "")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
if ($nb_contacts_update !== \count($_POST['contacts']))
|
||||
{
|
||||
\FlashMessage\FlashMessage::push('danger', 'Certais contacts n\'ont pas pu êtres mis à jour.');
|
||||
\FlashMessage\FlashMessage::push('danger', 'Certains contacts n\'ont pas pu êtres mis à jour.');
|
||||
|
||||
return $this->redirect(\descartes\Router::url('Contact', 'list'));
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ namespace controllers\publics;
|
|||
{
|
||||
private $internal_scheduled;
|
||||
private $internal_phone;
|
||||
private $internal_contact;
|
||||
|
||||
/**
|
||||
* Cette fonction est appelée avant toute les autres :
|
||||
|
@ -30,6 +31,7 @@ namespace controllers\publics;
|
|||
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
||||
$this->internal_scheduled = new \controllers\internals\Scheduled($bdd);
|
||||
$this->internal_phone = new \controllers\internals\Phone($bdd);
|
||||
$this->internal_contact = new \controllers\internals\Contact($bdd);
|
||||
|
||||
\controllers\internals\Tool::verifyconnect();
|
||||
}
|
||||
|
@ -87,10 +89,12 @@ namespace controllers\publics;
|
|||
$less_one_minute = new \DateInterval('PT1M');
|
||||
$now->sub($less_one_minute);
|
||||
|
||||
$contacts = $this->internal_contact->gets_for_user($_SESSION['user']['id']);
|
||||
$phones = $this->internal_phone->gets_for_user($_SESSION['user']['id']);
|
||||
|
||||
$this->render('scheduled/add', [
|
||||
'now' => $now->format('Y-m-d H:i'),
|
||||
'contacts' => $contacts,
|
||||
'phones' => $phones,
|
||||
]);
|
||||
}
|
||||
|
@ -110,6 +114,7 @@ namespace controllers\publics;
|
|||
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
|
||||
}
|
||||
|
||||
$all_contacts = $this->internal_contact->gets_for_user($_SESSION['user']['id']);
|
||||
$phones = $this->internal_phone->gets_for_user($_SESSION['user']['id']);
|
||||
$scheduleds = $this->internal_scheduled->gets_in_for_user($_SESSION['user']['id'], $ids);
|
||||
|
||||
|
@ -147,6 +152,7 @@ namespace controllers\publics;
|
|||
$this->render('scheduled/edit', [
|
||||
'scheduleds' => $scheduleds,
|
||||
'phones' => $phones,
|
||||
'contacts' => $all_contacts,
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -331,4 +337,5 @@ namespace controllers\publics;
|
|||
\FlashMessage\FlashMessage::push('success', 'Tous les SMS ont été mis à jour.');
|
||||
return $this->redirect(\descartes\Router::url('Scheduled', 'list'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
77
controllers/publics/Templating.php
Executable file
77
controllers/publics/Templating.php
Executable file
|
@ -0,0 +1,77 @@
|
|||
<?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\publics;
|
||||
|
||||
class Templating extends \descartes\Controller
|
||||
{
|
||||
private $internal_contact;
|
||||
private $internal_templating;
|
||||
|
||||
/**
|
||||
* Cette fonction est appelée avant toute les autres :
|
||||
* Elle vérifie que l'utilisateur est bien connecté.
|
||||
*
|
||||
* @return void;
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
||||
$this->internal_contact = new \controllers\internals\Contact($bdd);
|
||||
$this->internal_templating = new \controllers\internals\Templating();
|
||||
|
||||
\controllers\internals\Tool::verifyconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to render a template as a message for preview
|
||||
* @param string $_POST['template'] : Template string
|
||||
* @param int $_POST['id_contact'] : Id of the contact to render the template for
|
||||
* @return json string
|
||||
*/
|
||||
public function render_preview ()
|
||||
{
|
||||
$return = [
|
||||
'success' => false,
|
||||
'result' => 'Une erreur inconnue est survenue.',
|
||||
];
|
||||
|
||||
$template = $_POST['template'] ?? false;
|
||||
$id_contact = $_POST['id_contact'] ?? false;
|
||||
|
||||
if (!$template || !$id_contact)
|
||||
{
|
||||
$return['result'] = 'Veuillez remplir un message.';
|
||||
echo json_encode($return);
|
||||
return false;
|
||||
}
|
||||
|
||||
$contact = $this->internal_contact->get_for_user($_SESSION['user']['id'], $id_contact);
|
||||
if (!$contact)
|
||||
{
|
||||
$return['result'] = 'Ce contact n\'existe pas.';
|
||||
echo json_encode($return);
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($contact['datas'])
|
||||
{
|
||||
$contact['datas'] = json_decode($contact['datas'], true);
|
||||
}
|
||||
|
||||
$datas = [
|
||||
'contact' => $contact,
|
||||
];
|
||||
|
||||
echo json_encode($this->internal_templating->render($template, $datas));
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue