routes.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. /*
  3. |--------------------------------------------------------------------------
  4. | Application Routes
  5. |--------------------------------------------------------------------------
  6. |
  7. | Here is where you can register all of the routes for an application.
  8. | It's a breeze. Simply tell Laravel the URIs it should respond to
  9. | and give it the Closure to execute when that URI is requested.
  10. |
  11. */
  12. Route::get('/', function()
  13. {
  14. $gesehen = Film::zuletztGesehen()->get();
  15. $vorgeschlagen = Film::neuesteVorschlage()->get();
  16. $news = News::aktuell()->get();
  17. $nextfilm = Dumbo::find(1)->film;
  18. $topfilm = DB::table(DB::raw('film_films'))
  19. ->select(DB::raw('film_films.*, COUNT(case when film_votes.stimme IS TRUE then 1 end) as upvotes,
  20. COUNT(case when film_votes.stimme IS FALSE then 1 end) as downvotes,
  21. COUNT(case when film_votes.stimme IS TRUE then 1 end) - COUNT(case when film_votes.stimme IS FALSE then 1 end) as vcount'))
  22. ->leftJoin('votes', 'votes.film', '=', 'films.id')
  23. ->whereNull('films.gesehen')
  24. ->where('films.id', "!=", is_object($nextfilm) ? $nextfilm->id : 0)
  25. ->groupBy('id')
  26. ->orderBy('vcount', 'DESC')->orderBy('vorgeschlagen', 'ASC')
  27. ->first();
  28. $tmdb = new TMDb('b187f8d9c5e72b1faecb741d5d04239a', 'de', TRUE);
  29. $tmovie = $tmdb->getMovie($topfilm->tvdbid);
  30. $image = $tmdb->getImageUrl($tmovie['poster_path'], TMDb::IMAGE_POSTER, 'w342');
  31. if(is_object($nextfilm)) {
  32. $tmovie = $tmdb->getMovie($nextfilm->tvdbid);
  33. $nimage = $tmdb->getImageUrl($tmovie['poster_path'], TMDb::IMAGE_POSTER, 'w342');
  34. } else {
  35. $nimage = "http://d3a8mw37cqal2z.cloudfront.net/assets/7ea4ac6f271e37d/images/no-poster-w130.jpg";
  36. }
  37. $kommentare = Comment::neueste()->get();
  38. return View::make('index')
  39. ->with('gesehen', $gesehen)
  40. ->with('vorgeschlagen', $vorgeschlagen)
  41. ->with('image', $image)
  42. ->with('nimage', $nimage)
  43. ->with('news', $news)
  44. ->with('kommentare', $kommentare)
  45. ->with('nextfilm', $nextfilm)
  46. ->with('topfilm', $topfilm);
  47. });
  48. Route::get('film/{id}', array('as' => 'film', function($id) {
  49. $film = Film::findOrFail($id);
  50. $tmdb = new TMDb('b187f8d9c5e72b1faecb741d5d04239a', 'de', TRUE);
  51. $tmovie = $tmdb->getMovie($film->tvdbid);
  52. $tcast = $tmdb->getMovieCast($film->tvdbid);
  53. $ttrail = $tmdb->getMovieTrailers($film->tvdbid);
  54. $image = $tmdb->getImageUrl($tmovie['poster_path'], TMDb::IMAGE_POSTER, 'w342');
  55. $votes = $film->votes()->count();
  56. $vposi = $film->votes()->where('stimme', true)->count();
  57. $pv = "";
  58. foreach($film->votes()->where('stimme', true)->get() as $v) {
  59. $pv .= $v->voter->name . "<br> ";
  60. }
  61. $nv = "";
  62. foreach($film->votes()->where('stimme', false)->get() as $v) {
  63. $nv .= $v->voter->name . "<br> ";
  64. }
  65. if(!is_null(Auth::user()) && $film->votes()->where('user', Auth::user()->id)->count() > 0) {
  66. $uvote[0] = true;
  67. $uvote[1] = $film->votes()->where('user', Auth::user()->id)->first()->stimme;
  68. } else {
  69. $uvote[0] = false;
  70. }
  71. $comments = $film->comments()->orderBy('id', 'DESC')->get();
  72. $labels = array("", "danger", "danger", "warning", "warning", "info", "info", "primary", "primary", "success", "success");
  73. return View::make('film')
  74. ->with('film', $film)
  75. ->with('tfilm', $tmovie)
  76. ->with('poster', $image)
  77. ->with('comments', $comments)
  78. ->with('cast', $tcast)
  79. ->with('trail', $ttrail)
  80. ->with('votes', $votes)
  81. ->with('uvote', $uvote)
  82. ->with('pv', $pv)
  83. ->with('nv', $nv)
  84. ->with('vposi', $vposi)
  85. ->with('labels', $labels)
  86. ->with('tmdb', $tmdb);
  87. }));
  88. Route::get('vote/{stimme}/{user}/{film}', function($stimme, $user, $film) {
  89. $v = Vote::where('user', $user)->where('film', $film)->first();
  90. if(!is_null($v)) {
  91. $v->stimme = $stimme == "yes" ? true : false;
  92. } else {
  93. $v = new Vote();
  94. $v->user = $user;
  95. $v->film = $film;
  96. $v->stimme = $stimme == "yes" ? true : false;
  97. }
  98. $v->save();
  99. return Redirect::to('film/' . $film);
  100. });
  101. Route::post('comment', array('as' => 'comment', function() {
  102. $c = new Comment();
  103. $c->film = Input::get('film');
  104. $c->user = Input::get('user');
  105. $c->text = Input::get('text');
  106. $c->bewertung = !is_null(Input::get('rate')) ? Input::get('rate') : 0;
  107. $c->save();
  108. return Redirect::to('film/' . Input::get('film'));
  109. }));
  110. Route::post('comment/edit', array('as'=> 'modcomment', function () {
  111. $c = Comment::findOrFail(Input::get('id'));
  112. $c->text = Input::get('text');
  113. $c->bewertung = Input::get('rate');
  114. $c->save();
  115. return Redirect::to('film/' . $c->film);
  116. }));
  117. Route::get('login', array('as' => 'login', function() {
  118. return View::make('login');
  119. }));
  120. Route::post('login', function() {
  121. $userdata = array(
  122. 'name' => Input::get('user'),
  123. 'password' => Input::get('password'));
  124. if(Auth::attempt($userdata, true)) {
  125. return Redirect::intended('/');
  126. } else {
  127. echo "Login gescheitert.";
  128. var_dump($userdata);
  129. return Redirect::to('login')
  130. ->with('login_errors', true);
  131. }
  132. });
  133. Route::get('logout', array('as' => 'logout', function() {
  134. Auth::logout();
  135. return Redirect::to('/');
  136. }));
  137. Route::get('vorgeschlagen', function() {
  138. $filme = DB::table(DB::raw('film_films'))
  139. ->select(DB::raw('film_films.*, COUNT(case when film_votes.stimme IS TRUE then 1 end) as upvotes,
  140. COUNT(case when film_votes.stimme IS FALSE then 1 end) as downvotes,
  141. COUNT(case when film_votes.stimme IS TRUE then 1 end) - COUNT(case when film_votes.stimme IS FALSE then 1 end) as vcount'))
  142. ->leftJoin('votes', 'votes.film', '=', 'films.id')
  143. ->whereNull('films.gesehen')
  144. ->groupBy('id')
  145. ->orderBy('vcount', 'DESC')->orderBy('vorgeschlagen', 'ASC')
  146. ->paginate();
  147. return View::make('suggest')
  148. ->with('filme', $filme);
  149. })->where(array('field' => '[a-z]+', 'order' => 'asc|desc'));
  150. Route::get('gesehen/{field?}/{order?}', function($field = "gesehen", $order = "desc") {
  151. $filme = Film::whereNotNull('gesehen')->orderBy($field, $order)->paginate();
  152. return View::make('seen')
  153. ->with('filme', $filme);
  154. })->where(array('field' => '[a-z]+', 'order' => 'asc|desc'));
  155. Route::get('neu', array('before' => 'auth', function() {
  156. return View::make('new');
  157. }));
  158. Route::post('neu', array('before' => 'auth', function() {
  159. $tmdb = new TMDb('b187f8d9c5e72b1faecb741d5d04239a', 'de', TRUE);
  160. $r = $tmdb->searchMovie(Input::get('search'));
  161. return View::make('new')->with('result', $r)->with('tmdb', $tmdb);
  162. }));
  163. Route::get('vorschlag/{id}', array('before' => 'auth', function($id) {
  164. $tmdb = new TMDb('b187f8d9c5e72b1faecb741d5d04239a', 'de', TRUE);
  165. $f = $tmdb->getMovie($id);
  166. $film = new Film();
  167. $film ->name = $f['title'];
  168. $film->tvdbid = $id;
  169. $film->vorgeschlagen = \Carbon\Carbon::today();
  170. $film->user = Auth::user()->id;
  171. $film->save();
  172. return Redirect::to('film/' . $film->id);
  173. }));
  174. Route::get('mark-read/{id}', array('before' => 'auth', function($id) {
  175. $film = Film::findOrFail($id);
  176. $film->gesehen = \Carbon\Carbon::today();
  177. $film->save();
  178. if(Dumbo::find(1)->nextfilm == $film->id) {
  179. $system = Dumbo::findOrFail(1);
  180. $system->nextfilm = 0;
  181. $system->save();
  182. }
  183. return Redirect::to('film/' . $film->id);
  184. }));
  185. Route::get('view-next/{id}', array('before' => 'auth', function($id) {
  186. $system = Dumbo::findOrFail(1);
  187. $system->nextfilm = $id;
  188. $system->save();
  189. return Redirect::to('film/' . $id);
  190. }));
  191. Route::get('register', function() {
  192. return View::make('register');
  193. });
  194. Route::post('register', function() {
  195. $vrules = array(
  196. 'name' => 'required|unique:users',
  197. 'email' => 'required|email',
  198. 'password' => 'required|confirmed',
  199. 'fire' => array('required', 'regex:/^Kreis$/i')
  200. );
  201. $vfields = array(
  202. 'name' => Input::get('user'),
  203. 'email' => Input::get('email'),
  204. 'password' => Input::get('password'),
  205. 'password_confirmation' => Input::get('pw-confirm'),
  206. 'fire' => Input::get('fire')
  207. );
  208. $val = Validator::make($vfields, $vrules);
  209. if($val->fails()) {
  210. return View::make('register')->with('errors', $val->messages());
  211. } else {
  212. $u = new User();
  213. $u->name = Input::get('user');
  214. $u->email = Input::get('email');
  215. $u->password = Hash::make(Input::get('password'));
  216. $u->save();
  217. return Redirect::to('/')->with('message', 'Registriert!');
  218. }
  219. });
  220. Route::get('settings', array('before' => 'auth', function() {
  221. return View::make('settings');
  222. }));
  223. Route::post('settings/{mode}', array('before' => 'auth', function($mode) {
  224. Validator::extend('pass', function($attribute, $value, $parameters) {
  225. return Hash::check($value, Auth::user()->password);
  226. });
  227. if($mode == 'password') {
  228. var_dump(Hash::check(Input::get('oldpw'), Auth::user()->password));
  229. $vfields = array('oldpw' => Input::get('oldpw'), 'newpw' => Input::get('newpw'), 'newpw_confirmation' => Input::get('newpw2'));
  230. $vrules = array( 'oldpw' => 'required|pass', 'newpw' => 'required|confirmed' );
  231. $val = Validator::make($vfields, $vrules);
  232. if($val->passes()) {
  233. $u = Auth::user();
  234. $u->password = Hash::make(Input::get('newpw'));
  235. $u->save();
  236. return View::make('settings')->with('message', 'Passwort geändert.');
  237. } else {
  238. return View::make('settings')->with('errors', $val->messages());
  239. }
  240. }
  241. if($mode == 'email') {
  242. $vfields = array('pw' => Input::get('pw'), 'email' => Input::get('email'), 'email_confirmation' => Input::get('email2'));
  243. $vrules = array('pw' => 'required|pass', 'email' => 'required|confirmed');
  244. $val = Validator::make($vfields, $vrules);
  245. if($val->passes()) {
  246. $u = Auth::user();
  247. $u->email = Input::get('email');
  248. $u->save();
  249. return View::make('settings')->with('message', 'Email geändert.');
  250. } else {
  251. return View::make('settings')->with('errors', $val->messages());
  252. }
  253. }
  254. }));
  255. Route::get('users', array('before' => 'auth', function() {
  256. if(Auth::user()->admin) {
  257. $u = User::orderBy('name')->paginate();
  258. return View::make('users')->with('users', $u);
  259. } else {
  260. App::abort(401, 'Diese Seite ist nicht für Dich.');
  261. }
  262. }));
  263. Route::get('users/{operation}/{id}', array('before' => 'auth', function($operation, $id) {
  264. if(!Auth::user()->admin) App::abort(401, 'Diese Seite ist nicht für Dich.');
  265. $u = User::findOrFail($id);
  266. switch($operation) {
  267. case 'mkadm':
  268. $u->admin = true;
  269. $u->save();
  270. $msg = $u->name . " ist jetzt ein Admin.";
  271. break;
  272. case 'rmadm':
  273. $u->admin = false;
  274. $u->save();
  275. $msg = $u->name . " ist kein Admin mehr.";
  276. break;
  277. case 'rmusr':
  278. $msg = $u->name . " wurde gelöscht.";
  279. $u->delete();
  280. break;
  281. }
  282. return Redirect::to('users')->with('message', $msg);
  283. }));
  284. Route::get('news', array('before' => 'auth', function() {
  285. return View::make('news');
  286. }));
  287. Route::post('news', array('before' => 'auth', function() {
  288. $vrules = array(
  289. 'headline' => 'required',
  290. 'body' => 'required'
  291. );
  292. $vfields = array(
  293. 'headline' => Input::get('headline'),
  294. 'body' => Input::get('body')
  295. );
  296. $val = Validator::make($vfields, $vrules);
  297. if($val->fails()) {
  298. return View::make('news')->with('errors', $val->messages());
  299. } else {
  300. $n = new News();
  301. $n->author = Auth::user()->id;
  302. $n->headline = Input::get('headline');
  303. $n->body = Input::get('body');
  304. $n->save();
  305. return Redirect::to('/')->with('message', 'News erstellt!');
  306. }
  307. }));
  308. Route::get('passwort-vergessen', function() {
  309. return View::make('pwform');
  310. });
  311. Route::post('passwort-vergessen', function() {
  312. $credentials = array('email' => Input::get('email'));
  313. return Password::remind($credentials, function($message, $user) {
  314. $message->subject('Passwort für Dumbo zurücksetzen.');
  315. });
  316. });
  317. Route::get('passwort-reset/{token}', function($token) {
  318. return View::make('pwreset')->with('token', $token);
  319. });
  320. Route::post('passwort-reset', function() {
  321. $credentials = array(
  322. 'email' => Input::get('email'),
  323. 'password' => Input::get('password'),
  324. 'password_confirmation' => Input::get('password_confirmation')
  325. );
  326. return Password::reset($credentials, function($user, $password) {
  327. $user->password = Hash::make($password);
  328. $user->save();
  329. return Redirect::to('/');
  330. });
  331. });