type_user_nids.module

Tracking master branch
  1. drupal
    1. 6 contributions/type_user_nids/type_user_nids.module

This Module creates a User Nid token [unid]. The unid token provides a Number for each node that is unique within the nodes of the same type created by the same author.

Database definitions:

  CREATE TABLE number_unids (
    uid int(10) unsigned NOT NULL,
    type int(10) unsigned NOT NULL,
    num_unids int(10) unsigned NOT NULL,
    PRIMARY KEY (uid, type)
  )
  CREATE TABLE node_unid (
    nid int(10) unsigned NOT NULL,
    unid int(10) unsigned NOT NULL,
    PRIMARY KEY (nid)
  )

Functions & methods

NameDescription
type_user_nids_nodeapiImplementation of hook_nodeapi().
type_user_nids_node_typeImplementation of hook_node_type().
type_user_nids_token_listImplementation of hook_token_list(). Part of Token modules API. See http://drupal.org/node/307140 for more info
type_user_nids_token_valuesImplementation of hook_token_values(). Part of Token modules API. See http://drupal.org/node/307140 for more info
type_user_nids_views_apiImplementation of hook_views_api(). This function tells views that a type_user_nids_views.inc file exists with data/program for the views module
_type_user_nids_generate_unidInternal function called for nodes that don't yet have a unid. Function generates new unid and sets up database with new values.

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * This Module creates a User Nid token [unid].
  5. * The unid token provides a Number for each node that is unique
  6. * within the nodes of the same type created by the same author.
  7. *
  8. * Database definitions:
  9. * @code
  10. * CREATE TABLE number_unids (
  11. * uid int(10) unsigned NOT NULL,
  12. * type int(10) unsigned NOT NULL,
  13. * num_unids int(10) unsigned NOT NULL,
  14. * PRIMARY KEY (uid, type)
  15. * )
  16. * CREATE TABLE node_unid (
  17. * nid int(10) unsigned NOT NULL,
  18. * unid int(10) unsigned NOT NULL,
  19. * PRIMARY KEY (nid)
  20. * )
  21. * @endcode
  22. */
  23. /**
  24. * Internal function called for nodes that don't yet have a unid.
  25. * Function generates new unid and sets up database
  26. * with new values.
  27. */
  28. function _type_user_nids_generate_unid(&$node) {
  29. $node->unid = 1 + db_result(db_query("
  30. SELECT num_unids
  31. FROM {number_unids}
  32. WHERE uid = %d AND type = '%s' ",
  33. $node->uid,
  34. $node->type));
  35. if($node->unid != 1) {
  36. db_query("
  37. UPDATE {number_unids}
  38. SET num_unids = num_unids + 1
  39. WHERE uid = %d AND type = '%s' ",
  40. $node->uid,
  41. $node->type);
  42. }
  43. // else occurs when this node's author has never previously
  44. // created a node of this type.
  45. else {db_query("
  46. INSERT INTO {number_unids}
  47. SET uid = %d, type = '%s', num_unids = 1 ",
  48. $node->uid,
  49. $node->type);
  50. }
  51. db_query("
  52. INSERT INTO {node_unid}
  53. SET nid = %d, unid = %d",
  54. $node->nid,
  55. $node->unid);
  56. }
  57. /**
  58. * Implementation of hook_nodeapi().
  59. */
  60. function type_user_nids_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  61. switch ($op) {
  62. case 'load':
  63. case 'update':
  64. $node->unid = db_result(db_query("
  65. SELECT unid
  66. FROM {node_unid}
  67. WHERE nid = %d",
  68. $node->nid));
  69. case 'insert':
  70. if(!$node->unid) {
  71. _type_user_nids_generate_unid($node);
  72. }
  73. return array('unid' => $node->unid);
  74. case 'delete':
  75. db_query("
  76. DELETE FROM {node_unid}
  77. WHERE nid = %d",
  78. $node->nid);
  79. break;
  80. }
  81. }
  82. /**
  83. * Implementation of hook_node_type().
  84. */
  85. function type_user_nids_node_type($op, $info) {
  86. switch ($op){
  87. case 'delete':
  88. db_query("
  89. DELETE FROM {number_unids}
  90. WHERE type = '%s' ",
  91. $info->type);
  92. break;
  93. case 'update':
  94. if(!empty($info->old_type) && $info->old_type != $info->type) {
  95. db_query("
  96. UPDATE {number_unids}
  97. SET type = '%s'
  98. WHERE type = '%s' ",
  99. $info->type,
  100. $info->old_type);
  101. }
  102. break;
  103. }
  104. }
  105. /**
  106. * Implementation of hook_token_values().
  107. * Part of Token modules API.
  108. * See http://drupal.org/node/307140 for more info
  109. */
  110. function type_user_nids_token_values($type, $object = NULL, $options = array()) {
  111. if($type == 'node') {
  112. $tokens['unid'] = $object->unid;
  113. return $tokens;
  114. }
  115. }
  116. /**
  117. * Implementation of hook_token_list().
  118. * Part of Token modules API.
  119. * See http://drupal.org/node/307140 for more info
  120. */
  121. function type_user_nids_token_list($type = 'all') {
  122. if($type == 'node' || $type == 'all') {
  123. $tokens['node']['unid'] = t("The Type user nid.");
  124. return $tokens;
  125. }
  126. }
  127. /**
  128. * Implementation of hook_views_api().
  129. * This function tells views that a type_user_nids_views.inc file exists
  130. * with data/program for the views module
  131. */
  132. function type_user_nids_views_api() {
  133. return array('api' => 2.0);
  134. }