Skip to content

Commit

Permalink
Add support for automatic response to SMS stop
Browse files Browse the repository at this point in the history
  • Loading branch information
osaajani committed Feb 25, 2024
1 parent 6321899 commit 6e6c51a
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 12 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v3.8.4
v3.9.0
36 changes: 28 additions & 8 deletions controllers/internals/Received.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace controllers\internals;

use Exception;

class Received extends StandardController
{
protected $model;
Expand Down Expand Up @@ -88,14 +90,6 @@ public function create(int $id_user, int $id_phone, $at, string $text, string $o
return false;
}

//Check if the received message is a SMS STOP and we must register it
$internal_smsstop = new SmsStop($this->bdd);
$is_stop = $internal_smsstop->check_for_stop($received['text']);
if ($is_stop)
{
$internal_smsstop->create($id_user, $origin);
}

//Link medias
$internal_media = new Media($this->bdd);
foreach ($media_ids as $media_id)
Expand All @@ -116,6 +110,32 @@ public function create(int $id_user, int $id_phone, $at, string $text, string $o
return false;
}

//Check if the received message is a SMS STOP and we must register it
$internal_smsstop = new SmsStop($this->bdd);
$is_stop = $internal_smsstop->check_for_stop($received['text']);
if ($is_stop)
{
$stop_exists = (bool) $internal_smsstop->get_by_number_for_user($id_user, $origin);
if ($stop_exists)
{
return $id_received;
}

$internal_smsstop->create($id_user, $origin);

//If stop response enabled, respond to user
//(this will happen only for first stop, any further stop will not trigger responses)
$internal_setting = new Setting($this->bdd);
$user_settings = $internal_setting->gets_for_user($id_user);

if ((int) ($user_settings['smsstop_respond'] ?? false))
{
$response = $user_settings['smsstop_response'];
$internal_scheduled = new Scheduled($this->bdd);
$internal_scheduled->create($id_user, (new \DateTime())->format('Y-m-d H:i:s'), $response, $id_phone, null, false, false, \models\SmsStop::SMS_STOP_TAG, [['number' => $origin, 'data' => '[]']]);
}
}

return $id_received;
}

Expand Down
4 changes: 2 additions & 2 deletions controllers/internals/Scheduled.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,8 @@ public function get_smss_to_send()
continue;
}

//Remove messages to smsstops numbers
if (($users_smsstops[$id_user] ?? false) && in_array($target['number'], $users_smsstops[$id_user]))
//Remove messages to smsstops numbers if not with tag SMS_STOP
if ($scheduled['tag'] != \models\SmsStop::SMS_STOP_TAG && ($users_smsstops[$id_user] ?? false) && in_array($target['number'], $users_smsstops[$id_user]))
{
unset($targets[$key]);
continue;
Expand Down
3 changes: 2 additions & 1 deletion controllers/internals/SmsStop.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public function get_by_number_for_user(int $id_user, string $number)
*/
public function check_for_stop(string $str)
{
return 'stop' == trim(mb_strtolower($str));
$str = trim(mb_strtolower($str));
return 'stop' == $str || 'stop sms' == $str;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions env.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
'phone_limit' => 0,
'phone_priority' => 0,
'shorten_url' => 0,
'smsstop_respond' => 1,
'smsstop_response' => 'Demande prise en compte, vous ne recevrez plus de messages.',
],
];

2 changes: 2 additions & 0 deletions models/SmsStop.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class SmsStop extends StandardModel
{
const SMS_STOP_TAG = 'SMS_STOP';

/**
* Return a smsstop by his number and user.
*
Expand Down
10 changes: 10 additions & 0 deletions templates/phone/add.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ function change_adapter ()
'</div>' +
'</div>';
}
else if (field.type == 'textarea')
{
html += '<div class="form-group">' +
'<label>' + field.title + '</label>' +
'<p class="italic small help">' + field.description + '</p>' +
'<div class="form-group">' +
'<textarea name="adapter_data[' + field.name + ']" class="form-control" ' + (field.required ? 'required' : '') + ' >' + (field.default_value ? field.default_value : '') + '</textarea>' +
'</div>' +
'</div>';
}
else
{
html += '<div class="form-group">' +
Expand Down
10 changes: 10 additions & 0 deletions templates/phone/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ function change_adapter (target)
'</div>' +
'</div>';
}
else if (field.type == 'textarea')
{
html += '<div class="form-group">' +
'<label>' + field.title + '</label>' +
'<p class="italic small help">' + field.description + '</p>' +
'<div class="form-group">' +
'<textarea name="adapter_data[' + field.name + ']" class="form-control" ' + (field.required ? 'required' : '') + ' >' + (value ? value : '') + '</textarea>' +
'</div>' +
'</div>';
}
else
{
html += '' +
Expand Down
41 changes: 41 additions & 0 deletions templates/setting/show.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,47 @@
</div>
</div>

<div class="col-xs-12 col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title"><i class="fa fa-ban fa-fw"></i> Activation des réponses automatiques aux SMS-STOP</h4>
</div>
<div class="panel-body">
<form action="<?php echo \descartes\Router::url('Setting', 'update', ['setting_name' => 'smsstop_respond', 'csrf' => $_SESSION['csrf']]); ?>" method="POST">
<div class="form-group">
<label>Réponses automatiques aux SMS STOP activées : </label>
<select name="setting_value" class="form-control">
<option value="0">Non</option>
<option value="1" <?php echo $_SESSION['user']['settings']['smsstop_respond'] ? 'selected' : ''; ?>>Oui</option>
</select>
</div>
<div class="text-center">
<button class="btn btn-success">Mettre à jour les données</button>
</div>
</form>
</div>
</div>
</div>

<div class="col-xs-12 col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title"><i class="fa fa-comments-o fa-fw"></i> Texte de réponse aux SMS-STOP</h4>
</div>
<div class="panel-body">
<form action="<?php echo \descartes\Router::url('Setting', 'update', ['setting_name' => 'smsstop_response', 'csrf' => $_SESSION['csrf']]); ?>" method="POST">
<div class="form-group">
<label>Texte des réponses automatiques aux SMS-STOP : </label>
<input name="setting_value" class="form-control" value="<?php $this->s($_SESSION['user']['settings']['smsstop_response'] ?? ''); ?>" />
</div>
<div class="text-center">
<button class="btn btn-success">Mettre à jour les données</button>
</div>
</form>
</div>
</div>
</div>

<div class="col-xs-12 col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
Expand Down

0 comments on commit 6e6c51a

Please sign in to comment.