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 if (isset($criterias[$field])) {
159 $data[$field] = $criterias[$field];
163 /* Limit amount of rows */
164 if (!isset($data['rows'])) {
168 max(0, intval($data['rows'])),
173 for ($i = 0; $i <= $data['rows']; $i++
) {
174 $data['Or' . $i] = $criterias['Or' . $i];
177 $this->_criterias
= $data;
182 * Getter for criterias
186 public function getCriterias()
188 return $this->_criterias
;
192 * Setter for username
194 * @param string $username Username
198 public function setUsername($username)
200 $this->_username
= $username;
205 * Getter for username
209 public function getUsername()
211 return $this->_username
;
217 * @param string $dbname DB name
221 public function setDbname($dbname)
223 $this->_dbname
= $dbname;
232 public function getDbname()
234 return $this->_dbname
;
240 * @param array $config Global configuration
242 public function __construct($config)
244 $this->setConfig($config);
252 public function save()
254 if (null == $this->getSearchName()) {
255 $message = Message
::error(
256 __('Please provide a name for this bookmarked search.')
258 $response = Response
::getInstance();
259 $response->setRequestStatus($message->isSuccess());
260 $response->addJSON('fieldWithError', 'searchName');
261 $response->addJSON('message', $message);
265 if (null == $this->getUsername()
266 ||
null == $this->getDbname()
267 ||
null == $this->getSearchName()
268 ||
null == $this->getCriterias()
270 $message = Message
::error(
271 __('Missing information to save the bookmarked search.')
273 $response = Response
::getInstance();
274 $response->setRequestStatus($message->isSuccess());
275 $response->addJSON('message', $message);
280 = Util
::backquote($this->_config
['cfgRelation']['db']) . "."
281 . Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
284 if (null === $this->getId()) {
286 "search_name = '" . $GLOBALS['dbi']->escapeString($this->getSearchName())
289 $existingSearches = $this->getList($wheres);
291 if (!empty($existingSearches)) {
292 $message = Message
::error(
293 __('An entry with this name already exists.')
295 $response = Response
::getInstance();
296 $response->setRequestStatus($message->isSuccess());
297 $response->addJSON('fieldWithError', 'searchName');
298 $response->addJSON('message', $message);
302 $sqlQuery = "INSERT INTO " . $savedSearchesTbl
303 . "(`username`, `db_name`, `search_name`, `search_data`)"
305 . "'" . $GLOBALS['dbi']->escapeString($this->getUsername()) . "',"
306 . "'" . $GLOBALS['dbi']->escapeString($this->getDbname()) . "',"
307 . "'" . $GLOBALS['dbi']->escapeString($this->getSearchName()) . "',"
308 . "'" . $GLOBALS['dbi']->escapeString(json_encode($this->getCriterias()))
311 $result = (bool)PMA_queryAsControlUser($sqlQuery);
316 $this->setId($GLOBALS['dbi']->insertId());
321 //Else, it's an update.
323 "id != " . $this->getId(),
324 "search_name = '" . $GLOBALS['dbi']->escapeString($this->getSearchName()) . "'"
326 $existingSearches = $this->getList($wheres);
328 if (!empty($existingSearches)) {
329 $message = Message
::error(
330 __('An entry with this name already exists.')
332 $response = Response
::getInstance();
333 $response->setRequestStatus($message->isSuccess());
334 $response->addJSON('fieldWithError', 'searchName');
335 $response->addJSON('message', $message);
339 $sqlQuery = "UPDATE " . $savedSearchesTbl
340 . "SET `search_name` = '"
341 . $GLOBALS['dbi']->escapeString($this->getSearchName()) . "', "
342 . "`search_data` = '"
343 . $GLOBALS['dbi']->escapeString(json_encode($this->getCriterias())) . "' "
344 . "WHERE id = " . $this->getId();
345 return (bool)PMA_queryAsControlUser($sqlQuery);
353 public function delete()
355 if (null == $this->getId()) {
356 $message = Message
::error(
357 __('Missing information to delete the search.')
359 $response = Response
::getInstance();
360 $response->setRequestStatus($message->isSuccess());
361 $response->addJSON('fieldWithError', 'searchId');
362 $response->addJSON('message', $message);
367 = Util
::backquote($this->_config
['cfgRelation']['db']) . "."
368 . Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
370 $sqlQuery = "DELETE FROM " . $savedSearchesTbl
371 . "WHERE id = '" . $GLOBALS['dbi']->escapeString($this->getId()) . "'";
373 return (bool)PMA_queryAsControlUser($sqlQuery);
377 * Load the current search from an id.
379 * @return bool Success
381 public function load()
383 if (null == $this->getId()) {
384 $message = Message
::error(
385 __('Missing information to load the search.')
387 $response = Response
::getInstance();
388 $response->setRequestStatus($message->isSuccess());
389 $response->addJSON('fieldWithError', 'searchId');
390 $response->addJSON('message', $message);
394 $savedSearchesTbl = Util
::backquote($this->_config
['cfgRelation']['db'])
396 . Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
397 $sqlQuery = "SELECT id, search_name, search_data "
398 . "FROM " . $savedSearchesTbl . " "
399 . "WHERE id = '" . $GLOBALS['dbi']->escapeString($this->getId()) . "' ";
401 $resList = PMA_queryAsControlUser($sqlQuery);
403 if (false === ($oneResult = $GLOBALS['dbi']->fetchArray($resList))) {
404 $message = Message
::error(__('Error while loading the search.'));
405 $response = Response
::getInstance();
406 $response->setRequestStatus($message->isSuccess());
407 $response->addJSON('fieldWithError', 'searchId');
408 $response->addJSON('message', $message);
412 $this->setSearchName($oneResult['search_name'])
413 ->setCriterias($oneResult['search_data'], true);
419 * Get the list of saved searches of a user on a DB
421 * @param string[] $wheres List of filters
423 * @return array List of saved searches or empty array on failure
425 public function getList(array $wheres = array())
427 if (null == $this->getUsername()
428 ||
null == $this->getDbname()
433 $savedSearchesTbl = Util
::backquote($this->_config
['cfgRelation']['db'])
435 . Util
::backquote($this->_config
['cfgRelation']['savedsearches']);
436 $sqlQuery = "SELECT id, search_name "
437 . "FROM " . $savedSearchesTbl . " "
439 . "username = '" . $GLOBALS['dbi']->escapeString($this->getUsername()) . "' "
440 . "AND db_name = '" . $GLOBALS['dbi']->escapeString($this->getDbname()) . "' ";
442 foreach ($wheres as $where) {
443 $sqlQuery .= "AND " . $where . " ";
446 $sqlQuery .= "order by search_name ASC ";
448 $resList = PMA_queryAsControlUser($sqlQuery);
451 while ($oneResult = $GLOBALS['dbi']->fetchArray($resList)) {
452 $list[$oneResult['id']] = $oneResult['search_name'];