Index: app/Http/Controllers/Dashboard/PostsController.php
===================================================================
--- app/Http/Controllers/Dashboard/PostsController.php	(revision 0924b6c75483e6a4ae83e08487635bdc25436fed)
+++ app/Http/Controllers/Dashboard/PostsController.php	(revision d25ba6629ddc39867a49008948daa1cedd404fc4)
@@ -5,8 +5,10 @@
 use App\Helpers\Alert;
 use App\Http\Requests\Dashboard\PostRequest;
+use App\Models\Review;
 use App\Models\Tag;
 use App\Models\Post;
 use App\Models\User;
 use App\Models\Category;
+use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Str;
 use Illuminate\Http\Request;
@@ -91,39 +93,61 @@
     public function store(PostRequest $request)
     {
-        $post = new Post();
-        $user = auth()->user();
-        $category = Category::find($request->category);
-
-        $post->user()->associate($user);
-        $post->category()->associate($category);
-
-        $post->title = $request->title;
-
-        $image = $request->file("image");
-        $extension = $image->getClientOriginalExtension();
-        $imageName = $this->createImageName($extension);
-        Storage::disk('uploads')->put($imageName, File::get($image));
-
-        $post->image_link = $imageName;
-        $post->content = Purifier::clean($request->post_content, 'youtube');
-
-        $post->slug = $post->createSlug();
-
-        if ($post->user->hasPermission("publish_post")) {
-            $post->confirmed_by = $post->user->id;
-            $post->is_active = 1;
-            $post->is_confirmed = true;
-        }
-
-        $post->save();
-        $this->checkNewAndSaveTags($post, $request->tags);
-
-        if ($post->user->hasPermission("publish_post")) {
-            Alert::flash("New posts published successfully");
-        } else {
-            Alert::flash("New posts submitted for review successfully");
-            $adminsAndEditors = User::where("role_id", 1)->orWhere("role_id", 2)->get();
-            Notification::send($adminsAndEditors, new NewPostCreated("Have new post for review"));
-        }
+        DB::transaction(function () use ($request) {
+            $post = new Post();
+            $user = auth()->user();
+            $category = Category::find($request->category);
+
+            $post->user()->associate($user);
+            $post->category()->associate($category);
+
+            $post->title = $request->title;
+
+            $image = $request->file("image");
+            $extension = $image->getClientOriginalExtension();
+            $imageName = $this->createImageName($extension);
+            Storage::disk('uploads')->put($imageName, File::get($image));
+
+            $post->image_link = $imageName;
+            $post->content = Purifier::clean($request->post_content, 'youtube');
+
+            $post->slug = $post->createSlug();
+
+            $needReview = $post->needReview();
+
+            if (is_null($needReview)) {
+                $post->confirmed_by = $post->user->id;
+                $post->is_active = 1;
+                $post->is_confirmed = true;
+            }
+
+            $post->save();
+            $this->checkNewAndSaveTags($post, $request->tags);
+
+            if ($needReview) {
+
+                $review = new Review();
+                $review->post()->associate($post);
+                $review->postSecurity()->associate($needReview);
+                $review->current_phase = $review->postSecurity->phases()->first()->id;
+                $review->save();
+
+                Alert::flash("New posts submitted for review successfully");
+
+                Notification::send(
+                    User::whereRoleId($review->postSecurity->role->id)->get(),
+                    new NewPostCreated("Have new post for review")
+                );
+            } else {
+                Alert::flash("New posts published successfully");
+            }
+        });
+
+//        if ($post->user->hasPermission("publish_post")) {
+//            Alert::flash("New posts published successfully");
+//        } else {
+//            Alert::flash("New posts submitted for review successfully");
+//            $adminsAndEditors = User::where("role_id", 1)->orWhere("role_id", 2)->get();
+//            Notification::send($adminsAndEditors, new NewPostCreated("Have new post for review"));
+//        }
 
         return redirect()->route("dashboard.posts.create");
@@ -135,19 +159,31 @@
         $flag = false;
 
-        if (auth()->user()->hasPermission("confirm_post")) {
-            $flag = true;
-        }
-
-        if ($flag) {
-
-            $post->is_confirmed = true;
-            $post->confirmed_by = auth()->user()->id;
-
-            $post->save();
-
-            Alert::flash("Post confirmed successfully");
-
-            $post->user->notify(new PostConfirmed("Your post has been confirmed"));
-        }
+//        if (auth()->user()->hasPermission("confirm_post")) {
+//            $flag = true;
+//        }
+
+        if (!$post->review->canReview()) {
+            Alert::flash("Post need to be confirmed by " . $post->review->getCurrentReviewer()->name, "error");
+            return redirect()->back();
+        }
+
+        DB::transaction(function () use ($post) {
+
+            $post->review->increment("current_phase");
+            $post->review->save();
+
+            if (is_null($post->review->getPhase())) {
+
+                $post->review->is_passed_all_phases = true;
+                $post->review->save();
+
+                $post->is_confirmed = true;
+                $post->confirmed_by = auth()->user()->id;
+                $post->save();
+                $post->user->notify(new PostConfirmed("Your post has been confirmed"));
+            }
+        });
+
+        Alert::flash("Post confirmed successfully");
 
         return redirect()->route("dashboard.posts.index");
