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