-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.admin.inc
492 lines (371 loc) · 15.6 KB
/
class.admin.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
/**
* FAPI definition for the create class form.
*
* @ingroup forms
* @see storage_create_class_form_submit()
*/
function storage_create_class_form(array $form, array &$form_state) {
drupal_set_title(t('Create class'));
$class = new StorageClass();
$form = $class->formElements();
$result = db_select('storage_container', NULL, array('fetch' => PDO::FETCH_ASSOC))
->fields('storage_container')
->orderBy('name')
->orderBy('container_id')
->execute();
$containers = array();
foreach ($result as $fields) {
$container = storage_container_new($fields);
$containers[$container->container_id] = check_plain($container->name());
}
$form['initial_container_id'] = array(
'#type' => 'select',
'#title' => t('Initial container'),
'#options' => $containers,
'#description' => t('Files added to the class will initially only have an instance in this container.<br />If the initial container is not in the class, then files\' instances in it will be destroyed once they have been fully propagated.<br />When it is not possible to serve from a container in the class, this container will be used (while instances still exist).<br />Typically this is used to quickly store files locally on the server, providing a faster experience for the user. This is especially important for images rendered in-request.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Create class'),
);
return $form;
}
/**
* Submit handler for the create class form.
*
* @see storage_create_class_form()
*/
function storage_create_class_form_submit(array $form, array &$form_state) {
$class = new StorageClass($form_state['values']);
$class->options['initial_container_id'] = $form_state['values']['initial_container_id'];
$class->create();
$form_state['redirect'] = $class->path() . '/add';
}
function _storage_class_selectors(StorageClass $class) {
$result = db_select('storage_selector', NULL, array('fetch' => PDO::FETCH_ASSOC))
->fields('storage_selector')
->condition('storage_selector.class_id', $class->class_id)
->execute();
foreach ($result as $selector_info) {
unset($row);
$row[] = $selector_info['selector_id'];
$row[] = $selector_info['migrating'] ? t('Yes') : t('No');
$rows[] = $row;
}
if(empty($rows)) {
$rows[] = array(array('data' => t('No selectors are using this class.'), 'colspan' => 2, 'class' => 'message'));
}
$header = array(t('Selector Id'), t('Migrating away'));
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array('style' => 'width: auto;'),
));
return $output;
}
function storage_class(StorageClass $class) {
$info = array(
t('Files') => $class->count(),
t('Size') => $class->size(TRUE),
t('Serve by redirection') => $class->options['redirect'] ? t('Yes') : t('No'),
t('Serve source') => $class->options['serve_source_uri'] ? t('Yes') : t('No'),
t('Serve as data URI') => $class->options['data_uri'] ? t('Yes') : t('No'),
t('Initial container') => (isset($class->initial_container) && $class->initial_container) ? $class->initial_container->link() : t('n/a'),
);
$output = theme('storage_info_table', array('info' => $info));
$subquery = db_select('storage_selector');
$subquery->join('storage', NULL, 'storage_selector.selector_id = storage.selector_id');
$subquery->join('storage_file', NULL, 'storage.file_id = storage_file.file_id');
$subquery->join('storage_instance', NULL, 'storage.file_id = storage_instance.file_id');
$subquery->fields('storage_instance', array('container_id'));
$subquery->addExpression('COUNT(storage.storage_id)', 'count');
$subquery->addExpression('SUM(storage_file.size)', 'size');
$subquery->condition('storage_selector.class_id', $class->class_id)
->groupBy('storage_instance.container_id');
$query = db_select('storage_class_container', NULL, array('fetch' => PDO::FETCH_ASSOC));
$query->join('storage_container', NULL, 'storage_class_container.container_id = storage_container.container_id');
$query->leftJoin($subquery, 'files', 'storage_container.container_id = files.container_id');
$query->fields('storage_container')
->fields('storage_class_container');
$query->addField('files', 'count', 'count');
$query->addField('files', 'size', 'size');
$result = $query->condition('storage_class_container.class_id', $class->class_id)
->orderBy('storage_class_container.weight')
->orderBy('storage_container.container_id')
->execute();
foreach ($result as $container_info) {
unset($row);
$container = storage_container_load($container_info['container_id']);
$row[] = $container->link();
$row[] = $container_info['propagate'] ? t('Yes') : t('No');
$row[] = $container_info['serving'] ? t('Yes') : t('No');
$row[] = $container_info['remove'] ? t('Yes') : t('No');
$row[] = (int)$container_info['count'];
$row[] = storage_format_byte_count($container_info['size']);
$rows[] = $row;
}
if(empty($rows)) {
$rows[] = array(array('data' => t('There are no containers in this class.'), 'colspan' => 7, 'class' => 'message'));
}
$header = array(t('Container'), t('Propagate'), t('Serving'), t('Remove'), t('Files'), t('Size'));
$output .= theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array('style' => 'width: auto;'),
));
$output .= _storage_class_selectors($class);
return $output;
}
/**
* FAPI definition for the class edit form.
*
* @ingroup forms
* @see storage_class_edit_form_submit()
*/
function storage_class_edit_form(array $form, array &$form_state, StorageClass $class) {
$form = $class->formElements();
$form['#class'] = $class;
$query = db_select('storage_class_container', NULL, array('fetch' => PDO::FETCH_ASSOC));
$query->join('storage_container', NULL, 'storage_class_container.container_id = storage_container.container_id');
$result = $query->fields('storage_container', array('container_id', 'name', 'service_id'))
->fields('storage_class_container')
->condition('storage_class_container.class_id', $class->class_id)
->orderBy('storage_class_container.weight')
->orderBy('storage_container.container_id')
->execute();
$form['#container_ids'] = array();
$form['#tree'] = TRUE;
foreach ($result as $container_info) {
$container = storage_container_load($container_info['container_id']);
$form['#container_ids'][] = $container->container_id;
$form['names'][$container->container_id] = array(
'#markup' => $container->link(),
);
$form['services'][$container->container_id] = array(
'#markup' => check_plain($container->service()->name),
);
$form['weights'][$container->container_id] = array(
'#type' => 'weight',
'#default_value' => $container_info['weight'],
'#attributes' => array('class' => array('container-weight')),
);
$form['propagates'][$container->container_id] = array(
'#type' => 'checkbox',
'#default_value' => $container_info['propagate'],
);
$form['servings'][$container->container_id] = array(
'#type' => 'checkbox',
'#default_value' => $container_info['serving'],
);
$form['removes'][$container->container_id] = array(
'#type' => 'checkbox',
'#default_value' => $container_info['remove'],
);
}
$form['containers'] = array(
'#type' => 'fieldset',
'#title' => t('Class containers'),
);
$result = db_select('storage_container', NULL, array('fetch' => PDO::FETCH_ASSOC))
->fields('storage_container')
->orderBy('name')
->orderBy('container_id')
->execute();
$containers = array();
foreach ($result as $fields) {
$container = storage_container_new($fields);
$containers[$container->container_id] = check_plain($container->name());
}
$form['initial_container_id'] = array(
'#type' => 'select',
'#title' => t('Initial container'),
'#options' => $containers,
'#default_value' => isset($class->options['initial_container_id']) ? $class->options['initial_container_id'] : FALSE,
'#description' => t('Files added to the class will initially only have an instance in this container.<br />If the initial container is not in the class, then files\' instances in it will be destroyed once they have been fully propagated.<br />When it is not possible to serve from a container in the class, this container will be used (while instances still exist).<br />Typically this is used to quickly store files locally on the server, providing a faster experience for the user. This is especially important for images rendered in-request.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save')
);
return $form;
}
/**
* Themes the class edit form.
*
* @ingroup themeable
* @see storage_class_edit_form()
*/
function theme_storage_class_edit_form(array $elements) {
$form = $elements['form'];
$output = drupal_render($form['name']);
$output .= drupal_render($form['options']);
if (count($form['#container_ids'])) {
$header = array(t('Container'), t('Service'), t('Weight'), t('Propagate'), t('Serving'), t('Remove'));
$rows = array();
foreach ($form['#container_ids'] as $container_id) {
unset($row);
$row[] = drupal_render($form['names'][$container_id]);
$row[] = drupal_render($form['services'][$container_id]);
$row[] = drupal_render($form['weights'][$container_id]);
$row[] = array(
'data' => drupal_render($form['propagates'][$container_id]),
'align' => 'center',
);
$row[] = array(
'data' => drupal_render($form['servings'][$container_id]),
'align' => 'center',
);
$row[] = array(
'data' => drupal_render($form['removes'][$container_id]),
'align' => 'center',
);
$rows[] = array(
'data' => $row,
'class' => array('draggable'),
);
}
$form['containers']['table'] = array(
'#markup' => theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array('id' => 'class-containers-table', 'style' => 'width: auto;')
)));
$form['containers']['description'] = array(
'#markup' => "<div class=\"description\"><p>All files in the class are stored in all of the class's containers.</p><p>Containers at the top have the highest priority. They can be reordered using the drag-and-drop handles.</p><p>During cron, each file is propagated to the containers in priority order. When a file is fully propagated, instances of that file in containers that are not in the class are destroyed.</p><p>Files are retrieved from the highest priority container that has an instance of the file.</p><p>The same logic applies when serving a file, except that only serving containers are considered.</p><p>Suspended containers are not utilised for any purpose.</p><p>If a container is marked to be removed from the class, this will only occur once doing so will not cause any files to become unrecoverable, i.e. all files have an instance in another container.</p></div>",
);
}
$output .= drupal_render($form['containers']);
drupal_add_tabledrag('class-containers-table', 'order', 'sibling', 'container-weight');
$output .= drupal_render_children($form);
return $output;
}
/**
* Submit handler for the class edit form.
*
* @see storage_class_edit_form()
*/
function storage_class_edit_form_submit(array $form, array &$form_state) {
$class = $form['#class'];
$class->name = $form_state['values']['name'];
$class->options = $form_state['values']['options'];
$class->options['initial_container_id'] = $form_state['values']['initial_container_id'];
$class->update();
foreach ($form['#container_ids'] as $container_id) {
$update = array(
'class_id' => $class->class_id,
'container_id' => $container_id,
'weight' => $form_state['values']['weights'][$container_id],
'propagate' => $form_state['values']['propagates'][$container_id],
'serving' => $form_state['values']['servings'][$container_id],
'remove' => $form_state['values']['removes'][$container_id],
);
drupal_write_record('storage_class_container', $update, array('class_id', 'container_id'));
}
$selector_subquery = db_select('storage_selector')
->fields('storage_selector', array('selector_id'))
->condition('class_id', $class->class_id);
db_update('storage')
->fields(array('status' => STORAGE_STATUS_PROCESS_CRON))
->expression('serving_container', 'NULL')
->condition('selector_id', $selector_subquery, 'IN')
->execute();
cache_clear_all();
$message = "Storage class %name has been updated.";
$replacements = array('%name' => $class->name);
drupal_set_message(t($message, $replacements));
watchdog('storage', $message, $replacements, WATCHDOG_NOTICE, l(t('view'), $class->path()));
$form_state['redirect'] = $class->path();
}
/**
* FAPI definition for the class delete form.
*
* @ingroup forms
* @see storage_class_delete_form_submit()
*/
function storage_class_delete_form(array $form, array &$form_state, StorageClass $class) {
$form['#class'] = $class;
if ($class->selectorCount() > 0) {
$form[] = array(
'#markup' => t("<p>It is not possible to delete this class at the moment, as it is in-use:</p>") .
_storage_class_selectors($class),
);
}
else {
$form[] = array(
'#markup' => t("<p>Are you sure you want to delete this storage class?</p>")
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Delete')
);
}
return $form;
}
/**
* Submit handler for the class delete form.
*
* @see storage_class_delete_form()
*/
function storage_class_delete_form_submit(array $form, array &$form_state) {
$class = $form['#class'];
if ($class->selectorCount() > 0) {
$form_state['redirect'] = $class->path() . '/delete';
}
else {
$class->delete();
$form_state['redirect'] = 'admin/structure/storage';
}
}
/**
* FAPI definition for the class add container form.
*
* @ingroup forms
* @see storage_class_add_container_form_submit()
*/
function storage_class_add_container_form(array $form, array &$form_state, StorageClass $class) {
$form['#class'] = $class;
$selector_subquery = db_select('storage_class_container')
->fields('storage_class_container', array('container_id'))
->condition('class_id', $class->class_id);
$result = db_select('storage_container', NULL, array('fetch' => PDO::FETCH_ASSOC))
->fields('storage_container')
->condition('container_id', $selector_subquery, 'NOT IN')
->orderBy('name')
->orderBy('container_id')
->execute();
$containers = array();
foreach ($result as $fields) {
$container = storage_container_new($fields);
$containers[$container->container_id] = check_plain($container->name());
}
if(count($containers) == 0) {
$form[] = array(
'#markup' => '<p>There are no containers left to add to the class.</p>'
);
return $form;
}
$form['container_id'] = array(
'#title' => t('Container'),
'#type' => 'select',
'#required' => TRUE,
'#options' => $containers,
'#default_value' => 0,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add')
);
return $form;
}
/**
* Submit handler for the class add container form.
*
* @see storage_class_add_container_form()
*/
function storage_class_add_container_form_submit(array $form, array &$form_state) {
$class = $form['#class'];
$container = storage_container_load($form_state['values']['container_id']);
$class->addContainer($container);
$form_state['redirect'] = $class->path();
}