PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Http/Controllers/projectsController.php

https://gitlab.com/php2laravel/php-project-boost
PHP | 341 lines | 203 code | 42 blank | 96 comment | 22 complexity | e9ccd07af169aed4cf52c5c1005e5b8e MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Flash;
  4. use App\Project;
  5. use App\Vote;
  6. use App\User;
  7. use App\Tag;
  8. use Auth;
  9. //use Illuminate\Http\Request;
  10. use Request;
  11. use Input;
  12. use File;
  13. use Validator;
  14. use Redirect;
  15. use Session;
  16. use Carbon\Carbon;
  17. use App\Http\Requests;
  18. use App\Http\Controllers\Controller;
  19. use Image;
  20. use DB;
  21. use League\ColorExtractor\Client as ColorExtractor;
  22. class projectsController extends Controller
  23. {
  24. /**
  25. * Display a listing of the resource.
  26. *
  27. * @return \Illuminate\Http\Response
  28. */
  29. public function index()
  30. {
  31. $projects = DB::table('projects')
  32. ->join('users', 'users.id', '=', 'projects.user_id')
  33. ->where('competition', '=', false)
  34. ->select('users.name','projects.*')
  35. ->orderBy('projects.created_at', 'desc')
  36. ->paginate(12);
  37. return view('pages.projects', compact('projects', 'designershort'));
  38. }
  39. /**
  40. * Store a newly created resource in storage.
  41. *
  42. * @param \Illuminate\Http\Request $request
  43. * @return \Illuminate\Http\Response
  44. */
  45. public function store(/*Request $request*/)
  46. {
  47. //$this->validate($request, ['title'=>'required|min:3']);
  48. $input = Request::all();
  49. $lastproject = DB::table('projects')->max('id');
  50. $newid = $lastproject+1;
  51. $image = Input::file('image');
  52. $imageN = $image->getClientOriginalName();
  53. $filename = $newid. '.' . $image->getClientOriginalExtension();
  54. $path = public_path('uploads/projects/' . $filename);
  55. Image::make($image->getRealPath())->fit(800, 800)->save($path);
  56. $project = Auth::user()->projects()->create($input);
  57. $project->user_id = Auth::id();
  58. $project->title = $input['title'];
  59. if (isset($input['tag_list'])) {
  60. $this->syncTags($project, $input['tag_list']);
  61. }
  62. $project->desc = $input['desc'];
  63. $project->image = $filename;
  64. $project->competition = false;
  65. $project->save();
  66. Flash::success('Your project is added to the gallery');
  67. // add exp to the user for uploading a project
  68. $user = Auth::user();
  69. $user->update_xp($user->id, config('app.experience.upload'));
  70. // add badge if user hasn't got one
  71. $user->add_badge($user->id, config('app.badges.first_upload'));
  72. return redirect('/projects');
  73. }
  74. /**
  75. * Display the specified resource.
  76. *
  77. * @param int $id
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function show($id)
  81. {
  82. $project = Project::findOrFail($id)->load("User");
  83. //$input = Request::all();
  84. //-----------------------COLORS--------------------------//
  85. $client = new ColorExtractor;
  86. $info = pathinfo('uploads/projects/'.$project -> image);
  87. if ($info["extension"] == "jpg") {
  88. $image = $client->loadJpeg('uploads/projects/'.$project->image);
  89. }else if($info["extension"] == "png")
  90. {
  91. $image = $client->loadPng('uploads/projects/'.$project->image);
  92. }else if($info["extension"] == "gif") {
  93. $image = $client->loadGif('uploads/projects/'.$project->image);
  94. }else{
  95. echo 'Wrong image extension';
  96. }
  97. $palette = $image->extract(3);
  98. $palette = str_replace("#", "", $palette);
  99. $project->colors = implode(', ', $palette);
  100. $project->save();
  101. //-----------------------COLORS--------------------------//
  102. //-----------------------DB-----------------------------//
  103. $project_comments = DB::table('comments')
  104. ->select('comments.id', 'body', 'name', 'image', 'comments.created_at')
  105. ->where('project_id', '=', $id)
  106. ->join('users', 'users.id', '=', 'user_id')
  107. ->orderBy('created_at', 'desc')
  108. //->get());
  109. ->simplePaginate(10);
  110. //-----------------------DB-----------------------------//
  111. //return view('pages.showProject', compact('project', 'palette'));
  112. // votes
  113. $votes = DB::table('votes')
  114. ->where('votes.projectId', '=' , $id)
  115. ->join('users', 'users.id', '=', 'votes.userId')
  116. ->select('users.name','votes.*')
  117. ->get();
  118. $topVotes = DB::table('votes')
  119. ->where('votes.projectId', '=' , $id)
  120. ->join('users', 'users.id', '=', 'votes.userId')
  121. ->select('users.name','votes.*')
  122. ->limit(3)
  123. ->get();
  124. $project = Project::findOrFail($id);
  125. $designer = User::findOrFail($project->user_id);
  126. $firstWork = DB::table('projects')
  127. ->where('user_id', '=' , $designer->id)
  128. ->where('id', '<>', $id)
  129. ->select('id','image')
  130. ->orderBy('countlikes','desc')
  131. ->limit(4)
  132. ->get();
  133. $designershort = strtolower(str_replace(' ', '-', $designer->name));
  134. $firstvote = Vote::where('projectId', '=' , $id)->first();
  135. $myvote = DB::table('votes')
  136. ->where('projectId', '=' , $id)
  137. ->where('userId', '=', Auth::id())
  138. ->first();
  139. if ($myvote != null) {
  140. $myvote = true;
  141. } else {
  142. $myvote = false;
  143. }
  144. $countVotes = Vote::where('projectId', '=' , $project->id)->count();
  145. $follower = DB::table('followers')
  146. ->where('fan_id', '=' , Auth::id())
  147. ->where('designer_Id', '=', $designer->id)
  148. ->first();
  149. if ($follower != null) {
  150. $follower = true;
  151. } else {
  152. $follower = false;
  153. }
  154. $today = date("m-d-Y");
  155. $a = DB::table('ads')
  156. ->where('enddate', '>', $today)
  157. ->take(3)
  158. ->get();
  159. return view('pages.showProject', ['follower'=>$follower, 'project' => Project::findOrFail($id), 'comments' => $project_comments, 'palette' => $palette, 'designer' => $designer, 'countVotes' => $countVotes, 'votes' => $votes, 'topVotes' => $topVotes, 'designershort' => $designershort, 'myvote' =>$myvote, 'firstWork' => $firstWork, 'a' => $a]);
  160. }
  161. /**
  162. * Show the form for editing the specified resource.
  163. *
  164. * @param int $id
  165. * @return \Illuminate\Http\Response
  166. */
  167. public function edit($id)
  168. {
  169. $project = Project::findOrFail($id);
  170. $tags = Tag::lists('name', 'id');
  171. return view('pages.projects.edit', compact('project', 'tags'));
  172. }
  173. /**
  174. * Update the specified resource in storage.
  175. *
  176. * @param \Illuminate\Http\Request $request
  177. * @param int $id
  178. * @return \Illuminate\Http\Response
  179. */
  180. public function update($id)
  181. {
  182. //get project
  183. $project = Project::find($id);
  184. $input = Request::all();
  185. //auto prepopulate form
  186. $project->fill(\Input::all());
  187. //upload and rename new image
  188. if (Input::hasFile('image'))
  189. {
  190. $image = Input::file('image');
  191. $imageN = $image->getClientOriginalName();
  192. $filename = $id. '.' . $image->getClientOriginalExtension();
  193. $path = public_path('uploads/projects/' . $filename);
  194. $oldimage = $project->image;
  195. File::delete('uploads/projects/'.$oldimage);
  196. Image::make($image->getRealPath())->fit(800, 800)->save($path);
  197. $project->image = $filename;
  198. }
  199. //save project and return to project page
  200. if (isset($input['tag_list'])) {
  201. $this->syncTags($project, $input['tag_list']);
  202. } else {
  203. $project->tags()->detach();
  204. }
  205. $project->save();// no validation implemented
  206. Flash::message('Changes saved!');
  207. if ($project->competition == true) {
  208. return redirect('/contests');
  209. }
  210. else {
  211. return redirect('/projects');
  212. }
  213. }
  214. private function syncTags(Project $project, array $tags) {
  215. $project->tags()->sync($tags);
  216. }
  217. public function delete($id)
  218. {
  219. //get project
  220. $project = Project::find($id);
  221. //get images and delete from uploads folder
  222. $filename = $project->image;
  223. //delete project
  224. if ($project->competition == true) {
  225. $project->delete();
  226. File::delete('uploads/projects/'.$filename);
  227. //back to gallery
  228. Flash::message('Your submission is deleted from the gallery');
  229. return redirect('/contests');
  230. } else {
  231. $project->delete();
  232. File::delete('uploads/projects/'.$filename);
  233. //back to gallery
  234. Flash::message('Your project is deleted from the gallery');
  235. return redirect('/projects');
  236. }
  237. }
  238. /**
  239. * Remove the specified resource from storage.
  240. *
  241. * @param int $id
  242. * @return \Illuminate\Http\Response
  243. */
  244. public function destroy($id)
  245. {
  246. //
  247. }
  248. public function projects()
  249. {
  250. return view('pages.projects');
  251. }
  252. public function create()
  253. {
  254. // all tags (name collumn, with value set as key) from the DB
  255. $tags = Tag::lists('name', 'id');
  256. return view('pages.projectsCreate', compact('tags'));
  257. }
  258. public function ajax()
  259. {
  260. return view('ajax');
  261. }
  262. /*
  263. public function post_isLikedByMe($id)
  264. {
  265. $project = Project::findOrFail($id)->first();
  266. if (Vote::whereUserId(Auth::id())->whereProjectId($project->id)->exists()){
  267. return 'true';
  268. }
  269. return 'false';
  270. }
  271. public function like()
  272. {
  273. // Get the Project
  274. $project = Project::findOrFail($id)->first();
  275. // If the user already like this project, we delete the like
  276. $existing_vote = Vote::whereProjectId($project->id)->whereUserId(Auth::id())->first();
  277. if (!is_null($existing_vote)) {
  278. $existing_vote->delete();
  279. }
  280. else { // The user don't like this project yet
  281. // We check if the user already liked this project once, but unliked it.
  282. $existing_vote = Vote::onlyTrashed()->whereProjectId($project->id)->whereUserId(Auth::id())->first();
  283. if (!is_null($existing_vote)) { // The user has liked the project before, but deleted his like later
  284. // Then, we restore this like
  285. $existing_vote->restore();
  286. }
  287. else { // The user never liked this project before, so we create the like, and we can trigger some events (notifications, etc)
  288. $vote = new Vote;
  289. $vote->userId = Auth::id();
  290. $vote->projectId = 5;
  291. $vote->save();
  292. }
  293. }
  294. }
  295. */
  296. }