<?php

/**
 * 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
 * @copyright Copyright (c) 2007 - 2013 fusionLib. All rights reserved.
 * @link http://fusionlib.com
 */

/**
 * flDbInstallerInterface Class
 *
 * Class for use by core library function database installers.
 *
 * @package fusionLib
 */
abstract class flDbInstallerInterface {
	/**
	 * Create a table if it does not exist.
	 *
	 * @param flDb $db The database object to access the database through.
	 * @param string $table The name of the table to test for.
	 * @param string $sql The SQL to create the table.
	 * @return bool true if the table didn't exist or false if it already exists.
	 * @throws flDbException
	 */
	function createIfNotExists($db, $table, $sql) {
		$tables = $db->fetchTables();
		if(!in_array($table, $tables)) {
			if(!$db->query($sql))
				throw new flDbException("Error: " . $db->errorString());
			return true;
		}
		else
			return false;
	}

	/**
	 * Install or upgrade the database.
	 *
	 * @param flDb $db The database object through which to access the database.
	 */
	abstract function install($db);

	/**
	 * Uninstall the database, all data is lost.
	 *
	 * @param flDb $db The database object through which to access the database.
	 */
	abstract function uninstall($db);
}

/**
 * flDbInstaller Class
 *
 * Class to install, uninstall and update database tables used by fusionLib.
 *
 * @package fusionLib
 */
abstract class flDbInstaller {

	/**
	 * Install or upgrade database tables for modules.
	 *
	 * @param flDb $db The database object through which to access the database.
	 * @param array $what If null then the database tables are installed/upgraded for all modules
	 * otherwise it is a list of the module names e.g. 'Ac', 'Openid' to install/upgrade.
	 */
	static function install($db, $what = null) {

		// If list of what to install not given then get a list of all core libs
		if($what === null) {
			$folders = glob(FLLIBPATH . '*', GLOB_NOSORT | GLOB_ONLYDIR);
			$what = array();
			foreach($folders as $f)
				$what[] = basename($f);
		}

		// Try loading and installing each
		foreach($what as $l) {
			$clsName = "fl{$l}DbInstaller";
			if(is_readable(FLLIBPATH . "$l/$clsName.php")) {
				$cls = new $clsName();
				$cls->install($db);
			}
		}
	}

	/**
	 * Uninstall the database tables for modules, all data is lost.
	 *
	 * @param flDb $db The database object through which to access the database.
	 * @param array $what If null then the database tables are removed for all modules
	 * otherwise it is a list of the module names e.g. 'Ac', 'Openid' to uninstall.
	 */
	static function uninstall($db, $what = null) {

		// If list of what to install not given then get a list of all core libs
		if($what === null) {
			$folders = glob(FLLIBPATH . '*', GLOB_NOSORT | GLOB_ONLYDIR);
			$what = array();
			foreach($folders as $f)
				$what[] = basename($f);
		}

		// Try loading and installing each
		foreach($what as $l) {
			$clsName = "fl{$l}DbInstaller";
			if(is_readable(FLLIBPATH . "$l/$clsName.php")) {
				$cls = new $clsName();
				$cls->uninstall($db);
			}
		}
	}
}
