]> _ Git - fluidbook-toolbox.git/commitdiff
wait #5517 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 10 Oct 2022 08:16:00 +0000 (10:16 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 10 Oct 2022 08:16:00 +0000 (10:16 +0200)
app/Http/Controllers/Admin/Operations/FluidbookQuote/AssignOperation.php
app/Http/Controllers/Admin/Operations/FluidbookQuote/CreateFromWebsite.php
app/Jobs/DownloadBase.php
app/Mail/FluidbookQuoteReceived.php
app/Slack/Slack.php [new file with mode: 0644]
resources/views/vendor/backpack/crud/buttons/fluidbook_quote/assign.blade.php

index 2bdd266b0d22c93bb27349787ead008c59061d76..9de93b77fda268d7d0419391e3c78fc30d770bd0 100644 (file)
@@ -25,10 +25,17 @@ trait AssignOperation
 
     protected function assign($id, $reseller)
     {
+        if (!can('fluidbook-quote:admin')) {
+            abort(403);
+        }
+        $me = backpack_user()->id;
+        if ($reseller === 'me') {
+            $reseller = $me;
+        }
         $quote = FluidbookQuote::where('id', $id)->first();
         $quote->reseller = $reseller;
         $quote->status = 1;
-        $quote->admin = backpack_user()->id;
+        $quote->admin = $me;
         $quote->save();
 
         if ($quote->reseller != $quote->admin) {
index 50c45860badc33ad5d18c15b2530753f46422cb4..a3eb0e5041a5e3d7e9e66d72d4abefb926134520 100644 (file)
@@ -10,6 +10,7 @@ use App\Mail\FluidbookQuoteReceived;
 use App\Models\Company;
 use App\Models\FluidbookQuote;
 use App\Models\AuthUser;
+use App\Slack\Slack;
 use Cubist\Util\ArrayUtil;
 use Illuminate\Support\Facades\Mail;
 use Illuminate\Support\Facades\Route;
@@ -83,6 +84,10 @@ trait CreateFromWebsite
 
             $resp['message'] = __("Votre demande de devis a bien été envoyée. Merci pour l'intérêt que vous portez à Fluidbook.", [], $post['lang']);
 
+            if ($post && !$spam) {
+                Slack::send('C045CH0UB47', 'Demande de devis', FluidbookQuoteReceived::getMarkdown($post), ['Je vais voir' => 'https://toolbox.fluidbook.com/fluidbook-quote?status=0', 'Je m\'en charge' => 'https://toolbox.fluidbook.com/fluidbook-quote/' . $quote->id . '/assign/me']);
+            }
+
             $quoteMail = new FluidbookQuoteReceived();
             $quoteMail->setData($post);
             Mail::send($quoteMail);
index 837c546933f4b47f23ce7371730ec78458dd8883..2856fda1ef427c235566f862b2ab98d0628272c2 100644 (file)
@@ -5,6 +5,8 @@ namespace App\Jobs;
 use App\Mail\DeferredDownload;
 use App\Models\User;
 use App\Services\ScormCloud;
+use App\Slack\Message;
+use App\Slack\Slack;
 use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel;
 use Cubist\Util\Files\Files;
 use Cubist\Util\Str;
@@ -56,62 +58,7 @@ class DownloadBase extends Base
 
     public function sendSlack($subject, $text, $actions = [])
     {
-        $client = ClientFactory::create(env('SLACK_BOT_TOKEN'));
-        $blocks = [
-            [
-                'type' => 'section',
-                'text' => [
-                    'type' => 'mrkdwn',
-                    'text' => '*' . $subject . '*',
-                ]
-            ]
-        ];
-        if ($text) {
-            $blocks[] = [
-                'type' => 'section',
-                'text' => [
-                    'type' => 'mrkdwn',
-                    'text' => $text,
-                ]
-            ];
-        }
-        if (count($actions) > 0) {
-            $a = [
-                'type' => 'actions',
-                'block_id' => 'actions',
-                'elements' => [],
-            ];
-            $text = '';
-            $i = 0;
-            foreach ($actions as $label => $url) {
-                $a['elements'][] = [
-                    'type' => 'button',
-                    'text' => [
-                        'type' => 'plain_text',
-                        'text' => $label
-                    ],
-                    'url' => $url,
-                ];
-                $i++;
-                $text .= '> ' . $label . ' : ' . $url . "\n";
-            }
-            $blocks[] = [
-                'type' => 'section',
-                'text' => [
-                    'type' => 'mrkdwn',
-                    'text' => $text,
-                ]
-            ];
-            $blocks[] = $a;
-        }
-
-        $client->chatPostMessage([
-            'username' => 'Fluidbook Toolbox',
-            'channel' => $this->getUser()->slack,
-            'blocks' => json_encode($blocks),
-            'text' => $subject,
-            'unfurl_links' => false,
-        ]);
+        Slack::send($this->getUser()->slack, $subject, $text, $actions);
     }
 
     public function sendEmail($subject, $text, $actions = [])
index a23d91a09f135f9e5bb61e62155a58c04960aa7b..8e5d2ae471445e177f2a7ee709097702687e9284 100644 (file)
@@ -3,14 +3,32 @@
 namespace App\Mail;
 
 use Cubist\Locale\Country;
-use Illuminate\Bus\Queueable;
-use Illuminate\Mail\Mailable;
-use Illuminate\Queue\SerializesModels;
+use League\CommonMark\CommonMarkConverter;
 
 class FluidbookQuoteReceived extends Base
 {
 
     public function setData($post)
+    {
+        $this->from($post['email'], $post['prenom'] . ' ' . $post['nom']);
+        $this->to('tech@fluidbook.com');
+        $spam = '';
+        if ($post['spam'] === 'Oui') {
+            $spam = ' *** SPAM ***';
+        }
+        $this->subject('[Fluidbook Workshop]' . $spam . ' Demande de devis');
+
+        if (!$spam) {
+            $footer = 'Veuillez-vous rendre sur la [Toolbox](https://toolbox.fluidbook.com/fluidbook-quote) pour prendre en charge la demande ou la confier à un revendeur';
+        } else {
+            $footer = 'Cette demande est un SPAM supposé. Elle n\'a pas été enregistrée dans la Toolbox.';
+        }
+        $markdown = self::getMarkdown($post, $footer);
+        $converter = new CommonMarkConverter();
+        $this->html($converter->convertToHtml($markdown));
+    }
+
+    public static function getMarkdown($post, $footer = '')
     {
         $fields = array(
             'spam' => 'Demande SPAM',
@@ -35,34 +53,23 @@ class FluidbookQuoteReceived extends Base
             'gclid' => 'Google Ads ID (gclid)'
         );
 
-        $this->from($post['email'], $post['prenom'] . ' ' . $post['nom']);
-        $this->to('tech@fluidbook.com');
-        $spam = '';
-        if ($post['spam'] === 'Oui') {
-            $spam = ' *** SPAM ***';
-        }
-        $this->subject('[Fluidbook Workshop]' . $spam . ' Demande de devis');
-
-        $body = '<html><body><p>Une demande de devis a été envoyée avec les informations suivantes :</p><ul>';
+        $res = 'Une demande de devis a été envoyée avec les informations suivantes :' . "\n\n";
         foreach ($fields as $f => $d) {
             if ($post[$f] != '') {
+                $res .= '- ' . $d . ' : ';
                 if ($f == 'email') {
-                    $body .= '<li>' . $d . ' : <a href="mailto:' . $post[$f] . '">' . $post[$f] . '</a></li>';
+                    $res .= $post[$f];
                 } elseif ($f == 'pays') {
-                    $body .= '<li>' . $d . ' : ' . Country::translate($post[$f], 'fr') . '</li>';
+                    $res .= Country::translate($post[$f], 'fr');
                 } else {
-                    $body .= '<li>' . $d . ' : ' . nl2br($post[$f]) . '</li>';
+                    $res .= $post[$f];
                 }
+                $res .= "\n";
             }
         }
-        $body .= "</ul><p>";
-        if (!$spam) {
-            $body .= 'Veuillez-vous rendre sur la <a href="https://toolbox.fluidbook.com/fluidbook-quote">Toolbox</a> pour prendre en charge la demande ou la confier à un revendeur';
-        } else {
-            $body .= 'Cette demande est un SPAM supposé. Elle n\'a pas été enregistrée dans la Toolbox.';
+        if ($footer !== '') {
+            $res .= "\n" . $footer . "\n";
         }
-        $body .= '</p></body></html>';
-        $this->html($body);
-
+        return $res;
     }
 }
diff --git a/app/Slack/Slack.php b/app/Slack/Slack.php
new file mode 100644 (file)
index 0000000..358d30c
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+
+namespace App\Slack;
+
+use JoliCode\Slack\ClientFactory;
+
+class Slack
+{
+    public static function send($channel, $subject, $text, $actions = [], $from = 'Fluidbook Toolbox')
+    {
+        $client = ClientFactory::create(env('SLACK_BOT_TOKEN'));
+        $blocks = [
+            [
+                'type' => 'section',
+                'text' => [
+                    'type' => 'mrkdwn',
+                    'text' => '*' . $subject . '*',
+                ]
+            ]
+        ];
+        if ($text) {
+            $blocks[] = [
+                'type' => 'section',
+                'text' => [
+                    'type' => 'mrkdwn',
+                    'text' => $text,
+                ]
+            ];
+        }
+        if (count($actions) > 0) {
+            $a = [
+                'type' => 'actions',
+                'block_id' => 'actions',
+                'elements' => [],
+            ];
+            $text = '';
+            $i = 0;
+            foreach ($actions as $label => $url) {
+                $a['elements'][] = [
+                    'type' => 'button',
+                    'text' => [
+                        'type' => 'plain_text',
+                        'text' => $label
+                    ],
+                    'url' => $url,
+                ];
+                $i++;
+                $text .= '> ' . $label . ' : ' . $url . "\n";
+            }
+            $blocks[] = [
+                'type' => 'section',
+                'text' => [
+                    'type' => 'mrkdwn',
+                    'text' => $text,
+                ]
+            ];
+            $blocks[] = $a;
+        }
+
+        $client->chatPostMessage([
+            'username' => $from,
+            'channel' => $channel,
+            'blocks' => json_encode($blocks),
+            'text' => $subject,
+            'unfurl_links' => false,
+        ]);
+    }
+}
index 07d3d8114638de2890955e45d347d76b2bc22871..c3c4e331223fe86dd03b5df44e15e939a9999353 100644 (file)
@@ -1,6 +1,6 @@
 @if($entry->status==0)
     <a class="btn btn-sm btn-link assignto" href="#" data-toggle="tooltip"
-       data-context-actions="{{json_encode([backpack_user()->id=>__('Je vais traiter la demander'),'94'=>__('Confier à :reseller',['reseller'=>'Kadreo'])])}}"
+       data-context-actions="{{json_encode(['me'=>__('Je vais traiter la demander'),'94'=>__('Confier à :reseller',['reseller'=>'Kadreo'])])}}"
        data-context-route="{{$crud->route}}/$id/assign/$action"
        data-context-id="{{$entry->getKey()}}"
        data-context-confirm="{{json_encode(