Removing old documentation
[openemr.git] / phpmyadmin / libraries / SavedSearches.class.php
blob4a74f68e5617bc38bd4b62bc49b09eb18ae78ee4
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',
153 'TableList'
156 $data = array();
158 $data['criteriaColumnCount'] = count($criterias['criteriaColumn']);
160 foreach ($aListFieldsToGet as $field) {
161 $data[$field] = $criterias[$field];
164 for ($i = 0; $i <= $data['rows']; $i++) {
165 $data['Or' . $i] = $criterias['Or' . $i];
168 $this->_criterias = $data;
169 return $this;
173 * Getter for criterias
175 * @return array
177 public function getCriterias()
179 return $this->_criterias;
183 * Setter for username
185 * @param string $username Username
187 * @return static
189 public function setUsername($username)
191 $this->_username = $username;
192 return $this;
196 * Getter for username
198 * @return string
200 public function getUsername()
202 return $this->_username;
206 * Setter for DB name
208 * @param string $dbname DB name
210 * @return static
212 public function setDbname($dbname)
214 $this->_dbname = $dbname;
215 return $this;
219 * Getter for DB name
221 * @return string
223 public function getDbname()
225 return $this->_dbname;
229 * Public constructor
231 * @param array $config Global configuration
233 public function __construct($config)
235 $this->setConfig($config);
239 * Save the search
241 * @return boolean
243 public function save()
245 if (null == $this->getSearchName()) {
246 $message = PMA_Message::error(
247 __('Please provide a name for this bookmarked search.')
249 $response = PMA_Response::getInstance();
250 $response->isSuccess($message->isSuccess());
251 $response->addJSON('fieldWithError', 'searchName');
252 $response->addJSON('message', $message);
253 exit;
256 if (null == $this->getUsername()
257 || null == $this->getDbname()
258 || null == $this->getSearchName()
259 || null == $this->getCriterias()
261 $message = PMA_Message::error(
262 __('Missing information to save the bookmarked search.')
264 $response = PMA_Response::getInstance();
265 $response->isSuccess($message->isSuccess());
266 $response->addJSON('message', $message);
267 exit;
270 $savedSearchesTbl
271 = PMA_Util::backquote($this->_config['cfgRelation']['db']) . "."
272 . PMA_Util::backquote($this->_config['cfgRelation']['savedsearches']);
274 //If it's an insert.
275 if (null === $this->getId()) {
276 $wheres = array(
277 "search_name = '" . PMA_Util::sqlAddSlashes($this->getSearchName())
278 . "'"
280 $existingSearches = $this->getList($wheres);
282 if (!empty($existingSearches)) {
283 $message = PMA_Message::error(
284 __('An entry with this name already exists.')
286 $response = PMA_Response::getInstance();
287 $response->isSuccess($message->isSuccess());
288 $response->addJSON('fieldWithError', 'searchName');
289 $response->addJSON('message', $message);
290 exit;
293 $sqlQuery = "INSERT INTO " . $savedSearchesTbl
294 . "(`username`, `db_name`, `search_name`, `search_data`)"
295 . " VALUES ("
296 . "'" . PMA_Util::sqlAddSlashes($this->getUsername()) . "',"
297 . "'" . PMA_Util::sqlAddSlashes($this->getDbname()) . "',"
298 . "'" . PMA_Util::sqlAddSlashes($this->getSearchName()) . "',"
299 . "'" . PMA_Util::sqlAddSlashes(json_encode($this->getCriterias()))
300 . "')";
302 $result = (bool)PMA_queryAsControlUser($sqlQuery);
303 if (!$result) {
304 return false;
307 $this->setId($GLOBALS['dbi']->insertId());
309 return true;
312 //Else, it's an update.
313 $wheres = array(
314 "id != " . $this->getId(),
315 "search_name = '" . PMA_Util::sqlAddSlashes($this->getSearchName()) . "'"
317 $existingSearches = $this->getList($wheres);
319 if (!empty($existingSearches)) {
320 $message = PMA_Message::error(
321 __('An entry with this name already exists.')
323 $response = PMA_Response::getInstance();
324 $response->isSuccess($message->isSuccess());
325 $response->addJSON('fieldWithError', 'searchName');
326 $response->addJSON('message', $message);
327 exit;
330 $sqlQuery = "UPDATE " . $savedSearchesTbl
331 . "SET `search_name` = '"
332 . PMA_Util::sqlAddSlashes($this->getSearchName()) . "', "
333 . "`search_data` = '"
334 . PMA_Util::sqlAddSlashes(json_encode($this->getCriterias())) . "' "
335 . "WHERE id = " . $this->getId();
336 return (bool)PMA_queryAsControlUser($sqlQuery);
340 * Delete the search
342 * @return boolean
344 public function delete()
346 if (null == $this->getId()) {
347 $message = PMA_Message::error(
348 __('Missing information to delete the search.')
350 $response = PMA_Response::getInstance();
351 $response->isSuccess($message->isSuccess());
352 $response->addJSON('fieldWithError', 'searchId');
353 $response->addJSON('message', $message);
354 exit;
357 $savedSearchesTbl
358 = PMA_Util::backquote($this->_config['cfgRelation']['db']) . "."
359 . PMA_Util::backquote($this->_config['cfgRelation']['savedsearches']);
361 $sqlQuery = "DELETE FROM " . $savedSearchesTbl
362 . "WHERE id = '" . PMA_Util::sqlAddSlashes($this->getId()) . "'";
364 return (bool)PMA_queryAsControlUser($sqlQuery);
368 * Load the current search from an id.
370 * @return bool Success
372 public function load()
374 if (null == $this->getId()) {
375 $message = PMA_Message::error(
376 __('Missing information to load the search.')
378 $response = PMA_Response::getInstance();
379 $response->isSuccess($message->isSuccess());
380 $response->addJSON('fieldWithError', 'searchId');
381 $response->addJSON('message', $message);
382 exit;
385 $savedSearchesTbl = PMA_Util::backquote($this->_config['cfgRelation']['db'])
386 . "."
387 . PMA_Util::backquote($this->_config['cfgRelation']['savedsearches']);
388 $sqlQuery = "SELECT id, search_name, search_data "
389 . "FROM " . $savedSearchesTbl . " "
390 . "WHERE id = '" . PMA_Util::sqlAddSlashes($this->getId()) . "' ";
392 $resList = PMA_queryAsControlUser($sqlQuery);
394 if (false === ($oneResult = $GLOBALS['dbi']->fetchArray($resList))) {
395 $message = PMA_Message::error(__('Error while loading the search.'));
396 $response = PMA_Response::getInstance();
397 $response->isSuccess($message->isSuccess());
398 $response->addJSON('fieldWithError', 'searchId');
399 $response->addJSON('message', $message);
400 exit;
403 $this->setSearchName($oneResult['search_name'])
404 ->setCriterias($oneResult['search_data'], true);
406 return true;
410 * Get the list of saved search of a user on a DB
412 * @param string[] $wheres List of filters
414 * @return array|bool List of saved search or false on failure
416 public function getList(array $wheres = array())
418 if (null == $this->getUsername()
419 || null == $this->getDbname()
421 return false;
424 $savedSearchesTbl = PMA_Util::backquote($this->_config['cfgRelation']['db'])
425 . "."
426 . PMA_Util::backquote($this->_config['cfgRelation']['savedsearches']);
427 $sqlQuery = "SELECT id, search_name "
428 . "FROM " . $savedSearchesTbl . " "
429 . "WHERE "
430 . "username = '" . PMA_Util::sqlAddSlashes($this->getUsername()) . "' "
431 . "AND db_name = '" . PMA_Util::sqlAddSlashes($this->getDbname()) . "' ";
433 foreach ($wheres as $where) {
434 $sqlQuery .= "AND " . $where . " ";
437 $sqlQuery .= "order by search_name ASC ";
439 $resList = PMA_queryAsControlUser($sqlQuery);
441 $list = array();
442 while ($oneResult = $GLOBALS['dbi']->fetchArray($resList)) {
443 $list[$oneResult['id']] = $oneResult['search_name'];
446 return $list;