| 1 |
<?php
|
| 2 |
// $Id: spaces_announce.module,v 1.2.6.1 2008/09/19 13:48:54 yhahn Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Announcement module lets spaces broadcast to each other.
|
| 6 |
*
|
| 7 |
* TODO
|
| 8 |
* - Evaluate removing the annoucement log in favor or using statistics module (we'll use the
|
| 9 |
* ability to measure if a post was viewed outsite of the primary group, but I'm not sure that's
|
| 10 |
* really even valuable.)
|
| 11 |
* - Complete views argument support.
|
| 12 |
*/
|
| 13 |
|
| 14 |
define(ANNOUNCE_LOG_LENGTH, 60*60*24*14); // Two weeks.
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Implementation of hook_spaces_settings
|
| 18 |
*/
|
| 19 |
function spaces_announce_spaces_settings() {
|
| 20 |
$items = array();
|
| 21 |
$content_types = node_get_types('types');
|
| 22 |
foreach ($content_types as $k => $type) {
|
| 23 |
if (variable_get('spaces_announce_'. $k, false)) {
|
| 24 |
$items[$k .'_announce'] = array(
|
| 25 |
'label' => t('!type Announcements', array('!type' => $type->name)),
|
| 26 |
'description' => t("Accept incoming !type annoucements", array('!type' => $type->name)),
|
| 27 |
'options' => array('0' => 'Off', 1 => 'On'),
|
| 28 |
);
|
| 29 |
}
|
| 30 |
}
|
| 31 |
return $items;
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Implementation of hook_nodeapi
|
| 36 |
*
|
| 37 |
* Log viewing on view, and save primary gid relation on update and insert.
|
| 38 |
*/
|
| 39 |
function spaces_announce_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
|
| 40 |
if (variable_get('spaces_announce_'. $node->type, false)) {
|
| 41 |
switch ($op) {
|
| 42 |
case 'view':
|
| 43 |
$space = spaces_get_space();
|
| 44 |
if ($gid = spaces_announce_primary_gid($node->nid)) {
|
| 45 |
if ($space->sid != $gid) {
|
| 46 |
if ($page) {
|
| 47 |
// If this is not the primary group than then node is being
|
| 48 |
// recieved, so we do two things; log the view, and set context.
|
| 49 |
db_query("UPDATE {spaces_announce_log} a SET a.count = a.count + 1, a.viewed = %d WHERE a.nid = %d", time(), $node->nid);
|
| 50 |
context_set('spaces', 'annoucement', $node->type);
|
| 51 |
|
| 52 |
// If a ' NODETYPE_announcement' feature is defined give it the chance to
|
| 53 |
// become the active feature.
|
| 54 |
$context = new StdClass();
|
| 55 |
$context->space = 'spaces';
|
| 56 |
$context->key = 'feature';
|
| 57 |
$context->value = $node->type .'_announcement';
|
| 58 |
if ($context = context_ui_context('load', $context)) {
|
| 59 |
context_set($context->space, $context->key, $context->value);
|
| 60 |
}
|
| 61 |
}
|
| 62 |
// Add "from" text to content.
|
| 63 |
$group = node_load($gid);
|
| 64 |
$group_name = spaces_is_member($gid) ? l($group->title, 'node/'. $gid) : $group->title;
|
| 65 |
$node->content['announce_primary_group'] = array(
|
| 66 |
'#value' => theme('announce_group_label', $group_name),
|
| 67 |
'#weight' => -100,
|
| 68 |
);
|
| 69 |
}
|
| 70 |
}
|
| 71 |
break;
|
| 72 |
case 'update':
|
| 73 |
// If the annoucement settings have been changed for a root book page
|
| 74 |
// push those changes to the rest of the book.
|
| 75 |
if ($node->type == 'book' && $node->parent == 0) {
|
| 76 |
_spaces_announce_apply_book_perms($node);
|
| 77 |
}
|
| 78 |
|
| 79 |
// Check to see if the post has a primary_gid, if it doesn't cascade
|
| 80 |
// into the 'insert' case so that the primary_gid is silently corrected.
|
| 81 |
if (spaces_announce_primary_gid($node->nid)) {
|
| 82 |
break;
|
| 83 |
}
|
| 84 |
case 'insert':
|
| 85 |
$space = spaces_get_space();
|
| 86 |
db_query('INSERT INTO {spaces_announce} (nid, gid) VALUES (%d, %d)', $node->nid, $space->sid);
|
| 87 |
db_query('INSERT INTO {spaces_announce_log} (nid) VALUES (%d)', $node->nid);
|
| 88 |
break;
|
| 89 |
case 'delete':
|
| 90 |
db_query('DELETE FROM {spaces_announce} WHERE nid = %d', $node->nid);
|
| 91 |
db_query('DELETE FROM {spaces_announce_log} WHERE nid = %d', $node->nid);
|
| 92 |
break;
|
| 93 |
}
|
| 94 |
}
|
| 95 |
}
|
| 96 |
|
| 97 |
/**
|
| 98 |
* Implementation of hook_form_alter.
|
| 99 |
*/
|
| 100 |
function spaces_announce_form_alter($form_id, &$form) {
|
| 101 |
// Add check box to nodetype forms
|
| 102 |
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
|
| 103 |
$form['workflow']['spaces_announce'] = array(
|
| 104 |
'#type' => 'checkbox',
|
| 105 |
'#title' => t('Enable announcements'),
|
| 106 |
'#default_value' => variable_get('spaces_announce_'. $form['#node_type']->type, 0),
|
| 107 |
'#description' => t('Allow this content type to be broadcast from one group to another.'),
|
| 108 |
);
|
| 109 |
}
|
| 110 |
// Add group checkboxes to node forms.
|
| 111 |
if (isset($form['type']) && ($form['type']['#value'] .'_node_form') && variable_get('spaces_announce_'. $form['type']['#value'], 0)) {
|
| 112 |
global $user;
|
| 113 |
$space = spaces_get_space();
|
| 114 |
$gid = $space->sid;
|
| 115 |
// Retrieve the primary_gid for exiting nodes.
|
| 116 |
if (is_numeric($form['#node']->nid)) {
|
| 117 |
$primary_gid = spaces_announce_primary_gid($form['#node']->nid);
|
| 118 |
|
| 119 |
// It's possible that a post doesn't have an assigned primary gid, so if it's in the
|
| 120 |
// current group we assume that the current group is the primary one, and set the
|
| 121 |
// primary_gid so that it can be respected and saved on form submission.
|
| 122 |
if (!$primary_gid && in_array($gid, $form['#node']->og_groups)) {
|
| 123 |
$primary_gid = $gid;
|
| 124 |
}
|
| 125 |
}
|
| 126 |
|
| 127 |
if (!isset($primary_gid) || $primary_gid == $gid) {
|
| 128 |
// Retrieve groups which the current user is a member and where the announce setting is enabled.
|
| 129 |
$gids = array();
|
| 130 |
// If the user has admin nodes they can edit annoucements they didn't author and will need be
|
| 131 |
// able to post posts in any groups which can recieve posts.
|
| 132 |
if (user_access('administer nodes')) {
|
| 133 |
$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');
|
| 134 |
}
|
| 135 |
else {
|
| 136 |
$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);
|
| 137 |
}
|
| 138 |
while ($row = db_fetch_object($result)) {
|
| 139 |
$gids[$row->gid] = $row->title;
|
| 140 |
}
|
| 141 |
// Remove the current group from broadcast options.
|
| 142 |
unset($gids[$gid]);
|
| 143 |
|
| 144 |
// If the node has groups, present those - minus the primary groups, otherwise use the spaces_gid.
|
| 145 |
if (isset($primary_gid)) {
|
| 146 |
$active_gids = array_diff($form['#node']->og_groups, array($primary_gid));
|
| 147 |
}
|
| 148 |
else {
|
| 149 |
$active_gids = array();
|
| 150 |
}
|
| 151 |
|
| 152 |
if (isset($form['parent']) && $form['parent']['#default_value'] !== null) {
|
| 153 |
$parent = node_load($form['parent']['#default_value']);
|
| 154 |
$active_gids = array_merge($active_gids, $parent->og_groups);
|
| 155 |
$parent_set = true;
|
| 156 |
}
|
| 157 |
|
| 158 |
if (count($gids)) {
|
| 159 |
if ($parent_set) {
|
| 160 |
$form['announce_display'] = array(
|
| 161 |
'#type' => 'checkboxes',
|
| 162 |
'#title' => t('Broadcast to'),
|
| 163 |
'#description' => t('Broadcast settings are controled by at the root of the book.'),
|
| 164 |
// '#value' => $active_gids,
|
| 165 |
'#default_value' => $active_gids,
|
| 166 |
'#options' => $gids,
|
| 167 |
'#disabled' => true,
|
| 168 |
);
|
| 169 |
$form['announce_hidden'] = array(
|
| 170 |
'#type' => 'hidden',
|
| 171 |
'#value' => serialize($active_gids),
|
| 172 |
);
|
| 173 |
}
|
| 174 |
else {
|
| 175 |
$form['announce'] = array(
|
| 176 |
'#type' => 'checkboxes',
|
| 177 |
'#title' => t('Broadcast to'),
|
| 178 |
'#description' => t('Selected groups will recieve this post.'),
|
| 179 |
'#default_value' => $active_gids,
|
| 180 |
'#options' => $gids,
|
| 181 |
);
|
| 182 |
}
|
| 183 |
|
| 184 |
// Add after_build function to save extra og relations.
|
| 185 |
$form['#after_build'][] = 'spaces_announce_group_save';
|
| 186 |
}
|
| 187 |
}
|
| 188 |
else {
|
| 189 |
$link = cl(t('Click here'), spaces_group_path($primary_gid) .'/node/'. $form['#node']->nid, array(), null, null, false, false, true);
|
| 190 |
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)));
|
| 191 |
}
|
| 192 |
}
|
| 193 |
elseif ($form_id == 'intranet_features_form') {
|
| 194 |
$form['settings']['intranet_home']['#options']['announcements'] = t('Announcements');
|
| 195 |
}
|
| 196 |
}
|
| 197 |
|
| 198 |
/**
|
| 199 |
* After build handler for announcement node forms.
|
| 200 |
*/
|
| 201 |
function spaces_announce_group_save($form, $form_values) {
|
| 202 |
if (count($form['#post'])) {
|
| 203 |
$space = spaces_get_space();
|
| 204 |
$gid = $space->sid;
|
| 205 |
$gids[$gid] = $gid;
|
| 206 |
if (count($form['#post']['announce'])) {
|
| 207 |
$announce_gids = $form['#post']['announce'];
|
| 208 |
}
|
| 209 |
elseif (isset($form['#post']['announce_hidden'])) {
|
| 210 |
$announce_gids = unserialize($form['#post']['announce_hidden']);
|
| 211 |
}
|
| 212 |
|
| 213 |
if (is_array($announce_gids)) {
|
| 214 |
foreach ($announce_gids as $value) {
|
| 215 |
$gids[$value] = $value;
|
| 216 |
}
|
| 217 |
form_set_value($form['og_nodeapi']['visible']['og_groups'], $gids);
|
| 218 |
}
|
| 219 |
}
|
| 220 |
return $form;
|
| 221 |
}
|
| 222 |
|
| 223 |
/**
|
| 224 |
* Recursive function to walk a book and propagate OG changes.
|
| 225 |
*
|
| 226 |
* @param $node
|
| 227 |
* A skeletal node object, needs the following properties; nid, og_groups, og_public
|
| 228 |
*/
|
| 229 |
function _spaces_announce_apply_book_perms($node) {
|
| 230 |
$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);
|
| 231 |
while ($child = db_fetch_object($result)) {
|
| 232 |
// Add groups and privacy setting from parent.
|
| 233 |
$child->og_groups = $node->og_groups;
|
| 234 |
$child->og_public = $node->og_public;
|
| 235 |
|
| 236 |
// Save new group relations.
|
| 237 |
og_save_ancestry($child);
|
| 238 |
// Process access changes.
|
| 239 |
node_access_acquire_grants($child);
|
| 240 |
// Recurse.
|
| 241 |
_spaces_announce_apply_book_perms($child);
|
| 242 |
}
|
| 243 |
}
|
| 244 |
|
| 245 |
/**
|
| 246 |
* Retrieve the primary gid for a node.
|
| 247 |
*
|
| 248 |
* @param $nid
|
| 249 |
* The node id of the item
|
| 250 |
*
|
| 251 |
* @return
|
| 252 |
* Gid ie node id of the primary group.
|
| 253 |
*/
|
| 254 |
function spaces_announce_primary_gid($nid) {
|
| 255 |
return db_result(db_query('SELECT gid FROM {spaces_announce} WHERE nid = %d', $nid));
|
| 256 |
}
|
| 257 |
|
| 258 |
/**
|
| 259 |
* Retrieve the view log for a announcement
|
| 260 |
*/
|
| 261 |
function spaces_announce_log($nid) {
|
| 262 |
return db_fetch_object(db_query('SELECT count, viewed FROM {spaces_announce_log} WHERE nid = %d', $nid));
|
| 263 |
}
|
| 264 |
|
| 265 |
/**
|
| 266 |
* Implementation of hook_views_tables.
|
| 267 |
*/
|
| 268 |
function spaces_announce_views_tables() {
|
| 269 |
$tables['spaces_announce'] = array(
|
| 270 |
'name' => 'spaces_announce',
|
| 271 |
// 'provider' => 'internal',
|
| 272 |
'join' => array(
|
| 273 |
'left' => array(
|
| 274 |
'table' => 'node',
|
| 275 |
'field' => 'nid'
|
| 276 |
),
|
| 277 |
'right' => array(
|
| 278 |
'field' => 'nid'
|
| 279 |
)
|
| 280 |
),
|
| 281 |
'filters' => array(
|
| 282 |
'announce_cg' => array(
|
| 283 |
'name' => t('Announce: Posted in current group'),
|
| 284 |
'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.'),
|
| 285 |
'operator' => 'views_handler_operator_eqneq',
|
| 286 |
'list' => 'views_handler_filter_groupcurrent',
|
| 287 |
'list-type' => 'select',
|
| 288 |
'handler' => 'spaces_announce_handler_filter_cg',
|
| 289 |
),
|
| 290 |
),
|
| 291 |
);
|
| 292 |
$tables['spaces_announce_log'] = array(
|
| 293 |
'name' => 'spaces_announce_log',
|
| 294 |
// 'provider' => 'internal',
|
| 295 |
'join' => array(
|
| 296 |
'left' => array(
|
| 297 |
'table' => 'node',
|
| 298 |
'field' => 'nid'
|
| 299 |
),
|
| 300 |
'right' => array(
|
| 301 |
'field' => 'nid'
|
| 302 |
)
|
| 303 |
),
|
| 304 |
'fields' => array(
|
| 305 |
'count' => array(
|
| 306 |
'name' => t('Announce: Views Count'),
|
| 307 |
'sortable' => true,
|
| 308 |
'help' => t('This will display the number of times a node has been read.'),
|
| 309 |
),
|
| 310 |
'viewed' => array(
|
| 311 |
'name' => t('Announce: Last Viewed Time'),
|
| 312 |
'sortable' => true,
|
| 313 |
'handler' => views_handler_field_dates(),
|
| 314 |
'option' => 'string',
|
| 315 |
'help' => t('Display the time the node was last read.'),
|
| 316 |
),
|
| 317 |
),
|
| 318 |
);
|
| 319 |
return $tables;
|
| 320 |
}
|
| 321 |
|
| 322 |
/**
|
| 323 |
* Implementation of hook_views_arguments()
|
| 324 |
*/
|
| 325 |
function spaces_announce_views_arguments() {
|
| 326 |
$args = array(
|
| 327 |
'announce_gid' => array(
|
| 328 |
'name' => t("Announce: Primary group nid(s)"),
|
| 329 |
'handler' => 'spaces_announce_handler_argument_gid',
|
| 330 |
'help' => t('Filter for posts by primary group.'),
|
| 331 |
),
|
| 332 |
);
|
| 333 |
return $args;
|
| 334 |
}
|
| 335 |
|
| 336 |
/**
|
| 337 |
* Argument handler for primary group arg
|
| 338 |
*/
|
| 339 |
function spaces_announce_handler_argument_gid($op, &$query, $argtype, $arg = '') {
|
| 340 |
switch ($op) {
|
| 341 |
case 'summary':
|
| 342 |
// TODO implement a summary view
|
| 343 |
return;
|
| 344 |
case 'link':
|
| 345 |
// TODO implement link op for summary view
|
| 346 |
break;
|
| 347 |
case 'sort':
|
| 348 |
// TODO implement sort op for summary view
|
| 349 |
break;
|
| 350 |
case 'filter':
|
| 351 |
$query->ensure_table('spaces_announce');
|
| 352 |
$query->add_where('spaces_announce.gid = %d', $arg);
|
| 353 |
break;
|
| 354 |
case 'title':
|
| 355 |
return db_result(db_query_range('SELECT title FROM {node} WHERE nid = %d', $query, 0, 1));;
|
| 356 |
}
|
| 357 |
}
|
| 358 |
|
| 359 |
/**
|
| 360 |
* From picg in OG; "Lovely filter handler which restricts posts to the current group. Useful for group related blocks."
|
| 361 |
*
|
| 362 |
* @return void
|
| 363 |
**/
|
| 364 |
function spaces_announce_handler_filter_cg($op, $filter, $filterinfo, &$query) {
|
| 365 |
$query->ensure_table('spaces_announce');
|
| 366 |
$query->add_where("spaces_announce.gid ". $filter['operator'] ." ***CURRENT_GID***");
|
| 367 |
}
|
| 368 |
|
| 369 |
function theme_announce_group_label($link) {
|
| 370 |
return "<p class='announcement-label'><strong>". t('Announcement from ') . $link ."</strong></p>";
|
| 371 |
}
|