public function save()
{
if (null == $this->getSearchName()) {
$message = Message::error(__('Please provide a name for this bookmarked search.'));
$response = Response::getInstance();
$response->setRequestStatus($message->isSuccess());
$response->addJSON('fieldWithError', 'searchName');
$response->addJSON('message', $message);
exit;
}
if (null == $this->getUsername() || null == $this->getDbname() || null == $this->getSearchName() || null == $this->getCriterias()) {
$message = Message::error(__('Missing information to save the bookmarked search.'));
$response = Response::getInstance();
$response->setRequestStatus($message->isSuccess());
$response->addJSON('message', $message);
exit;
}
$savedSearchesTbl = Util::backquote($this->_config['cfgRelation']['db']) . "." . Util::backquote($this->_config['cfgRelation']['savedsearches']);
//If it's an insert.
if (null === $this->getId()) {
$wheres = array("search_name = '" . $GLOBALS['dbi']->escapeString($this->getSearchName()) . "'");
$existingSearches = $this->getList($wheres);
if (!empty($existingSearches)) {
$message = Message::error(__('An entry with this name already exists.'));
$response = Response::getInstance();
$response->setRequestStatus($message->isSuccess());
$response->addJSON('fieldWithError', 'searchName');
$response->addJSON('message', $message);
exit;
}
$sqlQuery = "INSERT INTO " . $savedSearchesTbl . "(`username`, `db_name`, `search_name`, `search_data`)" . " VALUES (" . "'" . $GLOBALS['dbi']->escapeString($this->getUsername()) . "'," . "'" . $GLOBALS['dbi']->escapeString($this->getDbname()) . "'," . "'" . $GLOBALS['dbi']->escapeString($this->getSearchName()) . "'," . "'" . $GLOBALS['dbi']->escapeString(json_encode($this->getCriterias())) . "')";
$result = (bool) PMA_queryAsControlUser($sqlQuery);
if (!$result) {
return false;
}
$this->setId($GLOBALS['dbi']->insertId());
return true;
}
//Else, it's an update.
$wheres = array("id != " . $this->getId(), "search_name = '" . $GLOBALS['dbi']->escapeString($this->getSearchName()) . "'");
$existingSearches = $this->getList($wheres);
if (!empty($existingSearches)) {
$message = Message::error(__('An entry with this name already exists.'));
$response = Response::getInstance();
$response->setRequestStatus($message->isSuccess());
$response->addJSON('fieldWithError', 'searchName');
$response->addJSON('message', $message);
exit;
}
$sqlQuery = "UPDATE " . $savedSearchesTbl . "SET `search_name` = '" . $GLOBALS['dbi']->escapeString($this->getSearchName()) . "', " . "`search_data` = '" . $GLOBALS['dbi']->escapeString(json_encode($this->getCriterias())) . "' " . "WHERE id = " . $this->getId();
return (bool) PMA_queryAsControlUser($sqlQuery);
}