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

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

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


Revision 1.9 - (show annotations) (download) (as text)
Tue Oct 13 08:38:52 2009 UTC (6 weeks, 3 days ago) by joachim
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +2 -2 lines
File MIME type: text/x-php
#602740 by Vin7: Fixed theme function to take $form by value.
1 <?php
2 // $Id: image.admin.inc,v 1.8 2009/09/02 12:57:48 joachim Exp $
3
4 /**
5 * Menu callback; Form builder function for image settings.
6 */
7 function image_admin_settings() {
8 $form['image_updated'] = array(
9 '#type' => 'hidden',
10 '#value' => variable_get('image_updated', time()),
11 );
12
13 $form['files'] = array(
14 '#type' => 'fieldset',
15 '#title' => t('Image file settings')
16 );
17 $form['files']['image_default_path'] = array(
18 '#type' => 'textfield',
19 '#title' => t('Default image path'),
20 '#default_value' => variable_get('image_default_path', 'images'),
21 '#description' => t('Subdirectory in the directory %dir where pictures will be stored. Do not include trailing slash.', array('%dir' => file_directory_path())),
22 );
23
24 $form['files']['image_max_upload_size'] = array(
25 '#type' => 'textfield',
26 '#title' => t('Maximum upload size'),
27 '#default_value' => variable_get('image_max_upload_size', 800),
28 '#field_suffix' => t('KB'),
29 '#size' => 12,
30 '#description' => t('Maximum file size for image uploads. When a maximum image dimensions is specified for original images the size is checked after resizing.'),
31 );
32
33 $form['image_sizes'] = array(
34 '#type' => 'fieldset',
35 '#title' => t('Image sizes'),
36 '#tree' => TRUE,
37 '#theme' => 'image_settings_sizes_form',
38 '#description' => '<p>'. t("The <em>Scale image</em> operation resizes images so that they fit with in the given dimensions. If only one dimension is specified the other dimension will be computed based on the image's aspect ratio. The <em>Scale and crop image</em> operation resizes images to be exactly the given dimensions. If only one dimension is specified the image will not be cropped, making this is equivalent to <em>Scale image</em>.") .'</p>'
39 .'<p>'. t("Note: 'Original' dimensions will only be used to resize images when they are first uploaded. Existing originals will not be modified.") .'</p>',
40 );
41
42 $link_options = array(
43 IMAGE_LINK_HIDDEN => t('Hidden'),
44 IMAGE_LINK_SHOWN => t('Same window'),
45 IMAGE_LINK_NEW => t('New window'),
46 );
47
48 $sizes = image_get_sizes();
49
50 // Add some empty rows for user defined sizes.
51 $num_sizes = count($sizes);
52 for ($i = $num_sizes; $i < ($num_sizes + 3); $i++) {
53 $sizes['new'. $i] = array(
54 'label' => '',
55 'operation' => 'scale',
56 'width' => '',
57 'height' => '',
58 'link' => IMAGE_LINK_SHOWN,
59 'new' => TRUE,
60 );
61 }
62
63 foreach ($sizes as $key => $size) {
64 $form['image_sizes'][$key]['label'] = array(
65 '#type' => 'textfield',
66 '#default_value' => $size['label'],
67 '#size' => 25,
68 '#maxlength' => 32,
69 );
70
71 // For required sizes, set the value and disable the field.
72 if (_image_is_required_size($key)) {
73 $form['image_sizes'][$key]['label']['#disabled'] = TRUE;
74 $form['image_sizes'][$key]['label']['#value'] = $size['label'];
75 $form['image_sizes'][$key]['label']['#required'] = TRUE;
76 }
77 $form['image_sizes'][$key]['operation'] = array(
78 '#type' => 'select',
79 '#default_value' => $size['operation'],
80 '#options' => array('scale' => t('Scale image'), 'scale_crop' => t('Scale and crop image')),
81 );
82 $form['image_sizes'][$key]['width'] = array(
83 '#type' => 'textfield',
84 '#default_value' => $size['width'],
85 '#size' => 5,
86 '#maxlength' => 5,
87 );
88 $form['image_sizes'][$key]['height'] = array(
89 '#type' => 'textfield',
90 '#default_value' => $size['height'],
91 '#size' => 5,
92 '#maxlength' => 5,
93 );
94 $form['image_sizes'][$key]['link'] = array(
95 '#type' => 'select',
96 '#default_value' => $size['link'],
97 '#options' => $link_options,
98 );
99 }
100
101 // Make changes to the settings before passing them off to
102 // system_settings_form_submit().
103 $form['#submit'][] = 'image_admin_settings_submit';
104
105 return system_settings_form($form);
106 }
107
108 /**
109 * Form validation handler for image admin settings form.
110 */
111 function image_admin_settings_validate($form, &$form_state) {
112 // Check that the sizes provided have the required amount of information.
113 foreach (element_children($form['image_sizes']) as $key) {
114 // If there's a label they must provide at either a height or width.
115 if ($key != IMAGE_ORIGINAL && !empty($form['image_sizes'][$key]['label']['#value'])) {
116 if (empty($form['image_sizes'][$key]['width']['#value']) && empty($form['image_sizes'][$key]['height']['#value'])) {
117 form_set_error("image_sizes][$key][width", t('You must specify width, height or both dimensions.'));
118 }
119 }
120 }
121
122 // Try to create directories and warn the user of errors.
123 $image_default_path = $form_state['values']['image_default_path'];
124 $image_path = file_create_path(file_directory_path() . '/' . $image_default_path);
125 $temp_path = $image_path .'/temp';
126
127 if (!file_check_directory($image_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS, 'image_default_path')) {
128 form_set_error('image_default_path', t('You have specified an invalid directory.'));
129 }
130 if (!file_check_directory($temp_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS, 'image_default_path')) {
131 form_set_error('image_default_path', t('You have specified an invalid directory.'));
132 }
133 }
134
135 /**
136 * Form submit handler for image admin settings form.
137 */
138 function image_admin_settings_submit($form, &$form_state) {
139 // Ensure that 'image_default_path' variable contains no trailing slash.
140 $form_state['values']['image_default_path'] = rtrim($form_state['values']['image_default_path'], '/');
141
142 // Remove deleted sizes, and use the label as indexes for new sizes.
143 $old_sizes = image_get_sizes();
144 // If the size's operation, or dimensions change we need to rebuild.
145 $rebuild = FALSE;
146
147 foreach ($form_state['values']['image_sizes'] as $key => $size) {
148 // Changed to the original setting only affect new images and they
149 // shouldn't be able to add or remove it.
150 if ($key == IMAGE_ORIGINAL) {
151 continue;
152 }
153
154 // Remove sizes without labels.
155 if (empty($size['label'])) {
156 unset($form_state['values']['image_sizes'][$key]);
157 }
158
159 // Check if only one is set, indicating an addition or removal.
160 if (isset($form_state['values']['image_sizes'][$key]) ^ isset($old_sizes[$key])) {
161 $rebuild |= TRUE;
162
163 // When adding a new size, we need to assign a key.
164 if (isset($form_state['values']['image_sizes'][$key])) {
165 unset($form_state['values']['image_sizes'][$key]);
166 $new_key = drupal_strtolower(drupal_substr($size['label'], 0, 32));
167 $form_state['values']['image_sizes'][$new_key] = $size;
168 }
169 }
170 // Check for changes.
171 else if (isset($form_state['values']['image_sizes'][$key]) && isset($old_sizes[$key])) {
172 // Did the operation, height or width change?
173 foreach (array('operation', 'height', 'width') as $field) {
174 $rebuild |= ($form_state['values']['image_sizes'][$key][$field] != $old_sizes[$key][$field]);
175 }
176 }
177 }
178
179 // If we've changed anything update the image_update variable so the
180 // derivative images are rebuilt.
181 if ($rebuild) {
182 drupal_set_message(t('Changes to the images sizes mean that the derivative images will need to be regenerated.'));
183 $form_state['values']['image_updated'] = time();
184 }
185 }
186
187 function theme_image_settings_sizes_form($form) {
188 $header = array(t('Label'), t('Operation'), t('Width'), t('Height'), t('Link'));
189 foreach (element_children($form) as $key) {
190 $row = array();
191 $row[] = drupal_render($form[$key]['label']);
192 $row[] = drupal_render($form[$key]['operation']);
193 $row[] = drupal_render($form[$key]['width']);
194 $row[] = drupal_render($form[$key]['height']);
195 $row[] = drupal_render($form[$key]['link']);
196 $rows[] = $row;
197 }
198 $output = theme('table', $header, $rows);
199 $output .= drupal_render($form);
200
201 return $output;
202 }
203

  ViewVC Help
Powered by ViewVC 1.1.2