-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamesController.php
More file actions
135 lines (116 loc) · 3.15 KB
/
GamesController.php
File metadata and controls
135 lines (116 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php //Bismillah
/*-------------------------*\
| gamesController Class |
\*-------------------------*/
/*
DESCRIPTION:
Provides interface between xbox.js and gamesModel
MVC MIGRATION:
- Copy to Controllers folder
- All migration steps are found within the class declaration line or the constructor
Migration Steps:
1. Change the parent class to a proper Controller parent class (e.g. CI_Controller or AppController)
- That's all for CodeIgniter, next two steps are for CakePHP
2. Uncomment $uses array
3. Delete both lines calling ->load->model();
*/
class GamesController extends fakeMVCController // <- Step 1.) Change to proper Controller class when migrating to MVC framework
{
//Response Properties
public $response = array();
public $json = "";
//MVC: Loads models for CakePHP
//public $uses = array("Game", "User"); // <- Step 2.) UNCOMMENT for CakePHP
//Constructor - loads Game and User models and sets Game's data to $_POST
public function __construct()
{
//Call parent constructor
parent::__construct();
//MVC: Load Game and User models
$this->load->model('Game'); // <- Step 3.) DELETE for CakePHP
$this->load->model('User'); // <- ^
if ($this->Game->error)
{
$this->error = $this->Game->error;
$this->respond();
}
//Set data from $_POST
if (isset($_POST))
{
$this->Game->data = $_POST;
}
}
//MVC: default action
public function index()
{
$this->getAll();
}
//MVC: Sends JSON Response to View
public function respond($resp = false)
{
if ($resp)
{
$this->response = $resp;
}
//Encode response and send as JSON; Xbox.js expects a JSON object with a games array and error string/false
$json = $this->response;
$this->json = json_encode($json);
header("Content-Type:text/json");
die($this->json);
}
//Return All Games
public function getAll()
{
//Load sorted games and return to xbox.js
$this->respond( $this->Game->loadGames() );
}
//Return Filtered games
public function find()
{
//Load filtered games and return to xbox.js
$this->respond( $this->Game->loadGames() );
}
//Add a Game
public function add()
{
//Set eligibility in game model's data array
$this->Game->data['eligible'] = $this->User->checkEligible();
//Ask game model to add game
$rs = $this->Game->add();
//If there are no errors, tell user model to set lastVote
if (!$rs['error'])
{
$this->User->setLastVote();
}
//Respond with new game
$this->respond($rs);
}
//Cast a Vote for a Title
public function vote()
{
//Set eligibility in game model's data array
$this->Game->data['eligible'] = $this->User->checkEligible();
//Ask game model to vote
$rs = $this->Game->vote();
//If there are no errors, tell user model to set lastVote
if (!$rs['error'])
{
$this->User->setLastVote();
}
//Respond with updated games array
$this->respond($rs);
}
//Purchase a game
public function purchase()
{
//Purchase game and return updated games to xbox.js
$this->respond( $this->Game->purchase() );
}
//Clear All Games
public function clearAll()
{
//Clear games and return empty games array to xbox.js
$this->respond( $this->Game->clearGames() );
}
}
?>