2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Saved searches managing
8 namespace PMA\libraries
;
11 * Saved searches managing
18 * Global configuration
21 private $_config = null;
33 private $_username = null;
39 private $_dbname = null;
45 private $_searchName = null;
50 * @param int|null $searchId Id of search
54 public function setId($searchId)
56 $searchId = (int)$searchId;
57 if (empty($searchId)) {
61 $this->_id
= $searchId;
70 public function getId()
76 * Setter of searchName
78 * @param string $searchName Saved search name
82 public function setSearchName($searchName)
84 $this->_searchName
= $searchName;
89 * Getter of searchName
93 public function getSearchName()
95 return $this->_searchName
;
102 private $_criterias = null;
107 * @param array $config Global configuration
111 public function setConfig($config)
113 $this->_config
= $config;
122 public function getConfig()
124 return $this->_config
;
128 * Setter for criterias
130 * @param array|string $criterias Criterias of saved searches
131 * @param bool $json Criterias are in JSON format
135 public function setCriterias($criterias, $json = false)
137 if (true === $json && is_string($criterias)) {
138 $this->_criterias
= json_decode($criterias, true);
142 $aListFieldsToGet = array(
148 'criteriaAndOrColumn',
155 $data['criteriaColumnCount'] = count($criterias['criteriaColumn']);
157 foreach ($aListFieldsToGet as $field) {
158 $data[$field] = $criterias[$field];
161 for ($i = 0; $i <= $data['rows']; $i++
) {
162 $data['Or' . $i] = $criterias['Or' . $i];
165 $this->_criterias
= $data;
170 * Getter for criterias
174 public function getCriterias()
176 return $this->_criterias
;
180 * Setter for username
182 * @param string $username Username
186 public function setUsername($username)
188 $this->_username
= $username;
193 * Getter for username
197 public function getUsername()
199 return $this->_username
;
205 * @param string $dbname DB name
209 public function setDbname($dbname)
211 $this->_dbname
= $dbname;
220 public function getDbname()
222 return $this->_dbname
;
228 * @param array $config Global configuration
230 public function __construct($config)
232 $this->setConfig($config);
240 public function save()
242 if (null == $this->getSearchName()) {
243 $message = Message
::error(
244 __('Please provide a name for this bookmarked search.')
246 $response = Response
::getInstance();
247 $response->setRequestStatus($message->isSuccess());
248 $response->addJSON('fieldWithError', 'searchName');
249 $response->addJSON('message', $message);
253 if (null == $this->getUsername()
254 ||
null == $this->getDbname()
255 ||
null == $this->getSearchName()
256 ||
null == $this->getCriterias()
258 $message = Message
::error(
259 __('Missing information to save the bookmarked search.')
261 $response = Response
::getInstance();
262 $response->setRequestStatus($message->isSuccess());
263 $response->addJSON('message', $message);
268 = Util
::backquote($this->_config
['cfgRelation']['db']) . "."
269 . Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
272 if (null === $this->getId()) {
274 "search_name = '" . Util
::sqlAddSlashes($this->getSearchName())
277 $existingSearches = $this->getList($wheres);
279 if (!empty($existingSearches)) {
280 $message = Message
::error(
281 __('An entry with this name already exists.')
283 $response = Response
::getInstance();
284 $response->setRequestStatus($message->isSuccess());
285 $response->addJSON('fieldWithError', 'searchName');
286 $response->addJSON('message', $message);
290 $sqlQuery = "INSERT INTO " . $savedSearchesTbl
291 . "(`username`, `db_name`, `search_name`, `search_data`)"
293 . "'" . Util
::sqlAddSlashes($this->getUsername()) . "',"
294 . "'" . Util
::sqlAddSlashes($this->getDbname()) . "',"
295 . "'" . Util
::sqlAddSlashes($this->getSearchName()) . "',"
296 . "'" . Util
::sqlAddSlashes(json_encode($this->getCriterias()))
299 $result = (bool)PMA_queryAsControlUser($sqlQuery);
304 $this->setId($GLOBALS['dbi']->insertId());
309 //Else, it's an update.
311 "id != " . $this->getId(),
312 "search_name = '" . Util
::sqlAddSlashes($this->getSearchName()) . "'"
314 $existingSearches = $this->getList($wheres);
316 if (!empty($existingSearches)) {
317 $message = Message
::error(
318 __('An entry with this name already exists.')
320 $response = Response
::getInstance();
321 $response->setRequestStatus($message->isSuccess());
322 $response->addJSON('fieldWithError', 'searchName');
323 $response->addJSON('message', $message);
327 $sqlQuery = "UPDATE " . $savedSearchesTbl
328 . "SET `search_name` = '"
329 . Util
::sqlAddSlashes($this->getSearchName()) . "', "
330 . "`search_data` = '"
331 . Util
::sqlAddSlashes(json_encode($this->getCriterias())) . "' "
332 . "WHERE id = " . $this->getId();
333 return (bool)PMA_queryAsControlUser($sqlQuery);
341 public function delete()
343 if (null == $this->getId()) {
344 $message = Message
::error(
345 __('Missing information to delete the search.')
347 $response = Response
::getInstance();
348 $response->setRequestStatus($message->isSuccess());
349 $response->addJSON('fieldWithError', 'searchId');
350 $response->addJSON('message', $message);
355 = Util
::backquote($this->_config
['cfgRelation']['db']) . "."
356 . Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
358 $sqlQuery = "DELETE FROM " . $savedSearchesTbl
359 . "WHERE id = '" . Util
::sqlAddSlashes($this->getId()) . "'";
361 return (bool)PMA_queryAsControlUser($sqlQuery);
365 * Load the current search from an id.
367 * @return bool Success
369 public function load()
371 if (null == $this->getId()) {
372 $message = Message
::error(
373 __('Missing information to load the search.')
375 $response = Response
::getInstance();
376 $response->setRequestStatus($message->isSuccess());
377 $response->addJSON('fieldWithError', 'searchId');
378 $response->addJSON('message', $message);
382 $savedSearchesTbl = Util
::backquote($this->_config
['cfgRelation']['db'])
384 . Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
385 $sqlQuery = "SELECT id, search_name, search_data "
386 . "FROM " . $savedSearchesTbl . " "
387 . "WHERE id = '" . Util
::sqlAddSlashes($this->getId()) . "' ";
389 $resList = PMA_queryAsControlUser($sqlQuery);
391 if (false === ($oneResult = $GLOBALS['dbi']->fetchArray($resList))) {
392 $message = Message
::error(__('Error while loading the search.'));
393 $response = Response
::getInstance();
394 $response->setRequestStatus($message->isSuccess());
395 $response->addJSON('fieldWithError', 'searchId');
396 $response->addJSON('message', $message);
400 $this->setSearchName($oneResult['search_name'])
401 ->setCriterias($oneResult['search_data'], true);
407 * Get the list of saved searches of a user on a DB
409 * @param string[] $wheres List of filters
411 * @return array List of saved searches or empty array on failure
413 public function getList(array $wheres = array())
415 if (null == $this->getUsername()
416 ||
null == $this->getDbname()
421 $savedSearchesTbl = Util
::backquote($this->_config
['cfgRelation']['db'])
423 . Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
424 $sqlQuery = "SELECT id, search_name "
425 . "FROM " . $savedSearchesTbl . " "
427 . "username = '" . Util
::sqlAddSlashes($this->getUsername()) . "' "
428 . "AND db_name = '" . Util
::sqlAddSlashes($this->getDbname()) . "' ";
430 foreach ($wheres as $where) {
431 $sqlQuery .= "AND " . $where . " ";
434 $sqlQuery .= "order by search_name ASC ";
436 $resList = PMA_queryAsControlUser($sqlQuery);
439 while ($oneResult = $GLOBALS['dbi']->fetchArray($resList)) {
440 $list[$oneResult['id']] = $oneResult['search_name'];