file.class.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * @file
  4. * Define Linkit file search plugin class.
  5. */
  6. /**
  7. * Define that file urls should link directly to the file itself.
  8. */
  9. define('LINKIT_FILE_URL_TYPE_DIRECT', 'direct');
  10. /**
  11. * Define that file urls should force download of the file.
  12. */
  13. define('LINKIT_FILE_URL_TYPE_DOWNLOAD', 'download');
  14. /**
  15. * Define that file urls should link the file entity view page.
  16. */
  17. define('LINKIT_FILE_URL_TYPE_ENTITY', 'entity');
  18. /**
  19. * Reprecents a Linkit file search plugin.
  20. */
  21. class LinkitSearchPluginFile extends LinkitSearchPluginEntity {
  22. /**
  23. * Overrides LinkitSearchPlugin::ui_title().
  24. */
  25. function ui_title() {
  26. return t('Managed files');
  27. }
  28. /**
  29. * Overrides LinkitSearchPlugin::ui_description().
  30. */
  31. function ui_description() {
  32. return t('Extend Linkit with file support (Managed files).');
  33. }
  34. /**
  35. * Overrides LinkitSearchPluginEntity::createDescription().
  36. *
  37. * If the file is an image, a small thumbnail can be added to the description.
  38. * Also, image dimensions can be shown.
  39. */
  40. function createDescription($data) {
  41. $description_array = array();
  42. // Get image info.
  43. $imageinfo = image_get_info($data->uri);
  44. // Add small thumbnail to the description.
  45. if ($this->conf['image_extra_info']['thumbnail']) {
  46. $image = $imageinfo ? theme_image_style(array(
  47. 'width' => $imageinfo['width'],
  48. 'height' => $imageinfo['height'],
  49. 'style_name' => 'linkit_thumb',
  50. 'path' => $data->uri,
  51. )) : '';
  52. }
  53. // Add image dimensions to the description.
  54. if ($this->conf['image_extra_info']['dimensions'] && !empty($imageinfo)) {
  55. $description_array[] = $imageinfo['width'] . 'x' . $imageinfo['height'] . 'px';
  56. }
  57. $description_array[] = parent::createDescription($data);
  58. // Add tiel files scheme to the description.
  59. if ($this->conf['show_scheme']) {
  60. $description_array[] = file_uri_scheme($data->uri) . '://';
  61. }
  62. $description = (isset($image) ? $image : '') . implode('<br />' , $description_array);
  63. return $description;
  64. }
  65. /**
  66. * Overrides LinkitSearchPluginEntity::createGroup().
  67. */
  68. function createGroup($entity) {
  69. // The the standard group name.
  70. $group = parent::createGroup($entity);
  71. // Add the scheme.
  72. if ($this->conf['group_by_scheme']) {
  73. // Get all stream wrappers.
  74. $stream_wrapper = file_get_stream_wrappers();
  75. $group .= ' - ' . $stream_wrapper[file_uri_scheme($entity->uri)]['name'];
  76. }
  77. return $group;
  78. }
  79. /**
  80. * Overrides LinkitSearchPluginEntity::createPath().
  81. *
  82. * If 'Direct download' is enabled, make the link point to the file entity
  83. * download endpoint.
  84. */
  85. function createPath($entity) {
  86. $url_type = isset($this->conf['url_type']) ? $this->conf['url_type'] : $this->getDefaultUrlType();
  87. // We can only support the download type if we have version 2.x of the file_entity module.
  88. if ($url_type == LINKIT_FILE_URL_TYPE_DOWNLOAD && !(module_exists('file_entity') && function_exists('file_entity_download_uri'))) {
  89. $url_type = $this->getDefaultUrlType();
  90. }
  91. switch ($url_type) {
  92. case LINKIT_FILE_URL_TYPE_DIRECT:
  93. // Check if this is a local file.
  94. $wrapper = file_stream_wrapper_get_instance_by_uri($entity->uri);
  95. if ($wrapper instanceof DrupalLocalStreamWrapper) {
  96. // Create a relative URL to the local file.
  97. // See https://www.drupal.org/node/837794.
  98. $path = $wrapper->getDirectoryPath() . '/' . file_uri_target($entity->uri);
  99. }
  100. else {
  101. $path = file_create_url($entity->uri);
  102. }
  103. // Process the uri with the insert plugin.
  104. return linkit_get_insert_plugin_processed_path($this->profile, $path, array('language' => (object) array('language' => FALSE)));
  105. case LINKIT_FILE_URL_TYPE_DOWNLOAD:
  106. $uri = file_entity_download_uri($entity);
  107. // Hack for LINKIT_URL_METHOD_RAW, which won't include the options that
  108. // we pass to linkit_get_insert_plugin_processed_path().
  109. if (isset($uri['options']['query']['token']) && $this->profile->data['insert_plugin']['url_method'] == LINKIT_URL_METHOD_RAW) {
  110. return $uri['path'] . '?token=' . rawurlencode($uri['options']['query']['token']);
  111. }
  112. // Process the uri with the insert plugin.
  113. return linkit_get_insert_plugin_processed_path($this->profile, $uri['path'], $uri['options']);
  114. case LINKIT_FILE_URL_TYPE_ENTITY:
  115. // Pass back to the parent if the user wants entity urls.
  116. return parent::createPath($entity);
  117. }
  118. }
  119. /**
  120. * Overrides LinkitSearchPluginEntity::getQueryInstance().
  121. */
  122. function getQueryInstance() {
  123. // Call the parent getQueryInstance method.
  124. parent::getQueryInstance();
  125. // Only search for permanent files.
  126. $this->query->propertyCondition('status', FILE_STATUS_PERMANENT);
  127. }
  128. /**
  129. * Overrides LinkitSearchPluginEntity::buildSettingsForm().
  130. */
  131. function buildSettingsForm() {
  132. $form = parent::buildSettingsForm();
  133. $form['entity:file']['show_scheme'] = array(
  134. '#title' => t('Show file scheme'),
  135. '#type' => 'checkbox',
  136. '#default_value' => isset($this->conf['show_scheme']) ? $this->conf['show_scheme'] : '',
  137. );
  138. $form['entity:file']['group_by_scheme'] = array(
  139. '#title' => t('Group files by scheme'),
  140. '#type' => 'checkbox',
  141. '#default_value' => isset($this->conf['group_by_scheme']) ? $this->conf['group_by_scheme'] : '',
  142. );
  143. $form['entity:file']['url_type'] = array(
  144. '#title' => t('URL type'),
  145. '#type' => 'radios',
  146. '#options' => array(
  147. LINKIT_FILE_URL_TYPE_DIRECT => t('Direct file link'),
  148. LINKIT_FILE_URL_TYPE_DOWNLOAD => t('Download file link'),
  149. LINKIT_FILE_URL_TYPE_ENTITY => t('Entity view page'),
  150. ),
  151. '#default_value' => isset($this->conf['url_type']) ? $this->conf['url_type'] : $this->getDefaultUrlType(),
  152. );
  153. // We can only support the download type if we have version 2.x of the file_entity module.
  154. if (!(module_exists('file_entity') && function_exists('file_entity_download_uri'))) {
  155. unset($form['entity:file']['url_type']['#options'][LINKIT_FILE_URL_TYPE_DOWNLOAD]);
  156. }
  157. $image_extra_info_options = array(
  158. 'thumbnail' => t('Show thumbnails <em>(using the image style !linkit_thumb_link)</em>', array('!linkit_thumb_link' => l(t('linkit_thumb'), 'admin/config/media/image-styles/edit/linkit_thumb'))),
  159. 'dimensions' => t('Show pixel dimensions'),
  160. );
  161. $form['entity:file']['image_extra_info'] = array(
  162. '#title' => t('Images'),
  163. '#type' => 'checkboxes',
  164. '#options' => $image_extra_info_options,
  165. '#default_value' => isset($this->conf['image_extra_info']) ? $this->conf['image_extra_info'] : array('thumbnail', 'dimensions'),
  166. );
  167. return $form;
  168. }
  169. /**
  170. * Gets the default URL type to use if no URL type has been explicitly set.
  171. *
  172. * @return string The URL type
  173. */
  174. protected function getDefaultUrlType() {
  175. $info = entity_get_info('file');
  176. $callback = $info['uri callback'];
  177. if ($callback == 'entity_metadata_uri_file') {
  178. // The Drupal core file URI callback would be used if we were to use the
  179. // "Entity view page" URL mode, which generates absolute URLs to files.
  180. // The "Direct link" URL mode is preferable in this case as it generates
  181. // relative links.
  182. return LINKIT_FILE_URL_TYPE_DIRECT;
  183. }
  184. else {
  185. // We use a custom URI callback such as in Media 1.x or File Entity 2.x.
  186. // The "Entity view page" URL mode is preferable in this case.
  187. return LINKIT_FILE_URL_TYPE_ENTITY;
  188. }
  189. }
  190. }