You are here

filefield_paths.install in File (Field) Paths 5

File

filefield_paths.install
View source
<?php

/**
 * @file
 */

/**
 * Implementation of hook_install().
 */
function filefield_paths_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {filefield_paths} (\n        type varchar(32) NOT NULL default '',\n        field varchar(32) NOT NULL default '',\n        filename mediumtext NOT NULL,\n        filepath mediumtext NOT NULL\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      db_query("ALTER TABLE {files} ADD COLUMN origname varchar(255) NOT NULL default ''");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {filefield_paths} (\n        type varchar(32) NOT NULL default '',\n        field varchar(32) NOT NULL default '',\n        filename text NOT NULL,\n        filepath text NOT NULL\n      )");
      db_add_column($ret = array(), 'files', 'origname', 'varchar(255)', array(
        'not null' => TRUE,
        'default' => '',
      ));
      break;
  }
  $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', 5103);
}

/**
 * Implementation of hook_uninstall().
 */
function filefield_paths_uninstall() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("DROP TABLE IF EXISTS {filefield_paths}");
      break;
    case 'pgsql':
      db_query('DROP TABLE {filefield_paths}');
      break;
  }
  db_query("ALTER TABLE {files} DROP origname");
  variable_del('filefield_paths_schema_version');
}

/**
 * Implementation of hook_update_N().
 */
function filefield_paths_update_5100() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("CREATE TABLE {filefield_paths} (\n        type varchar(32) NOT NULL default '',\n        field varchar(32) NOT NULL default '',\n        filename mediumtext NOT NULL,\n        filepath mediumtext NOT NULL\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      break;
    case 'pgsql':
      $ret[] = update_sql("CREATE TABLE {filefield_paths} (\n        type varchar(32) NOT NULL default '',\n        field varchar(32) NOT NULL default '',\n        filename text NOT NULL,\n        filepath text NOT NULL\n      )");
      break;
  }
  $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 {node_field_instance} " . "WHERE field_name = '%s' AND type_name = '%s'", $field, $type));
      $widget_settings = unserialize($result->widget_settings);
      $field_data['file_path']['value'] = !empty($widget_settings['file_path']) ? $widget_settings['file_path'] : $widget_settings['image_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_5101() {
  variable_set('filefield_paths_schema_version', 5101);
  return array();
}
function filefield_paths_update_5102() {
  variable_set('filefield_paths_schema_version', 5102);
  $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_5103() {
  variable_set('filefield_paths_schema_version', 5103);
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("ALTER TABLE {files} ADD COLUMN origname varchar(255) NOT NULL default ''");
      break;
    case 'pgsql':
      db_add_column($ret = array(), 'files', 'origname', 'varchar(255)', array(
        'not null' => TRUE,
        'default' => '',
      ));
      break;
  }
  $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();
}