Skip to content

Commit 537df19

Browse files
committed
Contacts and contact types
1 parent edf0a58 commit 537df19

File tree

10 files changed

+337
-0
lines changed

10 files changed

+337
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateLaravelCrmContactContactTypeTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('laravel_crm_contact_contact_type', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->unsignedBigInteger('contact_id')->index()->nullable();
19+
$table->unsignedBigInteger('contact_type_id')->index()->nullable();
20+
$table->timestamps();
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::dropIfExists('laravel_crm_contact_contact_type');
32+
}
33+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateLaravelCrmContactTypesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('laravel_crm_contact_types', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->unsignedBigInteger('team_id')->index()->nullable();
19+
$table->string('name');
20+
$table->text('description')->nullable();
21+
$table->timestamps();
22+
$table->softDeletes();
23+
});
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
Schema::dropIfExists('laravel_crm_contact_types');
34+
}
35+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateLaravelCrmContactsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create(config('laravel-crm.db_table_prefix').'contacts', function (Blueprint $table) {
17+
$table->bigIncrements('id');
18+
$table->unsignedBigInteger('team_id')->index()->nullable();
19+
$table->string('external_id');
20+
$table->morphs('contactable');
21+
$table->morphs('entityable');
22+
$table->unsignedBigInteger('user_created_id')->nullable();
23+
$table->foreign('user_created_id')->references('id')->on('users');
24+
$table->unsignedBigInteger('user_updated_id')->nullable();
25+
$table->foreign('user_updated_id')->references('id')->on('users');
26+
$table->unsignedBigInteger('user_deleted_id')->nullable();
27+
$table->foreign('user_deleted_id')->references('id')->on('users');
28+
$table->unsignedBigInteger('user_restored_id')->nullable();
29+
$table->foreign('user_restored_id')->references('id')->on('users');
30+
$table->softDeletes();
31+
$table->timestamps();
32+
});
33+
}
34+
35+
/**
36+
* Reverse the migrations.
37+
*
38+
* @return void
39+
*/
40+
public function down()
41+
{
42+
Schema::dropIfExists(config('laravel-crm.db_table_prefix').'contacts');
43+
}
44+
}

database/seeders/LaravelCrmTablesSeeder.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,30 @@ public function run()
119119
foreach ($items as $item) {
120120
\VentureDrake\LaravelCrm\Models\AddressType::firstOrCreate($item[0], $item[1]);
121121
}
122+
123+
// Contact types
124+
$items = [
125+
[
126+
[
127+
'id' => 1,
128+
],
129+
[
130+
'name' => 'Primary',
131+
],
132+
],
133+
[
134+
[
135+
'id' => 2,
136+
],
137+
[
138+
'name' => 'Secondary',
139+
],
140+
],
141+
];
142+
143+
foreach ($items as $item) {
144+
\VentureDrake\LaravelCrm\Models\ContactType::firstOrCreate($item[0], $item[1]);
145+
}
122146

123147
app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
124148

@@ -142,6 +166,11 @@ public function run()
142166
Permission::firstOrCreate(['name' => 'edit crm organisations', 'crm_permission' => 1]);
143167
Permission::firstOrCreate(['name' => 'delete crm organisations', 'crm_permission' => 1]);
144168

169+
Permission::firstOrCreate(['name' => 'create crm contacts', 'crm_permission' => 1]);
170+
Permission::firstOrCreate(['name' => 'view crm contacts', 'crm_permission' => 1]);
171+
Permission::firstOrCreate(['name' => 'edit crm contacts', 'crm_permission' => 1]);
172+
Permission::firstOrCreate(['name' => 'delete crm contacts', 'crm_permission' => 1]);
173+
145174
Permission::firstOrCreate(['name' => 'create crm users', 'crm_permission' => 1]);
146175
Permission::firstOrCreate(['name' => 'view crm users', 'crm_permission' => 1]);
147176
Permission::firstOrCreate(['name' => 'edit crm users', 'crm_permission' => 1]);
@@ -204,6 +233,10 @@ public function run()
204233
'view crm organisations',
205234
'edit crm organisations',
206235
'delete crm organisations',
236+
'create crm contacts',
237+
'view crm contacts',
238+
'edit crm contacts',
239+
'delete crm contacts',
207240
]);
208241

209242
$role = Role::firstOrCreate(['name' => 'Employee', 'crm_role' => 1])
@@ -224,6 +257,10 @@ public function run()
224257
'view crm organisations',
225258
'edit crm organisations',
226259
'delete crm organisations',
260+
'create crm contacts',
261+
'view crm contacts',
262+
'edit crm contacts',
263+
'delete crm contacts',
227264
]);
228265
}
229266
}

resources/views/organisations/partials/card-show.blade.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@
6767
<p><span class="fa fa-user" aria-hidden="true"></span> {{ $person->name }}</p>
6868
@endforeach
6969
@endcan
70+
{{-- @can('view crm contacts')
71+
<h6 class="text-uppercase mt-4 section-h6-title"><span>{{ ucfirst(__('laravel-crm::lang.contacts')) }} ({{ $organisation->contacts->count() }})</span>@can('create crm contacts')<span class="float-right"><a href="{{ url(route('laravel-crm.contacts.create',['model' => 'organisation', 'id' => $organisation->id])) }}" class="btn btn-outline-secondary btn-sm"><span class="fa fa-plus" aria-hidden="true"></span></a></span>@endcan</h6>
72+
<hr />
73+
@foreach($organisation->contacts as $contacts)
74+
<p><span class="fa fa-user" aria-hidden="true"></span> {{ $contacts->name }}</p>
75+
@endforeach
76+
@endcan--}}
7077
@can('view crm deals')
7178
<h6 class="text-uppercase mt-4 section-h6-title"><span>{{ ucfirst(__('laravel-crm::lang.deals')) }} ({{ $organisation->deals->count() }})</span>@can('create crm deals')<span class="float-right"><a href="{{ url(route('laravel-crm.deals.create',['model' => 'organisation', 'id' => $organisation->id])) }}" class="btn btn-outline-secondary btn-sm"><span class="fa fa-plus" aria-hidden="true"></span></a></span>@endcan</h6>
7279
<hr />

resources/views/people/partials/card-show.blade.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@
7676
<dt class="col-sm-3 text-right">{{ ucfirst(__('laravel-crm::lang.address')) }}</dt>
7777
<dd class="col-sm-9">{{ ($organisation_address) ? \VentureDrake\LaravelCrm\Http\Helpers\AddressLine\addressSingleLine($organisation_address) : null }}</dd>
7878
</dl>
79+
{{--@can('view crm contacts')
80+
<h6 class="text-uppercase mt-4 section-h6-title"><span>{{ ucfirst(__('laravel-crm::lang.contacts')) }} ({{ $person->contacts->count() }})</span>@can('create crm contacts')<span class="float-right"><a href="{{ url(route('laravel-crm.contacts.create',['model' => 'person', 'id' => $person->id])) }}" class="btn btn-outline-secondary btn-sm"><span class="fa fa-plus" aria-hidden="true"></span></a></span>@endcan</h6>
81+
<hr />
82+
@foreach($person->contacts as $contacts)
83+
<p><span class="fa fa-user" aria-hidden="true"></span> {{ $contacts->name }}</p>
84+
@endforeach
85+
@endcan--}}
7986
@can('view crm deals')
8087
<h6 class="text-uppercase mt-4 section-h6-title"><span>{{ ucfirst(__('laravel-crm::lang.deals')) }} ({{ $person->deals->count() }})</span>@can('create crm deals')<span class="float-right"><a href="{{ url(route('laravel-crm.deals.create',['model' => 'person', 'id' => $person->id])) }}" class="btn btn-outline-secondary btn-sm"><span class="fa fa-plus" aria-hidden="true"></span></a></span>@endcan</h6>
8188
<hr />

src/LaravelCrmServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class LaravelCrmServiceProvider extends ServiceProvider
5555
'VentureDrake\LaravelCrm\Models\Deal' => \VentureDrake\LaravelCrm\Policies\DealPolicy::class,
5656
'VentureDrake\LaravelCrm\Models\Person' => \VentureDrake\LaravelCrm\Policies\PersonPolicy::class,
5757
'VentureDrake\LaravelCrm\Models\Organisation' => \VentureDrake\LaravelCrm\Policies\OrganisationPolicy::class,
58+
'VentureDrake\LaravelCrm\Models\Contact' => \VentureDrake\LaravelCrm\Policies\ContactPolicy::class,
5859
'VentureDrake\LaravelCrm\Models\Product' => \VentureDrake\LaravelCrm\Policies\ProductPolicy::class,
5960
'VentureDrake\LaravelCrm\Models\ProductCategory' => \VentureDrake\LaravelCrm\Policies\ProductCategoryPolicy::class,
6061
'VentureDrake\LaravelCrm\Models\Label' => \VentureDrake\LaravelCrm\Policies\LabelPolicy::class,
@@ -168,6 +169,9 @@ function ($perPage = 30, $page = null, $options = []) {
168169
__DIR__ . '/../database/migrations/alter_type_on_laravel_crm_phones_table.php.stub' => $this->getMigrationFileName($filesystem, 'alter_type_on_laravel_crm_phones_table.php', 15),
169170
__DIR__ . '/../database/migrations/add_description_to_laravel_crm_labels_table.php.stub' => $this->getMigrationFileName($filesystem, 'add_description_to_laravel_crm_labels_table.php', 16),
170171
__DIR__ . '/../database/migrations/add_name_to_laravel_crm_addresses_table.php.stub' => $this->getMigrationFileName($filesystem, 'add_name_to_laravel_crm_addresses_table.php', 17),
172+
__DIR__ . '/../database/migrations/create_laravel_crm_contacts_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_contacts_table.php', 18),
173+
__DIR__ . '/../database/migrations/create_laravel_crm_contact_types_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_contact_types_table.php', 19),
174+
__DIR__ . '/../database/migrations/create_laravel_crm_contact_contact_type_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_contact_contact_type_table.php', 20),
171175
], 'migrations');
172176

173177
// Publishing the seeders

src/Models/Contact.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace VentureDrake\LaravelCrm\Models;
4+
5+
use Illuminate\Database\Eloquent\SoftDeletes;
6+
use VentureDrake\LaravelCrm\Traits\BelongsToTeams;
7+
8+
class Contact extends Model
9+
{
10+
use SoftDeletes;
11+
use BelongsToTeams;
12+
13+
protected $guarded = ['id'];
14+
15+
public function getTable()
16+
{
17+
return config('laravel-crm.db_table_prefix').'contacts';
18+
}
19+
20+
public function contactTypes()
21+
{
22+
return $this->belongsToMany(ContactType::class);
23+
}
24+
25+
/**
26+
* Get all of the owning contactable models.
27+
*/
28+
public function contactable()
29+
{
30+
return $this->morphTo('contactable');
31+
}
32+
33+
/**
34+
* Get all of the owning entityable models.
35+
*/
36+
public function entityable()
37+
{
38+
return $this->morphTo('entityable');
39+
}
40+
}

src/Models/ContactType.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace VentureDrake\LaravelCrm\Models;
4+
5+
use Illuminate\Database\Eloquent\SoftDeletes;
6+
use VentureDrake\LaravelCrm\Traits\BelongsToTeams;
7+
8+
class ContactType extends Model
9+
{
10+
use SoftDeletes;
11+
use BelongsToTeams;
12+
13+
protected $guarded = ['id'];
14+
15+
public function getTable()
16+
{
17+
return config('laravel-crm.db_table_prefix').'contact_types';
18+
}
19+
20+
public function contacts()
21+
{
22+
return $this->belongsToMany(Contact::class);
23+
}
24+
}

src/Policies/ContactPolicy.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace VentureDrake\LaravelCrm\Policies;
4+
5+
use App\User;
6+
use Illuminate\Auth\Access\HandlesAuthorization;
7+
use VentureDrake\LaravelCrm\Models\Contact;
8+
9+
class ContactPolicy
10+
{
11+
use HandlesAuthorization;
12+
13+
/**
14+
* Determine whether the user can view any contacts.
15+
*
16+
* @param \App\User $user
17+
* @return mixed
18+
*/
19+
public function viewAny(User $user)
20+
{
21+
if ($user->hasPermissionTo('view crm contacts')) {
22+
return true;
23+
}
24+
}
25+
26+
/**
27+
* Determine whether the user can view the contact.
28+
*
29+
* @param \App\User $user
30+
* @param \App\Contact $contact
31+
* @return mixed
32+
*/
33+
public function view(User $user, Contact $contact)
34+
{
35+
if ($user->hasPermissionTo('view crm contacts')) {
36+
return true;
37+
}
38+
}
39+
40+
/**
41+
* Determine whether the user can create contacts.
42+
*
43+
* @param \App\User $user
44+
* @return mixed
45+
*/
46+
public function create(User $user)
47+
{
48+
if ($user->hasPermissionTo('create crm contacts')) {
49+
return true;
50+
}
51+
}
52+
53+
/**
54+
* Determine whether the user can update the contact.
55+
*
56+
* @param \App\User $user
57+
* @param \App\Contact $contact
58+
* @return mixed
59+
*/
60+
public function update(User $user, Contact $contact)
61+
{
62+
if ($user->hasPermissionTo('edit crm contacts')) {
63+
return true;
64+
}
65+
}
66+
67+
/**
68+
* Determine whether the user can delete the contact.
69+
*
70+
* @param \App\User $user
71+
* @param \App\Contact $contact
72+
* @return mixed
73+
*/
74+
public function delete(User $user, Contact $contact)
75+
{
76+
if ($user->hasPermissionTo('delete crm contacts')) {
77+
return true;
78+
}
79+
}
80+
81+
/**
82+
* Determine whether the user can restore the contact.
83+
*
84+
* @param \App\User $user
85+
* @param \App\Contact $contact
86+
* @return mixed
87+
*/
88+
public function restore(User $user, Contact $contact)
89+
{
90+
if ($user->hasPermissionTo('delete crm contacts')) {
91+
return true;
92+
}
93+
}
94+
95+
/**
96+
* Determine whether the user can permanently delete the contact.
97+
*
98+
* @param \App\User $user
99+
* @param \App\Contact $contact
100+
* @return mixed
101+
*/
102+
public function forceDelete(User $user, Contact $contact)
103+
{
104+
return true;
105+
}
106+
}

0 commit comments

Comments
 (0)