<?php
/**
 * @file
 *   Install file for devel module.
 */

/**
 * Implementation of hook_schema().
 */
function devel_schema() {
  $schema['devel_queries'] = array(
    'fields' => array(
      'qid' => array('type' => 'serial', 'not null' => TRUE, 'disp-width' => '10'),
      'function' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
      'query' => array('type' => 'text', 'not null' => TRUE),
      'hash' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => '')),
    'primary key' => array('hash'),
    'indexes' => array(
      'qid' => array('qid'))
  );
  $schema['devel_times'] = array(
    'fields' => array(
      'tid' => array('type' => 'serial', 'not null' => TRUE, 'disp-width' => '10'),
      'qid' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
      'time' => array('type' => 'float', 'not null' => FALSE)),
    'primary key' => array('tid'),
    'indexes' => array(
      'qid' => array('qid'))
  );
  return $schema;
}
 
 /**
 * Implementation of hook_install()
 */
function devel_install() {
  drupal_install_schema('devel');
     
  // New module weights in core: put devel as the very last in the chain.
  db_query("UPDATE {system} SET weight = 88 WHERE name = 'devel'");
  
  // Create our menu. See menu.install for an example.
  $t = get_t();
  db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('%s', '%s', '%s')", 'devel', $t('Development'), $t('Development links.'));
  
  $item = array(
    'link_title' => 'Run cron',
    'link_path' => 'admin/reports/status/run-cron',
    'menu_name' => 'devel',
    'module' => 'devel',
  );
  menu_link_save($item);
  
  $item = array(
    'link_title' => 'Devel settings',
    'link_path' => 'admin/settings/devel',
    'menu_name' => 'devel',
    'module' => 'devel',
  );
  menu_link_save($item);
}

 /**
 * Implementation of hook_uninstall().
 */
function devel_uninstall() {
  drupal_uninstall_schema('devel');
  variable_del('dev_query');
  variable_del('devel_old_smtp_library');
  variable_del('devel_form_weights');
  variable_del('devel_store_random');
  variable_del('devel_execution');
  variable_del('dev_timer');
  variable_del('devel_query_display');
  variable_del('devel_redirect_page');
  variable_del('devel_api_url');
  variable_del('dev_mem');
  variable_del('devel_error_handler');
  variable_del('devel_store_queries');
  variable_del('devel_switch_user_list_size');
  variable_del('devel_switch_user_show_form');
  
  db_query("DELETE FROM {menu_custom} WHERE menu_name = 'devel'");
  db_query("DELETE FROM {menu_links} WHERE module = 'devel'");
}

/**
 * Implementation of hook_disable().
 */
function devel_disable() {
  // Query logging should probably not be set if devel.module is disabled.
  if (variable_get('dev_query', 0)) {
    variable_set('dev_query', 0);
    drupal_set_message(t('Disabled query logging since devel module is disabled.'));
  }
  
  // The SMTP server should also be restored.
  variable_set('smtp_library', variable_get('devel_old_smtp_library', ''));
  variable_del('devel_old_smtp_library');
  
  // Same for storing queries
  variable_del('devel_store_queries');

  // Disable Devel Block
  db_query("UPDATE {blocks} SET status = %d WHERE module = '%s' AND delta = '%s'", 0, 'menu', 'devel');
}

/**
 * Do update 1 again as the hook_install() was missing and new
 * installations are not having the weight set.
 */
function devel_update_2() {
  // New module weights in core: put devel as the very last in the chain.
  $ret = array();
  $ret[] = update_sql('UPDATE {system} SET weight = 10 WHERE name = "devel"');
  return $ret;
}

function devel_update_3() {
  switch ($GLOBALS['db_type']) {
    case 'mysqli':
    case 'mysql':
      $sql = "CREATE TABLE {devel_queries} (
        qid int(10) NOT NULL auto_increment,
        query varchar(255) NOT NULL default '',
        hash varchar(255) NOT NULL default '',
        PRIMARY KEY (`hash`),
        KEY qid (qid)
        ) /*!40100 DEFAULT CHARACTER SET utf8 */;";
      $ret[] = update_sql($sql);

      $sql = "CREATE TABLE {devel_times} (
        tid int(10) NOT NULL auto_increment,
        qid int(10) NOT NULL default 0,
        time float default NULL,
        PRIMARY KEY (tid),
        KEY qid (qid)
      ) /*!40100 DEFAULT CHARACTER SET utf8 */;";
      $ret[] = update_sql($sql);
      return $ret;
  }
}

function devel_update_4() {
  $ret = array();
  
  db_add_field($ret, 'devel_queries', 'function', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  
  return $ret;
}

function devel_update_5() {
  $ret = array();
  
  db_change_field($ret, 'devel_queries', 'query', 'query text', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
  
  return $ret;
}

function devel_update_6001() {
  // Create our menu. See menu.install for an example.
  $ret[] = update_sql("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('devel', 'Development', 'Development links.')");
  return $ret;
}

function devel_update_6002() {
  $item = array(
    'link_title' => 'Run cron',
    'link_path' => 'admin/reports/status/run-cron',
    'menu_name' => 'devel',
    'module' => 'devel',
  );
  menu_link_save($item);
  return array();
}

/**
 * As per issue #813132: change schablon.com to white for krumo.
 */
function devel_update_6003() {
  if (variable_get('devel_krumo_skin', 'white') == 'schablon.com') {
    variable_set('devel_krumo_skin', 'white');
  }
  return array();
}
