<?php

/**
 * Copyright (c) 2011 - 2013 fusionLib. All rights reserved.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package fusionLib
 */

/**
 * flTemplateDbInstaller Class
 *
 * Class to provide the functionality to install, upgrade and remove the database
 * tables optionally used by the template engine.
 *
 * @package fusionLib
 */
class flTemplateDbInstaller extends flDbInstallerInterface {

	/**
	 * Install or upgrade the database.
	 *
	 * @param flDb $db The database object through which to access the database.
	 */
	function install($db) {
		$this->createIfNotExists($db, 'fl_categories', "
CREATE  TABLE IF NOT EXISTS `fl_categories` (
  `catid` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `catname` VARCHAR(255) NOT NULL ,
  PRIMARY KEY (`catid`) ,
  UNIQUE INDEX `catname` (`catname` ASC) )
ENGINE = MyISAM
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci
");

		$this->createIfNotExists($db, 'fl_snippets', "
CREATE  TABLE IF NOT EXISTS `fl_snippets` (
  `snippetid` INT NOT NULL AUTO_INCREMENT ,
  `snippetname` VARCHAR(64) NOT NULL ,
  `description` VARCHAR(255) NOT NULL ,
  `catid` INT UNSIGNED NOT NULL ,
  `content` MEDIUMTEXT NOT NULL ,
  PRIMARY KEY (`snippetid`) ,
  UNIQUE INDEX `snippetname` (`snippetname` ASC) )
ENGINE = MyISAM
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci
");

		$this->createIfNotExists($db, 'fl_chunks', "
CREATE  TABLE IF NOT EXISTS `fl_chunks` (
  `chunkid` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `chunkname` VARCHAR(64) NOT NULL ,
  `themeid` INT UNSIGNED NOT NULL ,
  `catid` INT UNSIGNED NOT NULL ,
  `istemplate` TINYINT(1) NOT NULL ,
  `description` VARCHAR(255) NOT NULL ,
  `content` MEDIUMTEXT NOT NULL ,
  PRIMARY KEY (`chunkid`) ,
  UNIQUE INDEX `chunkname` (`themeid` ASC, `chunkname` ASC) )
ENGINE = MyISAM
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci
");

		$this->createIfNotExists($db, 'fl_themes', "
CREATE  TABLE IF NOT EXISTS `fl_themes` (
  `themeid` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `themename` VARCHAR(255) NOT NULL ,
  PRIMARY KEY (`themeid`) ,
  UNIQUE INDEX `name` (`themename` ASC) )
ENGINE = MyISAM
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci
");
	}

	/**
	 * Uninstall the database, all data is lost.
	 *
	 * @param flDb $db The database object through which to access the database.
	 */
	function uninstall($db) {
		$db->query('DROP TABLE IF EXISTS `fl_categories`');
		$db->query('DROP TABLE IF EXISTS `fl_snippets`');
		$db->query('DROP TABLE IF EXISTS `fl_chunks`');
		$db->query('DROP TABLE IF EXISTS `fl_themes`');
	}
}
