Project

General

Profile

Download (3.51 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
/**
4
 * Helper for the hook_cron().
5
 */
6
function _dwca_export_cron(){
7
  if(variable_get('dwca_export_rebuild', TRUE) && variable_get('dwca_export_zip_build_time', 0) < time() + 86400){
8
    // Get a list of views on which we operate.  FIXME - This should perhaps be
9
    // "altered" using a drupal_alter call.
10
    module_load_include('views_default.inc', 'dwca_export');
11
    //$views = dwca_export_views_default_views();
12
    $views = array();
13
    foreach(module_implements('dwca_export') as $module){
14
      if(function_exists($module . '_views_default_views')){
15
        $func = $module . '_views_default_views';
16
        $views = array_merge($views, $func());
17
      }
18
    }
19
    // Array of files to add to the zip.
20
    $files_to_add = array();
21
    foreach($views as $view){
22
      // Filename based on the name of the view.
23
      $filename = 'public://' . substr($view->name, 12) . FILE_EXTENSION;
24
      // Get the view and render it.
25
      $view = views_get_view($view->name);
26
      $data = $view->render('views_data_export_1');
27
      $data_saved = FALSE;
28
      $files_to_add[$filename] = $filename;
29
      // the description.txt file is made up from lots of views.  We do special
30
      // things for the dwca_export_description* views
31
      if(substr($filename, 9, 11) == 'description'){
32
        // Unset the description_*.txt filename, and instead set description.txt
33
        unset($files_to_add[$filename]);
34
        $filename = 'public://description' . FILE_EXTENSION;
35
        $files_to_add[$filename] = $filename;
36
        // Append if it already exists
37
        if(file_exists($filename)){
38
          file_unmanaged_save_data(file_get_contents($filename) . $data, $filename, FILE_EXISTS_REPLACE);
39
          $data_saved = TRUE;
40
        }
41
      }
42
      if(!$data_saved){
43
        // Save the data.
44
        file_unmanaged_save_data($data, $filename, FILE_EXISTS_REPLACE);
45
      }
46
    }
47
    // If we have reached here, then we have all the files we require, and can
48
    // therefore build the zip file and delete the files.
49
    variable_set('dwca_export_zip_build_time', time());
50
    $tmp_archive_file_name = drupal_realpath(drupal_tempnam("temporary://", "dwca_export_archive_"));
51
    // Unfortunately we cannot use drupals ArchiverZip because there ís
52
    // no way to pass in ZipArchive::CREATE to the constructor to create the archive
53
    // TODO test if zip functionality is available (i.e. if(function_exists('zip_open'))
54
    // but I don't know where the proper location for such a check would be
55
    $zip = new ZipArchive();
56
    if(!$zip->open($tmp_archive_file_name, ZipArchive::CREATE)){throw new Exception(t('Could not create temporary zip_archive for DwC-A'));}
57
    // add metadata
58
    //$zip->addFile(drupal_get_path('module', 'dwca_export') . '/static/meta.xml', 'dwca/meta.xml');
59
    $meta_string = file_get_contents(drupal_get_path('module', 'dwca_export') . '/static/meta.xml');
60
    drupal_alter('dwca_export_meta_file', $meta_string);
61
    $zip->addFromString('dwca/meta.xml', $meta_string);
62
    // We need to close after each file is added.  Fuck knows why PHP requires
63
    // this.
64
    $zip->close();
65
    // add the csv data files
66
    foreach($files_to_add as $filename){
67
      $result = $zip->open($tmp_archive_file_name);
68
      $zip->addFile(drupal_realpath($filename), 'dwca/' . substr($filename, 9));
69
      $zip->close();
70
      file_unmanaged_delete($filename);
71
    }
72
    if(!file_unmanaged_move($tmp_archive_file_name, 'public://dwca.zip', FILE_EXISTS_REPLACE)){throw new Exception(t('Unable to move the DwC-A'));}
73
    variable_set('dwca_export_rebuild', FALSE);
74
  }
75
}
(4-4/9)