Completed
Push — master ( 33d684...583195 )
by Taavo-Taur
05:02
created

app/controllers/AdminController.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
//namespace Admin;
4
5
use Illuminate\Support\Facades\Input;
6
use Krucas\Notification\Facades\Notification;
7
use Illuminate\Support\Facades\Redirect;
8
use Carbon\Carbon;
9
use Goutte\Client;
10
use Symfony\Component\DomCrawler\Crawler;
11
class AdminController extends \AdminBaseController {
12
13
	/**
14
	 * Admin Homepage
15
	 */
16
	public function index() {
17
		$stats = [
18
			'users' => User::count(),
19
			'leagues' => League::count(),
20
			'movies' => Movie::count(),
21
		];
22
23
24
		$this->layout->title = 'Admin';
25
		$this->layout->content = View::make('admin.index', compact('stats'));
26
	}
27
	
28
	public function movies() {
29
		$movies_query = Movie::query();
30
		$movies_query->orderBy('release', 'desc');
31
		$movies_query->select('movies.*');
32
		$movies = $movies_query->paginate(1000);
33
		$this->layout->title = 'Movies';
34
		$this->layout->content = View::make('admin.movies',compact('movies'));
35
	}
36
	private function getUrl($mojoid) {
0 ignored issues
show
This method is not used, and could be removed.
Loading history...
37
		return 'http://www.boxofficemojo.com/daily/chart/?view=1day&sortdate=' . $date->toDateString();
0 ignored issues
show
The variable $date does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
38
	}
39
40
	public function addMovie() {
41
		$mojo_id = str_replace(".htm",'',Input::get('movie'));
42
		$info = new stdClass();
43
		$info->found = false;
44
		$info->mojo_id = $mojo_id;
45
		if ($mojo_id != '')
46
		{
47
			$client = new Client();
48
			$crawler = $client->request('GET', 'http://www.boxofficemojo.com/movies/?page=daily&view=chart&id='.$mojo_id.'.htm', [], [], [
49
				'HTTP_USER_AGENT' => "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0"
50
			]);
51
			
52
			
53
			$info->title = $crawler->filterXPath("//div[@id='body']//font[@face='Verdana']/b")->text();
54
			$info->release_date = new Carbon($crawler->filterXPath("//div[@id='body']//td[starts-with(.,'Release Date')]//a")->text());
55
			
56
			$info->grosses = $crawler->filter('#body table[border="0"][class="chart-wide"] tr[bgcolor]:not([bgcolor="#dcdcdc"])')->each(function(Crawler $node) {
57
				$cols = $node->children();
58
59
				$myinfo = [
60
					'release_date'   => new Carbon($cols->eq(1)->text()),
61
					'domestic_total' => intval(str_replace(['$', ','], '', $cols->eq(8)->text())),
62
				];
63
				
64
				return $myinfo;
65
			});
66
			
67
			$info->found = true;
68
			
69
			
70
			//Notification::success($title." ".$release_date." ".$boxmojo_id);
71
72
			//return Redirect::route('admin.movies');			
73
		}
74
75
		$this->layout->content = View::make('admin.addmovie',[
76
			'info' => $info
77
		]);
78
	}
79
	
80
	public function confirmMovie() {
81
		// Create the league
82
		$movie = new Movie(Input::only([
83
			'title', 'release_date', 'boxmojo_id'
84
		]));
85
		$movie->name = Input::get('title');
86
		$movie->release = Input::get('release_date');
87
		$movie->boxmojo_id = Input::get('mojo_id');
88
		$movie->save();
89
		
90
		$earnings = unserialize(Input::get('grosses'));
91
		$latest_earnings_id = 0;
92
		$count = 0;
93
		foreach($earnings as $gross) { 
94
			$earnings = new MovieEarning([
95
				'movie_id' => $movie->id,
96
				'date'     => $gross['release_date'],
97
				'domestic' => $gross['domestic_total']
98
			]);
99
			$earnings->save();
100
			$latest_earnings_id = $earnings->id;
101
			$count += 1;
102
		}
103
		
104
		if($latest_earnings_id != 0) {
105
			$movie->latest_earnings_id = $latest_earnings_id;
106
			$movie->save();
107
		}
108
		
109
		Notification::success("Successfully added ".$movie->name." with ".$count." day(s) of earnings");
110
111
		return Redirect::route('admin.addMovie');
112
113
	}
114
	
115
	
116
}