/[drupal]/contributions/modules/user_display/user_display_links.module
ViewVC logotype

Contents of /contributions/modules/user_display/user_display_links.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Sat Jul 19 23:12:33 2008 UTC (16 months ago) by sun
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-5
File MIME type: text/x-php
Initial commit of User Display API.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Display action links for user names and pictures.
7 */
8
9 /**
10 * Implementation of hook_help().
11 */
12 function user_display_links_help($section) {
13 switch ($section) {
14 case 'admin/settings/user-display/links':
15 return t('Choose from the available action links that should be displayed for users.');
16 }
17 }
18
19 /**
20 * Implementation of hook_menu().
21 */
22 function user_display_links_menu($may_cache) {
23 $items = array();
24 if ($may_cache) {
25 $items[] = array(
26 'path' => 'admin/user/user-display/links',
27 'title' => t('User action links'),
28 'callback' => 'drupal_get_form',
29 'callback arguments' => array('user_display_links_settings'),
30 );
31 $items[] = array(
32 'path' => 'js/user_display_links/links',
33 'callback' => 'user_display_links_js_links',
34 'access' => TRUE,
35 'type' => MENU_CALLBACK,
36 );
37 }
38 else {
39 $path = drupal_get_path('module', 'user_display_links');
40 drupal_add_js($path .'/user_display_links.js');
41 drupal_add_css($path .'/user_display_links.css');
42 }
43 return $items;
44 }
45
46 /**
47 * Menu callback; return the module settings form.
48 */
49 function user_display_links_settings() {
50 $settings = variable_get('user_display_links', array());
51
52 foreach (module_implements('user_links', TRUE) as $module) {
53 $form['user_display_links'] = array(
54 '#tree' => TRUE,
55 '#type' => 'fieldset',
56 '#title' => ucfirst(str_replace('_', ' ', $module)),
57 '#collapsible' => TRUE,
58 '#collapsed' => FALSE,
59 );
60 // Get available action links.
61 $links = module_invoke($module, 'user_links', 'info');
62 foreach ($links as $class => $info) {
63 $form['user_display_links'][$class] = array(
64 '#type' => 'checkbox',
65 '#title' => $info,
66 '#default_value' => isset($settings[$class]) ? $settings[$class] : 0,
67 );
68 }
69 }
70
71 return system_settings_form($form);
72 }
73
74 /**
75 * @ingroup js_callback
76 * @{
77 */
78
79 /**
80 * Implementation of hook_js().
81 */
82 function user_display_links_js() {
83 return array(
84 'links' => array(
85 'callback' => 'user_display_links_js_links',
86 'dependencies' => array('user'),
87 ),
88 );
89 }
90
91 /**
92 * AJAX callback; render a list of action links for a user.
93 *
94 * @param $uid
95 * The user id.
96 *
97 * @see js.php, user_display_links.js
98 */
99 function user_display_links_js_links($uid = 0) {
100 global $user;
101 if (!$uid || !$account = db_fetch_object(db_query("SELECT u.uid FROM {users} u WHERE u.uid = %d", $uid))) {
102 return '';
103 }
104 $links = array();
105 // Determine destination path.
106 $destination = array();
107 if ($referer = referer_uri()) {
108 $referer = parse_url($referer);
109 if ($referer = trim($referer['path'], '/')) {
110 $destination['query'] = 'destination='. $referer;
111 }
112 }
113
114 if ($user->uid && $user->uid != $uid) {
115 // Private message.
116 $links['privatemsg'] = array(
117 'title' => t('New message'),
118 'href' => 'privatemsg/new/'. $uid,
119 );
120 }
121 // Profile.
122 if (user_access('access user profiles')) {
123 $links['profile'] = array(
124 'title' => t('Profile'),
125 'href' => 'user/'. $uid,
126 );
127 }
128 if ($user->uid && $user->uid != $uid) {
129 /* Buddylist actions */
130 $is_buddy = db_result(db_query('SELECT COUNT(buddy) FROM {buddylist} WHERE uid = %d AND buddy = %d', $user->uid, $account->uid));
131 $is_requested = db_result(db_query('SELECT COUNT(requestee_uid) FROM {buddylist_pending_requests} WHERE requester_uid = %d AND requestee_uid = %d', $user->uid, $account->uid));
132 if (!$is_buddy && !$is_requested) {
133 $links['friend-add'] = array(
134 'title' => t('Add as friend'),
135 'href' => 'buddy/add/'. $uid,
136 );
137 $links['friend-add'] += $destination;
138 }
139 else if ($is_buddy) {
140 $links['friend-remove'] = array(
141 'title' => t('Remove from friends'),
142 'href' => 'buddy/delete/'. $uid,
143 );
144 $links['friend-remove'] += $destination;
145 }
146 }
147 $output = theme_user_display_links($links);
148
149 print drupal_to_js(array('data' => $output));
150 exit;
151 }
152
153 /**
154 * @} End of "ingroup js_callback".
155 */
156
157 /**
158 * Return a themed set of user display links, invoked by JS callback.
159 *
160 * Fork of theme_links() in core to allow overriding these special links.
161 *
162 * @param $links
163 * A keyed array of links to be themed, structure like theme_links().
164 * @param $attributes
165 * A keyed array of attributes
166 * @return
167 * A string containing an unordered list of links.
168 */
169 function theme_user_display_links($links) {
170 $output = '';
171
172 if (count($links) > 0) {
173 $output = '<ul'. drupal_attributes($attributes) .'>';
174
175 $num_links = count($links);
176 $i = 1;
177
178 foreach ($links as $key => $link) {
179 $class = '';
180
181 // Automatically add a class to each link and also to each LI
182 if (isset($link['attributes']) && isset($link['attributes']['class'])) {
183 $link['attributes']['class'] .= ' ' . $key;
184 $class = $key;
185 }
186 else {
187 $link['attributes']['class'] = $key;
188 $class = $key;
189 }
190
191 // Add first and last classes to the list of links to help out themers.
192 $extra_class = '';
193 if ($i == 1) {
194 $extra_class .= 'first ';
195 }
196 if ($i == $num_links) {
197 $extra_class .= 'last ';
198 }
199 $output .= '<li class="'. $extra_class . $class .'">';
200
201 // Is the title HTML?
202 $html = isset($link['html']) && $link['html'];
203
204 // Initialize fragment and query variables.
205 $link['query'] = isset($link['query']) ? $link['query'] : NULL;
206 $link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;
207
208 if (isset($link['href'])) {
209 $output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
210 }
211 else if ($link['title']) {
212 // Some links are actually not links, but we wrap these in <span> for
213 // adding title and class attributes.
214 if (!$html) {
215 $link['title'] = check_plain($link['title']);
216 }
217 $output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
218 }
219
220 $i++;
221 $output .= "</li>\n";
222 }
223
224 $output .= '</ul>';
225 }
226
227 return $output;
228 }
229
230 /**
231 * @ingroup user_display_api
232 * @{
233 */
234
235 /**
236 * Implementation of hook_user_display().
237 */
238 function user_display_links_user_display($op) {
239 switch ($op) {
240 case 'elements':
241 $items = array(
242 'user_display_links' => array(
243 '#type' => 'checkboxes',
244 '#title' => t('Display user related links'),
245 '#options' => array(),
246 '#default_value' => 0,
247 '#category' => 'links',
248 ),
249 );
250 return $items;
251 }
252 }
253
254 function user_display_links($account, $style) {
255 if ($account->uid) {
256 return array(
257 'class' => array('links', $account->uid),
258 );
259 }
260 }
261
262 /**
263 * Implementation of hook_user_links().
264 */
265 function user_display_links_user_links($op, $uid = 0) {
266 global $user;
267 switch ($op) {
268 case 'info':
269 return array(
270 'profile-view' => t('Visit profile'),
271 'profile-edit' => t('Edit profile'),
272 );
273
274 case 'links':
275 $links = array();
276 if ($user->uid != $uid) {
277 $links['profile-view'] = l(t('Profile'), "user/$uid");
278 }
279 else {
280 $links['profile-edit'] = l(t('Edit my profile'), "user/$uid/edit");
281 }
282 return $links;
283 }
284 }
285
286 /**
287 * @} End of "ingroup user_display_api".
288 */
289

  ViewVC Help
Powered by ViewVC 1.1.2