You are here

function manualcrop_update_7100 in Manual Crop 7

Replace the style id with a style name column.

File

./manualcrop.install, line 145
Install, update and uninstall functions for the Manual Crop module.

Code

function manualcrop_update_7100(&$sandbox) {

  // Add the style name field.
  db_add_field('manualcrop', 'style_name', array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));

  // Select all style names.
  $styles = db_query('SELECT m.isid, s.name FROM {manualcrop} m INNER JOIN {image_styles} s ON m.isid = s.isid GROUP BY m.isid');
  foreach ($styles as $style) {

    // Set the style name for each used style.
    db_query('UPDATE {manualcrop} SET style_name = :name WHERE isid = :isid', array(
      ':name' => $style->name,
      ':isid' => $style->isid,
    ));
  }

  // Create the new PK.
  db_drop_primary_key('manualcrop');
  db_add_primary_key('manualcrop', array(
    'fid',
    'style_name',
  ));

  // Remove the style id field.
  db_drop_field('manualcrop', 'isid');
  foreach (image_styles() as $style_name => $style) {

    // Only styles that have an id should be updated.
    if (isset($style['isid']) && !empty($style['effects'])) {

      // Check if the first effect is a Manual Crop effect.
      $effect = reset($style['effects']);
      if ($effect['module'] == 'manualcrop') {

        // Update the effect data.
        unset($effect['data']['isid']);
        $effect['data']['style_name'] = $style_name;
        image_effect_save($effect);
      }
    }
  }
}