| 1 |
<?php
|
| 2 |
// $Id: bio_role_terms.module,v 1.2 2008/06/17 14:27:58 acm Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_help
|
| 6 |
*/
|
| 7 |
function bio_role_terms_help($section) {
|
| 8 |
switch ($section) {
|
| 9 |
case 'admin/help#bio_role_terms':
|
| 10 |
$output = t("<p>This module is an add-on for the <a href='http://drupal.org/project/bio'>Bio module</a>,
|
| 11 |
it automatically adds taxonomy terms to the Bio node based on the Bio nodes owners role.</p>
|
| 12 |
<p>With the Bio node assigned taxonomy terms with the same names as the rolse of its owner,
|
| 13 |
it opens up a number of opportunities :
|
| 14 |
<ul>
|
| 15 |
<li>Listing profiles by role</li>
|
| 16 |
<li>Restricting access to profiles by both who is viewing the profile and who owns the profile
|
| 17 |
(with the help of <a href='http://drupal.org/project/taxonomy_access'>Taxonomy Access Control</a>)</li>
|
| 18 |
<li>... and probably other things, answers on a postcard please :)</li>
|
| 19 |
</ul>
|
| 20 |
</p>
|
| 21 |
<h2>Settings</h2>
|
| 22 |
<p>A single setting is used, the vocabulary to draw the terms from. This vocabulary is then hidden
|
| 23 |
from all users on input forms.</p>");
|
| 24 |
return $output;
|
| 25 |
}
|
| 26 |
}
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Implementation of hook_perm().
|
| 30 |
*/
|
| 31 |
function bio_role_terms_perm() {
|
| 32 |
return array('administer bio role terms');
|
| 33 |
}
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Implementation of hook_menu().
|
| 37 |
*/
|
| 38 |
function bio_role_terms_menu() {
|
| 39 |
$items = array();
|
| 40 |
$admin_access = user_access('administer bio role terms');
|
| 41 |
|
| 42 |
$items[] = array(
|
| 43 |
'path' => 'admin/user/bio_role_terms',
|
| 44 |
'title' => t('User Bio role terms settings'),
|
| 45 |
'description' => t('Allows selection of the vocabulary used in the matching process'),
|
| 46 |
'callback' => 'drupal_get_form',
|
| 47 |
'callback arguments' => 'bio_role_terms_admin',
|
| 48 |
'access' => $admin_access,
|
| 49 |
'type' => MENU_NORMAL_ITEM,
|
| 50 |
);
|
| 51 |
|
| 52 |
$items[] = array(
|
| 53 |
'path' => 'admin/user/bio_role_terms/update_all',
|
| 54 |
'callback' => 'bio_role_terms_admin_update_all',
|
| 55 |
'access' => $admin_access,
|
| 56 |
'type' => MENU_CALLBACK,
|
| 57 |
);
|
| 58 |
|
| 59 |
return $items;
|
| 60 |
}
|
| 61 |
/**
|
| 62 |
* Menu call back for administration settings for bio_role_term
|
| 63 |
*
|
| 64 |
* @return Array containing a FormAPI form
|
| 65 |
*/
|
| 66 |
function bio_role_terms_admin() {
|
| 67 |
$active_vocabularies = db_query('SELECT DISTINCT {vocabulary}.vid, {vocabulary}.name FROM {vocabulary_node_types}, {vocabulary} WHERE {vocabulary_node_types}.vid = {vocabulary}.vid AND {vocabulary_node_types}.type LIKE "%%%s%%"', bio_get_type());
|
| 68 |
$options = '';
|
| 69 |
|
| 70 |
while ( $vocabulary = db_fetch_object($active_vocabularies) ) {
|
| 71 |
$options[$vocabulary->vid] = $vocabulary->name;
|
| 72 |
}
|
| 73 |
|
| 74 |
if ( $options != '' ) {
|
| 75 |
$form['bio_role_terms_vocab'] = array(
|
| 76 |
'#type' => 'select',
|
| 77 |
'#title' => t('Select vocabulary'),
|
| 78 |
'#options' => $options,
|
| 79 |
'#default_value' => variable_get('bio_role_terms_vocab', 1),
|
| 80 |
'#description' => t("Terms in the vocabulary will be matched against the @bio node's owner's roles, matches will then be attached to the @bio node.", array( '@bio' => bio_get_type() ) ),
|
| 81 |
);
|
| 82 |
|
| 83 |
$form['bio_role_terms_update_all'] = array(
|
| 84 |
'#type' => 'markup',
|
| 85 |
'#title' => t('Update all bio nodes'),
|
| 86 |
'#value' => '<p>'. t('!link all bio nodes with the correct taxonomy terms, this is useful when adding the module for the first time or changing taxonomy vocabularies (but make sure you save the change first).', array( '!link' => l(t('Click here to bulk update'), 'admin/user/bio_role_terms/update_all'), '@bio' => bio_get_type() ) ) .'</p>',
|
| 87 |
);
|
| 88 |
}
|
| 89 |
else {
|
| 90 |
$form['bio_role_terms_no_vocabs'] = array(
|
| 91 |
'#type' => 'markup',
|
| 92 |
'#title' => t('No active vocabularies for @bio node', array( '!bio' => bio_get_type() ) ),
|
| 93 |
'#value' => '<p>'. t('Please !create_vocab containing the names of the roles you wish to map and assign it to the @bio node.', array( '!create_vocab' => l(t('create a vocabulary'), 'admin/content/taxonomy/add/vocabulary'), '@bio' => bio_get_type() ) ) .'</p>'
|
| 94 |
);
|
| 95 |
}
|
| 96 |
|
| 97 |
return system_settings_form($form);
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Implementation of hook_form_alter().
|
| 102 |
*/
|
| 103 |
function bio_role_terms_form_alter($form_id, &$form) {
|
| 104 |
$bio_role_terms_vocab = variable_get('bio_role_terms_vocab', -1);
|
| 105 |
|
| 106 |
if ( $bio_role_terms_vocab != -1 ) {
|
| 107 |
//hide the role taxonomy on the bio node edit form
|
| 108 |
if ( $form_id == bio_get_type() .'_node_form' ) {
|
| 109 |
$hidden = array('#type' => 'hidden');
|
| 110 |
$form['taxonomy'][$bio_role_terms_vocab] = array_merge($form['taxonomy'], $hidden);
|
| 111 |
}
|
| 112 |
|
| 113 |
//highjack the bio node settings page, so it links to this module
|
| 114 |
if ( $form_id == 'bio_settings' ) {
|
| 115 |
$vocabularies = taxonomy_get_vocabularies();
|
| 116 |
$options = '';
|
| 117 |
|
| 118 |
foreach ( $vocabularies as $vocabulary ) {
|
| 119 |
$options[$vocabulary->vid] = $vocabulary->name;
|
| 120 |
}
|
| 121 |
|
| 122 |
$form['bio_role_terms_vocab'] = array(
|
| 123 |
'#type' => 'markup',
|
| 124 |
'#value' => '<p><b><i>'. t('NOTE : The !link plugin is installed', array( '!link' => l('Bio role terms', 'admin/user/bio_role_terms') ) ) .'</i></b></p>'
|
| 125 |
);
|
| 126 |
}
|
| 127 |
}
|
| 128 |
else if ( user_access('administer bio role terms') ) {
|
| 129 |
_bio_role_terms_not_configured();
|
| 130 |
}
|
| 131 |
}
|
| 132 |
|
| 133 |
|
| 134 |
/**
|
| 135 |
* Implementation of hook_user().
|
| 136 |
*/
|
| 137 |
function bio_role_terms_user($op, &$edit, &$account, $category = NULL) {
|
| 138 |
switch ( $op ) {
|
| 139 |
case 'after_update':
|
| 140 |
$bio_role_terms_vocab = variable_get('bio_role_terms_vocab', -1);
|
| 141 |
|
| 142 |
if ( $bio_role_terms_vocab != -1 ) {
|
| 143 |
_bio_role_terms_update_bio_node( $account, $bio_role_terms_vocab);
|
| 144 |
}
|
| 145 |
else if ( user_access('administer bio role terms') ) {
|
| 146 |
_bio_role_terms_not_configured();
|
| 147 |
}
|
| 148 |
break;
|
| 149 |
}
|
| 150 |
}
|
| 151 |
|
| 152 |
|
| 153 |
/**
|
| 154 |
* Implementation of hook_nodeapi().
|
| 155 |
*/
|
| 156 |
function bio_role_terms_nodeapi(&$node, $op, $a3, $a4) {
|
| 157 |
$bio_role_terms_vocab = variable_get('bio_role_terms_vocab', -1);
|
| 158 |
|
| 159 |
if ( $bio_role_terms_vocab != -1 ) {
|
| 160 |
//set the taxonomy of the bio node
|
| 161 |
if ( $node->type == bio_get_type() AND $op == 'submit' AND arg(0) == 'user' ) {
|
| 162 |
$user = user_load( array( 'uid' => arg(1) ) );
|
| 163 |
$node->taxonomy[$bio_role_terms_vocab] = _bio_role_terms_get_matched_terms($user, $bio_role_terms_vocab);
|
| 164 |
}
|
| 165 |
}
|
| 166 |
else if ( user_access('administer bio role terms') ) {
|
| 167 |
_bio_role_terms_not_configured();
|
| 168 |
}
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Menu call back, updates all Bio nodes with their owners roles as taxonomy
|
| 173 |
*
|
| 174 |
* @return Array containing a FormAPI form
|
| 175 |
*/
|
| 176 |
function bio_role_terms_admin_update_all( ) {
|
| 177 |
//TODO: Update to allow this to be done in batches / find a nicer way to do this
|
| 178 |
$bio_role_terms_vocab = variable_get('bio_role_terms_vocab', -1);
|
| 179 |
|
| 180 |
if ( $bio_role_terms_vocab != -1 ) {
|
| 181 |
$active_bios = db_query('SELECT * FROM {bio}');
|
| 182 |
$count = 0;
|
| 183 |
|
| 184 |
while ( $bio = db_fetch_object($active_bios) ) {
|
| 185 |
$user = user_load( array( 'uid' => $bio->uid ) );
|
| 186 |
|
| 187 |
_bio_role_terms_update_bio_node( $user, $bio_role_terms_vocab);
|
| 188 |
|
| 189 |
$count++;
|
| 190 |
}
|
| 191 |
|
| 192 |
drupal_set_message( t('@count @bio_nodes updated', array('@count' => $count, '@bio_nodes' => format_plural($count, bio_get_type() .' node', bio_get_type() .' nodes'))), 'status');
|
| 193 |
}
|
| 194 |
else if ( user_access('administer bio role terms') ) {
|
| 195 |
_bio_role_terms_not_configured();
|
| 196 |
}
|
| 197 |
|
| 198 |
drupal_goto('admin/user/bio_role_terms');
|
| 199 |
}
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
/**
|
| 205 |
* Helper function to match roles against the selected taxonomy
|
| 206 |
*
|
| 207 |
* @param
|
| 208 |
* User object
|
| 209 |
* @param
|
| 210 |
* Vocabuarly id
|
| 211 |
* @param
|
| 212 |
* bool, true = return object, false = return array
|
| 213 |
* @return
|
| 214 |
* Array of tids which match the user roles
|
| 215 |
*
|
| 216 |
*/
|
| 217 |
function _bio_role_terms_get_matched_terms($user, $bio_role_terms_vocab, $as_objects = false) {
|
| 218 |
$role_taxonomy = array();
|
| 219 |
$terms_tree = taxonomy_get_tree($bio_role_terms_vocab);
|
| 220 |
|
| 221 |
//cycle though the taxonomy options
|
| 222 |
foreach ( $terms_tree as $taxonomy_term ) {
|
| 223 |
//try to match terms to roles the user is a member of
|
| 224 |
foreach ( $user->roles as $role_name ) {
|
| 225 |
if ( $role_name == $taxonomy_term->name ) {
|
| 226 |
if ( !$as_objects ) {
|
| 227 |
$role_taxonomy[] = $taxonomy_term->tid;
|
| 228 |
}
|
| 229 |
else {
|
| 230 |
$role_taxonomy[$taxonomy_term->tid] = $taxonomy_term;
|
| 231 |
}
|
| 232 |
}
|
| 233 |
}
|
| 234 |
}
|
| 235 |
return $role_taxonomy;
|
| 236 |
}
|
| 237 |
|
| 238 |
/**
|
| 239 |
* Helper function to update the bio node object
|
| 240 |
*
|
| 241 |
* @param
|
| 242 |
* User object who owns the node
|
| 243 |
* @param
|
| 244 |
* Vocabulary id
|
| 245 |
*/
|
| 246 |
function _bio_role_terms_update_bio_node(&$user, $bio_role_terms_vocab) {
|
| 247 |
//TODO: refactor so it to avoids unnecessary saves, e.g. if nothing changed
|
| 248 |
|
| 249 |
$bio_node = node_load( array( 'uid' => $user->uid, 'type' => bio_get_type()) );
|
| 250 |
|
| 251 |
if ( $bio_node != null ) {
|
| 252 |
$new_taxonomy = _bio_role_terms_get_matched_terms($user, $bio_role_terms_vocab, true);
|
| 253 |
|
| 254 |
//strip current terms for this vocabulary
|
| 255 |
foreach ($bio_node->taxonomy as $key => $term) {
|
| 256 |
if ($term->vid == $bio_role_terms_vocab) {
|
| 257 |
unset($bio_node->taxonomy[$key]);
|
| 258 |
}
|
| 259 |
}
|
| 260 |
|
| 261 |
$bio_node->taxonomy = array_merge($bio_node->taxonomy, $new_taxonomy);
|
| 262 |
|
| 263 |
node_save($bio_node);
|
| 264 |
}
|
| 265 |
}
|
| 266 |
|
| 267 |
/**
|
| 268 |
* Helper function to set error message.
|
| 269 |
*/
|
| 270 |
function _bio_role_terms_not_configured() {
|
| 271 |
drupal_set_message(t('Bio role terms is installed but not yet configured, please !link', array('!link' => l('configure it', 'admin/user/bio_role_terms'))), 'error');
|
| 272 |
}
|