/[drupal]/drupal/modules/system/system.tokens.inc
ViewVC logotype

Contents of /drupal/modules/system/system.tokens.inc

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


Revision 1.4 - (show annotations) (download) (as text)
Sun Oct 11 03:07:20 2009 UTC (6 weeks, 6 days ago) by webchick
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10, HEAD
Changes since 1.3: +2 -2 lines
File MIME type: text/x-php
#557292 by peximo, plach, catch, and yched: Convert node title to Field API.
1 <?php
2 // $Id: system.tokens.inc,v 1.3 2009/10/02 14:49:10 dries Exp $
3
4 /**
5 * @file
6 * Builds placeholder replacement tokens system-wide data.
7 *
8 * This file handles tokens for the global 'site' token type, as well as
9 * 'date' and 'file' tokens.
10 */
11
12 /**
13 * Implement hook_token_info().
14 */
15 function system_token_info() {
16 $types['site'] = array(
17 'name' => t("Site information"),
18 'description' => t("Tokens for site-wide settings and other global information."),
19 );
20 $types['date'] = array(
21 'name' => t("Dates"),
22 'description' => t("Tokens related to times and dates."),
23 );
24 $types['file'] = array(
25 'name' => t("Files"),
26 'description' => t("Tokens related to uploaded files."),
27 'needs-data' => 'file',
28 );
29
30 // Site-wide global tokens.
31 $site['name'] = array(
32 'name' => t("Name"),
33 'description' => t("The name of the site."),
34 );
35 $site['slogan'] = array(
36 'name' => t("Slogan"),
37 'description' => t("The slogan of the site."),
38 );
39 $site['mission'] = array(
40 'name' => t("Mission"),
41 'description' => t("The optional 'mission' of the site."),
42 );
43 $site['mail'] = array(
44 'name' => t("Email"),
45 'description' => t("The administrative email address for the site."),
46 );
47 $site['url'] = array(
48 'name' => t("URL"),
49 'description' => t("The URL of the site's front page."),
50 );
51 $site['url-brief'] = array(
52 'name' => t("URL (brief)"),
53 'description' => t("The URL of the site's front page without the protocol."),
54 );
55 $site['login-url'] = array(
56 'name' => t("Login page"),
57 'description' => t("The URL of the site's login page."),
58 );
59
60 // Date related tokens.
61 $date['short'] = array(
62 'name' => t("Short format"),
63 'description' => t("A date in 'short' format. (%date)", array('%date' => format_date(REQUEST_TIME, 'short'))),
64 );
65 $date['medium'] = array(
66 'name' => t("Medium format"),
67 'description' => t("A date in 'medium' format. (%date)", array('%date' => format_date(REQUEST_TIME, 'medium'))),
68 );
69 $date['long'] = array(
70 'name' => t("Long format"),
71 'description' => t("A date in 'long' format. (%date)", array('%date' => format_date(REQUEST_TIME, 'long'))),
72 );
73 $date['custom'] = array(
74 'name' => t("Custom format"),
75 'description' => t("A date in a custom format. See !php-date for details.", array('!php-date' => l(t('the PHP documentation'), 'http://php.net/manual/en/function.date.php'))),
76 );
77 $date['since'] = array(
78 'name' => t("Time-since"),
79 'description' => t("A data in 'time-since' format. (%date)", array('%date' => format_interval(REQUEST_TIME - 360, 2))),
80 );
81 $date['raw'] = array(
82 'name' => t("Raw timestamp"),
83 'description' => t("A date in UNIX timestamp format (%date)", array('%date' => REQUEST_TIME)),
84 );
85
86
87 // File related tokens.
88 $file['fid'] = array(
89 'name' => t("File ID"),
90 'description' => t("The unique ID of the uploaded file."),
91 );
92 $file['uid'] = array(
93 'name' => t("User ID"),
94 'description' => t("The unique ID of the user who owns the file."),
95 );
96 $file['nid'] = array(
97 'name' => t("Node ID"),
98 'description' => t("The unique ID of the node the file is attached to."),
99 );
100 $file['name'] = array(
101 'name' => t("File name"),
102 'description' => t("The name of the file on disk."),
103 );
104 $file['description'] = array(
105 'name' => t("Description"),
106 'description' => t("An optional human-readable description of the file."),
107 );
108 $file['path'] = array(
109 'name' => t("Path"),
110 'description' => t("The location of the file on disk."),
111 );
112 $file['mime'] = array(
113 'name' => t("MIME type"),
114 'description' => t("The MIME type of the file."),
115 );
116 $file['size'] = array(
117 'name' => t("File size"),
118 'description' => t("The size of the file, in kilobytes."),
119 );
120 $file['path'] = array(
121 'name' => t("URL"),
122 'description' => t("The web-accessible URL for the file."),
123 );
124 $file['timestamp'] = array(
125 'name' => t("Timestamp"),
126 'description' => t("The date the file was most recently changed."),
127 'type' => 'date',
128 );
129 $file['node'] = array(
130 'name' => t("Node"),
131 'description' => t("The node the file is attached to."),
132 'type' => 'date',
133 );
134 $file['owner'] = array(
135 'name' => t("Owner"),
136 'description' => t("The user who originally uploaded the file."),
137 'type' => 'user',
138 );
139
140 return array(
141 'types' => $types,
142 'tokens' => array(
143 'site' => $site,
144 'date' => $date,
145 'file' => $file,
146 ),
147 );
148 }
149
150 /**
151 * Implement hook_tokens().
152 */
153 function system_tokens($type, $tokens, array $data = array(), array $options = array()) {
154 $url_options = array('absolute' => TRUE);
155 if (isset($language)) {
156 $url_options['language'] = $language;
157 }
158 $sanitize = !empty($options['sanitize']);
159
160 $replacements = array();
161
162 if ($type == 'site') {
163 foreach ($tokens as $name => $original) {
164 switch ($name) {
165 case 'name':
166 $site_name = variable_get('site_name', 'Drupal');
167 $replacements[$original] = $sanitize ? check_plain($site_name) : $site_name;
168 break;
169
170 case 'slogan':
171 $slogan = variable_get('site_slogan', '');
172 $replacements[$original] = $sanitize ? check_plain($slogan) : $slogan;
173 break;
174
175 case 'mission':
176 $mission = variable_get('site_mission', '');
177 $replacements[$original] = $sanitize ? filter_xss($mission) : $mission;
178 break;
179
180 case 'mail':
181 $replacements[$original] = variable_get('site_mail', '');
182 break;
183
184 case 'url':
185 $replacements[$original] = url('<front>', $url_options);
186 break;
187
188 case 'url-brief':
189 $replacements[$original] = preg_replace('!^https?://!', '', url('<front>', $url_options));
190 break;
191
192 case 'login-url':
193 $replacements[$original] = url('user', $url_options);
194 break;
195 }
196 }
197 }
198
199 elseif ($type == 'date') {
200 if (empty($data['date'])) {
201 $date = REQUEST_TIME;
202 }
203 else {
204 $date = $data['date'];
205 }
206 $langcode = (isset($language) ? $language->language : NULL);
207
208 foreach ($tokens as $name => $original) {
209 switch ($name) {
210 case 'raw':
211 $replacements[$original] = filter_xss($date);
212 break;
213
214 case 'short':
215 $replacements[$original] = format_date($date, 'short', '', NULL, $langcode);
216 break;
217
218 case 'medium':
219 $replacements[$original] = format_date($date, 'medium', '', NULL, $langcode);
220 break;
221
222 case 'long':
223 $replacements[$original] = format_date($date, 'long', '', NULL, $langcode);
224 break;
225
226 case 'since':
227 $replacements[$original] = format_interval((REQUEST_TIME - $date), 2, $langcode);
228 break;
229 }
230 }
231
232 if ($created_tokens = token_find_with_prefix($tokens, 'custom')) {
233 foreach ($created_tokens as $name => $original) {
234 $replacements[$original] = format_date($date, 'custom', $name, NULL, $langcode);
235 }
236 }
237 }
238
239 elseif ($type == 'file' && !empty($data['file'])) {
240 $file = $data['file'];
241
242 foreach ($tokens as $name => $original) {
243 switch ($name) {
244 // Basic keys and values.
245 case 'fid':
246 $replacements[$original] = $file->fid;
247 break;
248
249 case 'uid':
250 $replacements[$original] = $file->uid;
251 break;
252
253 case 'nid':
254 $replacements[$original] = $file->nid;
255 break;
256
257 // Essential file data
258 case 'name':
259 $replacements[$original] = $sanitize ? check_plain($file->filename) : $file->filename;
260 break;
261
262 case 'description':
263 $replacements[$original] = $sanitize ? filter_xss($file->description) : $file->description;
264 break;
265
266 case 'path':
267 $replacements[$original] = $sanitize ? filter_xss($file->filepath) : $file->filepath;
268 break;
269
270 case 'mime':
271 $replacements[$original] = $sanitize ? filter_xss($file->filemime) : $file->filemime;
272 break;
273
274 case 'size':
275 $replacements[$original] = format_size($file->filesize);
276 break;
277
278 case 'url':
279 $replacements[$original] = url(file_create_url($file->filepath), $url_options);
280 break;
281
282 // These tokens are default variations on the chained tokens handled below.
283 case 'node':
284 if ($nid = $file->nid) {
285 $node = node_load($file->nid);
286 $replacements[$original] = $sanitize ? filter_xss($node->title[FIELD_LANGUAGE_NONE][0]['value']) : $node->title[FIELD_LANGUAGE_NONE][0]['value'];
287 }
288 break;
289
290 case 'timestamp':
291 $replacements[$original] = format_date($file->timestamp, 'medium', '', NULL, (isset($language) ? $language->language : NULL));
292 break;
293
294 case 'owner':
295 $account = user_load($file->uid);
296 $replacements[$original] = $sanitize ? filter_xss($user->name) : $user->name;
297 break;
298 }
299 }
300
301 if ($node_tokens = token_find_with_prefix($tokens, 'node')) {
302 $node = node_load($file->nid);
303 $replacements += token_generate('node', $node_tokens, array('node' => $node), $language, $sanitize);
304 }
305
306 if ($date_tokens = token_find_with_prefix($tokens, 'timestamp')) {
307 $replacements += token_generate('date', $date_tokens, array('date' => $file->timestamp), $language, $sanitize);
308 }
309
310 if (($owner_tokens = token_find_with_prefix($tokens, 'owner')) && $account = user_load($file->uid)) {
311 $replacements += token_generate('user', $owner_tokens, array('user' => $account), $language, $sanitize);
312 }
313 }
314
315 return $replacements;
316 }

  ViewVC Help
Powered by ViewVC 1.1.2