Film.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. class Film extends Eloquent {
  3. protected $table = "films";
  4. public function comments() {
  5. return $this->hasMany('Comment', 'film');
  6. }
  7. public function votes() {
  8. return $this->hasMany('Vote', 'film');
  9. }
  10. public function besitzer() {
  11. return $this->belongsTo('User', 'user');
  12. }
  13. public function scopeZuletztGesehen($query) {
  14. return $query->whereNull('abgelehnt')->whereNotNull('gesehen')->orderBy('gesehen', 'DESC')->take(5);
  15. }
  16. public function scopeZuletztAbgelehnt($query) {
  17. return $query->whereNull('gesehen')->whereNotNull('abgelehnt')->orderBy('abgelehnt', 'DESC')->take(5);
  18. }
  19. public function scopeNeuesteVorschlage($query) {
  20. return $query->whereNull('gesehen')->whereNull('abgelehnt')->orderBy('updated_at', 'DESC')->take(5);
  21. }
  22. public function scopeMeistgewunschteVorschlage($query) {
  23. $nextfilm = Dumbo::find(1)->film;
  24. return $query
  25. ->addSelect(DB::raw('film_films.*, COUNT(case when film_votes.stimme IS TRUE then 1 end) as upvotes, COUNT(*) as votes'))
  26. ->leftJoin('votes', 'votes.film', '=', 'films.id')
  27. ->leftJoin('users', 'users.id', '=', 'votes.user')
  28. ->whereNull('gesehen')
  29. ->whereNull('abgelehnt')
  30. ->where('films.id', "!=", is_object($nextfilm) ? $nextfilm->id : 0)
  31. ->where('users.settings', 'NOT LIKE', '%disabled":true%')
  32. ->groupBy('films.id')
  33. ->orderBy('upvotes', 'DESC')
  34. ->orderBy('vorgeschlagen', 'ASC');
  35. }
  36. public function getVotes() {
  37. return Vote::where('film', '=', $this->id)
  38. ->where('users.settings', 'NOT LIKE', '%disabled":true%')
  39. ->leftJoin('users', 'users.id', '=', 'votes.user')
  40. ->count();
  41. }
  42. public function getUpvotes() {
  43. return Vote::where('film', '=', $this->id)
  44. ->whereRaw('stimme IS TRUE')
  45. ->where('users.settings', 'NOT LIKE', '%disabled":true%')
  46. ->leftJoin('users', 'users.id', '=', 'votes.user')
  47. ->count();
  48. }
  49. public function getBewertung() {
  50. if(!is_null($this->gesehen)) {
  51. $count = Comment::where('film', $this->id)->where('bewertung', '>', 0)->count();
  52. return $count > 0 ? Comment::where('film', $this->id)->where('bewertung', '>', 0)->sum('bewertung') / $count : 0;
  53. } else return (0);
  54. }
  55. public function getPoster() {
  56. $tmdb = new TMDb(Config::get('constants.tvdb.apikey'), 'de', TRUE);
  57. $tmovie = $tmdb->getMovie($this->tvdbid);
  58. return $tmdb->getImageUrl($tmovie['poster_path'], TMDb::IMAGE_POSTER, 'w342');
  59. }
  60. }