master branch
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)
)
| Name | Description |
|---|---|
| type_user_nids_nodeapi | Implementation of hook_nodeapi(). |
| type_user_nids_node_type | Implementation of hook_node_type(). |
| type_user_nids_token_list | Implementation of hook_token_list(). Part of Token modules API. See http://drupal.org/node/307140 for more info |
| type_user_nids_token_values | Implementation of hook_token_values(). Part of Token modules API. See http://drupal.org/node/307140 for more info |
| type_user_nids_views_api | Implementation 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_unid | Internal function called for nodes that don't yet have a unid. Function generates new unid and sets up database with new values. |
- <?php
-
- /**
- * @file
- * 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:
- * @code
- * 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)
- * )
- * @endcode
- */
-
- /**
- * Internal function called for nodes that don't yet have a unid.
- * Function generates new unid and sets up database
- * with new values.
- */
- function _type_user_nids_generate_unid(&$node) {
- $node->unid = 1 + db_result(db_query("
- SELECT num_unids
- FROM {number_unids}
- WHERE uid = %d AND type = '%s' ",
- $node->uid,
- $node->type));
- if($node->unid != 1) {
- db_query("
- UPDATE {number_unids}
- SET num_unids = num_unids + 1
- WHERE uid = %d AND type = '%s' ",
- $node->uid,
- $node->type);
- }
- // else occurs when this node's author has never previously
- // created a node of this type.
- else {db_query("
- INSERT INTO {number_unids}
- SET uid = %d, type = '%s', num_unids = 1 ",
- $node->uid,
- $node->type);
- }
- db_query("
- INSERT INTO {node_unid}
- SET nid = %d, unid = %d",
- $node->nid,
- $node->unid);
- }
-
- /**
- * Implementation of hook_nodeapi().
- */
- function type_user_nids_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
- switch ($op) {
- case 'load':
- case 'update':
- $node->unid = db_result(db_query("
- SELECT unid
- FROM {node_unid}
- WHERE nid = %d",
- $node->nid));
- case 'insert':
- if(!$node->unid) {
- _type_user_nids_generate_unid($node);
- }
- return array('unid' => $node->unid);
- case 'delete':
- db_query("
- DELETE FROM {node_unid}
- WHERE nid = %d",
- $node->nid);
- break;
- }
- }
-
- /**
- * Implementation of hook_node_type().
- */
- function type_user_nids_node_type($op, $info) {
- switch ($op){
- case 'delete':
- db_query("
- DELETE FROM {number_unids}
- WHERE type = '%s' ",
- $info->type);
- break;
- case 'update':
- if(!empty($info->old_type) && $info->old_type != $info->type) {
- db_query("
- UPDATE {number_unids}
- SET type = '%s'
- WHERE type = '%s' ",
- $info->type,
- $info->old_type);
- }
- break;
- }
- }
-
- /**
- * Implementation of hook_token_values().
- * Part of Token modules API.
- * See http://drupal.org/node/307140 for more info
- */
- function type_user_nids_token_values($type, $object = NULL, $options = array()) {
- if($type == 'node') {
- $tokens['unid'] = $object->unid;
- return $tokens;
- }
- }
-
- /**
- * Implementation of hook_token_list().
- * Part of Token modules API.
- * See http://drupal.org/node/307140 for more info
- */
- function type_user_nids_token_list($type = 'all') {
- if($type == 'node' || $type == 'all') {
- $tokens['node']['unid'] = t("The Type user nid.");
- return $tokens;
- }
- }
-
- /**
- * Implementation of hook_views_api().
- * This function tells views that a type_user_nids_views.inc file exists
- * with data/program for the views module
- */
- function type_user_nids_views_api() {
- return array('api' => 2.0);
- }
-