

CREATE TABLE `{{db_prefix}}articles` (
  `id` int NOT NULL,
  `title` varchar(200) NOT NULL,
  `content` text,
  `category` int DEFAULT '0',
  `staff_id` int NOT NULL DEFAULT '0',
  `date` int NOT NULL,
  `last_update` int NOT NULL DEFAULT '0',
  `views` int NOT NULL DEFAULT '0',
  `public` int NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `{{db_prefix}}attachments` (
  `id` int NOT NULL,
  `name` varchar(200) NOT NULL,
  `enc` varchar(200) NOT NULL,
  `filetype` varchar(200) DEFAULT NULL,
  `article_id` int NOT NULL DEFAULT '0',
  `ticket_id` int NOT NULL DEFAULT '0',
  `msg_id` int NOT NULL DEFAULT '0',
  `filesize` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `{{db_prefix}}canned_response` (
  `id` int NOT NULL,
  `title` varchar(255) DEFAULT NULL,
  `message` mediumtext,
  `position` int NOT NULL DEFAULT '1',
  `date` int NOT NULL DEFAULT '0',
  `last_update` int NOT NULL DEFAULT '0',
  `staff_id` int NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `{{db_prefix}}custom_fields` (
  `id` int NOT NULL,
  `type` varchar(100) NOT NULL,
  `title` varchar(255) NOT NULL,
  `value` text CHARACTER SET utf8 COLLATE utf8_general_ci,
  `required` tinyint(1) NOT NULL DEFAULT '0',
  `departments` mediumtext,
  `display` int NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `{{db_prefix}}departments` (
  `id` int NOT NULL,
  `dep_order` int NOT NULL DEFAULT '0',
  `name` varchar(255) NOT NULL,
  `private` int NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `{{db_prefix}}emails_tpl` (
  `id` varchar(255) NOT NULL,
  `position` smallint NOT NULL,
  `name` varchar(255) NOT NULL,
  `subject` varchar(255) NOT NULL,
  `message` mediumtext NOT NULL,
  `last_update` int NOT NULL DEFAULT '0',
  `status` tinyint(1) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `{{db_prefix}}kb_category` (
  `id` int NOT NULL,
  `name` varchar(200) NOT NULL,
  `position` int NOT NULL,
  `parent` int NOT NULL DEFAULT '0',
  `public` int NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `{{db_prefix}}login_attempt` (
  `ip` varchar(200) NOT NULL,
  `attempts` int NOT NULL DEFAULT '0',
  `date` int NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `{{db_prefix}}login_log` (
  `id` int NOT NULL,
  `date` int NOT NULL,
  `staff_id` int NOT NULL DEFAULT '0',
  `ip` varchar(255) NOT NULL,
  `agent` varchar(255) NOT NULL,
  `success` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `{{db_prefix}}priority` (
  `id` int NOT NULL,
  `name` varchar(255) NOT NULL,
  `color` varchar(10) NOT NULL DEFAULT '#000000'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `{{db_prefix}}tickets` (
  `id` int NOT NULL,
  `department_id` int NOT NULL DEFAULT '0',
  `priority_id` int NOT NULL DEFAULT '0',
  `user_id` int NOT NULL DEFAULT '0',
  `subject` varchar(255) NOT NULL,
  `date` int NOT NULL DEFAULT '0',
  `last_update` int NOT NULL DEFAULT '0',
  `status` smallint NOT NULL DEFAULT '1',
  `replies` int NOT NULL DEFAULT '0',
  `last_replier` tinyint(1) DEFAULT '0',
  `custom_vars` mediumtext
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `{{db_prefix}}tickets_messages` (
  `id` int NOT NULL,
  `ticket_id` int NOT NULL DEFAULT '0',
  `date` int NOT NULL DEFAULT '0',
  `customer` int NOT NULL DEFAULT '1',
  `staff_id` int NOT NULL DEFAULT '0',
  `message` text,
  `ip` varchar(255) DEFAULT NULL,
  `email` tinyint(1) NOT NULL DEFAULT '1'
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE `{{db_prefix}}ticket_notes` (
  `id` int NOT NULL AUTO_INCREMENT,
  `ticket_id` int NOT NULL,
  `staff_id` int NOT NULL,
  `date` int NOT NULL,
  `message` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `{{db_prefix}}articles`
  ADD PRIMARY KEY (`id`),
  ADD KEY `category` (`category`);

ALTER TABLE `{{db_prefix}}attachments`
  ADD PRIMARY KEY (`id`),
  ADD KEY `article_id` (`article_id`),
  ADD KEY `ticket_id` (`ticket_id`),
  ADD KEY `msg_id` (`msg_id`);

ALTER TABLE `{{db_prefix}}canned_response`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `{{db_prefix}}custom_fields`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `{{db_prefix}}departments`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `{{db_prefix}}emails_tpl`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `{{db_prefix}}kb_category`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `{{db_prefix}}login_attempt`
  ADD UNIQUE KEY `ip` (`ip`);

ALTER TABLE `{{db_prefix}}login_log`
  ADD PRIMARY KEY (`id`),
  ADD KEY `staff_id` (`staff_id`);

ALTER TABLE `{{db_prefix}}priority`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `{{db_prefix}}tickets`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `{{db_prefix}}tickets_messages`
  ADD PRIMARY KEY (`id`),
  ADD KEY `ticket_id` (`ticket_id`);

ALTER TABLE `{{db_prefix}}articles`
  MODIFY `id` int NOT NULL AUTO_INCREMENT;

ALTER TABLE `{{db_prefix}}attachments`
  MODIFY `id` int NOT NULL AUTO_INCREMENT;

ALTER TABLE `{{db_prefix}}canned_response`
  MODIFY `id` int NOT NULL AUTO_INCREMENT;

ALTER TABLE `{{db_prefix}}custom_fields`
  MODIFY `id` int NOT NULL AUTO_INCREMENT;

ALTER TABLE `{{db_prefix}}departments`
  MODIFY `id` int NOT NULL AUTO_INCREMENT;

ALTER TABLE `{{db_prefix}}kb_category`
  MODIFY `id` int NOT NULL AUTO_INCREMENT;

ALTER TABLE `{{db_prefix}}login_log`
  MODIFY `id` int NOT NULL AUTO_INCREMENT;

ALTER TABLE `{{db_prefix}}priority`
  MODIFY `id` int NOT NULL AUTO_INCREMENT;

ALTER TABLE `{{db_prefix}}tickets`
  MODIFY `id` int NOT NULL AUTO_INCREMENT;

ALTER TABLE `{{db_prefix}}tickets_messages`
  MODIFY `id` int NOT NULL AUTO_INCREMENT;
INSERT INTO `{{db_prefix}}departments` (`id`, `dep_order`, `name`, `private`) VALUES
(1, 1, 'General', 0),
(2, 2, 'Advertising', 0),
(3, 3, 'Sales', 0);
INSERT INTO `{{db_prefix}}emails_tpl` (`id`, `position`, `name`, `subject`, `message`, `last_update`, `status`) VALUES
('autoresponse', 4, 'New Message Autoresponse', '[#%ticket_id%] %ticket_subject%', '<p>Dear %client_name%,</p>\r\n<p>Your reply to support request #%ticket_id% has been noted.</p>\r\n<p>Ticket Details <br />--------------------<br />Ticket ID: %ticket_id% <br />Department: %ticket_department% <br />Status: %ticket_status% <br />Priority: %ticket_priority% <br />Helpdesk: %support_url%</p>', 0, 0),
('lost_password', 2, 'Lost password confirmation', 'Password recovery for %company_name%', '<p>We have received a request to reset your account password for the %company_name% helpdesk (%helpdesk_url%).</p>\r\n<p>Your new passsword is: %client_password%</p>\r\n<p>Thank you, <br />%company_name% <br />Helpdesk: %support_url%</p>', 0, 2),
('new_ticket', 3, 'New ticket creation', '[#%ticket_id%] %ticket_subject%', '<p>Dear %client_name%,</p>\r\n<p>Thank you for contacting us. This is an automated response confirming the receipt of your ticket. One of our agents will get back to you as soon as possible.</p>\r\n<p>For your records, the details of the ticket are listed below. When replying, please make sure that the ticket ID is kept in the subject line to ensure that your replies are tracked appropriately.</p>\r\n<p>Ticket ID: %ticket_id% <br />Subject: %ticket_subject% <br />Department: %ticket_department% <br />Status: %ticket_status% <br />Priority: %ticket_priority%</p>\r\n<p>You can check the status of or reply to this ticket online at: %support_url%</p>\r\n<p>Regards, <br />%company_name%</p>', 0, 1),
('new_user', 1, 'Welcome email registration', 'Welcome to %company_name% helpdesk', '<p>Hello,</p>\r\n<p>This email is confirmation that you are now registered at our helpdesk.</p>\r\n<p><strong>Registered email:</strong> %client_email% <br /><strong>Password:</strong> %client_password%</p>\r\n<p>You can visit the helpdesk to browse articles and contact us at any time:</p>\r\n<p>%support_url%</p>\r\n<p>Thank you for registering!</p>\r\n<p>%company_name%<br />Helpdesk: %support_url%</p>', 0, 1),
('staff_reply', 5, 'Staff Reply', 'Re: [#%ticket_id%] %ticket_subject%', '<p>%message% </p>\r\n<p>-------------------------------------------------------------<br />Ticket Details<br />-------------------------------------------------------------<br /><strong>Ticket ID:</strong> %ticket_id% <br /><strong>Department:</strong> %ticket_department% <br /><strong>Status:</strong> %ticket_status% <br /><strong>Priority:</strong> %ticket_priority% <br /><strong>Helpdesk:</strong> %support_url%</p>', 0, 2),
('staff_ticketnotification', 6, 'New ticket notification to staff', 'New ticket notification', '<p>Dear %staff_name%,</p>\r\n<p>A new ticket has been created in department assigned for you, please login to staff panel to answer it.</p>\r\n<p>Ticket Details<br />-------------------<br />Ticket ID: %ticket_id% <br />Department: %ticket_department% <br />Status: %ticket_status% <br />Priority: %ticket_priority% <br />Helpdesk: %support_url%</p>', 0, 0);
INSERT INTO `{{db_prefix}}priority` (`id`, `name`, `color`) VALUES
(1, 'Low', '#8A8A8A'),
(2, 'Medium', '#000000'),
(3, 'High', '#F07D18'),
(4, 'Urgent', '#E826C6'),
(5, 'Emergency', '#E06161'),
(6, 'Critical', '#FF0000');