|
1
|
|
|
<?php |
|
2
|
|
|
namespace PhpDraft\Domain\Services; |
|
3
|
|
|
|
|
4
|
|
|
use Silex\Application; |
|
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
6
|
|
|
use PhpDraft\Domain\Entities\Draft; |
|
7
|
|
|
use PhpDraft\Domain\Entities\ProPlayer; |
|
8
|
|
|
use PhpDraft\Domain\Models\PhpDraftResponse; |
|
9
|
|
|
|
|
10
|
|
|
class ProPlayerService { |
|
11
|
|
|
private $app; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(Application $app) { |
|
14
|
|
|
$this->app = $app; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function SearchPlayersManual($league, $first, $last, $team, $position) { |
|
18
|
|
|
$response = new PhpDraftResponse(); |
|
19
|
|
|
|
|
20
|
|
|
try { |
|
21
|
|
|
$players = $this->app['phpdraft.ProPlayerRepository']->SearchPlayersManual($league, $first, $last, $team, $position); |
|
22
|
|
|
|
|
23
|
|
|
$response->success = true; |
|
24
|
|
|
$response->players = $players; |
|
|
|
|
|
|
25
|
|
|
} catch (\Exception $e) { |
|
26
|
|
|
$response->success = false; |
|
27
|
|
|
$response->errors = array($e->getMessage()); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
return $response; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function SearchPlayers($league, $searchTerm) { |
|
34
|
|
|
$response = new PhpDraftResponse(); |
|
35
|
|
|
|
|
36
|
|
|
try { |
|
37
|
|
|
$players = $this->app['phpdraft.ProPlayerRepository']->SearchPlayers($league, $searchTerm); |
|
38
|
|
|
|
|
39
|
|
|
#If there's a space and no matches so far, create another searches where we manually split them firstname/lastname by sace automatically |
|
40
|
|
|
$split_name_automatically = count($players) == 0 && strpos($searchTerm, " ") != false; |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
if ($split_name_automatically) { |
|
43
|
|
|
$names = explode(" ", $searchTerm, 2); |
|
44
|
|
|
$players = $this->app['phpdraft.ProPlayerRepository']->SearchPlayersByAssumedName($league, $names[0], $names[1]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$response->success = true; |
|
48
|
|
|
$response->players = $players; |
|
|
|
|
|
|
49
|
|
|
} catch (\Exception $e) { |
|
50
|
|
|
$response->success = false; |
|
51
|
|
|
$response->errors = array($e->getMessage()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $response; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function Upload($sport, &$file) { |
|
58
|
|
|
$response = new PhpDraftResponse(); |
|
59
|
|
|
|
|
60
|
|
|
$tempName = $file->getRealPath(); |
|
61
|
|
|
$pro_players = array(); |
|
62
|
|
|
|
|
63
|
|
|
if (($handle = fopen($tempName, 'r')) == FALSE) { |
|
64
|
|
|
$response->success = false; |
|
65
|
|
|
$response->errors[] = "Files permission issue: unable to open CSV on server."; |
|
66
|
|
|
|
|
67
|
|
|
return $response; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if (SET_CSV_TIMEOUT) { |
|
|
|
|
|
|
71
|
|
|
set_time_limit(0); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
while (($data = fgetcsv($handle, 1000, ';')) !== FALSE) { |
|
75
|
|
|
if ($data[0] == "Player") { |
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$new_player = new ProPlayer(); |
|
80
|
|
|
|
|
81
|
|
|
$new_player->league = $sport; |
|
82
|
|
|
$name_column = explode(",", $data[0]); |
|
83
|
|
|
|
|
84
|
|
|
if (count($name_column) == 2) { |
|
85
|
|
|
$new_player->last_name = trim($name_column[0]); |
|
86
|
|
|
$new_player->first_name = trim($name_column[1]); |
|
87
|
|
|
} else { |
|
88
|
|
|
$new_player->last_name = "Player"; |
|
89
|
|
|
$new_player->first_name = "Unknown"; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$new_player->position = isset($data[1]) ? trim($data[1]) : ''; |
|
93
|
|
|
$new_player->team = isset($data[2]) ? trim($data[2]) : ''; |
|
94
|
|
|
|
|
95
|
|
|
$pro_players[] = $new_player; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
fclose($handle); |
|
99
|
|
|
|
|
100
|
|
|
try { |
|
101
|
|
|
$this->app['phpdraft.ProPlayerRepository']->SaveProPlayers($sport, $pro_players); |
|
102
|
|
|
$response->success = true; |
|
103
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
104
|
|
|
$response->success = false; |
|
105
|
|
|
$response->errors[] = "Error encountered when updating new players to database: " . $e->getMessage(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $response; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|