bug#3212720 Show error message on error.
[phpmyadmin/ayax.git] / libraries / bookmark.lib.php
blob62d7e1b7af763e1c8bb61f2c509fbc1dfd042638
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions used with the bookmark feature
6 * @package phpMyAdmin
7 */
9 /**
10 * Defines the bookmark parameters for the current user
12 * @uses $GLOBALS['server']
13 * @uses PMA_getRelationsParam()
14 * @uses $GLOBALS['cfg']['Server']['user']
15 * @uses $GLOBALS['cfg']['Server']['pmadb']
16 * @uses $GLOBALS['cfg']['Server']['bookmarktable']
17 * @return array the bookmark parameters for the current user
18 * @access public
20 function PMA_Bookmark_getParams()
22 static $cfgBookmark = null;
24 if (null !== $cfgBookmark) {
25 return $cfgBookmark;
28 $cfgRelation = PMA_getRelationsParam();
30 if ($cfgRelation['bookmarkwork']) {
31 $cfgBookmark = array(
32 'user' => $GLOBALS['cfg']['Server']['user'],
33 'db' => $GLOBALS['cfg']['Server']['pmadb'],
34 'table' => $GLOBALS['cfg']['Server']['bookmarktable'],
36 } else {
37 $cfgBookmark = false;
40 return $cfgBookmark;
41 } // end of the 'PMA_Bookmark_getParams()' function
44 /**
45 * Gets the list of bookmarks defined for the current database
47 * @uses PMA_backquote()
48 * @uses PMA_sqlAddslashes()
49 * @uses PMA_DBI_fetch_result()
50 * @uses PMA_DBI_QUERY_STORE
51 * @uses PMA_Bookmark_getParams()
52 * @global resource the controluser db connection handle
54 * @param string the current database name
56 * @return array the bookmarks list (key as index, label as value)
58 * @access public
60 function PMA_Bookmark_getList($db)
62 global $controllink;
64 $cfgBookmark = PMA_Bookmark_getParams();
66 if (empty($cfgBookmark)) {
67 return array();
70 $query = 'SELECT label, id FROM '. PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
71 . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
72 . ' AND user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
73 . ' ORDER BY label';
74 $per_user = PMA_DBI_fetch_result($query, 'id', 'label', $controllink, PMA_DBI_QUERY_STORE);
76 $query = 'SELECT label, id FROM '. PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
77 . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
78 . ' AND user = \'\''
79 . ' ORDER BY label';
80 $global = PMA_DBI_fetch_result($query, 'id', 'label', $controllink, PMA_DBI_QUERY_STORE);
82 foreach($global as $key => $val) {
83 $global[$key] = $val . ' (' . __('shared') . ')';
86 $ret = $global + $per_user;
88 asort($ret);
90 return $ret;
91 } // end of the 'PMA_Bookmark_getList()' function
94 /**
95 * Gets the sql command from a bookmark
97 * @uses PMA_backquote()
98 * @uses PMA_sqlAddslashes()
99 * @uses PMA_DBI_fetch_value()
100 * @uses PMA_Bookmark_getParams()
101 * @global resource the controluser db connection handle
103 * @param string the current database name
104 * @param mixed the id of the bookmark to get
105 * @param string which field to look up the $id
106 * @param boolean TRUE: get all bookmarks regardless of the owning user
107 * @param boolean whether to ignore bookmarks with no user
109 * @return string the sql query
111 * @access public
113 function PMA_Bookmark_get($db, $id, $id_field = 'id', $action_bookmark_all = FALSE, $exact_user_match = FALSE)
115 global $controllink;
117 $cfgBookmark = PMA_Bookmark_getParams();
119 if (empty($cfgBookmark)) {
120 return '';
123 $query = 'SELECT query FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
124 . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\'';
126 if (!$action_bookmark_all) {
127 $query .= ' AND (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\'';
128 if (!$exact_user_match) {
129 $query .= ' OR user = \'\'';
131 $query .= ')';
134 $query .= ' AND ' . PMA_backquote($id_field) . ' = ' . $id;
136 return PMA_DBI_fetch_value($query, 0, 0, $controllink);
137 } // end of the 'PMA_Bookmark_get()' function
140 * Adds a bookmark
142 * @uses PMA_backquote()
143 * @uses PMA_sqlAddslashes()
144 * @uses PMA_DBI_query()
145 * @uses PMA_Bookmark_getParams()
146 * @global resource the controluser db connection handle
148 * @param array the properties of the bookmark to add; here,
149 * $fields['query'] is urlencoded
150 * @param boolean whether to make the bookmark available for all users
152 * @return boolean whether the INSERT succeeds or not
154 * @access public
156 function PMA_Bookmark_save($fields, $all_users = false)
158 global $controllink;
160 $cfgBookmark = PMA_Bookmark_getParams();
162 if (empty($cfgBookmark)) {
163 return false;
166 $query = 'INSERT INTO ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
167 . ' (id, dbase, user, query, label) VALUES (NULL, \'' . PMA_sqlAddslashes($fields['dbase']) . '\', \'' . ($all_users ? '' : PMA_sqlAddslashes($fields['user'])) . '\', \'' . PMA_sqlAddslashes(urldecode($fields['query'])) . '\', \'' . PMA_sqlAddslashes($fields['label']) . '\')';
168 return PMA_DBI_query($query, $controllink);
169 } // end of the 'PMA_Bookmark_save()' function
173 * Deletes a bookmark
175 * @uses PMA_backquote()
176 * @uses PMA_sqlAddslashes()
177 * @uses PMA_DBI_try_query()
178 * @uses PMA_Bookmark_getParams()
179 * @global resource the controluser db connection handle
181 * @param string the current database name
182 * @param integer the id of the bookmark to get
184 * @access public
186 function PMA_Bookmark_delete($db, $id)
188 global $controllink;
190 $cfgBookmark = PMA_Bookmark_getParams();
192 if (empty($cfgBookmark)) {
193 return false;
196 $query = 'DELETE FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
197 . ' WHERE (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
198 . ' OR user = \'\')'
199 . ' AND id = ' . $id;
200 return PMA_DBI_try_query($query, $controllink);
201 } // end of the 'PMA_Bookmark_delete()' function
205 * Bookmark Support
207 $GLOBALS['cfg']['Bookmark'] = PMA_Bookmark_getParams();