| 1 |
<?php
|
| 2 |
// $Id: nat.module,v 1.34 2008/05/17 09:17:32 karthik Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* NAT - node auto term - is a helper module that automatically creates a
|
| 7 |
* term using the same title as a node.
|
| 8 |
*
|
| 9 |
* @author Karthik Kumar / Zen [ http://drupal.org/user/21209 ].
|
| 10 |
* @internal There are a number of cases to be considered (dev notes):
|
| 11 |
* o Term adds/updates/deletes: i.e. should this be a 2-way module?
|
| 12 |
* o Vocabulary deletes.
|
| 13 |
* o Dissociation of node type and vocabulary in nat_config - how should this
|
| 14 |
* be handled?
|
| 15 |
* o Filter handling for body/description fields.
|
| 16 |
* o Duplicate handling?
|
| 17 |
*
|
| 18 |
* Features to be added:
|
| 19 |
* o Node deletes: Optionally delete child nodes associated via NAT.
|
| 20 |
* o Maintain hierarchy on unassociated vocabularies (on a best effort basis?)
|
| 21 |
*/
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Implementation of hook_help().
|
| 25 |
*/
|
| 26 |
function nat_help($path, $arg) {
|
| 27 |
switch ($path) {
|
| 28 |
case 'admin/help#nat':
|
| 29 |
return t('NAT - node auto term - is a helper module that automatically creates a term using the same title as a node.');
|
| 30 |
}
|
| 31 |
}
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Implementation of hook_menu().
|
| 35 |
*/
|
| 36 |
function nat_menu() {
|
| 37 |
$items = array();
|
| 38 |
|
| 39 |
$items['admin/settings/nat'] = array(
|
| 40 |
'title' => t('NAT'),
|
| 41 |
'description' => t('Establish node - node relationships via the taxonomy module.'),
|
| 42 |
'page callback' => 'drupal_get_form',
|
| 43 |
'page arguments' => array('nat_settings_form'),
|
| 44 |
'access arguments' => array('administer NAT configuration')
|
| 45 |
);
|
| 46 |
$items['admin/settings/nat/settings'] = array(
|
| 47 |
'title' => t('Settings'),
|
| 48 |
'page callback' => 'drupal_get_form',
|
| 49 |
'page arguments' => array('nat_settings_form'),
|
| 50 |
'access arguments' => array('administer NAT configuration'),
|
| 51 |
'type' => MENU_DEFAULT_LOCAL_TASK
|
| 52 |
);
|
| 53 |
$items['admin/settings/nat/sync'] = array(
|
| 54 |
'title' => t('Sync'),
|
| 55 |
'page callback' => 'drupal_get_form',
|
| 56 |
'page arguments' => array('nat_sync_form'),
|
| 57 |
'access arguments' => array('administer NAT configuration'),
|
| 58 |
'type' => MENU_LOCAL_TASK
|
| 59 |
);
|
| 60 |
|
| 61 |
return $items;
|
| 62 |
}
|
| 63 |
|
| 64 |
/**
|
| 65 |
* Implementation of hook_views_api().
|
| 66 |
*
|
| 67 |
* This one is used as the base to reduce errors when updating.
|
| 68 |
*/
|
| 69 |
function nat_views_api() {
|
| 70 |
return array(
|
| 71 |
'api' => 2,
|
| 72 |
'path' => drupal_get_path('module', 'nat') .'/includes',
|
| 73 |
);
|
| 74 |
}
|
| 75 |
|
| 76 |
/**
|
| 77 |
* Implementation of hook_perm().
|
| 78 |
*/
|
| 79 |
function nat_perm() {
|
| 80 |
return array('administer NAT configuration');
|
| 81 |
}
|
| 82 |
|
| 83 |
/**
|
| 84 |
* Implementation of hook_nodeapi().
|
| 85 |
*/
|
| 86 |
function nat_nodeapi(&$node, $op, $teaser, $page) {
|
| 87 |
$nat_config = _nat_variable_get();
|
| 88 |
if (!isset($nat_config['types'][$node->type]) || empty($nat_config['types'][$node->type])) {
|
| 89 |
return;
|
| 90 |
}
|
| 91 |
|
| 92 |
switch ($op) {
|
| 93 |
case 'load':
|
| 94 |
$node->nat = nat_get_terms($node->nid);
|
| 95 |
break;
|
| 96 |
case 'insert':
|
| 97 |
$body = $node->body;
|
| 98 |
// Copying over the node body is optional.
|
| 99 |
$body = isset($nat_config['body'][$node->type]) ? $body : '';
|
| 100 |
|
| 101 |
// Add node title as terms.
|
| 102 |
$terms = _nat_add_terms($nat_config['types'][$node->type], $node->title, $body, $node->taxonomy, $node->nat['related'], $node->nat['synonyms']);
|
| 103 |
|
| 104 |
// Save node-term association in the NAT table.
|
| 105 |
_nat_save_association($node->nid, $terms);
|
| 106 |
break;
|
| 107 |
case 'update':
|
| 108 |
$body = $node->body;
|
| 109 |
// Copying over the node body is optional.
|
| 110 |
$body = isset($nat_config['body'][$node->type]) ? $body : '';
|
| 111 |
|
| 112 |
// Update node title in term(s).
|
| 113 |
$terms = nat_get_terms($node->nid);
|
| 114 |
_nat_update_terms($terms, $node->title, $body, $node->taxonomy, $node->nat['related'], $node->nat['synonyms']);
|
| 115 |
break;
|
| 116 |
case 'delete':
|
| 117 |
// Deleting the associated term when a node is deleted is optional.
|
| 118 |
if (isset($nat_config['delete'][$node->type])) {
|
| 119 |
_nat_delete_terms($node->nid);
|
| 120 |
}
|
| 121 |
// Delete node-term association from the NAT table.
|
| 122 |
_nat_delete_association($node->nid);
|
| 123 |
break;
|
| 124 |
}
|
| 125 |
}
|
| 126 |
|
| 127 |
/**
|
| 128 |
* Implementation of hook_form_alter().
|
| 129 |
*/
|
| 130 |
function nat_form_alter(&$form, &$form_state, $form_id) {
|
| 131 |
if ($form['#id'] == 'node-form') {
|
| 132 |
$config = _nat_variable_get();
|
| 133 |
|
| 134 |
foreach ($config['types'] as $type => $associations) {
|
| 135 |
if (count($associations) && $form_id == $type .'_node_form' && isset($config['related'][$type])) {
|
| 136 |
$form['nat'] = array(
|
| 137 |
'#type' => 'fieldset',
|
| 138 |
'#title' => t('Term information'),
|
| 139 |
'#tree' => TRUE,
|
| 140 |
'#weight' => -2,
|
| 141 |
'#collapsible' => TRUE,
|
| 142 |
'#collapsed' => FALSE
|
| 143 |
);
|
| 144 |
|
| 145 |
// Related terms.
|
| 146 |
if (isset($form['#node']->nat)) {
|
| 147 |
foreach ($form['#node']->nat as $term) {
|
| 148 |
$terms[$term->vid] = $term->tid;
|
| 149 |
}
|
| 150 |
}
|
| 151 |
foreach ($associations as $vocabulary_id) {
|
| 152 |
$vocabulary = taxonomy_vocabulary_load($vocabulary_id);
|
| 153 |
if ($vocabulary->relations) {
|
| 154 |
$form['nat']['related'][$vocabulary_id] = _taxonomy_term_select(t('Related !terms', array('!terms' => $vocabulary->name)), 'relations', array_keys(taxonomy_get_related($terms[$vocabulary_id])), $vocabulary_id, NULL, 1, '<'. t('none') .'>', array($term->tid));
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
| 158 |
// Synonyms.
|
| 159 |
$form['nat']['synonyms'] = array(
|
| 160 |
'#type' => 'textarea',
|
| 161 |
'#title' => t('Synonyms'),
|
| 162 |
'#default_value' => implode("\n", taxonomy_get_synonyms($term->tid)),
|
| 163 |
'#description' => t('<a href="@help-url">Synonyms</a> of this term, one synonym per line.', array('@help-url' => url('admin/help/taxonomy', array('fragment' => 'synonyms'))))
|
| 164 |
);
|
| 165 |
// This can only match once, so we just break the foreach.
|
| 166 |
break;
|
| 167 |
}
|
| 168 |
}
|
| 169 |
}
|
| 170 |
}
|
| 171 |
|
| 172 |
/**
|
| 173 |
* Implementation of hook_link_alter().
|
| 174 |
*/
|
| 175 |
function nat_link_alter(&$links, $node) {
|
| 176 |
$nat_config = _nat_variable_get();
|
| 177 |
|
| 178 |
if (isset($nat_config['node_links'][$node->type])) {
|
| 179 |
foreach ($links as $module => $link) {
|
| 180 |
// $link['title'] will be empty during node previews at which point
|
| 181 |
// taxonomy links do not work.
|
| 182 |
if (strpos($module, 'taxonomy_term') !== FALSE && $link['title']) {
|
| 183 |
$tids = array(str_replace('taxonomy/term/', '', $link['href']));
|
| 184 |
$nids = array_keys(nat_get_nids($tids, FALSE));
|
| 185 |
if (!empty($nids)) {
|
| 186 |
// Link back to the NAT node and not the taxonomy term page.
|
| 187 |
$links[$module]['href'] = "node/$nids[0]";
|
| 188 |
}
|
| 189 |
}
|
| 190 |
}
|
| 191 |
}
|
| 192 |
}
|
| 193 |
|
| 194 |
/**
|
| 195 |
* Menu callback: NAT module settings form.
|
| 196 |
*/
|
| 197 |
function nat_settings_form() {
|
| 198 |
$types = node_get_types();
|
| 199 |
$vocabularies = _nat_get_vocabularies();
|
| 200 |
|
| 201 |
if (empty($vocabularies)) {
|
| 202 |
drupal_set_message(t('The NAT module requires at least one vocabulary to be defined.'), 'error');
|
| 203 |
drupal_goto('admin/content/taxonomy');
|
| 204 |
}
|
| 205 |
|
| 206 |
$nat_config = _nat_variable_get();
|
| 207 |
|
| 208 |
foreach ($types as $type => $type_object) {
|
| 209 |
$collapsed = (!isset($nat_config['types'][$type])) || (empty($nat_config['types'][$type]));
|
| 210 |
$form['nat_'. $type] = array(
|
| 211 |
'#type' => 'fieldset',
|
| 212 |
'#title' => check_plain($type_object->name),
|
| 213 |
'#collapsible' => TRUE,
|
| 214 |
'#collapsed' => $collapsed
|
| 215 |
);
|
| 216 |
$form['nat_'. $type][$type] = array(
|
| 217 |
'#type' => 'select',
|
| 218 |
'#title' => t('Vocabularies'),
|
| 219 |
'#options' => $vocabularies,
|
| 220 |
'#default_value' => isset($nat_config['types'][$type]) ? $nat_config['types'][$type] : array(),
|
| 221 |
'#multiple' => TRUE,
|
| 222 |
'#description' => t('Creating a node of type %type will automatically create a term in any selected vocabularies.', array('%type' => $type)),
|
| 223 |
'#parents' => array('types', $type)
|
| 224 |
);
|
| 225 |
$form['nat_'. $type]['body_'. $type] = array(
|
| 226 |
'#type' => 'checkbox',
|
| 227 |
'#title' => t('Associate node body with term description.'),
|
| 228 |
'#default_value' => isset($nat_config['body'][$type]) ? $nat_config['body'][$type] : 0,
|
| 229 |
'#parents' => array('body', $type)
|
| 230 |
);
|
| 231 |
$form['nat_'. $type]['delete_'. $type] = array(
|
| 232 |
'#type' => 'checkbox',
|
| 233 |
'#title' => t('Delete associated term if a node is deleted.'),
|
| 234 |
'#default_value' => isset($nat_config['delete'][$type]) ? $nat_config['delete'][$type] : 0,
|
| 235 |
'#parents' => array('delete', $type)
|
| 236 |
);
|
| 237 |
$form['nat_'. $type]['related_'. $type] = array(
|
| 238 |
'#type' => 'checkbox',
|
| 239 |
'#title' => t('Allow users to define synonyms and related terms when they create and edit nodes.'),
|
| 240 |
'#default_value' => isset($nat_config['related'][$type]) ? $nat_config['related'][$type] : 0,
|
| 241 |
'#parents' => array('related', $type)
|
| 242 |
);
|
| 243 |
$form['nat_'. $type]['node_links_'. $type] = array(
|
| 244 |
'#type' => 'checkbox',
|
| 245 |
'#title' => t('Make NAT terms in %type node views point to the associated node rather than the taxonomy page.', array('%type' => $type)),
|
| 246 |
'#default_value' => isset($nat_config['node_links'][$type]) ? $nat_config['node_links'][$type] : 0,
|
| 247 |
'#parents' => array('node_links', $type)
|
| 248 |
);
|
| 249 |
}
|
| 250 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
|
| 251 |
|
| 252 |
return $form;
|
| 253 |
}
|
| 254 |
|
| 255 |
/**
|
| 256 |
* Process NAT settings form submissions.
|
| 257 |
*/
|
| 258 |
function nat_settings_form_submit($form, &$form_state) {
|
| 259 |
$form_values = $form_state['values'];
|
| 260 |
|
| 261 |
unset($form_values['form_id'], $form_values['form_build_id'], $form_values['submit'], $form_values['op'], $form_values['form_token']);
|
| 262 |
|
| 263 |
$form_values['body'] = array_filter($form_values['body']);
|
| 264 |
$form_values['delete'] = array_filter($form_values['delete']);
|
| 265 |
$form_values['node_links'] = array_filter($form_values['node_links']);
|
| 266 |
$form_values['related'] = array_filter($form_values['related']);
|
| 267 |
|
| 268 |
variable_set('nat_config', $form_values);
|
| 269 |
|
| 270 |
drupal_set_message(t('Configuration settings saved.'));
|
| 271 |
}
|
| 272 |
|
| 273 |
/**
|
| 274 |
* Sync the NAT table with the node table for associated vocabularies.
|
| 275 |
*/
|
| 276 |
function nat_sync_form() {
|
| 277 |
$vocabularies = _nat_get_vocabularies();
|
| 278 |
|
| 279 |
if (empty($vocabularies)) {
|
| 280 |
drupal_set_message(t('The NAT module requires at least one vocabulary to be defined.'), 'error');
|
| 281 |
drupal_goto('admin/content/taxonomy');
|
| 282 |
}
|
| 283 |
|
| 284 |
$nat_config = _nat_variable_get();
|
| 285 |
$options = array();
|
| 286 |
foreach ($nat_config['types'] as $type => $associations) {
|
| 287 |
if (!empty($associations)) {
|
| 288 |
foreach ($associations as $vid) {
|
| 289 |
$options[$type .'|'. $vid] = t('@type ‹-› !vocabulary', array('@type' => $type, '!vocabulary' => $vocabularies[$vid]));
|
| 290 |
}
|
| 291 |
}
|
| 292 |
}
|
| 293 |
if (empty($options)) {
|
| 294 |
drupal_set_message(t('There are no vocabularies available to sync.'));
|
| 295 |
drupal_goto('admin/settings/nat');
|
| 296 |
}
|
| 297 |
|
| 298 |
$form['sync'] = array(
|
| 299 |
'#type' => 'fieldset',
|
| 300 |
'#title' => t('Sync associations'),
|
| 301 |
'#description' => t('The Sync operation will create NAT associations (and terms) for nodes (marked for NAT association) not present in the NAT table.'),
|
| 302 |
'#collapsible' => TRUE
|
| 303 |
);
|
| 304 |
$form['sync']['vocabularies'] = array(
|
| 305 |
'#type' => 'checkboxes',
|
| 306 |
'#title' => t('Select the vocabularies to sync with associated node tables'),
|
| 307 |
'#description' => t('Any nodes not already NAT associated with the selected vocabularies will be associated.'),
|
| 308 |
'#required' => TRUE,
|
| 309 |
'#options' => $options
|
| 310 |
);
|
| 311 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Sync tables'));
|
| 312 |
|
| 313 |
return $form;
|
| 314 |
}
|
| 315 |
|
| 316 |
/**
|
| 317 |
* Process NAT sync form submissions.
|
| 318 |
*/
|
| 319 |
function nat_sync_form_submit($form, &$form_state) {
|
| 320 |
_nat_sync_associations(array_filter($form_state['values']['vocabularies']));
|
| 321 |
}
|
| 322 |
|
| 323 |
/**
|
| 324 |
* Gets terms associated with a node.
|
| 325 |
*
|
| 326 |
* @param $nid
|
| 327 |
* The nid of the node whose NAT terms are to be retrieved.
|
| 328 |
* @return $return
|
| 329 |
* An associative array of NAT-associated term objects.
|
| 330 |
*/
|
| 331 |
function nat_get_terms($nid) {
|
| 332 |
static $term_cache = NULL;
|
| 333 |
|
| 334 |
if (isset($term_cache[$nid])) {
|
| 335 |
return $term_cache[$nid];
|
| 336 |
}
|
| 337 |
|
| 338 |
$return = array();
|
| 339 |
|
| 340 |
$result = db_query("SELECT td.* FROM {nat} n INNER JOIN {term_data} td USING (tid) WHERE n.nid = %d", $nid);
|
| 341 |
while ($term = db_fetch_object($result)) {
|
| 342 |
$return[$term->tid] = $term;
|
| 343 |
}
|
| 344 |
|
| 345 |
// Cache result.
|
| 346 |
$term_cache[$nid] = $return;
|
| 347 |
|
| 348 |
return $return;
|
| 349 |
}
|
| 350 |
|
| 351 |
/**
|
| 352 |
* Retrieve the NAT terms associated with a node restricted by vocabulary.
|
| 353 |
*
|
| 354 |
* @param $nid
|
| 355 |
* The node ID of the node whose NAT-terms are to be retrieved.
|
| 356 |
* @param $vocabularies
|
| 357 |
* An array of vocabulary IDs used to optionally retrict the retrieved terms
|
| 358 |
* to a defined set of vocabularies.
|
| 359 |
*/
|
| 360 |
function nat_get_terms_by_vocabulary($nid, $vocabularies = array()) {
|
| 361 |
$terms = nat_get_terms($nid);
|
| 362 |
if (!empty($vocabularies)) {
|
| 363 |
foreach ($terms as $tid => $term) {
|
| 364 |
if (!in_array($term->vid, $vocabularies)) {
|
| 365 |
unset($terms[$tid]);
|
| 366 |
}
|
| 367 |
}
|
| 368 |
}
|
| 369 |
|
| 370 |
return $terms;
|
| 371 |
}
|
| 372 |
|
| 373 |
/**
|
| 374 |
* Retrieve the first / single NAT term associated with a node optionally
|
| 375 |
* restricted by vocabulary.
|
| 376 |
*
|
| 377 |
* @param $nid
|
| 378 |
* The node ID of the node whose NAT-term is to be retrieved.
|
| 379 |
* @param $vocabularies
|
| 380 |
* An optional array of vocabulary IDs used to optionally retrict the
|
| 381 |
* retrieved term to a defined set of vocabularies.
|
| 382 |
*/
|
| 383 |
function nat_get_term($nid, $vocabularies = array()) {
|
| 384 |
$terms = empty($vocabularies) ? nat_get_terms($nid) : nat_get_terms_by_vocabulary($nid, $vocabularies);
|
| 385 |
|
| 386 |
return array_shift($terms);
|
| 387 |
}
|
| 388 |
|
| 389 |
/**
|
| 390 |
* Retrieve all all the children of a node via its NAT association.
|
| 391 |
* Note: taxonomy_select_nodes is a rather resource hungry function.
|
| 392 |
*
|
| 393 |
* @param $nid
|
| 394 |
* The node ID of the node whose child nodes are to be retrieved.
|
| 395 |
* @param $types
|
| 396 |
* Unknown.
|
| 397 |
* @param $vocabularies
|
| 398 |
* Retrict children to nodes categorised using provided vocabularies.
|
| 399 |
* @return
|
| 400 |
* The resource identifier returned by taxonomy_select_nodes.
|
| 401 |
*/
|
| 402 |
function nat_get_children($nid, $types = array(), $vocabularies = array()) {
|
| 403 |
$terms = nat_get_terms_by_vocabulary($nid, $vocabularies);
|
| 404 |
$tids = array_keys($terms);
|
| 405 |
|
| 406 |
return taxonomy_select_nodes($tids);
|
| 407 |
}
|
| 408 |
|
| 409 |
/**
|
| 410 |
* Gets node IDs/nodes associated with a term.
|
| 411 |
*
|
| 412 |
* @param $tids
|
| 413 |
* An array of term IDs whose associated nodes are to be retrived.
|
| 414 |
* @param $get_nodes
|
| 415 |
* A boolean indicating if node_load operations are to be performed on the
|
| 416 |
* associated nodes.
|
| 417 |
* @return $return
|
| 418 |
* An associative array of (nid => node) or (nid => title) depending on the
|
| 419 |
* value of $get_nodes.
|
| 420 |
*/
|
| 421 |
function nat_get_nids($tids, $get_nodes = FALSE) {
|
| 422 |
static $nid_cache = NULL;
|
| 423 |
static $node_cache = NULL;
|
| 424 |
|
| 425 |
$return = array();
|
| 426 |
|
| 427 |
// Keep processing to a minimum for empty tid arrays.
|
| 428 |
if (!empty($tids)) {
|
| 429 |
// Sort tid array to ensure that the cache_string never suffers from order
|
| 430 |
// issues.
|
| 431 |
sort($tids);
|
| 432 |
$cache_string = implode('+', $tids);
|
| 433 |
if ($get_nodes) {
|
| 434 |
if (isset($node_cache[$cache_string])) {
|
| 435 |
return $node_cache[$cache_string];
|
| 436 |
}
|
| 437 |
elseif (isset($nid_cache[$cache_string])) {
|
| 438 |
// If the nid cache stores the same string, node_load() each nid and
|
| 439 |
// return them.
|
| 440 |
$return = array();
|
| 441 |
foreach (array_keys($nid_cache[$cache_string]) as $nid) {
|
| 442 |
$return[$nid] = node_load($nid);
|
| 443 |
}
|
| 444 |
$node_cache[$cache_string] = $return;
|
| 445 |
|
| 446 |
return $return;
|
| 447 |
}
|
| 448 |
}
|
| 449 |
else {
|
| 450 |
if (isset($nid_cache[$cache_string])) {
|
| 451 |
return $nid_cache[$cache_string];
|
| 452 |
}
|
| 453 |
elseif (isset($node_cache[$cache_string])) {
|
| 454 |
// If the node cache stores the same string, retrieve only the nids and
|
| 455 |
// return them.
|
| 456 |
foreach ($node_cache[$cache_string] as $nid => $node) {
|
| 457 |
$return[$nid] = $node->name;
|
| 458 |
}
|
| 459 |
// Cache extracted results.
|
| 460 |
$nid_cache[$cache_string] = $return;
|
| 461 |
|
| 462 |
return $return;
|
| 463 |
}
|
| 464 |
}
|
| 465 |
|
| 466 |
// Results have not been cached.
|
| 467 |
$tids = implode(', ', $tids);
|
| 468 |
$result = db_query("SELECT n.nid, t.name FROM {nat} n INNER JOIN {term_data} t USING (tid) WHERE n.tid IN (". db_placeholders($tids) .")", $tids);
|
| 469 |
while ($node = db_fetch_object($result)) {
|
| 470 |
if ($get_nodes) {
|
| 471 |
$return[$node->nid] = node_load($node->nid);
|
| 472 |
}
|
| 473 |
else {
|
| 474 |
$return[$node->nid] = $node->name;
|
| 475 |
}
|
| 476 |
}
|
| 477 |
|
| 478 |
if ($get_nodes) {
|
| 479 |
$node_cache[$cache_string] = $return;
|
| 480 |
}
|
| 481 |
else {
|
| 482 |
$nid_cache[$cache_string] = $return;
|
| 483 |
}
|
| 484 |
}
|
| 485 |
|
| 486 |
return $return;
|
| 487 |
}
|
| 488 |
|
| 489 |
/**
|
| 490 |
* Update the NAT config to include node->vocabulary associations and related
|
| 491 |
* settings. Commonly used in .install files to register associations and save
|
| 492 |
* the admin some work.
|
| 493 |
*
|
| 494 |
* @param $type
|
| 495 |
* The node type.
|
| 496 |
* @param $vids
|
| 497 |
* Array of vocabulary IDs that the above node type is to be associated with
|
| 498 |
* via NAT.
|
| 499 |
* @param $delete
|
| 500 |
* Boolean to indicate if associated term should be deleted when a node is
|
| 501 |
* deleted.
|
| 502 |
* @param $links
|
| 503 |
* Boolean to indicate if links to NAT terms should point to the associated
|
| 504 |
* nodes instead.
|
| 505 |
* @param $body
|
| 506 |
* Boolean to indicated if the node body should be in sync with the term
|
| 507 |
* description field.
|
| 508 |
* @param $related
|
| 509 |
* Boolean to indicate if related terms and synonyms can be set during node
|
| 510 |
* creation.
|
| 511 |
*/
|
| 512 |
function nat_set_config($type, $vids, $delete = TRUE, $links = TRUE, $body = FALSE, $related = FALSE) {
|
| 513 |
$nat_config = _nat_variable_get();
|
| 514 |
if (!isset($nat_config['types'][$type])) {
|
| 515 |
$nat_config['types'][$type] = array();
|
| 516 |
}
|
| 517 |
foreach ($vids as $vid) {
|
| 518 |
$nat_config['types'][$type][$vid] = $vid;
|
| 519 |
}
|
| 520 |
|
| 521 |
if ($delete) {
|
| 522 |
$nat_config['delete'][$type] = TRUE;
|
| 523 |
}
|
| 524 |
if ($links) {
|
| 525 |
$nat_config['links'][$type] = TRUE;
|
| 526 |
}
|
| 527 |
if ($body) {
|
| 528 |
$nat_config['body'][$type] = TRUE;
|
| 529 |
}
|
| 530 |
if ($related) {
|
| 531 |
$nat_config['related'][$type] = TRUE;
|
| 532 |
}
|
| 533 |
|
| 534 |
variable_set('nat_config', $nat_config);
|
| 535 |
}
|
| 536 |
|
| 537 |
/**
|
| 538 |
* Retrieve all vocabularies.
|
| 539 |
*
|
| 540 |
* @return $vocabularies
|
| 541 |
* An associative array of vocabulary IDs to vocabulary names.
|
| 542 |
*/
|
| 543 |
function _nat_get_vocabularies() {
|
| 544 |
$vocabularies = taxonomy_get_vocabularies();
|
| 545 |
foreach ($vocabularies as $id => $vocabulary) {
|
| 546 |
$vocabularies[$id] = check_plain($vocabulary->name);
|
| 547 |
}
|
| 548 |
|
| 549 |
return $vocabularies;
|
| 550 |
}
|
| 551 |
|
| 552 |
/**
|
| 553 |
* Add node titles as terms into the taxonomy system.
|
| 554 |
* @todo Ideas are welcome to allow retaining the hierarchy for vocabularies not
|
| 555 |
* present in the node form.
|
| 556 |
*
|
| 557 |
* @param $vids
|
| 558 |
* An array of vocabulary IDs denoting the vocabularies into which a NAT term
|
| 559 |
* is to be added.
|
| 560 |
* @param $title
|
| 561 |
* Node title.
|
| 562 |
* @param $body
|
| 563 |
* Node body.
|
| 564 |
* @param $hierarchy
|
| 565 |
* The taxonomy array for the node in question. This is used to, if set,
|
| 566 |
* insert the new term as a child term of the selected parent.
|
| 567 |
* @return $tids
|
| 568 |
* An array of term objects.
|
| 569 |
*/
|
| 570 |
function _nat_add_terms($vids, $title, $body, $hierarchy = array(), $relations = array(), $synonyms = NULL) {
|
| 571 |
$edit = array(
|
| 572 |
'name' => $title,
|
| 573 |
'description' => $body,
|
| 574 |
'weight' => 0
|
| 575 |
);
|
| 576 |
|
| 577 |
if (!empty($synonyms)) {
|
| 578 |
$edit['synonyms'] = $synonyms;
|
| 579 |
}
|
| 580 |
|
| 581 |
$tids = array();
|
| 582 |
|
| 583 |
foreach ($vids as $vid) {
|
| 584 |
// $edit is passed by reference and 'tid' is set with the tid of the new
|
| 585 |
// term.
|
| 586 |
unset($edit['tid']);
|
| 587 |
|
| 588 |
$edit['vid'] = $vid;
|
| 589 |
// Save hierarchy for vocabularies also present in the node form.
|
| 590 |
if (isset($hierarchy[$vid])) {
|
| 591 |
$edit['parent'] = $hierarchy[$vid];
|
| 592 |
}
|
| 593 |
else {
|
| 594 |
$edit['parent'] = array();
|
| 595 |
}
|
| 596 |
|
| 597 |
if (count($relations)) {
|
| 598 |
$edit['relations'] = $relations[$vid];
|
| 599 |
}
|
| 600 |
|
| 601 |
taxonomy_save_term($edit);
|
| 602 |
$tids[] = $edit;
|
| 603 |
}
|
| 604 |
|
| 605 |
return $tids;
|
| 606 |
}
|
| 607 |
|
| 608 |
/**
|
| 609 |
* Update saved node-terms.
|
| 610 |
*
|
| 611 |
* @param $terms
|
| 612 |
* An array of existing NAT-term objects.
|
| 613 |
* @param $title
|
| 614 |
* Title of the node.
|
| 615 |
* @param $body
|
| 616 |
* Body of the node.
|
| 617 |
* @param $hierarchy
|
| 618 |
* The taxonomy array for the node in question. This is used to, if set,
|
| 619 |
* insert the new term as a child term of the selected parent.
|
| 620 |
*/
|
| 621 |
function _nat_update_terms($terms, $title, $body, $hierarchy, $relations = array(), $synonyms = null) {
|
| 622 |
$edit = array(
|
| 623 |
'name' => $title,
|
| 624 |
'description' => $body,
|
| 625 |
'weight' => 0
|
| 626 |
);
|
| 627 |
|
| 628 |
if (!empty($synonyms)) {
|
| 629 |
$edit['synonyms'] = $synonyms;
|
| 630 |
}
|
| 631 |
|
| 632 |
foreach ($terms as $term) {
|
| 633 |
if (isset($hierarchy[$term->vid])) {
|
| 634 |
$edit['parent'] = $hierarchy[$term->vid];
|
| 635 |
}
|
| 636 |
else {
|
| 637 |
$edit['parent'] = array();
|
| 638 |
}
|
| 639 |
|
| 640 |
if (count($relations)) {
|
| 641 |
$edit['relations'] = $relations[$term->vid];
|
| 642 |
}
|
| 643 |
|
| 644 |
$edit['tid'] = $term->tid;
|
| 645 |
taxonomy_save_term($edit);
|
| 646 |
}
|
| 647 |
}
|
| 648 |
|
| 649 |
/**
|
| 650 |
* Delete associated terms from the taxonomy system.
|
| 651 |
* @todo Options to delete child nodes as well etc.
|
| 652 |
*
|
| 653 |
* @param $nid
|
| 654 |
* Node ID of the node whose NAT-terms are to be deleted.
|
| 655 |
*/
|
| 656 |
function _nat_delete_terms($nid) {
|
| 657 |
$terms = nat_get_terms($nid);
|
| 658 |
foreach ($terms as $term) {
|
| 659 |
taxonomy_del_term($term->tid);
|
| 660 |
}
|
| 661 |
}
|
| 662 |
|
| 663 |
/**
|
| 664 |
* Save node-term associations in the NAT table.
|
| 665 |
*
|
| 666 |
* @param $nid
|
| 667 |
* Node ID of the node.
|
| 668 |
* @param $terms
|
| 669 |
* NAT-terms of the above node.
|
| 670 |
*/
|
| 671 |
function _nat_save_association($nid, $terms) {
|
| 672 |
foreach ($terms as $term) {
|
| 673 |
db_query("INSERT INTO {nat} (nid, tid, vid) VALUES (%d, %d, %d)", $nid, $term['tid'], $term['vid']);
|
| 674 |
}
|
| 675 |
}
|
| 676 |
|
| 677 |
/**
|
| 678 |
* Delete node-term associations from the NAT table.
|
| 679 |
*
|
| 680 |
* @param $nid
|
| 681 |
* Node ID of the node which is to be removed from the NAT table.
|
| 682 |
*/
|
| 683 |
function _nat_delete_association($nid) {
|
| 684 |
db_query("DELETE FROM {nat} WHERE nid = %d", $nid);
|
| 685 |
}
|
| 686 |
|
| 687 |
/**
|
| 688 |
* Synchronize NAT node-term relationships. Create associated terms for node
|
| 689 |
* where missing.
|
| 690 |
*
|
| 691 |
* @param $associations
|
| 692 |
* Associative array denoting the node-vocabulary pair that is to be synced.
|
| 693 |
*/
|
| 694 |
function _nat_sync_associations($associations) {
|
| 695 |
$nat_config = _nat_variable_get();
|
| 696 |
|
| 697 |
$counter = 0;
|
| 698 |
foreach ($associations as $association) {
|
| 699 |
$association = explode('|', $association);
|
| 700 |
// This query can possibly be improved.
|
| 701 |
$result = db_query("SELECT n.nid, n.type, n.title, nr.body FROM {node} n INNER JOIN {node_revisions} nr USING (vid) LEFT JOIN {nat} n1 ON (n.nid = n1.nid AND n1.vid = %d) LEFT JOIN {nat} n2 ON (n.nid = n2.nid AND n2.nid <> n1.nid) WHERE n.type = '%s' AND ISNULL(n1.nid)", $association[1], $association[0]);
|
| 702 |
while ($node = db_fetch_array($result)) {
|
| 703 |
// Copying over the node body is optional.
|
| 704 |
$body = isset($nat_config['body'][$node['type']]) ? $node['body'] : '';
|
| 705 |
|
| 706 |
// Add node title as terms.
|
| 707 |
$terms = _nat_add_terms(array($association[1]), $node['title'], $body);
|
| 708 |
|
| 709 |
// Save node-term association in the NAT table.
|
| 710 |
_nat_save_association($node['nid'], $terms);
|
| 711 |
|
| 712 |
$counter++;
|
| 713 |
}
|
| 714 |
}
|
| 715 |
drupal_set_message(t('NAT sync complete: %count nodes synced.', array('%count' => $counter)));
|
| 716 |
}
|
| 717 |
|
| 718 |
/**
|
| 719 |
* Return a NAT module variable.
|
| 720 |
*
|
| 721 |
* @param $name
|
| 722 |
* The name of the variable to retrieve.
|
| 723 |
* @return
|
| 724 |
* The value of the variable requested.
|
| 725 |
*/
|
| 726 |
function _nat_variable_get($name = NULL) {
|
| 727 |
static $variables = array();
|
| 728 |
|
| 729 |
if (empty($variables)) {
|
| 730 |
$defaults = array(
|
| 731 |
'types' => array(),
|
| 732 |
'body' => array(),
|
| 733 |
'delete' => array(),
|
| 734 |
'node_links' => array()
|
| 735 |
);
|
| 736 |
$variables = variable_get('nat_config', array());
|
| 737 |
$variables = array_merge($defaults, $variables);
|
| 738 |
}
|
| 739 |
|
| 740 |
return $name ? $variables[$name] : $variables;
|
| 741 |
}
|