/** * deletes a request with a requestID on a users DB. * * @param $userdb * @param $requestId * @return bool */ public function deleteRequest($userdb, $requestId) { $username = RequestHelper::readUsernameFromRequest(); //control variable $all_query_ok = true; //disable autocommit so commit/rollback is possible $this->core->dbh->autocommit(false); // select all intervention IDs with the specified RequestID as an array if ($stmt = $this->core->dbh->query("SELECT IntervID FROM intervention WHERE Request_id = '" . $requestId . "'")) { $row = $stmt->fetch_row(); $stmt->close(); } else { $all_query_ok = false; } if (!$all_query_ok) { LogHelper::write("select intervid failed", $username); } //skip deleting intervention work and material if no interventions are in the DB //delete from intervention_material and intervention_work if (!empty($row)) { $oIntervention = new Intervention($userdb); foreach ($row as $intervID) { $all_query_ok &= $oIntervention->deleteInterventionMaterial($intervID); $all_query_ok &= $oIntervention->deleteInterventionWork($intervID); } } //delete from interventions $this->core->dbh->query("DELETE FROM intervention WHERE Request_id = '" . $requestId . "'") ? false : ($all_query_ok = false); if (!$all_query_ok) { LogHelper::write("delete intervention failed", $username); } //delete from requests $this->core->dbh->query("DELETE FROM request WHERE Request_id = '" . $requestId . "'") ? false : ($all_query_ok = false); if (!$all_query_ok) { LogHelper::write("delete request failed", $username); } // commit or rollback if ($all_query_ok) { $this->core->dbh->commit(); $this->core->dbh->close(); return TRUE; } else { $this->core->dbh->rollback(); $this->core->dbh->close(); return FALSE; } }
//required parameters not empty or 'null' $toVerify = array('IntervID', 'Action', 'Date_action', 'Time'); R::verifyRequiredParams($toVerify); $app->add(new \Slim\Middleware\ContentTypes()); $body = $app->request()->getBody(); $request = json_decode($body, true); $response = array(); $intervID = $request['IntervID']; $action = $request['Action']; $date_action = $request['Date_action']; $time = $request['Time']; //read user db and name from request $userdb = R::readDbFromRequest(); $by_user = R::readUsernameFromRequest(); //add work $oIntervention = new Intervention($userdb); $workId = $oIntervention->addWork($intervID, $action, $date_action, $time, $by_user); //read old total work $oldTotalWork = $oIntervention->getTotalWork($intervID); //update new total work as hours (not as minutes) $newTotalWork = $oldTotalWork + $time / 60; $InUpdated = $oIntervention->updateInterventionWork($intervID, $newTotalWork, $by_user); if ($workId != NULL & $InUpdated) { $response["error"] = false; $response["message"] = "Intervention work created"; Log::write("Intervention work with ID " . $workId . " created successfully.", $by_user); R::echoResponse(201, $response); } else { $response["error"] = true; $response["message"] = "Could not create intervention work"; Log::write("Failed to create new intervention work.", $by_user);
/** * Deletes an asset and its belonging requests, interventions and intervention material and work * * @param $userdb * @param $assetId * @return bool */ public function deleteAsset($userdb, $assetId) { //prevent sql injection $assetId = mysqli_real_escape_string($this->core->dbh, $assetId); //control variable $all_query_ok = true; //disable autocommit so commit/rollback is possible $this->core->dbh->autocommit(false); // select all intervention IDs with the specified AssetID as an array if ($stmt = $this->core->dbh->query("SELECT IntervID FROM intervention WHERE AssetID_Visit = '" . $assetId . "'")) { $row = $stmt->fetch_row(); $stmt->close(); } else { $all_query_ok = false; } //skip deleting intervention work and material if no interventions are in the DB //delete from intervention_material and intervention_work if (!empty($row)) { $oIntervention = new Intervention($userdb); foreach ($row as $intervID) { $all_query_ok &= $oIntervention->deleteInterventionMaterial($intervID); $all_query_ok &= $oIntervention->deleteInterventionWork($intervID); } } //delete from interventions $this->core->dbh->query("DELETE FROM intervention WHERE AssetID_Visit = '" . $assetId . "'") ? false : ($all_query_ok = false); //delete from requests $this->core->dbh->query("DELETE FROM request WHERE AssetID = '" . $assetId . "'") ? false : ($all_query_ok = false); //delete from assets $this->core->dbh->query("DELETE FROM assets WHERE AssetID = '" . $assetId . "'") ? false : ($all_query_ok = false); // commit or rollback if ($all_query_ok) { $this->core->dbh->commit(); $this->core->dbh->close(); return TRUE; } else { $this->core->dbh->rollback(); $this->core->dbh->close(); return FALSE; } }