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;
|
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
class Phone extends StandardController
|
2019-11-10 22:56:26 +01:00
|
|
|
{
|
2021-03-19 02:45:12 +01:00
|
|
|
const MMS_SENDING = 'sending';
|
|
|
|
const MMS_RECEPTION = 'reception';
|
|
|
|
const MMS_BOTH = 'both';
|
|
|
|
|
2020-01-17 18:19:25 +01:00
|
|
|
protected $model;
|
2019-11-10 22:56:26 +01:00
|
|
|
|
2023-01-31 23:11:25 +01:00
|
|
|
/**
|
|
|
|
* Return all phones for active users.
|
|
|
|
*
|
|
|
|
* @param int $id_user : user id
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_all_for_active_users()
|
|
|
|
{
|
|
|
|
return $this->get_model()->get_all_for_active_users();
|
|
|
|
}
|
|
|
|
|
2019-11-15 06:30:23 +01:00
|
|
|
/**
|
|
|
|
* Return all phones of a user.
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-15 06:30:23 +01:00
|
|
|
* @param int $id_user : user id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-15 06:30:23 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function gets_for_user(int $id_user)
|
2019-11-15 06:30:23 +01:00
|
|
|
{
|
|
|
|
return $this->get_model()->gets_for_user($id_user);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-11-14 23:09:56 +01:00
|
|
|
/**
|
2023-02-02 01:12:30 +01:00
|
|
|
* Return a list of phone limits
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2023-02-02 01:12:30 +01:00
|
|
|
* @param int $id_phone : Phone id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-14 23:09:56 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2023-02-02 01:12:30 +01:00
|
|
|
public function get_limits(int $id_phone)
|
2019-11-14 23:09:56 +01:00
|
|
|
{
|
2023-02-02 01:12:30 +01:00
|
|
|
return $this->get_model()->get_limits($id_phone);
|
2019-11-14 23:09:56 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
/**
|
2021-06-17 00:51:33 +02:00
|
|
|
* Check if a phone support mms.
|
2021-03-19 02:45:12 +01:00
|
|
|
*
|
|
|
|
* @param int $id : id of the phone to check
|
2021-06-17 00:51:33 +02:00
|
|
|
* @param $type : type of sms support, a const from Phone, MMS_SENDING, MMS_RECEPTION or MMS_BOTH
|
|
|
|
*
|
2021-03-19 02:45:12 +01:00
|
|
|
* @return bool : true if support, false else
|
|
|
|
*/
|
2021-06-17 00:51:33 +02:00
|
|
|
public function support_mms(int $id, string $type)
|
2021-03-19 02:45:12 +01:00
|
|
|
{
|
|
|
|
$phone = $this->get_model()->get($id);
|
|
|
|
if (!$phone)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($type)
|
|
|
|
{
|
2021-06-17 00:51:33 +02:00
|
|
|
case self::MMS_SENDING:
|
2021-03-19 02:45:12 +01:00
|
|
|
return $phone['adapter']::meta_support_mms_sending();
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
break;
|
|
|
|
|
2021-06-17 00:51:33 +02:00
|
|
|
case self::MMS_RECEPTION:
|
2021-03-19 02:45:12 +01:00
|
|
|
return $phone['adapter']::meta_support_mms_reception();
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
break;
|
|
|
|
|
2021-06-17 00:51:33 +02:00
|
|
|
case self::MMS_BOTH:
|
2021-03-19 02:45:12 +01:00
|
|
|
return $phone['adapter']::meta_support_mms_sending() && $phone['adapter']::meta_support_mms_reception();
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-17 00:51:33 +02:00
|
|
|
* Get all phones supporting mms for a user.
|
2021-03-19 02:45:12 +01:00
|
|
|
*
|
|
|
|
* @param int $id_user : id of the user
|
2021-06-17 00:51:33 +02:00
|
|
|
* @param $type : type of sms support, a const from Phone, MMS_SENDING, MMS_RECEPTION or MMS_BOTH
|
|
|
|
*
|
2021-03-19 02:45:12 +01:00
|
|
|
* @return array : array of phones supporting mms
|
|
|
|
*/
|
2021-06-17 00:51:33 +02:00
|
|
|
public function gets_phone_supporting_mms_for_user(int $id_user, string $type)
|
2021-03-19 02:45:12 +01:00
|
|
|
{
|
|
|
|
$phones = $this->get_model()->gets_for_user($id_user);
|
|
|
|
|
|
|
|
$valid_phones = [];
|
|
|
|
foreach ($phones as $phone)
|
|
|
|
{
|
|
|
|
if ($this->support_mms($phone['id'], $type))
|
|
|
|
{
|
|
|
|
$valid_phones[] = $phone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $valid_phones;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2020-03-31 01:19:21 +02:00
|
|
|
public function get_by_name_and_user(int $id_user, string $name)
|
2019-11-12 05:16:59 +01:00
|
|
|
{
|
2020-03-31 01:19:21 +02: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.
|
|
|
|
*
|
2021-02-23 00:31:54 +01:00
|
|
|
* @param int $id_user : User to insert phone for
|
|
|
|
* @param string $name : The name of the phone
|
|
|
|
* @param string $adapter : The adapter to use the phone
|
2021-01-17 03:16:57 +01:00
|
|
|
* @param string json $adapter_data : A JSON string representing adapter's data (for example credentials for an api)
|
2023-02-02 01:12:30 +01:00
|
|
|
* @param array $limits : An array of limits for this phone. Each limit must be an array with a key volume and a key startpoint
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2021-01-14 03:32:17 +01:00
|
|
|
* @return bool|int : false on error, new id on success
|
2019-11-10 22:56:26 +01:00
|
|
|
*/
|
2023-02-02 01:12:30 +01:00
|
|
|
public function create(int $id_user, string $name, string $adapter, string $adapter_data, array $limits = [])
|
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,
|
2020-03-31 01:19:21 +02:00
|
|
|
'name' => $name,
|
2019-11-12 05:16:59 +01:00
|
|
|
'adapter' => $adapter,
|
2021-01-17 03:16:57 +01:00
|
|
|
'adapter_data' => $adapter_data,
|
2019-11-10 22:56:26 +01:00
|
|
|
];
|
|
|
|
|
2023-02-02 01:12:30 +01:00
|
|
|
//Use transaction to garanty atomicity
|
|
|
|
$this->bdd->beginTransaction();
|
|
|
|
|
|
|
|
$new_phone_id = $this->get_model()->insert($phone);
|
|
|
|
if (!$new_phone_id)
|
|
|
|
{
|
|
|
|
$this->bdd->rollBack();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($limits as $limit)
|
|
|
|
{
|
|
|
|
$limit_id = $this->get_model()->insert_phone_limit($new_phone_id, $limit['volume'], $limit['startpoint']);
|
|
|
|
|
|
|
|
if (!$limit_id)
|
|
|
|
{
|
|
|
|
$this->bdd->rollBack();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$success = $this->bdd->commit();
|
|
|
|
return ($success ? $new_phone_id : false);
|
2019-11-10 22:56:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Update a phone.
|
|
|
|
*
|
2021-02-23 00:31:54 +01:00
|
|
|
* @param int $id_user : User to insert phone for
|
|
|
|
* @param int $id : Phone id
|
|
|
|
* @param string $name : The name of the phone
|
|
|
|
* @param string $adapter : The adapter to use the phone
|
2021-01-17 03:16:57 +01:00
|
|
|
* @param array $adapter_data : An array of the data of the adapter (for example credentials for an api)
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-10 22:56:26 +01:00
|
|
|
* @return bool : false on error, true on success
|
|
|
|
*/
|
2021-01-17 03:16:57 +01:00
|
|
|
public function update_for_user(int $id_user, int $id, string $name, string $adapter, array $adapter_data): bool
|
2019-11-10 22:56:26 +01:00
|
|
|
{
|
|
|
|
$phone = [
|
|
|
|
'id_user' => $id_user,
|
2020-03-31 01:19:21 +02:00
|
|
|
'name' => $name,
|
2019-11-12 05:16:59 +01:00
|
|
|
'adapter' => $adapter,
|
2021-01-17 03:16:57 +01:00
|
|
|
'adapter_data' => json_encode($adapter_data),
|
2019-11-10 22:56:26 +01:00
|
|
|
];
|
|
|
|
|
2019-11-14 23:09:56 +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.
|
|
|
|
*/
|
2021-07-19 17:32:23 +02:00
|
|
|
protected function get_model(): \models\Phone
|
2020-01-17 18:19:25 +01:00
|
|
|
{
|
|
|
|
$this->model = $this->model ?? new \models\Phone($this->bdd);
|
|
|
|
|
|
|
|
return $this->model;
|
|
|
|
}
|
2019-11-10 22:56:26 +01:00
|
|
|
}
|