/[drupal]/contributions/modules/thumb/thumb.admin.inc
ViewVC logotype

Contents of /contributions/modules/thumb/thumb.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Wed Dec 10 12:16:58 2008 UTC (11 months, 2 weeks ago) by davyvandenbremt
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +24 -19 lines
File MIME type: text/x-php
Refactored params saving to make it more extensible.
1 <?php
2 // $Id: thumb.admin.inc,v 1.4 2008/12/10 12:07:13 davyvandenbremt Exp $
3
4 /**
5 * Menu callback - Get an overview of all presets.
6 */
7 function thumb_presets_list() {
8 $header = array(t('Preset'), t('Operations'));
9
10 $rows = array();
11
12 $presets = thumb_presets();
13
14 foreach ($presets as $id => $preset) {
15 $links = array();
16 $links[] = l(t('Edit'), 'admin/build/thumb/'. $preset->pid .'/edit');
17 $links[] = l(t('Delete'), 'admin/build/thumb/'. $preset->pid .'/delete');
18 $links[] = l(t('Flush'), 'admin/build/thumb/'. $preset->pid .'/flush');
19
20 $rows[] = array(
21 $id,
22 implode('&nbsp;&nbsp;&nbsp;', $links)
23 );
24 }
25
26 if (empty($rows)) {
27 $rows[] = array(array('data' => t('No presets available.'), 'colspan' => '2', 'class' => 'message'));
28 }
29
30 return theme('table', $header, $rows);
31 }
32
33 /**
34 * Menu callback - Show preset form (add and delete).
35 *
36 * @param $preset
37 * The preset to edit (if in edit mode).
38 */
39 function thumb_presets_form(&$formstate, $preset = NULL) {
40 $form = array();
41
42 if ($preset) {
43 $form['pid'] = array(
44 '#type' => 'hidden',
45 '#value' => $preset->pid
46 );
47 }
48
49 /**
50 * General preset info.
51 */
52 $form['info'] = array(
53 '#type' => 'fieldset',
54 '#title' => t('Info'),
55 '#collapsible' => TRUE,
56 );
57 $form['info']['identifier'] = array(
58 '#type' => 'textfield',
59 '#title' => t('Identifier'),
60 '#description' => t('The title you enter here appears on the page.'),
61 '#size' => 32,
62 '#maxlength' => 32,
63 '#required' => !$preset ? TRUE : FALSE,
64 '#element_validate' => array('thumb_element_identifier_validate'),
65 '#default_value' => isset($preset) ? $preset->identifier : '',
66 '#disabled' => $preset ? TRUE : FALSE,
67 );
68
69 $form['info']['description'] = array(
70 '#type' => 'textarea',
71 '#title' => t('Description'),
72 '#description' => t('The text you enter here appears on the page.'),
73 '#cols' => 60,
74 '#rows' => 5,
75 '#default_value' => isset($preset) ? $preset->description : '',
76 );
77
78 /**
79 * Preset settings.
80 */
81
82 // size
83 $form['params']['size'] = array(
84 '#type' => 'fieldset',
85 '#title' => t('Size'),
86 '#collapsible' => TRUE,
87 '#collapsed' => TRUE,
88 );
89 $form['params']['size']['w'] = array(
90 '#type' => 'textfield',
91 '#title' => t('Maximum width'),
92 '#description' => t('Maximum width of output thumbnail in pixels.'),
93 '#size' => 4,
94 '#maxlength' => 4,
95 '#element_validate' => array('thumb_element_numeric_validate'),
96 '#default_value' => isset($preset) && isset($preset->data['w']) ? $preset->data['w'] : '',
97 );
98 $form['params']['size']['h'] = array(
99 '#type' => 'textfield',
100 '#title' => t('Maximum height'),
101 '#description' => t('Maximum width of output thumbnail in pixels.'),
102 '#size' => 4,
103 '#maxlength' => 4,
104 '#element_validate' => array('thumb_element_numeric_validate'),
105 '#default_value' => isset($preset) && isset($preset->data['h']) ? $preset->data['h'] : '',
106 );
107 $form['params']['size']['iar'] = array(
108 '#type' => 'checkbox',
109 '#title' => t('Ignore Aspect Ratio'),
110 '#description' => t('Disable proportional resizing and stretch image to fit "Maxium height" & "Maximum width" (which must both be set). Overrides Force Aspect Ratio.'),
111 '#default_value' => isset($preset) && isset($preset->data['iar']) ? $preset->data['iar'] : 0,
112 );
113 $form['params']['size']['far'] = array(
114 '#type' => 'select',
115 '#title' => t('Force Aspect Ratio'),
116 '#description' => t('Image will be created at size specified by "Maximum width" and "Maximum height" (which must both be set). Use the appropriate direction if the image is landscape or portrait.'),
117 '#options' => array(
118 '' => '<'. t('ignore') . '>',
119 'L' => t('Left'),
120 'R' => t('Right'),
121 'T' => t('Top'),
122 'B' => t('Bottom'),
123 'C' => t('Center'),
124 'BL' => t('Bottom left'),
125 'BR' => t('Bottom right'),
126 'TL' => t('Top left'),
127 'TR' => t('Top right')
128 ),
129 '#default_value' => isset($preset) && isset($preset->data['far']) ? $preset->data['far'] : '',
130 );
131
132 // output format and quality
133 $form['params']['output'] = array(
134 '#type' => 'fieldset',
135 '#title' => t('Output format and quality'),
136 '#collapsible' => TRUE,
137 '#collapsed' => TRUE,
138 '#access' => user_access('administer thumb.admin'),
139 );
140
141 $form['params']['output']['f'] = array(
142 '#type' => 'select',
143 '#title' => t('Format'),
144 '#description' => t('Output image format.'),
145 '#options' => array(-1 => t('Default'), 'jpeg' => 'jpeg', 'png' => 'png', 'gif' => 'gif'),
146 '#default_value' => isset($preset) && isset($preset->data['f']) ? $preset->data['f'] : -1,
147 );
148 $form['params']['output']['q'] = array(
149 '#type' => 'textfield',
150 '#title' => t('Quality'),
151 '#description' => t('JPEG compression (1=worst, 95=best, 75=default)'),
152 '#size' => 2,
153 '#maxlength' => 2,
154 '#element_validate' => array('thumb_element_numeric_validate'),
155 '#default_value' => isset($preset) && isset($preset->data['q']) ? $preset->data['q'] : '',
156 );
157
158 // color
159 $form['params']['color'] = array(
160 '#type' => 'fieldset',
161 '#title' => t('Colors'),
162 '#collapsible' => TRUE,
163 '#collapsed' => TRUE,
164 );
165
166 if (module_exists('color') && module_exists('colorpicker')) {
167 $form['params']['color']['colorpicker'] = array(
168 '#type' => 'colorpicker',
169 '#description' => t('Use this colorpicker to easily select color codes for the fields below.'),
170 );
171 $form['params']['color']['bg'] = array(
172 '#type' => 'colorpicker_textfield',
173 '#colorpicker' => 'colorpicker',
174 );
175 }
176 else {
177 $form['params']['color']['bg'] = array(
178 '#type' => 'textfield',
179 );
180 }
181
182 $form['params']['color']['bg']['#title'] = t('Background color');
183 $form['params']['color']['bg']['#default_value'] = isset($preset) && isset($preset->data['bg']) ? $preset->data['bg'] : '#ffffff';
184 $form['params']['color']['bg']['#size'] = 8;
185 $form['params']['color']['bg']['#description'] = t('Specify hex color.');
186
187 /**
188 * Buttons.
189 */
190 $form['submit'] = array(
191 '#type' => 'submit',
192 '#value' => t('Save'),
193 );
194
195 return $form;
196 }
197
198 /**
199 * Submit handler for the preset form.
200 */
201 function thumb_presets_form_submit($form, &$form_state) {
202 $params = array();
203
204 // Merging all params
205 $element_groups = element_children($form['params']);
206
207 $elements = array();
208 foreach ($element_groups as $group) {
209 $elements = array_merge($elements, element_children($form['params'][$group]));
210 }
211
212 // Gathering all settings in $params array.
213 foreach ($elements as $element) {
214 $params[$element] = $form_state['values'][$element];
215 }
216
217 // Serializing the array for storage.
218 $params = serialize($params);
219
220 // No preset id set; we're inserting a new one.
221 if (!isset($form_state['values']['pid'])) {
222 $sql = "INSERT INTO {thumb_preset} (identifier, description, data) VALUES ('%s', '%s', '%s')";
223 db_query($sql, $form_state['values']['identifier'], $form_state['values']['description'], $params);
224 drupal_set_message(t('Preset %identifier was created.', array('%identifier' => $form_state['values']['identifier'])));
225 }
226 // Preset id set; we're editing an existing one.
227 else {
228 $sql = "UPDATE {thumb_preset} SET description = '%s', data = '%s' WHERE pid = %d";
229 db_query($sql, $form_state['values']['description'], $params, $form_state['values']['pid']);
230 drupal_set_message(t('Preset %identifier was updated.', array('%identifier' => $form_state['values']['identifier'])));
231 }
232
233 // Redirecting to preset overview.
234 $form_state['redirect'] = 'admin/build/thumb';
235 }
236
237 /**
238 * Element validation handler - Validate if an element's value is a valid identifier (alphanumeric, _ and -).
239 */
240 function thumb_element_identifier_validate($element, &$form_state) {
241 // Check for duplicates.
242 $presets = thumb_presets();
243
244 if (in_array($element['#value'], array_keys($presets))) {
245 form_set_error($element['#name'], t('The identifier you have chosen is already in use.'));
246 }
247
248 // Check for illegal characters.
249 if (preg_match('/[^0-9a-zA-Z_\-]/', $element['#value'])) {
250 form_set_error($element['#name'], t('Please only use alphanumic characters, underscores (_), and hyphens (-) for identifiers.'));
251 }
252 }
253
254 /**
255 * Element validation handler - Validate if an element's value is numeric.
256 */
257 function thumb_element_numeric_validate($element, &$form_state) {
258 if (!empty($element['#value']) && !is_numeric($element['#value'])) {
259 form_set_error($element['#name'], t('Please specify a numeric value for !field', array('!field' => $element['#title'])));
260 }
261 }
262
263 /**
264 * Menu callback - Show preset delete confirmation form.
265 *
266 * @param $preset
267 * The preset to delete.
268 */
269 function thumb_delete_confirm_form(&$form_state, $preset) {
270 $form['pid'] = array(
271 '#type' => 'value',
272 '#value' => $preset->pid,
273 );
274 $form['identifier'] = array(
275 '#type' => 'value',
276 '#value' => $preset->identifier,
277 );
278
279 return confirm_form($form,
280 t('Are you sure you want to delete %identifier?', array('%identifier' => $preset->identifier)),
281 isset($_GET['destination']) ? $_GET['destination'] : 'admin/build/thumb',
282 t('This action cannot be undone.'),
283 t('Delete'),
284 t('Cancel')
285 );
286 }
287
288 /**
289 * Submit handler for the preset delete form.
290 */
291 function thumb_delete_confirm_form_submit($form, &$form_state) {
292 if ($form_state['values']['confirm']) {
293 thumb_preset_flush($form_state['values']['identifier']);
294 db_query("DELETE FROM {thumb_preset} WHERE pid = %d", $form_state['values']['pid']);
295 }
296
297 $form_state['redirect'] = 'admin/build/thumb';
298 }
299
300 /**
301 * Menu callback - Show preset flush confirmation form.
302 *
303 * @param $preset
304 * The preset to flush thumbnails for.
305 */
306 function thumb_flush_confirm_form(&$form_state, $preset) {
307 $form['identifier'] = array(
308 '#type' => 'value',
309 '#value' => $preset->identifier,
310 );
311
312 return confirm_form($form,
313 t('Are you sure you want to flush %identifier?', array('%identifier' => $preset->identifier)),
314 isset($_GET['destination']) ? $_GET['destination'] : 'admin/build/thumb',
315 t('This action cannot be undone.'),
316 t('Flush'),
317 t('Cancel')
318 );
319 }
320
321 /**
322 * Submit handler for the flush confirmation form.
323 */
324 function thumb_flush_confirm_form_submit($form, &$form_state) {
325 if ($form_state['values']['confirm']) {
326 thumb_preset_flush($form_state['values']['identifier']);
327 }
328
329 $form_state['redirect'] = 'admin/build/thumb';
330 }
331
332 /**
333 * Menu callback - Flush all thumbnails generated with a certain preset.
334 *
335 * @param $presetname
336 * The preset to flush thumbnails for.
337 */
338 function thumb_preset_flush($presetname) {
339 $presetdir = realpath(file_directory_path() .'/thumb/'. $presetname);
340 if (is_dir($presetdir)) {
341 _thumb_recursive_delete($presetdir);
342 }
343 }
344
345 /**
346 * Recursively delete images under a certain path.
347 *
348 * @param $path
349 * The path to delete images from.
350 */
351 function _thumb_recursive_delete($path) {
352 if (is_file($path) || is_link($path)) {
353 unlink($path);
354 }
355 elseif (is_dir($path)) {
356 $d = dir($path);
357 while (($entry = $d->read()) !== FALSE) {
358 if ($entry == '.' || $entry == '..') continue;
359 $entry_path = $path .'/'. $entry;
360 _thumb_recursive_delete($entry_path);
361 }
362 $d->close();
363 rmdir($path);
364 }
365 else {
366 watchdog('thumb', 'Unknown file type(%path) stat: %stat ', array('%path' => $path, '%stat' => print_r(stat($path), 1)), WATCHDOG_ERROR);
367 }
368 }

  ViewVC Help
Powered by ViewVC 1.1.2