[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/imagefield/ -> imagefield.install (source)

   1  <?php
   2  // $Id: imagefield.install,v 1.35 2010/06/29 22:33:43 quicksketch Exp $
   3  
   4  /**
   5   * Implementation of hook_install().
   6   */
   7  function imagefield_install() {
   8    drupal_load('module', 'content');
   9    content_notify('install', 'imagefield');
  10  }
  11  
  12  function imagefield_uninstall() {
  13    drupal_load('module', 'content');
  14    content_notify('uninstall', 'imagefield');
  15  }
  16  
  17  function imagefield_enable() {
  18    drupal_load('module', 'content');
  19    content_notify('enable', 'imagefield');
  20  }
  21  
  22  function imagefield_disable() {
  23    drupal_load('module', 'content');
  24    content_notify('disable', 'imagefield');
  25  }
  26  
  27  /**
  28   * Implementation of hook_update_last_removed().
  29   */
  30  function imagefield_update_last_removed() {
  31    // ImageField has later updates than this in the Drupal 5 version, however
  32    // they deal with changing formatters that are not necessary for a successful
  33    // upgrade to Drupal 6. Update 2 is as far as we really need.
  34    return 2;
  35  }
  36  
  37  /**
  38   * Upgrade to CCK 2 and Drupal 6.
  39   */
  40  function imagefield_update_6001() {
  41    // This update was moved into 6004 so that it can be run again for users
  42    // who were not properly updated.
  43    return array();
  44  }
  45  
  46  /**
  47   * Migrate fields to the new structure.
  48   */
  49  function imagefield_update_6002() {
  50    // This update was moved to 6004 so that it can be run again for users
  51    // who were not properly updated.
  52    return array();
  53  }
  54  
  55  /**
  56   * Convert image field type to filefield.
  57   */
  58  function imagefield_update_6003() {
  59    $ret = array();
  60  
  61    if (!module_exists('content')) {
  62      $ret['#abort'] = array('success' => FALSE, 'query' => t('Content module must be enabled before ImageField can be updated.'));
  63      return $ret;
  64    }
  65  
  66    if (drupal_get_installed_schema_version('filefield', TRUE) < 6001) {
  67      $ret['#abort'] = array('success' => FALSE, 'query' => t('FileField must be updated to Drupal 6 before ImageField can be updated.'));
  68      return $ret;
  69    }
  70  
  71    $ret[] = update_sql("UPDATE {" . content_field_tablename() . "} SET type = 'filefield', module = 'filefield', active = 1 WHERE module = 'imagefield' OR type = 'image'");
  72    $ret[] = update_sql("UPDATE {" . content_instance_tablename() . "} SET widget_type = 'imagefield_widget', widget_module = 'imagefield', widget_active = 1 WHERE widget_type = 'image' OR widget_type = 'imagefield_widget'");
  73    content_clear_type_cache();
  74  
  75    return $ret;
  76  }
  77  
  78  /**
  79   * Migrate fields to the new structure.
  80   */
  81  function imagefield_update_6004(&$context) {
  82    drupal_load('module', 'content');
  83    module_load_install('content');
  84    module_load_include('inc', 'imagefield', 'imagefield_file');
  85    module_load_include('inc', 'content', 'includes/content.admin');
  86    module_load_include('inc', 'content', 'includes/content.crud');
  87  
  88    $ret = array();
  89  
  90    if (!isset($context['sandbox']['progress'])) {
  91      // Get the latest cache values and schema.
  92      content_clear_type_cache(TRUE);
  93  
  94      // Grab the list of fields to update.
  95      $context['sandbox']['fields'] = array();
  96      foreach (content_types_install() as $type_name => $fields) {
  97        foreach ($fields as $field) {
  98          if ($field['type'] == 'filefield' && $field['widget']['type'] == 'imagefield_widget') {
  99            // We only process a given field once.
 100            $context['sandbox']['fields'][$field['field_name']] = $field;
 101            // Widgets are each updated individually.
 102            $context['sandbox']['widgets'][] = $field;
 103          }
 104        }
 105      }
 106  
 107      if (empty($context['sandbox']['fields'])) {
 108        return $ret;
 109      }
 110  
 111      // Add/update the database fields.
 112      foreach ($context['sandbox']['fields'] as $field) {
 113        $db_info = content_database_info($field);
 114  
 115        // Convert the default value for the FID field to NULL.
 116        db_change_field($ret, $db_info['table'], $field['field_name'] . '_fid', $field['field_name'] . '_fid', array('type' => 'int'));
 117        $ret[] = update_sql("UPDATE {" . $db_info['table'] . "} SET " . $field['field_name'] . "_fid = NULL WHERE " . $field['field_name'] . "_fid = 0");
 118  
 119        // Set any entries that were abandoned by poor housekeeping to NULL.
 120        $ret[] = update_sql("UPDATE {" . $db_info['table'] . "} SET " . $field['field_name'] . "_fid = NULL WHERE " . $field['field_name'] . "_fid NOT IN (SELECT fid FROM {files})");
 121  
 122        // Add the "data" and "list" columns to the field if not there already.
 123        if (!db_column_exists($db_info['table'], $field['field_name'] . '_list')) {
 124          db_add_field($ret, $db_info['table'], $field['field_name'] . '_list', array('type' => 'int', 'size' => 'tiny'));
 125        }
 126        if (!db_column_exists($db_info['table'], $field['field_name'] . '_data')) {
 127          db_add_field($ret, $db_info['table'], $field['field_name'] . '_data', array('type' => 'text'));
 128        }
 129  
 130        // Set the default state of the global settings.
 131        $field['list_field'] = '0';
 132        $field['list_default'] = '1';
 133        $field['description_field'] = '0';
 134  
 135        // Map 'max_number_images' parameter to CCK 'multiple'.
 136        if (!empty($field['widget']['multiple']) && isset($field['widget']['max_number_images'])) {
 137          if ($field['widget']['max_number_images'] == 0) {
 138            $field['multiple'] = 1; // 1 means "Unlimited" in CCK.
 139          }
 140          elseif ($field['widget']['max_number_images'] == 1) {
 141            $field['multiple'] = 0; // 0 means "Not Multiple" in CCK.
 142          }
 143          else {
 144            $field['multiple'] = $field['widget']['max_number_images'];
 145          }
 146        }
 147  
 148        // Update format names.
 149        $display_settings = array('teaser', 'full');
 150        foreach ($display_settings as $display_context) {
 151          if (isset($field['display_settings'][$display_context])) {
 152            switch ($field['display_settings'][$display_context]['format']) {
 153              case 'imagefield_nodelink':
 154                $field['display_settings'][$display_context]['format'] = 'image_nodelink';
 155                break;
 156              case 'imagefield_imagelink':
 157                $field['display_settings'][$display_context]['format'] = 'image_imagelink';
 158                break;
 159              case 'imagefield_path':
 160                $field['display_settings'][$display_context]['format'] = 'path_plain';
 161                break;
 162              case 'imagefield_url':
 163                $field['display_settings'][$display_context]['format'] = 'url_plain';
 164                break;
 165              case 'imagefield_default':
 166              case 'default':
 167                $field['display_settings'][$display_context]['format'] = 'image_plain';
 168                break;
 169            }
 170          }
 171        }
 172  
 173        // Move the default_image options to the widget level.
 174        $field['widget']['default_image'] = isset($field['default_image']) ? $field['default_image'] : NULL;
 175        $field['widget']['use_default_image'] = isset($field['use_default_image']) ? $field['use_default_image'] : 0;
 176  
 177        // The default image needs to be saved in the files table.
 178        if (isset($field['widget']['default_image']['filepath'])) {
 179          $file = (object) $field['widget']['default_image'];
 180          // Check if it's already in the files table.
 181          if ($fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", $file->filepath))) {
 182            $field['widget']['default_image']['fid'] = $fid;
 183          }
 184          // Otherwise add it.
 185          else {
 186            $file->uid = 1;
 187            $file->status = 1;
 188            drupal_write_record('files', $file);
 189            $field['widget']['default_image']['fid'] = $file->fid;
 190          }
 191  
 192          // Unset the field-level definition, or it will take precedence.
 193          unset($field['default_image']);
 194        }
 195  
 196        content_field_instance_update($field);
 197      }
 198  
 199      // Update each widget instance.
 200      foreach ($context['sandbox']['widgets'] as $field) {
 201        // Change file_path to image_path.
 202        if (isset($field['widget']['image_path'])) {
 203          $field['widget']['file_path'] = $field['widget']['image_path'];
 204        }
 205  
 206        // Update the formatters.
 207        foreach ($field['display_settings'] as $key => $display) {
 208          switch ($display['format']) {
 209            case 'default':
 210              $field['display_settings'][$key]['format'] = 'image_plain';
 211              break;
 212            case 'imagefield_nodelink':
 213              $field['display_settings'][$key]['format'] = 'image_nodelink';
 214              break;
 215            case 'imagefield_imagelink':
 216              $field['display_settings'][$key]['format'] = 'image_imagelink';
 217              break;
 218            case 'imagefield_path':
 219              $field['display_settings'][$key]['format'] = 'path_plain';
 220              break;
 221            case 'imagefield_url':
 222              $field['display_settings'][$key]['format'] = 'url_plain';
 223              break;
 224          }
 225        }
 226  
 227        content_field_instance_update($field);
 228      }
 229  
 230      $context['sandbox']['progress'] = 0;
 231      $context['sandbox']['total'] = count($context['sandbox']['fields']);
 232      $context['sandbox']['current_node'] = 0;
 233    }
 234  
 235    $field = array_shift($context['sandbox']['fields']);
 236    $db_info = content_database_info($field);
 237    $table = $db_info['table'];
 238    $col_fid = $field['field_name'] .'_fid';
 239    $col_alt = $field['field_name'] .'_alt';
 240    $col_title = $field['field_name'] .'_title';
 241    $col_data = $field['field_name'] .'_data';
 242    $col_list = $field['field_name'] .'_list';
 243  
 244    $limit = 100;
 245    $result = db_query_range("SELECT * FROM {". $table ."} WHERE vid > %d ORDER BY vid ASC", $context['sandbox']['current_node'], 0, $limit);
 246    $has_processed = FALSE;
 247  
 248    // Loop through each ImageField row and convert its alt and title columns.
 249    while ($row = db_fetch_array($result)) {
 250      // Try to unserialize the data column.
 251      if (!empty($row[$col_data])) {
 252        $data = unserialize($row[$col_data]);
 253      }
 254      if (empty($data)) {
 255        $data = array();
 256      }
 257  
 258      // Copy the values into the data array.
 259      if (isset($row[$col_alt])) {
 260        $data['alt'] = $row[$col_alt];
 261      }
 262      if (isset($row[$col_title])) {
 263        $data['title'] = $row[$col_title];
 264      }
 265      $list = isset($row[$col_list]) ? $row[$col_list] : 1;
 266  
 267      // Depending on if this is multivalue or not, update based on delta.
 268      if ($field['multiple'] > 0) {
 269        $query = "UPDATE {". $table ."} SET $col_data = '%s', $col_list = %d WHERE vid = %d AND delta = %d";
 270      }
 271      else {
 272        $query = "UPDATE {". $table ."} SET $col_data = '%s', $col_list = %d WHERE vid = %d";
 273        $row['delta'] = 0;
 274      }
 275  
 276      // Serialize it and store it back to the db.
 277      db_query($query, serialize($data), $list, $row['vid'], $row['delta']);
 278  
 279      // Update our progress information.
 280      $context['sandbox']['current_node'] = $row['vid'];
 281      $has_processed = TRUE;
 282    }
 283  
 284    if ($has_processed) {
 285      // Not finished, put back the field in the array.
 286      array_unshift($context['sandbox']['fields'], $field);
 287    }
 288    else {
 289      // Cleanup the old columns.
 290      if (db_column_exists($table, $col_alt)) {
 291        db_drop_field($ret, $table, $col_alt);
 292      }
 293      if (db_column_exists($table, $col_title)) {
 294        db_drop_field($ret, $table, $col_title);
 295      }
 296  
 297      // Process to next field.
 298      $context['sandbox']['progress']++;
 299      $context['sandbox']['current_node'] = 0;
 300    }
 301  
 302    if (!empty($context['sandbox']['fields'])) {
 303      $ret['#finished'] = $context['sandbox']['progress'] / $context['sandbox']['total'];
 304    }
 305  
 306    return $ret;
 307  }
 308  
 309  /**
 310   * Delete thumbnails spread throughout the files directory.
 311   */
 312  function imagefield_update_6005() {
 313    $ret = array();
 314  
 315    $result = db_query("SELECT * FROM {files} WHERE filemime LIKE 'image/%'");
 316  
 317    while ($file = db_fetch_object($result)) {
 318      // ImageField 6002 thumbnail path.
 319      $thumb_path_a = $file->filepath . '.thumb.jpg';
 320  
 321      // ImageField 6004 thumbnail path.
 322      $extension_dot = strrpos($file->filepath, '.');
 323      $extension = substr($file->filepath, $extension_dot + 1);
 324      $basepath = substr($file->filepath, 0, $extension_dot);
 325      $thumb_path_b = $basepath .'.thumb.'. $extension;
 326  
 327      file_delete($thumb_path_a);
 328      file_delete($thumb_path_b);
 329    }
 330  
 331    $ret[] = array('success' => TRUE, 'query' => t('Deleted admin thumbnails distributed throughout files directory. All thumbnails are now stored in the "imagefield_thumbs" directory.'));
 332  
 333    return $ret;
 334  }
 335  
 336  /**
 337   * Add default values to all ImageFields.
 338   */
 339  function imagefield_update_6006() {
 340    $ret = array();
 341    module_load_install('content');
 342    foreach (content_types_install() as $type_name => $fields) {
 343      foreach ($fields as $field) {
 344        if ($field['type'] == 'filefield' && $field['widget']['type'] == 'imagefield_widget') {
 345          $field['widget']['file_extensions'] = isset($field['widget']['file_extensions']) ? $field['widget']['file_extensions'] : NULL;
 346          $field['widget']['max_filesize_per_file'] = isset($field['widget']['max_filesize_per_file']) ? $field['widget']['max_filesize_per_file'] : NULL;
 347          $field['widget']['max_filesize_per_node'] = isset($field['widget']['max_filesize_per_node']) ? $field['widget']['max_filesize_per_node'] : NULL;
 348          content_field_instance_update($field);
 349        }
 350      }
 351    }
 352  
 353    return $ret;
 354  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7