View source
<?php
define(ANNOUNCE_LOG_LENGTH, 60 * 60 * 24 * 14);
function spaces_announce_spaces_settings() {
$items = array();
$content_types = node_get_types('types');
foreach ($content_types as $k => $type) {
if (variable_get('spaces_announce_' . $k, false)) {
$items[$k . '_announce'] = array(
'label' => t('!type Announcements', array(
'!type' => $type->name,
)),
'description' => t("Accept incoming !type annoucements", array(
'!type' => $type->name,
)),
'options' => array(
'0' => 'Off',
1 => 'On',
),
);
}
}
return $items;
}
function spaces_announce_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
if (variable_get('spaces_announce_' . $node->type, false)) {
switch ($op) {
case 'view':
if ($gid = spaces_announce_primary_gid($node->nid)) {
if (spaces_gid() != $gid) {
if ($page) {
db_query("UPDATE {spaces_announce_log} a SET a.count = a.count + 1, a.viewed = %d WHERE a.nid = %d", time(), $node->nid);
context_set('spaces', 'annoucement', $node->type);
$context = new StdClass();
$context->space = 'spaces';
$context->key = 'feature';
$context->value = $node->type . '_announcement';
if ($context = context_ui_context('load', $context)) {
context_set($context->space, $context->key, $context->value);
}
}
$group = node_load($gid);
$group_name = spaces_is_member($gid) ? l($group->title, 'node/' . $gid) : $group->title;
$node->content['announce_primary_group'] = array(
'#value' => theme('announce_group_label', $group_name),
'#weight' => -100,
);
}
}
break;
case 'update':
if ($node->type == 'book' && $node->parent == 0) {
_spaces_announce_apply_book_perms($node);
}
if (spaces_announce_primary_gid($node->nid)) {
break;
}
case 'insert':
db_query('INSERT INTO {spaces_announce} (nid, gid) VALUES (%d, %d)', $node->nid, spaces_gid());
db_query('INSERT INTO {spaces_announce_log} (nid) VALUES (%d)', $node->nid);
break;
case 'delete':
db_query('DELETE FROM {spaces_announce} WHERE nid = %d', $node->nid);
db_query('DELETE FROM {spaces_announce_log} WHERE nid = %d', $node->nid);
break;
}
}
}
function spaces_announce_form_alter($form_id, &$form) {
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
$form['workflow']['spaces_announce'] = array(
'#type' => 'checkbox',
'#title' => t('Enable announcements'),
'#default_value' => variable_get('spaces_announce_' . $form['#node_type']->type, 0),
'#description' => t('Allow this content type to be broadcast from one group to another.'),
);
}
if (isset($form['type']) && $form['type']['#value'] . '_node_form' && variable_get('spaces_announce_' . $form['type']['#value'], 0)) {
global $user;
$gid = spaces_gid();
if (is_numeric($form['#node']->nid)) {
$primary_gid = spaces_announce_primary_gid($form['#node']->nid);
if (!$primary_gid && in_array($gid, $form['#node']->og_groups)) {
$primary_gid = $gid;
}
}
if (!isset($primary_gid) || $primary_gid == $gid) {
$gids = array();
if (user_access('administer nodes')) {
$result = db_query("SELECT DISTINCT(gid), title FROM {spaces_features} i INNER JOIN {og_uid} og ON i.gid = og.nid INNER JOIN {node} n ON og.nid = n.nid WHERE i.id ='%s' AND i.type = 1 AND i.value <> 0", $form['type']['#value'] . '_announce');
}
else {
$result = db_query("SELECT DISTINCT(gid), title FROM {spaces_features} i INNER JOIN {og_uid} og ON i.gid = og.nid INNER JOIN {node} n ON og.nid = n.nid WHERE i.id ='%s' AND i.type = 1 AND i.value <> 0 AND og.uid = %d", $form['type']['#value'] . '_announce', $user->uid);
}
while ($row = db_fetch_object($result)) {
$gids[$row->gid] = $row->title;
}
unset($gids[$gid]);
if (isset($primary_gid)) {
$active_gids = array_diff($form['#node']->og_groups, array(
$primary_gid,
));
}
else {
$active_gids = array();
}
if (isset($form['parent']) && $form['parent']['#default_value'] !== null) {
$parent = node_load($form['parent']['#default_value']);
$active_gids = array_merge($active_gids, $parent->og_groups);
$parent_set = true;
}
if (count($gids)) {
if ($parent_set) {
$form['announce_display'] = array(
'#type' => 'checkboxes',
'#title' => t('Broadcast to'),
'#description' => t('Broadcast settings are controled by at the root of the book.'),
'#default_value' => $active_gids,
'#options' => $gids,
'#disabled' => true,
);
$form['announce_hidden'] = array(
'#type' => 'hidden',
'#value' => serialize($active_gids),
);
}
else {
$form['announce'] = array(
'#type' => 'checkboxes',
'#title' => t('Broadcast to'),
'#description' => t('Selected groups will recieve this post.'),
'#default_value' => $active_gids,
'#options' => $gids,
);
}
$form['#after_build'][] = 'spaces_announce_group_save';
}
}
else {
$link = cl(t('Click here'), spaces_group_path($primary_gid) . '/node/' . $form['#node']->nid, array(), null, null, false, false, true);
drupal_set_message(t('This !type was not authored in this group and should not be edited here. !click_here to edit where it was created.', array(
'!type' => $form['type']['#value'],
'!click_here' => $link,
)));
}
}
elseif ($form_id == 'intranet_features_form') {
$form['settings']['intranet_home']['#options']['announcements'] = t('Announcements');
}
}
function spaces_announce_group_save($form, $form_values) {
if (count($form['#post'])) {
$gid = spaces_gid();
$gids[$gid] = $gid;
if (count($form['#post']['announce'])) {
$announce_gids = $form['#post']['announce'];
}
elseif (isset($form['#post']['announce_hidden'])) {
$announce_gids = unserialize($form['#post']['announce_hidden']);
}
if (is_array($announce_gids)) {
foreach ($announce_gids as $value) {
$gids[$value] = $value;
}
form_set_value($form['og_nodeapi']['visible']['og_groups'], $gids);
}
}
return $form;
}
function _spaces_announce_apply_book_perms($node) {
$result = db_query('SELECT b.nid, n.type FROM {book} b INNER JOIN {node} n ON b.vid = n.vid WHERE parent = %d', $node->nid);
while ($child = db_fetch_object($result)) {
$child->og_groups = $node->og_groups;
$child->og_public = $node->og_public;
og_save_ancestry($child);
node_access_acquire_grants($child);
_spaces_announce_apply_book_perms($child);
}
}
function spaces_announce_primary_gid($nid) {
return db_result(db_query('SELECT gid FROM {spaces_announce} WHERE nid = %d', $nid));
}
function spaces_announce_log($nid) {
return db_fetch_object(db_query('SELECT count, viewed FROM {spaces_announce_log} WHERE nid = %d', $nid));
}
function spaces_announce_views_tables() {
$tables['spaces_announce'] = array(
'name' => 'spaces_announce',
'join' => array(
'left' => array(
'table' => 'node',
'field' => 'nid',
),
'right' => array(
'field' => 'nid',
),
),
'filters' => array(
'announce_cg' => array(
'name' => t('Announce: Posted in current group'),
'help' => t('Announcement was posted in current group. If page is not in any group context, no nodes are listed and thus a block would not appear.'),
'operator' => 'views_handler_operator_eqneq',
'list' => 'views_handler_filter_groupcurrent',
'list-type' => 'select',
'handler' => 'spaces_announce_handler_filter_cg',
),
),
);
$tables['spaces_announce_log'] = array(
'name' => 'spaces_announce_log',
'join' => array(
'left' => array(
'table' => 'node',
'field' => 'nid',
),
'right' => array(
'field' => 'nid',
),
),
'fields' => array(
'count' => array(
'name' => t('Announce: Views Count'),
'sortable' => true,
'help' => t('This will display the number of times a node has been read.'),
),
'viewed' => array(
'name' => t('Announce: Last Viewed Time'),
'sortable' => true,
'handler' => views_handler_field_dates(),
'option' => 'string',
'help' => t('Display the time the node was last read.'),
),
),
);
return $tables;
}
function spaces_announce_views_arguments() {
$args = array(
'announce_gid' => array(
'name' => t("Announce: Primary group nid(s)"),
'handler' => 'spaces_announce_handler_argument_gid',
'help' => t('Filter for posts by primary group.'),
),
);
return $args;
}
function spaces_announce_handler_argument_gid($op, &$query, $argtype, $arg = '') {
switch ($op) {
case 'summary':
return;
case 'link':
break;
case 'sort':
break;
case 'filter':
$query
->ensure_table('spaces_announce');
$query
->add_where('spaces_announce.gid = %d', $arg);
break;
case 'title':
return db_result(db_query_range('SELECT title FROM {node} WHERE nid = %d', $query, 0, 1));
}
}
function spaces_announce_handler_filter_cg($op, $filter, $filterinfo, &$query) {
$query
->ensure_table('spaces_announce');
$query
->add_where("spaces_announce.gid " . $filter['operator'] . " ***CURRENT_GID***");
}
function theme_announce_group_label($link) {
return "<p class='announcement-label'><strong>" . t('Announcement from ') . $link . "</strong></p>";
}