-
Notifications
You must be signed in to change notification settings - Fork 2
/
focal_point.install
200 lines (172 loc) · 5.65 KB
/
focal_point.install
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
<?php
/**
* @file
* Install hooks for focal_point.
*/
/**
* Implements hook_install().
*/
function focal_point_install() {
$t = get_t();
$count = focal_point_migrate_imagefield_focus_data();
$count_plural = format_plural($count, '%count image', '%count images', array('%count' => $count));
if (!empty($count)) {
backdrop_set_message($t('Migrated the imagefield_focus data for @count images. Any changes you make to imagefield_focus data from now on will not be reflected in your focal point data.', array('@count' => $count_plural)));
}
focal_point_update_1001();
}
/**
* Implements hook_uninstall().
*/
function focal_point_uninstall() {
// Delete the test drive image.
$fid = config_get('focal_point.settings', 'focal_point_test_drive_image');
if ($file = file_load($fid)) {
file_usage_delete($file, 'focal_point');
file_delete($file);
}
}
/**
* Implements hook_schema().
*/
function focal_point_schema() {
$schema['focal_point'] = array(
'fields' => array(
'fid' => array(
'description' => 'File ID.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
// The longest possible value of this is 100,100 which is 7 characters.
'focal_point' => array(
'type' => 'varchar',
'length' => 7,
'not null' => TRUE,
'default' => '',
),
),
'primary key' => array('fid'),
);
return $schema;
}
/**
* Convert existing imagefield_focus data to focal point.
*
* @param bool $overwrite
* If TRUE, existing focal point data will be overwritten.
*
* @return int|null
* Returns the number of images that were converted or NULL.
*/
function focal_point_migrate_imagefield_focus_data($overwrite = FALSE) {
if (module_exists('imagefield_focus')) {
// Get all imagefield_focus data, calculate the center of the focus rect,
// and use that as the new focal_point.
$select = db_select('imagefield_focus_file', 'iff');
if ($overwrite) {
$select->join('focal_point', 'fp', 'iff.fid = fp.fid');
}
$select->addField('iff', 'fid');
$select->addField('iff', 'focus_rect');
$rows = $select->execute()->fetchAllAssoc('fid');
foreach ($rows as $row) {
$file = file_load($row->fid);
list($x1, $y1, $x2, $y2) = explode(',', $row->focus_rect);
$focal_point_x = $x1 + round($x2 / 2);
$focal_point_y = $y1 + round($y2 / 2);
$file_info = image_get_info($file->uri);
$focal_point = round(100 * $focal_point_x / $file_info['width']) . ',' . round(100 * $focal_point_y / $file_info['height']);
focal_point_save($file->fid, $focal_point);
}
return count($rows);
}
return NULL;
}
/**
* Create a test drive image.
*
* If the test drive image already exists as a managed file, it will be
* replaced by this update.
*
* @param string $scheme
* The file scheme for saving a test image.
*/
function focal_point_create_test_image($scheme) {
$config = config('focal_point.settings');
$config->clear('focal_point_test_drive_focal_point');
$config->save();
// Create an image to use for the "Test Drive" page.
$test_drive_image = array(
'scheme' => $scheme . '://',
'dest_directory' => 'focal_point',
'source_path' => backdrop_get_path('module', 'focal_point') . '/images/' . 'test-drive.jpg',
'file_name' => 'test-drive.jpg',
);
$full_dir = $test_drive_image['scheme'] . $test_drive_image['dest_directory'];
$file_uri = $full_dir . '/' . $test_drive_image['file_name'];
file_prepare_directory($full_dir, FILE_CREATE_DIRECTORY);
file_unmanaged_copy($test_drive_image['source_path'], $file_uri, FILE_EXISTS_REPLACE);
$file = array(
'uid' => 1,
'status' => FILE_STATUS_PERMANENT,
'filename' => $test_drive_image['file_name'],
'uri' => $file_uri,
'filemime' => file_get_mimetype($test_drive_image['source_path']),
);
$file = entity_create('file', (array) $file);
file_save($file);
if ($file) {
$config->set('focal_point_test_drive_image', $file->fid);
$config->save();
}
}
/**
* Implements hook_update_last_removed().
*/
function focal_point_update_last_removed() {
return 7005;
}
/**
* @defgroup updates-7.x-to-1.x Updates from 7.x to 1.x
* @{
* Update functions from Drupal 7.x to Backdrop CMS 1.x.
*/
/**
* Move focal_point settings from variables to config.
*/
function focal_point_update_1000() {
// Migrate variables to config.
$config = config('focal_point.settings');
$config->set('focal_point_test_drive_image', update_variable_get('focal_point_test_drive_image', NULL));
$config->set('focal_point_test_drive_focal_point', update_variable_get('focal_point_test_drive_focal_point', FOCAL_POINT_DEFAULT));
$config->set('focal_point_default_method', update_variable_get('focal_point_default_method', ''));
$config->set('focal_point_enabled_for', update_variable_get('focal_point_enabled_for', array('image')));
$config->save();
// Delete variables.
update_variable_del('focal_point_test_drive_image');
update_variable_del('focal_point_test_drive_focal_point');
update_variable_del('focal_point_default_method');
update_variable_del('focal_point_enabled_for');
}
/**
* Update test drive image to use file default scheme, so remote schemes work.
*/
function focal_point_update_1001() {
$scheme = config_get('system.core', 'file_default_scheme');
$fid = config_get('focal_point.settings', 'focal_point_test_drive_image');
if (is_null($fid)) {
focal_point_create_test_image($scheme);
}
else {
$file = file_load($fid);
if ($file) {
$file->scheme = $scheme;
file_save($file);
}
}
}
/*
* @} End of "defgroup updates-7.x-to-1.x"
* The next series of updates should start at 2000.
*/