gallery.install 8.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
<?php
// $Id$

/**
 * gallery.module : gallery.install
 * Install/Uninstall functions
 */

/**
 * Implementation of hook_install().
 */
function gallery_install() {
13
  drupal_set_message(t(''));
14 15 16 17
}

/**
 * Implementation of hook_update_N().
18
 * (migrate settings from the 5.x-1.x to 5.x-2.x series)
19 20 21
 */
function gallery_update_1() {
  // Array containing 'old name' => 'new name' map
22
  $migrate = array(
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    // Image block settings
    'gallery_album_frame'               => 'gallery_block_image_0_album_frame',
    'gallery_block_block'               => 'gallery_block_image_0_block_block',
    'gallery_block_show'                => 'gallery_block_image_0_block_show',
    'gallery_item_frame'                => 'gallery_block_image_0_item_frame',
    'gallery_item_id'                   => 'gallery_block_image_0_item_id',
    'gallery_link_target'               => 'gallery_block_image_0_link_target',
    'gallery_block_num_images'          => 'gallery_block_image_0_num_images',
    'gallery_maxsize'                   => 'gallery_block_image_0_size',
    // Grid block settings
    'gallery_grid_album_frame'          => 'gallery_block_grid_0_album_frame',
    'gallery_grid_block_block'          => 'gallery_block_grid_0_block_block',
    'gallery_grid_block_show'           => 'gallery_block_grid_0_block_show',
    'gallery_grid_item_frame'           => 'gallery_block_grid_0_item_frame',
    'gallery_grid_item_id'              => 'gallery_block_grid_0_item_id',
    'gallery_grid_link_target'          => 'gallery_block_grid_0_link_target',
    'gallery_grid_maxsize'              => 'gallery_block_grid_0_size',
    'gallery_grid_num_cols'             => 'gallery_block_grid_0_num_cols',
    'gallery_grid_num_rows'             => 'gallery_block_grid_0_num_rows',
    // G2 filter settings
    'gallery_filter_default_size'       => 'gallery_filter_default_maxsize',
    // Search settings
    'gallery_search_max_rows_per_pager' => 'gallery_search_rows_per_pager',
    'gallery_search_maxsize'            => 'gallery_search_size',
    // Fullname support
    'gallery_use_full_name'             => 'gallery_use_fullname',
    'gallery_profile_full_name_field'   => 'gallery_profile_fullname_field'
  );
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  // Array containing obsolete variables
  $obsolete = array('gallery_search_max_items', 'gallery_autodetect_dir', 'gallery_uri', 'gallery_dir');
  
  // Update variables
  $ret = gallery_update_variables($migrate, $obsolete);
  
  // Update the blocks
  $ret[] = update_sql("UPDATE {blocks} SET delta = 'image-0' WHERE module = 'gallery' AND delta = 0");
  $ret[] = update_sql("UPDATE {blocks} SET delta = 'navigation' WHERE module = 'gallery' AND delta = 1");
  $ret[] = update_sql("UPDATE {blocks} SET delta = 'grid-0' WHERE module = 'gallery' AND delta = 2");
  
  // Mark gallery configuration invalid. This does NOT reset the configuration, but
  // forces the user to run the install wizard to (re)set and verify critical settings.
  variable_set('gallery_valid', FALSE);
  drupal_set_message('You were updating from gallery module 5.x-1.x (or earlier) to the 6.x-1.x
                      series of the module. All your settings were migrated automatically (see below),
                      but you will need to re-configure some basic options. Please visit the
                      Gallery settings page (admin/settings/gallery) to complete the update.',
                      'error');
  
  cache_clear_all('variables', 'cache');
  menu_rebuild();
  
  return $ret;
}

/**
 * Implementation of hook_update_N().
 * (initial update to Drupal 6)
 */
function gallery_update_6001() {
  $delta = 0;
  $new = $migrate = array();
  // Image block settings
  $imageblock_num = variable_get('gallery_block_image_num', 1);
  for ($i=0; $i<$imageblock_num; $i++) {
    $delta++;
    $new += array(
      'gallery_block_'. $delta .'_type'                 => 'imageblock',
      'gallery_block_'. $delta .'_imageblock_num_cols'  => 1,
    );
    $migrate += array(
      'gallery_block_image_'. $i .'_blockid'      => 'gallery_block_'. $delta .'_blockid',
      'gallery_block_image_'. $i .'_num_images'   => 'gallery_block_'. $delta .'_imageblock_num_rows',
      'gallery_block_image_'. $i .'_block_block'  => 'gallery_block_'. $delta .'_imageblock_block_block',
      'gallery_block_image_'. $i .'_item_id'      => 'gallery_block_'. $delta .'_imageblock_item_id',
      'gallery_block_image_'. $i .'_block_show'   => 'gallery_block_'. $delta .'_imageblock_block_show',
      'gallery_block_image_'. $i .'_size_method'  => 'gallery_block_'. $delta .'_imageblock_size_method',
      'gallery_block_image_'. $i .'_size'         => 'gallery_block_'. $delta .'_imageblock_size',
      'gallery_block_image_'. $i .'_album_frame'  => 'gallery_block_'. $delta .'_imageblock_album_frame',
      'gallery_block_image_'. $i .'_item_frame'   => 'gallery_block_'. $delta .'_imageblock_item_frame',
      'gallery_block_image_'. $i .'_link_target'  => 'gallery_block_'. $delta .'_imageblock_link_target',
      'gallery_block_image_'. $i .'_link'         => 'gallery_block_'. $delta .'_imageblock_link',
    );
  }
  // Grid block settings
  $gridblock_num = variable_get('gallery_block_grid_num', 1);
  for ($i=0; $i<$gridblock_num; $i++) {
    $delta++;
    $new['gallery_block_'. $delta .'_type'] = 'imageblock';
    $migrate += array(
      'gallery_block_grid_'. $i .'_blockid'       => 'gallery_block_'. $delta .'_blockid',
      'gallery_block_grid_'. $i .'_num_cols'      => 'gallery_block_'. $delta .'_imageblock_num_cols',
      'gallery_block_grid_'. $i .'_num_rows'      => 'gallery_block_'. $delta .'_imageblock_num_rows',
      'gallery_block_grid_'. $i .'_block_block'   => 'gallery_block_'. $delta .'_imageblock_block_block',
      'gallery_block_grid_'. $i .'_item_id'       => 'gallery_block_'. $delta .'_imageblock_item_id',
      'gallery_block_grid_'. $i .'_block_show'    => 'gallery_block_'. $delta .'_imageblock_block_show',
      'gallery_block_grid_'. $i .'_size_method'   => 'gallery_block_'. $delta .'_imageblock_size_method',
      'gallery_block_grid_'. $i .'_size'          => 'gallery_block_'. $delta .'_imageblock_size',
      'gallery_block_grid_'. $i .'_album_frame'   => 'gallery_block_'. $delta .'_imageblock_album_frame',
      'gallery_block_grid_'. $i .'_item_frame'    => 'gallery_block_'. $delta .'_imageblock_item_frame',
      'gallery_block_grid_'. $i .'_link_target'   => 'gallery_block_'. $delta .'_imageblock_link_target',
      'gallery_block_grid_'. $i .'_link'          => 'gallery_block_'. $delta .'_imageblock_link',
    );
  }
  $new['gallery_block_num'] = $delta;
  // Array containing obsolete variables
  $obsolete = array('gallery_page_callback', 'gallery_block_image_num', 'gallery_block_grid_num');

  // Add new variables
  foreach ($new as $name => $value) {
    db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, $value);
  }
  // Update variables
  $ret = gallery_update_variables($migrate, $obsolete);
  
  // Update the blocks
  $ret[] = update_sql("UPDATE {blocks} SET delta = 'image-0' WHERE module = 'gallery' AND delta = 0");
  $ret[] = update_sql("UPDATE {blocks} SET delta = 'navigation' WHERE module = 'gallery' AND delta = 1");
  $ret[] = update_sql("UPDATE {blocks} SET delta = 'grid-0' WHERE module = 'gallery' AND delta = 2");
  
  cache_clear_all('variables', 'cache');
  menu_rebuild();
  
  return $ret;
}

/**
 * Function gallery_update_variables().
 */
function gallery_update_variables($migrate, $obsolete) {
  $ret = array();
  
  $variables = array();
  // Fetch all gallery-related variables
  $result = db_query("SELECT * FROM {variable} WHERE name LIKE 'gallery_%'");
  while ($var = db_fetch_object($result)) {
    $variables[$var->name] = $var->value;
  }
  
  // Remove old variables
  db_query("DELETE FROM {variable} WHERE name LIKE 'gallery_%'");
  
164
  // Migrate old variables
165
  foreach ($migrate as $old => $new) {
166 167 168 169 170 171
    if (isset($variables[$old])) {
      $variables[$new] = $variables[$old];
      unset($variables[$old]);
      $ret[] = array('success' => TRUE, 'query' => 'Migrating variable ['. $old .' => '. $new .']');
    }
  }
172

173 174 175 176 177 178 179
  // Unset obsolete variables
  foreach ($obsolete as $var) {
    if (isset($variables[$var])) {
      unset($variables[$var]);
      $ret[] = array('success' => TRUE, 'query' => 'Removing variable ['. $var .']');
    }
  }
180
  
181 182 183 184 185 186 187
  // Save resulting variables array
  // (all variables not migrated or unset are taken over directly)
  foreach ($variables as $name => $value) {
    // We dont use variable_set() to reduce overhead
    // (i.e. unserialize => serialize and cache_clear_all() for each variable)
    db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $name, $value);
  }
188 189 190 191
  
  return $ret;
}

192 193 194 195 196 197 198 199 200
/**
 * Implementation of hook_uninstall().
 */
function gallery_uninstall() {
  // Remove all gallery related variables and blocks
  db_query("DELETE FROM {variable} WHERE name LIKE 'gallery_%'");
  db_query("DELETE FROM {blocks} WHERE module = 'gallery'");
  cache_clear_all('variables', 'cache');
}