File size: 7,719 Bytes
6966756 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | <?php
namespace App\Http\Controllers;
use App\Constants\Status;
use App\Models\Category;
use App\Models\Favorite;
use App\Models\OrderDetails;
use App\Models\Prompt;
use App\Models\Tool;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PromptController extends Controller {
private $pageTitle;
private $activeFilter = null;
public function index() {
$this->pageTitle = 'Prompts';
return $this->promptData('approved');
}
public function featured() {
$this->activeFilter = 'featured';
$this->pageTitle = 'Featured Prompts';
return $this->promptData('featured');
}
public function trending() {
$this->activeFilter = 'trending';
$this->pageTitle = 'Trending Prompts';
return $this->promptData('trending');
}
public function similar($slug) {
$this->pageTitle = 'Similar Prompts';
$prompt = Prompt::where('slug', $slug)->firstOrFail();
return $this->promptData('similarPrompts', $prompt);
}
public function categoryWise($slug) {
$category = Category::active()->where('slug', $slug)->firstOrFail();
$this->pageTitle = $category->name;
$this->activeFilter = 'category';
return $this->promptData(categoryId: $category->id);
}
public function toolWise($slug) {
$tool = Tool::active()->where('slug', $slug)->firstOrFail();
$this->pageTitle = $tool->name . ' Prompts';
$this->activeFilter = 'tool';
return $this->promptData(toolId: $tool->id);
}
public function details($slug, Request $request) {
$pageTitle = 'Prompt Details';
$slug = htmlspecialchars_decode($slug);
$prompt = Prompt::approved()
->where('slug', $slug)
->with('user', 'category', 'promptImages')
->withAvg('reviews', 'rating')
->withCount('reviews', 'favorites')
->firstOrFail();
$user = $prompt->user;
$reviews = $prompt->reviews()->with('user')->orderBy('id', 'DESC')->paginate(5);
if ($request->ajax()) {
return response()->json([
'view' => view('Template::prompt.review', compact('pageTitle', 'reviews'))->render(),
'nextPageUrl' => $reviews->nextPageUrl()
]);
}
$hasPurchased = false;
$userReview = null;
$isFavorite = false;
if (auth()->check()) {
$hasPurchased = OrderDetails::where('prompt_id', $prompt->id)
->whereHas('order', function($order){
$order->where('status', Status::ORDER_COMPLETED)
->where('user_id', auth()->id());
})->exists();
$userReview = $prompt->reviews()->where('user_id', auth()->id())->first();
$isFavorite = $prompt->favorites()->where('user_id', auth()->id())->exists();
}
if ($prompt->user_id != auth()->id()) {
$prompt->views += 1;
$prompt->save();
}
$relatedPrompts = Prompt::approved()
->where(function ($query) use ($prompt) {
$query->where('category_id', $prompt->category_id)
->orWhere('tool_id', $prompt->tool_id);
})
->where('id', '!=', $prompt->id)
->inRandomOrder()
->with('user')
->orderBy('id', 'DESC');
$relatedPromptsCount = (clone $relatedPrompts)->count();
$relatedPrompts = $relatedPrompts->take(6)->get();
$allImages = array_merge([$prompt->image], $prompt->promptImages->pluck('image')->toArray());
$description = $prompt->description;
$limit = 60;
if (strlen($description) > $limit) {
$shortDescription = substr($description, 0, strrpos(substr($description, 0, $limit), ' '));
} else {
$shortDescription = $description;
}
return view('Template::prompt.details', compact('pageTitle', 'limit', 'isFavorite', 'description', 'shortDescription', 'allImages', 'relatedPromptsCount', 'prompt', 'relatedPrompts', 'reviews', 'hasPurchased', 'userReview'));
}
public function promptFilter(Request $request) {
$validation = Validator::make($request->all(), [
'sort_by' => 'nullable|in:trending,featured,latest',
'tool' => 'nullable|array',
'tool.*' => 'required|exists:tools,id',
'category' => 'nullable|array',
'category.*' => 'required|exists:categories,id',
]);
if ($validation->fails()) {
return response()->json([
'success' => false,
'message' => $validation->errors()->all()
]);
}
$prompts = Prompt::approved()->with('category', 'user');
if ($request->has('category') && $request->category != 0) {
$prompts->whereIn('category_id', $request->category);
}
if ($request->has('tool') && $request->tool != 0) {
$prompts->whereIn('tool_id', $request->tool);
}
if ($request->has('sort_by')) {
$sortBy = $request->sort_by;
$prompts->$sortBy();
}
$prompts = $prompts->paginate(getPaginate(24));
$totalPrompt = $prompts->total();
$data = [
'view' => view('Template::partials.prompt', compact('prompts'))->render(),
'totalPrompt' => $totalPrompt,
];
return response()->json($data);
}
public function addFavorite(Request $request) {
if (!auth()->check()) {
return response()->json(['success' => false, 'message' => 'You must be logged in to add favorites'], 401);
}
$promptId = $request->input('prompt_id');
$userId = auth()->id();
$prompt = Prompt::find($promptId);
if ($userId == $prompt->user_id) {
return response()->json(['success' => false, 'message' => 'You cannot add to favorite own prompt'], 400);
}
$favorite = Favorite::where('user_id', $userId)
->where('prompt_id', $promptId)
->first();
if ($favorite) {
$favorite->delete();
return response()->json(['success' => true, 'message' => 'Removed from favorites']);
} else {
$favorite = new Favorite;
$favorite->user_id = $userId;
$favorite->prompt_id = $promptId;
$favorite->save();
return response()->json(['success' => true, 'message' => 'Added to favorites']);
}
}
protected function promptData($scope = null, $prompt = null, $categoryId = null, $toolId = null) {
$categories = Category::active()->get();
$tools = Tool::active()->get();
if ($scope) {
$prompts = $prompt ? Prompt::$scope($prompt) : Prompt::$scope();
} else {
$prompts = Prompt::query();
}
if ($categoryId) {
$prompts->where('category_id', $categoryId);
}
if ($toolId) {
$prompts->where('tool_id', $toolId);
}
$prompts = $prompts
->searchable(['title', 'user:username', 'category:name', 'tool:name'])
->filter(['status'])
->with('user:.id,username', 'tool:id,name')
->approved()
->latest()
->paginate(getPaginate(24));
$pageTitle = $this->pageTitle;
$activeFilter = $this->activeFilter;
return view('Template::prompt.prompts', compact('pageTitle', 'prompts', 'categories', 'tools', 'activeFilter', 'categoryId', 'toolId'));
}
}
|