00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 define('PRIVATE_DEFAULT', 0);
00016
00017
00018
00019
00020 define('PRIVATE_BY_GROUP_OWNER', 1);
00021
00022
00023
00024
00025 define('PUBLIC_AUTO', 2);
00026
00027
00028
00029
00030 define('PUBLIC_BY_GROUP_OWNER', 3);
00031
00032
00033
00034
00035 function og_forums_help($path, $arg) {
00036 switch ($path) {
00037 case 'admin/modules#description':
00038 return t('Ties forums to organic groups.');
00039 case 'admin/content/taxonomy':
00040 return t('Managing OG forums is not supported here. Please use the <a href="@administer-forums-url">@administer-forums</a>.', array('@administer-forums-url' => url('admin/content/forum'), '@administer-forums' => t('Forum administration')));
00041 case 'admin/content/forum':
00042 $message = t('<h3>Organic groups forums</h3>
00043 <p>The drag-and-drop feature is not fully supported. Groups forums can be moved only within a container. Do not move a group forum from one container to another container!</p>');
00044 return $message;
00045 case 'admin/og/og_forums':
00046 $message = '<p>'. t('Do not change the <em>Groups forums container</em> configuration if there are already groups forums - especially on a live site. This is a warning.').'</p>';
00047 return $message;
00048 case 'admin/help#og_forums':
00049 $module_path = drupal_get_path('module', 'og_forums');
00050 return '<p>'. t('The included module documentation contains <a href="@help-url">@help</a>.', array('@help-url' => url($module_path .'/docs/html/index.html'), '@help' => t('the help'))) .'</p>';
00051 break;
00052 }
00053 }
00054
00055
00056
00057
00058 function og_forums_perm() {
00059 return array('make forums public', 'delete own groups forums', 'admin own groups forums');
00060 }
00061
00062
00063
00064
00065 function og_forums_menu() {
00066 $items = array();
00067
00068
00069
00070
00071
00072 $items['admin/og/og_forums'] = array(
00073 'title' => 'Organic groups forums',
00074 'description' => 'Configure forums for organic groups',
00075 'page callback' => 'drupal_get_form',
00076 'page arguments' => array('og_forums_admin_settings'),
00077 'access arguments' => array('administer site configuration'),
00078 'file' => 'og_forums.admin.inc',
00079 );
00080 $items['og_forums/manage/public/%/%'] = array(
00081 'title' => 'Make forum public',
00082 'page callback' => 'og_forums_public',
00083 'page arguments' => array(3, 4),
00084 'access callback' => 'og_forums_access_privileges_1',
00085 'access arguments' => array(3),
00086 'type' => MENU_CALLBACK,
00087 );
00088 $items['og_forums/manage/private/%/%'] = array(
00089 'title' => 'Make forum private',
00090 'page callback' => 'og_forums_private',
00091 'page arguments' => array(3, 4),
00092 'access callback' => 'og_forums_access_privileges_1',
00093 'access arguments' => array(3),
00094 'type' => MENU_CALLBACK,
00095 );
00096 $items['og_forums/manage/reset/%/%'] = array(
00097 'title' => 'Reset forum publicity',
00098 'page callback' => 'og_forums_reset',
00099 'page arguments' => array(3, 4),
00100 'access callback' => 'og_forums_access_privileges_1',
00101 'access arguments' => array(3),
00102 'type' => MENU_CALLBACK,
00103 );
00104
00105 $items['og_forums/manage/%'] = array(
00106 'title' => 'Manage groups forums',
00107 'page callback' => 'og_forums_manage',
00108 'page arguments' => array(2),
00109 'access callback' => 'og_forums_access_privileges_2',
00110 'access arguments' => array(2),
00111 'type' => MENU_CALLBACK,
00112 );
00113 $items['og_forums/manage/add/%/%'] = array(
00114 'title' => 'Add forum',
00115 'page callback' => 'og_forums_form_main',
00116 'page arguments' => array(2, 3, 4),
00117 'access callback' => 'og_forums_access_privileges_3',
00118 'access arguments' => array(3, 4),
00119 'type' => MENU_CALLBACK,
00120 );
00121 $items['og_forums/manage/edit/%/%'] = array(
00122 'title' => 'Edit forum',
00123 'page callback' => 'og_forums_form_main',
00124 'page arguments' => array(2, 3, 4),
00125 'access callback' => 'og_forums_access_privileges_2',
00126 'access arguments' => array(3),
00127 'type' => MENU_CALLBACK,
00128 );
00129 $items['node/%og_forums_group_type/forum'] = array(
00130 'title' => 'Forums',
00131 'page callback' => 'drupal_goto',
00132 'page arguments' => array(1),
00133 'access callback' => TRUE,
00134 'type' => MENU_LOCAL_TASK
00135 );
00136
00137 return $items;
00138 }
00139
00140
00141
00142
00143 function og_forums_menu_alter(&$callbacks) {
00144 $callbacks['forum']['access callback'] = 'og_forums_access_forum_page';
00145 $callbacks['forum']['access arguments'] = array(1);
00146
00147 $callbacks['node/%node/edit']['access callback'] = 'node_access';
00148 $callbacks['node/%node/edit']['access arguments'] = array('update', 1);
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 function og_forums_form_alter(&$form, &$form_state, $form_id) {
00160
00161 if ($form_id == 'forum_node_form') {
00162 global $user;
00163
00164 $group = og_get_group_context();
00165
00166
00167 if (empty($group)) {
00168 if (arg(0) == 'node' && arg(2) == 'edit' && is_numeric(arg(1))) {
00169 $sql = "SELECT tid FROM {forum} WHERE nid = %d";
00170 $result = db_fetch_object(db_query($sql, arg(1)));
00171 og_forums_set_og_group_context_from_tid($result->tid);
00172 $group = og_get_group_context();
00173 }
00174 }
00175
00176 if (isset($group->nid)) {
00177 $forum_containers = variable_get('forum_containers', array());
00178
00179 $results = db_query('SELECT ot.tid, th.parent FROM {og_terms} ot LEFT JOIN {term_hierarchy} th ON ot.tid = th.tid WHERE nid = %d ORDER BY tid ASC', $group->nid);
00180
00181 while ($result = db_fetch_object($results)) {
00182 $all_results[$result->tid] = $result->parent;
00183 }
00184
00185 $vid = og_forums_get_vid();
00186
00187 if (arg(1) == 'add' && arg(3)) {
00188 $term = arg(3);
00189 $form['taxonomy'][$vid]['#default_value'] = array($term);
00190 }
00191 elseif (arg(2) == 'edit') {
00192 $term = $form['taxonomy'][$vid]['#default_value'][0];
00193 }
00194 elseif (arg(1) == 'add' && !arg(3)) {
00195 $first = reset($all_results);
00196 if ($first == 0) {
00197 next($all_results);
00198 }
00199 $term = key($all_results);
00200 $form['taxonomy'][$vid]['#default_value'] = array($term);
00201 }
00202
00203 foreach ($form['taxonomy'][$vid]['#options'] as $key => $the_rest) {
00204 if ($key === '') {
00205 continue;
00206 }
00207 reset($the_rest->option);
00208 $option_key = key($the_rest->option);
00209 if (is_array($all_results) && !array_key_exists($option_key, $all_results)) {
00210 unset($form['taxonomy'][$vid]['#options'][$key]);
00211 }
00212
00213
00214
00215 if (is_array($all_results) && array_key_exists($option_key, $all_results) && in_array($option_key, $forum_containers)) {
00216 $form['taxonomy'][$vid]['#options'][$key]->option[$option_key] .= ' '. t('(Container)');
00217 }
00218
00219
00220 if (is_array($all_results) && array_key_exists($option_key, $all_results) && !in_array($option_key, $forum_containers) ) {
00221 if (is_array($user->og_groups)) {
00222 $term_public = og_forums_is_public($option_key);
00223 $term_gid = og_forums_gid_from_tid($option_key);
00224
00225 if ($term_public == FALSE && !array_key_exists($term_gid, $user->og_groups)) {
00226 unset($form['taxonomy'][$vid]['#options'][$key]->option[$option_key]);
00227 }
00228 }
00229 }
00230
00231 }
00232
00233 $form['taxonomy'][$vid]['#description'] = t('Please choose a forum. In a container can not be posted a topic.');
00234
00235
00236 if (og_is_group_post_type($form['#node']->type)) {
00237 $groups = array($gid => $gid);
00238
00239
00240 if (variable_get('og_audience_checkboxes', TRUE)) {
00241 $options = $form['og_nodeapi']['visible']['og_groups']['#options'];
00242 $form['og_nodeapi']['visible']['og_groups_visible'] = array(
00243 '#type' => 'item',
00244 '#title' => t('Audience'),
00245 '#value' => $options[$gid]
00246 );
00247
00248 unset($form['og_nodeapi']['visible']['og_groups']);
00249 }
00250
00251
00252 $form['og_nodeapi']['invisible']['og_groups'] = array(
00253 '#type' => 'value',
00254 '#value' => $groups
00255 );
00256 }
00257 }
00258 else {
00259 $results = db_query('SELECT ot.tid, th.parent FROM {og_terms} ot LEFT JOIN {term_hierarchy} th ON ot.tid = th.tid ORDER BY tid ASC');
00260
00261 while ($result = db_fetch_object($results)) {
00262 $all_results[$result->tid] = $result->parent;
00263 }
00264
00265 $vid = og_forums_get_vid();
00266 $cut = FALSE;
00267
00268 if (arg(1) == 'add' && arg(3)) {
00269 $cut = TRUE;
00270 $term = arg(3);
00271 $form['taxonomy'][$vid]['#default_value'] = array($term);
00272 }
00273 elseif (arg(2) == 'edit') {
00274 $cut = TRUE;
00275 $term = $form['taxonomy'][$vid]['#default_value'][0];
00276 }
00277 if (!user_access('administer forums') && $cut) {
00278 foreach ($form['taxonomy'][$vid]['#options'] as $key => $the_rest) {
00279 if ($key == '') {
00280 continue;
00281 }
00282 reset($the_rest->option);
00283 $option_key = key($the_rest->option);
00284 if (is_array($all_results) && array_key_exists($option_key, $all_results)) {
00285 unset($form['taxonomy'][$vid]['#options'][$key]);
00286 }
00287 }
00288 }
00289
00290
00291 unset($form['og_nodeapi']['visible']['og_groups']);
00292 }
00293 }
00294
00295 if ($form_id == 'node_delete_confirm') {
00296
00297 $form['#submit'][] = 'og_forums_node_delete_confirm_submit';
00298 }
00299
00300 $group_type = variable_get('og_content_type_usage_group', 'group');
00301 if ($form_id == $group_type .'_node_form') {
00302 $form['group_id'] = array(
00303 '#type' => 'value',
00304 '#value' => $form['#node']->nid,
00305 );
00306 }
00307 }
00308
00309
00310
00311
00312
00313
00314
00315
00316 function og_forums_group_type_load($arg) {
00317 $node = node_load($arg);
00318
00319 if (og_is_group_type($node->type)) {
00320 global $user;
00321
00322
00323 $group_user = user_load(array('uid' => $node->uid));
00324 $availability = module_invoke('og_forums', 'get_forums_availability', $node, $group_user);
00325 if ($availability == FALSE) {
00326 return FALSE;
00327 }
00328
00329 $tid = og_forums_get_forum_container($arg);
00330 if ((!empty($user->og_groups) && array_key_exists($arg, $user->og_groups) && og_forums_is_public_all($node->nid)) || user_access('create forum topics')) {
00331 return "forum/$tid";
00332 }
00333 else {
00334 return FALSE;
00335 }
00336 }
00337
00338 return FALSE;
00339 }
00340
00341
00342
00343
00344
00345
00346 function og_forums_block($op = 'list', $delta = 0, $edit = array()) {
00347 if ($group_node = og_get_group_context()) {
00348 $nid = $group_node->nid;
00349 }
00350 switch ($op) {
00351 case 'list':
00352 $blocks[0]['info'] = t('Active forum topics in group');
00353 $blocks[1]['info'] = t('New forum topics in group');
00354 return $blocks;
00355
00356 case 'configure':
00357 $form['og_forums_block_num_'. $delta] = array('#type' => 'select', '#title' => t('Number of topics'), '#default_value' => variable_get('og_forums_block_num_'. $delta, '5'), '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)));
00358 return $form;
00359
00360 case 'save':
00361 variable_set('og_forums_block_num_'. $delta, $edit['og_forums_block_num_'. $delta]);
00362 break;
00363
00364 case 'view':
00365 if (user_access('access content') && $nid) {
00366 switch ($delta) {
00367 case 0:
00368 $title = t('Active group forum topics');
00369 $sql = "SELECT COUNT(*) FROM {node} n
00370 INNER JOIN {node_comment_statistics} l
00371 INNER JOIN {og_terms} ot INNER JOIN {term_node} tn
00372 WHERE ot.nid = $nid AND ot.tid = tn.tid AND tn.nid = n.nid AND n.nid = l.nid AND n.status = 1 AND n.type = 'forum' ORDER BY l.last_comment_timestamp DESC";
00373 $num_results = db_result(db_query($sql, $nid));
00374
00375 if ($num_results) {
00376 $sql = db_rewrite_sql("SELECT n.nid, n.title, l.comment_count, l.last_comment_timestamp FROM {node} n
00377 INNER JOIN {node_comment_statistics} l
00378 INNER JOIN {og_terms} ot
00379 INNER JOIN {term_node} tn
00380 WHERE ot.nid = $nid AND ot.tid = tn.tid AND tn.nid = n.nid AND n.nid = l.nid AND n.status = 1 AND n.type = 'forum' ORDER BY l.last_comment_timestamp DESC");
00381 $result = db_query_range($sql, 0, variable_get('og_forums_block_num_0', '5'));
00382
00383 $content = node_title_list($result);
00384 }
00385 break;
00386
00387 case 1:
00388 $title = t('New group forum topics');
00389 $sql = "SELECT COUNT(*) FROM {node} n
00390 INNER JOIN {node_comment_statistics} l
00391 INNER JOIN {og_terms} ot INNER JOIN {term_node} tn
00392 WHERE ot.nid = $nid AND ot.tid = tn.tid AND tn.nid = n.nid AND n.nid = l.nid AND n.type = 'forum' AND n.status = 1 ORDER BY n.nid DESC";
00393 $num_results = db_result(db_query($sql, $nid));
00394
00395 if ($num_results) {
00396 $sql = db_rewrite_sql("SELECT n.nid, n.title, l.comment_count FROM {node} n
00397 INNER JOIN {node_comment_statistics} l
00398 INNER JOIN {og_terms} ot
00399 INNER JOIN {term_node} tn
00400 WHERE ot.nid = $nid AND ot.tid = tn.tid AND tn.nid = n.nid AND n.nid = l.nid AND n.type = 'forum' AND n.status = 1 ORDER BY n.nid DESC");
00401 $result = db_query_range($sql, 0, variable_get('og_forums_block_num_1', '5'));
00402
00403 $content = node_title_list($result);
00404 }
00405 break;
00406 }
00407
00408 if ($content) {
00409 $forum = og_forums_get_forum_container($nid);
00410 $content .= '<div class="more-link">'. l(t('more'), "forum/$forum") .'</div>';
00411 }
00412
00413 $block['subject'] = $title;
00414 $block['content'] = $content;
00415
00416 return $block;
00417 }
00418 }
00419 }
00420
00421
00422
00423
00424
00425
00426 function og_forums_access_privileges_1($gid) {
00427 $group = node_load($gid);
00428 $allow_public = variable_get('og_forums_allow_public', 0);
00429
00430 return ((og_forums_is_admin($group) && user_access('admin own groups forums') && $allow_public) || user_access('make forums public'));
00431 }
00432
00433
00434
00435
00436
00437
00438 function og_forums_access_privileges_2($gid) {
00439 $group = node_load($gid);
00440
00441 return ((og_forums_is_admin($group) && user_access('admin own groups forums')) || user_access('administer forums'));
00442 }
00443
00444
00445
00446
00447
00448
00449 function og_forums_access_privileges_3($gid, $tid) {
00450 global $user;
00451 $group = node_load($gid);
00452
00453 $add_link_access = variable_get('og_forums_add_link', 1);
00454
00455 return ((og_forums_is_admin($group) && $add_link_access == 1 && user_access('admin own groups forums') && !og_forums_forum_limit_reached($tid)) || user_access('administer forums'));
00456 }
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466 function og_forums_public($gid, $tid) {
00467 $container = og_forums_get_forum_container($gid);
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477 $sql = "UPDATE {og_terms} SET public = %d WHERE tid = %d AND nid = %d";
00478 db_query($sql, PUBLIC_BY_GROUP_OWNER, $tid, $gid);
00479 db_query($sql, PUBLIC_BY_GROUP_OWNER, $container, $gid);
00480
00481 if ((arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'edit' && arg(3) == '') ||
00482 (arg(0) == 'admin' && arg(1) == 'content' && arg(2) == 'forum')) {
00483 return;
00484 }
00485 else {
00486 return drupal_goto("og_forums/manage/$gid");
00487 }
00488 }
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501 function og_forums_private($gid, $tid) {
00502 $container = og_forums_get_forum_container($gid);
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512 $sql = "UPDATE {og_terms} SET public = %d WHERE tid = %d and nid = %d";
00513 db_query($sql, PRIVATE_BY_GROUP_OWNER, $tid, $gid);
00514
00515
00516 $sql2 = "SELECT COUNT(tid) FROM {og_terms} WHERE nid = %d AND tid <> %d AND public > 1";
00517 $results = db_query($sql2, $gid, $container);
00518 $result = db_result($results);
00519
00520
00521 if (!$result) {
00522 db_query($sql, PRIVATE_DEFAULT, $container, $gid);
00523 }
00524
00525 if ((arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'edit' && arg(3) == '') ||
00526 (arg(0) == 'admin' && arg(1) == 'content' && arg(2) == 'forum')) {
00527 return;
00528 }
00529 else {
00530 return drupal_goto("og_forums/manage/$gid");
00531 }
00532 }
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544 function og_forums_reset($gid, $tid) {
00545 $container = og_forums_get_forum_container($gid);
00546
00547 $sql = "UPDATE {og_terms} SET public = %d WHERE tid = %d and nid = %d";
00548 db_query($sql, PRIVATE_DEFAULT, $tid, $gid);
00549
00550
00551 $sql2 = "SELECT COUNT(tid) FROM {og_terms} WHERE nid = %d AND tid <> %d AND public > 1";
00552 $results = db_query($sql2, $gid, $container);
00553 $result = db_result($results);
00554
00555
00556 if (!$result) {
00557 db_query($sql, PRIVATE_DEFAULT, $container, $gid);
00558 }
00559
00560 if ((arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'edit' && arg(3) == '') ||
00561 (arg(0) == 'admin' && arg(1) == 'content' && arg(2) == 'forum')) {
00562 return;
00563 }
00564 else {
00565 return drupal_goto("og_forums/manage/$gid");
00566 }
00567 }
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584 function og_forums_manage($gid) {
00585 $manage['group'] = node_load($gid);
00586 $manage['group_user'] = user_load(array('uid' => $manage['group']->uid));
00587
00588 $manage['forum_count'] = count(og_forums_get_group_forums($manage['group']->nid, 'forum'));
00589
00590 $manage['allow_public'] = variable_get('og_forums_allow_public', 0);
00591 $manage['display_add_link'] = variable_get('og_forums_add_link', 1);
00592 $manage['display_quota_message'] = variable_get('og_forums_quota_message', 1);
00593 $manage['availability_grant'] = variable_get('og_forums_availability_grant', 1);
00594
00595 og_set_theme($manage['group']->nid);
00596 og_set_language($manage['group']);
00597 og_set_group_context($manage['group']);
00598
00599
00600 _og_forums_manage_breadcrumb($manage['group']);
00601
00602 $manage['og_terms'] = _og_forums_ogterms($manage['group']);
00603
00604
00605
00606 if ($manage['availability_grant'] == 1 && $manage['forum_count'] > 0) {
00607 $content = drupal_get_form('og_forums_forum_availability');
00608 }
00609
00610 $manage['availability'] = module_invoke('og_forums', 'get_forums_availability', $manage['group'], $manage['group_user']);
00611
00612 if ($manage['availability'] == FALSE && $manage['availability_grant'] == 1) {
00613 $content .= '';
00614 return $content;
00615 }
00616
00617
00618 if (count($manage['og_terms']) == 0) {
00619 if ((og_forums_is_admin($manage['group']) && user_access('admin own groups forums')) || user_access('administer forums')) {
00620 $edit_path = "node/$gid/edit";
00621 $message = _og_forums_no_forums_message('manager', $edit_path);
00622 }
00623 else {
00624 $user_path = 'user/'. $manage['group']->uid;
00625 $message = _og_forums_no_forums_message('member', $user_path);
00626 }
00627
00628 drupal_set_message($message, 'status');
00629
00630 $content .= '';
00631 }
00632 else {
00633
00634 $manage['default_container'] = variable_get('og_forums_default_container', -1);
00635
00636 $manage['default_container_yn'] = variable_get('og_forums_default_container_yn', TRUE);
00637
00638 if ($manage['forum_count'] > 0) {
00639 $content .= '<p>' .t('This page shows the forums associated with the %group group.', array('%group' => $manage['group']->title)). '</p>';
00640 }
00641 else {
00642 $content .= '<p>' .t('There are no forums for the %group group.', array('%group' => $manage['group']->title)). '</p>';
00643 }
00644
00645 $header = array(t('Name'), t('Operations'));
00646 $sql = "SELECT public, tid FROM {og_terms} WHERE nid = %d";
00647 $results = db_query($sql, $manage['group']->nid);
00648
00649 $manage['all_tids'] = array();
00650 while ($result = db_fetch_object($results)) {
00651 $manage['all_tids'][$result->tid] = $result->public;
00652 }
00653
00654
00655 $first = each($manage['all_tids']);
00656 $manage['check_default'] = og_forums_forum_get_parent($first[0]);
00657
00658
00659 $manage['forum_limit_reached'] = NULL;
00660 if (isset($manage['check_default'])) {
00661 $manage['forum_limit_reached'] = og_forums_forum_limit_reached($manage['group']->nid, 0);
00662 }
00663 else {
00664 $manage['forum_limit_reached'] = og_forums_forum_limit_reached($manage['group']->nid, 0);
00665 }
00666
00667
00668
00669 $manage['group_tid'] = og_forums_tid_from_gid($gid);
00670 $manage['group_tree'] = og_forums_get_tree(og_forums_get_vid(), $manage['group_tid'], 0);
00671
00672 $manage['i'] = 1;
00673 if ($manage['group_tree']) {
00674 foreach ($manage['group_tree'] as $term) {
00675 $manage['term'] = $term;
00676 $manage['term_link'] = l(check_plain($term->name), "forum/$term->tid");
00677
00678 $manage['i']++;
00679
00680
00681 if (in_array($term->tid, variable_get('forum_containers', array())) && !isset($term->default)) {
00682
00683 $rows[] = _og_forums_manage_row($manage, 'container');
00684 $manage['i']--;
00685 }
00686
00687 elseif (!in_array($term->tid, variable_get('forum_containers', array()))) {
00688
00689 $manage['default_container_yn'] = NULL;
00690 $rows[] = _og_forums_manage_row($manage, 'forum');
00691 }
00692 }
00693 }
00694
00695 $content .= theme('table', $header, $rows);
00696 }
00697
00698 return $content;
00699 }
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728 function _og_forums_manage_row($manage, $op) {
00729 global $user;
00730
00731 $public_link = l(t('make public'), 'og_forums/manage/public/'. $manage[group]->nid. '/'. $manage[term]->tid, array('attributes' => array('title' => t('Current Status: Private'))));
00732 $private_link = l(t('make private'), 'og_forums/manage/private/'. $manage[group]->nid .'/'. $manage[term]->tid, array('attributes' => array('title' => t('Current Status: Public'))));
00733 $reset_link = l(t('reset publicity'), 'og_forums/manage/reset/'. $manage[group]->nid .'/'. $manage[term]->tid, array('attributes' => array('title' => t('Reset to default'))));
00734
00735 switch($op) {
00736 case 'container':
00737 if ($manage['i'] > 1) {
00738
00739 if ($manage['forum_limit_reached']) {
00740 if (isset($manage['default_container_yn'])) {
00741
00742 if ($manage['display_quota_message'] == 0) {
00743 $operation = array('data' => '', 'colspan' => '4');
00744 }
00745 else {
00746 $operation = array('data' => t('It is reached the maximum allowed number of forums for groups.'), 'colspan' => '4');
00747 }
00748 }
00749 }
00750
00751 else {
00752 if (!isset($manage['default_container_yn'])) {
00753 $parent = $manage['term']->parents[0];
00754 $operation = array('data' => '', 'colspan' => '3');
00755 }
00756 else {
00757
00758 if ($manage['display_add_link'] == 0 && !user_access('administer forums')) {
00759 $operation = array('data' => '', 'colspan' => '3');
00760 }
00761 else {
00762 $parent = $manage['term']->tid;
00763 $operation = array('data' => l(t('add forum'), 'og_forums/manage/add/'. $manage[group]->nid .'/'. $parent), 'colspan' => '3');
00764 }
00765 }
00766 }
00767 }
00768 else {
00769
00770 if ($manage['forum_limit_reached']) {
00771 if ($manage['term']->parents[0] == 0 || $manage['term']->parents[0] == $manage['default_container']) {
00772
00773 if ($manage['display_quota_message'] == 0) {
00774 $operation = array('data' => '', 'colspan' => '4');
00775 }
00776 else {
00777 $operation = array('data' => t('You have reached the maximum allowed number of forums for groups.'), 'colspan' => '4');
00778 }
00779 }
00780 else {
00781 $operation = array('data' => '', 'colspan' => '3');
00782 }
00783 }
00784
00785 else {
00786 if ($manage['term']->parents[0] == 0 || $manage['term']->parents[0] == $manage['default_container']) {
00787
00788 if ($manage['display_add_link'] == 0 && !user_access('administer forums')) {
00789 $operation = array('data' => '', 'colspan' => '3');
00790 }
00791 else {
00792 $parent = $manage['term']->tid;
00793 $operation = array('data' => l(t('add forum'), 'og_forums/manage/add/'. $manage[group]->nid .'/'. $parent), 'colspan' => '3');
00794 }
00795 }
00796 else {
00797 $operation = array('data' => '', 'colspan' => '');
00798 }
00799
00800 }
00801 }
00802
00803 $row = array(str_repeat('-', $manage['term']->depth) .' '. check_plain($manage['term']->name), $operation);
00804 break;
00805
00806 case 'forum':
00807
00808 if ((user_access('make forums public') && $manage['allow_public'] == 1) && $manage['all_tids'][$manage['term']->tid] < PUBLIC_AUTO) {
00809 $row = array(str_repeat('-', $manage['term']->depth) .' '. $manage['term_link'], l(t('edit forum'), 'og_forums/manage/edit/'. $manage[group]->nid .'/'. $manage[term]->tid), $public_link, $reset_link);
00810 }
00811
00812 elseif ((user_access('make forums public') && $manage['allow_public'] = 1) && $manage['all_tids'][$manage['term']->tid] > PRIVATE_BY_GROUP_OWNER) {
00813 $row = array(str_repeat('-', $manage['term']->depth) .' '. $manage['term_link'], l(t('edit forum'), 'og_forums/manage/edit/'. $manage[group]->nid .'/'. $manage[term]->tid), $private_link, $reset_link);
00814 }
00815 elseif (user_access('administer forums')) {
00816 $row = array(str_repeat('-', $manage['term']->depth) .' '. $manage['term_link'], l(t('edit forum'), 'og_forums/manage/edit/'. $manage[group]->nid .'/'. $manage[term]->tid), $private_link, $reset_link);
00817 }
00818 else {
00819 $row = array(str_repeat('-', $manage['term']->depth) .' '. $manage['term_link'], l(t('edit forum'), 'og_forums/manage/edit/'. $manage[group]->nid .'/'. $manage[term]->tid));
00820 }
00821 break;
00822 }
00823
00824 return $row;
00825 }
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835 function og_forums_forum_availability($form_state) {
00836 global $user;
00837
00838 $nid = arg(2);
00839 $node = node_load($nid);
00840 $group_user = user_load(array('uid' => $node->uid));
00841 if (isset($group_user->og_forums)) {
00842 if (array_key_exists($nid, $group_user->og_forums)) {
00843 $availability = $group_user->og_forums[$nid];
00844 }
00845 else {
00846 $availability = TRUE;
00847 }
00848 }
00849 else {
00850 $availability = TRUE;
00851 }
00852
00853 if ($availability == TRUE) {
00854 $submit_value = t('Deactivate availability');
00855 $description = t('Here you can deactivate the forum function for this group.');
00856 $description .= ' '. t('Existing containers or forums and their topics will not be deleted - they are no longer reachable.');
00857 $collapsed = TRUE;
00858 }
00859 else {
00860 $submit_value = t('Activate availability');
00861 $description = t('Here you can activate the forum function for this group.');
00862 $collapsed = FALSE;
00863 }
00864
00865 $form = array();
00866
00867 $form['availability'] = array(
00868 '#type' => 'fieldset',
00869 '#title' => t('Availability of forums'),
00870 '#description' => $description,
00871 '#collapsible' => TRUE,
00872 '#collapsed' => $collapsed,
00873 );
00874 $form['availability']['availability_value'] = array(
00875 '#type' => 'value',
00876 '#default_value' => TRUE ? $availability == FALSE : TRUE,
00877 );
00878 $form['availability']['uid'] = array(
00879 '#type' => 'value',
00880 '#default_value' => $node->uid,
00881 );
00882 $form['availability']['nid'] = array(
00883 '#type' => 'value',
00884 '#default_value' => $node->nid,
00885 );
00886 $form['availability']['submit'] = array(
00887 '#type' => 'submit',
00888 '#value' => $submit_value,
00889 );
00890 $form['#submit'][] = 'og_forums_forum_availability_submit';
00891
00892 return $form;
00893 }
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905 function og_forums_forum_availability_submit($form, &$form_state) {
00906 $group_user = user_load(array('uid' => $form_state['values']['uid']));
00907 $group_user->og_forums[$form_state['values']['nid']] = $form_state['values']['availability_value'];
00908
00909 user_save($group_user, array('og_forums' => $group_user->og_forums));
00910 }
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929 function og_forums_form_main($type, $group_id, $tid) {
00930 $parent_term = og_forums_forum_get_parent($tid);
00931
00932 $edit = array('group_id' => $group_id, 'parent' => $parent_term->tid);
00933
00934 if ($_POST['op'] == t('Delete') || $_POST['confirm']) {
00935 return drupal_get_form('og_forums_confirm_delete', $tid, $group_id);
00936 }
00937
00938 switch ($type) {
00939 case 'add':
00940 $group_id = $edit['group_id'];
00941
00942 return drupal_get_form('og_forums_form_forum', $edit, $group_id, $edit['tid']);
00943
00944 case 'edit':
00945 $group_id = $edit['group_id'];
00946 $group = node_load($group_id);
00947 $term = taxonomy_get_term($tid);
00948 $term->parent = $parent_term->tid;
00949 $term->group_id = $group_id;
00950 $edit = (array)$term;
00951
00952 return drupal_get_form('og_forums_form_forum', $edit, $group_id, $tid);
00953 }
00954 }
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964 function og_forums_set_og_group_context_from_tid($tid = 0) {
00965 $sql = "SELECT nid FROM {og_terms} WHERE tid = %d";
00966
00967 if ($gid = db_result(db_query($sql, $tid))) {
00968 $group_node = node_load($gid);
00969 og_set_group_context($group_node);
00970 }
00971 }
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983 function og_forums_gid_from_tid($tid = 0) {
00984 $sql = "SELECT nid FROM {og_terms} WHERE tid = %d";
00985
00986 if ($gid = db_result(db_query($sql, $tid))) {
00987 return $gid;
00988 }
00989 else {
00990 return 0;
00991 }
00992 }
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006 function og_forums_tid_from_gid($gid) {
01007 $sql = "SELECT MIN(tid) FROM {og_terms} WHERE nid = %d";
01008
01009 if ($tid = db_result(db_query($sql, $gid))) {
01010 return $tid;
01011 }
01012 else {
01013 return 0;
01014 }
01015 }
01016
01017
01018
01019
01020 function og_forums_db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid', $args = array()) {
01021 global $user;
01022
01023 if ($primary_field == 'tid') {
01024 if (arg(0) == 'forum') {
01025 if (user_access('administer forums') || (!module_exists('og_access') || variable_get('og_forums_all_public', 0))) {
01026 return;
01027
01028
01029 }
01030 else {
01031 $return['join'] = "LEFT JOIN {og_terms} ogt ON ogt.tid = t.tid";
01032
01033
01034 $return['where'] = "ogt.public IS NULL OR ogt.public > ". PRIVATE_BY_GROUP_OWNER;
01035
01036
01037 if (!empty($user->og_groups)) {
01038 $return['where'] .= " OR ogt.nid IN (". implode(',', array_keys($user->og_groups)) .")";
01039 }
01040
01041 return $return;
01042 }
01043 }
01044
01045 if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'forum' && empty($_GET['gids'])) {
01046 $return['join'] = "LEFT JOIN {og_terms} ogt ON t.tid = ogt.tid";
01047 $return['where'] = "ogt.nid IS NULL";
01048
01049 if ($og_forums_container = variable_get('og_forums_default_container', FALSE)) {
01050 $return['where'] .= " AND t.tid != $og_forums_container";
01051 }
01052 $return['distinct'] = TRUE;
01053
01054 return $return;
01055 }
01056 }
01057 }
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078 function og_forums_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
01079 switch ($op) {
01080 case 'load':
01081 if ($node->type == 'forum' && $og_forums_nid = db_result(db_query('SELECT nid FROM {og_terms} WHERE tid = %d', $node->tid))) {
01082 $node->og_forums_nid = $og_forums_nid;
01083 }
01084 break;
01085
01086 case 'prepare':
01087 if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'forum' && arg(3)) {
01088 if (!og_get_group_context()) {
01089 og_forums_set_og_group_context_from_tid(arg(3));
01090
01091 }
01092 }
01093 break;
01094
01095 case 'insert':
01096
01097 if (og_is_group_type($node->type)) {
01098 og_clear_group_context();
01099
01100 $default_container_yn = variable_get('og_forums_default_container_yn', TRUE);
01101 if ($default_container_yn == FALSE) {
01102 module_invoke('og_forums', 'manage_terms', $node, 'og_forums_default_container_yn_yes');
01103
01104 cache_clear_all();
01105 }
01106 else {
01107 module_invoke('og_forums', 'manage_terms', $node, 'og_forums_default_container_yn_no');
01108
01109 cache_clear_all();
01110 }
01111 }
01112
01113
01114
01115 if ($node->type == 'forum') {
01116 $sql = "SELECT tid, public FROM {og_terms} WHERE nid = %d";
01117 $container = og_forums_get_forum_container($node->og_groups[0]);
01118 $gid = og_forums_gid_from_tid($node->tid);
01119 $results = db_query($sql, $gid);
01120
01121 while ($result = db_fetch_object($results)) {
01122 $forums[$result->tid] = $result->public;
01123 }
01124 $sql2 = "UPDATE {og_terms} SET public = %d WHERE tid = %d";
01125
01126
01127 if ($node->og_public == PRIVATE_BY_GROUP_OWNER) {
01128
01129 if ($forums[$node->tid] == PRIVATE_DEFAULT && $forums[$container] != PRIVATE_BY_GROUP_OWNER) {
01130 db_query($sql2, PUBLIC_AUTO, $node->tid);
01131 if ($forums[$container] == PRIVATE_DEFAULT) {
01132 db_query($sql2, PUBLIC_AUTO, $container);
01133 }
01134 }
01135 }
01136
01137 elseif ($node->og_public == PRIVATE_DEFAULT) {
01138
01139 if ($forums[$node->tid] == PUBLIC_AUTO) {
01140 $should_be_public = FALSE;
01141 $sql3 = "SELECT DISTINCT COUNT(oap.nid) FROM {node} n
01142 INNER JOIN {og_access_post} oap ON oap.nid = n.nid
01143 INNER JOIN {term_node} tn ON tn.nid = oap.nid
01144 WHERE n.type = 'forum' AND oap.og_public = 1 AND tn.tid = %d";
01145
01146 if (!$count = db_result(db_query($sql3, $node->tid))) {
01147 db_query($sql2, PRIVATE_DEFAULT, $node->tid);
01148 foreach ($forums as $tid => $public) {
01149 if ($count = db_result(db_query($sql3, $tid))) {
01150 $should_be_public = TRUE;
01151 }
01152 }
01153
01154 if (!$should_be_public && $forums[$container] == PUBLIC_AUTO) {
01155 db_query($sql2, PRIVATE_DEFAULT, $container);
01156 }
01157 }
01158 }
01159 }
01160 }
01161 break;
01162
01163 case 'update':
01164
01165 if (og_is_group_type($node->type)) {
01166
01167 $group_forums = og_forums_get_group_forums($node->nid, 'forum');
01168
01169
01170 if (count($group_forums) == 0) {
01171 $default_container_yn = variable_get('og_forums_default_container_yn', TRUE);
01172 if ($default_container_yn) {
01173 module_invoke('og_forums', 'manage_terms', $node, 'og_forums_default_container_yn_yes');
01174 }
01175 else {
01176 module_invoke('og_forums', 'manage_terms', $node, 'og_forums_default_container_yn_no');
01177 }
01178 }
01179
01180 $tid = db_result(db_query('SELECT tid FROM {og_terms} WHERE nid = %d', $node->nid));
01181 if ($tid) {
01182 $term = taxonomy_get_term($tid);
01183 $term->parents = taxonomy_get_parents($tid);
01184 $term->parent = array_keys($term->parents);
01185 $term->name = $node->title;
01186
01187
01188 taxonomy_save_term($term);
01189 }
01190 }
01191
01192
01193 elseif ($node->type == 'forum') {
01194 $sql = "SELECT tid, public FROM {og_terms} WHERE nid = %d";
01195 $container = og_forums_get_forum_container($node->og_groups[0]);
01196 $gid = og_forums_gid_from_tid($node->tid);
01197 $results = db_query($sql, $gid);
01198
01199 while ($result = db_fetch_object($results)) {
01200 $forums[$result->tid] = $result->public;
01201 }
01202 $sql2 = "UPDATE {og_terms} SET public = %d WHERE tid = %d";
01203
01204
01205 if ($node->og_public == 1) {
01206
01207 if ($forums[$node->tid] == PRIVATE_DEFAULT && $forums[$container] != PRIVATE_BY_GROUP_OWNER) {
01208 db_query($sql2, PUBLIC_AUTO, $node->tid);
01209 if ($forums[$container] == PRIVATE_DEFAULT) {
01210 db_query($sql2, PUBLIC_AUTO, $container);
01211 }
01212 }
01213 }
01214
01215 elseif ($node->og_public == 0) {
01216
01217 if ($forums[$node->tid] == PUBLIC_AUTO) {
01218 $should_be_public = FALSE;
01219 $sql3 = "SELECT DISTINCT COUNT(oap.nid) FROM {node} n
01220 INNER JOIN {og_access_post} oap ON oap.nid = n.nid
01221 INNER JOIN {term_node} tn ON tn.nid = oap.nid
01222 WHERE n.type = 'forum' AND oap.og_public = 1 AND tn.tid = %d";
01223 if (!$count = db_result(db_query($sql3, $node->tid))) {
01224 db_query($sql2, PRIVATE_DEFAULT, $node->tid);
01225
01226 foreach ($forums as $tid => $public) {
01227 if ($count = db_result(db_query($sql3, $tid))) {
01228 $should_be_public = TRUE;
01229 }
01230 }
01231
01232
01233 if (!$should_be_public && $forums[$container] == PUBLIC_AUTO) {
01234 db_query($sql2, PRIVATE_DEFAULT, $container);
01235 }
01236 }
01237 }
01238 }
01239 }
01240 break;
01241
01242 case 'delete':
01243
01244 if (!user_access('delete own groups forums')) {
01245 return;
01246 }
01247
01248 if (og_is_group_type($node->type)) {
01249 $result = db_query('SELECT tid FROM {og_terms} WHERE nid = %d', $node->nid);
01250
01251
01252
01253 $group_user = node_load(array('uid' => $node->uid));
01254 if (isset($group_user->og_forums) && count($group_user->og_forums)) {
01255 if (array_key_exists($node->nid, $group_user->og_forums)) {
01256 unset($group_user->og_forums[$node->nid]);
01257
01258 user_save($group_user, $group_user->og_forums);
01259 }
01260 }
01261
01262 while ($term = db_fetch_object($result)) {
01263 taxonomy_del_term($term->tid);
01264 }
01265 db_query('DELETE FROM {og_terms} WHERE nid = %d', $node->nid);
01266
01267 og_clear_group_context();
01268
01269 }
01270 break;
01271
01272 case 'view':
01273
01274 if ($teaser) {
01275 break;
01276 }
01277
01278
01279 if ($page && !empty($node->og_groups) && $node->type == 'forum') {
01280
01281 og_set_group_context(node_load($node->og_groups[0]));
01282
01283 if (is_numeric($node->tid)) {
01284 $tid = $node->tid;
01285 }
01286
01287 global $_menu;
01288 if (og_get_group_context()) {
01289 _og_forums_set_breadcrumb($tid);
01290 }
01291
01292 drupal_set_title(check_plain($node->title));
01293 }
01294
01295
01296 if ($page && empty($node->og_groups) && $node->type == 'group') {
01297
01298 $forums = og_forums_get_group_forums($node->nid);
01299
01300 if (count($forums) == 0) {
01301 if ((og_forums_is_admin($node) && user_access('admin own groups forums')) || user_access('administer forums')) {
01302 $edit_path = "node/$node->nid/edit";
01303 $message = _og_forums_no_forums_message('manager', $edit_path);
01304 }
01305 else {
01306 $user_path = "user/$node->uid";
01307 $message = _og_forums_no_forums_message('member', $user_path);
01308 }
01309
01310 drupal_set_message($message);
01311 }
01312 }
01313 break;
01314
01315 case 'presave':
01316 if ($node->type == 'forum') {
01317 $gid = og_forums_gid_from_tid($node->tid);
01318 $node->og_groups = array();
01319 $node->og_groups[$gid] = $gid;
01320 $node->og_groups = array_filter($node->og_groups);
01321 $node->og_groups = array_keys($node->og_groups);
01322 }
01323 break;
01324 }
01325 }
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340
01341
01342
01343 function og_forums_manage_terms(&$node, $op) {
01344 switch ($op) {
01345 case 'og_forums_default_container_yn_yes':
01346
01347 $edit = array('name' => $node->title, 'vid' => og_forums_get_vid());
01348 $default_container = variable_get('og_forums_default_container', 0);
01349 $edit['parent'] = array($default_container);
01350
01351 unset($edit['parent']);
01352
01353 $status = taxonomy_save_term($edit);
01354
01355 if ($edit['tid'] == 0) {
01356 module_invoke('og_terms', 'error', $edit['tid'], $node->nid, $edit['title']);
01357 }
01358
01359 db_query('INSERT INTO {og_terms} (tid, nid) VALUES (%d, %d)', $edit['tid'], $node->nid);
01360
01361 $containers = variable_get('forum_containers', array());
01362 $containers[] = $edit['tid'];
01363 variable_set('forum_containers', $containers);
01364
01365
01366 if (variable_get('og_forums_access_template', 1) == 1) {
01367 $tpl_settings_container = variable_get('og_forums_access_template_container', _og_forums_access_template_container_default());
01368 $tpl_settings_forum = variable_get('og_forums_access_template_forum', _og_forums_access_template_forum_default());
01369 $grant = array_merge($tpl_settings_container, $tpl_settings_forum);
01370
01371 og_forums_access_template($edit['tid'], $grant, 'insert');
01372 }
01373
01374 break;
01375
01376 case 'og_forums_default_container_yn_no':
01377
01378 if (variable_get('og_forums_access_template', 1) == 1) {
01379 $tpl_settings_container = variable_get('og_forums_access_template_container', _og_forums_access_template_container_default());
01380 $tpl_settings_forum = variable_get('og_forums_access_template_forum', _og_forums_access_template_forum_default());
01381 $grant = array_merge($tpl_settings_container, $tpl_settings_forum);
01382 }
01383
01384
01385
01386
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397
01398
01399
01400
01401
01402
01403
01404
01405
01406
01407
01408 $og_forums_default_container = variable_get('og_forums_default_container', 0);
01409 if ($og_forums_default_container && $og_forums_default_container != -1) {
01410 $parent = $og_forums_default_container;
01411 }
01412 else {
01413 $parent = NULL;
01414 }
01415
01416
01417 $public = 0;
01418 $auto_public = variable_get('og_forums_auto_public', 0);
01419 if ($auto_public == 1) {
01420 $public = PUBLIC_AUTO;
01421 }
01422
01423 $edit = array('name' => $node->title, 'vid' => og_forums_get_vid(), 'parent' => $parent);
01424
01425 $status = taxonomy_save_term($edit);
01426
01427 if ($edit['tid'] == 0) {
01428 module_invoke('og_terms', 'error', $edit['tid'], $node->nid, $edit['title']);
01429 }
01430
01431 $containers = variable_get('forum_containers', array());
01432 $containers[] = $edit['tid'];
01433 variable_set('forum_containers', $containers);
01434
01435 db_query('INSERT INTO {og_terms} (tid, nid, public) VALUES (%d, %d, %d)', $edit['tid'], $node->nid, $public);
01436 og_forums_access_template($edit['tid'], $grant, 'insert');
01437
01438
01439
01440 $curr_container = $edit['tid'];
01441 $root_container = $curr_container;
01442
01443
01444 unset($edit['tid']);
01445 $default_forum_names = variable_get('og_forums_default_name', 'General discussion;Other');
01446
01447
01448 $default_forum_names = _og_forums_clean_forum_names($default_forum_names);
01449 $forum_names = explode(';', $default_forum_names);
01450 $is_container = 0;
01451
01452
01453 $c_order = array($root_container);
01454 $weight_indx = -10;
01455
01456 foreach ($forum_names as $fvalue) {
01457 switch(strtoupper($fvalue)) {
01458 case 'C':
01459 $is_container = 1;
01460 break;
01461
01462 case 'P':
01463
01464 if (end($c_order) != $root_container){
01465 array_pop($c_order);
01466 }
01467
01468 $curr_container = end($c_order);
01469 $is_container = 0;
01470 break;
01471
01472 case 'R':
01473 unset($c_order);
01474 $c_order = array($root_container);
01475 $curr_container = $root_container;
01476 $is_container = 0;
01477 break;
01478
01479 default:
01480 if ($is_container === 1) {
01481
01482
01483 $forum_names = explode(',', $fvalue);
01484 if (count($forum_names) == 2) {
01485 $fname = $forum_names[0];
01486 $fdesc = $forum_names[1];
01487 }
01488 else {
01489 $fname = $forum_names[0];
01490 unset($fdesc);
01491 }
01492
01493 $edit = array('name' => $fname, 'vid' => og_forums_get_vid(), 'parent' => $curr_container, 'weight' => $weight_indx, 'description' => $fdesc);
01494
01495 $status = taxonomy_save_term($edit);
01496
01497 if ($edit['tid'] == 0) {
01498 module_invoke('og_terms', 'error', $edit['tid'], $node->nid, $edit['title']);
01499 }
01500
01501 $containers[] = $edit['tid'];
01502
01503 variable_set('forum_containers', $containers);
01504 $curr_container = $edit['tid'];
01505
01506 db_query('INSERT INTO {og_terms} (tid, nid, public) VALUES (%d, %d, %d)', $edit['tid'], $node->nid, $public);
01507 og_forums_access_template($edit['tid'], $grant, 'insert');
01508
01509
01510 $c_order[] = $edit['tid'];
01511 unset($edit['tid']);
01512 unset($edit['parent']);
01513 $is_container = 0;
01514 $weight_indx = $weight_indx + 1;
01515 }
01516 else {
01517
01518
01519 $forum_names = explode(',', $fvalue);
01520 if (count($forum_names) == 2){
01521 $fname = $forum_names[0];
01522 $fdesc = $forum_names[1];
01523 }
01524 else {
01525 $fname = $forum_names[0];
01526 unset($fdesc);
01527 }
01528
01529 $edit['name'] = $fname;
01530 $edit['description'] = $fdesc;
01531 $edit['parent'] = $curr_container;
01532 $edit['weight'] = $weight_indx;
01533
01534 $status = taxonomy_save_term($edit);
01535
01536 if ($edit['tid'] == 0) {
01537 module_invoke('og_terms', 'error', $edit['tid'], $node->nid, $edit['title']);
01538 }
01539
01540 db_query('INSERT INTO {og_terms} (tid, nid, public) VALUES (%d, %d, %d)', $edit['tid'], $node->nid, $public);
01541 og_forums_access_template($edit['tid'], $grant, 'insert');
01542
01543
01544 unset($edit['tid']);
01545 $is_container = 0;
01546 $weight_indx = $weight_indx + 1;
01547 }
01548 break;
01549 }
01550 }
01551
01552
01553 unset($forum_names);
01554 unset($c_order);
01555
01556
01557
01558 break;
01559 }
01560 }
01561
01562
01563
01564
01565 function og_forums_comment(&$a1, $op) {
01566 $nid = $a1->nid;
01567 $node = node_load($nid);
01568
01569 if (is_numeric($node->tid)) {
01570 $tid = $node->tid;
01571 og_forums_set_og_group_context_from_tid($tid);
01572 $gid = og_forums_gid_from_tid($tid);
01573 if ($gid) {
01574 og_set_theme($gid);
01575 $group_node = node_load($gid);
01576 og_set_language($group_node);
01577 }
01578
01579 _og_forums_set_breadcrumb($tid, TRUE);
01580 }
01581 }
01582
01583
01584
01585
01586
01587
01588
01589
01590
01591 function og_forums_get_forum_container($gid) {
01592
01593 return db_result(db_query_range('SELECT tid FROM {og_terms} WHERE nid = %d ORDER BY tid ASC', $gid, 0, 1));
01594 }
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604 function og_forums_is_admin($group) {
01605 global $user;
01606
01607 $result = db_query(og_list_users_sql(0), $group->nid);
01608 while ($row = db_fetch_object($result)) {
01609 if ($user->uid == 1 && user_access('administer forums')) {
01610 return TRUE;
01611 }
01612 if ($row->uid == $user->uid) {
01613 if ($row->is_admin > 0) {
01614 return TRUE;
01615 }
01616 }
01617 }
01618 return FALSE;
01619 }
01620
01621
01622
01623
01624
01625
01626
01627
01628
01629 function og_forums_og_create_links($group) {
01630 global $user;
01631
01632
01633 $forum = og_forums_get_forum_container($group->nid);
01634 $links = array();
01635 if ($forum) {
01636 $node = node_load($group->nid);
01637 $group_user = user_load(array('uid' => $node->uid));
01638
01639
01640 $availability = module_invoke('og_forums', 'get_forums_availability', $node, $group_user);
01641 if ($availability == TRUE) {
01642 $links[] = l(t('Groups forums'), "forum/$forum", array('attributes' => array('title' => t('View group forum discussions.'))));
01643 }
01644
01645
01646 if ((og_forums_is_admin($group) && user_access('admin own groups forums')) || user_access('administer forums')) {
01647 $links[] = l(t('Manage groups forums'), "og_forums/manage/$group->nid", array('attributes' => array('title' => t('Let you create, edit or delete group\'s forums.'))));
01648 }
01649 }
01650
01651 return $links;
01652 }
01653
01654
01655
01656
01657
01658
01659 function og_forums_retroactively_apply() {
01660
01661 $result = db_query('SELECT og.nid, n.title, n.type FROM {og} og INNER JOIN {node} n on og.nid = n.nid');
01662
01663 $counter = 0;
01664 while ($group = db_fetch_object($result)) {
01665
01666 if (!db_result(db_query('SELECT * FROM {og_terms} ogt WHERE ogt.nid = %d', $group->nid))) {
01667
01668 og_forums_nodeapi($group, 'insert');
01669 $counter++;
01670 }
01671 }
01672
01673 drupal_set_message(t('%num groups had forums created', array('%num' => $counter)));
01674 }
01675
01676
01677
01678
01679
01680
01681
01682
01683
01684
01685
01686
01687 function og_forums_form_forum(&$form_state, $edit, $gid = 0, $tid = 0) {
01688 $node = node_load($gid);
01689 og_set_language($node);
01690 og_set_theme($gid);
01691
01692 if (!$edit['tid']) {
01693 $tid = $edit['parent'];
01694 }
01695
01696 $edit += array(
01697 'name' => '',
01698 'description' => '',
01699 'tid' => NULL,
01700 'weight' => 0,
01701 );
01702
01703 $form['name'] = array(
01704 '#type' => 'textfield',
01705 '#title' => t('Forum name'),
01706 '#default_value' => $edit['name'],
01707 '#maxlength' => 64,
01708 '#description' => t('The forum name is used to identify related discussions.'),
01709 '#required' => TRUE,
01710 );
01711 $form['description'] = array(
01712 '#type' => 'textarea',
01713 '#title' => t('Description'),
01714 '#default_value' => $edit['description'],
01715 '#description' => t('The forum description can give users more information about the discussion topics it contains.'),
01716 );
01717
01718 $form['parent']['#tree'] = TRUE;
01719
01720 if (arg(2) == 'add') {
01721 _og_forums_manage_breadcrumb(node_load($gid), 'add');
01722
01723 $parents = taxonomy_get_parents_all($edit['tid']);
01724
01725 $form['parent'][0] = _og_forums_parent_select($edit['tid'], t('Parent'), 'forum', $gid);
01726 $form['weight'] = array(
01727 '#type' => 'weight',
01728 '#title' => t('Weight'),
01729 '#default_value' => $edit['weight'],
01730 '#description' => t('Forums are displayed in ascending order by weight (forums with equal weights are displayed alphabetically).'),
01731 );
01732 }
01733 else {
01734 _og_forums_manage_breadcrumb(node_load($gid), 'edit');
01735
01736 $parents = taxonomy_get_parents_all($edit['tid']);
01737
01738 $form['parent'][0] = _og_forums_parent_select($edit['tid'], t('Parent'), 'forum', $gid);
01739 $form['weight'] = array(
01740 '#type' => 'weight',
01741 '#title' => t('Weight'),
01742 '#default_value' => $edit['weight'],
01743 '#description' => t('Forums are displayed in ascending order by weight (forums with equal weights are displayed alphabetically).'),
01744 );
01745 }
01746
01747 $form['group_id'] = array(
01748 '#type' => 'value',
01749 '#value' => $gid,
01750 );
01751 $form['weight'] = array(
01752 '#type' => 'weight',
01753 '#title' => t('Weight'),
01754 '#default_value' => $edit['weight'],
01755 '#description' => t('When listing forums, those with lighter (smaller) weights get listed before containers with heavier (larger) weights. Forums with equal weights are sorted alphabetically.'),
01756 );
01757
01758 $allow_public = variable_get('og_forums_allow_public', 0);
01759 if (($allow_public == 1 && (user_access('make forums public')) || user_access('administer forums'))) {
01760 $default_public = og_forums_is_public($tid);
01761
01762 $form['make_public'] = array(
01763 '#type' => 'checkbox',
01764 '#title' => t('Make forum public'),
01765 '#description' => t('This will allow non-group members to <em>view</em> this forum.'),
01766 '#default_value' => 1 ? $default_public > 0 : 0,
01767 );
01768 }
01769
01770 $form['vid'] = array(
01771 '#type' => 'hidden',
01772 '#value' => og_forums_get_vid(),
01773 );
01774 $form['submit'] = array(
01775 '#type' => 'submit',
01776 '#value' => t('Submit'),
01777 );
01778
01779 if ($edit['tid']) {
01780 if (user_access('delete own groups forums') || user_access('administer forums')) {
01781 $form['delete'] = array(
01782 '#type' => 'submit',
01783 '#value' => t('Delete'),
01784 );
01785 }
01786 $form['tid'] = array(
01787 '#type' => 'hidden',
01788 '#value' => $edit['tid'],
01789 );
01790 }
01791
01792 $form['#submit'][] = 'og_forums_form_submit';
01793 $form['#validate'][] = 'og_forums_form_validate';
01794
01795 return $form;
01796 }
01797
01798
01799
01800
01801
01802
01803 function og_forums_form_validate($form, &$form_state) {
01804 global $user;
01805
01806 $error = TRUE;
01807
01808 $default_container = variable_get('og_forums_default_container', -1);
01809
01810 if (is_numeric($form_state['values']['group_id'])) {
01811 $group = node_load($form_state['values']['group_id']);
01812
01813
01814
01815
01816 if ($group && og_forums_is_admin($group) && (in_array($form_state['values']['parent'][0], _og_forums_ogterms($group)) || $form_state['values']['tid']) ||
01817 $group && og_forums_is_admin($group) && ($form_state['values']['parent'][0] == $default_container)
01818 ) {
01819 $error = FALSE;
01820 }
01821 }
01822
01823
01824 if ($error) {
01825 form_set_error('name', t('Internal error processing your data. Please contact website administrators.'));
01826 }
01827 }
01828
01829
01830
01831
01832
01833
01834 function og_forums_form_submit($form, &$form_state) {
01835 $gid = $form_state['values']['group_id'];
01836
01837 $container = false;
01838 $type = t('forum');
01839 $status = taxonomy_save_term($form_state['values']);
01840
01841 switch ($status) {
01842 case SAVED_NEW:
01843 if ($container) {
01844 $containers = variable_get('forum_containers', array());
01845 $containers[] = $form_state['values']['tid'];
01846 variable_set('forum_containers', $containers);
01847 }
01848 drupal_set_message(t('Created new !type %term.', array('%term' => $form_state['values']['name'], '!type' => $type)));
01849 break;
01850
01851 case SAVED_UPDATED:
01852 drupal_set_message(t('The !type %term has been updated.', array('%term' => $form_state['values']['name'], '!type' => $type)));
01853 break;
01854 }
01855
01856 $form_state['redirect'] = "og_forums/manage/$gid";
01857 }
01858
01859
01860
01861
01862
01863
01864
01865
01866
01867
01868
01869
01870
01871
01872
01873
01874
01875
01876
01877
01878
01879
01880
01881
01882
01883
01884
01885 function og_forums_taxonomy($op, $type, $obj) {
01886 switch ($op) {
01887 case 'insert':
01888 if (!isset($obj['group_id'])) {
01889 return;
01890 }
01891
01892 $forum_limit_reached = og_forums_forum_limit_reached($obj['group_id']);
01893 if ($forum_limit_reached) {
01894 return;
01895 }
01896
01897 if (isset($obj['parent'])) {
01898 $parent = (is_array($obj['parent'])) ? $obj['parent'][0] : $obj['parent'];
01899 $result = db_result(db_query("SELECT nid FROM {og_terms} WHERE tid = %d", $obj['tid']));
01900
01901 if ($result) {
01902 return;
01903 }
01904 else {
01905 if ($obj['make_public'] == 0) {
01906 $public = 0;
01907 }
01908 elseif ($obj['make_public'] == 1) {
01909 $public = 3;
01910 }
01911
01912 db_query('INSERT INTO {og_terms} (tid, nid, public) VALUES (%d, %d, %d)', $obj['tid'], $obj['group_id'], $public);
01913
01914
01915 if (variable_get('og_forums_access_template', 1) == 1) {
01916 $tpl_settings_container = variable_get('og_forums_access_template_container', _og_forums_access_template_container_default());
01917 $tpl_settings_forum = variable_get('og_forums_access_template_forum', _og_forums_access_template_forum_default());
01918 $grant = array_merge($tpl_settings_container, $tpl_settings_forum);
01919
01920 og_forums_access_template($obj['tid'], $grant, 'insert');
01921 }
01922 }
01923 }
01924
01925 break;
01926
01927 case 'update':
01928 if (isset($obj['parent'])) {
01929 if ($obj['make_public'] == 1) {
01930 og_forums_public($obj['group_id'], $obj['tid']);
01931 }
01932 if ($obj['make_public'] == 0) {
01933 og_forums_private($obj['group_id'], $obj['tid']);
01934 }
01935 }
01936 break;
01937
01938 case 'delete':
01939 if (isset($obj['tid'])) {
01940 db_query("DELETE FROM {og_terms} WHERE tid = %d", $obj['tid']);
01941 og_forums_access_template($obj['tid'], array(), 'delete');
01942 }
01943 break;
01944 }
01945 }
01946
01947
01948
01949
01950
01951
01952
01953
01954
01955
01956
01957
01958
01959 function og_forums_confirm_delete($form_state, $tid, $gid) {
01960 $term = taxonomy_get_term($tid);
01961
01962 $form['tid'] = array(
01963 '#type' => 'value',
01964 '#value' => $tid,
01965 );
01966 $form['name'] = array(
01967 '#type' => 'value',
01968 '#value' => check_plain($term->name),
01969 );
01970 $form['group_id'] = array(
01971 '#type' => 'value',
01972 '#value' => $gid,
01973 );
01974 $form['return'] = array(
01975 '#type' => 'value',
01976 '#value' => 'og_forums/manage/'. arg(3),
01977 );
01978
01979 $return_address = 'og_forums/manage/'. arg(3);
01980 $form = confirm_form($form, t('Are you sure you want to delete the forum %name?', array('%name' => $term->name)), $return_address, t('Deleting a forum or container will delete all sub-forums and associated posts as well. This action cannot be undone.'), t('Delete'), t('Cancel'));
01981
01982 return $form;
01983 }
01984
01985
01986
01987
01988
01989
01990 function og_forums_confirm_delete_submit($form, &$form_state) {
01991 taxonomy_del_term($form_state['values']['tid']);
01992
01993
01994 drupal_set_message(t('The forum %term and all sub-forums and associated posts have been deleted.', array('%term' => $form_state['values']['name'])));
01995 watchdog('content', t('forum: deleted %term and all its sub-forums and associated posts.', array('%term' => $form_state['values']['name'])));
01996
01997 db_query('DELETE FROM {og_terms} WHERE tid = %d', $form_state['values']['tid']);
01998
01999 $form_state['redirect'] = $form_state['values']['return'];
02000 }
02001
02002
02003
02004
02005
02006
02007 function og_forums_node_delete_confirm_submit($form, &$form_state) {
02008 $return = 'og';
02009
02010 $group_view = views_get_view('og');
02011 if (empty($group_view)) {
02012 $return = '<front>';
02013 }
02014
02015 $form_state['redirect'] = $return;
02016 }
02017
02018
02019
02020
02021
02022
02023
02024
02025
02026 function og_forums_forum_limit_reached($nid, $corrector = 1) {
02027 $forum_limit = variable_get('og_forums_limit_forums', 0);
02028 if ($forum_limit == 0) {
02029 return FALSE;
02030 }
02031
02032 $forum_containers = variable_get('forum_containers', array());
02033
02034 $sql = "SELECT tid FROM {og_terms} WHERE nid = %d";
02035 $results = db_query($sql, $nid);
02036
02037 $num = 0;
02038 while ($result = db_fetch_object($results)) {
02039 if (!in_array($result->tid, $forum_containers)) {
02040 $num++;
02041 }
02042 }
02043
02044
02045 $num_forums = ($num - $corrector);
02046
02047 if ($num_forums >= $forum_limit) {
02048 return TRUE;
02049 }
02050 else {
02051 return FALSE;
02052 }
02053 }
02054
02055
02056
02057
02058
02059
02060
02061
02062
02063 function og_forums_is_public($tid) {
02064
02065
02066
02067 $sql = "SELECT tid, public, nid FROM {og_terms} WHERE tid = %d";
02068 $results = db_fetch_object(db_query($sql, $tid));
02069
02070 if (!empty($results) && ($results->public <= PRIVATE_BY_GROUP_OWNER)) {
02071 return FALSE;
02072 }
02073 else {
02074 return TRUE;
02075 }
02076 }
02077
02078
02079
02080
02081
02082
02083
02084 function og_forums_is_public_all($nid) {
02085
02086
02087
02088
02089
02090
02091 $sql = "SELECT COUNT(public) AS count FROM {og_terms} WHERE nid = %d AND (public = %d OR public = %d)";
02092 $result = db_fetch_object(db_query($sql, $nid, PUBLIC_BY_GROUP_OWNER, PUBLIC_AUTO));
02093
02094 if ($result > 0) {
02095 return TRUE;
02096 }
02097 else {
02098 return FALSE;
02099 }
02100 }
02101
02102
02103
02104
02105
02106
02107
02108 function og_forums_is_og_forums($tid) {
02109 $sql = "SELECT tid FROM {og_terms} WHERE tid = %d";
02110 $results = db_fetch_object(db_query($sql, $tid));
02111
02112 if (empty($results)) {
02113 return FALSE;
02114 }
02115 else {
02116 return TRUE;
02117 }
02118 }
02119
02120
02121
02122
02123
02124
02125
02126
02127 function og_forums_get_vid() {
02128 return variable_get('forum_nav_vocabulary', FALSE);
02129 }
02130
02131
02132
02133
02134
02135
02136
02137 function og_forums_get_parents_all($tid) {
02138 $parents = array();
02139
02140 if ($tid) {
02141 $parents[] = taxonomy_get_term($tid);
02142 $n = 0;
02143 while ($parent = taxonomy_get_parents($parents[$n]->tid)) {
02144 $parents = array_merge($parents, $parent);
02145 $n++;
02146 }
02147
02148 array_shift($parents);
02149 }
02150
02151 return $parents;
02152 }
02153
02154
02155
02156
02157
02158
02159
02160
02161
02162
02163
02164
02165
02166
02167
02168
02169
02170 function og_forums_access_template($tid, $grant, $op) {
02171 switch ($op) {
02172 case 'insert':
02173 $sql = "INSERT INTO {forum_access} (tid, rid, grant_view, grant_update, grant_delete, grant_create, priority) VALUES (%d, %d, %d, %d, %d, %d, %d)";
02174 $sql2 = "INSERT INTO {forum_access} (tid, rid, grant_view, grant_update, grant_delete, grant_create, priority) VALUES (%d, %d, %d, %d, %d, %d, %d)";
02175
02176 db_query($sql, $tid, DRUPAL_ANONYMOUS_RID, $grant[1]['grant_view'], $grant[1]['grant_update'], $grant[1]['grant_delete'], $grant[1]['grant_create'], $grant[1]['priority']);
02177
02178 db_query($sql2, $tid, DRUPAL_AUTHENTICATED_RID, $grant[2]['grant_view'], $grant[2]['grant_update'], $grant[2]['grant_delete'], $grant[2]['grant_create'], $grant[2]['priority']);
02179 break;
02180
02181 case 'update':
02182
02183 $sql = "UPDATE {forum_access} SET grant_view, grant_update, grant_delete, grant_create, priority WHERE tid = %d AND rid = %d";
02184 db_query($sql, $tid, $rid);
02185 break;
02186
02187 case 'delete':
02188 $sql = "DELETE FROM {forum_access} WHERE tid = %d AND rid = %d";
02189 $sql2 = "DELETE FROM {forum_access} WHERE tid = %d AND rid = %d";
02190 db_query($sql, $tid, DRUPAL_ANONYMOUS_RID);
02191 db_query($sql2, $tid, DRUPAL_AUTHENTICATED_RID);
02192 break;
02193 }
02194 }
02195
02196
02197
02198
02199
02200
02201
02202 function og_forums_preprocess_node(&$variables) {
02203 if ($variables['type'] == 'forum') {
02204 _og_forums_set_breadcrumb($variables['tid'], TRUE);
02205 }
02206 }
02207
02208
02209
02210
02211
02212
02213
02214 function og_forums_get_forums_availability($node, $user) {
02215 $return = TRUE;
02216
02217 if (isset($user->og_forums)) {
02218 if (array_key_exists($node->nid, $user->og_forums)) {
02219
02220 $return = $user->og_forums[$node->nid];
02221 }
02222 elseif (!array_key_exists($node->nid, $user->og_forums)) {
02223 $return = TRUE;
02224 }
02225 }
02226
02227 return $return;
02228 }
02229
02230
02231
02232
02233
02234
02235
02236
02237
02238
02239
02240 function og_forums_access_forum_page($tid = 0) {
02241 if (!user_access('access content')) {
02242 return FALSE;
02243 }
02244
02245 global $user;
02246
02247 if ($tid != 0) {
02248 og_forums_set_og_group_context_from_tid($tid);
02249 $gid = og_forums_gid_from_tid($tid);
02250
02251 if ($gid) {
02252 if ((!empty($user->og_groups) && !array_key_exists($gid, $user->og_groups))) {
02253 return FALSE;
02254 }
02255 og_set_theme($gid);
02256 $node = node_load($gid);
02257
02258
02259 $group_user = user_load(array('uid' => $node->uid));
02260 $availability = module_invoke('og_forums', 'get_forums_availability', $node, $group_user);
02261 if ($availability == FALSE) {
02262 return FALSE;
02263 }
02264
02265 og_set_language($node);
02266 }
02267 }
02268 elseif (is_numeric(og_get_group_context())) {
02269 $tid = og_get_group_context();
02270 }
02271
02272 return TRUE;
02273 }
02274
02275
02276
02277
02278
02279
02280
02281
02282
02283
02284
02285 function og_forums_preprocess_forums(&$variables) {
02286 global $public_forums;
02287
02288 if (isset($variables['tid']) && $variables['tid'] != 0) {
02289 $gid = og_forums_gid_from_tid($variables['tid']);
02290
02291 if (!empty($gid)) {
02292 if ($public_forums == FALSE) {
02293 unset($variables['forums']);
02294
02295 $node = node_load($gid);
02296 og_forums_forum_empty($node);
02297 }
02298
02299 _og_forums_set_group_context(node_load($gid));
02300 _og_forums_set_breadcrumb($variables['tid']);
02301
02302 if (isset($variables['links']['forum']) && $public_forums == TRUE) {
02303 $variables['links']['forum']['title'] = t('Post new Group forum topic');
02304 $variables['links']['forum']['query'] = array('gids[]' => $gid);
02305 }
02306 else {
02307 unset($variables['links']['forum']);
02308 }
02309 }
02310 }
02311 }
02312
02313
02314
02315
02316
02317
02318 function og_forums_preprocess_forum_topic_list(&$variables) {
02319
02320 if (count($variables['topics']) == 0) {
02321 unset($variables['header']);
02322 }
02323 }
02324
02325
02326
02327
02328
02329
02330
02331
02332
02333
02334
02335 function og_forums_preprocess_forum_list(&$variables) {
02336 global $user, $public_forums;
02337
02338 if (!isset($variables['forums'])) {
02339 return;
02340 }
02341
02342 $public_forums = TRUE;
02343
02344 if (arg(0) == 'forum' && is_numeric(arg(1))) {
02345
02346 $variables = _og_forums_forum_list_hide($variables);
02347 if (count($variables['forums']) == 0) {
02348 $public_forums = FALSE;
02349 }
02350 return;
02351 }
02352
02353 $default_container = variable_get('og_forums_default_container', 0);
02354 $main_visibility = variable_get('og_forums_main_visibility', 0);
02355 $group_forums = og_forums_get_group_forums();
02356
02357 foreach ($variables['forums'] as $tid => $forum) {
02358 $gid = og_forums_gid_from_tid($tid);
02359 $public = og_forums_is_public($tid);
02360
02361 if ($main_visibility == 0 && array_key_exists($tid, $group_forums)) {
02362 unset($variables['forums'][$tid]);
02363 }
02364 else {
02365 if ($public) {
02366
02367 }
02368 else {
02369 unset($variables['forums'][$tid]);
02370 }
02371 }
02372
02373
02374 if ($main_visibility == 0 && array_key_exists($default_container, $variables['forums'])) {
02375 unset($variables['forums'][$default_container]);
02376 }
02377 }
02378 }
02379
02380
02381
02382
02383
02384
02385
02386
02387
02388
02389
02390
02391
02392
02393
02394
02395
02396
02397
02398 function og_forums_get_group_forums($gid = FALSE, $op = 'all') {
02399 $forums = array();
02400
02401 if ($gid == FALSE) {
02402 $sql = "SELECT tid, public FROM {og_terms}";
02403 }
02404 else {
02405 $sql = "SELECT tid, public FROM {og_terms} WHERE nid = %d";
02406 }
02407
02408 $results = db_query($sql, $gid);
02409
02410 switch ($op) {
02411 case 'all':
02412 while ($result = db_fetch_object($results)) {
02413 $forums[$result->tid] = $result->public;
02414 }
02415 break;
02416
02417 case 'forum':
02418 $forum_containers = variable_get('forum_containers', array());
02419
02420 while ($result = db_fetch_object($results)) {
02421 if (!in_array($result->tid, $forum_containers)) {
02422 $forums[$result->tid] = $result->public;
02423 }
02424 }
02425 break;
02426 }
02427
02428 return $forums;
02429 }
02430
02431
02432
02433
02434
02435
02436
02437
02438
02439
02440
02441
02442 function og_forums_forum_get_parent($tid) {
02443 $parents = taxonomy_get_parents_all($tid);
02444
02445 if (count($parents) > 1) {
02446 return $parents[1];
02447 }
02448 else {
02449 return FALSE;
02450 }
02451 }
02452
02453
02454
02455
02456
02457
02458
02459
02460
02461
02462
02463
02464
02465
02466
02467
02468
02469
02470 function og_forums_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL) {
02471 $tree = taxonomy_get_tree($vid, $parent, $depth, $max_depth);
02472
02473 if ($parent > 0) {
02474 $parent_term = taxonomy_get_term($parent);
02475 $parent_term->depth = 0;
02476 $parent_term->parents[] = NULL;
02477 array_unshift($tree, $parent_term);
02478 }
02479
02480 return $tree;
02481 }
02482
02483
02484
02485
02486
02487
02488 function og_forums_forum_empty($node) {
02489 global $user;
02490
02491 $dest = drupal_get_destination();
02492 if (og_is_group_member($node->nid)) {
02493 $msg = t('No forums in this group.');
02494 }
02495 else {
02496 if (!$user->uid) {
02497 if (variable_get('user_register', 1) == 0) {
02498 $msg = t('No public forums in this group. You must <a href="!login">login</a> and become a member in order to post forum topics, and view any private forums.', array('!login' => url("user/login", array('query' => $dest))));
02499 }
02500 else {
02501 $msg = t('No public forums in this group. You must <a href="!register">register</a> or <a href="!login">login</a> and become a member in order to post forum topics, and view any private forums.', array('!register' => url("user/register", array('query' => $dest)), '!login' => url("user/login", array('query' => $dest))));
02502 }
02503 }
02504
02505 elseif ($node->og_selective < OG_INVITE_ONLY) {
02506 $msg = t('No public forums in this group. Consider <a href="!url">joining this group</a> in order to view its forums.', array('!url' => url("og/subscribe/$node->nid", array('query' => $dest))));
02507 }
02508 else {
02509 $msg = t('No public forums in this group.');
02510 }
02511 }
02512
02513 drupal_set_message($msg);
02514 }
02515
02516
02517
02518
02519
02520
02521
02522
02523
02524
02525
02526
02527
02528 function og_forums_forum_access_get_settings($tid = NULL, $rid = DRUPAL_AUTHENTICATED_RID) {
02529 $return = array('view' => array(), 'update' => array(), 'delete' => array(), 'create' => array(), 'priority' => array());
02530 if (!isset($tid)) {
02531
02532 $return['view'] = array(DRUPAL_AUTHENTICATED_RID => 1);
02533 $return['create'] = array(DRUPAL_AUTHENTICATED_RID => 1);
02534 }
02535 else {
02536 $result = db_query("SELECT * FROM {forum_access} where tid = %d AND rid = %d", $tid, $rid);
02537 while ($access = db_fetch_object($result)) {
02538 $row_received = TRUE;
02539
02540 $return['view'][$access->rid] = $access->grant_view;
02541 $return['update'][$access->rid] = $access->grant_update;
02542 $return['delete'][$access->rid] = $access->grant_delete;
02543 $return['create'][$access->rid] = $access->grant_create;
02544 $return['priority'][$access->rid] = $access->priority;
02545 }
02546
02547 if (empty($row_received)) {
02548
02549 $return = og_forums_forum_access_get_settings();
02550 }
02551 }
02552 return $return;
02553 }
02554
02555
02556
02557
02558
02559
02560
02561
02562
02563
02564
02565 function og_forums_error($tid = NULL, $nid = NULL, $term_title = NULL) {
02566 if ($tid && $nid && $tid == 0) {
02567 $message = t('It has created a groups node and it was used a term ID 0. The term has the name %term. Please Check: The database tables og_terms and forum_access, the forum administration and the content management. Possible Cause: A user uses the Opera browser to create group nodes.', array('%term' => $term_title));
02568 $link = l(t('Created content'), 'node/'. $nid);
02569
02570 watchdog('error', $message, array(), WATCHDOG_ALERT, $link);
02571 }
02572 }
02573
02574
02575
02576
02577
02578
02579
02580 function _og_forums_set_breadcrumb($tid, $is_forum_post = FALSE) {
02581 $vocabulary = taxonomy_vocabulary_load(variable_get('forum_nav_vocabulary', ''));
02582 $parents = array_reverse(taxonomy_get_parents_all($tid));
02583 $title = $vocabulary->name;
02584
02585
02586 $gid = og_forums_gid_from_tid($tid);
02587 $breadcrumb[] = l(t('Home'), NULL);
02588
02589 if ($gid != NULL) {
02590 $node = node_load($gid);
02591 $breadcrumb[] = l(t('Groups'), 'og');
02592 $breadcrumb[] = l($node->title, "node/$node->nid");
02593 }
02594
02595 if ($parents && count($parents) != 1) {
02596 $p = array_shift($parents);
02597 $title = t('Forums');
02598 $breadcrumb[] = l($title, "node/$node->nid/forum");
02599
02600 foreach ($parents as $p) {
02601 if ($p->tid == $tid) {
02602 $title = $p->name;
02603 if ($is_forum_post)
02604 $breadcrumb[] = l(t($p->name), "forum/$p->tid");
02605 }
02606 else {
02607 $breadcrumb[] = l(t($p->name), "forum/$p->tid");
02608 }
02609 }
02610 }
02611
02612 if (!$is_forum_post) {
02613 drupal_set_title(check_plain($title));
02614 }
02615
02616 drupal_set_breadcrumb($breadcrumb);
02617 }
02618
02619
02620
02621
02622
02623
02624
02625 function _og_forums_manage_breadcrumb($group, $op = NULL) {
02626 global $vocabulary;
02627
02628 $breadcrumb[] = l(t('Home'), NULL);
02629 if ($vocabulary->name) {
02630 $breadcrumb[] = l($vocabulary->name, 'forum');
02631 }
02632 $breadcrumb[] = l($group->title, "node/$group->nid");
02633
02634 switch ($op) {
02635 case 'add':
02636 $breadcrumb[] = l(t("Add @title forums", array('@title' => $group->title)), "og_forums/manage/$group->nid");
02637 break;
02638
02639 case 'edit':
02640 $breadcrumb[] = l(t("Edit @title forums", array('@title' => $group->title)), "og_forums/manage/$group->nid");
02641 break;
02642
02643 case 'default':
02644 $breadcrumb[] = l(t("Manage @title forums", array('@title' => $group->title)), "og_forums/manage/$group->nid");
02645 break;
02646 }
02647
02648 drupal_set_breadcrumb($breadcrumb);
02649 }
02650
02651
02652
02653
02654
02655
02656 function _og_forums_forum_list_hide(&$variables) {
02657 $gid = og_forums_gid_from_tid($variables['forum_id']);
02658 $group_forums = og_forums_get_group_forums($gid, 'all');
02659
02660 foreach ($group_forums as $tid => $public) {
02661 $parents = taxonomy_get_parents_all($tid);
02662 }
02663
02664 foreach ($variables['forums'] as $tid => $forum) {
02665 if ($forum->is_container == TRUE) {
02666 $tree = taxonomy_get_tree($forum->vid, $tid);
02667
02668 if (count($tree) > 0) {
02669 if (count($tree) == 1) {
02670 if ($variables['forums'][$tid]->is_container == TRUE) {
02671 $tree_tree = taxonomy_get_tree($forum->vid, $tid);
02672
02673 if (count($tree_tree) == 0) {
02674 unset($variables['forums'][$tid]);
02675 }
02676 elseif (count($tree_tree) > 0 && $variables['forums'][$tree_tree[0]->tid]->is_container == TRUE) {
02677 unset($variables['forums'][$tid]);
02678 }
02679 }
02680 }
02681 }
02682 else {
02683 unset($variables['forums'][$tid]);
02684 }
02685 }
02686 }
02687
02688 return $variables;
02689 }
02690
02691
02692
02693
02694
02695
02696
02697 function _og_forums_ogterms($group) {
02698 $result = db_query('SELECT * FROM {og_terms} WHERE nid = %d', $group->nid);
02699
02700 $og_terms = array();
02701 while ($term = db_fetch_object($result)) {
02702 $og_terms[] = $term->tid;
02703 }
02704
02705 return $og_terms;
02706 }
02707
02708
02709
02710
02711
02712
02713
02714
02715 function _og_forums_set_group_context($group) {
02716 og_set_theme($group);
02717 og_set_language($group);
02718 og_set_group_context($group);
02719 }
02720
02721
02722
02723
02724
02725
02726
02727
02728
02729
02730
02731
02732
02733
02734
02735
02736
02737
02738 function _og_forums_parent_select($tid, $title, $child_type, $gid) {
02739 $vid = variable_get('forum_nav_vocabulary', '');
02740 $children = taxonomy_get_tree($vid, $tid);
02741 $forum_containers = variable_get('forum_containers', array());
02742
02743 $parents = taxonomy_get_parents($tid);
02744 if ($parents) {
02745 $parent = array_shift($parents);
02746 $parent = $parent->tid;
02747 }
02748 else {
02749 $parent = 0;
02750 }
02751
02752
02753 foreach ($children as $child) {
02754 $exclude[] = $child->tid;
02755 }
02756 $exclude[] = $tid;
02757
02758
02759
02760 $group_tid = og_forums_tid_from_gid($gid);
02761 $group_tree = og_forums_get_tree($vid, $group_tid, 0);
02762
02763 if ($group_tree) {
02764 foreach ($group_tree as $term) {
02765 if (!in_array($term->tid, $exclude)) {
02766 $option = str_repeat('-', $term->depth) .' '. $term->name;
02767
02768
02769 if (in_array($term->tid, $forum_containers)) {
02770 $option = $option .' '. t('(Container)');
02771 }
02772
02773 $options[$term->tid] = $option;
02774 }
02775 }
02776 }
02777
02778 if ($child_type == 'container') {
02779 $description = t('Containers are usually placed at the top (root) level, but may also be placed inside another container or forum.') .'<br />';
02780 $description .= t('Note') .':<br />';
02781 $description .= t('A forum to place in another forum is possible but not always a good idea. Possible problem: Poor forum and topic lists.');
02782 }
02783 elseif ($child_type == 'forum') {
02784 $description = t('Forums may be placed at the top (root) group forums level, or inside another container.') .'<br />';
02785 $description .= t('Note') .':<br />';
02786 $description .= t('A forum to place in another forum is possible but not always a good idea. Possible problem: Poor forum and topic lists.');
02787 }
02788
02789 return array('#type' => 'select', '#title' => $title, '#default_value' => $parent, '#options' => $options, '#description' => $description, '#required' => TRUE);
02790 }
02791
02792
02793
02794
02795
02796
02797
02798 function _og_forums_access_template_container_default() {
02799 return array(
02800 2 => array(
02801 'grant_view' => 1,
02802 'grant_create' => 1,
02803 'priority' => 0,
02804 ),
02805 );
02806 }
02807
02808
02809
02810
02811
02812
02813
02814 function _og_forums_access_template_forum_default() {
02815 return array(
02816 1 => array(
02817 'grant_view' => 0,
02818 'grant_update' => 0,
02819 'grant_delete' => 0,
02820 'grant_create' => 0,
02821 'priority' => 0,
02822 ),
02823 2 => array(
02824 'grant_view' => 1,
02825 'grant_update' => 0,
02826 'grant_delete' => 0,
02827 'grant_create' => 1,
02828 'priority' => 0,
02829 ),
02830 );
02831 }
02832
02833
02834
02835
02836
02837
02838
02839
02840
02841
02842
02843
02844 function _og_forums_no_forums_message($op = 'member', $path) {
02845 $message = '';
02846 switch ($op) {
02847 case 'user':
02848 $message = '<p>'. t('It has encountered a problem: No group forum exists.') .'</p>';
02849 $message .= t('Please inform the <a href="@manager-url">@manager</a> of the group or an administrator. They can solve this problem.', array('@manager-url' => url($path), '@manager' => t('manager')));
02850 break;
02851
02852 case 'manager':
02853 $message = '<p>'. t('It has encountered a problem: No group forum exists.') .'</p>';
02854 $message .= t('Please update the group node to create an group forum. <a href="@edit-node-url">@edit-node</a> and resave the node simply.', array('@edit-node-url' => url($path), '@edit-node' => t('Edit this node')));
02855 break;
02856 }
02857
02858 return $message;
02859 }
02860
02861
02862
02863
02864
02865
02866
02867
02868
02869
02870
02871 function _og_forums_clean_forum_names($forum_names) {
02872 $forum_names = preg_replace("/\;+/", ';', $forum_names);
02873 $forum_names = preg_replace("/,+/", ',', $forum_names);
02874
02875 $forum_names = preg_replace("/^\,|\,$/", '', $forum_names);
02876 $forum_names = preg_replace("/^\;|\;$/", '', $forum_names);
02877
02878 $forum_names = preg_replace("/\;\,|\,\;/", '', $forum_names);
02879
02880 return $forum_names;
02881 }
02882
02883
02884
02885
02886
02887 function og_forum_migrate() {
02888
02889 }