/[drupal]/contributions/sandbox/jjeff/groupblog/groupblog.module
ViewVC logotype

Contents of /contributions/sandbox/jjeff/groupblog/groupblog.module

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


Revision 1.1 - (show annotations) (download) (as text)
Wed Sep 7 04:54:20 2005 UTC (4 years, 2 months ago) by jjeff
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-php
Initial commit of groupblog.module. Group Blogs are a bit like forum containers for blogs. The group blog administrator can add users to group blogs. These users may then assign their blog posts to this group blog. Group Blogs entries may then be viewed together on a single page.
1 <?php
2
3 // single or multiple selects for group blogs? [single for now]
4 //
5
6 // to do:
7 // _ create emulations of term add/edit/create pages rather than sending off to taxonomy
8 // _ remind folks that they need taxonomy installed
9 // _
10
11 /**
12 * Implementation of hook_help()
13 *
14 */
15
16 function groupblog_help($section) {
17 switch ($section) {
18 case 'admin/modules#description':
19 // ummm... a little better description please?
20 return t('Allows users to post to a group blog.');
21 break;
22 }
23 }
24
25 /**
26 * Implementation of hook_menu()
27 *
28 */
29
30 function groupblog_menu($may_cache) {
31
32 $items = array();
33 if ($may_cache) {
34 $items[] = array('path' => 'groupblog', 'title' => t('group blogs'),
35 'access' => user_access('access content'), 'callback' => 'groupblog_page');
36 $items[] = array('path' => 'admin/groupblog', 'title' => t('group blogs'),
37 'callback' => 'groupblog_setup_page', 'access' => user_access('manage group blogs')
38 );
39 $items[] = array('path' => 'admin/groupblog/manage',
40 'title' => t('manage group blog'), 'access' => user_access('manage group blogs'),
41 'callback' => 'groupblog_manage_group', 'type' => MENU_CALLBACK
42 );
43 $items[] = array('path' => 'admin/groupblog/autocomplete',
44 'callback' => 'groupblog_user_autocomplete',
45 'access' => user_access('administer users'),
46 'type' => MENU_CALLBACK
47 );
48
49 }
50 else {
51 // reroute requests for taxonomy terms that are groupblog terms
52 // is there a better way to do this? perhaps with the path system?
53 /*
54 if (arg(0) == 'taxonomy' && arg(1) == 'term') {
55 if (key_exists(arg(2), groupblog_options())) {
56 drupal_goto('groupblog/'.arg(2));
57 }
58 }
59 */
60 }
61 return $items;
62 }
63
64 /**
65 * Implementation of hook_perm
66 *
67 */
68
69 function groupblog_perm() {
70 return array('manage group blogs');
71 }
72
73 // the callback for the settings page
74
75 function groupblog_setup_page() {
76 $vid = _groupblog_get_vid();
77
78 $output = t('<p>Put documentation here. Use related terms for indirect assignment.</p>');
79
80 foreach((array)taxonomy_get_tree($vid, 0, -1, 1) as $item) {
81
82 $users = groupblog_get_users($item->tid);
83 if (count($users) > 6) {
84 $users = t('%c users', array('%c' => count($users)));
85 }
86 else {
87 if (count($users)) {
88 $users = t('users: ').implode(', ', $users);
89 }
90 else {
91 $users = t('no users have been assigned to this group');
92 }
93 }
94
95 $rows[$item->tid]['name'] = '<div>'.$item->name.'</div>';
96 $rows[$item->tid]['name'] .= $item->description ? '<div style="font-size:x-small">'.$item->description.'</div>' : '';
97 $rows[$item->tid]['name'] .= '<div style="font-size:xx-small">' . $users . '</div>';
98 $rows[$item->tid]['ops']['data'] = l('edit', 'admin/taxonomy/edit/term/'.$item->tid, array('title'=>t('change name or description of this group')), drupal_get_destination());
99 $rows[$item->tid]['ops']['data'] .= ' '.l('manage', 'admin/groupblog/manage/'.$item->tid, array('title'=>t('manage users')));
100 $rows[$item->tid]['ops']['valign'] = 'top';
101 }
102 $header = array(t('Group Blog'), '');
103 if ($rows) {
104 $table = theme('table', $header, $rows);
105 $output .= $table;
106 $output .= '<p>'.l(t('add another group blog'), 'admin/taxonomy/add/term/'.$vid, array(), drupal_get_destination()).'</p>';
107 }
108 else {
109 $output .= t('<p>No group blogs currently exist. <a href="%link">Create a group blog</a>.</p>', array('%link' => url('admin/taxonomy/add/term/'.$vid, drupal_get_destination())));
110 }
111 print theme('page', $output);
112 }
113
114 function groupblog_manage_group($tid) {
115 //are we in the "delete" submenu?
116 if (arg(4) == 'delete') {
117 if ($_POST['edit']['confirm']) {
118 groupblog_delete_user($uid, $tid);
119 drupal_goto('admin/groupblog/manage/'.$tid);
120 }
121 else {
122 $user = user_load(array('uid' => arg(5)));
123 $group = taxonomy_get_term(arg(3));
124 return theme('confirm', t('Are you sure you want to delete %uname from %gblog', array('%uname' => $user->name, '%gblog' => $group->name)), 'admin/groupblog/manage/'.$tid);
125 }
126 }
127 if ($_POST['op'] == t('Add User')) {
128 $edit = $_POST['edit'];
129 groupblog_user_validate($edit);
130 if (!form_get_errors()) {
131 groupblog_save_user($edit);
132 }
133 }
134 if (!array_key_exists($tid, groupblog_options())) {
135 drupal_goto('admin/groupblog');
136 }
137 else {
138 $users = groupblog_get_users($tid);
139 $group = taxonomy_get_term($tid);
140 $uaccess = user_access('access user profiles');
141 drupal_set_title(t('Manage Group Blog: %name', array('%name' => $group->name)));
142 $header = array(t('username'), t('id'), '&nbsp;');
143 foreach($users as $uid => $uname) {
144 $row[$uid]['uname'] = $uname;
145 $row[$uid]['id'] = $uaccess ? l($uid, 'user/'.$uid) : $uid;
146 $row[$uid]['ops'] = l(t('delete'), 'admin/groupblog/manage/'.$tid.'/delete/'.$uid);
147 }
148 $table = theme('table', $header, $row);
149 $output .= $table;
150
151 $form = t('<h2>Add a user</h2>');
152 $form .= form_hidden('tid', $tid);
153 $form .= form_autocomplete(t('username, email, or uid'), 'username', $edit['username'], 25, 120, 'admin/groupblog/autocomplete', NULL, NULL, TRUE);
154 $form .= form_submit(t('Add User'));
155 $output .= form($form);
156 return $output;
157 }
158 return;
159 }
160
161
162
163 // hook_nodeapi
164
165 function groupblog_nodeapi(&$node, $op, $teaser, $page) {
166 if ($node->type == 'blog') {
167 switch ($op) {
168 case 'form pre':
169 if (count($options = groupblog_options($node->uid))) {
170 // get the groupblog that this node is assigned to
171 $value = _groupblog_node_get_tids($node);
172 $options = array('' => '<'. t('none') .'>') + $options;
173 // multiple select option?
174 return form_select(t('Group Blog'), 'groupblogs', $edit['groupblog'] ? $edit['groupblog'] : $value, $options);
175 }
176 case 'validate':
177 $node->taxonomy = array_merge($node->taxonomy, $node->groupblogs);
178 break;
179 case 'load':
180 $node->groupblogs = _groupblog_node_get_tids($node, TRUE);
181 break;
182 }
183 }
184 }
185
186
187 // what group blogs can this user post to?
188
189 function groupblog_user_access($uid = NULL) {
190 if (!$uid) {
191 global $user;
192 $uid = $user->uid;
193 }
194 $tids = array();
195 $result = db_query('SELECT tid FROM {groupblog_users} WHERE uid = %d', $uid);
196 while ($r = db_fetch_object($result)) {
197 $tids[] = $r->tid;
198 }
199 return $tids;
200 }
201
202
203 // get list of users for a certain blog
204
205 function groupblog_get_users($tid) {
206 $result = db_query('SELECT gb.*, u.name FROM {groupblog_users} gb INNER JOIN {users} u ON gb.uid = u.uid WHERE gb.tid = %d', $tid);
207 while ($r = db_fetch_object($result)) {
208 $users[$r->uid] = $r->name;
209 }
210 return (array)$users;
211 }
212
213 // figure out if the given criteria matches a user
214
215 function groupblog_user_validate(&$edit) {
216 $identifier = $edit['username'];
217 // maybe a better/clearer way to do this cascade with switch?
218 // existing username?
219 if (!$user = user_load(array('name'=>$identifier))) {
220 // existing mail?
221 if (!$user = user_load(array('mail'=>$identifier))) {
222 // uid?
223 if (!$user = user_load(array('uid'=>$identifier))) {
224 form_set_error('username', t('Could not find a user with this information.'));
225 return FALSE;
226 }
227 }
228 }
229 $edit['uid'] = $user->uid;
230 return TRUE;
231 }
232
233
234
235 // Add a user to a group blog
236
237 function groupblog_save_user(&$edit) {
238 // check to see if they're already in this group
239 if (db_result(db_query('SELECT uid FROM {groupblog_users} WHERE uid = %d AND tid = %d', $edit['uid'], $edit['tid']))) {
240 drupal_set_message(t('User is already assigned to this group blog.'));
241 }
242 else {
243 db_query('INSERT INTO {groupblog_users} (uid, tid) VALUES (%d, %d)', $edit['uid'], $edit['tid']);
244 drupal_set_message(t('User assigned to group.'));
245 }
246 unset($edit);
247 }
248
249
250 function groupblog_delete_user($uid, $tid) {
251 db_query('DELETE FROM {groupblog_users} WHERE uid = %d AND tid = %d', $uid, $tid);
252 drupal_set_message(t('User deleted from group.'));
253 }
254
255
256 // returns an array of group blog tids suitable for select $options
257 // if uid is given, will just return options available to specific user
258
259 function groupblog_options($uid = NULL) {
260 if (isset($uid)) {
261 $accesslist = groupblog_user_access($uid);
262 }
263 foreach((array) taxonomy_get_tree(_groupblog_get_vid(), 0, -1, 1) as $item) {
264 if ($accesslist) {
265 if (in_array($item->tid, $accesslist)) {
266 $options[$item->tid] = $item->name;
267 }
268 }
269 else {
270 $options[$item->tid] = $item->name;
271 }
272 }
273 return (array)$options;
274 }
275
276 /**
277 * Returns the vocabulary id for group blogs.
278 */
279 function _groupblog_get_vid() {
280 $vid = variable_get('groupblog_vocabulary', '');
281 if (empty($vid)) {
282 // Check to see if a forum vocabulary exists
283 $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module='%s'", 'groupblog'));
284 if (!$vid) {
285 $description = "--- PLEASE READ ---\nDo NOT delete this vocabulary or Group Blogs will stop working. ALSO, do NOT enable this vocabulary for ANY node types (including blogs) or Group Blogs will not function correctly. Thank you.";
286 list($status, $object) = array_values(taxonomy_save_vocabulary(array('name' => t('Group Blogs'), 'description' => $description, 'multiple' => 0, 'required' => 0, 'hierarchy' => 0, 'relations' => 1, 'module' => 'groupblog', 'nodes' => array())));
287 if (user_access('manage group blogs')) {
288 drupal_set_message(t('Group Blogs taxonomy vocabulary has been created.'));
289 }
290 $vid = $object['vid'];
291 }
292 variable_set('groupblog_vocabulary', $vid);
293 }
294 return $vid;
295 }
296
297 /**
298 * Implementation of hook_taxonomy
299 * guards against deletion of Group Blogs vocabulary
300 *
301 **/
302
303 function groupblog_taxonomy($op, $type, $obj) {
304 switch ($op) {
305 case 'delete':
306 if ($type == 'vocabulary' && $obj == _groupblog_get_vid()) {
307 // they've deleted the groupblog vocabulary!!!
308 // let's delete the groupblog_vocabulary variable
309 // so that this module doesn't get confused
310 // forum.module really oughtta do this
311 variable_set('groupblog_vocabulary', NULL);
312 }
313 break;
314 case 'insert':
315 case 'update':
316 // if a taxonomy term has been added to the Group Blog vocabulary,
317 // make an alias for it at groupblog/tid
318 if ($type == 'term' && key_exists($obj['tid'], groupblog_options())) {
319 groupblog_set_alias('taxonomy/term/'.$obj['tid'], 'groupblog/'.$obj['tid']);
320 }
321 break;
322 case 'delete':
323 if ($type == 'term' && key_exists($obj['tid'], groupblog_options())) {
324 // is this the right way to remove the alias?
325 groupblog_set_alias('taxonomy/term/'.$obj['tid']);
326 }
327 }
328 }
329
330 // get group blog terms associated with this node
331 // $all - return entire array or just first hit?
332
333 function _groupblog_node_get_tids($node, $all = FALSE) {
334 $terms = taxonomy_node_get_terms_by_vocabulary($node->nid, _groupblog_get_vid());
335 $return = array_reverse($terms);
336 if (!$all) {
337 $return = $return[0];
338 }
339 return $return;
340 }
341
342
343 function groupblog_user_autocomplete($string) {
344 $matches = array();
345 if (!is_numeric($string)) {
346 $result = db_query_range('SELECT name FROM {users} WHERE (LOWER(name) LIKE LOWER("%%%s%%")) ORDER BY LENGTH(name)', $string, 0, 40);
347 }
348 else {
349 $result = db_query_range('SELECT name FROM {users} WHERE (uid LIKE "%%%d%%") ORDER BY LENGTH(name), LENGTH(mail)', $string, $string, 0, 40);
350 }
351 while ($user = db_fetch_object($result)) {
352 $matches[$user->name] = check_plain($user->name);
353 }
354 print drupal_implode_autocomplete($matches);
355 exit();
356 }
357
358 /**
359 * Generate a form element for selecting group blog.
360 * (modifications of taxonomy_form)
361 */
362 function groupblog_form($value = 0, $help = NULL, $name = 'taxonomy') {
363 $vocabulary = taxonomy_get_vocabulary(_groupblog_get_vid());
364 $help = ($help) ? $help : $vocabulary->help;
365 if ($vocabulary->required) {
366 $blank = 0;
367 }
368 else {
369 $blank = '<'. t('none') .'>';
370 }
371
372 return _taxonomy_term_select(check_plain($vocabulary->name), $name, $value, $vid, $help, intval($vocabulary->multiple), $blank);
373 }
374
375
376 //-----------------------------------------------------------
377 // page stuff
378
379 /*
380 This is all handled by taxonomy (using path aliasing) now...
381
382
383 function groupblog_page($tid = NULL, $op = 'page') {
384 //$crumb = drupal_get_breadcrumb();
385 //drupal_set_breadcrumb($crumb + array('groupblog' => 'Group Blogs'));
386 $groups = groupblog_options();
387 if (!$tid || !array_key_exists($tid, $groups)) {
388 // if there's no tid or tid is not within the vocabulary
389 // give a themed listing of the group blogs
390 drupal_set_title(t('Group Blogs'));
391 return(theme('list_groupblogs'));
392 }
393 else {
394 $title = drupal_set_title($groups[$tid]);
395 switch ($op) {
396 case 'page':
397 $breadcrumbs = drupal_get_breadcrumb();
398 drupal_add_link(array('rel' => 'alternate',
399 'type' => 'application/rss+xml',
400 'title' => 'RSS - '. $title,
401 'href' => url('groupblog/'. $tid .'/feed')));
402 $tids = array($tid);
403 $output = taxonomy_render_nodes(taxonomy_select_nodes($tids));
404 $output .= theme('xml_icon', url("groupblog/$tid/feed"));
405 return $output;
406 break;
407
408 case 'feed':
409 $term = taxonomy_get_term($tid);
410 $channel['link'] = url('groupblog/'. $tid, NULL, NULL, TRUE);
411 $channel['title'] = variable_get('site_name', 'drupal') .' - '. $title;
412 $channel['description'] = $term->description;
413
414 $tids = array($tid);
415 $result = taxonomy_select_nodes($tids);
416 node_feed($result, $channel);
417 break;
418 default:
419 drupal_not_found();
420 }
421 }
422 }
423
424 */
425
426 function groupblog_page() {
427 drupal_set_title(t('Group Blogs'));
428 return(theme('list_groupblogs'));
429 }
430
431
432
433 function theme_list_groupblogs() {
434 foreach((array)taxonomy_get_tree(_groupblog_get_vid(), 0, -1, 1) as $item) {
435 $output .= "<h2>".l($item->name, 'groupblog/'.$item->tid)."</h2>";
436 $output .= $item->description ? "<p>$item->description</p>" : '';
437 }
438 return $output;
439 }
440
441 // stolen from path_set_alias()
442
443 function groupblog_set_alias($path = NULL, $alias = NULL, $pid = NULL) {
444 if ($path && !$alias) {
445 db_query("DELETE FROM {url_alias} WHERE src = '%s'", $path);
446 drupal_clear_path_cache();
447 }
448 else if (!$path && $alias) {
449 db_query("DELETE FROM {url_alias} WHERE dst = '%s'", $alias);
450 drupal_clear_path_cache();
451 }
452 else if ($path && $alias) {
453 $path_count = db_result(db_query("SELECT COUNT(src) FROM {url_alias} WHERE src = '%s'", $path));
454 $alias_count = db_result(db_query("SELECT COUNT(dst) FROM {url_alias} WHERE dst = '%s'", $alias));
455
456 // We have an insert:
457 if ($path_count == 0 && $alias_count == 0) {
458 db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $path, $alias);
459 drupal_clear_path_cache();
460 }
461 else if ($path_count >= 1 && $alias_count == 0) {
462 if ($pid) {
463 db_query("UPDATE {url_alias} SET dst = '%s', src = '%s' WHERE pid = %d", $alias, $path, $pid);
464 }
465 else {
466 db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", $path, $alias);
467 }
468 drupal_clear_path_cache();
469 }
470 else if ($path_count == 0 && $alias_count == 1) {
471 db_query("UPDATE {url_alias} SET src = '%s' WHERE dst = '%s'", $path, $alias);
472 drupal_clear_path_cache();
473 }
474 else if ($path_count == 1 && $alias_count == 1) {
475 // This will delete the path that alias was originally pointing to:
476 groupblog_set_alias(NULL, $alias);
477 groupblog_set_alias($path);
478 groupblog_set_alias($path, $alias);
479 }
480 }
481 }
482
483
484 ?>

  ViewVC Help
Powered by ViewVC 1.1.2