Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh (connector): update SQL queries of centreon connector form (#3847) #3969

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
118 changes: 62 additions & 56 deletions centreon/www/class/centreonConnector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,79 +244,85 @@ public function read($id)
/**
* Updates connector
*
* @param int $id
* @param int $connectorId
* @param array $connector
*
* @throws InvalidArgumentException
* @throws RuntimeException
*
* @return CentreonConnector
*/
public function update($id, $connector = array())
public function update(int $connectorId, array $connector = array()): self
{
if (!is_array($connector)) {
throw new InvalidArgumentException('Data is not an array');
if ($connector === []) {
return $this;
}

if (!is_numeric($id)) {
throw new InvalidArgumentException('Id is not integer');
}
try {
$this->dbConnection->beginTransaction();
$bindValues = [];
$subRequest = '';

if (count($connector) === 0) {
return $this;
}
if (isset($connector['name'])) {
$bindValues[':name'] = $connector['name'];
$subRequest = ', `name` = :name';
}

$data = array();
if (isset($connector['description'])) {
$bindValues[':description'] = $connector['description'];
$subRequest .= ', `description` = :description';
}

if (isset($connector['name'])) {
$data['name'] = $connector['name'];
}
if (isset($connector['description'])) {
$data['description'] = $connector['description'];
}
if (isset($connector['command_line'])) {
$data['command_line'] = $connector['command_line'];
}
if (isset($connector['enabled'])) {
$data['enabled'] = $connector['enabled'];
}
if (count($data) !== 0) {
$sqlParts = array();
$values = array();
$sqlParts[] = '`modified` = ?';
$values[] = time();
foreach ($data as $fieldName => $fieldValue) {
$sqlParts[] = "`$fieldName` = ?";
$values[] = $fieldValue;
if (isset($connector['command_line'])) {
$bindValues[':command_line'] = $connector['command_line'];
$subRequest .= ', `command_line` = :command_line';
}
$sqlParts = implode(', ', $sqlParts);
$values[] = $id;
try {
$updateResult = $this->dbConnection->prepare(
"UPDATE `connector` SET $sqlParts WHERE `connector`.`id` = ? LIMIT 1"

if (isset($connector['enabled'])) {
$bindValues[':enabled'] = $connector['enabled'];
$subRequest .= ', `enabled` = :enabled';
}

if ($bindValues !== []) {
$bindValues[':date_now'] = time();
$subRequest = '`modified` = :date_now' . $subRequest;

$statement = $this->dbConnection->prepare(
<<<SQL
UPDATE `connector`
SET $subRequest
WHERE `connector`.`id` = :id
LIMIT 1
SQL
);
$updateResult->execute($values);
} catch (\PDOException $e) {
throw new RuntimeException('Cannot update connector');
$statement->bindValue(':id', (int) $connectorId, \PDO::PARAM_INT);
foreach ($bindValues as $fieldName => $fieldValue) {
$statement->bindValue($fieldName, $fieldValue);
}
$statement->execute();
}
}

try {
$updateResult = $this->dbConnection->query(
"UPDATE `command` SET connector_id = NULL WHERE `connector_id` = $id"
$statement = $this->dbConnection->prepare(
"UPDATE `command` SET connector_id = NULL WHERE `connector_id` = :id"
);
} catch (\PDOException $e) {
throw new RuntimeException('Cannot update connector');
}
$statement->bindValue(':id', (int) $connectorId, \PDO::PARAM_INT);
$statement->execute();

if (isset($connector["command_id"])) {
foreach ($connector["command_id"] as $key => $value) {
try {
$updateResult = $this->dbConnection->query(
"UPDATE `command` SET connector_id = '$id' WHERE `command_id` = '$value'"
);
} catch (\PDOException $e) {
throw new RuntimeException('Cannot update connector');
}
$commandIds = $connector["command_id"] ?? [];
foreach ($commandIds as $commandId) {
$statement = $this->dbConnection->prepare(
"UPDATE `command` SET `connector_id` = :id WHERE `command_id` = :command_id"
);
$statement->bindValue(':id', (int) $connectorId, \PDO::PARAM_INT);
$statement->bindValue(':command_id', (int) $commandId, \PDO::PARAM_INT);
$statement->execute();
}
$this->dbConnection->commit();
} catch (\Throwable) {
$this->dbConnection->rollBack();
throw new RuntimeException('Cannot update connector');
}


return $this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
if ($lvl_access == "w") {
$myConnector = $connectorObj->read($connector_id);
$myConnector['enabled'] = '1';
$connectorObj->update($connector_id, $myConnector);
$connectorObj->update((int) $connector_id, $myConnector);
}
} else {
unvalidFormMessage();
Expand All @@ -94,7 +94,7 @@
if ($lvl_access == "w") {
$myConnector = $connectorObj->read($connector_id);
$myConnector['enabled'] = '0';
$connectorObj->update($connector_id, $myConnector);
$connectorObj->update((int) $connector_id, $myConnector);
}
} else {
unvalidFormMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@
$connectorValues['enabled'] = $tab['connector_status']['connector_status'] === '0' ? 0 : 1;
$connectorValues['command_id'] = isset($tab['command_id']) ? $tab['command_id'] : null;
$connectorValues['command_line'] = $tab['command_line'];
$connectorId = (int)$tab['connector_id'];
$connectorId = (int) $tab['connector_id'];

if (!empty($connectorValues['name'])) {
if ($form->getSubmitValue("submitA")) {
$connectorId = $cntObj->create($connectorValues, true);
} elseif ($form->getSubmitValue("submitC")) {
$cntObj->update((int)$connectorId, $connectorValues);
$cntObj->update($connectorId, $connectorValues);
}
$valid = true;
}
Expand Down