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

/**
 * membersInstall Class
 *
 * Installer class for members module.
 * @package clearFusionCMS\module\members
 */
class membersInstall 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;
	}

	/**
	 * Write the initial configuration data.
	 *
	 * @param flConfig $cfg The config object to write to.
	 */
	function writeConfig($cfg) {

		// Password reset email
		$cfg->pwdResetEmail = "You have requested that your password be reset for your account [[+username]] at [[+sitename]].

Please copy and paste the authorization code below into the password reset form:

[[+hash]]

If you have closed the browser then you can find the password reset form at: [[+reseturi]]

If you did not request your password be reset then please ignore this email.";
		$cfg->pwdResetEmailHTML = "<p>You have requested that your password be reset for your account [[+username]] at [[+sitename]].</p>
<p>Please copy and paste the authorization code below into the password reset form:</p>
<p>[[+hash]]</p>
<p>If you have closed the browser then you can find the password reset form at: <a href=\"[[+reseturi]]\">[[+reseturi]]</a></p>
<p>If you did not request your password be reset then please ignore this email.</p>";

		$cfg->pwdResetSubject = "Password recovery for your account at [[+sitename]]";

		// User activation email
		$cfg->activationEmail = "To activate your new account [[+username]] at [[+sitename]] please go to the following web page:

[[+activateuri]]

If you do not activate your account within 30 days it will be deleted.

If you did not request this account then please ignore this email.";

		$cfg->activationEmailHTML = "<p>To activate your new account [[+username]] at [[+sitename]] please go to the following web page:</p>
<p><a href=\"[[+activateuri]]\">[[+activateuri]]</a></p>
<p>If you do not activate your account within 30 days it will be deleted.</p>
<p>If you did not request this account then please ignore this email.</p>";
		$cfg->activationSubject = "Activate your account at [[+sitename]]";

		$cfg->newUserSubject = "New user registration at [[+sitename]]";
		$cfg->newUserEmailHTML = "<p>A new user [[+username]] [[+email]] has registered at [[+sitename]].</p>";
		$cfg->newUserEmail = "A new user [[+username]] [[+email]] has registered at [[+sitename]].";

		// Default roles
		$cfg->defaultRoles = serialize(array());

		// Default prune age in days
		$cfg->purneAfter = 30;
	}

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

		// Create configs for all sites
		$siteMgr = new cfSiteManager($db);
		$sites = $siteMgr->listSites();
		foreach($sites as $site => $n) {
			$cfg = new flConfigDb('cfMembers', $db, "cf_config_{$site}");
			$this->writeConfig($cfg);
		}

		// Capture role deletes
		$em = clearFusionCMS::getEventManager();
		$em->addListener('system.roleDeleted', 'membersEvents', 'onRoleDeleted');
		$em->addListener('system.cron1d', 'membersEvents', 'onCron1d');

		// Permissions
		$ac = new flAcMgr($db);
		$ac->createPermissionGroup('cfMembers', __noop('Members'), 'membersBackend'); // 6000
		$ac->createPermission('cfMembers.settings', __noop('Can edit members settings.'), 6000, 'membersBackend');

		return true;
	}

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

		// Remove events
		$em = clearFusionCMS::getEventManager();
		$em->removeListener('system.roleDeleted', 'membersEvents', 'onRoleDeleted');
		$em->removeListener('system.cron1d', 'membersEvents', 'onCron1d');

		// Delete configs for all sites
		$siteMgr = new cfSiteManager($db);
		$sites = $siteMgr->listSites();
		foreach($sites as $site => $n) {
			$cfg = new flConfigDb('cfMembers', $db, "cf_config_{$site}");
			$cfg->deleteNamespace();
		}

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

		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) {
		$ac = new flAcMgr($db);
		if(version_compare($installedVer, '1.1.0') < 0) {
			// Permissions
			$ac->createPermissionGroup('cfMembers', __noop('Members'), 'membersBackend'); // 6000
			$ac->createPermission('cfMembers.settings', __noop('Can edit members settings.'), 6000, 'membersBackend');
		}

		if(version_compare($installedVer, '1.2.0') < 0) {
			// Permissions
			$ac->updatePermissionGroup('cfMembers', __noop('Members'), 'membersBackend');
			$ac->updatePermission('cfMembers.settings', __noop('Can edit members settings.'), 'membersBackend');
		}

		if(version_compare($installedVer, '1.3.0') < 0) {
			$siteMgr = new cfSiteManager($db);
			$sites = $siteMgr->listSites();
			foreach($sites as $site => $n) {
				$cfg = new flConfigDb('cfMembers', $db, "cf_config_{$site}");
				$cfg->newUserSubject = "New user registration at [[+sitename]]";
				$cfg->newUserEmailHTML = "<p>A new user [[+username]] [[+email]] has registered at [[+sitename]].</p>";
				$cfg->newUserEmail = "A new user [[+username]] [[+email]] has registered at [[+sitename]].";
			}
		}
		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) {
		$cfg = new flConfigDb('cfMembers', $db, "cf_config_{$site}");
		$this->writeConfig($cfg);
		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) {
		$cfg = new flConfigDb('cfMembers', $db, "cf_config_{$site}");
		$cfg->deleteNamespace();
		return true;
	}
}
