ed5d8db67ac2283f8c5728b0b0f824c3a14ae1b5
[L-seed.git] / web / php / Plant.php
1 <?php
2         
3         class Plant
4         {
5             public $ID;
6             public $UserID;
7             public $Name;
8             public $Code;
9                 public $IsActive;
10                 public $m_Database;
11
12                 public function __construct($id, $userid, $name, $code, $database) {
13                         $this->ID = $id;
14                         $this->UserID = $userid;
15                         $this->Name = $name;
16                         $this->Code = $code;
17                         $this->IsActive = false;
18                         $this->m_Database = $database;
19                         if (!isset($GLOBALS['ValidatorFile'])) {
20                                 $GLOBALS['ValidatorFile'] = '/home/lseed/.cabal/bin/validate';
21                                 #if ($GLOBALS['WINDOWS']) {
22                                 #       $GLOBALS['ValidatorFile'] = '../cgi/validate.exe';
23                                 #}
24                         }
25                 }
26
27                 public function ToJson() {
28                         
29                         $active = 'false';
30                         if ($this->IsActive) { $active = 'true'; } else { $active = 'false'; }
31                         return "{ ID: " . $this->ID . ", Name: '" . $this->Name . "', Code: '" . $this->Code . "', IsActive: " . $active . " }";
32                 }
33
34                 public function ToJsonArray() {
35                         return "[ " . $this->ID . ", '" . $this->Name . "', '" . $this->Code . "' ]";
36                 }
37
38                 public function Save($code) {
39                         $result = "{ success: true, msg: '' }";
40                         
41                         $this->Code = $code;
42
43                         $user = $this->m_Database->GetUserByID($this->UserID);
44                         if ($user) {
45                                 $plant = $this->m_Database->GetPlant($user->ID, $this->Name);
46                                 
47                                 if ($plant == null) {
48                                         if (!$this->m_Database->InsertNewPlant($user->ID, $this->Name, $this->Code)) {
49                                                 $result = "{ success: false, msg: 'Pflanze konnte nicht erstellt werden.' }";
50                                         }
51                                 } else {
52                                         $plant->Code = $code;
53                                         if (!$this->m_Database->UpdatePlant($plant)) {
54                                                 $result = "{ success: false, msg: 'Pflanze konnte nicht aktualisiert werden.' }";
55                                         }
56                                 }
57                         } else {
58                                 $result = "{ success: false, msg: 'User (id: \'".$this->UserID."\' could not be found.' }";
59                         }
60
61                         return $result;
62                 }
63
64                 public function Delete() {
65                         $result = "{ success: false, msg: 'Pflanze konnte nicht gelöscht werden.' }";
66
67                         if ($this->m_Database->DropPlant($this)) {
68                                 $result = "{ success: true, msg: '' }";
69                         }
70
71                         return $result;
72                 }
73                 
74                 public function Validate() {
75                         $arr =  $this->ValidateCode();
76                         return $arr[1];
77                 }
78
79                 public function IsValid() {
80                         $arr =  $this->ValidateCode();
81                         return $arr[0];
82                 }
83                 
84                 public function ValidateCode() {
85                         $result = "{valid: false, line: 0, column: 0, msg: 'Interner Server Fehler'}";
86                         $result_ok = 0;
87                         
88                         $descriptorspec = array(
89                            0 => array("pipe", "r"),  // STDIN ist eine Pipe, von der das Child liest
90                            1 => array("pipe", "w"),  // STDOUT ist eine Pipe, in die das Child schreibt
91                            2 => array("pipe", "w")   // STDERR
92                         );
93
94                         $cwd = realpath(".");
95
96                         $process = proc_open($GLOBALS['ValidatorFile'], $descriptorspec, $pipes, $cwd, array());
97
98                         if (is_resource($process)) {
99                                 // $pipes sieht nun so aus:
100                                 // 0 => Schreibhandle, das auf das Child STDIN verbunden ist
101                                 // 1 => Lesehandle, das auf das Child STDOUT verbunden ist
102                                 // Jedwede Fehlerausgaben werden an /tmp/error-output.txt angefügt
103
104                                 fwrite($pipes[0], $this->Code);
105                                 fclose($pipes[0]);
106
107                                 $output = stream_get_contents($pipes[1]);
108                                 fclose($pipes[1]);
109
110                                 //echo stream_get_contents($pipes[2]);
111                                 fclose($pipes[2]);
112
113                                 $return_value = proc_close($process);
114                                 
115                                 //echo $return_value;
116                                 if ($return_value == 0) {
117                                         $result = $output;
118                                         $result_ok = 1;
119                                 }
120                         }
121
122                         return array($result_ok, $result);
123                 }
124                 
125                 public function Activate() {
126                         $result = "{ success: false, msg: 'Ihre Pflanze konnte nicht aktiviert werden.' }";
127                         
128                         if ($this->IsActive) {
129                                 $result = "{ success: false, msg: 'Ihre Pflanze ist bereits aktiv.' }";
130                         } else {
131                                 if ($this->m_Database->SetUsersNextSeed($this->UserID, $this->ID)) {
132                                         $result = "{ success: true, msg: '' }";
133                                         $this->IsActive = true;
134                                 }
135                         }
136                         
137                         return $result;
138                 }
139         }
140 ?>