raspisms/controllers/internals/Phone.php

110 lines
3.3 KiB
PHP
Raw Normal View History

2019-11-10 22:56:26 +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\internals;
class Phone extends StandardController
2019-11-10 22:56:26 +01:00
{
2020-01-17 18:19:25 +01:00
protected $model;
2019-11-10 22:56:26 +01:00
/**
* Return all phones of a user.
2020-01-17 18:19:25 +01:00
*
* @param int $id_user : user id
2020-01-17 18:19:25 +01:00
*
* @return array
*/
2020-01-17 18:19:25 +01:00
public function gets_for_user(int $id_user)
{
return $this->get_model()->gets_for_user($id_user);
}
2020-01-17 18:19:25 +01:00
/**
2020-06-23 21:06:13 +02:00
* Return a phone by his name.
2020-01-17 18:19:25 +01:00
*
* @param string $name : Phone name
2020-01-17 18:19:25 +01:00
*
* @return array
*/
public function get_by_name(string $name)
{
return $this->get_model()->get_by_name($name);
}
2019-11-10 22:56:26 +01:00
/**
2020-06-23 21:06:13 +02:00
* Return a phone for a user by a name.
2020-01-17 18:19:25 +01:00
*
* @param int $id_user : user id
2020-06-23 21:06:13 +02:00
* @param string $name : Phone name
2020-01-17 18:19:25 +01:00
*
2019-11-12 05:16:59 +01:00
* @return array
*/
public function get_by_name_and_user(int $id_user, string $name)
2019-11-12 05:16:59 +01:00
{
return $this->get_model()->get_by_name_and_user($id_user, $name);
2019-11-10 22:56:26 +01:00
}
/**
2020-01-17 18:19:25 +01:00
* Create a phone.
*
* @param int $id_user : User to insert phone for
2020-06-23 21:06:13 +02:00
* @param string $name : The name of the phone
2020-01-17 18:19:25 +01:00
* @param string $adapter : The adapter to use the phone
2020-01-06 23:36:56 +01:00
* @param string json $adapter_datas : A JSON string representing adapter's datas (for example credentials for an api)
2020-01-17 18:19:25 +01:00
*
2021-01-14 03:25:58 +01:00
* @return int|bool : false on error, new id on success
2019-11-10 22:56:26 +01:00
*/
2021-01-14 03:25:58 +01:00
public function create(int $id_user, string $name, string $adapter, string $adapter_datas)
2019-11-10 22:56:26 +01:00
{
2020-01-17 18:19:25 +01:00
$phone = [
2019-11-10 22:56:26 +01:00
'id_user' => $id_user,
'name' => $name,
2019-11-12 05:16:59 +01:00
'adapter' => $adapter,
'adapter_datas' => $adapter_datas,
2019-11-10 22:56:26 +01:00
];
2021-01-14 03:25:58 +01:00
return $this->get_model()->insert($phone);
2019-11-10 22:56:26 +01:00
}
/**
2020-01-17 18:19:25 +01:00
* Update a phone.
*
* @param int $id_user : User to insert phone for
* @param int $id : Phone id
2020-06-23 21:06:13 +02:00
* @param string $name : The name of the phone
2020-01-17 18:19:25 +01:00
* @param string $adapter : The adapter to use the phone
* @param array $adapter_datas : An array of the datas of the adapter (for example credentials for an api)
*
2019-11-10 22:56:26 +01:00
* @return bool : false on error, true on success
*/
public function update_for_user(int $id_user, int $id, string $name, string $adapter, array $adapter_datas): bool
2019-11-10 22:56:26 +01:00
{
$phone = [
'id_user' => $id_user,
'name' => $name,
2019-11-12 05:16:59 +01:00
'adapter' => $adapter,
'adapter_datas' => json_encode($adapter_datas),
2019-11-10 22:56:26 +01:00
];
return (bool) $this->get_model()->update_for_user($id_user, $id, $phone);
2019-11-10 22:56:26 +01:00
}
2020-01-17 18:19:25 +01:00
/**
* Get the model for the Controller.
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Phone($this->bdd);
return $this->model;
}
2019-11-10 22:56:26 +01:00
}