-
Notifications
You must be signed in to change notification settings - Fork 1
/
timeline.install
495 lines (456 loc) · 14.8 KB
/
timeline.install
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
492
493
494
495
<?php
// $Id$
/**
* @file
* Install file for Node timeline module.
*
* The definition of the fields for the module is here,
*
* See @link http://drupal.org/node/707832 Field API Tutorial @endlink
*
* See @link http://drupal.org/node/443536 Field API Handbook Page @endlink
*
* See @link field Field API documentation @endlink
*
*/
/**
* Implements hook_install().
*
* - Add the body field.
* - Configure the body field.
* - Create color, quantity, and image fields.
* - Create color, quantity, and image instances.
*
* @see node_type_set_defaults()
* @see field_info_instance()
* @see field_update_instance()
* @see field_create_field()
* @see field_create_instance()
*/
function timeline_install() {
// use get_t() to get the name of our localization function for translation
// during install, when t() is not available.
$t = get_t();
// Define the node type.
$archive = array(
'type' => 'archive',
'name' => $t('Timeline Archive'),
'base' => 'node_content',
'description' => $t('This is an archive for which images will be collected'),
);
// Complete the node type definition by setting any defaults not explicitly
// declared above.
// http://api.drupal.org/api/function/node_type_set_defaults/7
$content_type = node_type_set_defaults($archive);
// Save the content type
node_type_save($content_type);
// Create all the fields we are adding to our content type.
// http://api.drupal.org/api/function/field_create_field/7
foreach (_timeline_archive_installed_fields() as $field) {
field_create_field($field);
}
// Create all the instances for our fields.
// http://api.drupal.org/api/function/field_create_instance/7
foreach (_timeline_archive_installed_instances() as $instance) {
$instance['entity_type'] = 'node';
$instance['bundle'] = $archive['type'];
field_create_instance($instance);
}
timeline_update_7001();
}
/**
* Return a structured array defining the fields created by this content type.
*
* This is packaged in a function so it can be used in both
* node_timeline_install() and node_timeline_uninstall().
*/
function _timeline_archive_installed_fields() {
return array(
'archive_url' => array(
'field_name' => 'archive_url',
'type' => 'text',
'settings' => array(
'max_length' => 255,
),
),
'archive_frequency' => array(
'field_name' => 'archive_frequency',
'type' => 'taxonomy_term_reference',
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => 'timeline_frequencies',
'parent' => 0
),
),
),
),
'archive_base_time' => array(
'field_name' => 'archive_base_time',
'type' => 'datestamp',
),
'preview_picture' => array(
'field_name' => 'preview_picture',
'type' => 'image',
),
);
}
/**
* Return a structured array defining the instances for this content type.
*
* The instance lets Drupal know which widget to use to allow the user to enter
* data and how to react in different view modes. We are going to display a
* page that uses a custom "node_timeline_list" view mode.
*
* This is provided as a function so that it can be used in both hook_install()
* and hook_uninstall().
*/
function _timeline_archive_installed_instances() {
$t = get_t();
return array(
'archive_url' => array(
'field_name' => 'archive_url',
'label' => $t('URL'),
'description' => $t('The URL of the image to be downloaded. Normally ending with .jpg, .jpeg or .png.'),
'type' => 'text',
'required' => true,
'widget' => array(
'type' => 'text_timeline_url',
),
),
'archive_frequency' => array(
'field_name' => 'archive_frequency',
'entity_type' => 'node',
'label' => $t('Interval'),
'description' => $t('In this Interval Images will be downloaded.'),
'required' => true,
'widget' => array(
'type' => 'options_select',
'module' => 'options',
),
'display' => array(
'default' => array('type' => 'hidden'),
'teaser' => array('type' => 'hidden')
),
),
'archive_basetime' => array(
'field_name' => 'archive_basetime',
'label' => $t('Basetime'),
'description' => 'The basetime is the time you want your pictures to be taken. It is used in low-frequency archives do determine the time of the picture. E.g. If in a daily archive the pictures should be taken midnight, morning or noon.',
'type' => 'datetime',
'required' => true,
'widget' => array(
'type' => 'date_popup',
),
),
'preview_picture' => array(
'field_name' => 'preview_picture',
'label' => $t('Preview Picture'),
'type' => 'image',
'widget' => array(
'type' => 'image_upload',
),
),
);
}
function _timeline_frequencies_installed_fields() {
$t = get_t();
return array(
'timeline_frequency_seconds' => array(
'field_name' => 'timeline_frequency_seconds',
'type' => 'number_integer',
),
);
}
function _timeline_frequencies_installed_instances() {
$t = get_t();
return array(
'timeline_frequency_seconds' => array(
'field_name' => 'timeline_frequency_seconds',
'label' => $t('Frequency in seconds'),
'type' => 'number_integer',
'required' => true,
'widget' => array(
'type' => 'number',
),
),
);
}
function _timeline_default_frequencies() {
return array(
array(
'text' => '5 minutes',
'seconds' => 300,
),
array(
'text' => '1 hour',
'seconds' => 3600,
),
array(
'text' => '1 day',
'seconds' => 86400,
),
);
}
/**
* Implements hook_uninstall().
*
*/
function timeline_uninstall() {
// Gather all the timeline content that might have been created while this
// module was enabled. Simple selects still use db_query().
// http://api.drupal.org/api/function/db_query/7
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => 'archive'));
$nids = array();
foreach ($result as $row) {
$nids[] = $row->nid;
}
// Delete all the nodes at once
// http://api.drupal.org/api/function/node_delete_multiple/7
node_delete_multiple($nids);
// Loop over each of the fields defined by this module and delete
// all instances of the field, their data, and the field itself.
// http://api.drupal.org/api/function/field_delete_field/7
foreach (array_keys(_timeline_archive_installed_fields()) as $field) {
field_delete_field($field);
}
// Loop over any remaining field instances attached to the node_timeline
// content type (such as the body field) and delete them individually.
// http://api.drupal.org/api/function/field_delete_field/7
$instances = field_info_instances('node', 'archive');
foreach ($instances as $instance_name => $instance) {
field_delete_instance($instance);
}
// Delete our content type
// http://api.drupal.org/api/function/node_type_delete/7
node_type_delete('archive');
foreach (array_keys(_timeline_frequencies_installed_fields()) as $field) {
field_delete_field($field);
}
// Delete all fields from the frequencies vocabulary
// ToDo: Doesn't work, for whatever reason
$instances = field_info_instances('taxonomy_term', 'timeline_frequencies');
foreach ($instances as $instance_name => $instance) {
field_delete_instance($instance);
}
taxonomy_vocabulary_delete(variable_get('timeline_freq_vocabulary', 0));
variable_del('timeline_freq_vocabulary');
// Purge all field infromation
// http://api.drupal.org/api/function/field_purge_batch/7
field_purge_batch(1000);
}
/*
*
*/
function timeline_schema() {
/*
* The schema for record information
*/
$schema['records'] = array(
'description' => 'All our records.',
'fields' => array(
'rid' => array(
'description' => 'The primary identifier for a record.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'nid' => array(
'description' => 'The {node}.nid of the archive this image belongs to.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'fid' => array(
'description' => 'The {file_managed}.fid.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'request_time' => array(
'description' => 'The time the download was requested.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'saved_time' => array(
'description' => 'The time the image was saved on the system.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'error' => array(
'description' => 'Contains an error code. Either http or -1 in case of not connected. 200 - 206 is ok.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'indexes' => array(
'record_requested' => array('request_time'),
'record_nid' => array('nid'),
),
'unique keys' => array(
'nid_request_time' => array('nid', 'request_time'),
),
'foreign keys' => array(
'file_managed' => array(
'table' => 'file_managed',
'columns' => array('fid' => 'fid'),
),
'nid' => array(
'table' => 'node',
'columns' => array('nid' => 'nid'),
),
),
'primary key' => array('rid'),
);
/*
* The schema for archive information
*/
$schema['archives'] = array(
'description' => 'All our archive information.',
'fields' => array(
'nid' => array(
'description' => 'The {node}.nid this archive belongs to.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'url' => array(
'description' => 'The url this archive is based on.',
'type' => 'varchar',
'length' => 2083, // Taken from http://stackoverflow.com/questions/219569/best-database-field-type-for-a-url
'not null' => TRUE,
'default' => '',
),
'frequency' => array(
'description' => 'The tid of the taxonomy_term that contains the frequency for this archive.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'basetime' => array(
'description' => 'The timestamp of the basetime for this archive.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'preview_picture' => array(
'description' => 'The {file_managed}.fid to use as preview picture for this archive.',
'type' => 'int',
'default' => 0,
),
),
'indexes' => array(
'archive_frequency' => array('frequency'),
),
'foreign keys' => array(
'file_managed' => array(
'table' => 'file_managed',
'columns' => array('fid' => 'fid'),
),
'nid' => array(
'table' => 'node',
'columns' => array('nid' => 'nid'),
),
),
'primary key' => array('nid'),
);
return $schema;
}
/*
*
*/
function timeline_update_7001() {
$t = get_t();
$vocabulary = taxonomy_vocabulary_load(variable_get('timeline_freq_vocabulary', 0));
if (!$vocabulary) {
$edit = array(
'name' => t('Frequencies'),
'machine_name' => 'timeline_frequencies',
'description' => t('The available frequencies for the image collector.'),
'hierarchy' => 1,
'module' => 'timeline',
'weight' => -10,
);
$vocabulary = (object) $edit;
taxonomy_vocabulary_save($vocabulary);
variable_set('timeline_freq_vocabulary', $vocabulary->vid);
foreach (_timeline_frequencies_installed_fields() as $field) {
field_create_field($field);
}
// Create all the instances for our fields.
// http://api.drupal.org/api/function/field_create_instance/7
foreach (_timeline_frequencies_installed_instances() as $instance) {
$instance['entity_type'] = 'taxonomy_term';
$instance['bundle'] = $vocabulary->machine_name;
field_create_instance($instance);
}
foreach (_timeline_default_frequencies() as $freqency) {
$edit = array(
'name' => $t($freqency['text']),
'description' => '',
'parent' => array(0),
'vid' => $vocabulary->vid,
'timeline_frequency_seconds' => array(
'und' => array(
0 => $frequency['seconds'],
),
),
);
$term = (object) $edit;
taxonomy_term_save($term);
}
}
}
/*
* Timeline: Create Basetime Field
*/
function timeline_update_7003() {
field_create_field(array(
'field_name' => 'archive_basetime',
'label' => t('Basetime'),
'description' => 'The basetime is the time you want your pictures to be taken. It is used in low-frequency archives do determine the time of the picture. E.g. If in a daily archive the pictures should be taken midnight, morning or noon.',
'type' => 'datetime',
'required' => true,
'widget' => array(
'type' => 'date_popup',
),
));
field_create_instance(array(
'field_name' => 'archive_basetime',
'label' => t('Basetime'),
'description' => 'The basetime is the time you want your pictures to be taken. It is used in low-frequency archives do determine the time of the picture. E.g. If in a daily archive the pictures should be taken midnight, morning or noon.',
'type' => 'datetime',
'required' => true,
'widget' => array(
'type' => 'date_popup',
),
'entity_type' => 'node',
'bundle' => 'archive',
));
}
/*
* Ban archive information in own table. Instead of fields. Much nicer :)
*/
function timeline_update_7005() {
$schema = timeline_schema();
db_create_table('archives', $schema['archives']);
db_query('INSERT INTO {archives} (nid, url, frequency, basetime, preview_picture)
SELECT freq.entity_id, url.archive_url_value, freq.archive_frequency_tid, IF(ISNULL(base.archive_basetime_value), 0, unix_timestamp(base.archive_basetime_value)), pict.preview_picture_fid
FROM `field_data_archive_frequency` freq
LEFT JOIN field_data_archive_basetime base
ON base.entity_id = freq.entity_id
LEFT JOIN field_data_archive_url url
ON url.entity_id = freq.entity_id
LEFT JOIN field_data_preview_picture pict
ON pict.entity_id = freq.entity_id
');
}