PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/ContestsController.php

https://gitlab.com/php2laravel/php-project-boost
PHP | 275 lines | 180 code | 41 blank | 54 comment | 13 complexity | 5f19511bde0714a54e642682050fdcce MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Contest;
  4. use Flash;
  5. use App\Project;
  6. use App\Vote;
  7. use App\User;
  8. use App\Tag;
  9. use Auth;
  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 ContestsController extends Controller
  23. {
  24. /**
  25. * Display a listing of the resource.
  26. *
  27. * @return \Illuminate\Http\Response
  28. */
  29. public function index()
  30. {
  31. // $deadline = DB::table('contests')
  32. // ->select('*')
  33. // ->whereDate('created_at', '<=', Carbon::now()->toDateString())
  34. // ->orderBy('enddate', 'desc')
  35. // ->paginate(12);
  36. $lastid = DB::table('contests')->max('id');
  37. $submissionsnow = DB::table('projects')
  38. ->join('users', 'users.id', '=', 'projects.user_id')
  39. ->join('contests', 'contests.id', '=', 'projects.competition_id')
  40. ->where('competition', '=', true)
  41. ->where('competition_id', '=', $lastid)
  42. ->select('users.name','projects.*')
  43. ->orderBy('countlikes', 'desc')
  44. ->simplePaginate(12);
  45. // return $submissionsnow;
  46. $submissionsprevious = DB::table('projects')
  47. ->join('users', 'users.id', '=', 'projects.user_id')
  48. ->where('competition', '=', true)
  49. ->select('users.name','projects.*')
  50. ->orderBy('countlikes', 'desc')
  51. ->simplePaginate(10);
  52. $today = Carbon::now();
  53. $contests = DB::table('contests')
  54. ->select('*')
  55. ->whereDate('created_at', '<=', Carbon::now()->toDateString())
  56. ->where('id', '<>', $lastid )
  57. ->orderBy('enddate', 'desc')
  58. ->simplePaginate(5);
  59. $lastcontest = DB::table('contests')
  60. ->select('id', 'title', 'desc', 'enddate')
  61. ->whereDate('created_at', '<=', Carbon::now()->toDateString())
  62. ->where('id', '=', $lastid )
  63. ->get();
  64. return view('pages.contests', compact('contests', 'lastcontest', 'submissionsnow', 'submissionsprevious'));
  65. }
  66. /**
  67. * Show the form for creating a new resource.
  68. *
  69. * @return \Illuminate\Http\Response
  70. */
  71. public function create()
  72. {
  73. return view('admin/createContest');
  74. }
  75. public function newcontest()
  76. {
  77. //$this->validate($request, ['title'=>'required|min:3']);
  78. $input = Request::all();
  79. $lastcontest = DB::table('contests')->max('id');
  80. $newid = $lastcontest+1;
  81. $contest = new Contest;
  82. $contest->fill(\Input::all());
  83. $contest->title = $input['title'];
  84. $contest->desc = $input['desc'];
  85. $contest->startdate = date("Y-m-d h:m:s", strtotime($input['startdate']));
  86. $contest->enddate = date("Y-m-d h:m:s", strtotime("+30 days"));
  87. $contest->save();
  88. Flash::success('You created a new contest');
  89. return redirect('/admin');
  90. }
  91. private function syncTags(Project $project, array $tags) {
  92. $project->tags()->sync($tags);
  93. }
  94. /**
  95. * Store a newly created resource in storage.
  96. *
  97. * @param \Illuminate\Http\Request $request
  98. * @return \Illuminate\Http\Response
  99. */
  100. public function store()
  101. {
  102. $contest_id = DB::table('contests')->max('id');
  103. $input = Request::all();
  104. $lastproject = DB::table('projects')->max('id');
  105. $newid = $lastproject+1;
  106. $image = Input::file('image');
  107. $imageN = $image->getClientOriginalName();
  108. $filename = $newid. '.' . $image->getClientOriginalExtension();
  109. $path = public_path('uploads/projects/' . $filename);
  110. Image::make($image->getRealPath())->fit(800, 800)->save($path);
  111. $contests = Auth::user()->projects()->create($input);
  112. $contests->user_id = Auth::id();
  113. $contests->title = $input['title'];
  114. if (isset($input['tag_list'])) {
  115. $this->syncTags($contests, $input['tag_list']);
  116. }
  117. $contests->desc = $input['desc'];
  118. $contests->image = $filename;
  119. $contests->competition = 1;
  120. $contests->competition_id = $contest_id;
  121. $contests->save();
  122. Flash::success('Your project is added to the contest! Good luck!');
  123. return redirect('contests');
  124. }
  125. /**
  126. * Display the specified resource.
  127. *
  128. * @param int $id
  129. * @return \Illuminate\Http\Response
  130. */
  131. public function show($id)
  132. {
  133. $project = Project::findOrFail($id)->load("User");
  134. $input = Request::all();
  135. //-----------------------COLORS--------------------------//
  136. $client = new ColorExtractor;
  137. $info = pathinfo('uploads/projects/'.$project->image);
  138. if ($info["extension"] == "jpg") {
  139. $image = $client->loadJpeg('uploads/projects/'.$project->image);
  140. }else if($info["extension"] == "png")
  141. {
  142. $image = $client->loadPng('uploads/projects/'.$project->image);
  143. }else if($info["extension"] == "gif") {
  144. $image = $client->loadGif('uploads/projects/'.$project->image);
  145. }else{
  146. echo 'Wrong image extension';
  147. }
  148. $palette = $image->extract(3);
  149. $palette = str_replace("#", "", $palette);
  150. $project->colors = implode(', ', $palette);
  151. $project->save();
  152. //-----------------------COLORS--------------------------//
  153. //-----------------------DB-----------------------------//
  154. $project_comments = DB::table('comments')
  155. ->select('body', 'name', 'image', 'comments.created_at')
  156. ->where('project_id', '=', $id)
  157. ->join('users', 'users.id', '=', 'user_id')
  158. ->orderBy('created_at', 'desc')
  159. ->simplePaginate(10);
  160. $votes = DB::table('votes')
  161. ->where('votes.projectId', '=' , $id)
  162. ->join('users', 'users.id', '=', 'votes.userId')
  163. ->select('users.name','votes.*')
  164. ->get();
  165. $topVotes = DB::table('votes')
  166. ->where('votes.projectId', '=' , $id)
  167. ->join('users', 'users.id', '=', 'votes.userId')
  168. ->select('users.name','votes.*')
  169. ->limit(3)
  170. ->get();
  171. $project = Project::findOrFail($id);
  172. $designer = User::findOrFail($project->user_id);
  173. $firstWork = array();
  174. $designershort = strtolower(str_replace(' ', '-', $designer->name));
  175. $firstvote = Vote::where('projectId', '=' , $id)->first();
  176. $myvote = DB::table('votes')
  177. ->where('projectId', '=' , $id)
  178. ->where('userId', '=', Auth::id())
  179. ->first();
  180. if ($myvote != null) {
  181. $myvote = true;
  182. } else {
  183. $myvote = false;
  184. }
  185. $countVotes = Vote::where('projectId', '=' , $id)->count();
  186. $contest = true;
  187. $follower = DB::table('followers')
  188. ->where('fan_id', '=' , Auth::id())
  189. ->where('designer_Id', '=', $designer->id)
  190. ->first();
  191. if ($follower != null) {
  192. $follower = true;
  193. } else {
  194. $follower = false;
  195. }
  196. $today = date("m-d-Y");
  197. $advertisements = DB::table('ads')
  198. ->where('enddate', '>', $today)
  199. ->take(3)
  200. ->get();
  201. return view('pages.showProject', ['follower'=>$follower, 'advertisements'=>$advertisements, 'contest'=>$contest, 'project' => $project, 'comments' => $project_comments, 'palette' => $palette, 'designer' => $designer, 'countVotes' => $countVotes, 'votes' => $votes, 'topVotes' => $topVotes, 'designershort' => $designershort, 'myvote' =>$myvote, 'firstWork' => $firstWork]);
  202. }
  203. /**
  204. * Show the form for editing the specified resource.
  205. *
  206. * @param int $id
  207. * @return \Illuminate\Http\Response
  208. */
  209. public function edit($id)
  210. {
  211. //
  212. }
  213. /**
  214. * Update the specified resource in storage.
  215. *
  216. * @param \Illuminate\Http\Request $request
  217. * @param int $id
  218. * @return \Illuminate\Http\Response
  219. */
  220. public function update(Request $request, $id)
  221. {
  222. //
  223. }
  224. /**
  225. * Remove the specified resource from storage.
  226. *
  227. * @param int $id
  228. * @return \Illuminate\Http\Response
  229. */
  230. public function destroy($id)
  231. {
  232. //
  233. }
  234. }