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