44
55use App \Models \Article ;
66use App \Models \Tag ;
7+ use App \Models \User ;
8+ use App \Notifications \SendSubmittedArticle ;
79use App \Traits \WithArticleAttributes ;
810use App \Traits \WithTagsAssociation ;
11+ use Illuminate \Support \Facades \Auth ;
12+ use Illuminate \Support \Facades \Cache ;
13+ use Illuminate \Support \Facades \Notification ;
914use Livewire \Component ;
1015use Livewire \WithFileUploads ;
1116
@@ -15,6 +20,7 @@ class Edit extends Component
1520
1621 public Article $ article ;
1722 public ?string $ preview = null ;
23+ public bool $ alreadySubmitted = false ;
1824
1925 protected $ listeners = ['markdown-x:update ' => 'onMarkdownUpdate ' ];
2026
@@ -25,6 +31,7 @@ public function mount(Article $article)
2531 $ this ->body = $ article ->body ;
2632 $ this ->slug = $ article ->slug ;
2733 $ this ->show_toc = $ article ->show_toc ;
34+ $ this ->submitted_at = $ article ->submitted_at ;
2835 $ this ->canonical_url = $ article ->originalUrl ();
2936 $ this ->preview = $ article ->getFirstMediaUrl ('media ' );
3037 $ this ->associateTags = $ this ->tags_selected = old ('tags ' , $ article ->tags ()->pluck ('id ' )->toArray ());
@@ -35,6 +42,13 @@ public function onMarkdownUpdate(string $content)
3542 $ this ->body = $ content ;
3643 }
3744
45+ public function submit ()
46+ {
47+ $ this ->alreadySubmitted = $ this ->article ->submitted_at !== null ;
48+ $ this ->submitted_at = $ this ->article ->submitted_at ?? now ();
49+ $ this ->store ();
50+ }
51+
3852 public function store ()
3953 {
4054 $ this ->save ();
@@ -44,12 +58,16 @@ public function save()
4458 {
4559 $ this ->validate ();
4660
61+ /** @var User $user */
62+ $ user = Auth::user ();
63+
4764 $ this ->article ->update ([
4865 'title ' => $ this ->title ,
4966 'slug ' => $ this ->slug ,
5067 'body ' => $ this ->body ,
5168 'show_toc ' => $ this ->show_toc ,
5269 'canonical_url ' => $ this ->canonical_url ,
70+ 'submitted_at ' => $ this ->submitted_at ,
5371 ]);
5472
5573 $ this ->article ->syncTags ($ this ->associateTags );
@@ -58,7 +76,19 @@ public function save()
5876 $ this ->article ->addMedia ($ this ->file ->getRealPath ())->toMediaCollection ('media ' );
5977 }
6078
61- $ this ->redirectRoute ('articles.show ' , $ this ->article );
79+ if (! $ this ->alreadySubmitted ) {
80+ // Envoi du mail a l'admin pour la validation de l'article
81+ $ admin = User::findByEmailAddress ('monneylobe@gmail.com ' );
82+ Notification::send ($ admin , new SendSubmittedArticle ($ this ->article ));
83+
84+ session ()->flash ('status ' , 'Merci d \'avoir soumis votre article. Vous aurez des nouvelles uniquement quand nous accepterons votre article. ' );
85+ }
86+
87+ Cache::forget ('post- ' . $ this ->article ->id );
88+
89+ $ user ->hasRole ('user ' ) ?
90+ $ this ->redirectRoute ('dashboard ' ) :
91+ $ this ->redirectRoute ('articles.show ' , $ this ->article );
6292 }
6393
6494 public function render ()
0 commit comments