add contact data enrichment && add templating on messages

This commit is contained in:
osaajani 2019-11-20 02:45:00 +01:00
parent 50c7e4298c
commit 9f98fb5ae3
16 changed files with 833 additions and 88 deletions

View file

@ -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'));
}

View file

@ -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'));
}
}

View 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;
}
}