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

/**
 * formsInstall Class
 *
 * Class for managing the forms module installation.
 *
 * @package clearFusionCMS\module\forms
 */
class formsInstall 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;
	}

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

		// Create the database table
		$db->query("CREATE  TABLE IF NOT EXISTS `cf_forms` (
  `formid` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `submitdelay` INT UNSIGNED NOT NULL ,
  `storeemails` TINYINT UNSIGNED NOT NULL ,
  `displayname` VARCHAR(255) NOT NULL ,
  `subject` VARCHAR(255) NOT NULL ,
  `recipient` VARCHAR(255) NOT NULL ,
  `mentions` VARCHAR(255) NOT NULL ,
  `intro` MEDIUMTEXT NOT NULL ,
  `thankyou` MEDIUMTEXT NOT NULL ,
  `htmlemail` MEDIUMTEXT NOT NULL ,
  `textemail` MEDIUMTEXT NOT NULL ,
  `formdata` MEDIUMTEXT NOT NULL ,
  PRIMARY KEY (`formid`) )
ENGINE = MyISAM
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci
");

		$db->query("CREATE  TABLE IF NOT EXISTS `cf_formssent` (
  `emailid` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `formid` INT UNSIGNED NOT NULL,
  `sentat` DATETIME NOT NULL,
  `email` VARCHAR(255) NOT NULL,
  `msg` MEDIUMTEXT NOT NULL,
   PRIMARY KEY (`emailid`),
   INDEX `formid` (`formid` ASC),
   INDEX `sentat` (`sentat` ASC) )
ENGINE = MyISAM
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci
");

		// Create permissions
		$ac = new flAcMgr($db);
		$ac->createPermissionGroup('cfForms', __noop('Forms'), 'formsBackend'); // 3000
		$ac->createPermission('cfForms.edit', __noop('Can access, create and edit forms.'), 3010, 'formsBackend');
		$ac->createPermission('cfForms.settings', __noop('Can edit forms settings.'), 3020, 'formsBackend');
		$ac->createPermission('cfForms.view', __noop('Can view form submissions.'), 3030, 'formsBackend');

		// Add to dashboard
		$dmgr = new cfDashboardMgr($db);
		$dmgr->addIcon('cfResources', 'cfForms.edit', 'cfForms.edit', __noop('Forms'), __noop('Manage forms.'), 'system/modules/forms/images/forms.svg', 'forms', 'editor', 'formsBackend');
		$dmgr->addIcon('cfResources', 'cfForms.view', 'cfForms.view', __noop('Form Emails'), __noop('View form emails.'), 'system/modules/forms/images/formsview.svg', 'forms', 'view', 'formsBackend');

		return true;
	}

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

		// Remove from dashboard
		$dmgr = new cfDashboardMgr($db);
		$dmgr->delIcon('cfForms.edit');
		$dmgr->delIcon('cfForms.view');

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

		// Delete the table
		$db->query("DROP TABLE IF EXISTS cf_forms");

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

		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, '1.1.0') < 0) {
			$dmgr = new cfDashboardMgr($db);
			$dmgr->setIconGroup('cfForms.edit', 'cfResources');

			$db->query("ALTER TABLE cf_forms ADD COLUMN submitdelay INT UNSIGNED NOT NULL AFTER formid");
		}

		$dmgr = new cfDashboardMgr($db);
		$ac = new flAcMgr($db);
		if(version_compare($installedVer, '1.2.0') < 0) {
			$ac->updatePermissionGroup('cfForms', __noop('Forms'), 'formsBackend');
			$ac->updatePermission('cfForms.edit', __noop('Can access, create and edit forms.'), 'formsBackend');

			// Add to dashboard
			$dmgr->setIconName('cfForms.edit', __noop('Forms'), 'formsBackend');
		}

		if(version_compare($installedVer, '1.3.0') < 0) {
			$ac->createPermission('cfForms.settings', __noop('Can edit forms settings.'), 3020, 'formsBackend');

			$db->query("ALTER TABLE cf_forms ADD COLUMN mentions VARCHAR(255) NOT NULL AFTER recipient");
		}

		if(version_compare($installedVer, '2.0.0') < 0) {
			$ac->createPermission('cfForms.view', __noop('Can view form submissions.'), 3030, 'formsBackend');
			$dmgr->addIcon('cfResources', 'cfForms.view', 'cfForms.view', __noop('Form Emails'), __noop('View form emails.'), 'system/modules/forms/images/formsview.svg', 'forms', 'view', 'formsBackend');

			$db->query("ALTER TABLE cf_forms ADD COLUMN storeemails TINYINT UNSIGNED NOT NULL AFTER submitdelay");
			$db->query("CREATE  TABLE IF NOT EXISTS `cf_formssent` (
  `emailid` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `formid` INT UNSIGNED NOT NULL,
  `sentat` DATETIME NOT NULL,
  `email` VARCHAR(255) NOT NULL,
  `msg` MEDIUMTEXT NOT NULL,
   PRIMARY KEY (`emailid`),
   INDEX `formid` (`formid` ASC),
   INDEX `sentat` (`sentat` ASC) )
ENGINE = MyISAM
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci
");
		}

		if(version_compare($installedVer, '2.0.2') < 0) {
			$dmgr = new cfDashboardMgr($db);
			$dmgr->setIconFile('cfForms.edit', 'system/modules/forms/images/forms.svg');
			$dmgr->setIconFile('cfForms.view', 'system/modules/forms/images/formsview.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) {
		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) {
		$siteMgr = new cfSiteManager($db);
		$cfg = $siteMgr->getSiteCfg('formsSystemNotify', $site);
		$cfg->deleteNamespace();

		$cfg = $siteMgr->getSiteCfg('formsSystemEdit', $site);
		$cfg->deleteNamespace();

		return true;
	}
}
