function imagefield_crop_update_7202 in Imagefield Crop 7.3
Update to 7.x-3.0
File
- ./
imagefield_crop.install, line 193 - Module installation file.
Code
function imagefield_crop_update_7202() {
$schema = imagefield_crop_schema();
foreach ($schema as $name => $table) {
if (!db_table_exists($name)) {
db_create_table($name, $table);
}
}
// Add default presets
$free_crop = array(
'name' => 'Free crop',
'data' => array(
'crop-type' => 'free',
),
);
drupal_write_record('imagefield_crop_preset', $free_crop);
$if_crop_instances = array();
foreach (field_info_instances() as $entity) {
foreach ($entity as $fields) {
foreach ($fields as $field_name => $instance) {
if ($instance['widget']['module'] == 'imagefield_crop') {
$to_update[$instance['field_name']] = $instance;
if (!empty($instance['widget']['settings']['resolution'])) {
$if_crop_instances[$instance['widget']['settings']['resolution']][] = $instance;
}
elseif (!empty($instance['widget']['settings']['ratio'])) {
$if_crop_instances[$instance['widget']['settings']['ratio']][] = $instance;
}
else {
$if_crop_instances['free'][] = $instance;
}
}
}
}
}
foreach ($if_crop_instances as $field_id => $instances) {
$record = array();
$instance = reset($instances);
if (!empty($instance['widget']['settings']['resolution'])) {
list($record['data']['resolution']['width'], $record['data']['resolution']['height']) = explode('x', $instance['widget']['settings']['resolution']);
$record['data']['crop-type'] = 'resolution';
}
elseif (!empty($instance['widget']['settings']['ration'])) {
list($record['data']['ratio']['width'], $record['data']['ratio']['height']) = explode('x', $instance['widget']['settings']['ratio']);
$record['data']['crop-type'] = 'ratio';
list($record['min-resolution']['width'], $record['min-resolution']['height']) = array(
0,
0,
);
}
$record['name'] = $field_id;
drupal_write_record('imagefield_crop_preset', $record);
foreach ($instances as $new_instance) {
unset($new_instance['widget']['settings']['croparea'], $new_instance['widget']['settings']['custom_ratio'], $new_instance['widget']['settings']['enforce_minimum'], $new_instance['widget']['settings']['enforce_ratio'], $new_instance['widget']['settings']['gif_processing'], $new_instance['widget']['settings']['resolution'], $new_instance['widget']['settings']['select_maximum_area']);
$new_instance['widget']['settings']['presets'] = array(
$record['pid'] => $record['pid'],
);
field_update_instance($new_instance);
$field_name = $new_instance['field_name'];
$table = _field_sql_storage_tablename(field_info_field($field_name));
$rev_table = _field_sql_storage_revision_tablename(field_info_field($field_name));
$query = db_select($table, $table)
->fields($table);
$query
->condition('bundle', $new_instance['bundle'])
->condition('entity_type', $new_instance['entity_type']);
$result = $query
->execute()
->fetchAllAssoc('entity_id');
if (empty($result)) {
continue;
}
else {
foreach ($result as $entity_id => $item) {
$source_file = _imagefield_crop_file_to_crop($item->{$field_name . '_fid'});
$data = array(
'cropbox_x' => $item->{$field_name . '_cropbox_x'},
'cropbox_y' => $item->{$field_name . '_cropbox_y'},
'cropbox_width' => $item->{$field_name . '_cropbox_width'},
'cropbox_height' => $item->{$field_name . '_cropbox_height'},
'uri' => file_load($item->{$field_name . '_fid'})->uri,
'active' => TRUE,
);
$variant = array(
'pid' => $record['pid'],
'vid' => $item->revision_id,
'fid' => $item->{$field_name . '_fid'},
'source_fid' => $source_file->fid,
'data' => (object) $data,
);
drupal_write_record('imagefield_crop_variants', $variant);
$variant = array();
foreach (array(
$table,
$rev_table,
) as $tbl) {
$query = db_update($tbl)
->fields(array(
$field_name . '_fid' => $source_file->fid,
))
->condition('entity_id', $entity_id)
->execute();
db_drop_field($table, $field_name . '_cropbox_x');
db_drop_field($table, $field_name . '_cropbox_y');
db_drop_field($table, $field_name . '_cropbox_width');
db_drop_field($table, $field_name . '_cropbox_height');
}
}
}
}
}
foreach ($if_crop_instances as $field_id => $instances) {
foreach ($instances as $new_instance) {
$field_name = $new_instance['field_name'];
$table = _field_sql_storage_tablename(field_info_field($field_name));
$rev_table = _field_sql_storage_revision_tablename(field_info_field($field_name));
foreach (array(
$table,
$rev_table,
) as $tbl) {
db_drop_field($table, $field_name . '_cropbox_x');
db_drop_field($table, $field_name . '_cropbox_y');
db_drop_field($table, $field_name . '_cropbox_width');
db_drop_field($table, $field_name . '_cropbox_height');
}
}
}
}