Translated using Weblate (Albanian)
[phpmyadmin.git] / libraries / SavedSearches.php
blob9cd652afaac1a481506173c7c73d248b8fa5bc5e
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 if (isset($criterias[$field])) {
159 $data[$field] = $criterias[$field];
163 /* Limit amount of rows */
164 if (!isset($data['rows'])) {
165 $data['rows'] = 0;
166 } else {
167 $data['rows'] = min(
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;
178 return $this;
182 * Getter for criterias
184 * @return array
186 public function getCriterias()
188 return $this->_criterias;
192 * Setter for username
194 * @param string $username Username
196 * @return static
198 public function setUsername($username)
200 $this->_username = $username;
201 return $this;
205 * Getter for username
207 * @return string
209 public function getUsername()
211 return $this->_username;
215 * Setter for DB name
217 * @param string $dbname DB name
219 * @return static
221 public function setDbname($dbname)
223 $this->_dbname = $dbname;
224 return $this;
228 * Getter for DB name
230 * @return string
232 public function getDbname()
234 return $this->_dbname;
238 * Public constructor
240 * @param array $config Global configuration
242 public function __construct($config)
244 $this->setConfig($config);
248 * Save the search
250 * @return boolean
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);
262 exit;
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);
276 exit;
279 $savedSearchesTbl
280 = Util::backquote($this->_config['cfgRelation']['db']) . "."
281 . Util::backquote($this->_config['cfgRelation']['savedsearches']);
283 //If it's an insert.
284 if (null === $this->getId()) {
285 $wheres = array(
286 "search_name = '" . $GLOBALS['dbi']->escapeString($this->getSearchName())
287 . "'"
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);
299 exit;
302 $sqlQuery = "INSERT INTO " . $savedSearchesTbl
303 . "(`username`, `db_name`, `search_name`, `search_data`)"
304 . " VALUES ("
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()))
309 . "')";
311 $result = (bool)PMA_queryAsControlUser($sqlQuery);
312 if (!$result) {
313 return false;
316 $this->setId($GLOBALS['dbi']->insertId());
318 return true;
321 //Else, it's an update.
322 $wheres = array(
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);
336 exit;
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);
349 * Delete the search
351 * @return boolean
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);
363 exit;
366 $savedSearchesTbl
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);
391 exit;
394 $savedSearchesTbl = Util::backquote($this->_config['cfgRelation']['db'])
395 . "."
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);
409 exit;
412 $this->setSearchName($oneResult['search_name'])
413 ->setCriterias($oneResult['search_data'], true);
415 return 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()
430 return array();
433 $savedSearchesTbl = Util::backquote($this->_config['cfgRelation']['db'])
434 . "."
435 . Util::backquote($this->_config['cfgRelation']['savedsearches']);
436 $sqlQuery = "SELECT id, search_name "
437 . "FROM " . $savedSearchesTbl . " "
438 . "WHERE "
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);
450 $list = array();
451 while ($oneResult = $GLOBALS['dbi']->fetchArray($resList)) {
452 $list[$oneResult['id']] = $oneResult['search_name'];
455 return $list;