Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / libraries / SavedSearches.php
bloba67819538cd6af6bf779c9b90be7c2874d29da79
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Saved searches managing
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries;
10 /**
11 * Saved searches managing
13 * @package PhpMyAdmin
15 class SavedSearches
17 /**
18 * Global configuration
19 * @var array
21 private $_config = null;
23 /**
24 * Id
25 * @var int|null
27 private $_id = null;
29 /**
30 * Username
31 * @var string
33 private $_username = null;
35 /**
36 * DB name
37 * @var string
39 private $_dbname = null;
41 /**
42 * Saved search name
43 * @var string
45 private $_searchName = null;
47 /**
48 * Setter of id
50 * @param int|null $searchId Id of search
52 * @return static
54 public function setId($searchId)
56 $searchId = (int)$searchId;
57 if (empty($searchId)) {
58 $searchId = null;
61 $this->_id = $searchId;
62 return $this;
65 /**
66 * Getter of id
68 * @return int|null
70 public function getId()
72 return $this->_id;
75 /**
76 * Setter of searchName
78 * @param string $searchName Saved search name
80 * @return static
82 public function setSearchName($searchName)
84 $this->_searchName = $searchName;
85 return $this;
88 /**
89 * Getter of searchName
91 * @return string
93 public function getSearchName()
95 return $this->_searchName;
98 /**
99 * Criterias
100 * @var array
102 private $_criterias = null;
105 * Setter of config
107 * @param array $config Global configuration
109 * @return static
111 public function setConfig($config)
113 $this->_config = $config;
114 return $this;
118 * Getter of config
120 * @return array
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
133 * @return static
135 public function setCriterias($criterias, $json = false)
137 if (true === $json && is_string($criterias)) {
138 $this->_criterias = json_decode($criterias, true);
139 return $this;
142 $aListFieldsToGet = array(
143 'criteriaColumn',
144 'criteriaSort',
145 'criteriaShow',
146 'criteria',
147 'criteriaAndOrRow',
148 'criteriaAndOrColumn',
149 'rows',
150 'TableList'
153 $data = array();
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;
166 return $this;
170 * Getter for criterias
172 * @return array
174 public function getCriterias()
176 return $this->_criterias;
180 * Setter for username
182 * @param string $username Username
184 * @return static
186 public function setUsername($username)
188 $this->_username = $username;
189 return $this;
193 * Getter for username
195 * @return string
197 public function getUsername()
199 return $this->_username;
203 * Setter for DB name
205 * @param string $dbname DB name
207 * @return static
209 public function setDbname($dbname)
211 $this->_dbname = $dbname;
212 return $this;
216 * Getter for DB name
218 * @return string
220 public function getDbname()
222 return $this->_dbname;
226 * Public constructor
228 * @param array $config Global configuration
230 public function __construct($config)
232 $this->setConfig($config);
236 * Save the search
238 * @return boolean
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);
250 exit;
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);
264 exit;
267 $savedSearchesTbl
268 = Util::backquote($this->_config['cfgRelation']['db']) . "."
269 . Util::backquote($this->_config['cfgRelation']['savedsearches']);
271 //If it's an insert.
272 if (null === $this->getId()) {
273 $wheres = array(
274 "search_name = '" . Util::sqlAddSlashes($this->getSearchName())
275 . "'"
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);
287 exit;
290 $sqlQuery = "INSERT INTO " . $savedSearchesTbl
291 . "(`username`, `db_name`, `search_name`, `search_data`)"
292 . " VALUES ("
293 . "'" . Util::sqlAddSlashes($this->getUsername()) . "',"
294 . "'" . Util::sqlAddSlashes($this->getDbname()) . "',"
295 . "'" . Util::sqlAddSlashes($this->getSearchName()) . "',"
296 . "'" . Util::sqlAddSlashes(json_encode($this->getCriterias()))
297 . "')";
299 $result = (bool)PMA_queryAsControlUser($sqlQuery);
300 if (!$result) {
301 return false;
304 $this->setId($GLOBALS['dbi']->insertId());
306 return true;
309 //Else, it's an update.
310 $wheres = array(
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);
324 exit;
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);
337 * Delete the search
339 * @return boolean
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);
351 exit;
354 $savedSearchesTbl
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);
379 exit;
382 $savedSearchesTbl = Util::backquote($this->_config['cfgRelation']['db'])
383 . "."
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);
397 exit;
400 $this->setSearchName($oneResult['search_name'])
401 ->setCriterias($oneResult['search_data'], true);
403 return 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()
418 return array();
421 $savedSearchesTbl = Util::backquote($this->_config['cfgRelation']['db'])
422 . "."
423 . Util::backquote($this->_config['cfgRelation']['savedsearches']);
424 $sqlQuery = "SELECT id, search_name "
425 . "FROM " . $savedSearchesTbl . " "
426 . "WHERE "
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);
438 $list = array();
439 while ($oneResult = $GLOBALS['dbi']->fetchArray($resList)) {
440 $list[$oneResult['id']] = $oneResult['search_name'];
443 return $list;