function epsacrop_update_7201 in EPSA Crop - Image Cropping 7.2
Converts all the epsacrop coordinates to a new machine_name format. Issue related: http://drupal.org/node/1396500
File
- ./
epsacrop.install, line 114 - install file for epsacrop.module
Code
function epsacrop_update_7201() {
// Get all the EPSACROP files
$result = db_select('epsacrop_files', 'ef')
->fields('ef', array())
->execute()
->fetchAllKeyed();
// Get all the EPSACROP image styles
$epsacrop_styles = _epsacrop_load_styles();
foreach ($result as $fid => $coords) {
// Old coordenates
$coords_old = drupal_json_decode(unserialize($coords));
// New Coordenates
$coords_new = array();
// Find the right coords
foreach ($coords_old as $key => $item) {
if (!empty($item)) {
// Iterates each crop coords by image style
foreach ($item as $name_old => $data) {
// Extract the ieid and the isid
preg_match("/epsacrop\\-([0-9]*)\\-([0-9]*)/", $name_old, $matches);
$ieid = $matches[1];
$isid = $matches[2];
// Find the machine name of the image style by the isid
$style_name = '';
foreach ($epsacrop_styles as $style) {
if ($style['isid'] == $isid) {
$style_name = $style['name'];
}
}
// Rename the object name with the new format
if (!empty($style_name)) {
$coords_new[$key]['epsacrop-' . $style_name] = $data;
}
}
}
else {
$coords_new[$key] = NULL;
}
}
if (!empty($coords_new)) {
// Encode the new coordenates
$coords_new = serialize(drupal_json_encode($coords_new));
// Save the changes
$record = array(
'fid' => $fid,
'coords' => $coords_new,
);
drupal_write_record('epsacrop_files', $record, 'fid');
}
else {
// No coordinates were found, probably because the image styles couldn't
// be found based on the id. Remove the coordinates data from the
// database.
db_delete('epsacrop_files')
->condition('fid', $fid)
->execute();
}
}
}