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',
158 $data['criteriaColumnCount'] = count($criterias['criteriaColumn']);
160 foreach ($aListFieldsToGet as $field) {
161 $data[$field] = $criterias[$field];
164 for ($i = 0; $i <= $data['rows']; $i++
) {
165 $data['Or' . $i] = $criterias['Or' . $i];
168 $this->_criterias
= $data;
173 * Getter for criterias
177 public function getCriterias()
179 return $this->_criterias
;
183 * Setter for username
185 * @param string $username Username
189 public function setUsername($username)
191 $this->_username
= $username;
196 * Getter for username
200 public function getUsername()
202 return $this->_username
;
208 * @param string $dbname DB name
212 public function setDbname($dbname)
214 $this->_dbname
= $dbname;
223 public function getDbname()
225 return $this->_dbname
;
231 * @param array $config Global configuration
233 public function __construct($config)
235 $this->setConfig($config);
243 public function save()
245 if (null == $this->getSearchName()) {
246 $message = PMA_Message
::error(
247 __('Please provide a name for this bookmarked search.')
249 $response = PMA_Response
::getInstance();
250 $response->isSuccess($message->isSuccess());
251 $response->addJSON('fieldWithError', 'searchName');
252 $response->addJSON('message', $message);
256 if (null == $this->getUsername()
257 ||
null == $this->getDbname()
258 ||
null == $this->getSearchName()
259 ||
null == $this->getCriterias()
261 $message = PMA_Message
::error(
262 __('Missing information to save the bookmarked search.')
264 $response = PMA_Response
::getInstance();
265 $response->isSuccess($message->isSuccess());
266 $response->addJSON('message', $message);
271 = PMA_Util
::backquote($this->_config
['cfgRelation']['db']) . "."
272 . PMA_Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
275 if (null === $this->getId()) {
277 "search_name = '" . PMA_Util
::sqlAddSlashes($this->getSearchName())
280 $existingSearches = $this->getList($wheres);
282 if (!empty($existingSearches)) {
283 $message = PMA_Message
::error(
284 __('An entry with this name already exists.')
286 $response = PMA_Response
::getInstance();
287 $response->isSuccess($message->isSuccess());
288 $response->addJSON('fieldWithError', 'searchName');
289 $response->addJSON('message', $message);
293 $sqlQuery = "INSERT INTO " . $savedSearchesTbl
294 . "(`username`, `db_name`, `search_name`, `search_data`)"
296 . "'" . PMA_Util
::sqlAddSlashes($this->getUsername()) . "',"
297 . "'" . PMA_Util
::sqlAddSlashes($this->getDbname()) . "',"
298 . "'" . PMA_Util
::sqlAddSlashes($this->getSearchName()) . "',"
299 . "'" . PMA_Util
::sqlAddSlashes(json_encode($this->getCriterias()))
302 $result = (bool)PMA_queryAsControlUser($sqlQuery);
307 $this->setId($GLOBALS['dbi']->insertId());
312 //Else, it's an update.
314 "id != " . $this->getId(),
315 "search_name = '" . PMA_Util
::sqlAddSlashes($this->getSearchName()) . "'"
317 $existingSearches = $this->getList($wheres);
319 if (!empty($existingSearches)) {
320 $message = PMA_Message
::error(
321 __('An entry with this name already exists.')
323 $response = PMA_Response
::getInstance();
324 $response->isSuccess($message->isSuccess());
325 $response->addJSON('fieldWithError', 'searchName');
326 $response->addJSON('message', $message);
330 $sqlQuery = "UPDATE " . $savedSearchesTbl
331 . "SET `search_name` = '"
332 . PMA_Util
::sqlAddSlashes($this->getSearchName()) . "', "
333 . "`search_data` = '"
334 . PMA_Util
::sqlAddSlashes(json_encode($this->getCriterias())) . "' "
335 . "WHERE id = " . $this->getId();
336 return (bool)PMA_queryAsControlUser($sqlQuery);
344 public function delete()
346 if (null == $this->getId()) {
347 $message = PMA_Message
::error(
348 __('Missing information to delete the search.')
350 $response = PMA_Response
::getInstance();
351 $response->isSuccess($message->isSuccess());
352 $response->addJSON('fieldWithError', 'searchId');
353 $response->addJSON('message', $message);
358 = PMA_Util
::backquote($this->_config
['cfgRelation']['db']) . "."
359 . PMA_Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
361 $sqlQuery = "DELETE FROM " . $savedSearchesTbl
362 . "WHERE id = '" . PMA_Util
::sqlAddSlashes($this->getId()) . "'";
364 return (bool)PMA_queryAsControlUser($sqlQuery);
368 * Load the current search from an id.
370 * @return bool Success
372 public function load()
374 if (null == $this->getId()) {
375 $message = PMA_Message
::error(
376 __('Missing information to load the search.')
378 $response = PMA_Response
::getInstance();
379 $response->isSuccess($message->isSuccess());
380 $response->addJSON('fieldWithError', 'searchId');
381 $response->addJSON('message', $message);
385 $savedSearchesTbl = PMA_Util
::backquote($this->_config
['cfgRelation']['db'])
387 . PMA_Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
388 $sqlQuery = "SELECT id, search_name, search_data "
389 . "FROM " . $savedSearchesTbl . " "
390 . "WHERE id = '" . PMA_Util
::sqlAddSlashes($this->getId()) . "' ";
392 $resList = PMA_queryAsControlUser($sqlQuery);
394 if (false === ($oneResult = $GLOBALS['dbi']->fetchArray($resList))) {
395 $message = PMA_Message
::error(__('Error while loading the search.'));
396 $response = PMA_Response
::getInstance();
397 $response->isSuccess($message->isSuccess());
398 $response->addJSON('fieldWithError', 'searchId');
399 $response->addJSON('message', $message);
403 $this->setSearchName($oneResult['search_name'])
404 ->setCriterias($oneResult['search_data'], true);
410 * Get the list of saved search of a user on a DB
412 * @param string[] $wheres List of filters
414 * @return array|bool List of saved search or false on failure
416 public function getList(array $wheres = array())
418 if (null == $this->getUsername()
419 ||
null == $this->getDbname()
424 $savedSearchesTbl = PMA_Util
::backquote($this->_config
['cfgRelation']['db'])
426 . PMA_Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
427 $sqlQuery = "SELECT id, search_name "
428 . "FROM " . $savedSearchesTbl . " "
430 . "username = '" . PMA_Util
::sqlAddSlashes($this->getUsername()) . "' "
431 . "AND db_name = '" . PMA_Util
::sqlAddSlashes($this->getDbname()) . "' ";
433 foreach ($wheres as $where) {
434 $sqlQuery .= "AND " . $where . " ";
437 $sqlQuery .= "order by search_name ASC ";
439 $resList = PMA_queryAsControlUser($sqlQuery);
442 while ($oneResult = $GLOBALS['dbi']->fetchArray($resList)) {
443 $list[$oneResult['id']] = $oneResult['search_name'];