CREATE TABLE PermissionConfig (
  PermissionConfigId int(11) NOT NULL auto_increment,
  PermissionName varchar(30) NOT NULL default '',
  Description varchar(255) NOT NULL default '',
  ErrorMessage varchar(255) NOT NULL default '',
  ModuleId varchar(20) NOT NULL default '0',
  PRIMARY KEY  (PermissionConfigId),
  KEY PermissionName (PermissionName)
);

CREATE TABLE Permissions (
  PermissionId int(11) NOT NULL auto_increment,
  Permission varchar(255) NOT NULL default '',
  GroupId int(11) default '0',
  PermissionValue int(11) NOT NULL default '0',
  `Type` tinyint(4) NOT NULL default '0',
  CatId int(11) NOT NULL default '0',
  PRIMARY KEY  (PermissionId),
  UNIQUE KEY PermIndex (Permission,GroupId,CatId,`Type`)
);

CREATE TABLE CustomField (
  CustomFieldId int(11) NOT NULL auto_increment,
  Type int(11) NOT NULL default '0',
  FieldName varchar(255) NOT NULL default '',
  FieldLabel varchar(40) default NULL,
  Heading varchar(60) default NULL,
  Prompt varchar(60) default NULL,
  ElementType varchar(50) NOT NULL default '',
  ValueList varchar(255) default NULL,
  DisplayOrder int(11) NOT NULL default '0',
  OnGeneralTab tinyint(4) NOT NULL default '0',
  IsSystem tinyint(3) unsigned NOT NULL default '0',
  PRIMARY KEY  (CustomFieldId),
  KEY Type (Type)
);

CREATE TABLE ConfigurationAdmin (
  VariableName varchar(80) NOT NULL default '',
  heading varchar(255) default NULL,
  prompt varchar(255) default NULL,
  element_type varchar(20) NOT NULL default '',
  validation varchar(255) default NULL,
  ValueList text default NULL,
  DisplayOrder double NOT NULL default '0',
  GroupDisplayOrder double NOT NULL default '0',
  Install int(11) NOT NULL default '1',
  PRIMARY KEY  (VariableName)
);

CREATE TABLE ConfigurationValues (
  VariableId int(11) NOT NULL auto_increment,
  VariableName varchar(255) NOT NULL default '',
  VariableValue varchar(255) default NULL,
  ModuleOwner varchar(20) default 'In-Portal',
  Section varchar(255) NOT NULL default '',
  PRIMARY KEY  (VariableId),
  UNIQUE KEY VariableName (VariableName)
);

CREATE TABLE EmailMessage (
  EmailMessageId int(10) NOT NULL auto_increment,
  Template longtext,
  MessageType enum('html','text') NOT NULL default 'text',
  LanguageId int(11) NOT NULL default '0',
  EventId int(11) NOT NULL default '0',
  Subject text,
  PRIMARY KEY  (EmailMessageId)
);

CREATE TABLE EmailQueue (
  Subject text NOT NULL,
  toaddr text NOT NULL,
  fromaddr text NOT NULL,
  message blob,
  headers blob,
  queued timestamp NOT NULL
);

CREATE TABLE EmailSubscribers (
  EmailMessageId int(11) NOT NULL default '0',
  PortalUserId int(11) NOT NULL default '0'
);

CREATE TABLE Events (
  EventId int(11) NOT NULL auto_increment,
  Event varchar(40) NOT NULL default '',
  Enabled int(11) NOT NULL default '1',
  FromUserId int(11) NOT NULL default '0',
  Module varchar(40) NOT NULL default '',
  Description varchar(255) NOT NULL default '',
  Type int(11) NOT NULL default '0',
  PRIMARY KEY  (EventId)
);

CREATE TABLE IdGenerator (
  lastid int(11) default NULL
);

CREATE TABLE Language (
  LanguageId int(11) NOT NULL auto_increment,
  PackName varchar(40) NOT NULL default '',
  LocalName varchar(40) NOT NULL default '',
  Enabled int(11) NOT NULL default '0',
  PrimaryLang int(11) NOT NULL default '0',
  IconURL varchar(255) default NULL,
  DateFormat varchar(50) NOT NULL default '',
  TimeFormat varchar(50) NOT NULL default '',
  InputDateFormat varchar(50) NOT NULL default '',
  InputTimeFormat varchar(50) NOT NULL default '',
  DecimalPoint char(2) NOT NULL default '.',
  ThousandSep tinytext NULL,
  Charset varchar(20) NOT NULL default '',
  UnitSystem tinyint(4) NOT NULL default '1',
  PRIMARY KEY  (LanguageId)
);

CREATE TABLE Modules (
  Name varchar(255) NOT NULL default '',
  Path varchar(255) NOT NULL default '',
  Var varchar(10) NOT NULL default '',
  Version varchar(10) NOT NULL default '',
  Loaded tinyint(4) NOT NULL default '1',
  LoadOrder tinyint(4) NOT NULL default '0',
  TemplatePath varchar(255) NOT NULL default '',
  RootCat int(11) NOT NULL default '0',
  BuildDate double NOT NULL default '0',
  PRIMARY KEY  (Name)
);

CREATE TABLE PersistantSessionData (
  PortalUserId int(11) NOT NULL default '0',
  VariableName varchar(255) NOT NULL default '',
  VariableValue text NOT NULL,
  PRIMARY KEY  (PortalUserId,VariableName),
  KEY UserId (PortalUserId),
  KEY VariableName (VariableName)
);

CREATE TABLE Phrase (
  Phrase varchar(255) NOT NULL default '',
  Translation text NOT NULL default '',
  PhraseType int(11) NOT NULL default '0',
  PhraseId int(11) NOT NULL auto_increment,
  LanguageId int(11) NOT NULL default '0',
  LastChanged int(10) unsigned NOT NULL default '0',
  LastChangeIP varchar(15) NOT NULL default '',
  Module varchar(30) NOT NULL default '',
  PRIMARY KEY  (PhraseId),
  KEY LanguageId (LanguageId),
  INDEX Phrase_Index (Phrase)
);

CREATE TABLE PhraseCache (
  Template varchar(40) NOT NULL default '',
  PhraseList text NOT NULL,
  CacheDate int(11) NOT NULL default '0',
  ThemeId int(11) NOT NULL default '0',
  StylesheetId int(10) unsigned NOT NULL default '0',
  ConfigVariables text,
  PRIMARY KEY  (Template)
);

CREATE TABLE PortalGroup (
  GroupId int(11) NOT NULL auto_increment,
  Name varchar(255) NOT NULL default '',
  Description varchar(255) default NULL,
  CreatedOn double NOT NULL default '0',
  System tinyint(4) NOT NULL default '0',
  Personal tinyint(4) NOT NULL default '0',
  Enabled tinyint(4) NOT NULL default '1',
  ResourceId int(11) NOT NULL default '0',
  PRIMARY KEY  (GroupId),
  UNIQUE KEY Name (Name),
  UNIQUE KEY ResourceId (ResourceId),
  KEY Personal (Personal),
  KEY Enabled (Enabled)
);

CREATE TABLE PortalUser (
  PortalUserId int(11) NOT NULL auto_increment,
  Login varchar(255) default NULL,
  `Password` varchar(255) default NULL,
  FirstName varchar(255) default NULL,
  LastName varchar(255) default NULL,
  Company varchar(255) NOT NULL default '',
  Email varchar(255) NOT NULL default '',
  CreatedOn double NOT NULL default '0',
  Phone varchar(20) default NULL,
  Fax varchar(255) NOT NULL default '',
  Street varchar(255) default NULL,
  Street2 varchar(255) NOT NULL default '',
  City varchar(20) default NULL,
  State varchar(20) NOT NULL default '',
  Zip varchar(20) default NULL,
  Country varchar(20) NOT NULL default '',
  ResourceId int(11) NOT NULL default '0',
  `Status` tinyint(4) NOT NULL default '2',
  Modified int(11) NOT NULL default '0',
  dob double NOT NULL default '0',
  tz int(11) default NULL,
  ip varchar(20) default NULL,
  IsBanned tinyint(1) NOT NULL default '0',
  PassResetTime bigint(20) default NULL,
  PwResetConfirm varchar(255) default NULL,
  PwRequestTime bigint(25) default NULL,
  MinPwResetDelay int(11) NOT NULL default '1800',
  PRIMARY KEY  (PortalUserId),
  UNIQUE KEY ResourceId (ResourceId),
  UNIQUE KEY Login (Login),
  KEY CreatedOn (CreatedOn)
);

CREATE TABLE PortalUserCustomData (
  CustomDataId int(11) NOT NULL auto_increment,
  ResourceId int(10) unsigned NOT NULL default '0',
  KEY ResourceId (ResourceId),
  PRIMARY KEY  (CustomDataId)
);

CREATE TABLE SessionData (
  SessionKey varchar(50) NOT NULL default '',
  VariableName varchar(255) NOT NULL default '',
  VariableValue text NOT NULL,
  PRIMARY KEY  (SessionKey,VariableName),
  KEY SessionKey (SessionKey),
  KEY VariableName (VariableName)
);

CREATE TABLE Theme (
  ThemeId int(11) NOT NULL auto_increment,
  Name varchar(40) NOT NULL default '',
  Enabled int(11) NOT NULL default '1',
  Description varchar(255) default NULL,
  PrimaryTheme int(11) NOT NULL default '0',
  CacheTimeout int(11) NOT NULL default '0',
  StylesheetId int(10) unsigned NOT NULL default '0',
  PRIMARY KEY  (ThemeId)
);

CREATE TABLE ThemeFiles (
  FileId int(11) NOT NULL auto_increment,
  ThemeId int(11) NOT NULL default '0',
  FileName varchar(255) NOT NULL default '',
  FilePath varchar(255) NOT NULL default '',
  Description varchar(255) default NULL,
  FileType int(11) NOT NULL default '0',
  FileFound tinyint(3) unsigned NOT NULL default '0',
  PRIMARY KEY  (FileId),
  KEY theme (ThemeId),
  KEY FileName (FileName),
  KEY FilePath (FilePath),
  KEY FileFound (FileFound)
);

CREATE TABLE UserGroup (
  PortalUserId int(11) NOT NULL default '0',
  GroupId int(11) NOT NULL default '0',
  MembershipExpires int(10) unsigned default NULL,
  PrimaryGroup tinyint(4) NOT NULL default '1',
  ExpirationReminderSent tinyint(4) NOT NULL default '0',
  PRIMARY KEY  (PortalUserId,GroupId),
  KEY GroupId (GroupId),
  KEY PrimaryGroup (PrimaryGroup)
);

CREATE TABLE UserSession (
  SessionKey varchar(50) NOT NULL default '',
  CurrentTempKey varchar(50) default NULL,
  PrevTempKey varchar(50) default NULL,
  LastAccessed double NOT NULL default '0',
  PortalUserId varchar(255) NOT NULL default '',
  Language varchar(255) NOT NULL default '',
  Theme varchar(255) NOT NULL default '',
  GroupId int(11) NOT NULL default '0',
  IpAddress varchar(20) NOT NULL default '0.0.0.0',
  Status int(11) NOT NULL default '1',
  GroupList varchar(255) default NULL,
  tz int(11) default NULL,
  PRIMARY KEY  (SessionKey),
  KEY UserId (PortalUserId),
  KEY LastAccessed (LastAccessed)
);

CREATE TABLE EmailLog (
  EmailLogId int(11) NOT NULL auto_increment,
  fromuser varchar(200) default NULL,
  addressto varchar(255) default NULL,
  subject varchar(255) default NULL,
  timestamp bigint(20) default '0',
  event varchar(100) default NULL,
  PRIMARY KEY  (EmailLogId)
);

CREATE TABLE Cache (
  VarName varchar(255) NOT NULL default '',
  Data longtext,
  Cached int(11) default NULL,
  LifeTime int(11) NOT NULL default '-1',
  PRIMARY KEY  (VarName),
  KEY Cached (Cached)
);

CREATE TABLE StdDestinations (
  DestId int(11) NOT NULL auto_increment,
  DestType int(11) NOT NULL default '0',
  DestParentId int(11) default NULL,
  DestName varchar(255) NOT NULL default '',
  DestAbbr char(3) NOT NULL default '',
  DestAbbr2 char(2) default '',
  PRIMARY KEY  (DestId)
);

CREATE TABLE Category (
  CategoryId int(11) NOT NULL auto_increment,
  `Type` int(11) NOT NULL default '0',
  ParentId int(11) NOT NULL default '0',
  Name varchar(255) NOT NULL default '',
  l1_Name varchar(255) NOT NULL default '',
  l2_Name varchar(255) NOT NULL default '',
  l3_Name varchar(255) NOT NULL default '',
  l4_Name varchar(255) NOT NULL default '',
  l5_Name varchar(255) NOT NULL default '',
  Filename varchar(255) NOT NULL default '',
  AutomaticFilename tinyint(3) unsigned NOT NULL default '1',
  Description text NOT NULL,
  l1_Description text NOT NULL,
  l2_Description text NOT NULL,
  l3_Description text NOT NULL,
  l4_Description text NOT NULL,
  l5_Description text NOT NULL,
  CreatedOn int(11) NOT NULL default '0',
  EditorsPick tinyint(4) NOT NULL default '0',
  `Status` tinyint(4) NOT NULL default '0',
  Pop tinyint(4) default NULL,
  Priority int(11) NOT NULL default '0',
  MetaKeywords varchar(255) default NULL,
  CachedDescendantCatsQty int(11) default NULL,
  CachedNavbar text NOT NULL,
  l1_CachedNavbar text NOT NULL,
  l2_CachedNavbar text NOT NULL,
  l3_CachedNavbar text NOT NULL,
  l4_CachedNavbar text NOT NULL,
  l5_CachedNavbar text NOT NULL,
  CreatedById int(11) NOT NULL default '0',
  ResourceId int(11) default NULL,
  ParentPath text NOT NULL,
  NamedParentPath text NOT NULL,
  MetaDescription varchar(255) default NULL,
  HotItem int(11) NOT NULL default '2',
  NewItem int(11) NOT NULL default '2',
  PopItem int(11) NOT NULL default '2',
  Modified int(11) NOT NULL default '0',
  ModifiedById int(11) NOT NULL default '0',
  CategoryTemplate varchar(255) NOT NULL default '',
  CachedCategoryTemplate varchar(255) NOT NULL default '',
  PRIMARY KEY  (CategoryId),
  UNIQUE KEY ResourceId (ResourceId),
  KEY ParentId (ParentId),
  KEY Modified (Modified),
  KEY Priority (Priority),
  KEY sorting (Name,Priority),
  KEY Filename (Filename(5)),
  KEY l1_Name (l1_Name(5)),
  KEY l2_Name (l2_Name(5)),
  KEY l3_Name (l3_Name(5)),
  KEY l4_Name (l4_Name(5)),
  KEY l5_Name (l5_Name(5)),
  KEY l1_Description (l1_Description(5)),
  KEY l2_Description (l2_Description(5)),
  KEY l3_Description (l3_Description(5)),
  KEY l4_Description (l4_Description(5)),
  KEY l5_Description (l5_Description(5))
);

CREATE TABLE CategoryCustomData (
  CustomDataId int(11) NOT NULL auto_increment,
  ResourceId int(10) unsigned NOT NULL default '0',
  KEY ResourceId (ResourceId),
  PRIMARY KEY  (CustomDataId)
);

CREATE TABLE CategoryItems (
  `CategoryId` int(11) NOT NULL default '0',
  `ItemResourceId` int(11) NOT NULL default '0',
  `PrimaryCat` tinyint(4) NOT NULL default '0',
  `ItemPrefix` varchar(50) NOT NULL default '',
  `Filename` varchar(255) NOT NULL default '',
  UNIQUE KEY `CategoryId` (`CategoryId`,`ItemResourceId`),
  KEY `PrimaryCat` (`PrimaryCat`),
  KEY `ItemPrefix` (`ItemPrefix`),
  KEY `Filename` (`Filename`(4))
);

CREATE TABLE PermCache (
  PermCacheId int(11) NOT NULL auto_increment,
  CategoryId int(11) NOT NULL default '0',
  PermId int(11) NOT NULL default '0',
  ACL varchar(255) NOT NULL default '',
  DACL varchar(255) NOT NULL default '',
  PRIMARY KEY  (PermCacheId),
  KEY CategoryId (CategoryId),
  KEY PermId (PermId)
);

CREATE TABLE Stylesheets (
  StylesheetId int(11) NOT NULL auto_increment,
  Name varchar(255) NOT NULL default '',
  Description varchar(255) NOT NULL default '',
  AdvancedCSS text NOT NULL,
  LastCompiled int(10) unsigned NOT NULL default '0',
  Enabled int(11) NOT NULL default '0',
  PRIMARY KEY  (StylesheetId)
);

CREATE TABLE PopupSizes (
  PopupId int(10) unsigned NOT NULL auto_increment,
  TemplateName varchar(255) NOT NULL default '',
  PopupWidth int(11) NOT NULL default '0',
  PopupHeight int(11) NOT NULL default '0',
  PRIMARY KEY  (PopupId),
  KEY TemplateName (TemplateName)
);
