Skip to content

Commit

Permalink
Add way to send SMS to customer
Browse files Browse the repository at this point in the history
  • Loading branch information
jimwins committed Dec 5, 2021
1 parent f392665 commit 8a7855a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
31 changes: 31 additions & 0 deletions lib/Scat/Controller/People.php
Expand Up @@ -377,6 +377,37 @@ public function remarketingList(Request $request, Response $response,
return $response->withHeader("Content-type", "text/csv");
}

public function startSms(Request $request, Response $response, $id,
View $view, \Scat\Service\Data $data)
{
$person= $data->factory('Person')->find_one($id);
if (!$person)
throw new \Slim\Exception\HttpNotFoundException($request);

$accept= $request->getHeaderLine('Accept');
if (strpos($accept, 'application/vnd.scat.dialog+html') !== false) {
return $view->render($response, 'dialog/sms.html', [
'person' => $person,
]);
}

throw new \Exception("This has to be a dialog.");
}

public function sendSms(Request $request, Response $response, $id,
\Scat\Service\Data $data, \Scat\Service\Phone $phone)
{
$person= $data->factory('Person')->find_one($id);
if (!$person)
throw new \Slim\Exception\HttpNotFoundException($request);

error_log("Sending message to {$person->phone}");
$data= $phone->sendSMS($person->phone,
$request->getParam('content'));

return $response;
}

public function getTaxExemption(Request $request, Response $response, $id,
\Scat\Service\Data $data,
\Scat\Service\Tax $tax,
Expand Down
5 changes: 5 additions & 0 deletions pos/index.php
Expand Up @@ -397,6 +397,11 @@ function (Request $request, Response $response, View $view) {
$app->post('/{id:[0-9]+}/~merge',
[ \Scat\Controller\People::class, 'mergePerson' ]);

$app->get('/{id:[0-9]+}/sms',
[ \Scat\Controller\People::class, 'startSms' ]);
$app->post('/{id:[0-9]+}/sms',
[ \Scat\Controller\People::class, 'sendSms' ]);

$app->get('/{id:[0-9]+}/tax-exemption',
[ \Scat\Controller\People::class, 'getTaxExemption' ]);
$app->post('/{id:[0-9]+}/tax-exemption',
Expand Down
50 changes: 50 additions & 0 deletions ui/dialog/sms.html
@@ -0,0 +1,50 @@
{% extends 'layout/dialog.html' %}
{% import 'macros.twig' as scat %}

{% block title %}
Send SMS
{% endblock %}

{% block body %}
<div class="modal-body">
<label for="content">Message</label>
<div class="label label-default" id="content-length">0 / 139</div>
<textarea name="content" id="content"
rows="4"
class="form-control"></textarea>
</div>
{% endblock %}

{% block submit %}
<button type="submit" class="btn btn-default">
Send
</button>
{% endblock %}

{% block script %}
let text= document.getElementById('content')
let label= document.getElementById('content-length')

text.addEventListener('keyup', (ev) => {
// Want the length in bytes
let len= (new TextEncoder().encode(text.value)).length
if (len > 140) {
label.classList.add('label-danger')
} else {
label.classList.remove('label-danger')
}
label.textContent= len + ' / 140'
})

form.onsubmit= (event) => {
event.preventDefault()

let form= dialog.getElementsByTagName('form')[0]
let formData= new FormData(form)
return scat.post("/person/{{ person.id }}/sms", formData)
.then((res) => {
scat.alert('info', 'Message sent.')
$(dialog).modal('hide')
})
}
{% endblock %}
11 changes: 10 additions & 1 deletion ui/person/person.html
Expand Up @@ -100,13 +100,19 @@ <h2 class="panel-title">Contact Info</h2>
<label for="phone" class="col-sm-2 control-label">
Phone
</label>
<div class="col-sm-10">
<div class="col-sm-7">
<p class="form-control-static editable"
data-pk="{{ person.id }}"
data-name="phone" data-value="{{ person.phone }}">
{{ person.pretty_phone }}
</p>
</div>
<div class="col-sm-3">
<button class="btn btn-xs btn-default" data-action="send-sms">
<i class="fa fa-mobile fa-fw"></i>
Send SMS
</button>
</div>
</div>

<div class="form-group">
Expand Down Expand Up @@ -502,6 +508,9 @@ <h2 class="panel-title">Activity</h2>
if (toggle) handleToggle(toggle)
})

scat.handleAction('click', 'send-sms', (act) => {
return scat.dialog("/person/{{ person.id }}/sms")
})
</script>

{{ scat.file_upload('/person/' ~ person.id ~ '/items', false) }}
Expand Down

0 comments on commit 8a7855a

Please sign in to comment.