-
Notifications
You must be signed in to change notification settings - Fork 2
/
focal_point.admin.inc
139 lines (122 loc) · 4.38 KB
/
focal_point.admin.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
<?php
/**
* @file
* Functionality needed by the focal point admin pages.
*/
/**
* System form for focal point configuration settings.
*/
function focal_point_configuration_form($form, &$form_state) {
$config = config('focal_point.settings');
$default_options = array();
$default_info = focal_point_get_default_method_info();
foreach ($default_info as $key => $info) {
$default_options[$key] = check_plain($info['label']);
}
$form['#config'] = 'focal_point.settings';
$form['focal_point_default_method'] = array(
'#type' => 'select',
'#title' => t('Calculate intial focal point using'),
'#descrition' => t('This can slow down your site, especially if you are bulk uploading images.'),
'#options' => array('' => t('Disabled')) + $default_options,
'#default_value' => $config->get('focal_point_default_method'),
'#access' => !empty($default_options),
);
$options = array(
'image' => t('Standard image fields'),
);
// @todo consider allowing $options to be altered.
$form['focal_point_enabled_for'] = array(
'#type' => 'checkboxes',
'#title' => 'Enable focal point for the following:',
'#options' => $options,
'#default_value' => $config->get('focal_point_enabled_for'),
);
$form['focal_point_test_drive_link'] = array(
'#type' => 'markup',
'#markup' => l(t('Take a test drive'), 'focal_point/test-drive'),
);
return system_settings_form($form);
}
/**
* Form builder for the "test drive" page.
*/
function focal_point_test_drive_form($form, &$form_state) {
$config = config('focal_point.settings');
$fid = $config->get('focal_point_test_drive_image');
$file = !is_null($fid) ? file_load($fid) : NULL;
$focal_point = $config->get('focal_point_test_drive_focal_point');
if (!empty($fid) && $file) {
// Display the image and include the focal point indicator and field.
$form['test_drive_image'] = array(
'#theme_wrappers' => array('container'),
'test_drive_indicator' => _focal_point_indicator('test-drive'),
'test_drive_widget_image' => array(
'#type' => 'markup',
'#theme' => 'image_style',
'#style_name' => 'focal_point_preview',
'#path' => $file->uri,
),
'focal_point_test_drive_focal_point' => _focal_point_field('test-drive', $focal_point),
);
}
$form['test_drive_submit'] = array(
'#type' => 'submit',
'#value' => t('Set the focal point'),
);
$form['examples'] = _focal_point_preview($fid, $focal_point);
$form['#submit'][] = 'focal_point_test_drive_form_submit';
return $form;
}
/**
* Form submit handler for focal_point_test_drive_form().
*
* @see focal_point_test_drive_form()
*/
function focal_point_test_drive_form_submit(&$form, &$form_state) {
$config = config('focal_point.settings');
// Save the test drive focal point setting.
if (isset($form_state['values']['focal_point_test_drive_focal_point'])) {
$old_value = $config->get('focal_point_test_drive_focal_point');
if ($old_value !== $form_state['values']['focal_point_test_drive_focal_point']) {
$config->set('focal_point_test_drive_focal_point', $form_state['values']['focal_point_test_drive_focal_point']);
$config->save();
// Delete old derivative images since the focal point changed.
$fid = $config->get('focal_point_test_drive_image');
$file = !is_null($fid) ? file_load($fid) : NULL;
if ($file) {
image_path_flush($file->uri);
}
}
}
}
/**
* Page callback function.
*
* This page displays a preview of derivative images for the given file.
*
* @param int $fid
* The fid of the image file entity in question.
* @param string $focal_point
* (Optional) A string in the form 'left-percent-offset, top-percent-offset'.
* Leave this value empty to use the value stored in the database.
*
* @return array
* The render array for the preview page.
*
* @see hook_focal_point_crop_data_alter()
*/
function focal_point_preview_page($fid, $focal_point = '') {
$file = is_numeric($fid) ? file_load($fid) : NULL;
if ($file && focal_point_validate($focal_point)) {
// Get the render array for the preview.
$output = _focal_point_preview($fid, $focal_point);
// Clear the existing derivative images if they exist.
image_path_flush($file->uri);
}
else {
backdrop_set_message(t('Invalid request. The preview images could not be displayed.'), 'error');
$output = array();
}
return $output;
}