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
|
1 |
|
public function index() { |
17
|
|
|
$stats = [ |
18
|
1 |
|
'users' => User::count(), |
19
|
1 |
|
'leagues' => League::count(), |
20
|
1 |
|
'movies' => Movie::count(), |
21
|
1 |
|
]; |
22
|
|
|
|
23
|
|
|
|
24
|
1 |
|
$this->layout->title = 'Admin'; |
25
|
1 |
|
$this->layout->content = View::make('admin.index', compact('stats')); |
26
|
1 |
|
} |
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(50); |
33
|
|
|
$this->layout->title = 'Movies'; |
34
|
|
|
$this->layout->content = View::make('admin.movies',compact('movies')); |
35
|
|
|
} |
36
|
|
|
public function addMovie() { |
37
|
|
|
$mojo_id = str_replace(".htm",'',Input::get('movie')); |
38
|
|
|
$info = new stdClass(); |
39
|
|
|
$info->found = false; |
40
|
|
|
$info->mojo_id = $mojo_id; |
41
|
|
|
$info->old = null; |
42
|
|
|
$info->grosses = null; |
43
|
|
|
if ($mojo_id != '') |
44
|
|
|
{ |
45
|
|
|
$client = new Client(); |
46
|
|
|
$crawler = $client->request('GET', 'http://www.boxofficemojo.com/movies/?page=daily&view=chart&id='.$mojo_id.'.htm', [], [], [ |
47
|
|
|
'HTTP_USER_AGENT' => "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0" |
48
|
|
|
]); |
49
|
|
|
$mojo_query = Movie::query(); |
50
|
|
|
$mojo_query->where('boxmojo_id', $mojo_id); |
51
|
|
|
$query_result = $mojo_query->first(); |
52
|
|
|
if ($query_result) |
53
|
|
|
{ |
54
|
|
|
$info->old = $query_result; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$info->title = $crawler->filterXPath("//div[@id='body']//font[@face='Verdana']/b")->text(); |
58
|
|
|
$info->release_date = new Carbon($crawler->filterXPath("//div[@id='body']//td[starts-with(.,'Release Date')]//b")->text()); |
59
|
|
|
if (!$info->old) |
60
|
|
|
{ |
61
|
|
|
$info->grosses = $crawler->filter('#body table[border="0"][class="chart-wide"] tr[bgcolor]:not([bgcolor="#dcdcdc"])')->each(function(Crawler $node) { |
62
|
|
|
$cols = $node->children(); |
63
|
|
|
|
64
|
|
|
$myinfo = [ |
65
|
|
|
'release_date' => new Carbon($cols->eq(1)->text()), |
66
|
|
|
'domestic_total' => intval(str_replace(['$', ','], '', $cols->eq(8)->text())), |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
return $myinfo; |
70
|
|
|
}); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$info->found = true; |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
//Notification::success($title." ".$release_date." ".$boxmojo_id); |
|
|
|
|
77
|
|
|
|
78
|
|
|
//return Redirect::route('admin.movies'); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->layout->content = View::make('admin.addmovie',[ |
82
|
|
|
'info' => $info |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function confirmMovie() { |
87
|
|
|
// Create the league |
88
|
|
|
$old_id = Input::get('old_id'); |
89
|
|
|
if ($old_id) { |
90
|
|
|
$movie_query = Movie::query(); |
91
|
|
|
$movie_query->where('id',$old_id); |
92
|
|
|
$movie = $movie_query->first(); |
93
|
|
|
} |
94
|
|
|
else |
95
|
|
|
{ |
96
|
|
|
$movie = new Movie(Input::only([ |
97
|
|
|
'title', 'release_date', 'boxmojo_id' |
98
|
|
|
])); |
99
|
|
|
} |
100
|
|
|
$movie->name = Input::get('title'); |
101
|
|
|
$movie->release = Input::get('release_date'); |
102
|
|
|
$movie->boxmojo_id = Input::get('mojo_id'); |
103
|
|
|
|
104
|
|
|
$movie->save(); |
105
|
|
|
|
106
|
|
|
$earnings = unserialize(Input::get('grosses')); |
107
|
|
|
$count = 0; |
108
|
|
|
if ($earnings) |
109
|
|
|
{ |
110
|
|
|
$latest_earnings_id = 0; |
111
|
|
|
|
112
|
|
|
foreach($earnings as $gross) { |
113
|
|
|
$earnings = new MovieEarning([ |
114
|
|
|
'movie_id' => $movie->id, |
115
|
|
|
'date' => $gross['release_date'], |
116
|
|
|
'domestic' => $gross['domestic_total'] |
117
|
|
|
]); |
118
|
|
|
$earnings->save(); |
119
|
|
|
$latest_earnings_id = $earnings->id; |
120
|
|
|
$count += 1; |
121
|
|
|
} |
122
|
|
|
if($latest_earnings_id != 0) { |
123
|
|
|
$movie->latest_earnings_id = $latest_earnings_id; |
124
|
|
|
$movie->save(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
Notification::success("Successfully ".($old_id ? "updated " : "added ").$movie->name." with ".$count." day(s) of earnings"); |
130
|
|
|
|
131
|
|
|
return Redirect::route('admin.addMovie'); |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
} |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: