2019-11-20 02:45:00 +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\publics;
|
|
|
|
|
|
|
|
class Templating extends \descartes\Controller
|
|
|
|
{
|
|
|
|
private $internal_contact;
|
|
|
|
private $internal_templating;
|
2021-06-15 01:49:47 +02:00
|
|
|
private $internal_quota;
|
2019-11-20 02:45:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
2021-06-15 01:49:47 +02:00
|
|
|
$this->internal_quota = new \controllers\internals\Quota($bdd);
|
2019-11-20 02:45:00 +01:00
|
|
|
|
|
|
|
\controllers\internals\Tool::verifyconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* 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
|
|
|
|
*
|
2020-01-17 18:36:53 +01:00
|
|
|
* @return mixed : False or json string ['success' => bool, 'result' => message]
|
2019-11-20 02:45:00 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function render_preview()
|
2019-11-20 02:45:00 +01:00
|
|
|
{
|
|
|
|
$return = [
|
|
|
|
'success' => false,
|
|
|
|
'result' => 'Une erreur inconnue est survenue.',
|
2021-06-15 01:49:47 +02:00
|
|
|
'estimation_credit' => 0,
|
2019-11-20 02:45:00 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$template = $_POST['template'] ?? false;
|
|
|
|
$id_contact = $_POST['id_contact'] ?? false;
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-20 02:45:00 +01:00
|
|
|
if (!$template || !$id_contact)
|
|
|
|
{
|
|
|
|
$return['result'] = 'Veuillez remplir un message.';
|
|
|
|
echo json_encode($return);
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-20 02:45:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-20 02:45:00 +01:00
|
|
|
$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);
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-20 02:45:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-17 03:16:57 +01:00
|
|
|
$contact['data'] = json_decode($contact['data'], true);
|
2019-11-20 02:45:00 +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 = $contact;
|
2021-01-17 03:16:57 +01:00
|
|
|
unset($metas['data'], $metas['id_user']);
|
2020-09-26 21:34:42 +02:00
|
|
|
|
2021-01-17 03:16:57 +01:00
|
|
|
$data = [
|
|
|
|
'contact' => $contact['data'],
|
2020-09-26 21:34:42 +02:00
|
|
|
'contact_metas' => $metas,
|
2019-11-20 02:45:00 +01:00
|
|
|
];
|
|
|
|
|
2021-01-17 03:16:57 +01:00
|
|
|
$result = $this->internal_templating->render($template, $data);
|
2019-11-29 05:29:03 +01:00
|
|
|
$return = $result;
|
2021-06-15 01:49:47 +02:00
|
|
|
|
2023-02-06 20:18:35 +01:00
|
|
|
// If we must force GSM 7 alphabet
|
|
|
|
if ((int) ($_SESSION['user']['settings']['force_gsm_alphabet'] ?? false))
|
|
|
|
{
|
|
|
|
$return['result'] = \controllers\internals\Tool::convert_to_gsm0338($return['result']);
|
|
|
|
}
|
|
|
|
|
2019-11-29 05:29:03 +01:00
|
|
|
if (!trim($result['result']))
|
|
|
|
{
|
|
|
|
$return['result'] = 'Message vide, il ne sera pas envoyé.';
|
|
|
|
}
|
|
|
|
|
2021-06-15 01:49:47 +02:00
|
|
|
//Add credit estimation
|
|
|
|
$return['estimation_credit'] = $this->internal_quota->compute_credits_for_message($return['result']);
|
|
|
|
|
2019-11-29 05:29:03 +01:00
|
|
|
echo json_encode($return);
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-20 02:45:00 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|