routes.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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) 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 = array();
  58. foreach($film->votes()->where('stimme', true)->get() as $v) {
  59. $pv[] = $v->voter->name;
  60. }
  61. $nv = array();
  62. foreach($film->votes()->where('stimme', false)->get() as $v) {
  63. $nv[] = $v->voter->name;
  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('beliebt', 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) 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(25);
  147. return View::make('suggest')
  148. ->with('filme', $filme)
  149. ->with('titel', 'Vorschl&auml;ge nach Wertung');
  150. });
  151. Route::get('vorgeschlagen', function() {
  152. $filme = DB::table(DB::raw('film_films'))
  153. ->select(DB::raw('film_films.*, COUNT(case when film_votes.stimme IS TRUE then 1 end) as upvotes,
  154. COUNT(case when film_votes.stimme IS FALSE then 1 end) as downvotes,
  155. COUNT(case when film_votes.stimme IS TRUE then 1 end) as vcount'))
  156. ->leftJoin('votes', 'votes.film', '=', 'films.id')
  157. ->whereNull('films.gesehen')
  158. ->groupBy('id')
  159. ->orderBy('vorgeschlagen', 'DESC')
  160. ->paginate(25);
  161. return View::make('suggest')
  162. ->with('filme', $filme)
  163. ->with('titel', 'Vorschl&auml;ge nach Datum');
  164. });
  165. Route::get('gesehen/{field?}/{order?}', function($field = "gesehen", $order = "desc") {
  166. $filme = Film::whereNotNull('gesehen')->orderBy($field, $order)->paginate(25);
  167. return View::make('seen')
  168. ->with('filme', $filme);
  169. })->where(array('field' => '[a-z]+', 'order' => 'asc|desc'));
  170. Route::get('neu', array('before' => 'auth', function() {
  171. return View::make('new');
  172. }));
  173. Route::post('neu', array('before' => 'auth', function() {
  174. $tmdb = new TMDb('b187f8d9c5e72b1faecb741d5d04239a', 'de', TRUE);
  175. $r = $tmdb->searchMovie(Input::get('search'));
  176. return View::make('new')->with('result', $r)->with('tmdb', $tmdb);
  177. }));
  178. Route::get('vorschlag/{id}', array('before' => 'auth', function($id) {
  179. $tmdb = new TMDb('b187f8d9c5e72b1faecb741d5d04239a', 'de', TRUE);
  180. $f = $tmdb->getMovie($id);
  181. $film = new Film();
  182. $film ->name = $f['title'];
  183. $film->tvdbid = $id;
  184. $film->vorgeschlagen = \Carbon\Carbon::today();
  185. $film->user = Auth::user()->id;
  186. $film->save();
  187. return Redirect::to('film/' . $film->id);
  188. }));
  189. Route::get('mark-read/{id}', array('before' => 'auth', function($id) {
  190. $film = Film::findOrFail($id);
  191. $film->gesehen = \Carbon\Carbon::today();
  192. $film->save();
  193. if(Dumbo::find(1)->nextfilm == $film->id) {
  194. $system = Dumbo::findOrFail(1);
  195. $system->nextfilm = 0;
  196. $system->save();
  197. }
  198. return Redirect::to('film/' . $film->id);
  199. }));
  200. Route::get('view-next/{id}', array('before' => 'auth', function($id) {
  201. $system = Dumbo::findOrFail(1);
  202. $system->nextfilm = $id;
  203. $system->save();
  204. return Redirect::to('film/' . $id);
  205. }));
  206. Route::get('register', function() {
  207. return View::make('register');
  208. });
  209. Route::post('register', function() {
  210. $vrules = array(
  211. 'name' => 'required|unique:users',
  212. 'email' => 'required|email',
  213. 'password' => 'required|confirmed',
  214. 'fire' => array('required', 'regex:/^Kreis$/i')
  215. );
  216. $vfields = array(
  217. 'name' => Input::get('user'),
  218. 'email' => Input::get('email'),
  219. 'password' => Input::get('password'),
  220. 'password_confirmation' => Input::get('pw-confirm'),
  221. 'fire' => Input::get('fire')
  222. );
  223. $val = Validator::make($vfields, $vrules);
  224. if($val->fails()) {
  225. return View::make('register')->with('errors', $val->messages());
  226. } else {
  227. $u = new User();
  228. $u->name = Input::get('user');
  229. $u->email = Input::get('email');
  230. $u->password = Hash::make(Input::get('password'));
  231. $u->save();
  232. return Redirect::to('/')->with('message', 'Registriert!');
  233. }
  234. });
  235. Route::get('settings', array('before' => 'auth', function() {
  236. return View::make('settings');
  237. }));
  238. Route::post('settings/{mode}', array('before' => 'auth', function($mode) {
  239. Validator::extend('pass', function($attribute, $value, $parameters) {
  240. return Hash::check($value, Auth::user()->password);
  241. });
  242. if($mode == 'password') {
  243. var_dump(Hash::check(Input::get('oldpw'), Auth::user()->password));
  244. $vfields = array('oldpw' => Input::get('oldpw'), 'newpw' => Input::get('newpw'), 'newpw_confirmation' => Input::get('newpw2'));
  245. $vrules = array( 'oldpw' => 'required|pass', 'newpw' => 'required|confirmed' );
  246. $val = Validator::make($vfields, $vrules);
  247. if($val->passes()) {
  248. $u = Auth::user();
  249. $u->password = Hash::make(Input::get('newpw'));
  250. $u->save();
  251. return View::make('settings')->with('message', 'Passwort geändert.');
  252. } else {
  253. return View::make('settings')->with('errors', $val->messages());
  254. }
  255. }
  256. if($mode == 'email') {
  257. $vfields = array('pw' => Input::get('pw'), 'email' => Input::get('email'), 'email_confirmation' => Input::get('email2'));
  258. $vrules = array('pw' => 'required|pass', 'email' => 'required|confirmed');
  259. $val = Validator::make($vfields, $vrules);
  260. if($val->passes()) {
  261. $u = Auth::user();
  262. $u->email = Input::get('email');
  263. $u->save();
  264. return View::make('settings')->with('message', 'Email geändert.');
  265. } else {
  266. return View::make('settings')->with('errors', $val->messages());
  267. }
  268. }
  269. }));
  270. Route::get('users', array('before' => 'auth', function() {
  271. if(Auth::user()->admin) {
  272. $u = User::orderBy('name')->paginate();
  273. return View::make('users')->with('users', $u);
  274. } else {
  275. App::abort(401, 'Diese Seite ist nicht für Dich.');
  276. }
  277. }));
  278. Route::get('users/{operation}/{id}', array('before' => 'auth', function($operation, $id) {
  279. if(!Auth::user()->admin) App::abort(401, 'Diese Seite ist nicht für Dich.');
  280. $u = User::findOrFail($id);
  281. switch($operation) {
  282. case 'mkadm':
  283. $u->admin = true;
  284. $u->save();
  285. $msg = $u->name . " ist jetzt ein Admin.";
  286. break;
  287. case 'rmadm':
  288. $u->admin = false;
  289. $u->save();
  290. $msg = $u->name . " ist kein Admin mehr.";
  291. break;
  292. case 'rmusr':
  293. $msg = $u->name . " wurde gelöscht.";
  294. $u->delete();
  295. break;
  296. }
  297. return Redirect::to('users')->with('message', $msg);
  298. }));
  299. Route::get('news', array('before' => 'auth', function() {
  300. return View::make('news');
  301. }));
  302. Route::post('news', array('before' => 'auth', function() {
  303. $vrules = array(
  304. 'headline' => 'required',
  305. 'body' => 'required'
  306. );
  307. $vfields = array(
  308. 'headline' => Input::get('headline'),
  309. 'body' => Input::get('body')
  310. );
  311. $val = Validator::make($vfields, $vrules);
  312. if($val->fails()) {
  313. return View::make('news')->with('errors', $val->messages());
  314. } else {
  315. $n = new News();
  316. $n->author = Auth::user()->id;
  317. $n->headline = Input::get('headline');
  318. $n->body = Input::get('body');
  319. $n->save();
  320. return Redirect::to('/')->with('message', 'News erstellt!');
  321. }
  322. }));
  323. Route::get('passwort-vergessen', function() {
  324. return View::make('pwform');
  325. });
  326. Route::post('passwort-vergessen', function() {
  327. $credentials = array('email' => Input::get('email'));
  328. return Password::remind($credentials, function($message, $user) {
  329. $message->subject('Passwort für Dumbo zurücksetzen.');
  330. });
  331. });
  332. Route::get('passwort-reset/{token}', function($token) {
  333. return View::make('pwreset')->with('token', $token);
  334. });
  335. Route::post('passwort-reset', function() {
  336. $credentials = array(
  337. 'email' => Input::get('email'),
  338. 'password' => Input::get('password'),
  339. 'password_confirmation' => Input::get('password_confirmation')
  340. );
  341. return Password::reset($credentials, function($user, $password) {
  342. $user->password = Hash::make($password);
  343. $user->save();
  344. return Redirect::to('/');
  345. });
  346. });