Ignore:
Timestamp:
02/24/21 17:04:35 (4 years ago)
Author:
Özkan İliyaz <iliyaz_96@…>
Branches:
master
Children:
0c07a90
Parents:
1f059b0
Message:

ADD post confirmation with multiple phases, notification after succesfully password creation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • app/Http/Controllers/Dashboard/PostsController.php

    r1f059b0 rd25ba66  
    55use App\Helpers\Alert;
    66use App\Http\Requests\Dashboard\PostRequest;
     7use App\Models\Review;
    78use App\Models\Tag;
    89use App\Models\Post;
    910use App\Models\User;
    1011use App\Models\Category;
     12use Illuminate\Support\Facades\DB;
    1113use Illuminate\Support\Str;
    1214use Illuminate\Http\Request;
     
    9193    public function store(PostRequest $request)
    9294    {
    93         $post = new Post();
    94         $user = auth()->user();
    95         $category = Category::find($request->category);
    96 
    97         $post->user()->associate($user);
    98         $post->category()->associate($category);
    99 
    100         $post->title = $request->title;
    101 
    102         $image = $request->file("image");
    103         $extension = $image->getClientOriginalExtension();
    104         $imageName = $this->createImageName($extension);
    105         Storage::disk('uploads')->put($imageName, File::get($image));
    106 
    107         $post->image_link = $imageName;
    108         $post->content = Purifier::clean($request->post_content, 'youtube');
    109 
    110         $post->slug = $post->createSlug();
    111 
    112         if ($post->user->hasPermission("publish_post")) {
    113             $post->confirmed_by = $post->user->id;
    114             $post->is_active = 1;
    115             $post->is_confirmed = true;
    116         }
    117 
    118         $post->save();
    119         $this->checkNewAndSaveTags($post, $request->tags);
    120 
    121         if ($post->user->hasPermission("publish_post")) {
    122             Alert::flash("New posts published successfully");
    123         } else {
    124             Alert::flash("New posts submitted for review successfully");
    125             $adminsAndEditors = User::where("role_id", 1)->orWhere("role_id", 2)->get();
    126             Notification::send($adminsAndEditors, new NewPostCreated("Have new post for review"));
    127         }
     95        DB::transaction(function () use ($request) {
     96            $post = new Post();
     97            $user = auth()->user();
     98            $category = Category::find($request->category);
     99
     100            $post->user()->associate($user);
     101            $post->category()->associate($category);
     102
     103            $post->title = $request->title;
     104
     105            $image = $request->file("image");
     106            $extension = $image->getClientOriginalExtension();
     107            $imageName = $this->createImageName($extension);
     108            Storage::disk('uploads')->put($imageName, File::get($image));
     109
     110            $post->image_link = $imageName;
     111            $post->content = Purifier::clean($request->post_content, 'youtube');
     112
     113            $post->slug = $post->createSlug();
     114
     115            $needReview = $post->needReview();
     116
     117            if (is_null($needReview)) {
     118                $post->confirmed_by = $post->user->id;
     119                $post->is_active = 1;
     120                $post->is_confirmed = true;
     121            }
     122
     123            $post->save();
     124            $this->checkNewAndSaveTags($post, $request->tags);
     125
     126            if ($needReview) {
     127
     128                $review = new Review();
     129                $review->post()->associate($post);
     130                $review->postSecurity()->associate($needReview);
     131                $review->current_phase = $review->postSecurity->phases()->first()->id;
     132                $review->save();
     133
     134                Alert::flash("New posts submitted for review successfully");
     135
     136                Notification::send(
     137                    User::whereRoleId($review->postSecurity->role->id)->get(),
     138                    new NewPostCreated("Have new post for review")
     139                );
     140            } else {
     141                Alert::flash("New posts published successfully");
     142            }
     143        });
     144
     145//        if ($post->user->hasPermission("publish_post")) {
     146//            Alert::flash("New posts published successfully");
     147//        } else {
     148//            Alert::flash("New posts submitted for review successfully");
     149//            $adminsAndEditors = User::where("role_id", 1)->orWhere("role_id", 2)->get();
     150//            Notification::send($adminsAndEditors, new NewPostCreated("Have new post for review"));
     151//        }
    128152
    129153        return redirect()->route("dashboard.posts.create");
     
    135159        $flag = false;
    136160
    137         if (auth()->user()->hasPermission("confirm_post")) {
    138             $flag = true;
    139         }
    140 
    141         if ($flag) {
    142 
    143             $post->is_confirmed = true;
    144             $post->confirmed_by = auth()->user()->id;
    145 
    146             $post->save();
    147 
    148             Alert::flash("Post confirmed successfully");
    149 
    150             $post->user->notify(new PostConfirmed("Your post has been confirmed"));
    151         }
     161//        if (auth()->user()->hasPermission("confirm_post")) {
     162//            $flag = true;
     163//        }
     164
     165        if (!$post->review->canReview()) {
     166            Alert::flash("Post need to be confirmed by " . $post->review->getCurrentReviewer()->name, "error");
     167            return redirect()->back();
     168        }
     169
     170        DB::transaction(function () use ($post) {
     171
     172            $post->review->increment("current_phase");
     173            $post->review->save();
     174
     175            if (is_null($post->review->getPhase())) {
     176
     177                $post->review->is_passed_all_phases = true;
     178                $post->review->save();
     179
     180                $post->is_confirmed = true;
     181                $post->confirmed_by = auth()->user()->id;
     182                $post->save();
     183                $post->user->notify(new PostConfirmed("Your post has been confirmed"));
     184            }
     185        });
     186
     187        Alert::flash("Post confirmed successfully");
    152188
    153189        return redirect()->route("dashboard.posts.index");
Note: See TracChangeset for help on using the changeset viewer.