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(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue