added activate plant functionality, improved datamapping, removed test button
authorSven Hecht <sven@sven-laptop.(none)>
Tue, 23 Jun 2009 22:04:26 +0000 (00:04 +0200)
committerSven Hecht <sven@sven-laptop.(none)>
Tue, 23 Jun 2009 22:04:26 +0000 (00:04 +0200)
web/js/communication.js
web/php/Communication.php
web/php/Controller.php
web/php/Database.php
web/php/Plant.php
web/php/User.php
web/php/page/createplant.pg
web/php/page/myplants.pg
web/php/tests/BuisnessLogicTest.php
web/php/tests/DatabaseTest.php

index a6923b7..784289f 100644 (file)
@@ -22,6 +22,8 @@ Lseed.MessageCommands = {
 };
 
 Lseed.Communication = function() {
+       
+       this.Username = '';
 
        // ----- Framework -----
        
@@ -192,6 +194,8 @@ Lseed.Communication = function() {
                        var pw = cmpPw.getValue();
                        pw = MD5(pw);
                        Ext.MessageBox.wait("Authentifiziere.", "Wird geladen...");
+                               
+                       this.Username = user;
                        
                        this.sendMessage(Lseed.MessageCommands.RPC, { func: 'Auth', user: user, pw: pw });
                        this.hideLoginDialog();
@@ -299,6 +303,8 @@ Lseed.Communication = function() {
                        if (pw == pwRepeat) {
                                pw = MD5(pw);
                                Ext.MessageBox.wait("Registriere.", "Wird geladen...");
+                               
+                               this.Username = user;
 
                                this.sendMessage(Lseed.MessageCommands.RPC, { func: 'Register', user: user, pw: pw });
                                this.hideRegisterDialog();
@@ -463,6 +469,7 @@ Lseed.Communication = function() {
                this.AddCallback("SavePlant", editor.SaveCallback.createDelegate(editor));
                this.AddCallback("DeletePlant", editor.DeleteCallback.createDelegate(editor));
                this.AddCallback("ValidatePlant", editor.CheckSyntaxCallback.createDelegate(editor));
+               this.AddCallback("ActivatePlant", editor.ActivateCallback.createDelegate(editor));
                
                this.sendMessage(Lseed.MessageCommands.RPC, {func: 'IsLoggedIn'});
        };
@@ -492,6 +499,24 @@ Lseed.Editor = function() {
                }
        };
        
+       this.Activate = function(plant) {
+               console.log(arguments);
+               communication.sendMessage(Lseed.MessageCommands.RPC, { 
+                       func: 'ActivatePlant'
+                       ,plant: plant.data.Name
+                       ,user: communication.Username
+               });
+       };
+       
+       this.ActivateCallback = function(data) {
+               if (data.success) {
+                       communication.showMessage("Erfolgreich aktiviert.", "info");
+                       communication.GetPlantList();
+               } else {
+                       communication.showMessage(data.msg, "error");
+               }
+       };
+       
        this.Test = function(plant, callback) {
                communication.showMessage("Diese Funktion ist leider momentan nicht verfügbar", "error");
        };
@@ -501,25 +526,39 @@ Lseed.Editor = function() {
        
        this.CheckSyntaxCallback = null;
        this.CheckSyntax = function(plant, callback) {
-               this.CheckSyntaxCallback = callback;
+               if (typeof callback != 'undefined' && callback != null) {
+                       this.CheckSyntaxCallback = callback;
+               }
                
                communication.sendMessage(Lseed.MessageCommands.RPC, { 
                        func: 'ValidatePlant'
+                       ,plant: plant.data.Name
                        ,code: plant.data.Code
                });
        };
        
-       this.CheckSyntaxCallback = function(data) {
-               if (!data.valid) {
-                       if (this.CheckSyntaxCallback != null) {
-                               this.CheckSyntaxCallback(data);
-                       } else {
-                               console.error("Lseed.Editor.CheckSyntaxCallback: no Callback given.");
-                       }
+       this.HandleSyntaxCheckAnswerForEditor = function(data) {
+               var pdEditor = Ext.getCmp("plantdefinitioneditor");
+               if (pdEditor) {
+                       communication.stopWaitingForPage();
+                       Ext.MessageBox.show({
+                               title:'Fehler'
+                               ,msg: data.msg
+                               ,buttons: Ext.Msg.OK
+                               ,fn: function() {
+                                       var index = editor.GetStartFromField(pdEditor, data.line-1, data.column);
+                                       if (index != -1) {
+                                               pdEditor.selectText(index-1, index);
+                                       }
+                               }
+                               ,icon: Ext.MessageBox.ERROR
+                       });
                } else {
-                       console.info("Syntax is Valid.");
+                       console.error("Lseed.Editor.CheckSyntaxCallback_Callback: 'plantdefinitioneditor' Could not be found.");
                }
-       };
+       }
+       
+       this.CheckSyntaxCallback = function() {};
        
        this.Delete = function(plant) {
                communication.sendMessage(Lseed.MessageCommands.RPC, { 
@@ -561,5 +600,8 @@ Lseed.Plant = Ext.data.Record.create([{
 }, {
        name: 'Code'
        ,type: 'string'
+}, {
+       name: 'IsActive'
+       ,type: 'boolean'
 }]);
 
index b150df4..30dc842 100644 (file)
@@ -4,6 +4,8 @@
        session_start();
        if (get_magic_quotes_gpc()) { $_POST = array_map( 'stripslashes', $_POST ); }
        
+       $_GLOBALS['WINDOWS'] = false;
+       
        include("Plant.php");
        include("User.php");
        include("Controller.php");
@@ -18,6 +20,9 @@
                switch ($_POST["cmd"]) {
                        case "RPC":
                                $username = $_POST["user"];
+                               if ($username == null) {
+                                       $username = $_SESSION['user'];
+                               }
                                $pw = $_POST["pw"];
                                $plantname = $_POST["plant"];
                                $code = $_POST["code"];
index 9474942..019365f 100644 (file)
                                        break;
 
                                case "CreatePlant":
-                                       $res = $this->CreatePlant($plantname, "");
+                                       $res = $this->CreatePlant($plantname, $code);
+                                       break;
+
+                               case "ActivatePlant":
+                                       $res = $plant->Activate();;
                                        break;
 
                                case "GetPlantList":
                                        if ($plant != null) {
                                                $res = $plant->Validate();
                                        } else {
-                                               $res = "{ success: false, msg: 'Keine Pflanze mit dem Namen '".$plantname."' für den Nutzer '".$username."' gefunden.' }";
+                                               $res = "{ success: false, msg: \"Keine Pflanze mit dem Namen '".$plantname."' für den Nutzer '".$username."' gefunden.\" }";
                                        }
                                        break;
                        }
index 578e21c..79a051e 100644 (file)
 
                        return $result;
                }
-               public function GetUser($user) {
+               public function GetUser($username) {
                        $result = null;
+                       $userid = null;
                        
-                       $stmt = $this->m_Connection->prepare("SELECT ID, Name, Password, IsAdmin FROM user WHERE Name=?");
+                       $stmt = $this->m_Connection->prepare("SELECT ID FROM user WHERE Name=?");
                        
                        if ($stmt) {
-                               $stmt->bind_param("s", $user);
+                               $stmt->bind_param("s", $username);
                                $stmt->execute();
-                               $stmt->bind_result( $id, $name, $pw, $isadmin);
+                               $stmt->bind_result( $userid);
 
-                               if ($stmt->fetch()) {
-                                       $result = new User($id, $name, $pw, $isadmin, $this);
-                               }
+                               $stmt->fetch();
                                $stmt->close();
                        } else {
                                die("You silly bastard. Not again!!!!");
                        }
+                       
+                       if ($userid != null) {
+                               $result = $this->GetUserByID($userid);
+                       }
 
                        return $result;
                }
                public function GetUserByID($userid) {
                        $result = null;
                        
-                       $stmt = $this->m_Connection->prepare("SELECT ID, Name, Password, IsAdmin FROM user WHERE ID=?");
+                       $stmt = $this->m_Connection->prepare("SELECT ID, Name, Password, IsAdmin, NextSeed FROM user WHERE ID=?");
                        
                        if ($stmt) {
                                $stmt->bind_param("d", $userid);
                                $stmt->execute();
-                               $stmt->bind_result( $id, $name, $pw, $isadmin);
+                               $stmt->bind_result( $id, $name, $pw, $isadmin, $nextseedid);
 
                                if ($stmt->fetch()) {
-                                       $result = new User($id, $name, $pw, $isadmin, $this);
+                                       $result = new User($id, $name, $pw, $isadmin, $nextseedid, $this);
                                }
                                $stmt->close();
                        } else {
                                die("AAARRRRRRR!!!!");
                        }
+                       
+                       if ($result != null) {
+                               $result->SeasonScore = $this->GetCurrentSeasonScore($result);
+                       }
 
                        return $result;
                }
+               public function SetUsersNextSeed($userid, $plantid) {
+                       $result = false;
+                       
+                       $stmt = $this->m_Connection->prepare("UPDATE user SET NextSeed=? WHERE ID=?");
+                       
+                       if ($stmt) {
+                               $stmt->bind_param("dd", $plantid, $userid);
+                               $stmt->execute();
+
+                               if ($stmt->affected_rows == 1) {
+                                       $result = true;
+                               }
+                               $stmt->close();
+                       } else {
+                               die("Look there, behind you. A three-headed monkey!");
+                       }
+                       
+                       return $result;
+               }
                public function InsertNewPlant($userid, $name, $code) {
                        $result = false;
                        
                }
                public function GetPlant($userid, $name) {
                        $result = null;
+                       $plantid = null;
 
-                       $stmt = $this->m_Connection->prepare("SELECT ID, UserID, Name, Code FROM plant WHERE UserID=? AND Name=?");
+                       $stmt = $this->m_Connection->prepare("SELECT ID FROM plant WHERE UserID=? AND Name=?");
                        
                        if ($stmt) {
                                $stmt->bind_param("ds", $userid, $name);
                                $stmt->execute();
-                               $stmt->bind_result( $id, $theuserid, $thename, $code);
+                               $stmt->bind_result( $plantid);
                                
-                               if ($stmt->fetch()) {
-                                       $result = new Plant($id, $theuserid, $thename, $code, $this);
-                               } else {
-                                       //echo "nope no plant like that found.";
-                               }
+                               $stmt->fetch();
                                $stmt->close();
                        } else {
-                               die("the server you requested is currently unavailable. Please engage in nose picking...");
+                               die("The server you requested is currently unavailable. Please engage in nosepicking...");
+                       }
+                       
+                       if ($plantid != null) {
+                               $result = $this->GetPlantByID($userid, $plantid);
                        }
+                       
                        return $result;
                }
                public function GetPlantByID($userid, $plantid) {
                                }
                                $stmt->close();
                        } else {
-                               die("the server you requested is currently unavailable. Please engage in nose picking...");
+                               die("WTF?");
                        }
+                       
+                       if ($result != null) {
+                               $user = $this->GetUserByID($userid);
+                               if ($user != null) {
+                                       $result->IsActive = $user->NextSeedID == $result->ID;
+                               } else {
+                                       die("Invisible? No waaaay.");
+                               }
+                       }
+                       
                        return $result;
                }
                public function UpdatePlant($plant) {
                }
                public function GetPlantsForUser($userid) {
                        $result = array();
+                       $plantids = array();
                        
-                       $stmt = $this->m_Connection->prepare("SELECT ID, UserID, Name, Code FROM plant WHERE UserID=?");
+                       $stmt = $this->m_Connection->prepare("SELECT ID FROM plant WHERE UserID=?");
                        if ($stmt) {
                                $stmt->bind_param("d", $userid);
                                $stmt->execute();
-                               $stmt->bind_result( $id, $userid, $name, $code);
+                               $stmt->bind_result($id);
 
                                while ($stmt->fetch()) {
-                                       $plant = new Plant($id, $userid, $name, $code, $this);
-                                       $result[] = $plant;
+                                       $plantids[] = $id;
                                }
                                $stmt->close();
                        } else {
                                die("None of that young lady!");
                        }
                        
+                       foreach ($plantids as $id) {
+                               $result[] = $this->GetPlantByID($userid, $id);
+                       }
+                       
                        return $result;
                }
+               public function GetCurrentSeasonScore($userid) {
+                       $result = null;
+                       
+                       $stmt = $this->m_Connection->prepare("SELECT ID, SeasonID, Score FROM seasonscore WHERE UserID=?");
+                       if ($stmt) {
+                               $stmt->bind_param("d", $userid);
+                               $stmt->execute();
+                               $stmt->bind_result( $id, $seasonid, $score);
+
+                               while ($stmt->fetch()) {
+                                       $score = new SeasonScore($id, $seasonid, $score, $this);
+                                       $result = $score;
+                               }
+                               $stmt->close();
+                       } else {
+                               die("WHY YOU LITTLE...!");
+                       }
+                       
+                       if ($result != null) {
+                               $result->Season = $this->GetSeasonForSeasonScore($score);
+                       }
+                       
+                       return $result;
+                       
+               }
+               
+               public function GetSeasonForSeasonScore($score) {
+                       $season = null;
+                       
+                       $stmt = $this->m_Connection->prepare("SELECT ID, IsRunning FROM season WHERE ID=?");
+                       if ($stmt) {
+                               $stmt->bind_param("d", $score->SeasonID);
+                               $stmt->execute();
+                               $stmt->bind_result( $id, $isrunnning);
+
+                               while ($stmt->fetch()) {
+                                       $season = new Season($id, $isrunnning);
+                               }
+                               $stmt->close();
+                       } else {
+                               die("You Crack me up little buddy.");
+                       }
+                       
+                       return $season;
+               }
        }
 ?>
index d149be9..7c97090 100644 (file)
@@ -6,6 +6,7 @@
            public $UserID;
            public $Name;
            public $Code;
+               public $IsActive;
                public $m_Database;
 
                public function __construct($id, $userid, $name, $code, $database) {
                        $this->UserID = $userid;
                        $this->Name = $name;
                        $this->Code = $code;
+                       $this->IsActive = false;
                        $this->m_Database = $database;
+                       if (!isset($GLOBALS['ValidatorFile'])) {
+                               $GLOBALS['ValidatorFile'] = '../cgi/validate';
+                               if ($GLOBALS['WINDOWS']) {
+                                       $GLOBALS['ValidatorFile'] = '../cgi/validate.exe';
+                               }
+                       }
                }
 
                public function ToJson() {
-                       return "{ ID: " . $this->ID . ", Name: '" . $this->Name . "', Code: '" . $this->Code . "' }";
+                       
+                       $active = 'false';
+                       if ($this->IsActive) { $active = 'true'; } else { $active = 'false'; }
+                       return "{ ID: " . $this->ID . ", Name: '" . $this->Name . "', Code: '" . $this->Code . "', IsActive: " . $active . " }";
                }
 
                public function ToJsonArray() {
@@ -75,8 +86,7 @@
 
                        $cwd = realpath("..\\cgi");
 
-
-                       $process = proc_open('validate.exe', $descriptorspec, $pipes, $cwd, array());
+                       $process = proc_open($GLOBALS['ValidatorFile'], $descriptorspec, $pipes, $cwd, array());
 
                        if (is_resource($process)) {
                                // $pipes sieht nun so aus:
 
                        return $result;
                }
+               
+               public function Activate() {
+                       $result = "{ success: false, msg: 'Ihre Pflanze konnte nicht aktiviert werden.' }";
+                       
+                       if ($this->IsActive) {
+                               $result = "{ success: false, msg: 'Ihre Pflanze ist bereits aktiv.' }";
+                       } else {
+                               if ($this->m_Database->SetUsersNextSeed($this->UserID, $this->ID)) {
+                                       $result = "{ success: true, msg: '' }";
+                                       $this->IsActive = true;
+                               }
+                       }
+                       
+                       return $result;
+               }
        }
 ?>
index c370be2..cd4bb1b 100644 (file)
@@ -6,13 +6,16 @@
            public $Name;
            public $Password;
            public $IsAdmin;
+           public $SeasonScore;
+               public $NextSeedID;
            public $m_Database;
 
-               public function __construct($id, $user, $md5pw, $isadmin, $database) {
+               public function __construct($id, $user, $md5pw, $isadmin, $nextseedid, $database) {
                        $this->ID = $id;
                        $this->Name = $user;
                        $this->Password = $md5pw;
                        $this->IsAdmin = $isadmin;
+                       $this->NextSeedID = $nextseedid;
                        $this->m_Database = $database;
                }
                
index d5ee719..d347416 100644 (file)
                                communication.showMessage("Ihre Pflanze benötigt einen Namen.", "error");
                        }
                }
-       },{
-               text: "Test"
-               ,icon: 'img/icons/magnifier.png'
-               ,cls: 'x-btn-text-icon'
-               ,handler: editor.Test.createDelegate(editor)
        },{
                text: "CheckSyntax"
                ,icon: 'img/icons/accept.png'
                ,handler: function() { 
                        communication.waitForPage();
                        var pdEditor = Ext.getCmp("plantdefinitioneditor");
-                       if (pdEditor) {
+                       var pnEditor = Ext.getCmp("plantdefinitionnamefield");
+                       if (pdEditor && pnEditor) {
+                               var name = pnEditor.getValue();
                                var code = pdEditor.getValue();
-                               editor.CheckSyntax({data:{Code:code}}, function(data) {
-                                       var pdEditor = Ext.getCmp("plantdefinitioneditor");
-                                       if (pdEditor) {
-                                               communication.stopWaitingForPage();
-                                               Ext.MessageBox.show({
-                                                       title:'Fehler'
-                                                       ,msg: data.msg
-                                                       ,buttons: Ext.Msg.OK
-                                                       ,fn: function() {
-                                                               var index = editor.GetStartFromField(pdEditor, data.line-1, data.column);
-                                                               if (index != -1) {
-                                                                       pdEditor.selectText(index-1, index);
-                                                               }
-                                                       }
-                                                       ,icon: Ext.MessageBox.ERROR
-                                               });
-                                       } else {
-                                               console.error("Lseed.Editor.CheckSyntaxCallback_Callback: 'plantdefinitioneditor' Could not be found.");
-                                       }
-                               }); 
+                               editor.CheckSyntax({data:{Code:code, Name:name}}, editor.HandleSyntaxCheckAnswerForEditor); 
                        } else {
                                console.error("Lseed.Editor.CheckSyntaxCallback_Callback: 'plantdefinitioneditor' Could not be found.");
                        }
@@ -88,4 +66,4 @@
                }
                ,value: 'RULE "MyRule"\nGROW BY 1'
        }]
-}
\ No newline at end of file
+}
index e3ee84f..d2f308f 100644 (file)
                                }
                        }
                },{
-                       id: 'plantlistTestPlant'
-                       ,text: 'Test'
-                       ,icon: 'img/icons/magnifier.png'
+                       id: 'plantlistCheckPlantSyntax'
+                       ,text: 'Check Syntax'
+                       ,icon: 'img/icons/accept.png'
                        ,cls: 'x-btn-text-icon'
                        ,disabled: true
-                       ,handler: editor.Test.createDelegate(editor, ['plantlistgrid'], true)
+                       ,handler: editor.CheckSyntax.createDelegate(editor, ['plantlistgrid'], true)
+               },{
+                       id: 'plantlistActivatePlant'
+                       ,text: 'Activate'
+                       ,icon: 'img/icons/accept.png'
+                       ,cls: 'x-btn-text-icon'
+                       ,disabled: true
+                       ,handler: function(){
+                               Ext.getCmp("plantlistgridEditor").stopEditing();
+                               var grid = Ext.getCmp("plantlistgrid");
+                               var selected = grid.getSelectionModel().getSelections();
+                               if(selected.length > 0) {
+                                   editor.Activate(selected[0]);
+                               }
+                       }
                },{
                        id: 'plantlistRefresh'
                        ,text: 'Refresh'
                                ,width: 200
                                ,sortable: true
                                ,dataIndex: 'Name'
-                               ,editor: {
-                                       xtype: 'textfield'
-                                       ,allowBlank: false
-                               }
                        },{
                                id: 'Code'
                                ,header: 'Code'
                                        ,allowBlank: true
                                }
                        },{
-                               id: 'Active'
-                               ,header: 'Active'
+                               id: 'IsActive'
+                               ,header: 'IsActive'
                                ,width: 200
                                ,sortable: true
-                               ,dataIndex: 'Active'
-                               ,editor: {
-                                       xtype: 'checkbox'
-                               }
+                               ,dataIndex: 'IsActive'
                        }
                ]
                ,sm: new Ext.grid.RowSelectionModel({
                        ,listeners: {
                                rowselect: function() {
                                        Ext.getCmp("plantlistDeletePlant").enable();
-                                       Ext.getCmp("plantlistTestPlant").enable();
+                                       Ext.getCmp("plantlistCheckPlantSyntax").enable();
+                                       Ext.getCmp("plantlistActivatePlant").enable();
                                }
                                ,rowdeselect: function() {
                                        Ext.getCmp("plantlistDeletePlant").disable();
-                                       Ext.getCmp("plantlistTestPlant").disable();
+                                       Ext.getCmp("plantlistCheckPlantSyntax").disable();
+                                       Ext.getCmp("plantlistActivatePlant").disable();
+                                       Ext.getCmp("plantlistActivatePlant").disable();
                                }
                        }
                })
        ,listeners: {
                show: communication.GetPlantList.createDelegate(communication)
        }
-}
\ No newline at end of file
+}
index 5edf43b..9749a87 100644 (file)
                        
                        $res = $this->m_Controller->HandleRemoteProcedureCall("Register", "test", "test", "", "");
                        $res = $this->m_Controller->HandleRemoteProcedureCall("Auth", "test", "test", "", "");
-                       $res = $this->m_Controller->HandleRemoteProcedureCall("CreatePlant", "", "", "newplant", "RULE GROW BY 1");
+                       $res = $this->m_Controller->HandleRemoteProcedureCall("CreatePlant", "", "", "newplant", "RULE a GROW BY 1");
+                       
+                       $GLOBALS['ValidatorFile'] = '../../cgi/validate';
                        $res = $this->m_Controller->HandleRemoteProcedureCall("ValidatePlant", "test", "", "newplant", "");
-                       $this->validateRPCResponse($res, "ValidatePlant", "{ success: true, msg: '' }");
+                       $this->validateRPCResponse($res, "ValidatePlant", "{\"valid\":true}");
                        
                }
        }
index 00ac4cd..03a245c 100644 (file)
                        $plant = $this->m_Database->GetPlant($user->ID, $data_name);
                        $this->assertTrue($plant == null);
                }
+               
+               function testNextSeed() {
+                       $username = "testuser";
+                       $data_name = "myplant";
+                       $data_code = "my fancy plant code!";
+                       
+                       $this->m_Database->CreateUser($username, "test");
+                       $user = $this->m_Database->GetUser($username);
+                       
+                       $this->m_Database->InsertNewPlant($user->ID, $data_name, $data_code);
+                       $plant = $this->m_Database->GetPlant($user->ID, $data_name);
+                       
+                       $result = $this->m_Database->SetUsersNextSeed($user->ID, $plant->ID);
+                       $this->assertTrue($result);
+                       
+                       $user = $this->m_Database->GetUserByID($user->ID);
+                       $this->assertTrue($user != null);
+                       $this->assertTrue($user->NextSeedID == $plant->ID);
+                       
+                       $plant = $this->m_Database->GetPlantByID($user->ID, $plant->ID);
+                       $this->assertTrue($plant != null);
+                       $this->assertTrue($plant->IsActive);
+               }
        }
 
 ?>