routes.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 = "img/no-poster-w92.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. $ef = Film::where('tvdbid', '=', $id)->whereNull('gesehen')->first();
  180. if(is_null($ef)) {
  181. $tmdb = new TMDb('b187f8d9c5e72b1faecb741d5d04239a', 'de', TRUE);
  182. $f = $tmdb->getMovie($id);
  183. $film = new Film();
  184. $film->name = $f['title'];
  185. $film->tvdbid = $id;
  186. $film->vorgeschlagen = \Carbon\Carbon::today();
  187. $film->user = Auth::user()->id;
  188. $film->save();
  189. Session::put('message', 'Film hinzugefügt.');
  190. return Redirect::to('film/' . $film->id);
  191. } else {
  192. Session::put('message', 'Film bereits vorgeschlagen von ' . $ef->besitzer->name . '.');
  193. return Redirect::to('film/' . $ef->id);
  194. }
  195. }));
  196. Route::get('mark-read/{id}', array('before' => 'auth', function($id) {
  197. $film = Film::findOrFail($id);
  198. $film->gesehen = \Carbon\Carbon::today();
  199. $film->save();
  200. if(Dumbo::find(1)->nextfilm == $film->id) {
  201. $system = Dumbo::findOrFail(1);
  202. $system->nextfilm = 0;
  203. $system->save();
  204. }
  205. return Redirect::to('film/' . $film->id);
  206. }));
  207. Route::get('view-next/{id}', array('before' => 'auth', function($id) {
  208. $system = Dumbo::findOrFail(1);
  209. $system->nextfilm = $id;
  210. $system->save();
  211. return Redirect::to('film/' . $id);
  212. }));
  213. Route::get('register', function() {
  214. return View::make('register');
  215. });
  216. Route::post('register', function() {
  217. $vrules = array(
  218. 'name' => 'required|unique:users',
  219. 'email' => 'required|email',
  220. 'password' => 'required|confirmed',
  221. 'fire' => array('required', 'regex:/^Kreis$/i')
  222. );
  223. $vfields = array(
  224. 'name' => Input::get('user'),
  225. 'email' => Input::get('email'),
  226. 'password' => Input::get('password'),
  227. 'password_confirmation' => Input::get('pw-confirm'),
  228. 'fire' => Input::get('fire')
  229. );
  230. $val = Validator::make($vfields, $vrules);
  231. if($val->fails()) {
  232. return View::make('register')->with('errors', $val->messages());
  233. } else {
  234. $u = new User();
  235. $u->name = Input::get('user');
  236. $u->email = Input::get('email');
  237. $u->password = Hash::make(Input::get('password'));
  238. $u->save();
  239. return Redirect::to('/')->with('message', 'Registriert!');
  240. }
  241. });
  242. Route::get('settings', array('before' => 'auth', function() {
  243. return View::make('settings');
  244. }));
  245. Route::post('settings/{mode}', array('before' => 'auth', function($mode) {
  246. Validator::extend('pass', function($attribute, $value, $parameters) {
  247. return Hash::check($value, Auth::user()->password);
  248. });
  249. if($mode == 'password') {
  250. var_dump(Hash::check(Input::get('oldpw'), Auth::user()->password));
  251. $vfields = array('oldpw' => Input::get('oldpw'), 'newpw' => Input::get('newpw'), 'newpw_confirmation' => Input::get('newpw2'));
  252. $vrules = array( 'oldpw' => 'required|pass', 'newpw' => 'required|confirmed' );
  253. $val = Validator::make($vfields, $vrules);
  254. if($val->passes()) {
  255. $u = Auth::user();
  256. $u->password = Hash::make(Input::get('newpw'));
  257. $u->save();
  258. return View::make('settings')->with('message', 'Passwort geändert.');
  259. } else {
  260. return View::make('settings')->with('errors', $val->messages());
  261. }
  262. }
  263. if($mode == 'email') {
  264. $vfields = array('pw' => Input::get('pw'), 'email' => Input::get('email'), 'email_confirmation' => Input::get('email2'));
  265. $vrules = array('pw' => 'required|pass', 'email' => 'required|confirmed');
  266. $val = Validator::make($vfields, $vrules);
  267. if($val->passes()) {
  268. $u = Auth::user();
  269. $u->email = Input::get('email');
  270. $u->save();
  271. return View::make('settings')->with('message', 'Email geändert.');
  272. } else {
  273. return View::make('settings')->with('errors', $val->messages());
  274. }
  275. }
  276. if($mode == 'avatar-reset') {
  277. /** @var User $u */
  278. $u = Auth::user();
  279. $u->setSetting('avatar', false);
  280. $u->save();
  281. /* Delete old Avatars */
  282. array_map('unlink', glob(public_path("img/avatars/". Auth::user()->id . "-*")));
  283. return View::make('settings')->with('message', 'Avatar gelöscht.');
  284. }
  285. if($mode == 'avatar-upload') {
  286. $vfields = array('avatar' => Input::file('avatar'));
  287. $vrules = array('avatar' => 'required|image|max:5000');
  288. $val = Validator::make($vfields, $vrules);
  289. if($val->passes()) {
  290. /* Delete old Avatars */
  291. array_map('unlink', glob(public_path("img/avatars/". Auth::user()->id . "-*")));
  292. /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
  293. $file = Input::file('avatar');
  294. $file = $file->move( public_path("img/avatars/"), Auth::user()->id . "-". Str::slug($file->getFilename()) . "." . $file->guessExtension() );
  295. $i = new Imagick();
  296. $i->readImage($file->getRealPath());
  297. $i->cropThumbnailImage(100, 100);
  298. $i->writeImage();
  299. /** @var User $u */
  300. $u = Auth::user();
  301. $u->setSetting('avatar', $file->getFilename());
  302. $u->save();
  303. return View::make('settings')->with('message', 'Avatar gespeichert.');
  304. } else {
  305. return View::make('settings')->with('errors', $val->messages());
  306. }
  307. }
  308. }));
  309. Route::get('users', array('before' => 'auth', function() {
  310. if(Auth::user()->admin) {
  311. $u = User::orderBy('name')->paginate();
  312. return View::make('users')->with('users', $u);
  313. } else {
  314. App::abort(401, 'Diese Seite ist nicht für Dich.');
  315. }
  316. }));
  317. Route::get('users/{operation}/{id}', array('before' => 'auth', function($operation, $id) {
  318. if(!Auth::user()->admin) App::abort(401, 'Diese Seite ist nicht für Dich.');
  319. $u = User::findOrFail($id);
  320. switch($operation) {
  321. case 'mkadm':
  322. $u->admin = true;
  323. $u->save();
  324. $msg = $u->name . " ist jetzt ein Admin.";
  325. break;
  326. case 'rmadm':
  327. $u->admin = false;
  328. $u->save();
  329. $msg = $u->name . " ist kein Admin mehr.";
  330. break;
  331. case 'rmusr':
  332. $msg = $u->name . " wurde gelöscht.";
  333. $u->delete();
  334. break;
  335. }
  336. return Redirect::to('users')->with('message', $msg);
  337. }));
  338. Route::get('news', array('before' => 'auth', function() {
  339. return View::make('news');
  340. }));
  341. Route::post('news', array('before' => 'auth', function() {
  342. $vrules = array(
  343. 'headline' => 'required',
  344. 'body' => 'required'
  345. );
  346. $vfields = array(
  347. 'headline' => Input::get('headline'),
  348. 'body' => Input::get('body')
  349. );
  350. $val = Validator::make($vfields, $vrules);
  351. if($val->fails()) {
  352. return View::make('news')->with('errors', $val->messages());
  353. } else {
  354. $n = new News();
  355. $n->author = Auth::user()->id;
  356. $n->headline = Input::get('headline');
  357. $n->body = Input::get('body');
  358. $n->save();
  359. return Redirect::to('/')->with('message', 'News erstellt!');
  360. }
  361. }));
  362. Route::get('passwort-vergessen', function() {
  363. return View::make('pwform');
  364. });
  365. Route::post('passwort-vergessen', function() {
  366. $credentials = array('email' => Input::get('email'));
  367. return Password::remind($credentials, function($message, $user) {
  368. $message->subject('Passwort für Dumbo zurücksetzen.');
  369. });
  370. });
  371. Route::get('passwort-reset/{token}', function($token) {
  372. return View::make('pwreset')->with('token', $token);
  373. });
  374. Route::post('passwort-reset', function() {
  375. $credentials = array(
  376. 'email' => Input::get('email'),
  377. 'password' => Input::get('password'),
  378. 'password_confirmation' => Input::get('password_confirmation')
  379. );
  380. return Password::reset($credentials, function($user, $password) {
  381. $user->password = Hash::make($password);
  382. $user->save();
  383. return Redirect::to('/');
  384. });
  385. });