2019-11-12 05:16:59 +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 ;
/**
* Page des phones .
*/
class Phone extends \descartes\Controller
{
private $internal_phone ;
private $internal_adapter ;
public function __construct ()
{
$bdd = \descartes\Model :: _connect ( DATABASE_HOST , DATABASE_NAME , DATABASE_USER , DATABASE_PASSWORD );
$this -> internal_phone = new \controllers\internals\Phone ( $bdd );
2020-01-17 18:36:53 +01:00
$this -> internal_adapter = new \controllers\internals\Adapter ();
2019-11-12 05:16:59 +01:00
\controllers\internals\Tool :: verifyconnect ();
}
/**
* Cette fonction retourne tous les phones , sous forme d 'un tableau permettant l' administration de ces phones .
*
* @ param mixed $page
*/
public function list ( $page = 0 )
{
$id_user = $_SESSION [ 'user' ][ 'id' ];
$page = ( int ) $page ;
2019-11-14 23:09:56 +01:00
$phones = $this -> internal_phone -> list_for_user ( $id_user , 25 , $page );
2019-11-12 05:16:59 +01:00
$adapters = [];
$adapters_metas = $this -> internal_adapter -> list_adapters ();
foreach ( $adapters_metas as $adapter_metas )
{
$adapters [ $adapter_metas [ 'meta_classname' ]] = $adapter_metas [ 'meta_name' ];
}
foreach ( $phones as & $phone )
{
$phone [ 'adapter' ] = $adapters [ $phone [ 'adapter' ]] ? ? 'Inconnu' ;
}
$this -> render ( 'phone/list' , [ 'phones' => $phones ]);
}
/**
* Cette fonction va supprimer une liste de phones .
*
* @ param array int $_GET [ 'ids' ] : Les id des phonees à supprimer
* @ param mixed $csrf
*
* @ return boolean ;
*/
public function delete ( $csrf )
{
if ( ! $this -> verify_csrf ( $csrf ))
{
\FlashMessage\FlashMessage :: push ( 'danger' , 'Jeton CSRF invalid !' );
return $this -> redirect ( \descartes\Router :: url ( 'Phone' , 'list' ));
}
if ( ! \controllers\internals\Tool :: is_admin ())
{
\FlashMessage\FlashMessage :: push ( 'danger' , 'Vous devez être administrateur pour supprimer un utilisateur !' );
return $this -> redirect ( \descartes\Router :: url ( 'Phone' , 'list' ));
}
$ids = $_GET [ 'ids' ] ? ? [];
foreach ( $ids as $id )
{
2019-11-14 22:33:00 +01:00
$this -> internal_phone -> delete_for_user ( $_SESSION [ 'user' ][ 'id' ], $id );
2019-11-12 05:16:59 +01:00
}
return $this -> redirect ( \descartes\Router :: url ( 'Phone' , 'list' ));
}
/**
* Cette fonction retourne la page d 'ajout d' un phone .
*/
public function add ()
{
$adapters = $this -> internal_adapter -> list_adapters ();
2020-01-17 18:19:25 +01:00
2019-11-12 05:16:59 +01:00
return $this -> render ( 'phone/add' , [ 'adapters' => $adapters ]);
}
/**
2020-01-17 18:19:25 +01:00
* Create a new phone .
*
2019-11-12 05:16:59 +01:00
* @ param $csrf : CSRF token
2020-03-31 01:19:21 +02:00
* @ param string $_POST [ 'name' ] : Phone name
2020-01-17 18:19:25 +01:00
* @ param string $_POST [ 'adapter' ] : Phone adapter
* @ param array $_POST [ 'adapter_datas' ] : Phone adapter datas
2019-11-12 05:16:59 +01:00
*/
public function create ( $csrf )
{
if ( ! $this -> verify_csrf ( $csrf ))
{
\FlashMessage\FlashMessage :: push ( 'danger' , 'Jeton CSRF invalid !' );
2020-01-17 18:19:25 +01:00
2019-11-12 05:16:59 +01:00
return $this -> redirect ( \descartes\Router :: url ( 'Phone' , 'add' ));
}
$id_user = $_SESSION [ 'user' ][ 'id' ];
2020-03-31 01:19:21 +02:00
$name = $_POST [ 'name' ] ? ? false ;
2019-11-12 05:16:59 +01:00
$adapter = $_POST [ 'adapter' ] ? ? false ;
2020-01-09 22:23:58 +01:00
$adapter_datas = ! empty ( $_POST [ 'adapter_datas' ]) ? $_POST [ 'adapter_datas' ] : [];
2019-11-12 05:16:59 +01:00
2020-03-31 01:19:21 +02:00
if ( ! $name || ! $adapter )
2019-11-12 05:16:59 +01:00
{
\FlashMessage\FlashMessage :: push ( 'danger' , 'Des champs obligatoires sont manquants.' );
2020-01-17 18:19:25 +01:00
2019-11-12 05:16:59 +01:00
return $this -> redirect ( \descartes\Router :: url ( 'Phone' , 'add' ));
}
2020-03-31 01:19:21 +02:00
$name_exist = $this -> internal_phone -> get_by_name ( $name );
if ( $name_exist )
2019-11-12 05:16:59 +01:00
{
2020-03-31 01:19:21 +02:00
\FlashMessage\FlashMessage :: push ( 'danger' , 'Ce nom est déjà utilisé pour un autre téléphone.' );
2020-01-17 18:19:25 +01:00
2019-11-12 05:16:59 +01:00
return $this -> redirect ( \descartes\Router :: url ( 'Phone' , 'add' ));
}
$adapters = $this -> internal_adapter -> list_adapters ();
2020-01-09 22:23:58 +01:00
$find_adapter = false ;
2019-11-12 05:16:59 +01:00
foreach ( $adapters as $metas )
{
if ( $metas [ 'meta_classname' ] === $adapter )
{
2020-01-09 22:23:58 +01:00
$find_adapter = $metas ;
2020-01-17 18:19:25 +01:00
2019-11-12 05:16:59 +01:00
break ;
}
}
2020-01-09 22:23:58 +01:00
if ( ! $find_adapter )
2019-11-12 05:16:59 +01:00
{
\FlashMessage\FlashMessage :: push ( 'danger' , 'Cet adaptateur n\'existe pas.' );
2020-01-17 18:19:25 +01:00
2019-11-12 05:16:59 +01:00
return $this -> redirect ( \descartes\Router :: url ( 'Phone' , 'add' ));
}
2020-01-09 22:23:58 +01:00
//If missing required data fields, error
foreach ( $find_adapter [ 'meta_datas_fields' ] as $field )
{
2020-01-17 18:19:25 +01:00
if ( false === $field [ 'required' ])
2020-01-09 22:23:58 +01:00
{
continue ;
}
if ( ! empty ( $adapter_datas [ $field [ 'name' ]]))
{
continue ;
}
\FlashMessage\FlashMessage :: push ( 'danger' , 'Vous n\'avez pas rempli certains champs obligatoires pour l\'adaptateur choisis.' );
2020-01-17 18:19:25 +01:00
2020-01-09 22:23:58 +01:00
return $this -> redirect ( \descartes\Router :: url ( 'Phone' , 'add' ));
}
2020-04-02 01:55:55 +02:00
//If field phone number is invalid
foreach ( $find_adapter [ 'meta_datas_fields' ] as $field )
{
if ( false === ( $field [ 'number' ] ? ? false ))
{
continue ;
}
if ( ! empty ( $adapter_datas [ $field [ 'name' ]]))
{
$adapter_datas [ $field [ 'name' ]] = \controllers\internals\Tool :: parse_phone ( $adapter_datas [ $field [ 'name' ]]);
if ( $adapter_datas [ $field [ 'name' ]])
{
continue ;
}
}
var_dump ( $field );
var_dump ( $adapter_datas [ $field [ 'name' ]]);
die ();
\FlashMessage\FlashMessage :: push ( 'danger' , 'Vous avez fourni un numéro de téléphone avec un format invalide.' );
return $this -> redirect ( \descartes\Router :: url ( 'Phone' , 'add' ));
}
2020-01-09 22:23:58 +01:00
$adapter_datas = json_encode ( $adapter_datas );
2020-03-31 01:19:21 +02:00
//Check adapter is working correctly with thoses names and datas
2020-01-09 22:23:58 +01:00
$adapter_classname = $find_adapter [ 'meta_classname' ];
2020-04-02 01:55:55 +02:00
$adapter_instance = new $adapter_classname ( $adapter_datas );
2020-01-09 22:23:58 +01:00
$adapter_working = $adapter_instance -> test ();
2020-03-04 05:10:45 +01:00
2020-01-09 22:23:58 +01:00
if ( ! $adapter_working )
2019-11-12 05:16:59 +01:00
{
2020-01-09 22:23:58 +01:00
\FlashMessage\FlashMessage :: push ( 'danger' , 'Impossible d\'utiliser l\'adaptateur choisis avec les données fournies. Vérifiez le numéro de téléphone et les réglages.' );
2020-01-17 18:19:25 +01:00
2019-11-12 05:16:59 +01:00
return $this -> redirect ( \descartes\Router :: url ( 'Phone' , 'add' ));
}
2020-03-31 01:19:21 +02:00
$success = $this -> internal_phone -> create ( $id_user , $name , $adapter , $adapter_datas );
2019-11-12 05:16:59 +01:00
if ( ! $success )
{
\FlashMessage\FlashMessage :: push ( 'danger' , 'Impossible de créer ce téléphone.' );
2020-01-17 18:19:25 +01:00
2019-11-12 05:16:59 +01:00
return $this -> redirect ( \descartes\Router :: url ( 'Phone' , 'add' ));
}
2020-01-17 18:19:25 +01:00
2019-11-12 05:16:59 +01:00
\FlashMessage\FlashMessage :: push ( 'success' , 'Le téléphone a bien été créé.' );
2020-01-17 18:19:25 +01:00
2019-11-12 05:16:59 +01:00
return $this -> redirect ( \descartes\Router :: url ( 'Phone' , 'list' ));
}
}