Remove some debugging code
[L-seed.git] / web / php / Controller.php
1 <?php
2         include("Response.php");
3         include("Server.php");
4         
5         class Controller
6         {
7                 protected $m_Database;
8                 protected $m_Server;
9                 protected $m_User;
10                 protected $m_IsLoggedIn;
11
12                 public function __construct() {
13                         $this->m_Database = new Database();
14                         $this->m_Server = new Server($this);
15                         
16                         if (isset($_SESSION['user'])) {
17                                 $this->m_User = $this->m_Database->GetUser($_SESSION['user']);
18                                 $this->m_IsLoggedIn = $this->m_Server->IsLoggedIn();
19                         }
20                 }
21                 
22                 public function GetDatabase() { return $this->m_Database; }
23                 public function GetUser() { return $this->m_User; }
24                 public function SetUser($user) { $this->m_User = $user; }
25                 public function IsLoggedIn() { $this->m_Server->IsLoggedIn(); }
26
27                 private function CreatePlant($plantname, $code) {
28                         $res = "{ success: false, msg: 'Unbekannter Fehler' }";
29                         
30                         if (isset($this->m_User)) {
31                                 if ($plantname != null) {
32                                         if ($this->m_User->CreatePlant($plantname, $code) == "true") {
33                                                 $res = "{ success: true, msg: 'Es wurde eine neue Pflanze für sie angelegt.' }";
34                                         } else {
35                                                 $res = "{ success: false, msg: 'Pflanze konnte nicht erstellt werden.' }";
36                                         }
37                                 } else {
38                                         $res = "{ success: false, msg: 'Kein Name wurde übergeben:' }";
39                                 }
40                         } else {
41                                 $res = "{ success: false, msg: 'Sie sind nicht angemeldet' }";
42                         }
43                         
44                         return $res;
45                 }
46                 
47                 private function GetSeasonScoreList() {
48                         $result = "{ success: false, msg: 'Unbekannter Fehler' }";
49                         
50                         $list = $this->m_Database->GetAllSeasonScores();
51                         
52                         if (count($list) > 0) {
53                                 $result = "{ list: [";
54                                 $first = true;
55                                 foreach ($list as $seasonscore) {
56                                         if (!$first) {
57                                                 $result .= ", ";
58                                         }
59                                         $result .= $seasonscore->ToJson();
60                                         $first = false;
61                                 }
62                                 $result .= "] }";
63                         }
64                         
65                         return $result;
66                 }
67
68                 public function HandleRemoteProcedureCall($func, $username, $pw, $plantname, $code, $plantid) {
69                         $plant = null;
70                         if ($this->m_User == null) {
71                                 $this->m_User = $this->m_Database->GetUser($username);
72                         }
73                         if ($this->m_User != null) {
74                                 if ($plantid != null) {
75                                         $plant = $this->m_User->GetPlantById($plantid);
76                                 } else {
77                                         $plant = $this->m_User->GetPlant($plantname);
78                                 }
79                         }
80                         
81                         $res = "";
82
83                         switch ($func) {
84                                 case "IsLoggedIn":
85                                         $res = $this->m_Server->IsLoggedIn();
86                                         break;
87
88                                 case "Auth":
89                                         $res = $this->m_Server->LogIn($username, $pw);
90                                         break;
91
92                                 case "Logout":
93                                         $res = $this->m_Server->LogOut();
94                                         break;
95
96                                 case "Register":
97                                         $res = $this->m_Server->Register($username, $pw);
98                                         break;
99
100                                 case "SavePlant":
101                                         if ($plant != null) {
102                                                 $res = $plant->Save($code);
103                                         } else {
104                                         $res = $this->CreatePlant($plantname, $code);
105                                         }
106                                         break;
107
108                                 case "CreatePlant":
109                                         $res = $this->CreatePlant($plantname, $code);
110                                         break;
111
112                                 case "ActivatePlant":
113                                         $res = $plant->Activate();;
114                                         break;
115
116                                 case "GetPlantList":
117                                         if (isset($this->m_User)) {
118                                                 $res = $this->m_User->GetPlantList();
119                                         } else {
120                                                 $res = "{ success: false, msg: 'Sie sind nicht angemeldet' }";
121                                         }
122                                         break;
123
124                                 case "GetSeasonList":
125                                         $res = $this->GetSeasonScoreList();
126                                         break;
127
128                                 case "DeletePlant":
129                                         if ($plant != null) {
130                                                 $res = $plant->Delete();
131                                         } else {
132                                                 $res = "{ success: false, msg: \"Keine Pflanze mit dem Namen '".$plantname."' für den Nutzer '".$username."' gefunden.\" }";
133                                         }
134                                         break;
135
136                                 case "TestPlant":
137                                         break;
138
139                                 case "CheckSyntax":
140                                         # Create a temporary plant              
141                                         $plant = new Plant(0,0,$plantname,$code,0);
142                                         $res = $plant->Validate();
143                                         break;
144
145                                 default:
146                                         $res = "{ success : false, msg: \"Unkown RPC call '".$func."'\"}";
147                         }
148                 
149                         return new RPCAnswer($func, $res);
150                 }
151         }
152 ?>