routes.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. define('TMDB_API_KEY', 'b187f8d9c5e72b1faecb741d5d04239a');
  13. Route::get('/', function()
  14. {
  15. $gesehen = Film::zuletztGesehen()->get();
  16. $vorgeschlagen = Film::neuesteVorschlage()->get();
  17. $mg = Film::meistgewunschteVorschlage()->get();
  18. $news = News::aktuell()->get();
  19. $nextfilm = Dumbo::find(1)->film;
  20. $tmdb = new TMDb(TMDB_API_KEY, 'de', TRUE);
  21. foreach(array("top" => $mg[0], "neu" => $vorgeschlagen[0], "alt" => $gesehen[0]) as $key => $val) {
  22. $tmovie = $tmdb->getMovie($val->tvdbid);
  23. $images[$key] = $tmdb->getImageUrl($tmovie['poster_path'], TMDb::IMAGE_POSTER, 'w342');
  24. }
  25. if(is_object($nextfilm)) {
  26. $tmovie = $tmdb->getMovie($nextfilm->tvdbid);
  27. $images["next"] = $tmdb->getImageUrl($tmovie['poster_path'], TMDb::IMAGE_POSTER, 'w342');
  28. } else {
  29. $images["next"] = "img/no-poster-w92.jpg";
  30. }
  31. $kommentare = Comment::neueste()->get();
  32. $labels = array("", "danger", "danger", "warning", "warning", "info", "info", "primary", "primary", "success", "success");
  33. return View::make('index')
  34. ->with('gesehen', $gesehen)
  35. ->with('vorgeschlagen', $vorgeschlagen)
  36. ->with('meistgw', $mg)
  37. ->with('images', $images)
  38. ->with('news', $news)
  39. ->with('kommentare', $kommentare)
  40. ->with('labels', $labels)
  41. ->with('nextfilm', $nextfilm);
  42. });
  43. Route::get('film/{id}', array('as' => 'film', function($id) {
  44. $film = Film::findOrFail($id);
  45. $tmdb = new TMDb('b187f8d9c5e72b1faecb741d5d04239a', 'de', TRUE);
  46. $tmovie = $tmdb->getMovie($film->tvdbid);
  47. $tcast = $tmdb->getMovieCast($film->tvdbid);
  48. $ttrail = $tmdb->getMovieTrailers($film->tvdbid);
  49. $image = $tmdb->getImageUrl($tmovie['poster_path'], TMDb::IMAGE_POSTER, 'w342');
  50. $votes = $film->votes()->count();
  51. $vposi = $film->votes()->where('stimme', true)->count();
  52. $pv = array();
  53. foreach($film->votes()->where('stimme', true)->get() as $v) {
  54. $pv[] = $v->voter;
  55. }
  56. $nv = array();
  57. foreach($film->votes()->where('stimme', false)->get() as $v) {
  58. $nv[] = $v->voter;
  59. }
  60. if(!is_null(Auth::user()) && $film->votes()->where('user', Auth::user()->id)->count() > 0) {
  61. $uvote[0] = true;
  62. $uvote[1] = $film->votes()->where('user', Auth::user()->id)->first()->stimme;
  63. } else {
  64. $uvote[0] = false;
  65. }
  66. $comments = $film->comments()->orderBy('id', 'DESC')->get();
  67. $labels = array("", "danger", "danger", "warning", "warning", "info", "info", "primary", "primary", "success", "success");
  68. return View::make('film')
  69. ->with('film', $film)
  70. ->with('tfilm', $tmovie)
  71. ->with('poster', $image)
  72. ->with('comments', $comments)
  73. ->with('cast', $tcast)
  74. ->with('trail', $ttrail)
  75. ->with('votes', $votes)
  76. ->with('uvote', $uvote)
  77. ->with('pv', $pv)
  78. ->with('nv', $nv)
  79. ->with('vposi', $vposi)
  80. ->with('labels', $labels)
  81. ->with('tmdb', $tmdb);
  82. }));
  83. Route::get('vote/{stimme}/{user}/{film}', function($stimme, $user, $film) {
  84. $v = Vote::where('user', $user)->where('film', $film)->first();
  85. if(!is_null($v)) {
  86. $v->stimme = $stimme == "yes" ? true : false;
  87. } else {
  88. $v = new Vote();
  89. $v->user = $user;
  90. $v->film = $film;
  91. $v->stimme = $stimme == "yes" ? true : false;
  92. }
  93. $v->save();
  94. return Redirect::to('film/' . $film);
  95. });
  96. Route::post('comment', array('as' => 'comment', function() {
  97. $c = new Comment();
  98. $c->film = Input::get('film');
  99. $c->user = Input::get('user');
  100. $c->text = Input::get('text');
  101. $c->bewertung = !is_null(Input::get('rate')) ? Input::get('rate') : 0;
  102. $c->save();
  103. return Redirect::to('film/' . Input::get('film'));
  104. }));
  105. Route::post('comment/edit', array('as'=> 'modcomment', function () {
  106. $c = Comment::findOrFail(Input::get('id'));
  107. $c->text = Input::get('text');
  108. $c->bewertung = Input::get('rate');
  109. $c->save();
  110. return Redirect::to('film/' . $c->film);
  111. }));
  112. Route::get('login', array('as' => 'login', function() {
  113. return View::make('login');
  114. }));
  115. Route::post('login', function() {
  116. $userdata = array(
  117. 'name' => Input::get('user'),
  118. 'password' => Input::get('password'));
  119. if(Auth::attempt($userdata, true)) {
  120. return Redirect::intended('/');
  121. } else {
  122. echo "Login gescheitert.";
  123. var_dump($userdata);
  124. return Redirect::to('login')
  125. ->with('login_errors', true);
  126. }
  127. });
  128. Route::get('logout', array('as' => 'logout', function() {
  129. Auth::logout();
  130. return Redirect::to('/');
  131. }));
  132. Route::get('beliebt', function() {
  133. $filme = DB::table(DB::raw('film_films'))
  134. ->select(DB::raw('film_films.*, COUNT(case when film_votes.stimme IS TRUE then 1 end) as upvotes,
  135. COUNT(case when film_votes.stimme IS FALSE then 1 end) as downvotes,
  136. COUNT(case when film_votes.stimme IS TRUE then 1 end) as vcount'))
  137. ->leftJoin('votes', 'votes.film', '=', 'films.id')
  138. ->whereNull('films.gesehen')
  139. ->groupBy('id')
  140. ->orderBy('vcount', 'DESC')->orderBy('vorgeschlagen', 'ASC')
  141. ->paginate(25);
  142. return View::make('suggest')
  143. ->with('filme', $filme)
  144. ->with('titel', 'Vorschl&auml;ge nach Wertung');
  145. });
  146. Route::get('vorgeschlagen', function() {
  147. $filme = DB::table(DB::raw('film_films'))
  148. ->select(DB::raw('film_films.*, COUNT(case when film_votes.stimme IS TRUE then 1 end) as upvotes,
  149. COUNT(case when film_votes.stimme IS FALSE then 1 end) as downvotes,
  150. COUNT(case when film_votes.stimme IS TRUE then 1 end) as vcount'))
  151. ->leftJoin('votes', 'votes.film', '=', 'films.id')
  152. ->whereNull('films.gesehen')
  153. ->groupBy('id')
  154. ->orderBy('vorgeschlagen', 'DESC')
  155. ->paginate(25);
  156. return View::make('suggest')
  157. ->with('filme', $filme)
  158. ->with('titel', 'Vorschl&auml;ge nach Datum');
  159. });
  160. Route::get('gesehen/{field?}/{order?}', function($field = "gesehen", $order = "desc") {
  161. $filme = Film::whereNotNull('gesehen')->orderBy($field, $order)->paginate(25);
  162. return View::make('seen')
  163. ->with('filme', $filme);
  164. })->where(array('field' => '[a-z]+', 'order' => 'asc|desc'));
  165. Route::get('neu', array('before' => 'auth', function() {
  166. return View::make('new');
  167. }));
  168. Route::post('neu', array('before' => 'auth', function() {
  169. $tmdb = new TMDb('b187f8d9c5e72b1faecb741d5d04239a', 'de', TRUE);
  170. $r = $tmdb->searchMovie(Input::get('search'));
  171. return View::make('new')->with('result', $r)->with('tmdb', $tmdb);
  172. }));
  173. Route::get('vorschlag/{id}', array('before' => 'auth', function($id) {
  174. $ef = Film::where('tvdbid', '=', $id)->whereNull('gesehen')->first();
  175. if(is_null($ef)) {
  176. $tmdb = new TMDb('b187f8d9c5e72b1faecb741d5d04239a', 'de', TRUE);
  177. $f = $tmdb->getMovie($id);
  178. $film = new Film();
  179. $film->name = $f['title'];
  180. $film->tvdbid = $id;
  181. $film->vorgeschlagen = \Carbon\Carbon::today();
  182. $film->user = Auth::user()->id;
  183. $film->save();
  184. Session::put('message', 'Film hinzugefügt.');
  185. return Redirect::to('film/' . $film->id);
  186. } else {
  187. Session::put('message', 'Film bereits vorgeschlagen von ' . $ef->besitzer->name . '.');
  188. return Redirect::to('film/' . $ef->id);
  189. }
  190. }));
  191. Route::get('mark-read/{id}', array('before' => 'auth', function($id) {
  192. $film = Film::findOrFail($id);
  193. $film->gesehen = \Carbon\Carbon::today();
  194. $film->save();
  195. if(Dumbo::find(1)->nextfilm == $film->id) {
  196. $system = Dumbo::findOrFail(1);
  197. $system->nextfilm = 0;
  198. $system->save();
  199. }
  200. return Redirect::to('film/' . $film->id);
  201. }));
  202. Route::get('view-next/{id}', array('before' => 'auth', function($id) {
  203. $system = Dumbo::findOrFail(1);
  204. $system->nextfilm = $id;
  205. $system->save();
  206. return Redirect::to('film/' . $id);
  207. }));
  208. Route::get('register', function() {
  209. return View::make('register');
  210. });
  211. Route::post('register', function() {
  212. $vrules = array(
  213. 'name' => 'required|unique:users',
  214. 'email' => 'required|email',
  215. 'password' => 'required|confirmed',
  216. 'fire' => array('required', 'regex:/^Kreis$/i')
  217. );
  218. $vfields = array(
  219. 'name' => Input::get('user'),
  220. 'email' => Input::get('email'),
  221. 'password' => Input::get('password'),
  222. 'password_confirmation' => Input::get('pw-confirm'),
  223. 'fire' => Input::get('fire')
  224. );
  225. $val = Validator::make($vfields, $vrules);
  226. if($val->fails()) {
  227. return View::make('register')->with('errors', $val->messages());
  228. } else {
  229. $u = new User();
  230. $u->name = Input::get('user');
  231. $u->email = Input::get('email');
  232. $u->password = Hash::make(Input::get('password'));
  233. $u->save();
  234. return Redirect::to('/')->with('message', 'Registriert!');
  235. }
  236. });
  237. Route::get('settings', array('before' => 'auth', function() {
  238. return View::make('settings');
  239. }));
  240. Route::post('settings/{mode}', array('before' => 'auth', function($mode) {
  241. Validator::extend('pass', function($attribute, $value, $parameters) {
  242. return Hash::check($value, Auth::user()->password);
  243. });
  244. if($mode == 'password') {
  245. var_dump(Hash::check(Input::get('oldpw'), Auth::user()->password));
  246. $vfields = array('oldpw' => Input::get('oldpw'), 'newpw' => Input::get('newpw'), 'newpw_confirmation' => Input::get('newpw2'));
  247. $vrules = array( 'oldpw' => 'required|pass', 'newpw' => 'required|confirmed' );
  248. $val = Validator::make($vfields, $vrules);
  249. if($val->passes()) {
  250. $u = Auth::user();
  251. $u->password = Hash::make(Input::get('newpw'));
  252. $u->save();
  253. return View::make('settings')->with('message', 'Passwort geändert.');
  254. } else {
  255. return View::make('settings')->with('errors', $val->messages());
  256. }
  257. }
  258. if($mode == 'email') {
  259. $vfields = array('pw' => Input::get('pw'), 'email' => Input::get('email'), 'email_confirmation' => Input::get('email2'));
  260. $vrules = array('pw' => 'required|pass', 'email' => 'required|confirmed');
  261. $val = Validator::make($vfields, $vrules);
  262. if($val->passes()) {
  263. $u = Auth::user();
  264. $u->email = Input::get('email');
  265. $u->save();
  266. return View::make('settings')->with('message', 'Email geändert.');
  267. } else {
  268. return View::make('settings')->with('errors', $val->messages());
  269. }
  270. }
  271. if($mode == 'avatar-reset') {
  272. /** @var User $u */
  273. $u = Auth::user();
  274. $u->setSetting('avatar', false);
  275. $u->save();
  276. /* Delete old Avatars */
  277. array_map('unlink', glob(public_path("img/avatars/". Auth::user()->id . "-*")));
  278. return View::make('settings')->with('message', 'Avatar gelöscht.');
  279. }
  280. if($mode == 'avatar-upload') {
  281. $vfields = array('avatar' => Input::file('avatar'));
  282. $vrules = array('avatar' => 'required|image|max:5000');
  283. $val = Validator::make($vfields, $vrules);
  284. if($val->passes()) {
  285. /* Delete old Avatars */
  286. array_map('unlink', glob(public_path("img/avatars/". Auth::user()->id . "-*")));
  287. /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
  288. $file = Input::file('avatar');
  289. $file = $file->move( public_path("img/avatars/"), Auth::user()->id . "-". Str::slug($file->getFilename()) . "." . $file->guessExtension() );
  290. $i = new Imagick();
  291. $i->readImage($file->getRealPath());
  292. $i->cropThumbnailImage(100, 100);
  293. $i->writeImage();
  294. /** @var User $u */
  295. $u = Auth::user();
  296. $u->setSetting('avatar', $file->getFilename());
  297. $u->save();
  298. return View::make('settings')->with('message', 'Avatar gespeichert.');
  299. } else {
  300. return View::make('settings')->with('errors', $val->messages());
  301. }
  302. }
  303. }));
  304. Route::get('users', array('before' => 'auth', function() {
  305. if(Auth::user()->admin) {
  306. $u = User::orderBy('name')->paginate();
  307. return View::make('users')->with('users', $u);
  308. } else {
  309. App::abort(401, 'Diese Seite ist nicht für Dich.');
  310. }
  311. }));
  312. Route::get('users/{operation}/{id}', array('before' => 'auth', function($operation, $id) {
  313. if(!Auth::user()->admin) App::abort(401, 'Diese Seite ist nicht für Dich.');
  314. $u = User::findOrFail($id);
  315. switch($operation) {
  316. case 'mkadm':
  317. $u->admin = true;
  318. $u->save();
  319. $msg = $u->name . " ist jetzt ein Admin.";
  320. break;
  321. case 'rmadm':
  322. $u->admin = false;
  323. $u->save();
  324. $msg = $u->name . " ist kein Admin mehr.";
  325. break;
  326. case 'rmusr':
  327. $msg = $u->name . " wurde gelöscht.";
  328. $u->delete();
  329. break;
  330. }
  331. return Redirect::to('users')->with('message', $msg);
  332. }));
  333. Route::get('news', array('before' => 'auth', function() {
  334. return View::make('news');
  335. }));
  336. Route::post('news', array('before' => 'auth', function() {
  337. $vrules = array(
  338. 'headline' => 'required',
  339. 'body' => 'required'
  340. );
  341. $vfields = array(
  342. 'headline' => Input::get('headline'),
  343. 'body' => Input::get('body')
  344. );
  345. $val = Validator::make($vfields, $vrules);
  346. if($val->fails()) {
  347. return View::make('news')->with('errors', $val->messages());
  348. } else {
  349. $n = new News();
  350. $n->author = Auth::user()->id;
  351. $n->headline = Input::get('headline');
  352. $n->body = Input::get('body');
  353. $n->save();
  354. return Redirect::to('/')->with('message', 'News erstellt!');
  355. }
  356. }));
  357. Route::get('passwort-vergessen', function() {
  358. return View::make('pwform');
  359. });
  360. Route::post('passwort-vergessen', function() {
  361. $credentials = array('email' => Input::get('email'));
  362. return Password::remind($credentials, function($message, $user) {
  363. $message->subject('Passwort für Dumbo zurücksetzen.');
  364. });
  365. });
  366. Route::get('passwort-reset/{token}', function($token) {
  367. return View::make('pwreset')->with('token', $token);
  368. });
  369. Route::post('passwort-reset', function() {
  370. $credentials = array(
  371. 'email' => Input::get('email'),
  372. 'password' => Input::get('password'),
  373. 'password_confirmation' => Input::get('password_confirmation')
  374. );
  375. return Password::reset($credentials, function($user, $password) {
  376. $user->password = Hash::make($password);
  377. $user->save();
  378. return Redirect::to('/');
  379. });
  380. });