imagefield_crop.install in Imagefield Crop 7.3
Same filename and directory in other branches
Module installation file.
File
imagefield_crop.installView source
<?php
// $id$
/**
* @file
* Module installation file.
*/
function imagefield_crop_install() {
$free_crop = array(
'name' => 'Free crop',
'data' => array(
'crop-type' => 'free',
),
);
drupal_write_record('imagefield_crop_preset', $free_crop);
}
function imagefield_crop_uninstall() {
if (db_table_exists('imagefield_crop_preset')) {
db_delete('imagefield_crop_preset');
}
if (db_table_exists('imagefield_crop_variants')) {
db_delete('imagefield_crop_variants');
}
}
/**
* Implements hook_field_schema().
*/
function imagefield_crop_field_schema($field) {
return array(
'columns' => array(
'fid' => array(
'description' => 'The {file_managed}.fid being referenced in this field.',
'type' => 'int',
'not null' => FALSE,
'unsigned' => TRUE,
),
'alt' => array(
'description' => "Alternative image text, for the image's 'alt' attribute.",
'type' => 'varchar',
'length' => 512,
'not null' => FALSE,
),
'title' => array(
'description' => "Image title text, for the image's 'title' attribute.",
'type' => 'varchar',
'length' => 1024,
'not null' => FALSE,
),
),
'indexes' => array(
'fid' => array(
'fid',
),
),
'foreign keys' => array(
'fid' => array(
'table' => 'file_managed',
'columns' => array(
'fid' => 'fid',
),
),
),
);
}
/**
* Impplements hook_schema
* @return array
*/
function imagefield_crop_schema() {
$schema = array();
$schema['imagefield_crop_preset'] = array(
'fields' => array(
'pid' => array(
'description' => 'Imagefield crop preset id',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'name' => array(
'description' => 'Imagefield crop preset name',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
'data' => array(
'description' => 'The configuration data for the preset.',
'type' => 'blob',
'not null' => TRUE,
'size' => 'big',
'serialize' => TRUE,
),
),
'indexes' => array(
'pid' => array(
'pid',
),
),
'primary key' => array(
'pid',
),
);
$schema['imagefield_crop_variants'] = array(
'fields' => array(
'cvid' => array(
'description' => 'Imagefield crop variant id',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'source_fid' => array(
'description' => 'Source file id',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'pid' => array(
'description' => 'Imagefield crop preset id',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'vid' => array(
'description' => 'Revision id',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'fid' => array(
'description' => 'Variant file id',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'data' => array(
'description' => 'The configuration data for the variant.',
'type' => 'blob',
'not null' => TRUE,
'size' => 'big',
'serialize' => TRUE,
),
),
'indexes' => array(
'cvid' => array(
'cvid',
),
),
'primary key' => array(
'cvid',
),
);
return $schema;
}
function imagefield_crop_update_7001() {
variable_del('imagefield_crop_info');
}
/**
* Enlarge size of Alt and Title columns.
*/
function imagefield_crop_update_7201() {
$alt_spec = array(
'type' => 'varchar',
'length' => 512,
'not null' => FALSE,
);
$title_spec = array(
'type' => 'varchar',
'length' => 1024,
'not null' => FALSE,
);
$fields = _update_7000_field_read_fields(array(
'module' => 'imagefield_crop',
'storage_type' => 'field_sql_storage',
));
foreach ($fields as $field_name => $field) {
$tables = array(
_field_sql_storage_tablename($field),
_field_sql_storage_revision_tablename($field),
);
$alt_column = $field['field_name'] . '_alt';
$title_column = $field['field_name'] . '_title';
foreach ($tables as $table) {
db_change_field($table, $alt_column, $alt_column, $alt_spec);
db_change_field($table, $title_column, $title_column, $title_spec);
}
}
}
/**
* Update to 7.x-3.0
*/
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');
}
}
}
}
/**
* Free crop preset
*/
function imagefield_crop_update_7301() {
$free_crop = array(
'name' => 'Free crop',
'data' => array(
'crop-type' => 'free',
),
);
drupal_write_record('imagefield_crop_preset', $free_crop);
}
/**
* Upgrade Imagecrop preset to use machine name
*/
function imagefield_crop_update_7302() {
if (!db_field_exists('imagefield_crop_preset', 'label')) {
db_add_field('imagefield_crop_preset', 'label', array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
));
db_query('UPDATE imagefield_crop_preset cp SET cp.label = cp.name');
}
foreach (field_info_fields() as $fname => $info) {
if ($info['type'] == 'imagefield_crop') {
foreach ($info['bundles'] as $type => $bundles) {
foreach ($bundles as $bundle) {
$instance = field_info_instance($type, $fname, $bundle);
$new_presets = array();
foreach ($instance['widget']['settings']['presets'] as $pid => $preset_name) {
if (is_int($pid)) {
$preset = imagefield_crop_preset_load($pid);
$new_presets[$preset->name] = $preset->label;
}
else {
$new_presets[$preset_name] = $preset_name;
}
}
$instance['widget']['settings']['presets'] = $new_presets;
field_update_instance($instance);
}
}
}
}
}
Functions
Name | Description |
---|---|
imagefield_crop_field_schema | Implements hook_field_schema(). |
imagefield_crop_install | @file Module installation file. |
imagefield_crop_schema | Impplements hook_schema |
imagefield_crop_uninstall | |
imagefield_crop_update_7001 | |
imagefield_crop_update_7201 | Enlarge size of Alt and Title columns. |
imagefield_crop_update_7202 | Update to 7.x-3.0 |
imagefield_crop_update_7301 | Free crop preset |
imagefield_crop_update_7302 | Upgrade Imagecrop preset to use machine name |