diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ea34216 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/_screenshots/bulk_sms.png b/_screenshots/bulk_sms.png new file mode 100644 index 0000000..f38b7d3 Binary files /dev/null and b/_screenshots/bulk_sms.png differ diff --git a/src/Controller/Backend/Controller.php b/src/Controller/Backend/Controller.php index b8b1e30..5ad7a25 100644 --- a/src/Controller/Backend/Controller.php +++ b/src/Controller/Backend/Controller.php @@ -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, @@ -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'); } @@ -40,7 +38,7 @@ 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); @@ -48,45 +46,61 @@ private function post(string $endpoint, array $data) { 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()); } diff --git a/src/ReferenceWidget.php b/src/ReferenceWidget.php new file mode 100644 index 0000000..85e5dae --- /dev/null +++ b/src/ReferenceWidget.php @@ -0,0 +1,25 @@ +Missing API key. Please go to settings and set one. {% else %} -
+ + {% 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', + } %} + +
+ {% 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', + } %} +
+ +
+ {% include '@bolt/_partials/fields/textarea.html.twig' with { 'label': 'Text', 'name': 'text', diff --git a/templates/widget.html.twig b/templates/widget.html.twig new file mode 100644 index 0000000..79d53fe --- /dev/null +++ b/templates/widget.html.twig @@ -0,0 +1,21 @@ +
+
+ {{ extension.name }} +
+
+ +

{{ extension.composerpackage.description }}

+ +

+ + Config + + + + Bulk SMS + +

+
+