file_styles.module 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @file styles/contrib/file_styles/file_styles.module
  4. * File widget formatter definitions.
  5. */
  6. // A registry of variable_get defaults.
  7. include_once('includes/file_styles.variables.inc');
  8. /**
  9. * Implementation of Styles module hook_styles_register().
  10. */
  11. function file_styles_styles_register() {
  12. return array(
  13. 'FileStyles' => array(
  14. 'field_types' => 'file',
  15. 'name' => t('file'),
  16. 'description' => t('file styles'),
  17. 'path' => drupal_get_path('module', 'file_styles') .'/includes/styles',
  18. 'file' => 'FileStyles.inc',
  19. ),
  20. );
  21. }
  22. /**
  23. * Styles filter callback.
  24. *
  25. * This will determine the correct style container corresponding to media type.
  26. */
  27. function file_styles_styles_filter($object, $element = NULL) {
  28. // Ensure we're working against the fully loaded file object.
  29. $file = file_styles_uri_to_object($object->uri, TRUE);
  30. // Allow other modules to define their own file styles.
  31. // In general, they'll most likely want to check against the mimetype.
  32. $containers = styles_default_containers('file');
  33. $filters = module_invoke_all('file_styles_filter', $object);
  34. foreach ($filters as $filter) {
  35. if (isset($containers['containers'][$filter])) {
  36. return $filter;
  37. }
  38. }
  39. // Now check the part of the mimetype before the slash.
  40. // Note that we can't use strstr($haystack, $needle, $before_needle)
  41. // < PHP 5.3, so we need a work-around.
  42. $filter = file_styles_strstr($object->filemime, '/', TRUE);
  43. if (isset($containers['containers'][$filter])) {
  44. return $filter;
  45. }
  46. // Fallback to default.
  47. return 'default';
  48. }
  49. /**
  50. * Support for strstr with $before_needle < PHP 5.3.
  51. */
  52. function file_styles_strstr($haystack, $needle, $before_needle = FALSE){
  53. if ($before_needle) {
  54. list($var) = explode($needle, $haystack, 2);
  55. return $var;
  56. }
  57. return strstr($haystack, $needle);
  58. }
  59. /**
  60. * Return the path to the image for previews in Styles UI.
  61. *
  62. * If it doesn't yet exist, then copy the source to the files directory.
  63. *
  64. * @param boolean $replace
  65. * If TRUE, then replace the file.
  66. *
  67. * @return mixed
  68. * The path to the image preview file, or FALSE if unable to copy.
  69. */
  70. function file_styles_preview_image($replace = FALSE) {
  71. $path = file_styles_variable_get('preview_image');
  72. if (!$path || $replace) {
  73. $dir = file_default_scheme() . '://' . file_styles_variable_get('preview_image_directory');
  74. if (file_prepare_directory($dir, FILE_CREATE_DIRECTORY)) {
  75. $source = file_styles_variable_get('image_style_preview_image');
  76. if ($path = file_unmanaged_copy($source, $dir . '/' . basename($source), FILE_EXISTS_REPLACE)) {
  77. file_styles_variable_set('preview_image', $path);
  78. }
  79. }
  80. }
  81. return $path;
  82. }
  83. /**
  84. * Implementation of hook_theme().
  85. */
  86. function file_styles_theme($existing, $type, $theme, $path) {
  87. return array(
  88. 'file_styles_image' => array(
  89. 'variables' => array('image_uri' => '', 'alt' => '', 'title' => '', 'attributes' => array(), 'image_style' => NULL, 'instance' => NULL),
  90. 'file' => 'file_styles.theme.inc',
  91. 'path' => $path . '/includes/themes',
  92. ),
  93. 'file_styles_image_preview' => array(
  94. 'variables' => array('style_name' => NULL),
  95. 'file' => 'file_styles.theme.inc',
  96. 'path' => $path . '/includes/themes',
  97. ),
  98. );
  99. }
  100. /**
  101. * Returns a file object which can be passed to file_save().
  102. *
  103. * @param $uri
  104. * A string containing the URI, path, or filename.
  105. * @param $use_existing
  106. * (Optional) If TRUE and there's an existing file in the {file_managed}
  107. * table with the passed in URI, then that file object is returned.
  108. * Otherwise, a new file object is returned.
  109. * @return
  110. * A file object, or FALSE on error.
  111. *
  112. * @see http://drupal.org/node/685818
  113. */
  114. function file_styles_uri_to_object($uri, $use_existing = FALSE) {
  115. if ($use_existing) {
  116. $query = db_select('file_managed', 'f')
  117. ->fields('f', array('fid'))
  118. ->condition('uri', $uri)
  119. ->execute()
  120. ->fetchCol();
  121. if (!empty($query)) {
  122. $file = file_load(array_shift($query));
  123. }
  124. }
  125. if (!isset($file)) {
  126. global $user;
  127. $uri = file_stream_wrapper_uri_normalize($uri);
  128. $wrapper = file_stream_wrapper_get_instance_by_uri($uri);
  129. $file = new StdClass;
  130. $file->uid = $user->uid;
  131. $file->filename = basename($uri);
  132. $file->uri = $uri;
  133. $file->filemime = file_get_mimetype($uri);
  134. // This is gagged because some uris will not support it.
  135. $file->filesize = @filesize($uri);
  136. $file->timestamp = REQUEST_TIME;
  137. $file->status = FILE_STATUS_PERMANENT;
  138. $file->is_new = TRUE;
  139. }
  140. return $file;
  141. }
  142. /**
  143. * Implements hook_image_style_save().
  144. */
  145. function file_styles_image_style_save($image_style) {
  146. // Rebuild the styles to account for any new image styles.
  147. styles_style_flush();
  148. }
  149. /**
  150. * Implements hook_image_style_delete().
  151. */
  152. function file_styles_image_style_delete($image_style) {
  153. // Rebuild the styles to account for any deleted image styles.
  154. styles_style_flush();
  155. }