You are here

filefield_paths.install in File (Field) Paths 6.2

Install, update and uninstall functions for the FileField Paths module.

File

filefield_paths.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the FileField Paths module.
 */

/**
 * Implements hook_install().
 */
function filefield_paths_install() {
  drupal_install_schema('filefield_paths');
  db_add_field($ret = array(), 'files', 'origname', array(
    'description' => 'Original name of the file.',
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  ));
  $result = db_query("SELECT fid, filename FROM {files}");
  while ($file = db_fetch_object($result)) {
    db_query("UPDATE {files} SET origname = '%s' WHERE fid = %d", $file->filename, $file->fid);
  }
  variable_set('filefield_paths_schema_version', 6103);
}

/**
 * Implements hook_uninstall().
 */
function filefield_paths_uninstall() {
  drupal_uninstall_schema('filefield_paths');
  db_drop_field($ret = array(), 'files', 'origname');
  variable_del('filefield_paths_schema_version');
}

/**
 * Implements hook_update_N().
 */
function filefield_paths_update_6100() {
  $ret = array();
  if (db_table_exists('filefield_paths')) {
    return $ret;
  }
  $schema['filefield_paths'] = array(
    'fields' => array(
      'type' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'field' => array(
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'filename' => array(
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
        'serialize' => TRUE,
      ),
      'filepath' => array(
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
        'serialize' => TRUE,
      ),
    ),
    'unique keys' => array(
      'type_field' => array(
        'type',
        'field',
      ),
    ),
  );
  db_create_table($ret, 'filefield_paths', $schema['filefield_paths']);
  $fields = array();
  $content_type = content_types();
  foreach ($content_type as $type) {
    $types[] = $type['type'];
    foreach ($type['fields'] as $field) {
      if (preg_match('/\\bfilefield\\b|\\bimage\\b/', $field['type'])) {
        if (!in_array($field['field_name'], $fields)) {
          $fields[] = $field['field_name'];
        }
      }
    }
  }
  $types = implode('|', $types);
  $fields = implode('|', $fields);
  $result = db_query("SELECT * FROM {variable} WHERE name LIKE 'filefield_paths_%'");
  while ($variable = db_fetch_object($result)) {
    $pattern = '/filefield_paths_(' . $types . ')_(' . $fields . ')_(file_name|file_path)_?(.*)/';
    if (preg_match($pattern, $variable->name, $results)) {
      if (empty($results[4])) {
        $results[4] = 'value';
      }
      $data[$results[1]][$results[2]][$results[3]][$results[4]] = unserialize($variable->value);
    }
  }
  foreach ($data as $type => $type_data) {
    foreach ($type_data as $field => $field_data) {
      $result = db_fetch_object(db_query("SELECT widget_settings FROM {content_node_field_instance} " . "WHERE field_name = '%s' AND type_name = '%s'", $field, $type));
      $widget_settings = unserialize($result->widget_settings);
      $field_data['file_path']['value'] = $widget_settings['file_path'];
      $result = db_query("INSERT INTO {filefield_paths} (type, field, filename, filepath) VALUES ('%s', '%s', '%s', '%s')", $type, $field, serialize($field_data['file_name']), serialize($field_data['file_path']), TRUE);
      $ret[] = array(
        'success' => $result !== FALSE,
        'query' => check_plain("\n        INSERT INTO {filefield_paths} (type, field, filename, filepath) VALUES ('" . $type . "', '" . $field . "', '" . serialize($field_data['file_name']) . "', '" . serialize($field_data['file_path']) . "')\n      "),
      );
    }
  }
  $ret[] = update_sql("DELETE FROM {variable} WHERE name LIKE 'filefield_paths_%'");
  cache_clear_all('variables', 'cache');
  return $ret;
}
function filefield_paths_update_6101() {
  variable_set('filefield_paths_schema_version', 6101);
  return array();
}
function filefield_paths_update_6102() {
  variable_set('filefield_paths_schema_version', 6102);
  $fields = module_invoke_all('filefield_paths_field_settings');
  $result = db_query("SELECT * FROM {filefield_paths}");
  while ($data = db_fetch_object($result)) {
    foreach ($fields as $field) {
      $cols[] = $field['sql'] . " = '%s'";
      $val = unserialize($data->{$field}['sql']);
      $val = str_replace('[filefield_paths-name]', '[filefield-onlyname]', $val);
      $val = str_replace('[filefield_paths-ext]', '[filefield-extension]', $val);
      $vals[] = serialize($val);
    }
    db_query("UPDATE {filefield_paths} SET " . implode(', ', $cols) . " WHERE type = '%s' AND field = '%s'", array_merge($vals, array(
      $data->type,
      $data->field,
    )));
  }
  return array();
}
function filefield_paths_update_6103() {
  $schema_version = variable_get('filefield_paths_schema_version', '6102');
  variable_set('filefield_paths_schema_version', 6103);
  if ($schema_version < 5103 || $schema_version > 6000 && $schema_version < 6103) {
    db_add_field($ret = array(), 'files', 'origname', array(
      'description' => 'Original name of the file.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => '',
    ));
    $result = db_query("SELECT fid, filename FROM {files}");
    while ($file = db_fetch_object($result)) {
      db_query("UPDATE {files} SET origname = '%s' WHERE fid = %d", $file->filename, $file->fid);
    }
  }
  return array();
}