og_subgroups.module in Subgroups for Organic groups 5
Same filename and directory in other branches
Maintains subgroups hierarchy created by the orgainc groups module.
File
og_subgroups.moduleView source
<?php
/**
* @file
* Maintains subgroups hierarchy created by the orgainc groups module.
*
*/
/**
* Implementation of hook_init().
*
*/
function og_subgroups_init() {
if (module_exists('views')) {
include_once drupal_get_path('module', 'og_subgroups') . '/og_subgroups.views.inc';
}
if (module_exists('workflow_ng')) {
include_once drupal_get_path('module', 'og_subgroups') . '/og_subgroups.workflow_ng.inc';
}
}
/**
* Implementation of hook_perm().
*/
function og_subgroups_perm() {
return array(
'administer groups hierachy',
'edit groups hierachy',
);
}
/**
* Implementation of hook_menu().
*/
function og_subgroups_menu($may_cache) {
$items = array();
if (!$may_cache) {
$items[] = array(
'path' => 'admin/og/subgroups',
'title' => t('Subgroups for organic groups configuration'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'og_subgroups_settings',
),
'access' => user_access('administer groups hierachy'),
'weight' => 1,
);
$arg0 = arg(0);
$arg1 = arg(1);
if ($arg0 == 'node' && is_numeric($arg1)) {
$node = node_load($arg1);
if (og_is_group_type($node->type)) {
$items[] = array(
'path' => 'node/' . arg(1) . '/og/subgroups',
'title' => t('Subgroups'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'og_subgroups_outline',
$arg1,
),
'access' => user_access('edit groups hierachy'),
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
}
}
}
return $items;
}
/**
* API function to set/ update groups hierarchy.
*
* @param $op
* Define the operation 'insert' or 'update'.
* @param $node
* The node object.
* @param $parent_nid
* The node id of the group that will be the parrent
* @param $validate
* Set to TRUE when a validation on the node and parent node is required.
* @param $log
* The log message.
* @return
* boolean indicating if operation preformed in case $validate is enabled.
*/
function og_subgroups_set_hierarchy($op, $node, $parent_nid, $validate = FALSE, $log = NULL) {
$valid = FALSE;
if ($validate) {
// Check node is a group type.
if (og_is_group_type($node->type)) {
// Get all accessible/ inaccesiable groups for the user.
$options = og_subgroups_get_eligable_groups('accessibale');
$inaccessibale = og_subgroups_get_eligable_groups('inaccessibale');
// Check parent_nid is a valid parent.
if (array_key_exists($parent_nid, og_subgroups_tree($options, $node, $inaccessibale))) {
$valid = TRUE;
}
// Check if $op should be insert or update.
$parent = og_subgroups_get_family($node->nid, 'up') ? $op = 'update' : ($op = 'insert');
}
}
else {
$valid = TRUE;
}
if ($valid) {
if ($op == 'insert') {
db_query('INSERT INTO {og_subgroups} (gid, parent) VALUES (%d, %d)', $node->nid, $parent_nid);
}
elseif ($op == 'update') {
db_query('UPDATE {og_subgroups} SET parent = %d WHERE gid = %d', $parent_nid, $node->nid);
}
if ($log) {
db_query("UPDATE {node_revisions} SET log = '%s' WHERE vid = %d", $log, $node->vid);
}
}
return $valid;
}
/**
* API function to remove a groups hierarchy.
*
* @param $node
* The node object.
*/
function og_subgroups_remove_hierarchy($node) {
db_query('DELETE FROM {og_subgroups} WHERE gid = %d', $node->nid);
}
/**
* Menu callback; displays the subgroups configuration page.
*/
function og_subgroups_settings() {
$form['propagte_content'] = array(
'#type' => 'fieldset',
'#title' => t('Group posts propagation'),
'#description' => t('Content can be propagated through the subgroups tree.'),
'#collapsible' => TRUE,
);
$form['propagte_content']['og_subgroups_propagate_content'] = array(
'#type' => 'checkboxes',
'#title' => t('Content propagation'),
'#description' => t('Set the propogation type for posts which are group posts.'),
'#options' => array(
'up' => t('Parents'),
'down' => t('Children'),
'side' => t('Syblings'),
),
'#default_value' => variable_get('og_subgroups_propagate_content', array()),
);
$form['propagte_user'] = array(
'#type' => 'fieldset',
'#title' => t('User propagation'),
'#description' => t('Group members and admins can be propagated through the subgroups tree.'),
'#collapsible' => TRUE,
);
$form['propagte_user']['og_subgroups_propagate_members'] = array(
'#type' => 'checkboxes',
'#title' => t('Members propagation'),
'#description' => t('Set the propogation type for group members.'),
'#options' => array(
'up' => t('Parents'),
'down' => t('Children'),
'side' => t('Syblings'),
),
'#default_value' => variable_get('og_subgroups_propagate_members', array()),
);
$form['propagte_user']['og_subgroups_propagate_demote'] = array(
'#type' => 'checkboxes',
'#title' => t('User demotion'),
'#description' => t("Determine if a demotion action should occur along the subgroups. \n Demotion referes to removing admin and to completely unsubscribing a member/ admin from a group.\n These options will be executed only if the 'Members propagation' is set."),
'#options' => array(
'remove_admin' => t('Removing admin rights (user still remains a member)'),
'unsubscribe' => t('Unsubscribing or denying membership of a user'),
),
'#default_value' => variable_get('og_subgroups_propagate_demote', array()),
);
return system_settings_form($form);
}
/**
* Implementation of hook_block().
*
* Displays the book table of contents in a block when the current page is a
* single-node view inside a group context.
*/
function og_subgroups_block($op = 'list', $delta = 0) {
$block = array();
if ($op == 'list') {
$block[0]['info'] = t('Subgroups');
return $block;
}
else {
if ($op == 'view') {
// Only display this block when the user is browsing inside a group:
$arg = arg(1);
if ($group_node = og_get_group_context()) {
$block['subject'] = t('Subgroups');
$inaccessibale = og_subgroups_get_eligable_groups('inaccessibale');
$block['content'] = og_subgroups_menu_tree($group_node->nid, $group_node->title, $inaccessibale);
}
return $block;
}
}
}
/**
* Given a node, this function returns an array of 'group node' objects
* representing the path in the subroups tree.
*
* @param $node
* A group node object for which to compute the path.
*
* @return
* An array of group node objects representing the path nodes root to
* parent of the given node. Returns an empty array if the node does
* not exist or is not part of a subgroup.
*/
function og_subgroups_get_all_family($gid, $direction, $nodes = array()) {
// Get the syblings by getting the group parent and then its children.
if ($direction == 'side') {
$family = og_subgroups_get_family($gid, 'up');
$family = og_subgroups_get_family($family[0]->gid, 'down');
}
else {
$family = og_subgroups_get_family($gid, $direction);
}
if ($family[0]->gid) {
if ($direction == 'up') {
$nodes = og_subgroups_get_all_family($family[0]->gid, $direction, $nodes);
$nodes[] = $family[0];
}
else {
foreach ($family as $group) {
// We get the result for syblings, no need to recurse.
$nodes[] = $group;
if ($direction == 'down') {
$nodes = og_subgroups_get_all_family($group->gid, $direction, $nodes);
}
}
}
}
return $nodes;
}
/**
* Implementation of hook_nodeapi().
*/
function og_subgroups_nodeapi($node, $op, $teaser = NULL, $page = NULL) {
switch ($op) {
case 'load':
if (og_is_group_type($node->type)) {
$parent = og_subgroups_get_family($node->nid, 'up');
$parent[0]->gid ? $node->og_subgroups = $parent[0]->gid : NULL;
}
break;
case 'insert':
$groups = og_get_node_groups($node);
if ($groups) {
$content_directions = variable_get('og_subgroups_propagate_content', array());
og_subgroups_propogate_content($node, $groups, $content_directions);
}
break;
case 'update':
$groups = og_get_node_groups($node);
if ($groups) {
$content_directions = variable_get('og_subgroups_propagate_content', array());
og_subgroups_propogate_content($node, $groups, $content_directions);
}
break;
case 'delete':
if (og_is_group_type($node->type)) {
db_query('DELETE FROM {og_subgroups} WHERE gid = %d', $node->nid);
}
}
}
/**
* Implementation of hook_og().
*/
function og_subgroups_og($op, $gid, $uid, $args = array()) {
// Don't propgate in the middle of propogation action.
if (!$args['og_subgroups']) {
switch ($op) {
case 'user insert':
og_subgroups_propogate_user($gid, $uid, $args);
break;
case 'user update':
og_subgroups_propogate_user($gid, $uid, $args);
break;
case 'user delete':
og_subgroups_propogate_user_removal($gid, $uid, $args);
break;
}
}
}
/**
* Get the parent/ children for a given group node id.
*
* @param $gid
* The group id.
* @param $direction
* The direction along the subgroups tree (up/ down, side).
*
* @return
* Array with the parent/ children group id.
*/
function og_subgroups_get_family($gid, $direction) {
$return = array();
if ($direction == 'up') {
$sql_gid = "SELECT ogh.parent AS gid FROM {node} n INNER JOIN {og_subgroups} ogh ON n.nid = ogh.gid WHERE n.nid = %d";
}
else {
$sql_gid = "SELECT ogh.gid FROM {node} n INNER JOIN {og_subgroups} ogh ON n.nid = ogh.parent WHERE n.nid = %d";
}
$sql_title = "SELECT title FROM {node} WHERE nid = %d";
$family_gid = db_query($sql_gid, $gid);
while ($result_gid = db_fetch_object($family_gid)) {
$family_title = db_query($sql_title, $result_gid->gid);
$result_title = db_fetch_object($family_title);
$return[] = (object) array(
'gid' => $result_gid->gid,
'title' => $result_title->title,
);
}
return $return;
}
/**
* Helper function to get the accessibale and inaccessiable groups.
*
* @param $op
* The operation
*/
function og_subgroups_get_eligable_groups($op) {
$eligable_groups = og_node_groups_distinguish(og_all_groups_options(), FALSE);
$return = array();
switch ($op) {
case 'accessibale':
// Get all accessible groups for the user.
foreach ($eligable_groups['accessible'] as $key => $group) {
$return[$key] = $group['title'];
}
break;
case 'inaccessibale':
// Get excluded groups.
foreach ($eligable_groups['inaccessible'] as $group) {
$return[$group] = $group;
}
}
return $return;
}
/**
* Implementation of function og_subgroups_outline()
* Handles all subgroups outline operations.
*/
function og_subgroups_outline($nid) {
$node = node_load($nid);
// Get all accessible/ inaccesiable groups for the user.
$options = og_subgroups_get_eligable_groups('accessibale');
$inaccessibale = og_subgroups_get_eligable_groups('inaccessibale');
$description = t('The parent group for this group node in the subgroups.');
if ($inaccessibale) {
$description .= '<br/>' . t("There are some groups which are inaccesible to you and their names appears as <private group>.");
}
$form['parent'] = array(
'#type' => 'select',
'#title' => t('Parent'),
'#default_value' => $node->og_subgroups,
'#options' => og_subgroups_tree($options, $node->nid, $inaccessibale),
'#description' => $description,
);
$form['log'] = array(
'#type' => 'textarea',
'#title' => t('Log message'),
'#description' => t('An explanation to help other group admins understand your motivations to change the subgroups.'),
);
$form['nid'] = array(
'#type' => 'value',
'#value' => $nid,
);
if (isset($node->og_subgroups)) {
$form['update'] = array(
'#type' => 'submit',
'#value' => t('Update subgroups'),
);
$form['remove'] = array(
'#type' => 'submit',
'#value' => t('Remove group from subgroups'),
);
}
else {
$form['add'] = array(
'#type' => 'submit',
'#value' => t('Set subgroups'),
);
}
in_array($node->nid, $inaccessibale) ? $title = t('<private group>') : ($title = check_plain($node->title));
drupal_set_title($title);
return $form;
}
/**
* Handles subgroups form submissions.
*/
function og_subgroups_outline_submit($form_id, $form_values) {
$op = $form_values['op'];
$node = node_load($form_values['nid']);
switch ($op) {
case t('Set subgroups'):
if ($form_values['parent']) {
og_subgroups_set_hierarchy('insert', $node, $form_values['parent'], FALSE, $form_values['log']);
}
drupal_set_message(t('The subgroup has been set.'));
break;
case t('Update subgroups'):
if ($form_values['parent']) {
og_subgroups_set_hierarchy('update', $node, $form_values['parent'], FALSE, $form_values['log']);
}
else {
og_subgroups_remove_hierarchy($node);
}
drupal_set_message(t('The subgroup has been updated.'));
break;
case t('Remove group from subgroups'):
og_subgroups_remove_hierarchy($node);
drupal_set_message(t('Group has been removed from subgroups.'));
break;
}
return "node/{$node->nid}";
}
/**
* Returns an array of titles and groups nids in tree hiearachy order.
*
* @param $groups
* All the accessibale groups.
* @param $exclude
* The current group id that shouldn't appear in the subgroups.
* @param $inaccessibale
* All the inaccessibale groups.
* @param $indent
* The indention to be used.
*
*/
function og_subgroups_tree($groups, $exclude, $inaccessibale, $indent = '') {
$tree = array();
$tree[0] = '<' . t('top-level') . '>';
foreach ($groups as $key => $group) {
// Start recurse only from top level groups.
$parent = og_subgroups_get_all_family($key, 'up');
// Get the top level parent of a group.
if ($parent[0]->gid) {
$gid = $parent[0]->gid;
$title = $parent[0]->title;
}
else {
$gid = $key;
$title = $group;
}
// Set the top level group. Make sure the gid is part of the accessible groups.
if (empty($tree[$gid]) && $gid != $exclude) {
!in_array($gid, $inaccessibale) ? $tree[$gid] = $title : ($tree[$gid] = t('<private group>'));
$tree = og_subgroups_tree_recurse($gid, $exclude, $inaccessibale, $tree, $indent . '--');
}
}
return $tree;
}
/**
* Helper function for og_subgroups_tree().
*/
function og_subgroups_tree_recurse($gid, $exclude, $inaccessibale, $tree, $indent) {
$children = og_subgroups_get_family($gid, 'down');
foreach ($children as $node) {
if ($node->gid != $exclude) {
!in_array($node->gid, $inaccessibale) ? $title = $node->title : ($title = '<private group>');
$tree[$node->gid] = $indent . ' ' . $title;
$tree = og_subgroups_tree_recurse($node->gid, $exclude, $inaccessibale, $tree, $indent . '--');
}
}
return $tree;
}
/**
* Returns an HTML nested list (wrapped in a menu-class div) representing
* the group nodes as a tree.
*
* @param $gid
* The group id.
* @param $title
* The title of the group the node belongs to.
*
* @return
* Menu class nested list.
*/
function og_subgroups_menu_tree($gid, $title, $inaccessibale) {
// Get the top level.
$top = og_subgroups_get_all_family($gid, 'up');
// Group might be already top-level.
if (empty($top[0])) {
$top[0]->gid = $gid;
$top[0]->title = $title;
}
// Get children of the top level.
$tree = og_subgroups_menu_tree_recurse($top[0]->gid, $inaccessibale);
// We always have the top level group.
$tree ? $output .= '<ul class="menu"><li class="expanded">' : ($output .= '<ul class="menu"><li class="leaf">');
$output .= l($top[0]->title, 'node/' . $top[0]->gid);
$tree ? $output .= '<ul class="menu">' . $tree . '</ul>' : NULL;
$output .= '</ul></li>';
return $output;
}
/**
* Helper function for og_subgroups_menu_tree()
*/
function og_subgroups_menu_tree_recurse($gid, $inaccessibale, $children = array()) {
$children = og_subgroups_get_family($gid, 'down');
foreach ($children as $node) {
// Check group isn't inaccessibale for user.
!in_array($node->gid, $inaccessibale) ? $title = $node->title : ($title = t('<private group>'));
if ($tree = og_subgroups_menu_tree_recurse($node->gid, $inaccessibale, $children)) {
$output .= '<li class="expanded">';
$output .= l($title, 'node/' . $node->gid);
$output .= '<ul class="menu">' . $tree . '</ul>';
$output .= '</li>';
}
else {
$output .= '<li class="leaf">' . l($title, 'node/' . $node->gid) . '</li>';
}
}
return $output;
}
/**
* Propagates content along the subgroups tree.
*
* @param $node
* The node object that needs to be propagated
* @param $groups
* The groups the node is currently assigend to
* @param $directions
* Array with the direction propogation should happen (parent, children, syblings)
*
*/
function og_subgroups_propogate_content($node, $groups, $directions) {
$save = FALSE;
$directions = array_filter($directions);
foreach ($directions as $direction) {
foreach ($groups as $group => $foo) {
// Get the path to the top level group.
$trees = og_subgroups_get_all_family($group, $direction);
foreach ($trees as $tree) {
// Check that group isn't already there.
$save = TRUE;
if (!in_array($tree->gid, $node->og_groups)) {
$node->og_groups[] = $tree->gid;
$node->og_groups_both[$tree->gid] = $tree->title;
}
}
}
}
// Save node only if changed.
if ($save) {
og_save_ancestry($node);
}
}
/**
* Propagates members and admins along the subgroups tree.
*
* @param $gid
* The group id.
* @param $uid
* The propagated user id.
* @param $args
* Array with the updated action, as returned from hook_og()
*
*/
function og_subgroups_propogate_user($gid, $uid, $args) {
$demote = variable_get('og_subgroups_propagate_demote', array());
// User's membership was approved.
// User was promoted to group admin.
// User was demoted from admin and demote action should occur.
if ($args['is_active'] == 1 || $args['is_admin'] == 1 || $args['is_admin'] == 0 && $demote['remove_admin']) {
$tree = _og_subgroups_propogate_user_get_tree($gid);
$user = user_load(array(
'uid' => $uid,
));
$args['is_admin'] ? $is_admin = 1 : ($is_admin = 0);
foreach ($tree as $gid => $foo) {
// Check user isn't already subscribed with same rights.
if (empty($user->og_groups[$gid]) || $user->og_groups[$gid]['is_active'] != 1 || $user->og_groups[$gid]['is_admin'] != $is_admin) {
// Pass in the $args info about propogation done by og_subgroups module.
og_save_subscription($gid, $uid, array(
'is_active' => 1,
'is_admin' => $is_admin,
'og_subgroups' => TRUE,
));
}
}
}
}
function og_subgroups_propogate_user_removal($gid, $uid) {
$demote = variable_get('og_subgroups_propagate_demote', array());
if ($demote['unsubscribe']) {
$tree = _og_subgroups_propogate_user_get_tree($gid);
$user = user_load(array(
'uid' => $uid,
));
foreach ($tree as $gid => $foo) {
// Check user isn't the manager of the group.
$sql = "SELECT uid FROM {node} WHERE nid = %d";
$manager_uid = db_fetch_object(db_query($sql, $gid));
if ($uid != $manager_uid->uid) {
og_delete_subscription($gid, $uid, array(
'og_subgroups' => TRUE,
));
}
}
}
}
/**
* Helper function for og_subgroups_propogate_user().
*
* @param $gid
* The group id.
* @return
* Array with the group nids for propagation.
*/
function _og_subgroups_propogate_user_get_tree($gid) {
$tree = array();
$directions = variable_get('og_subgroups_propagate_members', array());
foreach ($directions as $direction) {
if ($direction) {
// Get the groups member should be propagated.
$trees = og_subgroups_get_all_family($gid, $direction);
foreach ($trees as $group) {
$tree[$group->gid] = $group->title;
}
}
}
return $tree;
}
Functions
Name | Description |
---|---|
og_subgroups_block | Implementation of hook_block(). |
og_subgroups_get_all_family | Given a node, this function returns an array of 'group node' objects representing the path in the subroups tree. |
og_subgroups_get_eligable_groups | Helper function to get the accessibale and inaccessiable groups. |
og_subgroups_get_family | Get the parent/ children for a given group node id. |
og_subgroups_init | Implementation of hook_init(). |
og_subgroups_menu | Implementation of hook_menu(). |
og_subgroups_menu_tree | Returns an HTML nested list (wrapped in a menu-class div) representing the group nodes as a tree. |
og_subgroups_menu_tree_recurse | Helper function for og_subgroups_menu_tree() |
og_subgroups_nodeapi | Implementation of hook_nodeapi(). |
og_subgroups_og | Implementation of hook_og(). |
og_subgroups_outline | Implementation of function og_subgroups_outline() Handles all subgroups outline operations. |
og_subgroups_outline_submit | Handles subgroups form submissions. |
og_subgroups_perm | Implementation of hook_perm(). |
og_subgroups_propogate_content | Propagates content along the subgroups tree. |
og_subgroups_propogate_user | Propagates members and admins along the subgroups tree. |
og_subgroups_propogate_user_removal | |
og_subgroups_remove_hierarchy | API function to remove a groups hierarchy. |
og_subgroups_settings | Menu callback; displays the subgroups configuration page. |
og_subgroups_set_hierarchy | API function to set/ update groups hierarchy. |
og_subgroups_tree | Returns an array of titles and groups nids in tree hiearachy order. |
og_subgroups_tree_recurse | Helper function for og_subgroups_tree(). |
_og_subgroups_propogate_user_get_tree | Helper function for og_subgroups_propogate_user(). |