| 1 |
<?php
|
| 2 |
// $Id: subscriptions_taxonomy.module,v 1.12 2009/07/20 21:22:21 salvis Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Subscriptions to taxonomy terms.
|
| 7 |
*
|
| 8 |
* Subscriptions_taxonomy extends the subscription module to allow users to
|
| 9 |
* subscribe by taxonomy term. If a user subscribes to a term he will receive
|
| 10 |
* notifications each time a node is published to that taxonomy term. The user
|
| 11 |
* can also select to receive notifications when such a node is updated or
|
| 12 |
* commented.
|
| 13 |
*/
|
| 14 |
|
| 15 |
/**
|
| 16 |
* Implementation of hook_subscriptions().
|
| 17 |
*
|
| 18 |
* @ingroup hooks
|
| 19 |
*/
|
| 20 |
function subscriptions_taxonomy_subscriptions($op, $arg0 = NULL, $arg1 = NULL, $arg2 = NULL) {
|
| 21 |
static $stypes = array('taxa' => array('node', 'tid'));
|
| 22 |
$function = '_subscriptions_taxonomy_'. $op;
|
| 23 |
if (function_exists($function)) {
|
| 24 |
return $function($arg0, $arg1, $arg2);
|
| 25 |
}
|
| 26 |
switch ($op) {
|
| 27 |
case 'queue':
|
| 28 |
// $arg0 is $event array.
|
| 29 |
if ($arg0['module'] == 'node') {
|
| 30 |
$node = $arg0['node'];
|
| 31 |
$params['node']['tid'] = array(
|
| 32 |
'join' => 'INNER JOIN {term_node} tn ON s.value = '. ($GLOBALS['db_type'] == 'pgsql' ? 'CAST(' : '')
|
| 33 |
.'tn.tid'. ($GLOBALS['db_type'] == 'pgsql' ? ' AS VARCHAR)' : ''),
|
| 34 |
'where' => 'tn.nid = %d',
|
| 35 |
'args' => array($node->nid),
|
| 36 |
);
|
| 37 |
if ($arg0['type'] == 'comment') {
|
| 38 |
$where = ' AND s.send_comments = 1';
|
| 39 |
}
|
| 40 |
elseif ($arg0['type'] == 'node' && $arg0['action'] == 'update') {
|
| 41 |
$where = ' AND s.send_updates = 1';
|
| 42 |
}
|
| 43 |
if (isset($where)) {
|
| 44 |
$params['node']['tid']['where'] .= $where;
|
| 45 |
}
|
| 46 |
return $params;
|
| 47 |
}
|
| 48 |
break;
|
| 49 |
case 'fields': // Parameter is module
|
| 50 |
if ($arg0 == 'node' || $arg0 == 'comment') {
|
| 51 |
$tr = 't';
|
| 52 |
return array(
|
| 53 |
'tid' => array(
|
| 54 |
'mailvars_function' => '_subscriptions_content_node_mailvars',
|
| 55 |
'mailkey' => 'subscriptions_taxonomy_node-tid',
|
| 56 |
'!subs_type' => $tr('category'),
|
| 57 |
),
|
| 58 |
);
|
| 59 |
}
|
| 60 |
break;
|
| 61 |
case 'stypes':
|
| 62 |
return $stypes;
|
| 63 |
case 'stype':
|
| 64 |
return (isset($stypes[$arg0]) ? array_merge( $stypes[$arg0], array($arg1, $arg2)) : NULL);
|
| 65 |
}
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* Implementation of hook_node_options(), subhook of hook_subscriptions().
|
| 70 |
*
|
| 71 |
* This is called by subscriptions_ui_node_form() in subscriptions_ui.module.
|
| 72 |
*
|
| 73 |
* @ingroup form
|
| 74 |
* @ingroup hooks
|
| 75 |
*
|
| 76 |
* @see subscriptions_ui_node_form()
|
| 77 |
*/
|
| 78 |
function _subscriptions_taxonomy_node_options($account, $node) {
|
| 79 |
if (!isset($node->taxonomy) || !is_array($node->taxonomy)) {
|
| 80 |
return;
|
| 81 |
}
|
| 82 |
$options = array();
|
| 83 |
$vids_to_omit = variable_get('subscriptions_omitted_taxa', array());
|
| 84 |
foreach ($node->taxonomy as $tid => $term) {
|
| 85 |
// Taxonomy term
|
| 86 |
if (in_array($term->vid, $vids_to_omit)) {
|
| 87 |
continue;
|
| 88 |
}
|
| 89 |
$options['tid'][] = array(
|
| 90 |
'name' => t('To content in %term', array('%term' => $term->name)),
|
| 91 |
'params' => array('module' => 'node', 'field' => 'tid', 'value' => $tid),
|
| 92 |
'link' => 'taxa/'. $tid,
|
| 93 |
);
|
| 94 |
$options['tid'][] = array(
|
| 95 |
'name' => t('To content in %term by %name', array('%term' => $term->name, '%name' => ($node->uid ? $node->name : variable_get('anonymous', '???')))),
|
| 96 |
'params' => array('module' => 'node', 'field' => 'tid', 'value' => $tid, 'author_uid' => $node->uid),
|
| 97 |
'link' => 'taxa/'. $tid .'/'. $node->uid,
|
| 98 |
);
|
| 99 |
$options['tid']['weight'] = -1;
|
| 100 |
}
|
| 101 |
return $options;
|
| 102 |
}
|
| 103 |
|
| 104 |
/**
|
| 105 |
* Implementation of hook_types(), subhook of hook_subscriptions().
|
| 106 |
*
|
| 107 |
* This is called by subscriptions_types() in subscriptions.module.
|
| 108 |
*
|
| 109 |
* @return
|
| 110 |
* Returns information about types for Subscriptions module interface.
|
| 111 |
*
|
| 112 |
* @ingroup form
|
| 113 |
* @ingroup hooks
|
| 114 |
*
|
| 115 |
* @see subscriptions_types()
|
| 116 |
*/
|
| 117 |
function _subscriptions_taxonomy_types() {
|
| 118 |
$tr = 't';
|
| 119 |
$types['taxa'] = array(
|
| 120 |
'title' => 'Categories',
|
| 121 |
'access' => 'subscribe to taxonomy terms',
|
| 122 |
'page' => 'subscriptions_taxonomy_page_taxa',
|
| 123 |
'fields' => array('node', 'tid'),
|
| 124 |
'weight' => -1,
|
| 125 |
);
|
| 126 |
return $types;
|
| 127 |
t('subscribe to taxonomy terms');
|
| 128 |
}
|
| 129 |
|
| 130 |
/**
|
| 131 |
* Implementation of hook_form_alter().
|
| 132 |
*
|
| 133 |
* Adds the Taxonomy Settings part to admin/settings/subscriptions.
|
| 134 |
*
|
| 135 |
* @ingroup hooks
|
| 136 |
* @ingroup form
|
| 137 |
*/
|
| 138 |
function subscriptions_taxonomy_form_subscriptions_settings_form_alter(&$form, &$form_state) {
|
| 139 |
global $user;
|
| 140 |
$tr = 't';
|
| 141 |
|
| 142 |
$form['taxonomy'] = array(
|
| 143 |
'#type' => 'fieldset',
|
| 144 |
'#title' => t('Taxonomy settings'),
|
| 145 |
'#collapsible' => TRUE,
|
| 146 |
'#weight' => -8,
|
| 147 |
);
|
| 148 |
|
| 149 |
$vocabularies = taxonomy_get_vocabularies();
|
| 150 |
$select[0] = '<'. t('none') .'>';
|
| 151 |
foreach ($vocabularies as $vocabulary) {
|
| 152 |
$select[$vocabulary->vid] = $vocabulary->name;
|
| 153 |
}
|
| 154 |
|
| 155 |
$form['taxonomy']['subscriptions_restricted_taxa'] = array(
|
| 156 |
'#type' => 'select',
|
| 157 |
'#title' => t('Restricted vocabularies'),
|
| 158 |
'#default_value' => variable_get('subscriptions_restricted_taxa', array()),
|
| 159 |
'#options' => $select,
|
| 160 |
'#description' => t('Select vocabularies for which only the subscribed terms should be listed on the %Subscriptions | %Categories page.<br />This helps to reduce the size of the listing, especially for free-tagging vocabularies with large numbers of terms.', array('%Subscriptions' => t('Subscriptions'), '%Categories' => $tr('Categories'))),
|
| 161 |
'#multiple' => TRUE,
|
| 162 |
);
|
| 163 |
$form['taxonomy']['subscriptions_omitted_taxa'] = array(
|
| 164 |
'#type' => 'select',
|
| 165 |
'#title' => t('Omitted vocabularies'),
|
| 166 |
'#default_value' => variable_get('subscriptions_omitted_taxa', array()),
|
| 167 |
'#options' => $select,
|
| 168 |
'#description' => t('Select vocabularies which should be <strong>omitted</strong> from subscription listings; this means the terms of those vocabularies will be unlisted, i.e. they will be removed from subscription listings.<br />The content may still be available for subscribing via different kinds of subscriptions, but subscribing by category will be unavailable for the terms in the selected vocabularies.'),
|
| 169 |
'#multiple' => TRUE,
|
| 170 |
);
|
| 171 |
|
| 172 |
// @ TODO write the code that supports this setting
|
| 173 |
/*
|
| 174 |
$form['taxonomy']['subscriptions_allow_vid'] = array(
|
| 175 |
'#type' => 'checkbox',
|
| 176 |
'#title' => t('Allow vocabularies subscription'),
|
| 177 |
'#default_value' => variable_get('subscriptions_allow_vid', 1),
|
| 178 |
'#description' => t('Allow users to subscribe to an entire vocabluary of terms.'),
|
| 179 |
);
|
| 180 |
*/
|
| 181 |
|
| 182 |
}
|
| 183 |
|
| 184 |
/**
|
| 185 |
* Returns a list of taxonomy subscriptions.
|
| 186 |
*
|
| 187 |
* @ingroup form
|
| 188 |
*/
|
| 189 |
function subscriptions_taxonomy_page_taxa($account, $form) {
|
| 190 |
// traverse the taxonomy tree
|
| 191 |
$vocabularies = function_exists('taxonomy_help') ? taxonomy_get_vocabularies() : array();
|
| 192 |
|
| 193 |
// omit undesired vocabularies from listing
|
| 194 |
$omits = variable_get('subscriptions_omitted_taxa', array());
|
| 195 |
foreach ($omits as $omit) {
|
| 196 |
unset($vocabularies[$omit]);
|
| 197 |
}
|
| 198 |
if ($vocabularies) {
|
| 199 |
return drupal_get_form('subscriptions_taxonomy_taxa_form', $vocabularies, $account, $form);
|
| 200 |
}
|
| 201 |
else {
|
| 202 |
return t('There are no available !subs_types.', array('!subs_types' => t('category groups')));
|
| 203 |
}
|
| 204 |
}
|
| 205 |
|
| 206 |
/**
|
| 207 |
* Build the Categories subscription form at user/UID/subscriptions/taxa.
|
| 208 |
*
|
| 209 |
* @ingroup form
|
| 210 |
*/
|
| 211 |
function subscriptions_taxonomy_taxa_form(&$form_state, $vocabularies, $account, $form) {
|
| 212 |
$uid = (isset($account) ? $account->uid : (is_numeric(arg(5)) ? -arg(5) : -DRUPAL_AUTHENTICATED_RID));
|
| 213 |
$sql = db_rewrite_sql("
|
| 214 |
SELECT s.value, s.send_interval, s.author_uid, s.send_comments, s.send_updates, t.tid, t.vid, t.name
|
| 215 |
FROM {term_data} t
|
| 216 |
INNER JOIN {subscriptions} s ON ". ($GLOBALS['db_type'] == 'pgsql' ? 'CAST(' : '')
|
| 217 |
."t.tid". ($GLOBALS['db_type'] == 'pgsql' ? ' AS VARCHAR)' : '')
|
| 218 |
." = s.value
|
| 219 |
WHERE s.module = 'node' AND s.field = 'tid' AND s.recipient_uid = %d
|
| 220 |
ORDER BY s.author_uid", 't', 'tid');
|
| 221 |
$result = db_query($sql, $uid);
|
| 222 |
while ($s = db_fetch_array($result)) {
|
| 223 |
$subscriptions[$s['vid']][$s['value']][$s['author_uid']] = $s;
|
| 224 |
$subscriptions_terms_by_vid[$s['vid']][$s['value']] = $s;
|
| 225 |
}
|
| 226 |
foreach ($vocabularies as $vocab) {
|
| 227 |
// display vocabulary name and group terms together
|
| 228 |
$form[$vocab->vid] = array(
|
| 229 |
'#type' => 'fieldset',
|
| 230 |
'#title' => $vocab->name,
|
| 231 |
'#collapsible' => TRUE,
|
| 232 |
'#collapsed' => FALSE,
|
| 233 |
);
|
| 234 |
$form[$vocab->vid][0] = array(
|
| 235 |
'#tree' => TRUE,
|
| 236 |
'#theme' => 'subscriptions_form_table',
|
| 237 |
);
|
| 238 |
$subscriptions_vid = (isset($subscriptions[$vocab->vid]) ? $subscriptions[$vocab->vid] : NULL);
|
| 239 |
$vids_to_restrict = variable_get('subscriptions_restricted_taxa', array());
|
| 240 |
$tree = NULL;
|
| 241 |
if (!in_array($vocab->vid, $vids_to_restrict)) {
|
| 242 |
// @ TODO create mechanism to allow users to
|
| 243 |
// subscribe to all terms under this vocabulary
|
| 244 |
$tree = taxonomy_get_tree($vocab->vid);
|
| 245 |
}
|
| 246 |
else {
|
| 247 |
if (isset($subscriptions_terms_by_vid[$vocab->vid])) {
|
| 248 |
$tree = $subscriptions_terms_by_vid[$vocab->vid];
|
| 249 |
$form[$vocab->vid][1] = array(
|
| 250 |
'#type' => 'item',
|
| 251 |
'#description' => '<div>'. t('This is a restricted category group; deactivated subscriptions will be removed from the list. To subscribe to an unlisted category, go to a post in that category and subscribe from there.') .'</div>',
|
| 252 |
);
|
| 253 |
}
|
| 254 |
}
|
| 255 |
$forum_containers = NULL;
|
| 256 |
if (module_exists('forum') && $vocab->vid == variable_get('forum_nav_vocabulary', '')) {
|
| 257 |
$forum_containers = variable_get('forum_containers', array());
|
| 258 |
}
|
| 259 |
$defaults = array();
|
| 260 |
if (isset($tree)) {
|
| 261 |
foreach ($tree as $term) {
|
| 262 |
if (is_array($term)) {
|
| 263 |
$term = (object)$term;
|
| 264 |
}
|
| 265 |
$title = str_repeat('-- ', (empty($term->depth) ? 0 : $term->depth)) . l($term->name, 'taxonomy/term/'. $term->tid);
|
| 266 |
if (!isset($subscriptions_vid[$term->tid][-1])) {
|
| 267 |
// author-less item is missing -- add it here:
|
| 268 |
$subscriptions_vid[$term->tid][-1] = array(
|
| 269 |
'send_interval' => _subscriptions_get_setting('send_interval', ($uid < 0 ? $uid : $account)),
|
| 270 |
'send_comments' => _subscriptions_get_setting('send_comments', ($uid < 0 ? $uid : $account)),
|
| 271 |
'send_updates' => _subscriptions_get_setting('send_updates', ($uid < 0 ? $uid : $account)),
|
| 272 |
'author_uid' => FALSE,
|
| 273 |
);
|
| 274 |
}
|
| 275 |
// add the active subscriptions
|
| 276 |
foreach ($subscriptions_vid[$term->tid] as $author_uid => $subscription) {
|
| 277 |
// TODO: support multi-parent hierarchies (currently, the child is not repeated)
|
| 278 |
subscriptions_form_helper($form[$vocab->vid][0], $defaults, $author_uid, $term->tid, $title, $subscription);
|
| 279 |
if (isset($forum_containers) && in_array($term->tid, $forum_containers)) {
|
| 280 |
$tids = array_keys($form[$vocab->vid][0]['checkboxes']);
|
| 281 |
$tid = end($tids);
|
| 282 |
$form[$vocab->vid][0]['checkboxes'][$tid][-1]['#disabled'] = TRUE;
|
| 283 |
}
|
| 284 |
}
|
| 285 |
}
|
| 286 |
}
|
| 287 |
else {
|
| 288 |
$form[$vocab->vid]['info']['#value'] = '<p>'. t('This is a restricted category group; only subscribed categories show up in this list.<br />To subscribe to a category in this group, go to a post in that category and subscribe from there.') .'</p>';
|
| 289 |
}
|
| 290 |
$form[$vocab->vid][0]['defaults'] = array(
|
| 291 |
'#type' => 'value',
|
| 292 |
'#value' => $defaults,
|
| 293 |
);
|
| 294 |
subscriptions_form_column_filter($form[$vocab->vid][0], $uid);
|
| 295 |
}
|
| 296 |
|
| 297 |
if (empty($form)) {
|
| 298 |
$form = array('#value' => t('There are no active categories.'));
|
| 299 |
}
|
| 300 |
else {
|
| 301 |
$form['#tree'] = TRUE;
|
| 302 |
$form['uid'] = array('#type' => 'value', '#value' => $uid);
|
| 303 |
$form['access_key'] = array('#type' => 'value', '#value' => 'taxa');
|
| 304 |
$form['module'] = array('#type' => 'value', '#value' => 'node');
|
| 305 |
$form['field'] = array('#type' => 'value', '#value' => 'tid');
|
| 306 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'), '#weight' => 10);
|
| 307 |
$form['#submit'][] = 'subscriptions_page_form_submit';
|
| 308 |
}
|
| 309 |
return $form;
|
| 310 |
}
|
| 311 |
|
| 312 |
/**
|
| 313 |
* Implementation of hook_mailkeys().
|
| 314 |
*
|
| 315 |
* Provide mailkeys for mail_edit.
|
| 316 |
*
|
| 317 |
* @ingroup hooks
|
| 318 |
*/
|
| 319 |
function subscriptions_taxonomy_mailkeys() {
|
| 320 |
return array('node-tid' => t('Notifications for %Categories subscriptions', array('%Categories' => t('Categories'))));
|
| 321 |
}
|
| 322 |
|
| 323 |
/**
|
| 324 |
* Implementation of hook_mail_edit_tokens_list().
|
| 325 |
*
|
| 326 |
* Provide replacable tokens for mail_edit.
|
| 327 |
*
|
| 328 |
* @ingroup hooks
|
| 329 |
*/
|
| 330 |
function subscriptions_taxonomy_mail_edit_tokens_list($mailkey, $options = array()) {
|
| 331 |
$tokens = array();
|
| 332 |
switch ($mailkey) {
|
| 333 |
case 'node-tid':
|
| 334 |
$tokens['!term_name']= t('The name of the term/category/tag/forum/etc.');
|
| 335 |
break;
|
| 336 |
}
|
| 337 |
if (isset($options['tokens'])) {
|
| 338 |
$tokens += $options['tokens'];
|
| 339 |
}
|
| 340 |
$tokens = subscriptions_content_mail_edit_tokens_list($mailkey, array('tokens' => $tokens));
|
| 341 |
return $tokens;
|
| 342 |
}
|
| 343 |
|
| 344 |
/**
|
| 345 |
* Implementation of hook_mail_edit_text().
|
| 346 |
*
|
| 347 |
* Provide default template strings for mail_edit.
|
| 348 |
*
|
| 349 |
* @ingroup hooks
|
| 350 |
*/
|
| 351 |
function subscriptions_taxonomy_mail_edit_text($mailkey, $language) {
|
| 352 |
$return = subscriptions_content_mail_edit_text($mailkey, $language);
|
| 353 |
return $return;
|
| 354 |
}
|
| 355 |
|
| 356 |
/**
|
| 357 |
* Implementation of hook_taxonomy().
|
| 358 |
*
|
| 359 |
* Remove taxonomy subscriptions when the underlying terms or vocabularies are removed.
|
| 360 |
*
|
| 361 |
* @ingroup hooks
|
| 362 |
*/
|
| 363 |
function subscriptions_taxonomy_taxonomy($op, $type, $array) {
|
| 364 |
if ($op != 'delete') {
|
| 365 |
return;
|
| 366 |
}
|
| 367 |
switch ($type) {
|
| 368 |
case 'term':
|
| 369 |
$tid = $array['tid'];
|
| 370 |
db_query("DELETE FROM {subscriptions_queue} WHERE module = 'node' AND field = 'tid' AND value = '%s'", $tid);
|
| 371 |
db_query("DELETE FROM {subscriptions} WHERE module = 'node' AND field = 'tid' AND value = '%s'", $tid);
|
| 372 |
break;
|
| 373 |
case 'vocabulary':
|
| 374 |
$vid = $array['vid'];
|
| 375 |
foreach (array('omitted', 'restricted') as $key) {
|
| 376 |
$array = variable_get('subscriptions_'. $key .'_taxa', array());
|
| 377 |
unset($array[$vid]);
|
| 378 |
variable_set('subscriptions_'. $key .'_taxa', $array);
|
| 379 |
}
|
| 380 |
break;
|
| 381 |
}
|
| 382 |
}
|
| 383 |
|
| 384 |
/**
|
| 385 |
* Implementation of hook_disable().
|
| 386 |
*
|
| 387 |
* Remove our queue items.
|
| 388 |
*
|
| 389 |
* @ingroup hooks
|
| 390 |
*/
|
| 391 |
function subscriptions_taxonomy_disable() {
|
| 392 |
db_query("DELETE FROM {subscriptions_queue} WHERE module = 'node' AND field = 'tid'");
|
| 393 |
}
|