-
Notifications
You must be signed in to change notification settings - Fork 6
/
bpi.images.inc
212 lines (183 loc) · 4.55 KB
/
bpi.images.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
<?php
/**
* @file
* Images syndication routine here.
*/
/**
* Entry point for image syndication.
*
* @param string $type
* Whether it's an ajax call.
*
* @return array
* A set of ajax commands.
*/
function bpi_syndicate_images($type = 'ajax') {
$assets = isset($_SESSION['bpi']['assets']) ? $_SESSION['bpi']['assets'] : array();
if (empty($assets)) {
return;
}
$form = drupal_get_form('bpi_assets_form', $assets);
$commands = array();
$commands[] = ajax_command_ding_popup(
'bpi-syndicate-images',
t('Syndicate images'),
drupal_render($form)
);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Image syndication form.
*
* @param array $form
* Form structure.
* @param array $form_state
* Form state values.
* @param array $assets
* Array of assets to be downloaded.
*
* @return array
* Form structure.
*
* @ingroup forms
*/
function bpi_assets_form($form = array(), $form_state, $assets = array()) {
if (!empty($assets)) {
$form['bpi_assets'] = array(
'#type' => 'fieldset',
'#title' => t('Available images'),
);
foreach ($assets as $asset => $url) {
$form['bpi_assets'][$asset] = array(
'#type' => 'checkbox',
'#default_value' => 1,
'#prefix' => '<div class="bpi-syndicate-asset"><img src="' . $url . '" width="100" height="100" />',
'#suffix' => '</div>',
);
}
}
$form['bpi_assets_syndicate'] = array(
'#type' => 'submit',
'#value' => t('Import'),
'#ajax' => array(
'callback' => 'bpi_asset_action_ajax_callback',
),
);
return $form;
}
/**
* Submit handler for images form.
*
* @see bpi_assets_form()
*
* @param array $form
* Form structure.
* @param array $form_state
* Form state values.
*
* @ingroup forms
*/
function bpi_assets_form_submit($form, &$form_state) {
$input = array_keys($form_state['input']);
$assets = array();
// Seek the assets.
foreach ($input as $value) {
if (preg_match('/^asset(\d+)*/', $value) && !empty($form_state['input'][$value])) {
if (isset($_SESSION['bpi']['assets'][$value])) {
$assets[] = $_SESSION['bpi']['assets'][$value];
}
}
}
unset($_SESSION['bpi']['assets']);
foreach ($assets as $path) {
$filename = basename($path);
$managed_file = bpi_fetch_image('public://bpi/' . $filename, $path);
$status = TRUE;
if (!is_object($managed_file)) {
$status = FALSE;
}
}
if ($status) {
drupal_set_message(t('Images downloaded successfully.'));
}
else {
drupal_set_message(t('Not all images were fetched successfully, check reports for more info.'));
}
}
/**
* Images form download action ajax callback.
*
* @see bpi_assets_form()
*
* @param array $form
* Form structure.
* @param array $form_state
* Form state values.
* @return array
* Set of ajax commands.
*
* @ingroup forms
*/
function bpi_asset_action_ajax_callback($form, &$form_state) {
$html = theme('status_messages');
$commands = array();
$commands[] = ajax_command_ding_popup(
'bpi-syndicate-images',
t('Syndicate images'),
$html
);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Images form cancel action ajax callback.
*
* @see bpi_assets_form()
*
* @param array $form
* Form structure.
* @param array $form_state
* Form state values.
* @return array
* Set of ajax commands.
*
* @ingroup forms
*/
function bpi_asset_cancel_ajax_callback($form, &$form_state) {
$commands = array();
$commands[] = ajax_command_ding_popup_close(
'bpi-syndicate-images'
);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Download images to local instance.
*
* @param string $filename
* Filename, with extension. An URI is optional.
* @param string $image_url
* File URL form where to download.
* @return object
* File object or FALSE on failure.
*/
function bpi_fetch_image($filename, $image_url) {
$result = drupal_http_request($image_url);
if ($result->code != 200) {
watchdog(
'bpi',
'Failed to fetch image %url, with HTTP status %status', array('%url' => $image_url, '%status' => $result->code),
WATCHDOG_ERROR
);
return FALSE;
}
$directory = dirname($filename);
if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
watchdog(
'bpi',
t('Failed to create directory: %directory'),
array('%directory' => $directory),
WATCHDOG_ERROR
);
return FALSE;
}
return file_save_data($result->data, $filename, FILE_EXISTS_REPLACE);
}