From: Vincent Vanwaelscappel Date: Tue, 15 Dec 2020 20:25:51 +0000 (+0100) Subject: wip #3753 @2 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=82d9104d3a0666d3f067032fa0dcda126aeb919c;p=cubedesigners_userdatabase.git wip #3753 @2 --- diff --git a/src/app/Models/Company.php b/src/app/Models/Company.php index 24d2dbb..9ec5804 100644 --- a/src/app/Models/Company.php +++ b/src/app/Models/Company.php @@ -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)); + } + } diff --git a/src/app/Models/User.php b/src/app/Models/User.php index 9f271cb..83f0f5a 100644 --- a/src/app/Models/User.php +++ b/src/app/Models/User.php @@ -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 index 0000000..5d5ab3c --- /dev/null +++ b/src/app/Operations/CreateFromCompany.php @@ -0,0 +1,37 @@ + $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); + } + +} diff --git a/src/app/Permissions.php b/src/app/Permissions.php index 90f98c1..209ca52 100644 --- a/src/app/Permissions.php +++ b/src/app/Permissions.php @@ -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 diff --git a/src/resources/views/columns/users.blade.php b/src/resources/views/columns/users.blade.php index b3d9bbc..344f74e 100644 --- a/src/resources/views/columns/users.blade.php +++ b/src/resources/views/columns/users.blade.php @@ -1 +1,13 @@ -getUsers() as $id=>$name) { + $c[]=''.$name.''; + } +@endphp + + + @includeWhen(!empty($column['wrapper']), 'crud::columns.inc.wrapper_start') + {!! implode(', ',$c) !!} + @includeWhen(!empty($column['wrapper']), 'crud::columns.inc.wrapper_end') + diff --git a/src/resources/views/fields/users.blade.php b/src/resources/views/fields/users.blade.php index b3d9bbc..2c9ce24 100644 --- a/src/resources/views/fields/users.blade.php +++ b/src/resources/views/fields/users.blade.php @@ -1 +1,20 @@ - +
+ {{__('Ajouter un contact')}} +
+ + @foreach($entry->getUsers() as $id=>$name) + + + + + + @endforeach +
{{$id}}{{$name}}{{__('Modifier')}}
+ +@push('crud_fields_styles') + +@endpush