You are here

mobile_codes.install in Mobile Codes 6.2

Same filename and directory in other branches
  1. 5 mobile_codes.install
  2. 6 mobile_codes.install
  3. 7.2 mobile_codes.install

File

mobile_codes.install
View source
<?php

/**
 * @file
 */

/**
 * Implements hook_schema().
 */
function mobile_codes_schema() {
  $schema['mobile_codes_presets'] = array(
    'export' => array(
      'key' => 'name',
      'key name' => 'Name',
      'primary key' => 'name',
      'identifier' => 'preset',
      'default hook' => 'default_mobile_codes_preset',
      'api' => array(
        'owner' => 'mobile_codes',
        'api' => 'default_mobile_codes_presets',
        'minimum_version' => 2,
        'current_version' => 2,
      ),
    ),
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => '',
      ),
      'provider' => array(
        'type' => 'varchar',
        'length' => 255,
        'description' => '',
      ),
      'defaults' => array(
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
        'serialize' => TRUE,
        'description' => '',
      ),
    ),
    'unique keys' => array(
      'name' => array(
        'name',
      ),
    ),
    'primary key' => array(
      'name',
    ),
  );
  $schema['mobile_codes_providers'] = array(
    'export' => array(
      'key' => 'name',
      'key name' => 'Name',
      'primary key' => 'name',
      'identifier' => 'provider',
      'default hook' => 'default_mobile_codes_provider',
      'api' => array(
        'owner' => 'mobile_codes',
        'api' => 'default_mobile_codes_providers',
        'minimum_version' => 2,
        'current_version' => 2,
      ),
    ),
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'description' => '',
      ),
      'url' => array(
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
        'description' => '',
      ),
      'parameters' => array(
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
        'serialize' => TRUE,
        'description' => '',
      ),
    ),
    'primary key' => array(
      'name',
    ),
    'unique keys' => array(
      'name' => array(
        'name',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function mobile_codes_install() {
  drupal_install_schema('mobile_codes');
}

/**
 * Implements hook_uninstall().
 */
function mobile_codes_uninstall() {
  drupal_uninstall_schema('mobile_codes');
  variable_del('mobile_codes_block_node_url_preset');
  variable_del('mobile_codes_block_user_vcard_preset');
  variable_del('mobile_codes_settings');
}

/**
 * Implements hook_update_N().
 */
function mobile_codes_update_6200() {
  $ret = array();

  // Remove 'mobile_codes' table.
  db_drop_table($ret, 'mobile_codes');

  // Modify 'mobile_codes_presets' table and data.
  db_drop_field($ret, 'mobile_codes_presets', 'pid');
  db_add_field($ret, 'mobile_codes_presets', 'provider', array(
    'type' => 'varchar',
    'length' => 255,
    'description' => '',
  ));
  db_change_field($ret, 'mobile_codes_presets', 'data', 'defaults', array(
    'type' => 'text',
    'size' => 'medium',
    'not null' => TRUE,
    'serialize' => TRUE,
    'description' => '',
  ));
  $results = db_query('SELECT * FROM {mobile_codes_presets} WHERE provider IS NULL');
  while ($result = db_fetch_object($results)) {
    $old = unserialize($result->defaults);
    $provider = "nokia_{$old['type']}";
    $name = drupal_strtolower($result->name);
    $size = $old['type'] = 'qr' ? array(
      's' => 2,
      'm' => 4,
      'l' => 6,
    ) : array(
      's' => 0.12,
      'm' => 0.18,
      'l' => 0.25,
    );
    $defaults = serialize(array(
      'size' => $size[strtolower($old['size'])],
    ));
    $ret[] = _mobile_codes_update_sql("UPDATE {mobile_codes_presets} SET name = '{$name}', provider = '{$provider}', defaults = '%s' WHERE name = '{$result->name}'", $defaults);
  }

  // Add 'mobile_codes_providers' table.
  db_create_table($ret, 'mobile_codes_providers', array(
    'fields' => array(
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'description' => '',
      ),
      'url' => array(
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
        'description' => '',
      ),
      'parameters' => array(
        'type' => 'text',
        'size' => 'medium',
        'not null' => TRUE,
        'serialize' => TRUE,
        'description' => '',
      ),
    ),
    'primary key' => array(
      'name',
    ),
    'unique keys' => array(
      'name' => array(
        'name',
      ),
    ),
  ));

  // Delete variables.
  variable_del('mobile_codes_flush');
  return $ret;
}

/**
 * Perform an SQL query and return success or failure.
 *
 * @see http://api.drupal.org/api/function/update_sql/6
 *
 * @param $sql
 *   A string containing a complete SQL query.
 * @return
 *   An array containing the keys:
 *      success: a boolean indicating whether the query succeeded
 *      query: the SQL query executed, passed through check_plain()
 *      rows: number of rows effected
 */
function _mobile_codes_update_sql($sql) {
  $args = func_get_args();
  array_shift($args);
  $result = db_query($sql, $args);
  $sql = _mobile_codes_return_query_string($sql, $args);
  return array(
    'success' => $result !== FALSE,
    'query' => check_plain($sql),
    'rows' => db_affected_rows(),
  );
}

/**
 * Builds a basic query, returns string.
 *
 * @see http://api.drupal.org/api/function/db_query/6
 *
 * @param $query
 *   A string containing an SQL query.
 * @param ...
 *   A variable number of arguments which are substituted into the query
 *   using printf() syntax. Instead of a variable number of query arguments,
 *   you may also pass a single array containing the query arguments.
 *
 *   Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
 *   in '') and %%.
 *
 *   NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
 *   and TRUE values to decimal 1.
 *
 * @return
 *   String of query that would have been run
 */
function _mobile_codes_return_query_string($query) {
  $args = func_get_args();
  array_shift($args);
  $query = db_prefix_tables($query);
  if (isset($args[0]) and is_array($args[0])) {

    // 'All arguments in one array' syntax
    $args = $args[0];
  }
  _db_query_callback($args, TRUE);
  $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
  return $query;
}

Functions

Namesort descending Description
mobile_codes_install Implements hook_install().
mobile_codes_schema Implements hook_schema().
mobile_codes_uninstall Implements hook_uninstall().
mobile_codes_update_6200 Implements hook_update_N().
_mobile_codes_return_query_string Builds a basic query, returns string.
_mobile_codes_update_sql Perform an SQL query and return success or failure.