1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Images plugin.
|
6
|
*/
|
7
|
class sweaver_plugin_images extends sweaver_plugin {
|
8
|
|
9
|
/**
|
10
|
* Menu registry.
|
11
|
*/
|
12
|
public function sweaver_menu(&$weight, $page_arguments, $base) {
|
13
|
|
14
|
$items = array();
|
15
|
|
16
|
$items['admin/config/user-interface/sweaver/images'] = $base + array(
|
17
|
'title' => 'Images',
|
18
|
'page arguments' => array($page_arguments),
|
19
|
'type' => MENU_LOCAL_TASK,
|
20
|
'weight' => $weight++,
|
21
|
);
|
22
|
$page_arguments['callback_method'] = 'sweaver_images_backend_form';
|
23
|
$items['admin/config/user-interface/sweaver/images/add'] = $base + array(
|
24
|
'title' => 'Add new image',
|
25
|
'page arguments' => array($page_arguments),
|
26
|
'type' => MENU_CALLBACK,
|
27
|
);
|
28
|
$page_arguments['callback_method'] = 'sweaver_images_backend_form';
|
29
|
$items['admin/config/user-interface/sweaver/images/edit'] = $base + array(
|
30
|
'title' => 'Edit image',
|
31
|
'page arguments' => array($page_arguments),
|
32
|
'type' => MENU_CALLBACK,
|
33
|
);
|
34
|
$page_arguments['callback_method'] = 'sweaver_images_confirm_form_delete';
|
35
|
$items['admin/config/user-interface/sweaver/images/delete'] = $base + array(
|
36
|
'title' => 'Add new image',
|
37
|
'page arguments' => array($page_arguments),
|
38
|
'type' => MENU_CALLBACK,
|
39
|
);
|
40
|
|
41
|
return $items;
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* Frontend form: add image form.
|
46
|
*/
|
47
|
public function sweaver_form() {
|
48
|
$form = array();
|
49
|
|
50
|
if (variable_get('sweaver_plugin_images_tab', TRUE)) {
|
51
|
$form['image_form']['image_description'] = array(
|
52
|
'#type' => 'textfield',
|
53
|
'#title' => t('Description'),
|
54
|
'#description' => t('Description of the image used in the select box for files.')
|
55
|
);
|
56
|
$form['image_form']['image_image'] = array(
|
57
|
'#type' => 'file',
|
58
|
'#title' => t('Image'),
|
59
|
'#size' => '40',
|
60
|
'#description' => t('Upload images which you can use as background images. Extensions are limited to jpg, jpeg, png and gif.<br />Note: there is no scaling on the images on upload.'),
|
61
|
);
|
62
|
$form['image_form']['image_submit'] = array(
|
63
|
'#type' => 'submit',
|
64
|
'#value' => t('Save image'),
|
65
|
);
|
66
|
}
|
67
|
|
68
|
return $form;
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* Frontend form submit handler.
|
73
|
*/
|
74
|
public function sweaver_form_submit($form, &$form_state) {
|
75
|
|
76
|
if ($form_state['clicked_button']['#value'] == t('Save image')) {
|
77
|
|
78
|
$validators = array(
|
79
|
'file_validate_is_image' => array(),
|
80
|
);
|
81
|
|
82
|
$sweaver_directory = 'public://sweaver';
|
83
|
file_prepare_directory($sweaver_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
|
84
|
$image = file_save_upload('image_image', $validators, 'public://sweaver');
|
85
|
|
86
|
if ($image && !empty($form_state['values']['image_description'])) {
|
87
|
$image->description = $form_state['values']['image_description'];
|
88
|
$image_parts = explode('.', $image->uri);
|
89
|
$extension = array_pop($image_parts);
|
90
|
|
91
|
$new_image_name = 'public://sweaver/sweaver_image_' . $image->fid . '.' . $extension;
|
92
|
file_unmanaged_move($image->uri, $new_image_name);
|
93
|
|
94
|
// Save permanent.
|
95
|
$image->uri = $new_image_name;
|
96
|
$image->status = FILE_STATUS_PERMANENT;
|
97
|
file_save($image);
|
98
|
|
99
|
drupal_write_record('sweaver_image', $image);
|
100
|
sweaver_session(t('Image @image has been uploaded.', array('@image' => $image->description)));
|
101
|
}
|
102
|
else {
|
103
|
|
104
|
// Reset error messages.
|
105
|
$errors = form_get_errors();
|
106
|
if (!empty($errors)) {
|
107
|
// Reset messages error.
|
108
|
unset($_SESSION['messages']['error']);
|
109
|
}
|
110
|
|
111
|
// Messages for Sweaver.
|
112
|
if (empty($form_state['values']['image_description'])) {
|
113
|
sweaver_session(t('You need to enter a description for the image.'));
|
114
|
}
|
115
|
if (!empty($errors)) {
|
116
|
sweaver_session(t('The image is invalid. Only JPG, PNG and GIF images are allowed.'));
|
117
|
}
|
118
|
elseif (!$image) {
|
119
|
sweaver_session(t('The image is empty.'));
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* Images handler.
|
127
|
*/
|
128
|
public function sweaver_images_handler(&$images) {
|
129
|
$styles = array();
|
130
|
if (variable_get('sweaver_plugin_images_styles', FALSE) && function_exists('image_styles')) {
|
131
|
$styles = image_styles();
|
132
|
}
|
133
|
|
134
|
$query = 'SELECT ti.fid, ti.description, f.uri FROM {sweaver_image} ti
|
135
|
INNER JOIN {file_managed} f on f.fid = ti.fid order by description ASC';
|
136
|
$images_result = db_query($query)->fetchAll();
|
137
|
global $base_root;
|
138
|
foreach ($images_result as $image) {
|
139
|
$images[str_replace($base_root, '', file_create_url($image->uri))] = $image->description;
|
140
|
|
141
|
// Styles presets.
|
142
|
foreach ($styles as $preset_key => $preset) {
|
143
|
$images[image_style_url($preset_key, $image->uri)] = $image->description .' ('. $preset['name'] .')';
|
144
|
}
|
145
|
}
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* Menu callback.
|
150
|
*/
|
151
|
public function sweaver_menu_callback() {
|
152
|
$form = array();
|
153
|
|
154
|
$form['sweaver_plugin_images_tab'] = array(
|
155
|
'#type' => 'checkbox',
|
156
|
'#title' => t('Editor tab'),
|
157
|
'#description' => t('Toggle this checkbox if you want to enable the images tab on the editor.'),
|
158
|
'#default_value' => variable_get('sweaver_plugin_images_tab', TRUE),
|
159
|
);
|
160
|
|
161
|
// Use styles on images ?
|
162
|
if (function_exists('image_styles')) {
|
163
|
$form['sweaver_plugin_images_styles'] = array(
|
164
|
'#type' => 'checkbox',
|
165
|
'#title' => t('Allow to see styled version of images'),
|
166
|
'#description' => t('Toggle this checkbox if you want to create styled versions of the images uploaded by the Image plugin.'),
|
167
|
'#default_value' => variable_get('sweaver_plugin_images_styles', FALSE),
|
168
|
);
|
169
|
}
|
170
|
else {
|
171
|
$form['style_disabled'] = array(
|
172
|
'#markup' => '<p>' . t('If you enable the image module, all images can be made available with all style variants.') . '</p>',
|
173
|
);
|
174
|
$form['sweaver_plugin_images_styles'] = array(
|
175
|
'#type' => 'value',
|
176
|
'#value' => 0,
|
177
|
);
|
178
|
}
|
179
|
|
180
|
$form['sweaver_plugin_handle_images'] = array(
|
181
|
'#type' => 'hidden',
|
182
|
'#value' => variable_get('sweaver_plugin_handle_images', 'sweaver_plugin_images'),
|
183
|
);
|
184
|
|
185
|
$form = system_settings_form($form);
|
186
|
|
187
|
// Images list
|
188
|
$rows = array();
|
189
|
$output = '<p>'. l(t('Add new image'), 'admin/config/user-interface/sweaver/images/add') .'</p>';
|
190
|
|
191
|
$query = 'SELECT ti.fid, ti.description, f.uri FROM {sweaver_image} ti
|
192
|
INNER JOIN {file_managed} f on f.fid = ti.fid order by description ASC';
|
193
|
$result = db_query($query)->fetchAll();
|
194
|
foreach ($result as $image) {
|
195
|
$row = array();
|
196
|
$row[] = check_plain($image->description);
|
197
|
$operations = l(t('View'), file_create_url($image->uri)) . ' - ';
|
198
|
$operations .= l(t('Edit'), 'admin/config/user-interface/sweaver/images/edit/' . $image->fid, array('alias' => TRUE)) . ' - ';
|
199
|
$operations .= l(t('Delete'), 'admin/config/user-interface/sweaver/images/delete/' . $image->fid, array('alias' => TRUE));
|
200
|
$row[] = $operations;
|
201
|
$rows[] = $row;
|
202
|
}
|
203
|
|
204
|
if (!empty($rows)) {
|
205
|
|
206
|
// Output table.
|
207
|
$header = array(
|
208
|
t('Image'),
|
209
|
t('Operations'),
|
210
|
);
|
211
|
$variables = array(
|
212
|
'header' => $header,
|
213
|
'rows' => $rows,
|
214
|
);
|
215
|
|
216
|
$output .= theme('table', $variables);
|
217
|
}
|
218
|
else {
|
219
|
$output .= '<p>' . t('No images uploaded.') . '</p>';
|
220
|
}
|
221
|
|
222
|
$form['images_list'] = array(
|
223
|
'#markup' => $output,
|
224
|
);
|
225
|
|
226
|
return $form;
|
227
|
}
|
228
|
|
229
|
/**
|
230
|
* Menu callback submit.
|
231
|
*/
|
232
|
public function sweaver_menu_callback_submit($form, &$form_state) {
|
233
|
variable_set('sweaver_plugin_images_styles', $form_state['values']['sweaver_plugin_images_styles']);
|
234
|
variable_set('sweaver_plugin_images_tab', $form_state['values']['sweaver_plugin_images_tab']);
|
235
|
variable_set('sweaver_plugin_handle_images', 'sweaver_plugin_images');
|
236
|
drupal_set_message(t('The configuration options have been saved.'));
|
237
|
}
|
238
|
|
239
|
/**
|
240
|
* Images form.
|
241
|
*/
|
242
|
public function sweaver_images_backend_form() {
|
243
|
$form = array();
|
244
|
|
245
|
$description = '';
|
246
|
$check_upload = TRUE;
|
247
|
$image = $this->sweaver_get_image(arg(6));
|
248
|
if ($image) {
|
249
|
$check_upload = FALSE;
|
250
|
$description = $image->description;
|
251
|
$fid = $image->fid;
|
252
|
}
|
253
|
else {
|
254
|
$fid = NULL;
|
255
|
}
|
256
|
|
257
|
$form['#attributes'] = array('enctype' => 'multipart/form-data');
|
258
|
$form['#fid'] = $fid;
|
259
|
$form['#check_upload'] = $check_upload;
|
260
|
|
261
|
$form['image_form']['description'] = array(
|
262
|
'#type' => 'textfield',
|
263
|
'#title' => t('Description'),
|
264
|
'#required' => TRUE,
|
265
|
'#default_value' => $description,
|
266
|
'#description' => t('Description of the image used in the select box for files.')
|
267
|
);
|
268
|
if (!$fid) {
|
269
|
$form['image_form']['image'] = array(
|
270
|
'#type' => 'file',
|
271
|
'#title' => t('Image'),
|
272
|
'#description' => t('Upload images which you can use as background images. Extensions are limited to jpg, jpeg, png and gif.<br />Note: there is no scaling on the images on upload.'),
|
273
|
);
|
274
|
}
|
275
|
$form['image_form']['submit'] = array(
|
276
|
'#type' => 'submit',
|
277
|
'#value' => t('Save image'),
|
278
|
);
|
279
|
|
280
|
return $form;
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* Images validate.
|
285
|
*/
|
286
|
public function sweaver_images_backend_form_validate($form, &$form_state) {
|
287
|
$validators = array(
|
288
|
'file_validate_is_image' => array(),
|
289
|
);
|
290
|
|
291
|
$sweaver_directory = 'public://sweaver';
|
292
|
file_prepare_directory($sweaver_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
|
293
|
$image = file_save_upload('image', $validators, 'public://sweaver');
|
294
|
if ($image) {
|
295
|
$form_state['image'] = $image;
|
296
|
}
|
297
|
elseif ($form['#check_upload']) {
|
298
|
form_set_error('image', t('Image is required'));
|
299
|
}
|
300
|
}
|
301
|
|
302
|
/**
|
303
|
* Images add submit.
|
304
|
*/
|
305
|
public function sweaver_images_backend_form_submit(&$form, &$form_state) {
|
306
|
$image = new stdClass;
|
307
|
|
308
|
if (isset($form_state['image'])) {
|
309
|
$image_uploaded = $form_state['image'];
|
310
|
$image->fid = $image_uploaded->fid;
|
311
|
$image_parts = explode('.', $image_uploaded->uri);
|
312
|
$extension = array_pop($image_parts);
|
313
|
$new_image_name = 'public://sweaver/sweaver_image_' . $image_uploaded->fid . '.' . $extension;
|
314
|
file_unmanaged_move($image_uploaded->uri, $new_image_name);
|
315
|
|
316
|
// Resave.
|
317
|
$image_uploaded->uri = $new_image_name;
|
318
|
$image_uploaded->status = FILE_STATUS_PERMANENT;
|
319
|
file_save($image_uploaded);
|
320
|
}
|
321
|
|
322
|
if (!isset($image->fid)) $image->fid = $form['#fid'];
|
323
|
$update = (!$form['#check_upload']) ? array('fid') : array();
|
324
|
$image->description = $form_state['values']['description'];
|
325
|
drupal_write_record('sweaver_image', $image, $update);
|
326
|
|
327
|
drupal_set_message(t('Image %description has been saved.', array('%description' => $image->description)));
|
328
|
$form_state['redirect'] = 'admin/config/user-interface/sweaver/images';
|
329
|
}
|
330
|
|
331
|
/**
|
332
|
* Image delete form.
|
333
|
*/
|
334
|
public function sweaver_images_confirm_form_delete() {
|
335
|
$image = $this->sweaver_get_image(arg(6));
|
336
|
if ($image) {
|
337
|
$form['#image'] = $image;
|
338
|
return confirm_form($form, t('Are you sure you want to delete image %description?', array('%description' => $image->description)), 'admin/config/user-interface/sweaver/images');
|
339
|
}
|
340
|
else {
|
341
|
drupal_set_message(t('Image not found'));
|
342
|
drupal_goto('admin/config/user-interface/sweaver/images');
|
343
|
}
|
344
|
}
|
345
|
|
346
|
/**
|
347
|
* Image delete submit callback.
|
348
|
*/
|
349
|
public function sweaver_images_confirm_form_delete_submit(&$form, &$form_state) {
|
350
|
$image = $form['#image'];
|
351
|
file_delete($image);
|
352
|
db_query("DELETE FROM {sweaver_image} where fid = :fid", array(':fid' => $image->fid));
|
353
|
drupal_set_message(t('Image %description has been deleted.', array('%description' => $image->description)));
|
354
|
$form_state['redirect'] = 'admin/config/user-interface/sweaver/images';
|
355
|
}
|
356
|
|
357
|
/**
|
358
|
* Helper function to get image.
|
359
|
*
|
360
|
* @param $fid
|
361
|
* The file id.
|
362
|
* @return
|
363
|
* False or image object.
|
364
|
*/
|
365
|
public function sweaver_get_image($fid) {
|
366
|
if (!empty($fid)) {
|
367
|
$query = 'SELECT ti.fid, ti.description, f.uri FROM {sweaver_image} ti
|
368
|
INNER JOIN {file_managed} f on f.fid = ti.fid WHERE f.fid = :fid';
|
369
|
$image = db_query($query, array(':fid' => $fid))->fetchObject();
|
370
|
if (isset($image->fid)) {
|
371
|
return $image;
|
372
|
}
|
373
|
}
|
374
|
return FALSE;
|
375
|
}
|
376
|
}
|