Translated using Weblate (Slovenian)
[phpmyadmin.git] / libraries / SavedSearches.class.php
blob5dd8f40614f6e898f254a4ce44a48f2b2f2a76e5
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Saved searches managing
6 * @package PhpMyAdmin
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * Saved searches managing
16 * @package PhpMyAdmin
18 class PMA_SavedSearches
20 /**
21 * Global configuration
22 * @var array
24 private $_config = null;
26 /**
27 * Id
28 * @var int|null
30 private $_id = null;
32 /**
33 * Username
34 * @var string
36 private $_username = null;
38 /**
39 * DB name
40 * @var string
42 private $_dbname = null;
44 /**
45 * Saved search name
46 * @var string
48 private $_searchName = null;
50 /**
51 * Setter of id
53 * @param int|null $searchId Id of search
55 * @return static
57 public function setId($searchId)
59 $searchId = (int)$searchId;
60 if (empty($searchId)) {
61 $searchId = null;
64 $this->_id = $searchId;
65 return $this;
68 /**
69 * Getter of id
71 * @return int|null
73 public function getId()
75 return $this->_id;
78 /**
79 * Setter of searchName
81 * @param string $searchName Saved search name
83 * @return static
85 public function setSearchName($searchName)
87 $this->_searchName = $searchName;
88 return $this;
91 /**
92 * Getter of searchName
94 * @return string
96 public function getSearchName()
98 return $this->_searchName;
102 * Criterias
103 * @var array
105 private $_criterias = null;
108 * Setter of config
110 * @param array $config Global configuration
112 * @return static
114 public function setConfig($config)
116 $this->_config = $config;
117 return $this;
121 * Getter of config
123 * @return array
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
136 * @return static
138 public function setCriterias($criterias, $json = false)
140 if (true === $json && is_string($criterias)) {
141 $this->_criterias = json_decode($criterias, true);
142 return $this;
145 $aListFieldsToGet = array(
146 'criteriaColumn',
147 'criteriaSort',
148 'criteriaShow',
149 'criteria',
150 'criteriaAndOrRow',
151 'criteriaAndOrColumn',
152 'rows'
155 $data = array();
157 $data['criteriaColumnCount'] = count($criterias['criteriaColumn']);
159 foreach ($aListFieldsToGet as $field) {
160 $data[$field] = $criterias[$field];
163 for ($i = 0; $i <= $data['rows']; $i++) {
164 $data['Or' . $i] = $criterias['Or' . $i];
167 $this->_criterias = $data;
168 return $this;
172 * Getter for criterias
174 * @return array
176 public function getCriterias()
178 return $this->_criterias;
182 * Setter for username
184 * @param string $username Username
186 * @return static
188 public function setUsername($username)
190 $this->_username = $username;
191 return $this;
195 * Getter for username
197 * @return string
199 public function getUsername()
201 return $this->_username;
205 * Setter for DB name
207 * @param string $dbname DB name
209 * @return static
211 public function setDbname($dbname)
213 $this->_dbname = $dbname;
214 return $this;
218 * Getter for DB name
220 * @return string
222 public function getDbname()
224 return $this->_dbname;
228 * Public constructor
230 * @param array $config Global configuration
232 public function __construct($config)
234 $this->setConfig($config);
238 * Save the search
240 * @return boolean
242 public function save()
244 if (null == $this->getSearchName()) {
245 $message = PMA_Message::error(
246 __('Please provide a name for this bookmarked search.')
248 $response = PMA_Response::getInstance();
249 $response->isSuccess($message->isSuccess());
250 $response->addJSON('fieldWithError', 'searchName');
251 $response->addJSON('message', $message);
252 exit;
255 if (null == $this->getUsername()
256 || null == $this->getDbname()
257 || null == $this->getSearchName()
258 || null == $this->getCriterias()
260 $message = PMA_Message::error(
261 __('Missing information to save the bookmarked search.')
263 $response = PMA_Response::getInstance();
264 $response->isSuccess($message->isSuccess());
265 $response->addJSON('message', $message);
266 exit;
269 $savedSearchesTbl
270 = PMA_Util::backquote($this->_config['cfgRelation']['db']) . "."
271 . PMA_Util::backquote($this->_config['cfgRelation']['savedsearches']);
273 //If it's an insert.
274 if (null === $this->getId()) {
275 $wheres = array(
276 "search_name = '" . PMA_Util::sqlAddSlashes($this->getSearchName())
277 . "'"
279 $existingSearches = $this->getList($wheres);
281 if (!empty($existingSearches)) {
282 $message = PMA_Message::error(
283 __('An entry with this name already exists.')
285 $response = PMA_Response::getInstance();
286 $response->isSuccess($message->isSuccess());
287 $response->addJSON('fieldWithError', 'searchName');
288 $response->addJSON('message', $message);
289 exit;
292 $sqlQuery = "INSERT INTO " . $savedSearchesTbl
293 . "(`username`, `db_name`, `search_name`, `search_data`)"
294 . " VALUES ("
295 . "'" . PMA_Util::sqlAddSlashes($this->getUsername()) . "',"
296 . "'" . PMA_Util::sqlAddSlashes($this->getDbname()) . "',"
297 . "'" . PMA_Util::sqlAddSlashes($this->getSearchName()) . "',"
298 . "'" . PMA_Util::sqlAddSlashes(json_encode($this->getCriterias()))
299 . "')";
301 $result = (bool)PMA_queryAsControlUser($sqlQuery);
302 if (!$result) {
303 return false;
306 $this->setId($GLOBALS['dbi']->insertId());
308 return true;
311 //Else, it's an update.
312 $wheres = array(
313 "id != " . $this->getId(),
314 "search_name = '" . PMA_Util::sqlAddSlashes($this->getSearchName()) . "'"
316 $existingSearches = $this->getList($wheres);
318 if (!empty($existingSearches)) {
319 $message = PMA_Message::error(
320 __('An entry with this name already exists.')
322 $response = PMA_Response::getInstance();
323 $response->isSuccess($message->isSuccess());
324 $response->addJSON('fieldWithError', 'searchName');
325 $response->addJSON('message', $message);
326 exit;
329 $sqlQuery = "UPDATE " . $savedSearchesTbl
330 . "SET `search_name` = '"
331 . PMA_Util::sqlAddSlashes($this->getSearchName()) . "', "
332 . "`search_data` = '"
333 . PMA_Util::sqlAddSlashes(json_encode($this->getCriterias())) . "' "
334 . "WHERE id = " . $this->getId();
335 return (bool)PMA_queryAsControlUser($sqlQuery);
339 * Delete the search
341 * @return boolean
343 public function delete()
345 if (null == $this->getId()) {
346 $message = PMA_Message::error(
347 __('Missing information to delete the search.')
349 $response = PMA_Response::getInstance();
350 $response->isSuccess($message->isSuccess());
351 $response->addJSON('fieldWithError', 'searchId');
352 $response->addJSON('message', $message);
353 exit;
356 $savedSearchesTbl
357 = PMA_Util::backquote($this->_config['cfgRelation']['db']) . "."
358 . PMA_Util::backquote($this->_config['cfgRelation']['savedsearches']);
360 $sqlQuery = "DELETE FROM " . $savedSearchesTbl
361 . "WHERE id = '" . PMA_Util::sqlAddSlashes($this->getId()) . "'";
363 return (bool)PMA_queryAsControlUser($sqlQuery);
367 * Load the current search from an id.
369 * @return bool Success
371 public function load()
373 if (null == $this->getId()) {
374 $message = PMA_Message::error(
375 __('Missing information to load the search.')
377 $response = PMA_Response::getInstance();
378 $response->isSuccess($message->isSuccess());
379 $response->addJSON('fieldWithError', 'searchId');
380 $response->addJSON('message', $message);
381 exit;
384 $savedSearchesTbl = PMA_Util::backquote($this->_config['cfgRelation']['db'])
385 . "."
386 . PMA_Util::backquote($this->_config['cfgRelation']['savedsearches']);
387 $sqlQuery = "SELECT id, search_name, search_data "
388 . "FROM " . $savedSearchesTbl . " "
389 . "WHERE id = '" . PMA_Util::sqlAddSlashes($this->getId()) . "' ";
391 $resList = PMA_queryAsControlUser($sqlQuery);
393 if (false === ($oneResult = $GLOBALS['dbi']->fetchArray($resList))) {
394 $message = PMA_Message::error(__('Error while loading the search.'));
395 $response = PMA_Response::getInstance();
396 $response->isSuccess($message->isSuccess());
397 $response->addJSON('fieldWithError', 'searchId');
398 $response->addJSON('message', $message);
399 exit;
402 $this->setSearchName($oneResult['search_name'])
403 ->setCriterias($oneResult['search_data'], true);
405 return true;
409 * Get the list of saved search of a user on a DB
411 * @param string[] $wheres List of filters
413 * @return array|bool List of saved search or false on failure
415 public function getList(array $wheres = array())
417 if (null == $this->getUsername()
418 || null == $this->getDbname()
420 return false;
423 $savedSearchesTbl = PMA_Util::backquote($this->_config['cfgRelation']['db'])
424 . "."
425 . PMA_Util::backquote($this->_config['cfgRelation']['savedsearches']);
426 $sqlQuery = "SELECT id, search_name "
427 . "FROM " . $savedSearchesTbl . " "
428 . "WHERE "
429 . "username = '" . PMA_Util::sqlAddSlashes($this->getUsername()) . "' "
430 . "AND db_name = '" . PMA_Util::sqlAddSlashes($this->getDbname()) . "' ";
432 foreach ($wheres as $where) {
433 $sqlQuery .= "AND " . $where . " ";
436 $sqlQuery .= "order by search_name ASC ";
438 $resList = PMA_queryAsControlUser($sqlQuery);
440 $list = array();
441 while ($oneResult = $GLOBALS['dbi']->fetchArray($resList)) {
442 $list[$oneResult['id']] = $oneResult['search_name'];
445 return $list;