Skip to content

Commit

Permalink
add bulk sms options
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiez committed Jul 13, 2021
1 parent 171adce commit cd8a3af
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 37 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021-present sms77 e.K.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Binary file added _screenshots/bulk_sms.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 50 additions & 36 deletions src/Controller/Backend/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
use Bolt\Extension\ExtensionController;
use Bolt\Extension\ExtensionRegistry;
use Bolt\Repository\ContentRepository;
use Bolt\Utils\Sanitiser;
use Sms77\Bolt\Extension;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Tightenco\Collect\Support\Collection;
use Twig\Environment;

class Controller extends ExtensionController {
public function __construct(Config $config, ExtensionRegistry $registry,
Expand All @@ -26,9 +26,7 @@ private function getExtensionConfig(): Collection {
return $this->registry->getExtension(Extension::class)->getConfig();
}

private function getApiKey(?Collection $collection) {
if (!$collection) $collection = $this->getExtensionConfig();

private function getApiKey(Collection $collection) {
return $collection->get('apiKey');
}

Expand All @@ -40,53 +38,69 @@ private function post(string $endpoint, array $data) {
'Accept: application/json',
'Content-type: application/json',
'SentWith: BoltCMS',
'X-Api-Key: ' . $this->getApiKey(),
'X-Api-Key: ' . $this->getApiKey($this->getExtensionConfig()),
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}

private function sms(array $data) {
return $this->post('sms', $data);
private function sms(string $to, string $text, array $data = []) {
return $this->post('sms', array_merge($data, compact('text', 'to')));
}

/** @Route("/sms77/bulk/sms", name="sms77_bulk_sms", methods={"GET", "POST"}) */
public function bulk_sms(): Response {
$cfg = $this->getExtensionConfig();
$req = $this->getRequest();
private function handleBulkSms(Request $req, array $mappings): void {
foreach ($mappings as $contentType => $phoneField) {
$contents = $this->contentRepository->findBy(['contentType' => $contentType]);

foreach ($contents as $content) {
if (!$content->hasField($phoneField)) continue;
$to = $content->getField($phoneField)->getValue();
if (!$to || empty($to)) continue;

if ('POST' === $req->getMethod())
foreach ($cfg['mappings'] as $contentType => $phoneField) {
$contents = $this->contentRepository->findBy(['contentType' => $contentType]);
$names = [];
foreach ($content->getFields() as $field) $names[] = $field->getName();
$text = $req->get('text');
$matches = [];
preg_match_all('{{{(' . implode('|', $names) . ')}}}', $text, $matches);

foreach ($contents as $content) {
if (!$content->hasField($phoneField)) continue;
$to = $content->getField($phoneField)->getValue();
if (!$to) continue;
$to = $to[0];
if ($matches) foreach ($matches[1] as $match) {
if (!$content->hasField($match)) continue;
$value = $content->getField($match)->getValue();
if (!$value || empty($value)) continue;

$fieldNames = [];
foreach ($content->getFields() as $field)
$fieldNames[] = $field->getName();
$text = str_replace('{{' . $match . '}}', $value[0], $text);
}

$text = $req->get('text');
$fieldNames = implode('|', $fieldNames);
$pattern = '{{{(' . $fieldNames . ')}}}';
$matches = [];
preg_match_all($pattern, $text, $matches);
$this->addFlash('notice',
$this->sms($to[0], $text, $this->getExtraSmsOptions($req->request)));
}
}
}

foreach ($matches[1] as $match) {
$value = $content->getField($match)->getValue();
if (!$value || empty($value)) continue;
private function getExtraSmsOptions(InputBag $bag): array {
$delay = $bag->get('delay');
if ($delay) $delay = (new \DateTime($delay))->getTimestamp();

return [
'debug' => (int)$bag->getBoolean('debug'),
'delay' => $delay,
'label' => $bag->get('label'),
'foreign_id' => $bag->get('foreign_id'),
'flash' => (int)$bag->getBoolean('flash'),
'from' => $bag->get('from'),
'no_reload' => (int)$bag->getBoolean('no_reload'),
'performance_tracking' => (int)$bag->getBoolean('performance_tracking'),
];
}

$text = str_replace('{{' . $match . '}}', $value[0], $text);
}
/** @Route("/sms77/bulk/sms", name="sms77_bulk_sms", methods={"GET", "POST"}) */
public function bulk_sms(): Response {
$cfg = $this->getExtensionConfig();
$req = $this->getRequest();

$this->addFlash('notice', $this->sms(compact('text', 'to')));
}
}
if ('POST' === $req->getMethod()) $this->handleBulkSms($req, $cfg['mappings']);

return $this->render('@sms77-bolt/bulk_sms.html.twig', $cfg->toArray());
}
Expand Down
25 changes: 25 additions & 0 deletions src/ReferenceWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace Sms77\Bolt;

use Bolt\Widget\BaseWidget;
use Bolt\Widget\CacheAwareInterface;
use Bolt\Widget\CacheTrait;
use Bolt\Widget\Injector\AdditionalTarget;
use Bolt\Widget\Injector\RequestZone;
use Bolt\Widget\StopwatchAwareInterface;
use Bolt\Widget\StopwatchTrait;
use Bolt\Widget\TwigAwareInterface;

class ReferenceWidget extends BaseWidget
implements TwigAwareInterface, CacheAwareInterface, StopwatchAwareInterface {
use CacheTrait;
use StopwatchTrait;

protected $cacheDuration = -1800;
protected $name = 'Sms77 BackWidget';
protected $priority = 200;
protected $target = AdditionalTarget::WIDGET_BACK_DASHBOARD_ASIDE_TOP;
protected $template = '@sms77-bolt/widget.html.twig';
protected $zone = RequestZone::BACKEND;
}
49 changes: 48 additions & 1 deletion templates/bulk_sms.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,57 @@
{% if apiKey is empty %}
<h1>Missing API key. Please go to settings and set one.</h1>
{% else %}
<form method='post'>
<form id='sms77_bulk_sms' method='post'>
<input type='hidden' name='_csrf_token'
value='{{ csrf_token('sms77_bulk_sms') }}'>

{% include '@bolt/_partials/fields/text.html.twig' with {
'label': 'Label',
'name': 'label',
} %}

{% include '@bolt/_partials/fields/text.html.twig' with {
'label': 'Foreign ID',
'name': 'foreign_id',
} %}

{% include '@bolt/_partials/fields/date.html.twig' with {
'form': 'sms77_bulk_sms',
'label': 'Delay',
'locale': app.user.locale,
'mode' : 'datetime',
'name': 'delay',
} %}

{% include '@bolt/_partials/fields/text.html.twig' with {
'label': 'From',
'name': 'from',
} %}

<div style='display: flex'>
{% include '@bolt/_partials/fields/checkbox.html.twig' with {
'label': 'Flash',
'name': 'flash',
} %}

{% include '@bolt/_partials/fields/checkbox.html.twig' with {
'label': 'Debug',
'name': 'debug',
} %}

{% include '@bolt/_partials/fields/checkbox.html.twig' with {
'label': 'No Reload',
'name': 'no_reload',
} %}

{% include '@bolt/_partials/fields/checkbox.html.twig' with {
'label': 'Performance Tracking',
'name': 'performance_tracking',
} %}
</div>

<hr>

{% include '@bolt/_partials/fields/textarea.html.twig' with {
'label': 'Text',
'name': 'text',
Expand Down
21 changes: 21 additions & 0 deletions templates/widget.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<div class='card mb-4'>
<div class='card-header'>
<i class='fas fa-plug'></i> {{ extension.name }}
</div>
<div class='card-body'>

<p>{{ extension.composerpackage.description }}</p>

<p>
<a href='/bolt/file-edit/config?file=extensions/sms77-bolt.yaml'
class='btn btn-secondary'>
<i class='fas fa-cog'></i> Config
</a>

<a href='{{ url('sms77_bulk_sms') }}'
class='btn btn-secondary'>
<i class='fas fa-envelope'></i> Bulk SMS
</a>
</p>
</div>
</div>

0 comments on commit cd8a3af

Please sign in to comment.