/[drupal]/contributions/modules/skeleton/skeleton_token.inc
ViewVC logotype

Contents of /contributions/modules/skeleton/skeleton_token.inc

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


Revision 1.2 - (show annotations) (download) (as text)
Thu Aug 27 17:36:57 2009 UTC (2 months, 4 weeks ago) by deviantintegral
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-ALPHA1, HEAD
Changes since 1.1: +46 -2 lines
File MIME type: text/x-php
#544560: CCK text field support for tokens.
1 <?php
2
3 // $Id$
4
5 /**
6 * Form API callback for Tokens tab
7 */
8 function skeleton_token_form() {
9 $form = array();
10 $form['skeleton_tokens_help'] = array(
11 '#prefix' => '<p>',
12 '#suffix' => '</p>',
13 '#value' => t('Skeleton tokens allows the use of tokens to automatically customize instantiated templates. For example, the [site-mail] token will insert the site administrative email address. Custom tokens added on this page will be presented in a form to fill in values when instantiating a template.'),
14 );
15 $form['skeleton_tokens'] = array(
16 '#type' => 'markup',
17 '#value' => _skeleton_build_token_help('skeleton'),
18 );
19 $form['tokens_fieldset'] = array(
20 '#type' => 'fieldset',
21 '#title' => t('Node tokens'),
22 '#collapsible' => TRUE,
23 '#collapsed' => TRUE,
24 );
25 $form['tokens_fieldset']['tokens'] = array(
26 '#type' => 'markup',
27 '#value' => _skeleton_build_token_help('node'),
28 );
29 return $form;
30 }
31
32 /**
33 * Implementation of theme function for the skeleton token page.
34 *
35 * @param $form
36 * The Form API array to display
37 * @return
38 * The themed HTML.
39 */
40 function theme_skeleton_token_form($form) {
41 $output .= drupal_render($form);
42 return $output;
43 }
44
45 /**
46 * Forms API for the add token form. This allows new skeleton tokens to be
47 * added and existing tokens to be edited.
48 *
49 * @param $form_id
50 * The form_id of the current form.
51 * @param $token
52 * The token object to edit, or NULL if adding a new token.
53 * @return
54 * Form API form array.
55 */
56 function skeleton_add_token_form($form_id, $token = NULL) {
57 $form = array();
58 $form['token_name'] = array(
59 '#type' => 'textfield',
60 '#title' => t('Token name'),
61 '#description' => t('Enter the name of a new token to create. Do not include the enclosing brackets. Only letters, numbers, and dashes are allowed.'),
62 '#default_value' => empty($token) ? "" : $token->token,
63 '#maxlength' => 80,
64 '#required' => TRUE,
65 );
66 $form['token_description'] = array(
67 '#type' => 'textfield',
68 '#title' => t('Token description'),
69 '#description' => t('Enter a description for this token describing what it is for.'),
70 '#default_value' => empty($token) ? "" : check_plain($token->description),
71 '#maxlength' => 80,
72 '#required' => TRUE,
73 );
74 if (!empty($token)) {
75 $form['token_id'] = array(
76 '#type' => 'value',
77 '#value' => $token->token_id,
78 );
79 }
80 $form['submit'] = array(
81 '#type' => 'submit',
82 '#value' => t('Save'),
83 );
84 $form['cancel'] = array(
85 '#type' => 'submit',
86 '#value' => t('Cancel'),
87 );
88 $form['#redirect'] = 'admin/content/skeleton/token';
89 return $form;
90 }
91
92 /**
93 * Validation function for skeleton_add_token_form.
94 *
95 * We only allow letters, numbers, and dashes in the token name.
96 *
97 * @param $form
98 * The form being validated.
99 * @param $form_state
100 * The current state of skeleton_add_token_form.
101 */
102 function skeleton_add_token_form_validate($form, &$form_state) {
103 if (!($form_state['values']['op'] == t('Cancel'))) {
104 if (!preg_match('!^[a-zA-Z0-9\-]+$!', $form_state['values']['token_name'])) {
105 form_set_error('token_name', t('Only letters, numbers, and dashes are allowed.'));
106 }
107 }
108 }
109
110 /**
111 * Submit function for skeleton_add_token_form.
112 *
113 * Adds or updates a token to the database.
114 *
115 * @param $form
116 * The form being submitted.
117 * @param $form_state
118 * The current state of the skeleton_add_token_form.
119 */
120 function skeleton_add_token_form_submit($form, &$form_state) {
121 if (!($form_state['values']['op'] == t('Cancel'))) {
122 $token = new stdClass();
123 $token->token = $form_state['values']['token_name'];
124 $token->description = $form_state['values']['token_description'];
125 if ($form_state['values']['token_id']) {
126 $token->token_id = $form_state['values']['token_id'];
127 db_query("UPDATE {skeleton_token} SET token = '%s', description = '%s' WHERE token_id = %d", $token->token, $token->description, $token->token_id);
128 }
129 else {
130 db_query("INSERT INTO {skeleton_token} (token, description) VALUES('%s', '%s')", $token->token, $token->description);
131 }
132 }
133 }
134
135 /**
136 * Form API for the delete token form.
137 *
138 * @param $form_id
139 * The form_id of the current form.
140 * @param $token
141 * The token object being deleted.
142 * @return
143 * Form API array of the deletion form.
144 */
145 function skeleton_delete_token_form($form_id, $token) {
146 $form['token_id'] = array(
147 '#type' => 'value',
148 '#value' => $token->token_id,
149 );
150 return confirm_form($form,
151 t('Are you sure you want to delete the token %token?', array('%token' => $token->token)),
152 'admin/content/skeleton/token',
153 t('This action cannot be undone.'),
154 t('Delete token'),
155 t('Cancel')
156 );
157 }
158
159 /**
160 * Submit function for skeleton_delete_token_form.
161 *
162 * @param $form
163 * The current form.
164 * @param $form_state
165 * The state of the current form.
166 */
167 function skeleton_delete_token_form_submit($form, &$form_state) {
168 db_query("DELETE from {skeleton_token} WHERE token_id = %d", $form_state['values']['token_id']);
169 drupal_set_message(t('Token deleted.'));
170 drupal_goto('admin/content/skeleton/token');
171 }
172
173 /**
174 * Implementation of hook_token_list.
175 *
176 * @param $type
177 * The type of token list to return. Either 'skeleton' or 'all'.
178 * @return
179 * An array of skeleton tokens.
180 */
181 function skeleton_token_list($type = 'all') {
182 if ($type == 'skeleton' || $type == 'all') {
183 $tokens = array();
184 $tokens['skeleton'] = array();
185 $result = db_query("SELECT * FROM {skeleton_token}");
186 while ($token = db_fetch_object($result)) {
187 $tokens['skeleton'][$token->token] = check_plain($token->description);
188 }
189 return $tokens;
190 }
191 }
192
193 /**
194 * Implementation of hook_token_values.
195 *
196 * @param $type
197 * The type of tokens to return.
198 * @param $object
199 * An array of skeleton tokens with keys as tokens as values as the value to
200 * be replaced.
201 * @param $options
202 * Any options passed to token_replace.
203 * @return
204 * An array of tokens and their replacement values.
205 */
206 function skeleton_token_values($type, $object = NULL, $options = array()) {
207 $tokens = array();
208 if ($type == 'skeleton') {
209 $skeleton_tokens = $object;
210 foreach ($skeleton_tokens as $token => $value) {
211 $tokens[$token] = $value;
212 }
213 }
214 return $tokens;
215 }
216
217 /**
218 * Private function to generate HTML for showing the available tokens.
219 *
220 * @param type
221 * The token type to return, or 'all' for all tokens.
222 * @return
223 * The themed representation of the available tokens.
224 */
225 function _skeleton_build_token_help($type = 'all') {
226 if ($type == 'skeleton') {
227 return _skeleton_build_skeleton_token_help();
228 }
229 else {
230 return _skeleton_build_other_token_help($type);
231 }
232 }
233
234 /**
235 * Build the help for custom skeleton tokens.
236 *
237 * @return
238 * The themed representation of available skeleton tokens.
239 */
240 function _skeleton_build_skeleton_token_help() {
241 static $help_html;
242 if (empty($help_html)) {
243 $skeleton_tokens = array();
244 $result = db_query("SELECT * FROM {skeleton_token}");
245 while ($token = db_fetch_object($result)) {
246 $token->description = check_plain($token->description);
247 $skeleton_tokens[] = $token;
248 }
249 if (empty($skeleton_tokens)) {
250 $help_html = '<p>' . t('No custom tokens have been created. <a href="@add-url">Add a token</a> to use them in skeleton outlines.', array('@add-url' => url('admin/content/skeleton/token/add')));
251 }
252 else {
253 $help_html = theme('skeleton_token_help', $skeleton_tokens);
254 }
255 }
256 return $help_html;
257 }
258
259 /**
260 * Private function to generate HTML for showing the available tokens
261 *
262 * @param type
263 * The token type to return, or 'all' for all tokens.
264 * @return
265 * The themed representation of the available tokens.
266 */
267 function _skeleton_build_other_token_help($type = 'all') {
268 if (!isset($help_html)) {
269 static $help_html = array();
270 }
271 $help_html[$type] = '';
272 if (empty($help_html[$type])) {
273 if ($type == 'all') {
274 $patterns = token_get_list();
275 }
276 else {
277 $patterns = token_get_list($type);
278 // We unset global tokens so they only show when 'all' is passed.
279 unset($patterns['global']);
280 }
281 foreach ($patterns as $pattern_type => $pattern_set) {
282 foreach ($pattern_set as $pattern => $description) {
283 $tokens[$pattern] = $description;
284 }
285 }
286 $help_html[$type] = theme('skeleton_other_token_help', $tokens);
287 }
288 return $help_html[$type];
289 }
290
291 /**
292 * Replace tokens within the title, body, and an CCK text fields on a node.
293 *
294 * @param $node
295 * The node object to act upon.
296 *
297 * @param $type
298 * The token type to replace, corresponding to the types used with
299 * token_replace().
300 *
301 * @param $tokens
302 * The tokens to pass to token_replace().
303 *
304 * @param $fix_bookpathalias [optional]
305 * If the node contains [bookpathalias] tokens, replace them manually so that
306 * it can be used when the node hasn't been saved yet.
307 *
308 * @return
309 * The node object with the tokenized title, body, and CCK text fields.
310 */
311 function _skeleton_token_replace_values($node, $type, $tokens, $fix_bookpathalias = FALSE) {
312 // For subpages, we can simply simulate the [bookpathalias] token here.
313 // Otherwise, it will be replaced with '' during token_replace() as the
314 // node isn't saved yet.
315 if ($fix_bookpathalias && module_exists('pathauto')) {
316 $book_root = node_load($node->book['bid']);
317 $node->title = str_replace('[bookpathalias]', drupal_get_path_alias($book_root->path), $node->title);
318 $node->body = str_replace('[bookpathalias]', drupal_get_path_alias($book_root->path), $node->body);
319 }
320 $node->title = token_replace($node->title, $type, $tokens);
321 $node->body = token_replace($node->body, $type, $tokens);
322 // Also do any CCK text fields.
323 $content_type = content_types($node->type);
324 $fields = $content_type['fields'];
325 foreach ($fields as $field) {
326 if ($field['type'] == 'text' && is_array($node->{$field['field_name']})) {
327 foreach (array_keys($node->{$field['field_name']}) as $delta) {
328 $node->{$field['field_name']}[$delta]['value'] = token_replace($node->{$field['field_name']}[$delta]['value'], $type, $tokens);
329 }
330 }
331 }
332 return $node;
333 }
334
335 /**
336 * theme_skeleton_token_help()
337 *
338 * @param $tokens
339 * An array of skeleton token objects.
340 *
341 * @return
342 * The themed representation of the tokens with the appropriate operations.
343 */
344 function theme_skeleton_token_help($tokens) {
345 $rows = array();
346 foreach ($tokens as $token) {
347 $row = array();
348 $row[] = '[' . $token->token . ']';
349 $row[] = $token->description;
350 $row[] = theme('item_list', array(l(t('edit token'), 'admin/content/skeleton/token/' . $token->token_id . '/edit'), l(t('delete token'), 'admin/content/skeleton/token/' . $token->token_id . '/delete')));
351 $rows[] = $row;
352 }
353 $header = array(t('Token'), t('Description'), t('Operations'));
354 $tokens_html .= theme('table', $header, $rows);
355
356 return $tokens_html;
357 }
358
359 /**
360 * Theme help for tokens other than skeleton tokens.
361 *
362 * @param $tokens
363 * An array of tokens to display.
364 * @return
365 * The themed representation of the tokens with the appropriate operations.
366 */
367 function theme_skeleton_other_token_help($tokens) {
368 $tokens_html = "";
369 $rows = array();
370 foreach ($tokens as $name => $description) {
371 $row = array();
372 $row[] = '['. $name .']';
373 $row[] = $description;
374 $rows[] = $row;
375 }
376 $header = array(t('Token'), t('Description'));
377 $tokens_html .= theme('table', $header, $rows);
378
379 return $tokens_html;
380 }

  ViewVC Help
Powered by ViewVC 1.1.2