-
Notifications
You must be signed in to change notification settings - Fork 0
/
perfecto.module
443 lines (370 loc) · 13.6 KB
/
perfecto.module
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
/**
* @file
* Float composition over web page.
*/
/**
* Don't use l() here because it adds active class. Drupal thinks it's
* another message thus showing 2 visually identical messages when landing
* on module page.
*
* It's a feature that it's shown always when there are no compositions
* uploaded - Perfecto should not be enabled when you don't use it.
*/
define('PERFECTO_WARNING_NO_COMPOSITIONS',
t('You have not uploaded any web compositions to Perfecto. !click_here to add some or !disable_this_module.',
array(
'!click_here' => '<a href=' . url('admin/settings/perfecto') . '>' . t('Click here') . '</a>',
'!disable_this_module' => '<a href=' . url('admin/settings/perfecto/disable_module', array('query' => array('token' => drupal_get_token('perfecto_disable_module')))) . '>' . t('disable this module') . '</a>',
)
)
);
/**
* Path where the images are saved.
*/
define('PERFECTO_FILE_PATH', 'public://mod_perfecto');
/**
* Implements hook_menu().
*/
function perfecto_menu() {
// Upload and listing page.
$items['admin/settings/perfecto'] = array(
'title' => 'Perfecto',
'description' => 'View, upload and delete web compositions.',
'page callback' => 'drupal_get_form',
'page arguments' => array('perfecto_admin_page_callback'),
'access arguments' => array('bypass perfecto content access control'),
);
$items['admin/settings/perfecto/disable_module'] = array(
'title' => 'Disable module',
'description' => '',
'page callback' => 'perfecto_disable_module_page_callback',
'access arguments' => array('bypass perfecto content access control'),
'type' => MENU_CALLBACK,
);
$items['admin/settings/perfecto/delete'] = array(
'title' => 'Delete composotion',
'description' => 'Hook for deleting composition.',
'page callback' => 'perfecto_unmanaged_delete_page_callback',
'access arguments' => array('bypass perfecto content access control'),
'type' => MENU_CALLBACK,
);
$items['admin/settings/perfecto/delete/all/confirmation'] = array(
'title' => 'Delete all compositions confirmation',
'description' => '',
'page callback' => 'drupal_get_form',
'page arguments' => array('perfecto_delete_all_confirmation_page_callback'),
'access arguments' => array('bypass perfecto content access control'),
'type' => MENU_CALLBACK,
);
$items['admin/settings/perfecto/composition_control_panel'] = array(
'title' => 'Overplay panel',
'description' => '',
'page callback' => 'perfecto_composition_panel_page_callback',
'access callback' => 'perfecto_check_multiple_access_controls_access_callback',
'access arguments' => array('bypass perfecto content access control', 'access perfecto control panel'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_init().
*/
function perfecto_init() {
if (user_access('bypass perfecto content access control') || user_access('access perfecto control panel')) {
global $base_path;
drupal_add_js('misc/jquery.cookie.js');
drupal_add_js('misc/ui/jquery.ui.core.min.js');
drupal_add_js('misc/ui/jquery.ui.widget.min.js');
drupal_add_js('misc/ui/jquery.ui.mouse.min.js');
drupal_add_js('misc/ui/jquery.ui.draggable.min.js');
drupal_add_js('misc/ui/jquery.ui.slider.min.js');
$module_path = drupal_get_path('module', 'perfecto');
drupal_add_css($module_path . '/css/perfecto-control-panel.css');
drupal_add_js($module_path . '/perfecto.js');
drupal_add_js(array('perfecto' => array('path' => $base_path . $module_path)), 'setting');
if (variable_get('perfecto_first_upload_ever')) {
drupal_add_js(array('perfecto' => array('firstUploadEver' => TRUE)), 'setting');
variable_set('perfecto_first_upload_ever', FALSE);
}
else {
drupal_add_js(array('perfecto' => array('firstUploadEver' => FALSE)), 'setting');
}
$directory = PERFECTO_FILE_PATH;
$files = file_scan_directory($directory, '/.*/');
// Add link to delete all files.
if (count($files) === 0) {
drupal_set_message(PERFECTO_WARNING_NO_COMPOSITIONS, 'warning', FALSE);
}
}
}
/**
* Implements hook_help().
*/
function perfecto_help($path, $arg) {
switch ($path) {
case 'admin/help#perfecto':
return nl2br(file_get_contents(dirname(__FILE__) . '/README.txt'));
}
}
/**
* Implements hook_permission().
*/
function perfecto_permission() {
return array(
'bypass perfecto content access control' => array(
'title' => t('Bypass content access control'),
'description' => t("Use, view, upload and delete web compositions regardless of permission restrictions."),
'restrict access' => TRUE,
),
'access perfecto control panel' => array(
'title' => t('Access composition control panel'),
'description' => t('Can use Perfecto but not upload or delete compositions.'),
),
);
}
/**
* Callback for hook_menu() item.
*/
function perfecto_composition_panel_page_callback() {
print theme('perfecto_composition', array('compositions' => perfecto_get_compositions()));
}
/**
* Implements hook_theme().
*/
function perfecto_theme() {
$path = drupal_get_path('module', 'perfecto');
return array(
'perfecto_composition' => array(
'template' => 'perfecto',
'path' => $path . '/theme',
),
'perfecto_composition_thumbnail' => array(
'template' => 'perfecto_composition_thumbnail',
'path' => $path . '/theme',
),
);
}
/**
* Returns composition files as array for our perfecto_theme().
*/
function perfecto_get_compositions() {
$output = array();
$directory = PERFECTO_FILE_PATH;
$files = file_scan_directory($directory, '/.*/');
foreach ($files as $file) {
$imageinfo = image_get_info($file->uri);
$file = (object) array_merge((array) $file, $imageinfo);
$file->url = file_create_url($file->uri);
$file->id = 'perfecto_composition-' . strtolower(str_replace(' ', '-', $file->name));
$output[] = $file;
}
sort($output);
return $output;
}
/**
* Menu callback; Returns form with image upload.
*/
function perfecto_admin_page_callback($form, &$form_state) {
// Composition file.
$form['unmanaged_file'] = array(
'#title' => t('Image'),
'#type' => 'file',
'#description' => t('Allowed files: jpg, jpeg and png.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Upload'),
);
$form['#suffix'] = perfecto_display_unmanaged_files_page();
return $form;
}
/**
* Form validation before submiting new composition.
*
* Check if file has correct extension and exists.
*/
function perfecto_admin_page_callback_validate($form, &$form_state) {
$validators = array(
'file_validate_extensions' => array('jpg jpeg png'),
);
$file = file_save_upload('unmanaged_file', $validators, FALSE, FILE_EXISTS_REPLACE);
if (isset($file)) {
// File upload was attempted.
if ($file) {
// Put the temporary file in form_values so we can save it on submit.
$form_state['values']['unmanaged_file'] = $file;
}
else {
form_set_error('unmanaged_file', t('The file could not be uploaded.'));
}
}
}
/**
* Submit new composition.
*/
function perfecto_admin_page_callback_submit($form, &$form_state) {
// Check if a file was uploaded.
if ($file = $form_state['values']['unmanaged_file']) {
// Create the directory if it doesn't exist.
$directory = PERFECTO_FILE_PATH;
file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
// Copy the file over.
$filename = file_unmanaged_copy($file->uri, $directory, FILE_EXISTS_REPLACE);
if ($filename) {
// Clear messages set by drupal_set_message().
drupal_get_messages();
$first_upload_ever = variable_get('perfecto_first_upload_ever', TRUE);
if ($first_upload_ever) {
drupal_set_message(t('Your first composition has been uploaded!'));
drupal_set_message(t("There's a red line blinking in the top right corner of the viewport. This area is for opening Perfecto control panel where you can toggle and move your composition. This line will be made invisible after you hover your mouse on it but it's still going to be there until Perfecto is enabled."), 'warning');
drupal_set_message(t('To drag the composition, hold down ctrl + left mouse button.'), 'warning');
drupal_set_message(t('You can also move composition by holding down ctrl + pressing arrow keys on your keyboard. If you hold down ctrl + shift, composition will move by 10px.'), 'warning');
// Add this variable to js from hook_init and then set to FALSE.
variable_set('perfecto_first_upload_ever', TRUE);
}
else {
drupal_set_message(t('Your composition has been uploaded!'));
}
}
else {
drupal_set_message(t('Sorry, could not upload composition. Check directory permissions.'), 'error');
}
}
}
/**
* Displays thumbnails and delete links of compositions.
*/
function perfecto_display_unmanaged_files_page() {
$module_path = drupal_get_path('module', 'perfecto');
drupal_add_css($module_path . '/css/perfecto-admin.css');
$directory = PERFECTO_FILE_PATH;
$files = file_scan_directory($directory, '/.*/');
// Add link to delete all files.
if (count($files)) {
$delete_all_link = l(t('Delete all compositions'), 'admin/settings/perfecto/delete/all/confirmation', array('query' => array('destination' => current_path())));
}
else {
$delete_all_link = FALSE;
}
// Loop through each file and display as thumbnail with 'view original' and
// 'delete' links.
$thumbnails = array();
$token = drupal_get_token('perfecto_delete_composition');
foreach ($files as $file) {
$thumbnails[] = array(
'image' => theme('image_style', array('style_name' => 'thumbnail', 'path' => $file->uri)),
'filename' => check_plain($file->filename),
'view_full_size_link' => l(t('View full size composition'), file_create_url($file->uri)),
'delete_link' => l(t('Delete'), 'admin/settings/perfecto/delete',
array(
'query' => array(
'destination' => current_path(),
'file' => $file->uri,
'token' => $token,
),
)
),
);
}
return theme('perfecto_composition_thumbnail', array('delete_all_link' => $delete_all_link, 'thumbnails' => $thumbnails));
}
/**
* Menu callback; Ask confirmation when deleting all compositions.
*/
function perfecto_delete_all_confirmation_page_callback() {
return confirm_form(array(),
t('Are you sure you want to delete all compositions?'),
isset($_GET['destination']) ? $_GET['destination'] : 'admin/settings/perfecto',
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* After confirmation delete all compositions.
*/
function perfecto_delete_all_confirmation_page_callback_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
if (file_unmanaged_delete_recursive(PERFECTO_FILE_PATH)) {
drupal_set_message(t('All compositions have been deleted.'));
global $base_path;
setcookie('perfecto_composition_id', '', time() - 3600, $base_path);
drupal_goto($_GET['destination']);
}
else {
drupal_set_message(t('Sorry, could not delete compositions. Check file permissions.'), 'error');
}
}
}
/**
* Menu callback; Takes care of deletion for compositions one by one.
*/
function perfecto_unmanaged_delete_page_callback() {
$error = FALSE;
if (!(isset($_GET['token']) && drupal_valid_token($_GET['token'], 'perfecto_delete_composition', TRUE))) {
drupal_set_message(t("Sorry, you can't access this page."), 'error');
$error = TRUE;
}
// Throw an error if the request is in the wrong directory.
if (!strpos($_GET['file'], PERFECTO_FILE_PATH) === FALSE) {
drupal_set_message(t("Sorry, you can't delete that composition."), 'error');
$error = TRUE;
}
// Throw an error if the file does not exist.
if (!file_exists($_GET['file'])) {
drupal_set_message(t('Sorry, that composition does not exist.'), 'error');
$error = TRUE;
}
// All is good, go ahead and delete the file.
if (!$error) {
if (drupal_unlink($_GET['file'])) {
$file_name = end(explode('/', $_GET['file']));
drupal_set_message(t('%file has been deleted.', array('%file' => $file_name)));
}
else {
drupal_set_message(t('Sorry, could not delete composition. Check file permissions.'), 'error');
}
}
if (isset($_GET['destination'])) {
drupal_goto($_GET['destination']);
}
else {
global $base_url;
drupal_goto($base_url);
}
}
/**
* Menu callback; Disables perfecto.
*/
function perfecto_disable_module_page_callback() {
if (!(isset($_GET['token']) && drupal_valid_token($_GET['token'], 'perfecto_disable_module', TRUE))) {
drupal_set_message(t("Sorry, you can't access this page."), 'error');
global $base_url;
drupal_goto($base_url);
}
$pre_install_list = module_list();
module_disable(array('perfecto'), FALSE);
// Gets module list after install process, flushes caches and displays a
// message if there are changes.
$post_install_list = module_list(TRUE);
if ($pre_install_list != $post_install_list) {
drupal_flush_all_caches();
// Clear our message (You have not uploaded any web compositions to ...)
drupal_get_messages();
drupal_set_message(t('The configuration options have been saved.'));
}
drupal_goto('admin/modules');
}
/**
* Access callback. If any of the permissions is true, function returns true.
*/
function perfecto_check_multiple_access_controls_access_callback() {
$args = func_get_args();
foreach ($args as $arg) {
if (user_access($arg)) {
return TRUE;
}
}
return FALSE;
}