raspisms/controllers/internals/Call.php

137 lines
4.3 KiB
PHP
Raw Normal View History

2021-03-23 04:31:13 +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 Call extends StandardController
{
protected $model;
/**
* Create a call.
2021-03-23 04:31:13 +01:00
*
* @param int $id_user : Id of the user
* @param int $id_phone : Id of the phone that emitted (outbound) or received (inbound) the call
* @param string $uid : Uid of the phone call
* @param string $direction : Direction of the call, \models\Call::DIRECTION_INBOUND | \models\Call::DIRECTION_OUTBOUND
* @param string $start : Date of the call beginning
* @param ?string $end : Date of the call end
* @param ?string $origin : Origin of the call or null if outbound
* @param ?string $destination : Destination of the call or null if inbound
*
* @return mixed bool|int : false on error, new call id else
*/
public function create(int $id_user, int $id_phone, string $uid, string $direction, string $start, ?string $end = null, ?string $origin = null, ?string $destination = null)
{
$call = [
'id_user' => $id_user,
'id_phone' => $id_phone,
'uid' => $uid,
'start' => $start,
'end' => $end,
'direction' => $direction,
'origin' => $origin,
'destination' => $destination,
];
if (!$origin && !$destination)
{
return false;
}
switch ($direction)
{
case \models\Call::DIRECTION_OUTBOUND :
if (null === $destination) { return false; }
2021-03-23 04:31:13 +01:00
break;
case \models\Call::DIRECTION_INBOUND :
if (null === $origin) { return false; }
2021-03-23 04:31:13 +01:00
break;
default :
return false;
}
if (!\controllers\internals\Tool::validate_date($start, 'Y-m-d H:i:s'))
{
return false;
}
if (null !== $end && !\controllers\internals\Tool::validate_date($end, 'Y-m-d H:i:s'))
{
return false;
}
if (null !== $end && new \DateTime($end) < new \DateTime($start))
{
return false;
}
2021-03-26 17:55:47 +01:00
$new_call_id = $this->get_model()->insert($call);
if (!$new_call_id)
{
return false;
}
$internal_webhook = new Webhook($this->bdd);
$internal_webhook->trigger($id_user, \models\Webhook::TYPE_INBOUND_CALL, $call);
2021-03-23 04:31:13 +01:00
2021-03-26 17:55:47 +01:00
return $new_call_id;
2021-03-23 04:31:13 +01:00
}
/**
* End a call
*
* @param int $id_user : Id of the user to end call for
* @param int $id_phone : If of the phone to end call for
* @param string $uid : Uid of the call to end
* @param string $end : End date of the call, format Y-m-d H:i:s
*
* @return bool : False if cannot end phone call, true else
*/
public function end(int $id_user, int $id_phone, string $uid, string $end)
{
if (!\controllers\internals\Tool::validate_date($end, 'Y-m-d H:i:s'))
{
return false;
}
$call = $this->get_model()->get_by_uid_and_phone_for_user($id_user, $id_phone, $uid);
if (!$call)
{
return false;
}
if (new \DateTime($end) < new \DateTime($call['start']))
{
return false;
}
$datas = [
'end' => $end,
];
return (bool) $this->get_model()->update_for_user($id_user, $call['id'], $datas);
}
/**
* Get the model for the Controller.
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Call($this->bdd);
return $this->model;
}
2021-03-23 04:31:13 +01:00
}