uc_file.tokens.inc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @file
  4. * Token hooks for the uc_file module.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function uc_file_token_info() {
  10. $type = array(
  11. 'name' => t('File downloads'),
  12. 'description' => t('Tokens for purchased file downloads.'),
  13. 'needs-data' => 'uc_file',
  14. );
  15. $tokens['downloads'] = array(
  16. 'name' => t('Downloads'),
  17. 'description' => t('The list of file download links (if any) associated with an order'),
  18. );
  19. return array(
  20. 'types' => array('uc_file' => $type),
  21. 'tokens' => array('uc_file' => $tokens),
  22. );
  23. }
  24. /**
  25. * Implements hook_tokens().
  26. */
  27. function uc_file_tokens($type, $tokens, $data = array(), $options = array()) {
  28. $language_code = NULL;
  29. if (isset($options['language'])) {
  30. $language_code = $options['language']->language;
  31. }
  32. $sanitize = !empty($options['sanitize']);
  33. $replacements = array();
  34. if ($type == 'uc_file' && !empty($data['uc_file'])) {
  35. $files = $data['uc_file'];
  36. if (isset($tokens['downloads'])) {
  37. $replacements[$tokens['downloads']] = theme('uc_file_downloads_token', array('file_downloads' => $files));
  38. }
  39. }
  40. return $replacements;
  41. }
  42. /**
  43. * Themes the file download links token.
  44. *
  45. * @ingroup themeable
  46. */
  47. function theme_uc_file_downloads_token($variables) {
  48. $file_downloads = $variables['file_downloads'];
  49. $output = '';
  50. foreach ($file_downloads as $file_download) {
  51. // Let's only notify of them of the files, not the directories.
  52. if (is_dir($file_download->filename)) {
  53. continue;
  54. }
  55. $output .= l($file_download->filename, 'download/' . $file_download->fid, array('absolute' => TRUE)) . "\n";
  56. }
  57. return $output;
  58. }