<?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 clearFusionCMS\module\comments
 * @copyright Copyright (c) 2013 - 2016 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * commentsInstall Class
 *
 * Class for managing the comments module installation.
 *
 * @package clearFusionCMS\module\comments
 */
class commentsInstall implements cfInstallInterface {
	/**
	 * The last error message.
	 *
	 * @var string
	 */
	protected $lastError = '';

	/**
	 * Return the last error.
	 *
	 * @return string The last error string.
	 */
	function getLastError() {
		return $this->lastError;
	}

	/**
	 * Create the tables for the site.
	 *
	 * @param flDb $db The database object.
	 * @param string $site The site name.
	 */
	function createTables($db, $site) {
		// Create the database tables
		$db->query("CREATE  TABLE IF NOT EXISTS `cf_comments_{$site}` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `commentdate` DATETIME NOT NULL ,
  `rating` TINYINT NOT NULL ,
  `published` TINYINT NOT NULL ,
  `contentid` INT UNSIGNED NOT NULL ,
  `modulename` VARCHAR(64) NOT NULL ,
  `author` VARCHAR(255) NOT NULL ,
  `email` VARCHAR(255) NOT NULL ,
  `content` MEDIUMTEXT NOT NULL ,
  PRIMARY KEY (`id`) ,
  INDEX `commentdate` (`commentdate` DESC) ,
  INDEX `ctntidx` (`modulename` DESC, `contentid` DESC) ,
  INDEX `published` (`published` ASC ) )
ENGINE = MyISAM
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci
");
	}

	/**
	 * Delete the tables.
	 *
	 * @param flDb $db The database object.
	 * @param string $site The name of the site to delete the table.
	 */
	function deleteTables($db, $site) {
		$db->query("DROP TABLE IF EXISTS cf_comments_{$site}");
	}

	/**
	 * Install the module.
	 *
	 * @param flDb The database object to use.
	 * @return bool true if installed; else false.
	 */
	function install($db) {

		// Add to all sites
		$siteMgr = new cfSiteManager($db);
		$sites = $siteMgr->listSites();
		foreach($sites as $site => $n) {
			$this->createTables($db, $site);
		}

		// Create permissions
		$ac = new flAcMgr($db);
		$ac->createPermissionGroup('cfComments', __noop('Comments'), 'commentsBackend'); // 5100
		$ac->createPermission('cfComments.post', __noop('Can post and manage comments.'), 5100, 'commentsBackend');
		$ac->createPermission('cfComments.settings', __noop('Can edit comment settings.'), 5110, 'commentsBackend');

		// Add to dashboard
		$dmgr = new cfDashboardMgr($db);
		$dmgr->addIcon('cfContent', 'cfComments', 'cfComments.post', __noop('Comments'), __noop('Manage comments.'), 'system/modules/comments/images/comments.svg', 'comments', 'dashboard', 'commentsBackend');

		// Capture events
		$em = clearFusionCMS::getEventManager();
		$em->addListener('system.moduleRemoved', 'commentsEvents', 'onModuleRemoved');

		return true;
	}

	/**
	 * Uninstall the module.
	 *
	 * @param flDb The database object to use.
	 * @return bool true if uninstalled; else false.
	 */
	function uninstall($db) {
		// Release events
		$em = clearFusionCMS::getEventManager();
		$em->removeListener('system.moduleRemoved', 'commentsEvents', 'onModuleRemoved');

		// Remove from dashboard
		$dmgr = new cfDashboardMgr($db);
		$dmgr->delIcon('cfComments');

		// Remove permissions
		$ac = new flAcMgr($db);
		$ac->deletePermissionGroup('cfComments');

		// Delete the tables
		$siteMgr = new cfSiteManager($db);
		$sites = $siteMgr->listSites();
		foreach($sites as $site => $n) {
			$this->deleteSite($db, $site);
		}

		// Tell everyone this module has been removed
		clearFusionCMS::getEventManager()->notify(new flEvent('system.moduleRemoved', array(
			'module' => 'comments'
			)));

		return true;
	}

	/**
	 * Upgrade the module.
	 *
	 * @param flDb The database object to use.
	 * @param string $installedVer The installed version.
	 * @return bool true if upgraded; else false.
	 */
	function upgrade($db, $installedVer) {

		if(version_compare($installedVer, '2.1.0') < 0) {
			$dmgr = new cfDashboardMgr($db);
			$dmgr->setIconFile('cfComments', 'system/modules/comments/images/comments.svg');
		}
		return true;
	}

	/**
	 * Create a new site.
	 *
	 * @param flDb $db The database object.
	 * @param string $site The short name of the site.
	 * @return bool true if the site was created; else false.
	 */
	function createSite($db, $site) {
		$this->createTables($db, $site);

		$siteMgr = new cfSiteManager($db);
		$cfg = $siteMgr->getSiteCfg('cfComments', $site);
		$cfg->notifyEmail = '';

		return true;
	}

	/**
	 * Delete a site.
	 *
	 * @param flDb $db The database object.
	 * @param string $site The name of the site to delete.
	 * @return bool true if the site was deleted; else false.
	 */
	function deleteSite($db, $site) {
		$this->deleteTables($db, $site);

		$siteMgr = new cfSiteManager($db);
		$cfg = $siteMgr->getSiteCfg('cfComments', $site);
		$cfg->deleteNamespace();
		$cfg = $siteMgr->getSiteCfg('commentsSystemNotify', $site);
		$cfg->deleteNamespace();

		return true;
	}
}
