Project

General

Profile

Paste
Download (6.43 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / file_entity / file_entity.devel_generate.inc @ a8cee257

1
<?php
2

    
3
/**
4
 * @file
5
 * Devel generate integration for the File Entity module.
6
 */
7

    
8
/**
9
 * Devel generate file form.
10
 *
11
 * Options for Devel generate file integration.
12
 */
13
function file_entity_generate_file_form() {
14
  $form['count'] = array(
15
    '#type' => 'textfield',
16
    '#title' => t('How many files would you like to generate?'),
17
    '#default_value' => 50,
18
    '#size' => 4,
19
  );
20
  $form['file_types'] = array(
21
    '#type' => 'select',
22
    '#title' => t('File types'),
23
    '#description' => t('Restrict files to these file types.'),
24
    '#options' => file_entity_type_get_names() + array(FILE_TYPE_NONE => t('Undetermined')),
25
    '#multiple' => TRUE,
26
  );
27
  $form['delete'] = array(
28
    '#type' => 'checkbox',
29
    '#title' => t('Delete existing files in specified file types before generating new files.'),
30
    '#default_value' => FALSE,
31
  );
32
  $form['submit'] = array(
33
    '#type' => 'submit',
34
    '#value' => t('Generate'),
35
  );
36

    
37
  return $form;
38
}
39

    
40
/**
41
 * Implements hook_ID_form_submit().
42
 */
43
function file_entity_generate_file_form_submit(&$form, &$form_state) {
44
  $file_types = array_values(array_filter($form_state['values']['file_types']));
45
  $batch = file_entity_generate_file_batch_info($form_state['values']['count'], $file_types, $form_state['values']['delete']);
46
  batch_set($batch);
47
}
48

    
49
/**
50
 * Implements hook_batch_info().
51
 */
52
function file_entity_generate_file_batch_info($count, array $file_types = array(), $delete = FALSE) {
53
  if (empty($file_types)) {
54
    $file_types = array_keys(file_entity_type_get_names());
55
  }
56

    
57
  if ($delete) {
58
    $operations[] = array('file_entity_generate_file_batch_delete', array($file_types));
59
  }
60

    
61
  $operations[] = array('file_entity_generate_file_batch_generate', array($file_types, $count));
62

    
63
  return array(
64
    'operations' => $operations,
65
    'finished' => 'file_entity_generate_file_batch_finished',
66
    'file' => drupal_get_path('module', 'file_entity') . '/file_entity.devel_generate.inc',
67
  );
68
}
69

    
70
/**
71
 * Implements hook_batch_delete().
72
 */
73
function file_entity_generate_file_batch_delete(array $file_types, array &$context) {
74
  if (empty($context['sandbox'])) {
75
    $context['sandbox'] = array();
76
    $context['sandbox']['progress'] = 0;
77
    $context['sandbox']['current_fid'] = 0;
78
    $context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT fid) FROM {file_managed} WHERE type IN (:types)', array(':types' => $file_types))->fetchField();
79
  }
80

    
81
  $limit = 20;
82
  $fids = db_query_range("SELECT fid FROM {file_managed} WHERE type IN (:types) AND fid > :fid ORDER BY fid", 0, $limit, array(':types' => $file_types, ':fid' => $context['sandbox']['current_fid']))->fetchCol();
83
  file_delete_multiple($fids);
84

    
85
  // Update our progress information.
86
  $context['sandbox']['progress'] += count($fids);
87
  $context['sandbox']['current_rid'] = end($fids);
88
  $context['message'] = t('Deleted file @fid.', array('@fid' => $context['sandbox']['current_rid']));
89

    
90
  // Inform the batch engine that we are not finished,
91
  // and provide an estimation of the completion level we reached.
92
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
93
    $context['finished'] = ($context['sandbox']['progress'] >= $context['sandbox']['max']);
94
  }
95
}
96

    
97
/**
98
 * Implements hook_generate_file_batch_generate() using Devel generate api.
99
 */
100
function file_entity_generate_file_batch_generate(array $file_types, $num, array &$context) {
101
  if (empty($context['sandbox'])) {
102
    module_load_include('inc', 'devel_generate');
103
    $context['sandbox'] = array();
104
    $context['sandbox']['progress'] = 0;
105
    $context['sandbox']['max'] = $num;
106
    $context['sandbox']['users'] = devel_get_users();
107
  }
108

    
109
  $limit = 20;
110

    
111
  for ($i = 0; $i < min($limit, $context['sandbox']['max'] - $context['sandbox']['progress']); $i++) {
112
    $type = array_rand(drupal_map_assoc($file_types), 1);
113
    if ($uri = file_entity_generate_file($type)) {
114
      $file = file_uri_to_object($uri, FALSE);
115
      $file->uid = array_rand(drupal_map_assoc($context['sandbox']['users']), 1);
116
      file_save($file);
117
      if (!empty($file->fid)) {
118
        $context['results'][] = $file->fid;
119
      }
120
    }
121
  }
122

    
123
  // Update our progress information.
124
  $context['sandbox']['progress'] += $limit;
125
  //$context['message'] = t('Deleted URL redirect @rid.', array('@rid' => end($rids)));
126

    
127
  // Inform the batch engine that we are not finished,
128
  // and provide an estimation of the completion level we reached.
129
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
130
    $context['finished'] = ($context['sandbox']['progress'] >= $context['sandbox']['max']);
131
  }
132
}
133

    
134
/**
135
 * When the batch is finished set a status message.
136
 */
137
function file_entity_generate_file_batch_finished($success, $results, $operations) {
138
  if ($success) {
139
    drupal_set_message(format_plural(count($results), 'One file created.', '@count files created.'));
140
  }
141
  else {
142
    // An error occurred.
143
    // $operations contains the operations that remained unprocessed.
144
    $error_operation = reset($operations);
145
    drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
146
  }
147
}
148

    
149
/**
150
 * Generate file function for file_entity.
151
 */
152
function file_entity_generate_file($file_type) {
153
  $type = file_type_load($file_type);
154
  $possible_extensions = file_type_get_valid_extensions($type);
155

    
156
  $image_extensions = array('png', 'gif', 'jpg', 'jpeg');
157
  if (array_intersect($possible_extensions, $image_extensions)) {
158
    $extension = array_rand(array_flip($image_extensions));
159
    module_load_include('inc', 'devel_generate', 'image.devel_generate');
160
    $path = devel_generate_image($extension, '100x100', '1500x1500');
161
  }
162
  else {
163
    $extension = array_rand(array_flip($possible_extensions));
164
    module_load_include('inc', 'devel_generate', 'file.devel_generate');
165
    $path = devel_generate_textfile(mt_rand(1024, 102400));
166
  }
167

    
168
  $uri = file_entity_generate_unique_uri($extension);
169
  $dir = dirname($uri);
170
  if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
171
    return FALSE;
172
  }
173

    
174
  if (file_unmanaged_move($path, $uri)) {
175
    return $uri;
176
  }
177

    
178
  return FALSE;
179
}
180

    
181
function file_entity_generate_unique_uri($extension) {
182
  module_load_include('inc', 'devel_generate');
183
  do {
184
    $uri = 'public://devel-generate/' . devel_generate_word(3) . '/' . devel_generate_word(16) . '.' . $extension;
185
  } while (is_file($uri) || db_query("SELECT 1 FROM {file_managed} WHERE uri = :uri", array(':uri' => $uri))->fetchField());
186
  return $uri;
187
}