Bläddra i källkod

Merge pull request #6 from advation/deleteEntry

Changed the timesheetmodel to appropriately handle the different time…
Adam 9 år sedan
förälder
incheckning
d25b986213

+ 9 - 6
application/controllers/timesheetController.php

@@ -150,11 +150,12 @@ class timesheetController extends Staple_Controller
         if($id != null)
         {
             //Confirm entry for user
-            $timesheet = new timesheetModel();
-            if($timesheet->exists($id))
+            $timeEntry = new timeEntryModel($id);
+
+            if($timeEntry->getId() !== NULL)
             {
-                //Delete Item
-                if($timesheet->remove($id))
+                //Remove Entry
+                if($timeEntry->remove($timeEntry->getId()))
                 {
                     $this->view->message = "Entry removed.";
                 }
@@ -165,12 +166,12 @@ class timesheetController extends Staple_Controller
             }
             else
             {
-                header("location: ".$this->_link(array('timesheet'))."");
+                //header("location: ".$this->_link(array('timesheet'))."");
             }
         }
         else
         {
-            header("location: ".$this->_link(array('timesheet'))."");
+            //header("location: ".$this->_link(array('timesheet'))."");
         }
     }
 
@@ -186,6 +187,8 @@ class timesheetController extends Staple_Controller
             $data['lessTime'] = $entry->getLessTime();
             $data['code'] = $entry->getCodeId();
 
+            $this->view->id = $entry->getId();
+
             $form = new editTimeForm();
             $form->setAction($this->_link(array('timesheet','edit',$id)));
             $form->addData($data);

+ 32 - 8
application/models/timeEntryModel.php

@@ -329,11 +329,14 @@
                 $user = new userModel($auth->getAuthId());
                 $userId = $user->getId();
 
-                $sql = "DELETE FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."' AND userId = '".$this->db->real_escape_string($userId)."'";
-
-                if($this->db->query($sql))
+                //Check if validated
+                if($this->_validated($id))
                 {
-                    return true;
+                    $sql = "DELETE FROM timeEntries WHERE id = '".$this->db->real_escape_string($id)."' AND userId = '".$this->db->real_escape_string($userId)."'";
+                    if($this->db->query($sql))
+                    {
+                        return true;
+                    }
                 }
             }
         }
@@ -351,7 +354,7 @@
 
             if($this->id == NULL)
 			{
-                if($this->_overlap($inTime))
+                if($this->_overlap($inTime,$outTime))
                 {
                     //Insert new item
                     $sql = "INSERT INTO timeEntries (userId, inTime, outTime, lessTime, codeId, batchId)
@@ -373,7 +376,7 @@
 			}
 			else
 			{
-                if($this->_overlap($inTime,$this->getId()))
+                if($this->_overlap($inTime,$outTime,$this->getId()))
                 {
                     //Update item
                     $sql = "UPDATE timeEntries SET
@@ -421,7 +424,7 @@
             }
         }
 
-        function _overlap($inTime,$id = null)
+        function _overlap($inTime,$outTime,$id = null)
         {
             $this->db = Staple_DB::get();
 
@@ -429,7 +432,7 @@
             $user = new userModel($auth->getAuthId());
             $userId = $user->getId();
 
-            $sql = "SELECT id FROM timeEntries WHERE '".$this->db->real_escape_string($inTime)."' >= inTime AND '".$this->db->real_escape_string($inTime)."' < outTime AND id <> '".$this->db->real_escape_string($id)."' AND userId = '".$this->db->real_escape_string($userId)."'";
+            $sql = "SELECT id FROM timeEntries WHERE '".$this->db->real_escape_string($inTime)."' >= inTime AND '".$this->db->real_escape_string($outTime)."' <= outTime AND id <> '".$this->db->real_escape_string($id)."' AND userId = '".$this->db->real_escape_string($userId)."'";
 
             if($this->db->query($sql)->num_rows > 0)
             {
@@ -440,5 +443,26 @@
                 return true;
             }
         }
+
+        function _validated($id)
+        {
+            $auth = Staple_Auth::get();
+            $user = new userModel($auth->getAuthId());
+
+            $userId = $user->getId();
+            $batchId = $user->getBatchId();
+
+            $sql = "SELECT id FROM timeEntries WHERE userId = '".$this->db->real_escape_string($userId)."' AND batchId = '".$this->db->real_escape_string($batchId)."' AND id = '".$this->db->real_escape_string($id)."'";
+
+            if($this->db->query($sql)->num_rows > 0)
+            {
+                return true;
+            }
+            else
+            {
+                return false;
+            }
+
+        }
 	}
 ?>

+ 2 - 0
application/models/timesheetModel.php

@@ -534,9 +534,11 @@
 			{
 				//Key already in use
 				return false;
+				echo "this";
 			}
 			else
 			{
+				echo "that";
 				//Set new key in user account
 				$sql = "UPDATE accounts SET batchId='".$this->db->real_escape_string($key)."' WHERE id=$userId";