]> _ Git - pmi.git/commitdiff
done #2975 @4
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 27 Aug 2019 16:13:51 +0000 (18:13 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 27 Aug 2019 16:13:51 +0000 (18:13 +0200)
app/Http/Controllers/AjaxController.php
app/Models/QuoteRequest.php [new file with mode: 0644]
resources/views/vendor/backpack/base/inc/sidebar_content.blade.php

index 0f982bb280394b8541b54af287ed437854dd07fa..de088bdf2973bd3440dba7dfdc2b1306b1ad9e47 100644 (file)
@@ -7,7 +7,9 @@ namespace App\Http\Controllers;
 use App\Models\Page;
 use App\Models\Product;
 use App\Models\ProductType;
+use App\Models\QuoteRequest;
 use App\Models\Specification;
+use Carbon\Carbon;
 use Cubist\Backpack\app\Http\Controllers\CubistFrontController;
 use Cubist\Backpack\app\Magic\PageData;
 use Cubist\Backpack\app\Magic\Search;
@@ -19,23 +21,27 @@ use Illuminate\Support\Str;
 
 class AjaxController extends CubistFrontController
 {
+    protected static $_labels = ['firstname' => 'Prénom', 'name' => 'Nom', 'company' => 'Société', 'sku' => 'Numéro de série', 'ref' => 'Reference', 'email' => 'Adresse e-mail', 'subject' => 'Sujet', 'message' => 'Message'];
+
     // Shared validation logic for dynamic CMS forms
-    protected function _validate_form(Request $request)
-    {
-        $data = $request->all();
-        /** @var PageData $page */
-        $page = Page::find($data['page'])->getPageData();
 
+    /**
+     * @param Request $request
+     * @param PageData $page
+     * @return array
+     */
+    protected function _validate_form(Request $request, $page)
+    {
         $validation = [];
         $messages = [];
 
-        $labels = ['firstname' => 'Prénom', 'name' => 'Nom', 'company' => 'Société', 'sku' => 'Numéro de série', 'ref' => 'Reference', 'email' => 'Adresse e-mail', 'subject' => 'Sujet', 'message' => 'Message'];
-
         foreach ($page->get('form') as $field) {
             $v = [];
             if ($field['mandatory']) {
                 $v[] = 'required';
-                $messages[$field['type'] . '.required'] = sprintf(__('Champ obligatoire'), $labels[$field['type']]);
+                $messages[$field['type'] . '.required'] = sprintf(__('Champ obligatoire'), self::$_labels[$field['type']]);
+            } else {
+                $v[] = 'nullable';
             }
             if ($field['type'] == 'email') {
                 $v[] = 'email';
@@ -54,16 +60,32 @@ class AjaxController extends CubistFrontController
         return $validatedData;
     }
 
+    /**
+     * @param Request $request
+     */
     public function mailform(Request $request)
     {
-        $validatedData = $this->_validate_form($request);
+        /** @var PageData $page */
+        $page = Page::find($request['page'])->getPageData();
+        $validatedData = $this->_validate_form($request, $page);
 
+        $this->_sendMail($validatedData, $page, []);
+    }
+
+    /**
+     * @param array $validatedData
+     * @param PageData $page
+     * @param array $appendContents
+     */
+    protected function _sendMail($validatedData, $page, $appendContents = [])
+    {
         $contents = [];
-        foreach ($labels as $key => $label) {
+        foreach (self::$_labels as $key => $label) {
             if (isset($validatedData[$key])) {
                 $contents[] = $label . ' : ' . $validatedData[$key];
             }
         }
+        $contents = array_merge($contents, $appendContents);
 
         Mail::raw(implode("\r\n", $contents), function ($message) use ($validatedData, $page) {
             $message->from(config('mail.from.address'), config('mail.from.name'));
@@ -146,14 +168,34 @@ class AjaxController extends CubistFrontController
 
     public function request_quote(Request $request)
     {
-
+        $page = Page::find($request['page'])->getPageData();
         // Array of product IDs => quantity
-        $cartData = (array)json_decode($request->input('cart_data'));
+        $cartData = json_decode($request->input('cart_data'), true);
 
         // Validated form fields
-        $validatedData = $this->_validate_form($request);
+        $validatedData = $this->_validate_form($request, $page);
+        $products = PageData::fromEntities(Product::whereIn('id', array_keys($cartData))->get());
+
+        $data = $validatedData;
+        $data['request_date'] = Carbon::now('Europe/Paris');
+        $data['status'] = 'new';
+        $data['locale'] = 'fr';
+        $data['products'] = [];
+        $productsMessage = ['Produits : (reférence : quantité)'];
+        foreach ($cartData as $id => $quantity) {
+            $data['products'][] = ['id' => $id, 'reference' => $products[$id]['reference'], 'name' => $products[$id]['name'], 'quantity' => $quantity];
+            $productsMessage[] = $products[$id]['reference'] . ' : ' . $quantity;
+        }
+        $data['products'] = json_encode($data['products']);
+        unset($data['page']);
+        unset($data['cart_data']);
+
+        $validatedData['subject'] = $data['company'];
+
+        $quote = new QuoteRequest($data);
+        $quote->save();
 
-        // Todo: save this data to db using quotes model, then send a notification e-mail with link to new record in the backend
+        $this->_sendMail($validatedData, $page, [implode("\r\n", $productsMessage), '', 'Voir la demande en ligne : ' . url('/admin/quotes/' . $quote->id . '/edit')]);
     }
 
     // Subscribe to newsletter via MailChimp API
diff --git a/app/Models/QuoteRequest.php b/app/Models/QuoteRequest.php
new file mode 100644 (file)
index 0000000..099f401
--- /dev/null
@@ -0,0 +1,81 @@
+<?php
+
+
+namespace App\Models;
+
+
+use Cubist\Backpack\app\Magic\Models\CubistMagicModel;
+
+class QuoteRequest extends CubistMagicModel
+{
+    protected $table = 'catalog_quotes';
+
+    protected $_options = ['name' => 'quotes',
+        'singular' => 'demande de devis',
+        'plural' => 'demandes de devis'];
+
+    public function setFields()
+    {
+        parent::setFields();
+
+        $tabcontact = 'Informations de contact';
+        $tabrequest = 'Demande de devis';
+
+        $this->addField(['name' => 'firstname',
+            'label' => 'Prénom',
+            'type' => 'Text',
+            'translatable' => false,
+            'tab' => $tabcontact]);
+
+        $this->addField(['name' => 'name',
+            'label' => 'Nom',
+            'type' => 'Text',
+            'translatable' => false,
+            'tab' => $tabcontact]);
+
+        $this->addField(['name' => 'company',
+            'label' => 'Société',
+            'type' => 'Text',
+            'translatable' => false,
+            'column' => true,
+            'tab' => $tabcontact]);
+
+        $this->addField(['name' => 'email',
+            'label' => 'E-mail',
+            'type' => 'Email',
+            'translatable' => false,
+            'column' => true,
+            'tab' => $tabcontact]);
+
+        $this->addField(['name' => 'locale',
+            'label' => 'Langue',
+            'type' => 'LocaleEnabled',
+            'tab' => $tabcontact]);
+
+        $this->addField(['name' => 'status',
+            'label' => 'Status',
+            'type' => 'SelectFromArray',
+            'column' => true,
+            'options' => ['new' => 'Nouvelle', 'wait' => 'En attente', 'done' => 'Traitée'],
+            'tab' => $tabrequest]);
+
+        $this->addField(['name' => 'request_date',
+            'label' => 'Date',
+            'type' => 'Datetime',
+            'column' => true,
+            'tab' => $tabrequest]);
+
+        $this->addField(['name' => 'products',
+            'label' => 'Produits',
+            'translatable' => false,
+            'type' => 'Table',
+            'columns' => ['id' => '#', 'reference' => 'Référence', 'name' => 'Nom de produit', 'quantity' => 'Quantité'],
+            'tab' => $tabrequest]);
+
+        $this->addField(['name' => 'message',
+            'label' => 'Message',
+            'translatable' => false,
+            'type' => 'Textarea',
+            'tab' => $tabrequest]);
+    }
+}
index e51462498af66b6f5184554a7be7ace000a03660..32b0402b829b9bf5810a2e691010618e5ff3b6f3 100644 (file)
@@ -28,6 +28,9 @@
     <li><a href='{{ backpack_url('specification') }}'><i class='fa fa-align-left'></i>
             <span>Spécifications</span></a>
     </li>
+    <li><a href='{{ backpack_url('quotes') }}'><i class='fa fa-table'></i>
+            <span>Demandes de devis</span></a>
+    </li>
 {{--    <li><a href='{{ backpack_url('catalog_category') }}'><i class='fa fa-folder-open'></i>--}}
 {{--            <span>Catégories</span></a>--}}
 {{--    </li>--}}