You are here

function photos_update_7300 in Album Photos 7.3

Migrate image title from {file_managed}.filename to {photos_image}.title.

File

./photos.install, line 448
Install, update, and uninstall functions for the Album Photos module.

Code

function photos_update_7300() {
  $spec = array(
    'description' => 'The title of this image.',
    'type' => 'varchar',
    'length' => 255,
    'not null' => FALSE,
    'default' => '',
  );
  if (!db_field_exists('photos_image', 'title')) {
    db_add_field('photos_image', 'title', $spec);
  }
  $query = db_select('photos_image', 'i');
  $query
    ->join('file_managed', 'f', 'f.fid = i.fid');
  $query
    ->fields('i', array(
    'fid',
  ));
  $query
    ->fields('f', array(
    'filename',
    'uri',
  ));
  $result = $query
    ->execute();
  foreach ($result as $file) {

    // Update new image title field.
    db_update('photos_image')
      ->fields(array(
      'title' => $file->filename,
    ))
      ->condition('fid', $file->fid)
      ->execute();

    // Reset file_managed filename.
    $filename = drupal_basename($file->uri);
    db_update('file_managed')
      ->fields(array(
      'filename' => $filename,
      'status' => 1,
    ))
      ->condition('fid', $file->fid)
      ->execute();
  }
  return t('Photos image titles migrated successfully!');
}