Skip to content

Commit 6b9f137

Browse files
committed
show user friendly api responses
1 parent 753f97b commit 6b9f137

File tree

10 files changed

+178
-12
lines changed

10 files changed

+178
-12
lines changed

_screenshots/bulk_sms.png

-22 KB
Loading

_screenshots/bulk_voice.png

-64.9 KB
Loading

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@
4040
"source": "https://github.com/sms77io/concrete5"
4141
},
4242
"type": "concrete5-package",
43-
"version": "2.2.0"
43+
"version": "2.3.0"
4444
}

controller.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class Controller extends Package {
2626
protected $pkgHandle = 'sms77';
2727

2828
/** @var string $pkgVersion */
29-
protected $pkgVersion = '2.2.0';
29+
protected $pkgVersion = '2.3.0';
3030

3131
/** @return string */
3232
public function getPackageDescription() {

controllers/single_page/dashboard/sms77/bulk_sms.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function __construct(Page $c, UserInfoRepository $repo) {
1313
}
1414

1515
protected function onSubmit(array $recipients) {
16-
$this->setMessage($this->encode($this->client->sms((new SmsParams)
16+
$res = $this->client->sms((new SmsParams)
1717
->setDebug((bool)$this->config['debug'])
1818
->setFlash((bool)$this->config['flash'])
1919
->setForeignId($this->config['foreign_id'])
@@ -22,6 +22,8 @@ protected function onSubmit(array $recipients) {
2222
->setNoReload((bool)$this->config['no_reload'])
2323
->setPerformanceTracking((bool)$this->config['performance_tracking'])
2424
->setText($this->getText())
25-
->setTo(implode(',', $recipients)))));
25+
->setTo(implode(',', $recipients)));
26+
27+
$this->setMessage($res);
2628
}
2729
}

controllers/single_page/dashboard/sms77/bulk_voice.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function onSubmit(array $recipients) {
1919
->setXml((bool)$this->post('xml', false));
2020

2121
foreach ($recipients as $to) {
22-
$msg[] = $this->encode($this->client->voice($params->setTo($to)));
22+
$msg[] = $this->client->voice($params->setTo($to));
2323
}
2424

2525
$this->setMessage($msg);

single_pages/dashboard/sms77/bulk_sms.php

+78-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,86 @@
1-
<?php defined('C5_EXECUTE') or die('Access Denied.'); ?>
1+
<?php use Sms77\Concrete5\AbstractSinglePageDashboardController;
2+
3+
defined('C5_EXECUTE') or die('Access Denied.'); ?>
24

35
<?php if (isset($dashboardLink)): ?>
46
<a href='<?= $dashboardLink ?>'>
57
<?= t('Click here for navigating to the dashboard.') ?>
68
</a>
79
<?php else: ?>
10+
<?php if (isset($apiResponses)): ?>
11+
<script>
12+
function sms77ToggleRawResponse() {
13+
document.getElementById('sms77RawResponse').classList.toggle('hidden')
14+
}
15+
</script>
16+
17+
<div class='alert alert-info'>
18+
<button onclick='sms77ToggleRawResponse()' class='btn btn-primary btn-xs'>
19+
<?= t('Raw') ?></button>
20+
<button type='button' class='close' data-dismiss='alert'>×</button>
21+
22+
<pre id='sms77RawResponse' class='hidden'>
23+
<?= json_encode($apiResponses, JSON_PRETTY_PRINT) ?></pre>
24+
25+
<?php foreach ($apiResponses as $i => $res): ?>
26+
<table class='table table-responsive'>
27+
<caption><?= t('Response') ?> #<?= $i ?></caption>
28+
29+
<tr>
30+
<th><?= t('Success') ?></th>
31+
<td><?= $res->success ?></td>
32+
</tr>
33+
<tr>
34+
<th><?= t('Total Price') ?></th>
35+
<td><?= $res->total_price ?></td>
36+
</tr>
37+
<tr>
38+
<th><?= t('Balance') ?></th>
39+
<td><?= $res->balance ?></td>
40+
</tr>
41+
<tr>
42+
<th><?= t('Debug') ?></th>
43+
<td><?= $res->debug ?></td>
44+
</tr>
45+
<tr>
46+
<th><?= t('Messages') ?></th>
47+
<td>
48+
<table class='table table-responsive'>
49+
<thead>
50+
<tr>
51+
<th><?= t('ID') ?></th>
52+
<th><?= t('Recipient') ?></th>
53+
<th><?= t('Parts') ?></th>
54+
<th><?= t('Price') ?></th>
55+
<th><?= t('Success') ?></th>
56+
<th><?= t('Error') ?></th>
57+
</tr>
58+
</thead>
59+
<tbody>
60+
<?php foreach ($res->messages as $msg): ?>
61+
<tr>
62+
<td><?= AbstractSinglePageDashboardController::stringify($msg->id) ?></td>
63+
<td><?= $msg->recipient ?></td>
64+
<td><?= $msg->parts ?></td>
65+
<td><?= $msg->price ?></td>
66+
<td><?= AbstractSinglePageDashboardController::stringify($msg->success) ?></td>
67+
<td>
68+
<?= AbstractSinglePageDashboardController::stringify($msg->error) ?>
69+
<?php if ($msg->error_text): ?>
70+
&nbsp;(<?= $msg->error_text ?>)
71+
<?php endif ?>
72+
</td>
73+
</tr>
74+
<?php endforeach ?>
75+
</tbody>
76+
</table>
77+
</td>
78+
</tr>
79+
</table>
80+
<?php endforeach ?>
81+
</div>
82+
<?php endif ?>
83+
884
<form action='<?= $view->action('submit') ?>' method='post'>
985
<?= $this->controller->token->output('submit') ?>
1086

@@ -31,4 +107,4 @@
31107
</div>
32108
</div>
33109
</form>
34-
<?php endif; ?>
110+
<?php endif; ?>

single_pages/dashboard/sms77/bulk_voice.php

+76-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,84 @@
1-
<?php defined('C5_EXECUTE') or die('Access Denied.'); ?>
1+
<?php use Sms77\Concrete5\AbstractSinglePageDashboardController;
2+
3+
defined('C5_EXECUTE') or die('Access Denied.'); ?>
24

35
<?php if (isset($dashboardLink)): ?>
46
<a href='<?= $dashboardLink ?>'>
57
<?= t('Click here for navigating to the dashboard.') ?>
68
</a>
79
<?php else: ?>
10+
<?php if (isset($apiResponses)): ?>
11+
<script>
12+
function sms77ToggleRawResponse() {
13+
document.getElementById('sms77RawResponse').classList.toggle('hidden')
14+
}
15+
</script>
16+
17+
<div class='alert alert-info'>
18+
<button onclick='sms77ToggleRawResponse()' class='btn btn-primary btn-xs'>
19+
<?= t('Raw') ?></button>
20+
<button type='button' class='close' data-dismiss='alert'>×</button>
21+
22+
<pre id='sms77RawResponse' class='hidden'>
23+
<?= json_encode($apiResponses, JSON_PRETTY_PRINT) ?></pre>
24+
25+
<?php foreach ($apiResponses as $i => $res): ?>
26+
<table class='table table-responsive'>
27+
<caption><?= t('Response') ?> #<?= $i ?></caption>
28+
29+
<tr>
30+
<th><?= t('Success') ?></th>
31+
<td><?= $res->success ?></td>
32+
</tr>
33+
<tr>
34+
<th><?= t('Total Price') ?></th>
35+
<td><?= $res->total_price ?></td>
36+
</tr>
37+
<tr>
38+
<th><?= t('Balance') ?></th>
39+
<td><?= $res->balance ?></td>
40+
</tr>
41+
<tr>
42+
<th><?= t('Debug') ?></th>
43+
<td><?= $res->debug ?></td>
44+
</tr>
45+
<tr>
46+
<th><?= t('Messages') ?></th>
47+
<td>
48+
<table class='table table-responsive'>
49+
<thead>
50+
<tr>
51+
<th><?= t('ID') ?></th>
52+
<th><?= t('Recipient') ?></th>
53+
<th><?= t('Price') ?></th>
54+
<th><?= t('Success') ?></th>
55+
<th><?= t('Error') ?></th>
56+
</tr>
57+
</thead>
58+
<tbody>
59+
<?php foreach ($res->messages as $msg): ?>
60+
<tr>
61+
<td><?= AbstractSinglePageDashboardController::stringify($msg->id) ?></td>
62+
<td><?= $msg->recipient ?></td>
63+
<td><?= $msg->price ?></td>
64+
<td><?= AbstractSinglePageDashboardController::stringify($msg->success) ?></td>
65+
<td>
66+
<?= AbstractSinglePageDashboardController::stringify($msg->error) ?>
67+
<?php if ($msg->error_text): ?>
68+
&nbsp;(<?= $msg->error_text ?>)
69+
<?php endif ?>
70+
</td>
71+
</tr>
72+
<?php endforeach ?>
73+
</tbody>
74+
</table>
75+
</td>
76+
</tr>
77+
</table>
78+
<?php endforeach ?>
79+
</div>
80+
<?php endif ?>
81+
882
<form action='<?= $view->action('submit') ?>' method='post'>
983
<?= $this->controller->token->output('submit') ?>
1084

@@ -40,4 +114,4 @@
40114
</div>
41115
</div>
42116
</form>
43-
<?php endif; ?>
117+
<?php endif; ?>

src/AbstractMessageController.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,11 @@ private function initClient() {
160160

161161
/** @param string|array|null $msg */
162162
protected function setMessage($msg) {
163-
$this->set('message', isset($msg)
164-
? is_array($msg) ? implode(PHP_EOL, $msg) : $msg
165-
: t('Nothing has been sent.'));
163+
if (!isset($msg)) {
164+
$this->set('error', t('Nothing has been sent.'));
165+
return;
166+
}
167+
168+
$this->set('apiResponses', is_array($msg) ? $msg : [$msg]);
166169
}
167170
}

src/AbstractSinglePageDashboardController.php

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ public function __construct(Page $c) {
2525
$this->setConfig();
2626
}
2727

28+
/**
29+
* @param $val
30+
* @return string
31+
*/
32+
public static function stringify($val) {
33+
if (true === $val) return 'true';
34+
if (false === $val) return 'false';
35+
if (null === $val) return '-';
36+
return $val;
37+
}
38+
2839
/**
2940
*
3041
*/

0 commit comments

Comments
 (0)