cms/drupal/modules/system/system.tokens.inc
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Builds placeholder replacement tokens system-wide data.
       
     6  *
       
     7  * This file handles tokens for the global 'site' token type, as well as
       
     8  * 'date' and 'file' tokens.
       
     9  */
       
    10 
       
    11 /**
       
    12  * Implements hook_token_info().
       
    13  */
       
    14 function system_token_info() {
       
    15   $types['site'] = array(
       
    16     'name' => t("Site information"),
       
    17     'description' => t("Tokens for site-wide settings and other global information."),
       
    18   );
       
    19   $types['date'] = array(
       
    20     'name' => t("Dates"),
       
    21     'description' => t("Tokens related to times and dates."),
       
    22   );
       
    23   $types['file'] = array(
       
    24     'name' => t("Files"),
       
    25     'description' => t("Tokens related to uploaded files."),
       
    26     'needs-data' => 'file',
       
    27   );
       
    28 
       
    29   // Site-wide global tokens.
       
    30   $site['name'] = array(
       
    31     'name' => t("Name"),
       
    32     'description' => t("The name of the site."),
       
    33   );
       
    34   $site['slogan'] = array(
       
    35     'name' => t("Slogan"),
       
    36     'description' => t("The slogan of the site."),
       
    37   );
       
    38   $site['mail'] = array(
       
    39     'name' => t("Email"),
       
    40     'description' => t("The administrative email address for the site."),
       
    41   );
       
    42   $site['url'] = array(
       
    43     'name' => t("URL"),
       
    44     'description' => t("The URL of the site's front page."),
       
    45   );
       
    46   $site['url-brief'] = array(
       
    47     'name' => t("URL (brief)"),
       
    48     'description' => t("The URL of the site's front page without the protocol."),
       
    49   );
       
    50   $site['login-url'] = array(
       
    51     'name' => t("Login page"),
       
    52     'description' => t("The URL of the site's login page."),
       
    53   );
       
    54 
       
    55   // Date related tokens.
       
    56   $date['short'] = array(
       
    57     'name' => t("Short format"),
       
    58     'description' => t("A date in 'short' format. (%date)", array('%date' => format_date(REQUEST_TIME, 'short'))),
       
    59   );
       
    60   $date['medium'] = array(
       
    61     'name' => t("Medium format"),
       
    62     'description' => t("A date in 'medium' format. (%date)", array('%date' => format_date(REQUEST_TIME, 'medium'))),
       
    63   );
       
    64   $date['long'] = array(
       
    65     'name' => t("Long format"),
       
    66     'description' => t("A date in 'long' format. (%date)", array('%date' => format_date(REQUEST_TIME, 'long'))),
       
    67   );
       
    68   $date['custom'] = array(
       
    69     'name' => t("Custom format"),
       
    70     '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'))),
       
    71   );
       
    72   $date['since'] = array(
       
    73     'name' => t("Time-since"),
       
    74     'description' => t("A date in 'time-since' format. (%date)", array('%date' => format_interval(REQUEST_TIME - 360, 2))),
       
    75   );
       
    76   $date['raw'] = array(
       
    77     'name' => t("Raw timestamp"),
       
    78     'description' => t("A date in UNIX timestamp format (%date)", array('%date' => REQUEST_TIME)),
       
    79   );
       
    80 
       
    81 
       
    82   // File related tokens.
       
    83   $file['fid'] = array(
       
    84     'name' => t("File ID"),
       
    85     'description' => t("The unique ID of the uploaded file."),
       
    86   );
       
    87   $file['name'] = array(
       
    88     'name' => t("File name"),
       
    89     'description' => t("The name of the file on disk."),
       
    90   );
       
    91   $file['path'] = array(
       
    92     'name' => t("Path"),
       
    93     'description' => t("The location of the file relative to Drupal root."),
       
    94   );
       
    95   $file['mime'] = array(
       
    96     'name' => t("MIME type"),
       
    97     'description' => t("The MIME type of the file."),
       
    98   );
       
    99   $file['size'] = array(
       
   100     'name' => t("File size"),
       
   101     'description' => t("The size of the file."),
       
   102   );
       
   103   $file['url'] = array(
       
   104     'name' => t("URL"),
       
   105     'description' => t("The web-accessible URL for the file."),
       
   106   );
       
   107   $file['timestamp'] = array(
       
   108     'name' => t("Timestamp"),
       
   109     'description' => t("The date the file was most recently changed."),
       
   110     'type' => 'date',
       
   111   );
       
   112   $file['owner'] = array(
       
   113     'name' => t("Owner"),
       
   114     'description' => t("The user who originally uploaded the file."),
       
   115     'type' => 'user',
       
   116   );
       
   117 
       
   118   return array(
       
   119     'types' => $types,
       
   120     'tokens' => array(
       
   121       'site' => $site,
       
   122       'date' => $date,
       
   123       'file' => $file,
       
   124     ),
       
   125   );
       
   126 }
       
   127 
       
   128 /**
       
   129  * Implements hook_tokens().
       
   130  */
       
   131 function system_tokens($type, $tokens, array $data = array(), array $options = array()) {
       
   132   $url_options = array('absolute' => TRUE);
       
   133   if (isset($options['language'])) {
       
   134     $url_options['language'] = $options['language'];
       
   135     $language_code = $options['language']->language;
       
   136   }
       
   137   else {
       
   138     $language_code = NULL;
       
   139   }
       
   140   $sanitize = !empty($options['sanitize']);
       
   141 
       
   142   $replacements = array();
       
   143 
       
   144   if ($type == 'site') {
       
   145     foreach ($tokens as $name => $original) {
       
   146       switch ($name) {
       
   147         case 'name':
       
   148           $site_name = variable_get('site_name', 'Drupal');
       
   149           $replacements[$original] = $sanitize ? check_plain($site_name) : $site_name;
       
   150           break;
       
   151 
       
   152         case 'slogan':
       
   153           $slogan = variable_get('site_slogan', '');
       
   154           $replacements[$original] = $sanitize ? check_plain($slogan) : $slogan;
       
   155           break;
       
   156 
       
   157         case 'mail':
       
   158           $replacements[$original] = variable_get('site_mail', '');
       
   159           break;
       
   160 
       
   161         case 'url':
       
   162           $replacements[$original] = url('<front>', $url_options);
       
   163           break;
       
   164 
       
   165         case 'url-brief':
       
   166           $replacements[$original] = preg_replace(array('!^https?://!', '!/$!'), '', url('<front>', $url_options));
       
   167           break;
       
   168 
       
   169         case 'login-url':
       
   170           $replacements[$original] = url('user', $url_options);
       
   171           break;
       
   172       }
       
   173     }
       
   174   }
       
   175 
       
   176   elseif ($type == 'date') {
       
   177     if (empty($data['date'])) {
       
   178       $date = REQUEST_TIME;
       
   179     }
       
   180     else {
       
   181       $date = $data['date'];
       
   182     }
       
   183 
       
   184     foreach ($tokens as $name => $original) {
       
   185       switch ($name) {
       
   186         case 'short':
       
   187           $replacements[$original] = format_date($date, 'short', '', NULL, $language_code);
       
   188           break;
       
   189 
       
   190         case 'medium':
       
   191           $replacements[$original] = format_date($date, 'medium', '', NULL, $language_code);
       
   192           break;
       
   193 
       
   194         case 'long':
       
   195           $replacements[$original] = format_date($date, 'long', '', NULL, $language_code);
       
   196           break;
       
   197 
       
   198         case 'since':
       
   199           $replacements[$original] = format_interval((REQUEST_TIME - $date), 2, $language_code);
       
   200           break;
       
   201 
       
   202         case 'raw':
       
   203           $replacements[$original] = $sanitize ? check_plain($date) : $date;
       
   204           break;
       
   205       }
       
   206     }
       
   207 
       
   208     if ($created_tokens = token_find_with_prefix($tokens, 'custom')) {
       
   209       foreach ($created_tokens as $name => $original) {
       
   210         $replacements[$original] = format_date($date, 'custom', $name, NULL, $language_code);
       
   211       }
       
   212     }
       
   213   }
       
   214 
       
   215   elseif ($type == 'file' && !empty($data['file'])) {
       
   216     $file = $data['file'];
       
   217 
       
   218     foreach ($tokens as $name => $original) {
       
   219       switch ($name) {
       
   220         // Basic keys and values.
       
   221         case 'fid':
       
   222           $replacements[$original] = $file->fid;
       
   223           break;
       
   224 
       
   225         // Essential file data
       
   226         case 'name':
       
   227           $replacements[$original] = $sanitize ? check_plain($file->filename) : $file->filename;
       
   228           break;
       
   229 
       
   230         case 'path':
       
   231           $replacements[$original] = $sanitize ? check_plain($file->uri) : $file->uri;
       
   232           break;
       
   233 
       
   234         case 'mime':
       
   235           $replacements[$original] = $sanitize ? check_plain($file->filemime) : $file->filemime;
       
   236           break;
       
   237 
       
   238         case 'size':
       
   239           $replacements[$original] = format_size($file->filesize);
       
   240           break;
       
   241 
       
   242         case 'url':
       
   243           $replacements[$original] = $sanitize ? check_plain(file_create_url($file->uri)) : file_create_url($file->uri);
       
   244           break;
       
   245 
       
   246         // These tokens are default variations on the chained tokens handled below.
       
   247         case 'timestamp':
       
   248           $replacements[$original] = format_date($file->timestamp, 'medium', '', NULL, $language_code);
       
   249           break;
       
   250 
       
   251         case 'owner':
       
   252           $account = user_load($file->uid);
       
   253           $name = format_username($account);
       
   254           $replacements[$original] = $sanitize ? check_plain($name) : $name;
       
   255           break;
       
   256       }
       
   257     }
       
   258 
       
   259     if ($date_tokens = token_find_with_prefix($tokens, 'timestamp')) {
       
   260       $replacements += token_generate('date', $date_tokens, array('date' => $file->timestamp), $options);
       
   261     }
       
   262 
       
   263     if (($owner_tokens = token_find_with_prefix($tokens, 'owner')) && $account = user_load($file->uid)) {
       
   264       $replacements += token_generate('user', $owner_tokens, array('user' => $account), $options);
       
   265     }
       
   266   }
       
   267 
       
   268   return $replacements;
       
   269 }