--- og_forum.module.default 2006-06-06 19:10:43.000000000 -0400 +++ og_forum.module 2007-01-12 13:27:07.000000000 -0500 @@ -9,7 +9,6 @@ /* * TODO: - * - Support for multiple forums per group * - Support for both public and private forums */ @@ -27,17 +26,69 @@ function og_forum_help($section) { * Implementation of hook_menu(). */ function og_forum_menu($may_cache) { + global $user; + $items = array(); - if ($may_cache) { + if ($may_cache && FALSE) { $items[] = array( 'path' => 'og_forum', 'callback' => 'og_forum_page', 'type' => MENU_CALLBACK, 'access' => user_access('access content'), ); - } + } else { + // we expect the group nid as 1st argument + if(is_numeric(arg(2))) { + // load forum's group + $group = node_load(arg(2)); + if ($group) { + $result = db_query(og_list_users_sql(0), $group->nid); + $cntall = db_num_rows($result); + $cntpending = 0; + while ($row = db_fetch_object($result)) { + if ($row->uid == $user->uid) { + if ($row->is_admin > 0) { + $items[] = array( + 'title' => t('manage group\'s forum'), + 'path' => 'og_forum/manage', + 'callback' => 'og_forum_manage', + 'callback arguments' => array($group), + 'type' => MENU_CALLBACK, + 'access' => user_access('access content'), + ); + } + break; + } + } + } + } elseif (is_numeric(arg(3)) && is_numeric(arg(4))) { + if(arg(2) == 'add') { + $items[] = array( + 'path' => 'og_forum/manage/add', + 'title' => t('add forum'), + 'callback' => 'og_forum_form_forum', + 'callback arguments' => array(array('parent' => arg(4))), + 'access' => user_access('access content'), + 'type' => MENU_CALLBACK + ); + } elseif(arg(2) == 'edit') { + $term = taxonomy_get_term(arg(4)); + $parents = taxonomy_get_parents($term->tid); + $parent = array_pop($parents); + $term->parent = $parent->tid; + $items[] = array( + 'path' => 'og_forum/manage/edit', + 'title' => t('add forum'), + 'callback' => 'og_forum_form_forum', + 'callback arguments' => array((array)$term), + 'access' => user_access('access content'), + 'type' => MENU_CALLBACK + ); + } + } + } return $items; } @@ -103,8 +154,8 @@ function og_forum_db_rewrite_sql($query, // The forum vocab should have a lower weight than any other vocabulary assigned to forum nodes. static $og_vocab = FALSE; if ($restrict) { - $return['join'] = "INNER JOIN {og_term} ogt ON $primary_table.tid = ogt.tid LEFT JOIN {og_uid} ogu ON ogt.nid = ogu.nid AND ogu.uid = $user->uid"; - $return['where'] = '1=1'; + $return['join'] = "INNER JOIN {og_term} ogt ON $primary_table.tid = ogt.tid INNER JOIN {og_uid} ogu ON ogt.nid = ogu.nid AND ogu.uid = $user->uid"; + $return['where'] = 'ogu.is_active=1'; if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'forum') { $og_nid = intval($_GET['edit']['og_groups'][0]); if ($og_nid && !$og_vocab) { @@ -196,14 +247,28 @@ function og_forum_get_forum_container($g * Implementation of hook_og_create_links(). */ function og_forum_og_create_links($group) { + global $user; + $links = array(); // Get group's forum $forum = og_forum_get_forum($group->nid); if ($forum) { $links[] = l(t('group forum'), "og_forum/$forum/$group->nid", array('title' => t('View group forum discussions.')), "edit[og_groups][]=$group->nid"); - } + // Add forum creation link for the group managers + $result = db_query(og_list_users_sql(0), $group->nid); + $cntall = db_num_rows($result); + $cntpending = 0; + while ($row = db_fetch_object($result)) { + if ($row->uid == $user->uid) { + if ($row->is_admin > 0) { + $links[] = l(t('manage group forums'), 'og_forum/manage/' . $group->nid, array('title' => t('Let you create, edit, delete group\'s forums.'))); + } + break; + } + } + } return $links; } @@ -266,3 +331,215 @@ function og_forum_system_module_validate drupal_set_message(t('The module %module was deactivated--it requires the following disabled/non-existant modules to function properly: %dependencies', array('%module' => $module, '%dependencies' => implode(', ', $missing_dependency_list))), 'error'); } } + + +/** + * Let group managers manage their forums + */ +function og_forum_manage($group) { + global $user; + + // set a nice breadcrumb + _og_forum_manage_breadcrumb($group); + + $content = '

' . t('This page shows the forums associated to the') . ' ' . theme('placeholder', $group->title) . ' ' . t('group.') . '

'; + + $og_terms = _og_forum_ogterms($group); + + $header = array(t('Name'), t('Operations')); + + $tree = taxonomy_get_tree(_forum_get_vid()); + + if ($tree) { + foreach ($tree as $term) { + if(in_array($term->tid, $og_terms)) { + if (in_array($term->tid, variable_get('forum_containers', array()))) { + $rows[] = array(_taxonomy_depth($term->depth) .' '. check_plain($term->name), l(t('add forum'), "og_forum/manage/add/$group->nid/$term->tid")); + } + else { + $rows[] = array(_taxonomy_depth($term->depth) .' '. check_plain($term->name), l(t('edit forum'), "og_forum/manage/edit/$group->nid/$term->tid")); + } + } + } + } + return $content . theme('table', $header, $rows); +} + +/** + * Render forum creation form + */ +function og_forum_form_forum($edit, $group_id = 0, $tid = 0) { + _og_forum_manage_breadcrumb(node_load($group_id)); + + // Handle a delete operation. + if ($_POST['op'] == t('Delete') || $_POST['edit']['confirm']) { + return _og_forum_confirm_delete($edit['tid'], $group_id); + } + + $form['name'] = array('#type' => 'textfield', + '#title' => t('Forum name'), + '#default_value' => $edit['name'], + '#maxlength' => 64, + '#description' => t('The forum name is used to identify related discussions.'), + '#required' => TRUE, + ); + $form['description'] = array('#type' => 'textarea', + '#title' => t('Description'), + '#default_value' => $edit['description'], + '#description' => t('The forum description can give users more information about the discussion topics it contains.'), + ); + if(arg(2) == 'add') { + $form['parent'] = array( + '#type' => 'hidden', + '#value' => $tid, + ); + } + else { + $form['parent'] = array( + '#type' => 'hidden', + '#value' => $edit['parent'], + ); + } + $form['group_id'] = array( + '#type' => 'hidden', + '#value' => $group_id, + ); + $form['weight'] = array('#type' => 'weight', + '#title' => t('Weight'), + '#default_value' => $edit['weight'], + '#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.'), + ); + + $form['vid'] = array('#type' => 'hidden', '#value' => _forum_get_vid()); + $form['submit' ] = array('#type' => 'submit', '#value' => t('Submit')); + if ($edit['tid']) { + $form['delete'] = array('#type' => 'submit', '#value' => t('Delete')); + $form['tid'] = array('#type' => 'hidden', '#value' => $edit['tid']); + } + + return drupal_get_form('og_forum_form_forum', $form, 'og_forum_form'); +} + + +/** + * Validate group id and term id values + */ +function og_forum_form_validate($form_id, $form_values) { + global $user; + + $error = true; + if(is_numeric($form_values['group_id'])) { + $group = node_load($form_values['group_id']); + + // check that the user is the manager + if($group && $group->uid == $user->uid && (in_array($form_values['parent'], _og_forum_ogterms($group)) || $form_values['tid'])) { + $error = false; + } + } + + // stop insertion if we found invalid values + if($error) { + form_set_error('name', t('Internal error processing your data. Please contact website administrators.')); + } +} + + +/** + * Store created/updated forum on the db + */ +function og_forum_form_submit($form_id, $form_values) { + + $container = false; + $type = t('forum'); + + $status = taxonomy_save_term($form_values); + switch ($status) { + case SAVED_NEW: + if ($container) { + $containers = variable_get('forum_containers', array()); + $containers[] = $form_values['tid']; + variable_set('forum_containers', $containers); + } + drupal_set_message(t('Created new %type %term.', array('%term' => theme('placeholder', $form_values['name']), '%type' => $type))); + + // store relation group-forum on the db + db_query('INSERT INTO {og_term} (tid, nid) VALUES (%d, %d)', $form_values['tid'], $form_values['group_id']); + break; + case SAVED_UPDATED: + drupal_set_message(t('The %type %term has been updated.', array('%term' => theme('placeholder', $form_values['name']), '%type' => $type))); + break; + } + + return 'og_forum/manage/'.$form_values['group_id']; +} + + +/** + * Returns a confirmation page for deleting a forum taxonomy term. + * + * @param $tid ID of the term to be deleted + */ +function _og_forum_confirm_delete($tid, $group_id) { + $term = taxonomy_get_term($tid); + + $form['tid'] = array('#type' => 'value', '#value' => $tid); + $form['name'] = array('#type' => 'value', '#value' => $term->name); + $form['group_id'] = array('#type' => 'value', '#value' => $group_id); + + return confirm_form('og_forum_confirm_delete', $form, t('Are you sure you want to delete the forum %name?', array('%name' => theme('placeholder', $term->name))), 'admin/forums', 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')); +} + +/** + * Implementation of forms api _submit call. Deletes a forum after confirmation. + */ +function og_forum_confirm_delete_submit($form_id, $form_values) { + + taxonomy_del_term($form_values['tid']); + + drupal_set_message(t('The forum %term and all sub-forums and associated posts have been deleted.', array('%term' => theme('placeholder', $form_values['name'])))); + + watchdog('content', t('forum: deleted %term and all its sub-forums and associated posts.', array('%term' => theme('placeholder', $form_values['name'])))); + + db_query('DELETE FROM {og_term} WHERE tid = %d', $form_values['tid']); + + return 'og_forum/manage/' . $form_values['group_id']; +} + + +/** + * Set a nice breadcrumb for the manage pages + */ +function _og_forum_manage_breadcrumb($group) { + + $breadcrumb = array(); + + $breadcrumb[] = array( + 'path' => 'forum', + 'title' => $vocabulary->name + ); + + $breadcrumb[] = array( + 'path' => 'node/'. $group->nid, + 'title' => $group->title + ); + + $breadcrumb[] = array( + 'path' => 'og_forum/manage/'. $group->nid, + 'title' => t('Manage') . ' ' . $group->title . ' ' . t('forums') + ); + + menu_set_location($breadcrumb); +} + +/** + * get an array containing terms associated to this og + */ +function _og_forum_ogterms($group) { + $result = db_query('SELECT * FROM {og_term} WHERE nid = %d', $group->nid); + + $og_terms = array(); + while($term = db_fetch_object($result)) { + $og_terms[] = $term->tid; + } + return $og_terms; +}