]> _ Git - cubedesigners_userdatabase.git/commitdiff
wip #3753 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 15 Dec 2020 20:25:51 +0000 (21:25 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 15 Dec 2020 20:25:51 +0000 (21:25 +0100)
src/app/Models/Company.php
src/app/Models/User.php
src/app/Operations/CreateFromCompany.php [new file with mode: 0644]
src/app/Permissions.php
src/resources/views/columns/users.blade.php
src/resources/views/fields/users.blade.php

index 24d2dbb1d5f3804e3594bda7c90151c17f389f51..9ec58046f6150a0b1320bdcca740155cfddb85c4 100644 (file)
@@ -3,6 +3,7 @@
 namespace Cubedesigners\UserDatabase\Models;
 
 use Cubedesigners\UserDatabase\Fields\Users;
+use Cubedesigners\UserDatabase\Permissions;
 use Cubedesigners\UserDatabase\SubForms\Address;
 use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel;
 use Illuminate\Database\Eloquent\Builder;
@@ -73,7 +74,7 @@ class Company extends CubistMagicAbstractModel
         $this->addField([
             'name' => 'users',
             'type' => Users::class,
-            'label' => '',
+            'label' => 'Contacts',
             'column' => true,
             'tab' => __('Contacts'),
         ]);
@@ -112,4 +113,9 @@ class Company extends CubistMagicAbstractModel
         $builder->whereIn('id', $bu->getManagedCompanies());
     }
 
+    public function getUsers()
+    {
+        return Permissions::getNames(Permissions::getUsersByCompany($this->id));
+    }
+
 }
index 9f271cbcbd27e853d5ccc1ae1df6d6c8222477ce..83f0f5a39bb7668f2117a69e757c4e9d3fbe747e 100644 (file)
@@ -15,7 +15,7 @@ class User extends CubistMagicAuthenticatable
         'singular' => 'utilisateur',
         'plural' => 'utilisateurs'];
 
-    protected $_operations = ['\Cubedesigners\UserDatabase\Operations\LoginasOperation'];
+    protected $_operations = ['\Cubedesigners\UserDatabase\Operations\LoginasOperation','\Cubedesigners\UserDatabase\Operations\CreateFromCompany'];
 
     protected $_managedUsers = null;
     protected $_managedCompanies = null;
diff --git a/src/app/Operations/CreateFromCompany.php b/src/app/Operations/CreateFromCompany.php
new file mode 100644 (file)
index 0000000..5d5ab3c
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+
+namespace Cubedesigners\UserDatabase\Operations;
+
+use Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
+use Cubedesigners\UserDatabase\Models\Company;
+use Cubedesigners\UserDatabase\Models\User;
+use Illuminate\Support\Facades\Route;
+
+trait CreateFromCompany
+{
+    protected function setupCreateFromCompanyRoutes($segment, $routeName, $controller)
+    {
+        Route::get($segment . '/createfromcompany/{id}', [
+            'as' => $routeName . '.createfromcompany',
+            'uses' => $controller . '@createFromCompany',
+            'operation' => 'create',
+        ]);
+    }
+
+    public function createFromCompany($id)
+    {
+        $this->crud->hasAccessOrFail('create');
+
+        // prepare the fields you need to show
+        $this->data['crud'] = $this->crud;
+        $this->data['saveAction'] = $this->crud->getSaveAction();
+        $this->data['title'] = $this->crud->getTitle() ?? trans('backpack::crud.add') . ' ' . $this->crud->entity_name;
+
+        $company = Company::find($id);
+        dd($this->data);
+
+        // load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
+        return view($this->crud->getCreateView(), $this->data);
+    }
+
+}
index 90f98c1c1c41454bafa628b5b5e6a4a43d1f48be..209ca52a88c1feaaf7e30fd8cd588a8938969517 100644 (file)
@@ -18,7 +18,7 @@ class Permissions
         $cacheKey = 'cubedesigners_userdatabase_permissions_data';
 
         \Barryvdh\Debugbar\Facade::startMeasure($cacheKey, 'Build permissions tree');
-        self::$_data = cache()->remember($cacheKey, 86400, function () {
+        self::$_data = cache()->tags(['model_' . Company::class, 'model_' . User::class])->remember($cacheKey, 86400, function () {
 
             set_time_limit(0);
 
@@ -28,6 +28,7 @@ class Permissions
                 'companyManagedBy' => [],
                 'managedCompanies' => [],
                 'managedUsers' => [],
+                'userNames' => [],
             ];
 
             foreach (User::withoutGlobalScopes()->get() as $user) {
@@ -38,6 +39,7 @@ class Permissions
 
                 $data['companyUsers'][$user->company][] = $user->id;
                 $data['usersCompany'][$user->id] = $user->company;
+                $data['userNames'][$user->id] = $user->name;
             }
 
             foreach (Company::withoutGlobalScopes()->get() as $company) {
@@ -137,6 +139,18 @@ class Permissions
         return [];
     }
 
+    public static function getNames($ids)
+    {
+        $data = self::_getData();
+        $res = [];
+        foreach ($ids as $id) {
+            if (isset($data['userNames'][$id])) {
+                $res[$id] = $data['userNames'][$id];
+            }
+        }
+        return $res;
+    }
+
 
     /**
      * @param $user integer
index b3d9bbc7f3711e882119cd6b3af051245d859d04..344f74e592b93d0ae03e3ec2be1d34410db0e405 100644 (file)
@@ -1 +1,13 @@
-<?php
+{{-- regular object attribute --}}
+@php
+    $c=[];
+    foreach ($entry->getUsers() as $id=>$name) {
+        $c[]='<a data-featherlight="iframe" data-featherlight-iframe-style="display:block;width:85vw;height:85vh;" href="'.backpack_url('users/'.$id.'/edit?embeded=1').'">'.$name.'</a>';
+    }
+@endphp
+
+<span>
+    @includeWhen(!empty($column['wrapper']), 'crud::columns.inc.wrapper_start')
+    <span class="textarea-value">{!! implode(', ',$c) !!}</span>
+    @includeWhen(!empty($column['wrapper']), 'crud::columns.inc.wrapper_end')
+</span>
index b3d9bbc7f3711e882119cd6b3af051245d859d04..2c9ce24ede901ff221ded21aaf01396ca00cf981 100644 (file)
@@ -1 +1,20 @@
-<?php
+{{-- regular object attribute --}}
+<div class="company-userlist">
+    <div class="add">
+    <a data-featherlight="iframe" data-featherlight-iframe-style="display:block;width:85vw;height:85vh;"
+       href="{!! backpack_url('users/createfromcompany/'.$entry->id.'?embeded=1') !!}" class="btn btn-light btn-small"><i class="la la-plus"></i> {{__('Ajouter un contact')}}</a>
+    </div>
+    <table>
+        @foreach($entry->getUsers() as $id=>$name)
+            <tr>
+                <td>{{$id}}</td>
+                <td>{{$name}}</td>
+                <td><a data-featherlight="iframe" data-featherlight-iframe-style="display:block;width:85vw;height:85vh;"
+                       href="{!! backpack_url('users/'.$id.'/edit?embeded=1') !!}">{{__('Modifier')}}</a></td>
+            </tr>
+        @endforeach
+    </table>
+</div>
+@push('crud_fields_styles')
+
+@endpush