routes.php 19 KB

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