filefield_paths.module 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /**
  3. * @file
  4. * Contains core functions for the File (Field) Paths module.
  5. */
  6. /**
  7. * Include additional files.
  8. */
  9. foreach (module_list() as $module) {
  10. if (file_exists($file = dirname(__FILE__) . "/modules/{$module}.inc")) {
  11. require_once $file;
  12. }
  13. }
  14. /**
  15. * Implements hook_form_alter().
  16. */
  17. function filefield_paths_form_alter(&$form, $form_state, $form_id) {
  18. $field_types = _filefield_paths_get_field_types();
  19. if (isset($form['#field']) && in_array($form['#field']['type'], array_keys($field_types))) {
  20. $entity_info = entity_get_info($form['#instance']['entity_type']);
  21. $settings = isset($form['#instance']['settings']['filefield_paths']) ? $form['#instance']['settings']['filefield_paths'] : array();
  22. // Hide standard File directory field.
  23. $form['instance']['settings']['file_directory']['#access'] = FALSE;
  24. // File (Field) Paths fieldset element.
  25. $form['instance']['settings']['filefield_paths'] = array(
  26. '#type' => 'fieldset',
  27. '#title' => t('File (Field) Path settings'),
  28. '#collapsible' => TRUE,
  29. '#collapsed' => TRUE,
  30. '#weight' => 1,
  31. '#tree' => TRUE,
  32. );
  33. // Additional File (Field) Paths widget fields.
  34. $fields = module_invoke_all('filefield_paths_field_settings');
  35. foreach ($fields as $name => $field) {
  36. // Attach widget fields.
  37. $form['instance']['settings']['filefield_paths'][$name] = array(
  38. '#type' => 'container',
  39. );
  40. // Attach widget field form elements.
  41. if (isset($field['form']) && is_array($field['form'])) {
  42. foreach (array_keys($field['form']) as $delta => $key) {
  43. $form['instance']['settings']['filefield_paths'][$name][$key] = array_merge(
  44. $field['form'][$key],
  45. array(
  46. '#element_validate' => array('token_element_validate'),
  47. '#token_types' => array('file', $entity_info['token type']),
  48. )
  49. );
  50. // Fetch stored value from instance.
  51. if (isset($settings[$name][$key])) {
  52. $form['instance']['settings']['filefield_paths'][$name][$key]['#default_value'] = $settings[$name][$key];
  53. }
  54. }
  55. // Field options.
  56. $form['instance']['settings']['filefield_paths'][$name]['options'] = array(
  57. '#type' => 'fieldset',
  58. '#title' => t('@title options', array('@title' => $field['title'])),
  59. '#collapsible' => TRUE,
  60. '#collapsed' => TRUE,
  61. '#weight' => 1,
  62. '#attributes' => array(
  63. 'class' => array("{$name} cleanup")
  64. ),
  65. );
  66. // @TODO - Make this more modular.
  67. // Cleanup field with Pathauto module.
  68. $form['instance']['settings']['filefield_paths'][$name]['options']['pathauto'] = array(
  69. '#type' => 'checkbox',
  70. '#title' => t('Cleanup using Pathauto') . '.',
  71. '#default_value' => isset($settings[$name]['options']['pathauto']) && module_exists('pathauto')
  72. ? $settings[$name]['options']['pathauto']
  73. : FALSE,
  74. '#description' => t('Cleanup @title using', array('@title' => $field['title'])) . ' ' . l(t('Pathauto settings'), 'admin/config/search/path/settings'),
  75. '#disabled' => !module_exists('pathauto'),
  76. );
  77. // Transliterate field with Transliteration module.
  78. $form['instance']['settings']['filefield_paths'][$name]['options']['transliterate'] = array(
  79. '#type' => 'checkbox',
  80. '#title' => t('Transliterate') . '.',
  81. '#default_value' => isset($settings[$name]['options']['transliterate']) && module_exists('transliteration')
  82. ? $settings[$name]['options']['transliterate']
  83. : 0,
  84. '#description' => t('Transliterate @title', array('@title' => $field['title'])) . '.',
  85. '#disabled' => !module_exists('transliteration'),
  86. );
  87. // Replacement patterns for field.
  88. $form['instance']['settings']['filefield_paths']['token_tree'] = array(
  89. '#type' => 'fieldset',
  90. '#title' => t('Replacement patterns'),
  91. '#collapsible' => TRUE,
  92. '#collapsed' => TRUE,
  93. '#description' => theme('token_tree', array('token_types' => array('file', $entity_info['token type']))),
  94. '#weight' => 10,
  95. );
  96. // Retroactive updates.
  97. $form['instance']['settings']['filefield_paths']['retroactive_update'] = array(
  98. '#type' => 'checkbox',
  99. '#title' => t('Retroactive update'),
  100. '#description' => t('Move and rename previously uploaded files') . '.' .
  101. '<br /> <strong style="color: #FF0000;">' . t('Warning') . ':</strong> ' .
  102. t('This feature should only be used on developmental servers or with extreme caution') . '.',
  103. '#weight' => 11,
  104. );
  105. // Active updating.
  106. $form['instance']['settings']['filefield_paths']['active_updating'] = array(
  107. '#type' => 'checkbox',
  108. '#title' => t('Active updating'),
  109. '#default_value' => isset($settings['active_updating']) ? $settings['active_updating'] : FALSE,
  110. '#description' => t('Actively move and rename previously uploaded files as required') . '.' .
  111. '<br /> <strong style="color: #FF0000;">' . t('Warning') . ':</strong> ' .
  112. t('This feature should only be used on developmental servers or with extreme caution') . '.',
  113. '#weight' => 12
  114. );
  115. }
  116. }
  117. $form['#submit'][] = 'filefield_paths_form_submit';
  118. }
  119. }
  120. /**
  121. * Submit callback for File (Field) Paths settings form.
  122. */
  123. function filefield_paths_form_submit($form, &$form_state) {
  124. // Retroactive updates.
  125. if ($form_state['values']['instance']['settings']['filefield_paths']['retroactive_update']) {
  126. filefield_paths_batch_update($form_state['values']['instance']);
  127. batch_process($form_state['redirect']);
  128. }
  129. }
  130. /**
  131. * Set batch process to update File (Field) Paths.
  132. *
  133. * @param $instance
  134. */
  135. function filefield_paths_batch_update($instance) {
  136. $query = new EntityFieldQuery();
  137. $result = $query->entityCondition('entity_type', $instance['entity_type'])
  138. ->entityCondition('bundle', array($instance['bundle']))
  139. ->fieldCondition($instance['field_name'])
  140. ->execute();
  141. $objects = array_keys($result[$instance['entity_type']]);
  142. // Create batch.
  143. $batch = array(
  144. 'title' => t('Updating File (Field) Paths'),
  145. 'operations' => array(
  146. array('_filefield_paths_batch_update_process', array($objects, $instance))
  147. ),
  148. );
  149. batch_set($batch);
  150. }
  151. /**
  152. *
  153. */
  154. function _filefield_paths_batch_update_process($objects, $instance, &$context) {
  155. if (!isset($context['sandbox']['progress'])) {
  156. $context['sandbox']['progress'] = 0;
  157. $context['sandbox']['max'] = count($objects);
  158. $context['sandbox']['objects'] = $objects;
  159. }
  160. // Process nodes by groups of 5.
  161. $count = min(5, count($context['sandbox']['objects']));
  162. for ($i = 1; $i <= $count; $i++) {
  163. // For each oid, load the object, update the files and save it.
  164. $oid = array_shift($context['sandbox']['objects']);
  165. $entity = current(entity_load($instance['entity_type'], array($oid)));
  166. // Enable active updating if it isn't already enabled.
  167. $active_updating = $instance['settings']['filefield_paths']['active_updating'];
  168. if (!$active_updating) {
  169. $instance['settings']['filefield_paths']['active_updating'] = TRUE;
  170. field_update_instance($instance);
  171. }
  172. // Invoke File (Field) Paths implementation of hook_entity_update().
  173. filefield_paths_entity_update($entity, $instance['entity_type']);
  174. // Restore active updating to it's previous state if necessary.
  175. if (!$active_updating) {
  176. $instance['settings']['filefield_paths']['active_updating'] = $active_updating;
  177. field_update_instance($instance);
  178. }
  179. // Update our progress information.
  180. $context['sandbox']['progress']++;
  181. }
  182. // Inform the batch engine that we are not finished,
  183. // and provide an estimation of the completion level we reached.
  184. if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
  185. $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  186. }
  187. }
  188. /**
  189. * Implements hook_entity_insert().
  190. */
  191. function filefield_paths_entity_insert($entity, $type) {
  192. filefield_paths_entity_update($entity, $type);
  193. }
  194. /**
  195. * Implements hook_entity_update().
  196. */
  197. function filefield_paths_entity_update($entity, $type) {
  198. $field_types = _filefield_paths_get_field_types();
  199. $entity_info = entity_get_info($type);
  200. $bundle_name = !empty($entity_info['entity keys']['bundle']) ? $entity->{$entity_info['entity keys']['bundle']} : $type;
  201. if ($entity_info['fieldable']) {
  202. foreach (field_info_fields($type, $bundle_name) as $field) {
  203. if (in_array($field['type'], array_keys($field_types))) {
  204. $files = array();
  205. $instance = field_info_instance($type, $field['field_name'], $bundle_name);
  206. if (isset($entity->{$field['field_name']})) {
  207. foreach ($entity->{$field['field_name']} as $langcode => &$deltas) {
  208. foreach ($deltas as $delta => &$file) {
  209. // Prepare file.
  210. if (function_exists($function = "{$field['module']}_field_load")) {
  211. $items = array(array(&$file));
  212. $function($type, array($entity), $field, array($instance), $langcode, $items, FIELD_LOAD_CURRENT);
  213. }
  214. $files[] = &$file;
  215. }
  216. }
  217. // Invoke hook_filefield_paths_process_file().
  218. foreach (module_implements('filefield_paths_process_file') as $module) {
  219. if (function_exists($function = "{$module}_filefield_paths_process_file")) {
  220. $function($type, $entity, $field, $instance, $langcode, $files);
  221. }
  222. }
  223. }
  224. }
  225. }
  226. if (isset($entity->revision)) {
  227. // Remember revision flag.
  228. $revision = $entity->revision;
  229. // Remove revision flag as long as fields already processed it, and no need
  230. // to create new revision for moved files.
  231. $entity->revision = FALSE;
  232. }
  233. // Save any changes back to the database.
  234. field_attach_update($type, $entity);
  235. if (isset($entity->revision)) {
  236. // Restore revision flag so that other modules can process it if needed.
  237. $entity->revision = $revision;
  238. }
  239. }
  240. }
  241. /**
  242. * Implements hook_file_presave().
  243. */
  244. function filefield_paths_file_presave($file) {
  245. // Store original filename in the database.
  246. if (empty($file->origname)) {
  247. $file->origname = $file->filename;
  248. }
  249. }
  250. /**
  251. * Run regular expression over all available text-based fields.
  252. */
  253. function _filefield_paths_replace_path($old, $new, $entity) {
  254. // Build regular expression.
  255. $info = parse_url($old);
  256. $info['path'] = !empty($info['path']) ? $info['path'] : '';
  257. $absolute = str_replace("{$info['host']}{$info['path']}", '', file_create_url($old));
  258. $relative = parse_url($absolute, PHP_URL_PATH);
  259. $regex = str_replace('/', '\/', "({$absolute}|{$relative}|{$info['scheme']}://)(styles/.*?/{$info['scheme']}/|)({$info['host']}{$info['path']})");
  260. // Build replacement.
  261. $info = parse_url($new);
  262. $info['path'] = !empty($info['path']) ? $info['path'] : '';
  263. $replacement = "_filefield_paths_replace_path_uri_scheme('\\1', '{$old}', '{$new}') . '\\2{$info['host']}{$info['path']}'";
  264. $fields = field_info_fields();
  265. foreach ($fields as $name => $field) {
  266. if ($field['module'] == 'text' && isset($entity->{$field['field_name']}) && is_array($entity->{$field['field_name']})) {
  267. foreach ($entity->{$field['field_name']} as &$language) {
  268. foreach ($language as &$item) {
  269. $item['value'] = preg_replace("/$regex/e", $replacement, $item['value']);
  270. }
  271. }
  272. }
  273. }
  274. }
  275. /**
  276. * Helper function for File (Field) Paths URI updater regular expression.
  277. *
  278. * Determines what format the old URI prefix was and returns the new URI prefix
  279. * in the same format.
  280. */
  281. function _filefield_paths_replace_path_uri_scheme($prefix, $old, $new) {
  282. switch (TRUE) {
  283. case $prefix == file_uri_scheme($old) . '://':
  284. return file_uri_scheme($new) . '://';
  285. case $prefix == file_create_url(file_uri_scheme($old) . '://'):
  286. return file_create_url(file_uri_scheme($new) . '://');
  287. case $prefix == parse_url(file_create_url(file_uri_scheme($old) . '://'), PHP_URL_PATH):
  288. return parse_url(file_create_url(file_uri_scheme($new) . '://'), PHP_URL_PATH);
  289. }
  290. return $prefix;
  291. }
  292. /**
  293. * Process and cleanup strings.
  294. */
  295. function filefield_paths_process_string($value, $data, $settings = array()) {
  296. $transliterate = module_exists('transliteration') && isset($settings['transliterate']) && $settings['transliterate'];
  297. $pathauto = module_exists('pathauto') && isset($settings['pathauto']) && $settings['pathauto'] == TRUE;
  298. if ($pathauto == TRUE) {
  299. module_load_include('inc', 'pathauto');
  300. }
  301. $paths = explode('/', $value);
  302. foreach ($paths as &$path) {
  303. // Process string tokens.
  304. $path = token_replace($path, $data, array('clear' => TRUE));
  305. // Cleanup with pathauto.
  306. if ($pathauto == TRUE) {
  307. $path_parts = explode('.', $path);
  308. foreach ($path_parts as &$path_part) {
  309. $path_part = pathauto_cleanstring($path_part);
  310. }
  311. $path = implode('.', $path_parts);
  312. }
  313. // Transliterate string.
  314. if ($transliterate == TRUE) {
  315. $path = transliteration_clean_filename($path);
  316. }
  317. }
  318. $value = implode('/', $paths);
  319. // Ensure that there are no double-slash sequences due to empty token values.
  320. $value = preg_replace('/\/+/', '/', $value);
  321. return $value;
  322. }
  323. /**
  324. *
  325. */
  326. function _filefield_paths_get_field_types($reset = FALSE) {
  327. $field_types = &drupal_static(__FUNCTION__);
  328. if (empty($field_types) || $reset) {
  329. $field_types = module_invoke_all('filefield_paths_field_type_info');
  330. $field_types = array_flip($field_types);
  331. foreach (array_keys($field_types) as $type) {
  332. $info = field_info_field_types($type);
  333. $field_types[$type] = array(
  334. 'label' => $info['label']
  335. );
  336. }
  337. }
  338. return $field_types;
  339. }