2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Saved searches managing
9 if (! defined('PHPMYADMIN')) {
14 * Saved searches managing
18 class PMA_SavedSearches
21 * Global configuration
24 private $_config = null;
36 private $_username = null;
42 private $_dbname = null;
48 private $_searchName = null;
53 * @param int|null $searchId Id of search
57 public function setId($searchId)
59 $searchId = (int)$searchId;
60 if (empty($searchId)) {
64 $this->_id
= $searchId;
73 public function getId()
79 * Setter of searchName
81 * @param string $searchName Saved search name
85 public function setSearchName($searchName)
87 $this->_searchName
= $searchName;
92 * Getter of searchName
96 public function getSearchName()
98 return $this->_searchName
;
105 private $_criterias = null;
110 * @param array $config Global configuration
114 public function setConfig($config)
116 $this->_config
= $config;
125 public function getConfig()
127 return $this->_config
;
131 * Setter for criterias
133 * @param array|string $criterias Criterias of saved searches
134 * @param bool $json Criterias are in JSON format
138 public function setCriterias($criterias, $json = false)
140 if (true === $json && is_string($criterias)) {
141 $this->_criterias
= json_decode($criterias, true);
145 $aListFieldsToGet = array(
151 'criteriaAndOrColumn',
157 $data['criteriaColumnCount'] = count($criterias['criteriaColumn']);
159 foreach ($aListFieldsToGet as $field) {
160 $data[$field] = $criterias[$field];
163 for ($i = 0; $i <= $data['rows']; $i++
) {
164 $data['Or' . $i] = $criterias['Or' . $i];
167 $this->_criterias
= $data;
172 * Getter for criterias
176 public function getCriterias()
178 return $this->_criterias
;
182 * Setter for username
184 * @param string $username Username
188 public function setUsername($username)
190 $this->_username
= $username;
195 * Getter for username
199 public function getUsername()
201 return $this->_username
;
207 * @param string $dbname DB name
211 public function setDbname($dbname)
213 $this->_dbname
= $dbname;
222 public function getDbname()
224 return $this->_dbname
;
230 * @param array $config Global configuration
232 public function __construct($config)
234 $this->setConfig($config);
242 public function save()
244 if (null == $this->getSearchName()) {
245 $message = PMA_Message
::error(
246 __('Please provide a name for this bookmarked search.')
248 $response = PMA_Response
::getInstance();
249 $response->isSuccess($message->isSuccess());
250 $response->addJSON('fieldWithError', 'searchName');
251 $response->addJSON('message', $message);
255 if (null == $this->getUsername()
256 ||
null == $this->getDbname()
257 ||
null == $this->getSearchName()
258 ||
null == $this->getCriterias()
260 $message = PMA_Message
::error(
261 __('Missing information to save the bookmarked search.')
263 $response = PMA_Response
::getInstance();
264 $response->isSuccess($message->isSuccess());
265 $response->addJSON('message', $message);
270 = PMA_Util
::backquote($this->_config
['cfgRelation']['db']) . "."
271 . PMA_Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
274 if (null === $this->getId()) {
276 "search_name = '" . PMA_Util
::sqlAddSlashes($this->getSearchName())
279 $existingSearches = $this->getList($wheres);
281 if (!empty($existingSearches)) {
282 $message = PMA_Message
::error(
283 __('An entry with this name already exists.')
285 $response = PMA_Response
::getInstance();
286 $response->isSuccess($message->isSuccess());
287 $response->addJSON('fieldWithError', 'searchName');
288 $response->addJSON('message', $message);
292 $sqlQuery = "INSERT INTO " . $savedSearchesTbl
293 . "(`username`, `db_name`, `search_name`, `search_data`)"
295 . "'" . PMA_Util
::sqlAddSlashes($this->getUsername()) . "',"
296 . "'" . PMA_Util
::sqlAddSlashes($this->getDbname()) . "',"
297 . "'" . PMA_Util
::sqlAddSlashes($this->getSearchName()) . "',"
298 . "'" . PMA_Util
::sqlAddSlashes(json_encode($this->getCriterias()))
301 $result = (bool)PMA_queryAsControlUser($sqlQuery);
306 $this->setId($GLOBALS['dbi']->insertId());
311 //Else, it's an update.
313 "id != " . $this->getId(),
314 "search_name = '" . PMA_Util
::sqlAddSlashes($this->getSearchName()) . "'"
316 $existingSearches = $this->getList($wheres);
318 if (!empty($existingSearches)) {
319 $message = PMA_Message
::error(
320 __('An entry with this name already exists.')
322 $response = PMA_Response
::getInstance();
323 $response->isSuccess($message->isSuccess());
324 $response->addJSON('fieldWithError', 'searchName');
325 $response->addJSON('message', $message);
329 $sqlQuery = "UPDATE " . $savedSearchesTbl
330 . "SET `search_name` = '"
331 . PMA_Util
::sqlAddSlashes($this->getSearchName()) . "', "
332 . "`search_data` = '"
333 . PMA_Util
::sqlAddSlashes(json_encode($this->getCriterias())) . "' "
334 . "WHERE id = " . $this->getId();
335 return (bool)PMA_queryAsControlUser($sqlQuery);
343 public function delete()
345 if (null == $this->getId()) {
346 $message = PMA_Message
::error(
347 __('Missing information to delete the search.')
349 $response = PMA_Response
::getInstance();
350 $response->isSuccess($message->isSuccess());
351 $response->addJSON('fieldWithError', 'searchId');
352 $response->addJSON('message', $message);
357 = PMA_Util
::backquote($this->_config
['cfgRelation']['db']) . "."
358 . PMA_Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
360 $sqlQuery = "DELETE FROM " . $savedSearchesTbl
361 . "WHERE id = '" . PMA_Util
::sqlAddSlashes($this->getId()) . "'";
363 return (bool)PMA_queryAsControlUser($sqlQuery);
367 * Load the current search from an id.
369 * @return bool Success
371 public function load()
373 if (null == $this->getId()) {
374 $message = PMA_Message
::error(
375 __('Missing information to load the search.')
377 $response = PMA_Response
::getInstance();
378 $response->isSuccess($message->isSuccess());
379 $response->addJSON('fieldWithError', 'searchId');
380 $response->addJSON('message', $message);
384 $savedSearchesTbl = PMA_Util
::backquote($this->_config
['cfgRelation']['db'])
386 . PMA_Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
387 $sqlQuery = "SELECT id, search_name, search_data "
388 . "FROM " . $savedSearchesTbl . " "
389 . "WHERE id = '" . PMA_Util
::sqlAddSlashes($this->getId()) . "' ";
391 $resList = PMA_queryAsControlUser($sqlQuery);
393 if (false === ($oneResult = $GLOBALS['dbi']->fetchArray($resList))) {
394 $message = PMA_Message
::error(__('Error while loading the search.'));
395 $response = PMA_Response
::getInstance();
396 $response->isSuccess($message->isSuccess());
397 $response->addJSON('fieldWithError', 'searchId');
398 $response->addJSON('message', $message);
402 $this->setSearchName($oneResult['search_name'])
403 ->setCriterias($oneResult['search_data'], true);
409 * Get the list of saved search of a user on a DB
411 * @param string[] $wheres List of filters
413 * @return array|bool List of saved search or false on failure
415 public function getList(array $wheres = array())
417 if (null == $this->getUsername()
418 ||
null == $this->getDbname()
423 $savedSearchesTbl = PMA_Util
::backquote($this->_config
['cfgRelation']['db'])
425 . PMA_Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
426 $sqlQuery = "SELECT id, search_name "
427 . "FROM " . $savedSearchesTbl . " "
429 . "username = '" . PMA_Util
::sqlAddSlashes($this->getUsername()) . "' "
430 . "AND db_name = '" . PMA_Util
::sqlAddSlashes($this->getDbname()) . "' ";
432 foreach ($wheres as $where) {
433 $sqlQuery .= "AND " . $where . " ";
436 $sqlQuery .= "order by search_name ASC ";
438 $resList = PMA_queryAsControlUser($sqlQuery);
441 while ($oneResult = $GLOBALS['dbi']->fetchArray($resList)) {
442 $list[$oneResult['id']] = $oneResult['search_name'];