Skip to content

Commit 153fe38

Browse files
committed
Show related notes on linked contacts
1 parent 63eaae2 commit 153fe38

15 files changed

+135
-11
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AddRelatedNoteToLaravelCrmNotesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table(config('laravel-crm.db_table_prefix').'notes', function (Blueprint $table) {
17+
$table->integer('related_note_id')->after('pinned')->nullable();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table(config('laravel-crm.db_table_prefix').'notes', function (Blueprint $table) {
29+
$table->dropColumn([
30+
'related_note_id',
31+
]);
32+
});
33+
}
34+
}

resources/views/livewire/notes.blade.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,20 @@
2121
<div class="card-body">
2222
{{--<img src="..." class="mr-3" alt="...">--}}
2323
<div class="media-body">
24-
<h5 class="mt-0 mb-1">{{ $note->created_at->diffForHumans() }} - {{ $note->createdByUser->name }}</h5>
25-
{{ $note->content }}
24+
@if($note->relatedNote)
25+
<h5 class="mt-0 mb-1">{{ $note->relatedNote->created_at->diffForHumans() }} - {{ $note->relatedNote->createdByUser->name }}</h5>
26+
<p class="pb-0 mb-2">
27+
@if($note->relatedNote->noteable instanceof \VentureDrake\LaravelCrm\Models\Person)
28+
<span class="fa fa-user mr-1" aria-hidden="true"></span> <a href="{{ route('laravel-crm.people.show', $note->relatedNote->noteable) }}">{{ $note->relatedNote->noteable->name }}</a>
29+
@elseif($note->relatedNote->noteable instanceof \VentureDrake\LaravelCrm\Models\Organisation)
30+
<span class="fa fa-building mr-1" aria-hidden="true"></span> <a href="{{ route('laravel-crm.organisations.show', $note->relatedNote->noteable) }}">{{ $note->relatedNote->noteable->name }}</a>
31+
@endif
32+
</p>
33+
{{ $note->relatedNote->content }} TEST
34+
@else
35+
<h5 class="mt-0 mb-1">{{ $note->created_at->diffForHumans() }} - {{ $note->createdByUser->name }}</h5>
36+
{{ $note->content }}
37+
@endif
2638
</div>
2739
</div>
2840
</div>

resources/views/livewire/related-contact-organisations.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<h6 class="text-uppercase mt-4 section-h6-title"><span>{{ ucfirst(__('laravel-crm::lang.related_organizations')) }} ({{ $contacts->count() }})</span>@can('create crm contacts')<span class="float-right"><a href="#" data-toggle="modal" data-target="#linkOrganisationModal" class="btn btn-outline-secondary btn-sm"><span class="fa fa-plus" aria-hidden="true"></span></a></span>@endcan</h6>
44
<hr />
55
@foreach($contacts as $contact)
6-
<p><span class="fa fa-building" aria-hidden="true"></span> {{ $contact->entityable->name }}</p>
6+
<p><span class="fa fa-building mr-1" aria-hidden="true"></span> <a href="{{ route('laravel-crm.organisations.show',$contact->entityable) }}">{{ $contact->entityable->name }}</a> <span class="float-right"><button wire:click.prevent="remove({{ $contact->entityable->id }})" type="button" class="btn btn-outline-danger btn-sm"><span class="fa fa-remove"></span></button></span></p>
77
@endforeach
88

99
<!-- Modal -->

resources/views/livewire/related-contact-people.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<h6 class="text-uppercase mt-4 section-h6-title"><span>{{ ucfirst(__('laravel-crm::lang.related_people')) }} ({{ $contacts->count() }})</span>@can('create crm contacts')<span class="float-right"><a href="#" data-toggle="modal" data-target="#linkPersonModal" class="btn btn-outline-secondary btn-sm"><span class="fa fa-plus" aria-hidden="true"></span></a></span>@endcan</h6>
44
<hr />
55
@foreach($contacts as $contact)
6-
<p><span class="fa fa-user" aria-hidden="true"></span> {{ $contact->entityable->name }}</p>
6+
<p><span class="fa fa-user mr-1" aria-hidden="true"></span> <a href="{{ route('laravel-crm.people.show',$contact->entityable) }}">{{ $contact->entityable->name }}</a> <span class="float-right"><button wire:click.prevent="remove({{ $contact->entityable->id }})" type="button" class="btn btn-outline-danger btn-sm"><span class="fa fa-remove"></span></button></span></p>
77
@endforeach
88

99
<!-- Modal -->

resources/views/livewire/related-people.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<h6 class="text-uppercase mt-4 section-h6-title"><span>{{ ucfirst(__('laravel-crm::lang.people')) }} ({{ $people->count() }})</span>@can('create crm people')<span class="float-right"><a href="#" data-toggle="modal" data-target="#linkPersonModal" class="btn btn-outline-secondary btn-sm"><span class="fa fa-plus" aria-hidden="true"></span></a></span>@endcan</h6>
44
<hr />
55
@foreach($people as $person)
6-
<p><span class="fa fa-user" aria-hidden="true"></span> {{ $person->name }}</p>
6+
<p><span class="fa fa-user mr-1" aria-hidden="true"></span> <a href="{{ route('laravel-crm.people.show',$person) }}">{{ $person->name }}</a> <span class="float-right"><button wire:click.prevent="remove({{ $person->id }})" type="button" class="btn btn-outline-danger btn-sm"><span class="fa fa-remove"></span></button></span></p>
77
@endforeach
88

99
<!-- Modal -->

src/Http/Livewire/LiveNote.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use Livewire\Component;
66
use Ramsey\Uuid\Uuid;
7+
use VentureDrake\LaravelCrm\Models\Contact;
8+
use VentureDrake\LaravelCrm\Models\Organisation;
9+
use VentureDrake\LaravelCrm\Models\Person;
710

811
class LiveNote extends Component
912
{
@@ -23,10 +26,34 @@ public function create()
2326
'content' => 'required',
2427
]);
2528

26-
$this->model->notes()->create([
29+
$note = $this->model->notes()->create([
2730
'external_id' => Uuid::uuid4()->toString(),
2831
'content' => $data['content'],
2932
]);
33+
34+
// Add to any upstream related models
35+
if ($this->model instanceof Person) {
36+
if ($this->model->organisation) {
37+
$this->model->organisation->notes()->create([
38+
'external_id' => Uuid::uuid4()->toString(),
39+
'content' => $data['content'],
40+
'related_note_id' => $note->id,
41+
]);
42+
}
43+
}
44+
45+
if ($this->model instanceof Organisation || $this->model instanceof Person) {
46+
foreach (Contact::where([
47+
'entityable_type' => $this->model->getMorphClass(),
48+
'entityable_id' => $this->model->id,
49+
])->get() as $contact) {
50+
$contact->contactable->notes()->create([
51+
'external_id' => Uuid::uuid4()->toString(),
52+
'content' => $data['content'],
53+
'related_note_id' => $note->id,
54+
]);
55+
}
56+
}
3057

3158
$this->resetFields();
3259
}

src/Http/Livewire/LiveRelatedContactOrganisation.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ public function link()
4848
$this->dispatchBrowserEvent('linkedOrganisation');
4949
}
5050

51+
public function remove($id)
52+
{
53+
if ($organisation = Organisation::find($id)) {
54+
$this->model->contacts()
55+
->where([
56+
'entityable_type' => $organisation->getMorphClass(),
57+
'entityable_id' => $organisation->id,
58+
])
59+
->delete();
60+
}
61+
62+
$this->getContacts();
63+
64+
$this->dispatchBrowserEvent('linkedOrganisation');
65+
}
66+
5167
public function updatedOrganisationName($value)
5268
{
5369
$this->dispatchBrowserEvent('updatedNameFieldAutocomplete');

src/Http/Livewire/LiveRelatedContactPerson.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ public function link()
5151
$this->dispatchBrowserEvent('linkedPerson');
5252
}
5353

54+
public function remove($id)
55+
{
56+
if ($person = Person::find($id)) {
57+
$this->model->contacts()
58+
->where([
59+
'entityable_type' => $person->getMorphClass(),
60+
'entityable_id' => $person->id,
61+
])
62+
->delete();
63+
}
64+
65+
$this->getContacts();
66+
67+
$this->dispatchBrowserEvent('linkedPerson');
68+
}
69+
5470
public function updatedPersonName($value)
5571
{
5672
$this->dispatchBrowserEvent('updatedNameFieldAutocomplete');

src/Http/Livewire/LiveRelatedPerson.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ public function link()
4848

4949
$this->dispatchBrowserEvent('linkedPerson');
5050
}
51+
52+
public function remove($id)
53+
{
54+
if ($person = Person::find($id)) {
55+
$person->update([
56+
'organisation_id' => null,
57+
]);
58+
}
59+
60+
$this->getPeople();
61+
62+
$this->dispatchBrowserEvent('linkedPerson');
63+
}
5164

5265
public function updatedPersonName($value)
5366
{

src/LaravelCrmServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ function ($perPage = 30, $page = null, $options = []) {
200200
__DIR__ . '/../database/migrations/update_logins_and_devices_table_user_relation.php.stub' => $this->getMigrationFileName($filesystem, 'update_logins_and_devices_table_user_relation.php', 25), // Laravel Auth Checker
201201
__DIR__ . '/../database/migrations/create_laravel_crm_organisation_types_table.php.stub' => $this->getMigrationFileName($filesystem, 'create_laravel_crm_organisation_types_table.php', 26),
202202
__DIR__ . '/../database/migrations/change_morph_col_names_on_laravel_crm_notes_table.php.stub' => $this->getMigrationFileName($filesystem, 'change_morph_col_names_on_laravel_crm_notes_table.php', 27),
203+
__DIR__ . '/../database/migrations/add_related_note_to_laravel_crm_notes_table.php.stub' => $this->getMigrationFileName($filesystem, 'add_related_note_to_laravel_crm_notes_table.php', 28),
203204
], 'migrations');
204205

205206
// Publishing the seeders

0 commit comments

Comments
 (0)