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

/**
 * contentbuilderInstall Class
 *
 * Module installer for content builder editor.
 *
 * @package clearFusionCMS\module\contentbuilder
 */
class contentbuilderInstall 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) {

		// Capture render events
		$em = clearFusionCMS::getEventManager();
		$em->addListener('system.beforeAdminPageRender', 'contentbuilderEvents', 'onBeforeAdminPageRender');
		$em->addListener('system.beforePageRender', 'contentbuilderEvents', 'onBeforePageRender');

		// Permissions
		$ac = new flAcMgr($db);
		$ac->createPermissionGroup('cfContentBuilder', __noop('ContentBuilder'), 'contentbuilderBackend'); // 6000
		$ac->createPermission('cfContentBuilder.settings', __noop('Can edit ContentBuilder editor settings.'), 6000, 'contentbuilderBackend');

		return true;
	}

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

		// Clean up
		$em = clearFusionCMS::getEventManager();
		$em->removeListener('system.beforeAdminPageRender', 'contentbuilderEvents', 'onBeforeAdminPageRender');
		$em->removeListener('system.beforePageRender', 'contentbuilderEvents', 'onBeforePageRender');

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

		$cfg = new flConfigDb('cfContentBuilder', $db);
		$cfg->deleteNamespace();

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

		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.8.8') < 0) {
			$em = clearFusionCMS::getEventManager();
			$em->addListener('system.beforeAdminPageRender', 'contentbuilderEvents', 'onBeforeAdminPageRender');
			$em->addListener('system.beforePageRender', 'contentbuilderEvents', 'onBeforePageRender');

			// Permissions
			$ac = new flAcMgr($db);
			$ac->createPermissionGroup('cfContentBuilder', __noop('ContentBuilder'), 'contentbuilderBackend'); // 6000
			$ac->createPermission('cfContentBuilder.settings', __noop('Can edit ContentBuilder editor settings.'), 6000, 'contentbuilderBackend');
		}

		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) {
		return true;
	}
}
