forked from DBCDK/ting_covers
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathting_covers.module
More file actions
174 lines (152 loc) · 4.79 KB
/
ting_covers.module
File metadata and controls
174 lines (152 loc) · 4.79 KB
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
<?php
/**
* @file ting_covers.module
* Provide functionality and page callbacks for retrieving covers for Ting objects and collections
*/
define('SERVICE_ADDI', 0);
define('SERVICE_MOREINFO', 1);
define('TING_COVERS_CACHE_LIFETIME', 86400);
// Load Field module hooks.
module_load_include('inc', 'ting_covers', 'ting_covers.field');
/**
* Implementation of hook_menu().
*/
function ting_covers_menu() {
$items = array();
$items['ting/covers'] = array(
'title' => 'Retreives cover for Ting objects',
'page callback' => 'ting_covers_objects',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
'file' => 'ting_covers.pages.inc',
);
$items['admin/config/ting/covers'] = array(
'title' => 'Covers',
'description' => 'Configure how covers are handled.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_covers_admin_settings_form'),
'access arguments' => array('administer site configuration'),
'file' => 'ting_covers.admin.inc',
);
$items['admin/config/ting/covers/setttings'] = array(
'title' => 'Settings',
'description' => 'Configure how covers are handled.',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/config/ting/covers/addi'] = array(
'title' => 'ADDI service',
'description' => 'Configure integration with the ADDI service.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_covers_admin_addi_settings_form'),
'access arguments' => array('administer site configuration'),
'file' => 'ting_covers.admin.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implementation of hook_theme().
*/
function ting_covers_theme() {
return array(
'ting_object_cover' => array(
'render element' => 'elements',
'file' => 'ting_covers.theme.inc',
),
);
}
/**
* Implementation of hook_cron().
*/
function ting_covers_cron() {
_ting_covers_delete_old_files($_SERVER['REQUEST_TIME'] - variable_get('ting_covers_cache_lifetime', TING_COVERS_CACHE_LIFETIME));
}
/**
* Implementation of hook_cache_clear().
*/
function ting_covers_flush_caches() {
_ting_covers_delete_old_files($_SERVER['REQUEST_TIME'] - variable_get('ting_covers_cache_lifetime', TING_COVERS_CACHE_LIFETIME));
}
/**
* Implements hook_ding_install_tasks().
*/
function ting_covers_ding_install_tasks() {
module_load_include('inc', 'ting_covers', 'ting_covers.admin');
return array(
'ting_covers_admin_addi_settings_form' => array(
'display_name' => st('ADDI service settings'),
'type' => 'form',
'file' => drupal_get_path('module', 'ting_covers') . '/ting_covers.admin.inc',
),
);
}
/**
* Template preprocessor.
*/
function ting_covers_preprocess_ting_object(&$variables) {
/*
* Add the image style as a class, allowing templates to react on the size.
*/
if (isset($variables['elements']['ting_cover'][0])) {
$variables['classes_array'][] = drupal_html_class('imagestyle-' . $variables['elements']['ting_cover'][0]['#image_style']);
}
}
/**
* Delete ting covers files.
*
* @param int $time Timestamp where older files will be deleted.
* Obmit or set to NULL to delete all files.
*/
function _ting_covers_delete_old_files($time = NULL) {
//Collect potential locations of cover folders
$files_dir = file_default_scheme() . '://';
$image_dirs = array($files_dir);
$styles_dir = $files_dir . 'styles';
foreach (scandir($styles_dir) as $style_dir) {
$style_dir = $styles_dir . DIRECTORY_SEPARATOR . $style_dir . DIRECTORY_SEPARATOR . 'public';
if (is_dir($style_dir)) {
$image_dirs[] = $style_dir;
}
}
//Collect cover files
$cover_files = array();
foreach ($image_dirs as $uri) {
$ting_covers_dir = $uri . DIRECTORY_SEPARATOR . 'ting' . DIRECTORY_SEPARATOR . 'covers';
if (is_dir($ting_covers_dir)) {
$cover_files = array_merge($cover_files, _ting_covers_get_files($ting_covers_dir));
}
}
//Delete obsolete files
foreach ($cover_files as $file) {
if (!$time || (filemtime($file) < $time)) {
file_unmanaged_delete($file);
}
}
}
/**
* Retrieve all files under a path recursively
* @param string $files_path Path or URI
* @return array An array of file paths or URIs
*/
function _ting_covers_get_files($files_path) {
$files = array();
foreach (scandir($files_path) as $path) {
$file_path = $files_path . DIRECTORY_SEPARATOR . $path;
if (is_dir($file_path)) {
if (!in_array($path, array('.', '..'))) {
$files = array_merge($files, _ting_covers_get_files($file_path));
}
}
else {
$files[] = $file_path;
}
}
return $files;
}
/**
* Return the path to the cover of the object.
*/
function ting_covers_object_path($object_id) {
return file_default_scheme() . '://ting/covers/object/' . md5($object_id) . '.jpg';
}