Film.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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->whereNotNull('gesehen')->orderBy('gesehen', 'DESC')->take(5);
  15. }
  16. public function scopeNeuesteVorschlage($query) {
  17. return $query->whereNull('gesehen')->orderBy('updated_at', 'DESC')->take(5);
  18. }
  19. public function scopeMeistgewunschteVorschlage($query) {
  20. $nextfilm = Dumbo::find(1)->film;
  21. return $query
  22. ->addSelect(DB::raw('film_films.*, COUNT(case when film_votes.stimme IS TRUE then 1 end) as upvotes'))
  23. ->leftJoin('votes', 'votes.film', '=', 'films.id')
  24. ->leftJoin('users', 'users.id', '=', 'votes.user')
  25. ->whereNull('gesehen')
  26. ->where('films.id', "!=", is_object($nextfilm) ? $nextfilm->id : 0)
  27. ->where('users.settings', 'NOT LIKE', '%disabled":true%')
  28. ->groupBy('films.id')
  29. ->orderBy('upvotes', 'DESC')
  30. ->orderBy('vorgeschlagen', 'ASC');
  31. }
  32. public function getVotes() {
  33. return Vote::where('film', '=', $this->id)->count();
  34. }
  35. public function getUpvotes() {
  36. return Vote::where('film', '=', $this->id)->whereRaw('stimme IS TRUE')->count();
  37. }
  38. public function getBewertung() {
  39. if(!is_null($this->gesehen)) {
  40. $count = Comment::where('film', $this->id)->where('bewertung', '>', 0)->count();
  41. return $count > 0 ? Comment::where('film', $this->id)->where('bewertung', '>', 0)->sum('bewertung') / $count : 0;
  42. } else return (0);
  43. }
  44. public function getPoster() {
  45. $tmdb = new TMDb(Config::get('constants.tvdb.apikey'), 'de', TRUE);
  46. $tmovie = $tmdb->getMovie($this->tvdbid);
  47. return $tmdb->getImageUrl($tmovie['poster_path'], TMDb::IMAGE_POSTER, 'w342');
  48. }
  49. }