f70cf014421835d157bc2639e2a2be58a650f806
[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) {
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                                 $plant = $this->m_User->GetPlant($plantname);
75                         }
76                         
77                         $res = "";
78
79                         switch ($func) {
80                                 case "IsLoggedIn":
81                                         $res = $this->m_Server->IsLoggedIn();
82                                         break;
83
84                                 case "Auth":
85                                         $res = $this->m_Server->LogIn($username, $pw);
86                                         break;
87
88                                 case "Logout":
89                                         $res = $this->m_Server->LogOut();
90                                         break;
91
92                                 case "Register":
93                                         $res = $this->m_Server->Register($username, $pw);
94                                         break;
95
96                                 case "SavePlant":
97                                         if ($plant != null) {
98                                                 $res = $plant->Save($code);
99                                         } else {
100                                         $res = $this->CreatePlant($plantname, $code);
101                                         }
102                                         break;
103
104                                 case "CreatePlant":
105                                         $res = $this->CreatePlant($plantname, $code);
106                                         break;
107
108                                 case "ActivatePlant":
109                                         $res = $plant->Activate();;
110                                         break;
111
112                                 case "GetPlantList":
113                                         if (isset($this->m_User)) {
114                                                 $res = $this->m_User->GetPlantList();
115                                         } else {
116                                                 $res = "{ success: false, msg: 'Sie sind nicht angemeldet' }";
117                                         }
118                                         break;
119
120                                 case "GetSeasonList":
121                                         $res = $this->GetSeasonScoreList();
122                                         break;
123
124                                 case "DeletePlant":
125                                         if ($plant != null) {
126                                                 $res = $plant->Delete();
127                                         } else {
128                                                 $res = "{ success: false, msg: \"Keine Pflanze mit dem Namen '".$plantname."' für den Nutzer '".$username."' gefunden.\" }";
129                                         }
130                                         break;
131
132                                 case "TestPlant":
133                                         break;
134
135                                 case "ValidatePlant":
136                                         if ($plant != null) {
137                                                 $res = $plant->Validate();
138                                         } else {
139                                                 $res = "{ success: false, msg: \"Keine Pflanze mit dem Namen '".$plantname."' für den Nutzer '".$username."' gefunden.\" }";
140                                         }
141                                         break;
142
143                                 default:
144                                         $res = "{ success : false, msg: \"Unkown RPC call '".$func."'\"}";
145                         }
146                 
147                         return new RPCAnswer($func, $res);
148                 }
149         }
150 ?>