<?php
  // $Id: views.install,v 1.20 2006/06/25 18:24:56 merlinofchaos Exp $

  function views_install() {
    drupal_set_message('Installing views');
    switch ($GLOBALS['db_type']) {
      case 'mysqli':
      case 'mysql':
      db_query("CREATE TABLE if not exists {view_view} (
        vid int(10) unsigned NOT NULL default '0',
        name varchar(32) NOT NULL UNIQUE,
        description varchar(255),
        access varchar(255),
        -- page fields
        page int(1),
        page_title varchar(255),
        page_header longtext,
        page_header_format int(4) NOT NULL,
        page_empty longtext,
        page_empty_format int(4) NOT NULL,
        page_footer longtext,
        page_footer_format int(4) NOT NULL,
        page_type varchar(20),
        use_pager int(1),
        nodes_per_page int(5),
        url varchar(255),
        -- menu fields
        menu int(1),
        menu_tab int(1),
        menu_tab_default int(1),
        menu_tab_weight int(4),
        menu_title varchar(255),
        -- block fields
        block int(1),
        block_title varchar(255),
        block_use_page_header int(1),
        block_header longtext,
        block_header_format int(4) NOT NULL,
        block_use_page_footer int(1),
        block_footer longtext,
        block_footer_format int(4) NOT NULL,
        block_use_page_empty int(1),
        block_empty longtext,
        block_empty_format int(4) NOT NULL,
        block_type varchar(20),
        nodes_per_block int(5),
        block_more int(1),
        -- breadcrumb
        breadcrumb_no_home int(1), -- true means start breadcrumb trail from 'Home'.
        -- other
        changed int(11),
        query longtext,
        countquery longtext,
        view_args_php longtext,
        PRIMARY KEY (vid),
        KEY (name)
      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");

      db_query("CREATE TABLE if not exists {view_sort} (
        vid int(10) unsigned NOT NULL default '0',
        position int(2),
        field varchar(255),
        sortorder varchar(5),
        options varchar(255),
        tablename varchar(255),
        KEY (vid)
      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");

      db_query("CREATE TABLE if not exists {view_argument} (
        vid int(10) unsigned NOT NULL default '0',
        type varchar(255),
        argdefault varchar(255),
        title varchar(255),
        options varchar(255),
        position int(2),
        wildcard varchar(32),
        wildcard_substitution varchar(32),
        KEY (vid)
      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");

      db_query("CREATE TABLE if not exists {view_tablefield} (
        vid int(10) unsigned NOT NULL default '0',
        tablename varchar(255),
        field varchar(255),
        label varchar(255),
        handler varchar(255),
        sortable int(1),
        defaultsort varchar(5),
        options varchar(255),
        position int(2),
        KEY (vid)
      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");

      db_query("CREATE TABLE if not exists {view_filter} (
        vid int(10) unsigned NOT NULL default '0',
        tablename varchar(255),
        field varchar(255),
        value longtext,
        operator varchar(20),
        options varchar(255),
        position int(2),
        KEY (vid)
      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");

      db_query("CREATE TABLE if not exists {view_exposed_filter} (
        vid int(10) unsigned NOT NULL default '0',
        field varchar(255),
        label varchar(255),
        optional int(1),
        is_default int(1),
        operator int(1),
        single int(1),
        position int(2),
        KEY (vid)
      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");

      $success = TRUE;
      break;
    case 'pgsql':
      db_query("CREATE TABLE {view_view} (
        vid SERIAL,
        name varchar(32) NOT NULL UNIQUE,
        description varchar(255),
        access varchar(255),
        -- page fields
        page smallint,
        page_title varchar(255),
        page_header text,
        page_header_format smallint NOT NULL,
        page_empty text,
        page_empty_format smallint NOT NULL,
        page_footer text,
        page_footer_format smallint NOT NULL,
        page_type varchar(20),
        use_pager smallint,
        nodes_per_page smallint, 
        url varchar(255),
        -- menu fields
        menu smallint,
        menu_tab smallint,
        menu_tab_default smallint,
        menu_tab_weight smallint,
        menu_title varchar(255),
        -- block fields
        block smallint,
        block_title varchar(255),
        block_use_page_header smallint,
        block_header text,
        block_header_format smallint NOT NULL,
        block_use_page_footer smallint,
        block_footer text,
        block_footer_format smallint NOT NULL,
        block_use_page_empty smallint,
        block_empty text,
        block_empty_format smallint NOT NULL,
        block_type varchar(20),
        nodes_per_block smallint,
        block_more smallint,
        -- breadcrumb
        breadcrumb_no_home smallint, -- true means start breadcrumb trail from 'Home'.
        -- other
        changed integer,
        query text,
        countquery text,
        view_args_php text,
        PRIMARY KEY (vid)
      )");
      db_query("CREATE INDEX {view_view}_name_idx ON {view_view} (name)");

      db_query("CREATE TABLE {view_sort} (
        vid integer NOT NULL default '0',
        position smallint,
        field varchar(255),
        sortorder varchar(5),
        options varchar(255),
        tablename varchar(255)
      )");
      db_query("CREATE INDEX {view_sort}_vid_idx ON {view_sort} (vid)");

      db_query("CREATE TABLE {view_argument} (
        vid integer NOT NULL default '0',
        type varchar(255),
        argdefault varchar(255),
        title varchar(255),
        options varchar(255),
        position smallint
        wildcard varchar(32),
        wildcard_substitution varchar(32),
      )");
      db_query("CREATE INDEX {view_argument}_vid_idx ON {view_argument} (vid)");

      db_query("CREATE TABLE {view_tablefield} (
        vid integer NOT NULL default '0',
        tablename varchar(255),
        field varchar(255),
        label varchar(255),
        handler varchar(255),
        sortable smallint,
        defaultsort varchar(5),
        options varchar(255),
        position smallint
      )");
      db_query("CREATE INDEX {view_tablefield}_vid_idx ON {view_tablefield} (vid)");

      db_query("CREATE TABLE {view_filter} (
        vid integer NOT NULL default '0',
        tablename varchar(255),
        field varchar(255),
        value text,
        operator varchar(20),
        options varchar(255),
        position smallint
      )");
      db_query("CREATE INDEX {view_filter}_vid_idx ON {view_filter} (vid)");
      db_query("CREATE TABLE {view_exposed_filter} (
        vid integer NOT NULL default '0',
        field varchar(255),
        label varchar(255),
        optional smallint,
        is_default smallint,
        operator smallint,
        single smallint,
        position smallint
      )");
      db_query("CREATE INDEX {view_exposed_filter}_vid_idx ON {view_exposed_filter} (vid)");

      $success = TRUE;
      break;
  } // End case

  if ($success) {
    drupal_set_message(t('Views module installed tables successfully.'));
  }
  else {
    drupal_set_message(t('The installation of views module was unsuccessful.'), 'error');
  }
}

/*
 * update module for UTF.
 */
function views_update_1() {
  return _system_update_utf8(array('view_view', 'view_tablefield', 'view_filter', 'view_sort', 'view_argument'));
}

/**
 * Dummy function cause we're apparently not to skip update #s.
 */
function views_update_2() {
// dummy function
  return array();
}
/*
 * update module for exposed filters.
 */
function views_update_3() {
  $ret = array();
//  db_add_column($ret, 'view_view', 'page_empty', 'longtext');
//  db_add_column($ret, 'view_view', 'page_empty_format', 'int(4)', array('not null' => TRUE));
//  db_add_column($ret, 'view_view', 'block_use_page_empty', 'int(1)');
//  db_add_column($ret, 'view_view', 'block_empty', 'longtext');
//  db_add_column($ret, 'view_view', 'block_empty_format', 'int(4)', array('not null' => TRUE));
  switch($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
    $ret[] = update_sql("CREATE TABLE if not exists {view_exposed_filter} (
        vid int(10) unsigned NOT NULL default '0',
        field varchar(255),
        label varchar(255),
        optional int(1),
        is_default int(1),
        operator int(1),
        single int(1),
        position int(2),
        KEY (vid)
      ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      $ret[] = update_sql("CREATE TABLE {view_exposed_filter} (
        vid numeric(10) unsigned NOT NULL default '0',
        field varchar(255),
        label varchar(255),
        optional numeric(1),
        is_default numeric(1),
        operator numeric(1),
        single numeric(1),
        position numeric(2),
      )");
      $ret[] = update_sql("CREATE INDEX {view_exposed_filter}_vid_idx ON {view_exposed_filter} (vid)");
      break;
  }
  return $ret;
}

function views_update_4() {
  $ret = array();
  db_add_column($ret, 'view_view', 'page_footer', 'longtext');
  db_add_column($ret, 'view_view', 'page_footer_format', 'int(4)', array('default' => 0));
  db_add_column($ret, 'view_view', 'block_use_page_footer', 'int(1)');
  db_add_column($ret, 'view_view', 'block_footer', 'longtext');
  db_add_column($ret, 'view_view', 'block_footer_format', 'int(4)', array('default' => 0));
  db_add_column($ret, 'view_view', 'page_empty', 'longtext');
  db_add_column($ret, 'view_view', 'page_empty_format', 'int(4)', array('default' => 0));
  db_add_column($ret, 'view_view', 'block_use_page_empty', 'int(1)');
  db_add_column($ret, 'view_view', 'block_empty', 'longtext');
  db_add_column($ret, 'view_view', 'block_empty_format', 'int(4)', array('default' => 0));
  return $ret;
}

function views_update_5() {
  $ret = array();
  db_add_column($ret, 'view_view', 'view_args_php', 'longtext');
  return $ret;
}

function views_update_6() {
  $ret = array();
  db_add_column($ret, 'view_tablefield', 'options', 'varchar(255)');
  return $ret;
}

function views_update_7() {
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {view_argument} MODIFY type varchar(255)");
  return $ret;
}

function views_update_8() {
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {view_view} MODIFY nodes_per_page int(5)");
  return $ret;
}

function views_update_9() {
  $ret = array();
  db_add_column($ret, 'view_argument', 'wildcard', 'varchar(32)');
  db_add_column($ret, 'view_argument', 'wildcard_substitution', 'varchar(32)');
  return $ret;
}