routes.php 19 KB

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