Skip to content

Commit 5587782

Browse files
committed
added notifications and email templates
1 parent 1fc64fd commit 5587782

27 files changed

+429
-28
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
/.vscode
33
/.vagrant
44
/node_modules
5-
/Resources/assets/js/my-blog.min.js
5+
/Resources/assets/js/*.min.js
66
/storage/*.keyr
7-
/vendor
7+
/vendor

Database/Seeds/EmailTemplates.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Modules\MyBlog\Database\Seeds;
4+
5+
use App\Abstracts\Model;
6+
use App\Models\Common\EmailTemplate;
7+
use Illuminate\Database\Seeder;
8+
9+
class EmailTemplates extends Seeder
10+
{
11+
public function run()
12+
{
13+
Model::unguard();
14+
15+
$this->create();
16+
17+
Model::reguard();
18+
}
19+
20+
private function create()
21+
{
22+
$company_id = $this->command->argument('company');
23+
24+
$templates = [
25+
[
26+
'alias' => 'comment_new_author',
27+
'class' => 'Modules\MyBlog\Notifications\Comment',
28+
'name' => 'my-blog::settings.email.templates.comment_new_author',
29+
],
30+
[
31+
'alias' => 'comment_delete_author',
32+
'class' => 'Modules\MyBlog\Notifications\Comment',
33+
'name' => 'my-blog::settings.email.templates.comment_delete_author',
34+
],
35+
];
36+
37+
foreach ($templates as $template) {
38+
EmailTemplate::firstOrCreate([
39+
'company_id' => $company_id,
40+
'alias' => $template['alias'],
41+
'class' => $template['class'],
42+
'name' => $template['name'],
43+
'subject' => trans('my-blog::email_templates.' . $template['alias'] . '.subject'),
44+
'body' => trans('my-blog::email_templates.' . $template['alias'] . '.body'),
45+
]);
46+
}
47+
}
48+
}

Database/Seeds/Install.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function run()
1818

1919
$this->call(Categories::class);
2020
$this->call(Dashboards::class);
21+
$this->call(EmailTemplates::class);
2122

2223
Model::reguard();
2324
}

Http/Controllers/Portal/Comments.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Modules\MyBlog\Http\Controllers\Portal;
4+
5+
use App\Abstracts\Http\Controller;
6+
use Illuminate\Http\Response;
7+
use Modules\MyBlog\Http\Requests\Comment as Request;
8+
use Modules\MyBlog\Jobs\CreateComment;
9+
10+
class Comments extends Controller
11+
{
12+
/**
13+
* Store a newly created resource in storage.
14+
*
15+
* @param Request $request
16+
*
17+
* @return Response
18+
*/
19+
public function store(Request $request)
20+
{
21+
$response = $this->ajaxDispatch(new CreateComment($request));
22+
23+
$response['redirect'] = route('portal.my-blog.posts.show', $request->get('post_id'));
24+
25+
if ($response['success']) {
26+
$message = trans('messages.success.added', ['type' => trans_choice('my-blog::general.comments', 1)]);
27+
28+
flash($message)->success();
29+
} else {
30+
$message = $response['message'];
31+
32+
flash($message)->error()->important();
33+
}
34+
35+
return response()->json($response);
36+
}
37+
}

Http/Controllers/Portal/Posts.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@ public function index()
2727
*/
2828
public function show(Post $post)
2929
{
30-
$post->load('comments');
30+
if (setting('my-blog.enable_comments')) {
31+
$post->load('category', 'comments');
3132

32-
return view('my-blog::portal.posts.show', compact('post'));
33+
$limit = (int) request('limit', setting('default.list_limit', '25'));
34+
$comments = $this->paginate($post->comments->sortByDesc('created_at'), $limit);
35+
} else {
36+
$post->load('category');
37+
38+
$comments = [];
39+
}
40+
41+
return view('my-blog::portal.posts.show', compact('post', 'comments'));
3342
}
3443
}

Notifications/Comment.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Modules\MyBlog\Notifications;
4+
5+
use App\Abstracts\Notification;
6+
use App\Models\Common\EmailTemplate;
7+
use Illuminate\Notifications\Messages\MailMessage;
8+
use Modules\MyBlog\Models\Comment as Model;
9+
10+
class Comment extends Notification
11+
{
12+
/**
13+
* The comment model.
14+
*
15+
* @var Model
16+
*/
17+
public $comment;
18+
19+
/**
20+
* The email template.
21+
*
22+
* @var EmailTemplate
23+
*/
24+
public $template;
25+
26+
public function __construct($comment = null, $template_alias = null)
27+
{
28+
parent::__construct();
29+
30+
$this->comment = $comment;
31+
$this->template = EmailTemplate::alias($template_alias)->first();
32+
}
33+
34+
/**
35+
* Get the mail representation of the notification.
36+
*/
37+
public function toMail($notifiable): MailMessage
38+
{
39+
$message = $this->initMessage();
40+
41+
return $message;
42+
}
43+
44+
/**
45+
* Get the array representation of the notification.
46+
*/
47+
public function toArray($notifiable): array
48+
{
49+
return [
50+
'template_alias' => $this->template->alias,
51+
'post_id' => $this->comment->post->id,
52+
'post_name' => $this->comment->post->name,
53+
'comment_id' => $this->comment->id,
54+
'comment_author' => $this->comment->owner->name,
55+
];
56+
}
57+
58+
public function getTags(): array
59+
{
60+
return [
61+
'{post_name}',
62+
'{post_author}',
63+
'{comment_author}',
64+
'{comment_description}',
65+
'{company_name}',
66+
];
67+
}
68+
69+
public function getTagsReplacement(): array
70+
{
71+
return [
72+
$this->comment->post->name,
73+
$this->comment->post->owner->name,
74+
$this->comment->owner->name,
75+
$this->comment->description,
76+
$this->comment->company->name,
77+
];
78+
}
79+
}

Observers/Comment.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Modules\MyBlog\Observers;
4+
5+
use App\Abstracts\Observer;
6+
use Modules\MyBlog\Notifications\Comment as Notification;
7+
use Modules\MyBlog\Models\Comment as Model;
8+
9+
class Comment extends Observer
10+
{
11+
/**
12+
* Listen to the created event.
13+
*
14+
* @param Model $comment
15+
*
16+
* @return void
17+
*/
18+
public function created(Model $comment)
19+
{
20+
$user = $comment->post->owner;
21+
22+
$user->notify(new Notification($comment, 'comment_new_author'));
23+
}
24+
25+
/**
26+
* Listen to the deleted event.
27+
*
28+
* @param Model $comment
29+
*
30+
* @return void
31+
*/
32+
public function deleted(Model $comment)
33+
{
34+
$user = $comment->post->owner;
35+
36+
$user->notify(new Notification($comment, 'comment_delete_author'));
37+
}
38+
}

Providers/Observer.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Modules\MyBlog\Providers;
4+
5+
use Illuminate\Support\ServiceProvider as Provider;
6+
use Modules\MyBlog\Models\Comment;
7+
8+
class Observer extends Provider
9+
{
10+
/**
11+
* Boot the application events.
12+
*
13+
* @return void
14+
*/
15+
public function boot()
16+
{
17+
Comment::observe('Modules\MyBlog\Observers\Comment');
18+
}
19+
}

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ This is an example module with beyond CRUD functions for admin and is shown in t
2424
- [x] Dashboard ([seed](Database/Seeds/Install.php#L20))
2525
- [x] ~~Document Type~~
2626
- [x] Dynamic Relationships ([define](Providers/Main.php#L35), [use](Widgets/PostsByCategory.php#L18))
27-
- [ ] Email Templates
27+
- [x] Email Templates ([seed](Database/Seeds/Install.php#L21), [content](Resources/lang/en-GB/email_templates.php))
2828
- [x] Exports ([controller](Http/Controllers/Posts.php#L244), [class](Exports))
2929
- [x] Imports ([controller](Http/Controllers/Posts.php#L117), [class](Imports))
3030
- [x] Jobs ([bulk action](BulkActions/Posts.php#L47), [ui](Http/Controllers/Posts.php#L222), [api](Http/Controllers/Api/Posts.php#L104))
3131
- [x] Menu ([admin](Listeners/AddToAdminMenu.php), [portal](Listeners/AddToPortalMenu.php))
32-
- [ ] Notifications
32+
- [x] Notifications ([trigger](Observers/Comment.php#L22), [class](Notifications/Comment.php))
33+
- [x] Observers ([define](module.json#L9), [provider](Providers/Observer.php), [class](Observers/Comment.php))
3334
- [x] Ownership (`created_by` [field](Models/Post.php#L14), [controller](Http/Controllers/Posts.php#L26), [blade](Resources/views/posts/index.blade.php#L54))
3435
- [x] Permissions ([listener](Listeners/FinishInstallation.php#L32))
3536
- [x] Reports ([define](module.json#L13), [report](Reports/PostSummary.php), [listener](Listeners/AddCategoriesToReport.php))

Resources/assets/js/comments.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* First we will load all of this project's JavaScript dependencies which
3+
* includes Vue and other libraries. It is a great starting point when
4+
* building robust, powerful web applications using Vue and Laravel.
5+
*/
6+
7+
require('./../../../../../resources/assets/js/bootstrap');
8+
9+
import Vue from 'vue';
10+
11+
import Global from './../../../../../resources/assets/js/mixins/global';
12+
import DashboardPlugin from './../../../../../resources/assets/js/plugins/dashboard-plugin';
13+
import Form from './../../../../../resources/assets/js/plugins/form';
14+
import BulkAction from './../../../../../resources/assets/js/plugins/bulk-action';
15+
16+
// plugin setup
17+
Vue.use(DashboardPlugin);
18+
19+
const app = new Vue({
20+
el: '#main-body',
21+
22+
mixins: [
23+
Global
24+
],
25+
26+
data() {
27+
return {
28+
form: new Form('comment'),
29+
bulk_action: new BulkAction('comments')
30+
}
31+
},
32+
});

0 commit comments

Comments
 (0)