/[drupal]/contributions/modules/tokenize/tokenize.module
ViewVC logotype

Contents of /contributions/modules/tokenize/tokenize.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.8 - (show annotations) (download) (as text)
Thu Oct 16 22:24:22 2008 UTC (13 months, 1 week ago) by greggles
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +1 -2 lines
File MIME type: text/x-php
bug #283480 by greggles: left in debugging code - whoops
1 <?php
2 // $Id: tokenize.module,v 1.7 2008/06/23 21:12:56 greggles Exp $
3
4 /**
5 * @file
6 * Provides configurable, automatic tokenization of CCK widgets.
7 *
8 * @ingroup token
9 */
10
11 /**
12 * Implementation of hook_form_alter().
13 *
14 * Adds a checkbox to the field settings form to allow tokenization.
15 * Captures form submission to handle the checkbox.
16 */
17 function tokenize_form_alter($form_id, &$form) {
18 if (isset($form['#node'])) {
19 $node = $form['#node'];
20 }
21 else {
22 //This may seem pretty pointless, but it allows for some splicity down below.
23 $node = new stdClass();
24 $node->type = '';
25 }
26
27 switch ($form_id) {
28 case '_content_admin_field':
29 $tokenize_widget = tokenize_get_settings($form['field_name']['#value'], $form['type_name']['#value'], TRUE);
30
31 // Add a new checkbox for tokenization
32 $form['widget']['tokenize'] = array(
33 '#type' => 'fieldset',
34 '#title' => t('Tokenization'),
35 '#collapsed' => FALSE,
36 '#collapsible' => FALSE,
37 '#description' => t('Because the %t module is enabled, you may use tokens in this widget when creating new nodes of this type. To enable token replacement, check the box below.', array('%t' => 'Token')),
38 );
39
40 $form['widget']['tokenize']['tokenize'] = array(
41 '#type' => 'radios',
42 '#title' => t('Token replacement'),
43 '#default_value' => $tokenize_widget,
44 '#options' => array(
45 FALSE => t('Off'),
46 1 => t('Replace on node views / Preserve tokens'),
47 2 => t('Replace on node submission'),
48 ),
49 );
50
51 // Make sure we capture form submission to deal with our new checkbox.
52 $form['#submit']['tokenize_content_admin_field_submit'] = array();
53
54 break;
55 case $node->type .'_node_form':
56 $fields = tokenize_get_settings(NULL, $node->type);
57 foreach ((array)$fields as $field => $method) {
58 $form[$field][0]['token_help'] = array(
59 '#title' => t('Replacement patterns'),
60 '#type' => 'fieldset',
61 '#collapsible' => TRUE,
62 '#collapsed' => TRUE,
63 '#description' => t('Prefer raw-text replacements for text to avoid problems with HTML entities!'),
64 );
65 $form[$field][0]['token_help']['help'] = array(
66 '#value' => theme('token_help', 'node'),
67 );
68 }
69
70 break;
71
72 }
73 }
74
75 /**
76 * Implementation of hook_nodeapi().
77 *
78 * Does the tokenization for the proper fields on update and insert.
79 */
80 function tokenize_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
81 switch ($op) {
82 case 'submit':
83 $fields = array();
84 $fields = tokenize_get_settings(NULL, $node->type);
85 foreach ((array)$fields as $field => $method) {
86 if ($method == 2) {
87 $new_field = array();
88 foreach ($node->$field as $key => $value) {
89 $value['value'] = token_replace($value['value'], $type = 'node', $node);
90 $new_field[$key] = $value;
91 }
92 $node->$field = $new_field;
93 }
94 }
95 break;
96 case 'view':
97 $fields = array();
98 $fields = tokenize_get_settings(NULL, $node->type);
99 foreach ((array)$fields as $field => $method) {
100 if ($method == 1) {
101 $node->content[$field]['#value'] = token_replace($node->content[$field]['#value'], 'node', $node);
102 }
103 }
104 break;
105 }
106 }
107
108 /**
109 * Form callback to capture submission of the field settings form
110 */
111 function tokenize_content_admin_field_submit($form_id, $form_values) {
112 db_query("DELETE FROM {tokenize} WHERE field_name = '%s' AND type_name = '%s'", $form_values['field_name'], $form_values['type_name']);
113
114 if ($form_values['tokenize'] > 0) {
115 db_query("INSERT INTO {tokenize} (field_name, type_name, method) VALUES ('%s', '%s', %d)", $form_values['field_name'], $form_values['type_name'], $form_values['tokenize']);
116 }
117 }
118
119 /**
120 * Retrieves information on a field for a given content type (optional).
121 */
122 function tokenize_get_settings($field_name = NULL, $type_name = NULL, $reset = FALSE) {
123 $must_cache = FALSE;
124
125 if (!$reset && $cached = cache_get('tokenize')) {
126 // See if there's cached data. If so, use that.
127 $data = unserialize($cached->data);
128 }
129 else {
130 $data = array(
131 'by field' => array(),
132 'by type' => array(),
133 );
134
135 $must_cache = TRUE;
136 $qs = db_query("SELECT * FROM {tokenize}") ;
137
138 while ($obj = db_fetch_object($qs)) {
139 $data['by field'][$obj->field_name][$obj->type_name] = $obj->method;
140 $data['by type'][$obj->type_name][$obj->field_name] = $obj->method;
141 }
142 }
143
144 if ($must_cache) {
145 if (!empty($cached)) {
146 cache_clear_all('tokenize', 'cache');
147 }
148 cache_set('tokenize', 'cache', serialize($data), CACHE_PERMANENT);
149 }
150
151 if (empty($field_name) && empty($type_name)) {
152 return $data;
153 }
154 if (empty($field_name) && !empty($type_name)) {
155 return $data['by type'][$type_name];
156 }
157
158 if (!empty($field_name) && empty($type_name)) {
159 return $data['by_field'][$field_name];
160 }
161
162 return isset($data['by type'][$type_name][$field_name]) ? $data['by type'][$type_name][$field_name] : NULL;
163
164 }

  ViewVC Help
Powered by ViewVC 1.1.2