tmgmt_file.module 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @file
  4. * Module file of the translation management test module.
  5. */
  6. /**
  7. * Implements hook_tmgmt_translator_plugin_info().
  8. */
  9. function tmgmt_file_tmgmt_translator_plugin_info() {
  10. return array(
  11. 'file' => array(
  12. 'label' => t('File translator'),
  13. 'description' => t('File translator that exports and imports files.'),
  14. 'plugin controller class' => 'TMGMTFileTranslatorPluginController',
  15. 'ui controller class' => 'TMGMTFileTranslatorUIController',
  16. ),
  17. );
  18. }
  19. /**
  20. * Implements hook_theme().
  21. */
  22. function tmgmt_file_theme() {
  23. return array(
  24. 'tmgmt_file_html_template' => array(
  25. 'path' => drupal_get_path('module', 'tmgmt_file') . '/templates',
  26. 'template' => 'tmgmt_file_html_template',
  27. ),
  28. );
  29. }
  30. /**
  31. * Import form submit callback.
  32. */
  33. function tmgmt_file_import_form_submit($form, &$form_state) {
  34. // Ensure we have the file uploaded.
  35. $job = $form_state['tmgmt_job'];
  36. $supported_formats = array_keys(tmgmt_file_format_plugin_info());
  37. if ($file = file_save_upload('file', array('file_validate_extensions' => array(implode(' ', $supported_formats))))) {
  38. $extension = pathinfo($file->uri, PATHINFO_EXTENSION);
  39. $controller = tmgmt_file_format_controller($extension);
  40. if ($controller) {
  41. // Validate the file on job.
  42. $validated_job = $controller->validateImport($file->uri, $job);
  43. if (!$validated_job) {
  44. $job->addMessage('Failed to validate file, import aborted.', array(), 'error');
  45. }
  46. elseif ($validated_job->tjid != $job->tjid) {
  47. $job->addMessage('The imported file job id @file_tjid does not match the job id @job_tjid.', array(
  48. '@file_tjid' => $validated_job->tjid,
  49. '@job_tjid' => $job->tjid,
  50. ), 'error');
  51. }
  52. else {
  53. try {
  54. // Validation successful, start import.
  55. $job->addTranslatedData($controller->import($file->uri));
  56. $job->addMessage('Successfully imported file.');
  57. } catch (Exception $e) {
  58. $job->addMessage('File import failed with the following message: @message', array('@message' => $e->getMessage()), 'error');
  59. }
  60. }
  61. }
  62. }
  63. foreach ($job->getMessagesSince() as $message) {
  64. // Ignore debug messages.
  65. if ($message->type == 'debug') {
  66. continue;
  67. }
  68. if ($text = $message->getMessage()) {
  69. drupal_set_message(filter_xss($text), $message->type);
  70. }
  71. }
  72. }
  73. /**
  74. * Returns information about file format plugins.
  75. *
  76. * @param $plugin
  77. * (Optional) Name of a plugin/extension.
  78. *
  79. * @return array
  80. * If a plugin name is provided, information about that plugin, an array of
  81. * plugin information otherwise. The information of each plugin consists of
  82. * the label and plugin controller class, keyed by the plugin name which is
  83. * also the extension for that file format.
  84. */
  85. function tmgmt_file_format_plugin_info($plugin = NULL) {
  86. return _tmgmt_plugin_info('file_format', $plugin);
  87. }
  88. /**
  89. * Returns an array of file format plugin labels.
  90. */
  91. function tmgmt_file_format_plugin_labels() {
  92. return _tmgmt_plugin_labels('file_format');
  93. }
  94. /**
  95. * Returns the file format plugin controller.
  96. *
  97. * @param $plugin
  98. * (Optional) Name of a plugin/extension.
  99. *
  100. * @return TMGMTFileFormatInterface
  101. * Either a specific file format plugin controller instance or an array of
  102. * available controllers.
  103. */
  104. function tmgmt_file_format_controller($plugin = NULL) {
  105. return _tmgmt_plugin_controller('file_format', $plugin);
  106. }
  107. /**
  108. * Implements hook_tmgmt_file_format_info().
  109. */
  110. function tmgmt_file_tmgmt_file_format_plugin_info() {
  111. return array(
  112. 'xlf' => array(
  113. 'label' => t('XLIFF'),
  114. 'plugin controller class' => 'TMGMTFileFormatXLIFF',
  115. ),
  116. 'html' => array(
  117. 'label' => t('HTML'),
  118. 'plugin controller class' => 'TMGMTFileFormatHTML',
  119. ),
  120. );
  121. }
  122. /**
  123. * Implements hook_tmgmt_job_delete().
  124. */
  125. function tmgmt_file_tmgmt_job_delete(TMGMTJob $job) {
  126. $translator = $job->getTranslator();
  127. // Ignore jobs that don't have a file translator.
  128. if (!$translator || $translator->plugin != 'file') {
  129. return;
  130. }
  131. // Check if there are any files that need to be deleted.
  132. // @todo There doesn't seem to be an API function for this...
  133. $args = array(
  134. ':module' => 'tmgmt_file',
  135. ':type' => 'tmgmt_job',
  136. ':id' => $job->tjid,
  137. );
  138. $result = db_query('SELECT fid FROM {file_usage} WHERE module = :module and type = :type and id = :id', $args);
  139. $fids = $result->fetchCol();
  140. if (!empty($fids)) {
  141. foreach (file_load_multiple($fids) as $file) {
  142. file_usage_delete($file, 'tmgmt_file', 'tmgmt_job', $job->tjid);
  143. // It is very unlikely that these files are used anywhere else. Delete it.
  144. file_delete($file);
  145. }
  146. }
  147. }
  148. /**
  149. * Implements hook_file_download().
  150. */
  151. function tmgmt_file_file_download($uri) {
  152. // Get the file record based on the URI. If not in the database just return.
  153. $files = file_load_multiple(array(), array('uri' => $uri));
  154. if (count($files)) {
  155. foreach ($files as $item) {
  156. // Since some database servers sometimes use a case-insensitive comparison
  157. // by default, double check that the filename is an exact match.
  158. if ($item->uri === $uri) {
  159. $file = $item;
  160. break;
  161. }
  162. }
  163. }
  164. if (!isset($file)) {
  165. return;
  166. }
  167. // Check if this file belongs to a job.
  168. $usage_list = file_usage_list($file);
  169. if (!isset($usage_list['tmgmt_file']['tmgmt_job'])) {
  170. return;
  171. }
  172. foreach (tmgmt_job_load_multiple(array_keys($usage_list['tmgmt_file']['tmgmt_job'])) as $job) {
  173. if (tmgmt_job_access('view', $job)) {
  174. // Access is granted.
  175. $headers = file_get_content_headers($file);
  176. return $headers;
  177. }
  178. }
  179. // Returning nothing means access denied unless another module specifically
  180. // grants access.
  181. }