sorry, wrong version checked in
[phpmyadmin/arisferyanto.git] / libraries / bookmark.lib.php
blobb501c070c84d940aecd38e9195c892163c621715
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Set of functions used with the bookmark feature
7 */
10 /**
11 * Defines the bookmark parameters for the current user
13 * @return array the bookmark parameters for the current user
15 * @global integer the id of the current server
17 * @access public
19 function PMA_getBookmarksParam()
21 global $server;
23 $cfgBookmark = '';
25 // No server selected -> no bookmark table
26 if ($server == 0) {
27 return '';
30 $cfgBookmark['user'] = $GLOBALS['cfg']['Server']['user'];
31 $cfgBookmark['db'] = $GLOBALS['cfg']['Server']['pmadb'];
32 $cfgBookmark['table'] = $GLOBALS['cfg']['Server']['bookmarktable'];
34 return $cfgBookmark;
35 } // end of the 'PMA_getBookmarksParam()' function
38 /**
39 * Gets the list of bookmarks defined for the current database
41 * @global resource the controluser db connection handle
43 * @param string the current database name
44 * @param array the bookmark parameters for the current user
46 * @return mixed the bookmarks list if defined, false else
48 * @access public
50 function PMA_listBookmarks($db, $cfgBookmark)
52 global $dbh;
54 $query = 'SELECT label, id FROM '. PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
55 . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
56 . ' AND (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
57 . ' OR user = \'\')'
58 . ' ORDER BY label';
59 $result = PMA_DBI_query($query, $dbh, PMA_DBI_QUERY_STORE);
61 // There are some bookmarks -> store them
62 // use the unique id as the key
63 if ($result && PMA_DBI_num_rows($result) > 0) {
64 while ($row = PMA_DBI_fetch_row($result)) {
65 $bookmark_list[$row[1]] = $row[0];
66 } // end while
67 return $bookmark_list;
69 // No bookmarks for the current database
70 else {
71 return FALSE;
73 } // end of the 'PMA_listBookmarks()' function
76 /**
77 * Gets the sql command from a bookmark
79 * @global resource the controluser db connection handle
81 * @param string the current database name
82 * @param array the bookmark parameters for the current user
83 * @param mixed the id of the bookmark to get
84 * @param string which field to look up the $id
85 * @param boolean TRUE: get all bookmarks regardless of the owning user
87 * @return string the sql query
89 * @access public
91 function PMA_queryBookmarks($db, $cfgBookmark, $id, $id_field = 'id', $action_bookmark_all = FALSE)
93 global $dbh;
95 if (empty($cfgBookmark['db']) || empty($cfgBookmark['table'])) {
96 return '';
99 $query = 'SELECT query FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
100 . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
101 . ($action_bookmark_all? '' : ' AND (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
102 . ' OR user = \'\')' )
103 . ' AND ' . PMA_backquote($id_field) . ' = ' . $id;
104 $result = PMA_DBI_try_query($query, $dbh);
105 if (!$result) return FALSE;
106 list($bookmark_query) = PMA_DBI_fetch_row($result) or array(FALSE);
108 return $bookmark_query;
109 } // end of the 'PMA_queryBookmarks()' function
113 * Gets bookmarked DefaultQuery for a Table
115 * @global resource the controluser db connection handle
117 * @param string the current database name
118 * @param array the bookmark parameters for the current user
119 * @param array the list of all labels to look for
121 * @return array bookmark SQL statements
123 * @access public
125 function &PMA_queryDBBookmarks($db, $cfgBookmark, &$table_array)
127 global $dbh;
128 $bookmarks = array();
130 if (empty($cfgBookmark['db']) || empty($cfgBookmark['table'])) {
131 return $bookmarks;
134 $search_for = array();
135 foreach($table_array AS $table => $table_sortkey) {
136 $search_for[] = "'" . PMA_sqlAddslashes($table) . "'";
139 $query = 'SELECT label, query FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
140 . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
141 . (count($search_for) > 0 ? ' AND label IN (' . implode(', ', $search_for) . ')' : '');
142 $result = PMA_DBI_try_query($query, $dbh, PMA_DBI_QUERY_STORE);
143 if (!$result || PMA_DBI_num_rows($result) < 1) return $bookmarks;
144 while ($row = PMA_DBI_fetch_assoc($result)) {
145 $bookmarks[$row['label']] = $row['query'];
148 return $bookmarks;
149 } // end of the 'PMA_queryBookmarks()' function
152 * Adds a bookmark
154 * @global resource the controluser db connection handle
156 * @param array the properties of the bookmark to add
157 * @param array the bookmark parameters for the current user
158 * @param boolean whether to make the bookmark available for all users
160 * @return boolean whether the INSERT succeeds or not
162 * @access public
164 function PMA_addBookmarks($fields, $cfgBookmark, $all_users = false)
166 global $dbh;
168 $query = 'INSERT INTO ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
169 . ' (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']) . '\')';
170 $result = PMA_DBI_query($query, $dbh);
172 return TRUE;
173 } // end of the 'PMA_addBookmarks()' function
177 * Deletes a bookmark
179 * @global resource the controluser db connection handle
181 * @param string the current database name
182 * @param array the bookmark parameters for the current user
183 * @param integer the id of the bookmark to get
185 * @access public
187 function PMA_deleteBookmarks($db, $cfgBookmark, $id)
189 global $dbh;
191 $query = 'DELETE FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
192 . ' WHERE (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
193 . ' OR user = \'\')'
194 . ' AND id = ' . $id;
195 $result = PMA_DBI_try_query($query, $dbh);
196 } // end of the 'PMA_deleteBookmarks()' function
200 * Bookmark Support
202 $cfg['Bookmark'] = PMA_getBookmarksParam();