2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used with the bookmark feature
13 require_once './libraries/relation.lib.php';
16 * Defines the bookmark parameters for the current user
18 * @uses $GLOBALS['server']
19 * @uses PMA_getRelationsParam()
20 * @uses $GLOBALS['cfg']['Server']['user']
21 * @uses $GLOBALS['cfg']['Server']['pmadb']
22 * @uses $GLOBALS['cfg']['Server']['bookmarktable']
23 * @return array the bookmark parameters for the current user
26 function PMA_Bookmark_getParams()
28 static $cfgBookmark = null;
30 if (null !== $cfgBookmark) {
34 $cfgRelation = PMA_getRelationsParam();
36 if ($cfgRelation['bookmarkwork']) {
38 'user' => $GLOBALS['cfg']['Server']['user'],
39 'db' => $GLOBALS['cfg']['Server']['pmadb'],
40 'table' => $GLOBALS['cfg']['Server']['bookmarktable'],
47 } // end of the 'PMA_Bookmark_getParams()' function
51 * Gets the list of bookmarks defined for the current database
53 * @uses PMA_backquote()
54 * @uses PMA_sqlAddslashes()
55 * @uses PMA_DBI_fetch_result()
56 * @uses PMA_DBI_QUERY_STORE
57 * @uses PMA_Bookmark_getParams()
58 * @global resource the controluser db connection handle
60 * @param string the current database name
62 * @return array the bookmarks list (key as index, label as value)
66 function PMA_Bookmark_getList($db)
70 $cfgBookmark = PMA_Bookmark_getParams();
72 if (empty($cfgBookmark)) {
76 $query = 'SELECT label, id FROM '. PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
77 . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
78 . ' AND (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
81 return PMA_DBI_fetch_result($query, 'id', 'label', $controllink, PMA_DBI_QUERY_STORE
);
82 } // end of the 'PMA_Bookmark_getList()' function
86 * Gets the sql command from a bookmark
88 * @uses PMA_backquote()
89 * @uses PMA_sqlAddslashes()
90 * @uses PMA_DBI_fetch_value()
91 * @uses PMA_Bookmark_getParams()
92 * @global resource the controluser db connection handle
94 * @param string the current database name
95 * @param mixed the id of the bookmark to get
96 * @param string which field to look up the $id
97 * @param boolean TRUE: get all bookmarks regardless of the owning user
99 * @return string the sql query
103 function PMA_Bookmark_get($db, $id, $id_field = 'id', $action_bookmark_all = FALSE)
107 $cfgBookmark = PMA_Bookmark_getParams();
109 if (empty($cfgBookmark)) {
113 $query = 'SELECT query FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
114 . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
115 . ($action_bookmark_all?
'' : ' AND (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
116 . ' OR user = \'\')')
117 . ' AND ' . PMA_backquote($id_field) . ' = ' . $id;
118 return PMA_DBI_fetch_value($query, 0, 0, $controllink);
119 } // end of the 'PMA_Bookmark_get()' function
124 * @uses PMA_backquote()
125 * @uses PMA_sqlAddslashes()
126 * @uses PMA_DBI_query()
127 * @uses PMA_Bookmark_getParams()
128 * @global resource the controluser db connection handle
130 * @param array the properties of the bookmark to add; here,
131 * $fields['query'] is urlencoded
132 * @param boolean whether to make the bookmark available for all users
134 * @return boolean whether the INSERT succeeds or not
138 function PMA_Bookmark_save($fields, $all_users = false)
142 $cfgBookmark = PMA_Bookmark_getParams();
144 if (empty($cfgBookmark)) {
148 $query = 'INSERT INTO ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
149 . ' (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']) . '\')';
150 return PMA_DBI_query($query, $controllink);
151 } // end of the 'PMA_Bookmark_save()' function
157 * @uses PMA_backquote()
158 * @uses PMA_sqlAddslashes()
159 * @uses PMA_DBI_try_query()
160 * @uses PMA_Bookmark_getParams()
161 * @global resource the controluser db connection handle
163 * @param string the current database name
164 * @param integer the id of the bookmark to get
168 function PMA_Bookmark_delete($db, $id)
172 $cfgBookmark = PMA_Bookmark_getParams();
174 if (empty($cfgBookmark)) {
178 $query = 'DELETE FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
179 . ' WHERE (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
181 . ' AND id = ' . $id;
182 return PMA_DBI_try_query($query, $controllink);
183 } // end of the 'PMA_Bookmark_delete()' function
189 $GLOBALS['cfg']['Bookmark'] = PMA_Bookmark_getParams();