2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used with the bookmark feature
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
20 function PMA_Bookmark_getParams()
22 static $cfgBookmark = null;
24 if (null !== $cfgBookmark) {
28 $cfgRelation = PMA_getRelationsParam();
30 if ($cfgRelation['bookmarkwork']) {
32 'user' => $GLOBALS['cfg']['Server']['user'],
33 'db' => $GLOBALS['cfg']['Server']['pmadb'],
34 'table' => $GLOBALS['cfg']['Server']['bookmarktable'],
41 } // end of the 'PMA_Bookmark_getParams()' function
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)
60 function PMA_Bookmark_getList($db)
64 $cfgBookmark = PMA_Bookmark_getParams();
66 if (empty($cfgBookmark)) {
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']) . '\''
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) . '\''
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;
91 } // end of the 'PMA_Bookmark_getList()' function
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
113 function PMA_Bookmark_get($db, $id, $id_field = 'id', $action_bookmark_all = FALSE, $exact_user_match = FALSE)
117 $cfgBookmark = PMA_Bookmark_getParams();
119 if (empty($cfgBookmark)) {
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 = \'\'';
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
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
156 function PMA_Bookmark_save($fields, $all_users = false)
160 $cfgBookmark = PMA_Bookmark_getParams();
162 if (empty($cfgBookmark)) {
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
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
186 function PMA_Bookmark_delete($db, $id)
190 $cfgBookmark = PMA_Bookmark_getParams();
192 if (empty($cfgBookmark)) {
196 $query = 'DELETE FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
197 . ' WHERE (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
199 . ' AND id = ' . $id;
200 return PMA_DBI_try_query($query, $controllink);
201 } // end of the 'PMA_Bookmark_delete()' function
207 $GLOBALS['cfg']['Bookmark'] = PMA_Bookmark_getParams();