added buddylist argument handlers to the views integration, that allow to list userno...
[project/buddylist.git] / buddylist_views.inc
1 <?php
2 // $Id$
3
4 /**
5 * This include file implements views functionality on behalf of the
6 * buddylist.module
7 */
8
9 function buddylist_views_tables() {
10 $tables['buddylist'] = array(
11 "name" => "buddylist",
12 "join" => array(
13 "left" => array(
14 "table" => "users",
15 "field" => "uid",
16 ),
17 "right" => array(
18 "field" => "buddy",
19 ),
20 ),
21 );
22
23 $tables['buddylist_users'] = array(
24 'name' => 'users',
25 'join' => array(
26 'left' => array(
27 'table' => 'buddylist',
28 'field' => 'uid'
29 ),
30 'right' => array(
31 'field' => 'uid'
32 ),
33 ),
34 'fields' => array(
35 'name' => array(
36 'name' => t('Buddylist: User Name'),
37 'handler' => 'views_handler_field_username',
38 'sortable' => true,
39 'uid' => 'uid',
40 'addlfields' => array('uid'),
41 'help' => t('This will display the username of the owner of the buddylist.'),
42 ),
43 'uid' => array(
44 'name' => t('Buddylist: User Picture'),
45 'handler' => 'views_handler_field_userpic',
46 'sortable' => false,
47 'help' => t('This will display the user picture of the owner of the buddylist.'),
48 ),
49 ),
50 'sorts' => array(
51 'name' => array(
52 'name' => t('Buddylist: Buddy Name'),
53 'help' => t('This allows you to sort alphabetically by buddy name.'),
54 )
55 ),
56 'filters' => array(
57 'uid' => array(
58 'name' => t('Buddylist: Author Is A Buddy Of Username'),
59 'operator' => 'views_handler_operator_or',
60 'list' => 'views_handler_filter_username',
61 'value-type' => 'array',
62 'help' => t('This allows you to filter by buddies of a particular user.'),
63 ),
64 'currentuid' => array(
65 'field' => 'uid',
66 'name' => t('Buddylist: Author Is A Buddy Of Current User'),
67 'operator' => 'views_handler_operator_eqneq',
68 'list' => 'views_handler_filter_usercurrent',
69 'help' => t('This allows you to filter by buddies of the current user.'),
70 'cacheable' => 'no',
71 ),
72 ),
73 );
74 if (module_exists('usernode')) {
75 $tables['usernode_buddylist'] = array(
76 "name" => "buddylist",
77 "join" => array(
78 "left" => array(
79 "table" => "usernode",
80 "field" => "uid",
81 ),
82 "right" => array(
83 "field" => "buddy",
84 ),
85 ),
86 'filters' => array(
87 'uid' => array(
88 'name' => t('Buddylist: Usernode Is A Buddy Of Username'),
89 'operator' => 'views_handler_operator_or',
90 'list' => 'views_handler_filter_username',
91 'value-type' => 'array',
92 'help' => t('This allows you to filter by buddies of a particular user.'),
93 ),
94 'currentuid' => array(
95 'field' => 'uid',
96 'name' => t('Buddylist: Usernode Is A Buddy Of Current User'),
97 'operator' => 'views_handler_operator_eqneq',
98 'list' => 'views_handler_filter_usercurrent',
99 'help' => t('This allows you to filter by buddies of the current user.'),
100 ),
101 ),
102 );
103 $tables['usernode_buddylist_buddyof'] = array(
104 "name" => "buddylist",
105 "join" => array(
106 "left" => array(
107 "table" => "usernode",
108 "field" => "uid",
109 ),
110 "right" => array(
111 "field" => "uid",
112 ),
113 ),
114 'filters' => array(
115 'uid' => array(
116 'name' => t('Buddylist: Username Is A Buddy Of Usernode'),
117 'operator' => 'views_handler_operator_or',
118 'list' => 'views_handler_filter_username',
119 'value-type' => 'array',
120 'help' => t('This allows you to filter by buddies of a particular user.'),
121 ),
122 'currentuid' => array(
123 'field' => 'uid',
124 'name' => t('Buddylist: Current User Is A Buddy Of Usernode'),
125 'operator' => 'views_handler_operator_eqneq',
126 'list' => 'views_handler_filter_usercurrent',
127 'help' => t('This allows you to filter by buddies of the current user.'),
128 ),
129 ),
130 );
131 $tables['usernode_buddylist_buddyof'] = array(
132 "name" => "buddylist",
133 "join" => array(
134 "left" => array(
135 "table" => "usernode",
136 "field" => "uid",
137 ),
138 "right" => array(
139 "field" => "uid",
140 ),
141 ),
142 'filters' => array(
143 'buddy' => array(
144 'name' => t('Buddylist: Username Is A Buddy Of Usernode'),
145 'operator' => 'views_handler_operator_or',
146 'list' => 'views_handler_filter_username',
147 'value-type' => 'array',
148 'help' => t('This allows you to filter by buddies of a particular user.'),
149 ),
150 'currentuid' => array(
151 'field' => 'buddy',
152 'name' => t('Buddylist: Current User Is A Buddy Of Usernode'),
153 'operator' => 'views_handler_operator_eqneq',
154 'list' => 'views_handler_filter_usercurrent',
155 'help' => t('This allows you to filter by buddies of the current user.'),
156 ),
157 ),
158 );
159 }
160 return($tables);
161 }
162
163 function buddylist_views_default_views() {
164 $view = new stdClass();
165 $view->name = 'buddylist_posts';
166 $view->description = 'A list of nodes created by buddies of current user';
167 $view->access = array ();
168 $view->view_args_php = '';
169 $view->page = TRUE;
170 $view->page_title = 'Buddylist Content';
171 $view->page_header = 'Nodes authored by buddies of current user<br />';
172 $view->page_footer = '';
173 $view->page_empty = '';
174 $view->page_type = 'table';
175 $view->url = 'buddies/posts';
176 $view->use_pager = TRUE;
177 $view->nodes_per_page = '10';
178 $view->sort = array (
179 array (
180 'tablename' => 'node',
181 'field' => 'created',
182 'sortorder' => 'DESC',
183 'options' => '',
184 ),
185 );
186 $view->argument = array (
187 );
188 $view->field = array (
189 array (
190 'tablename' => 'users',
191 'field' => 'name',
192 'label' => 'Buddy',
193 ),
194 array (
195 'tablename' => 'node',
196 'field' => 'title',
197 'label' => 'Node Title',
198 'handler' => 'views_handler_field_nodelink',
199 ),
200 array (
201 'tablename' => 'node',
202 'field' => 'type',
203 'label' => 'Node Type',
204 ),
205 );
206 $view->filter = array (
207 array (
208 'tablename' => 'node',
209 'field' => 'status',
210 'operator' => '=',
211 'options' => '',
212 'value' => '1',
213 ),
214 array (
215 'tablename' => 'buddylist_users',
216 'field' => 'currentuid',
217 'operator' => '=',
218 'options' => '',
219 'value' => '***CURRENT_USER***',
220 ),
221 );
222 $view->exposed_filter = array (
223 );
224 $view->requires = array(node, users, buddylist_users);
225 $views[$view->name] = $view;
226
227
228 if (module_exists('usernode')) {
229 $view = new stdClass();
230 $view->name = 'buddylist';
231 $view->description = 'Buddies of the current user';
232 $view->access = array();
233 $view->view_args_php = '';
234 $view->page = TRUE;
235 $view->page_title = 'Buddylist';
236 $view->page_header = 'Buddies of the current user<br />';
237 $view->page_type = 'table';
238 $view->url = 'buddies';
239 $view->use_pager = TRUE;
240 $view->nodes_per_page = '10';
241 $view->sort = array (
242 array (
243 'tablename' => 'node',
244 'field' => 'created',
245 'sortorder' => 'DESC',
246 'options' => 'normal',
247 ),
248 );
249 $view->field = array (
250 array (
251 'tablename' => 'node',
252 'field' => 'title',
253 'label' => 'Name',
254 'handler' => 'views_handler_field_nodelink',
255 'options' => 'link',
256 ),
257 );
258 $view->filter = array (
259 array (
260 'tablename' => 'node',
261 'field' => 'status',
262 'operator' => '=',
263 'options' => '',
264 'value' => '1',
265 ),
266 array (
267 'tablename' => 'usernode_buddylist',
268 'field' => 'currentuid',
269 'operator' => '=',
270 'options' => '',
271 'value' => '***CURRENT_USER***',
272 ),
273 array (
274 'tablename' => 'node',
275 'field' => 'type',
276 'operator' => 'OR',
277 'options' => '',
278 'value' => array (0 => 'usernode'),
279 ),
280 );
281 $view->exposed_filter = array (
282 );
283 $view->requires = array(node, usernode_buddylist);
284 $views[$view->name] = $view;
285 }
286
287 return($views);
288 }
289
290
291 /**
292 * Implementation of hook_views_arguments
293 */
294 function buddylist_views_arguments() {
295 if (module_exists('usernode')) {
296 $arguments = array(
297 'buddylist_buddies' => array(
298 'name' => t('Buddylist: Usernode is buddy of UID'),
299 'handler' => 'buddylist_views_handler_arg_buddies',
300 'help' => t('This will filter usernodes from users, which are buddies of the user with the ID passed to the view as argument.'),
301 ),
302 'buddylist_buddies_buddyof' => array(
303 'name' => t('Buddylist: UID is buddy of Usernode'),
304 'handler' => 'buddylist_views_handler_arg_buddies_buddyof',
305 'help' => t('This will filter usernodes from users, of which the user with the ID passed to the view as argument is a buddy of.'),
306 ),
307 );
308 return $arguments;
309 }
310 }
311
312 /**
313 * Callback for buddylist_views_arguments
314 */
315 function buddylist_views_handler_arg_buddies($op, &$query, $argtype, $arg = '') {
316 switch($op) {
317 case 'summary':
318 $table_data = _views_get_tables();
319 $joininfo = $table_data['usernode_buddylist_buddyof']['join'];
320 $joininfo['type'] = 'inner';
321 $query->add_table("usernode_buddylist_buddyof", true, 1, $joininfo);
322
323 $query->add_table('users', true);
324 $query->add_field('name', 'users');
325 $query->add_field('uid', 'users');
326 $fieldinfo['field'] = "users.name";
327 return $fieldinfo;
328 break;
329 case 'sort':
330 $query->add_orderby('users', 'name', $argtype);
331 break;
332 case 'filter':
333 $uid = intval($arg);
334 $table_data = _views_get_tables();
335 $joininfo = $table_data['usernode_buddylist']['join'];
336 $joininfo['type'] = 'inner';
337 $joininfo['extra'] = array('uid' => $uid);
338 $query->add_table("usernode_buddylist", true, 1, $joininfo);
339 break;
340 case 'link':
341 $name = ($query->name ? $query->name : variable_get('anonymous', 'Anonymous'));
342 return l($name, "$arg/" . intval($query->uid));
343 case 'title':
344 if (!$query) {
345 return variable_get('anonymous', 'Anonymous');
346 }
347 $user = db_fetch_object(db_query("SELECT name FROM {users} WHERE uid = '%d'", $query));
348 return check_plain($user->name);
349 }
350 }
351
352
353 /**
354 * Callback for buddylist_views_arguments
355 */
356 function buddylist_views_handler_arg_buddies_buddyof($op, &$query, $argtype, $arg = '') {
357 switch($op) {
358 case 'summary':
359 $table_data = _views_get_tables();
360 $joininfo = $table_data['usernode_buddylist']['join'];
361 $joininfo['type'] = 'inner';
362 $query->add_table("usernode_buddylist", true, 1, $joininfo);
363
364 $query->add_table('users', true);
365 $query->add_field('name', 'users');
366 $query->add_field('uid', 'users');
367 $fieldinfo['field'] = "users.name";
368 return $fieldinfo;
369 break;
370 case 'sort':
371 $query->add_orderby('users', 'name', $argtype);
372 break;
373 case 'filter':
374 $uid = intval($arg);
375 $table_data = _views_get_tables();
376 $joininfo = $table_data['usernode_buddylist_buddyof']['join'];
377 $joininfo['type'] = 'inner';
378 $joininfo['extra'] = array('buddy' => $uid);
379 $query->add_table("usernode_buddylist_buddyof", true, 1, $joininfo);
380 break;
381 case 'link':
382 $name = ($query->name ? $query->name : variable_get('anonymous', 'Anonymous'));
383 return l($name, "$arg/" . intval($query->uid));
384 case 'title':
385 if (!$query) {
386 return variable_get('anonymous', 'Anonymous');
387 }
388 $user = db_fetch_object(db_query("SELECT name FROM {users} WHERE uid = '%d'", $query));
389 return check_plain($user->name);
390 }
391 }