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

/**
 * socialInstall Class
 *
 * Class for managing the social module installation.
 *
 * @package clearFusionCMS\module\social
 */
class socialInstall 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 $db The database object to use.
	 * @return bool true if installed; else false.
	 */
	function install($db) {
		// Create permissions
		$ac = new flAcMgr($db);
		$ac->createPermissionGroup('cfSocial', __noop('Social'), 'socialBackend'); // 5000
		$ac->createPermission('cfSocial.settings', __noop('Can edit social module settings.'), 5010, 'socialBackend');
		$ac->createPermission('cfSocial.twitter', __noop('Can add and edit twitter feeds.'), 5020, 'socialBackend');

		// Add to dashboard
		$dmgr = new cfDashboardMgr($db);
		$dmgr->addIcon('cfResources', 'cfSocial.twitter', 'cfSocial.twitter', __noop('Twitter'), __noop('Manage linked twitter accounts.'), 'system/modules/social/images/twitter.svg', 'social', 'twitter', 'socialBackend');

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

		return true;
	}

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

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

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

		// 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' => 'social'
		)));

		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) {
			$ac->createPermissionGroup('cfSocial', __noop('Social'), 'socialBackend'); // 5000
			$ac->createPermission('cfSocial.settings', __noop('Can edit social module settings.'), 5010, 'socialBackend');
		}

		if(version_compare($installedVer, '1.2.0') < 0) {
			$ac->updatePermissionGroup('cfSocial', __noop('Social'), 'socialBackend');
			$ac->updatePermission('cfSocial.settings', __noop('Can edit social module settings.'), 'socialBackend');
		}

		if(version_compare($installedVer, '1.3.0') < 0) {
			$ac->createPermission('cfSocial.twitter', __noop('Can add and edit twitter feeds.'), 5020, 'socialBackend');

			// Add to dashboard
			$dmgr = new cfDashboardMgr($db);
			$dmgr->addIcon('cfResources', 'cfSocial.twitter', 'cfSocial.twitter', __noop('Twitter'), __noop('Manage linked twitter accounts.'), 'system/modules/social/images/twitter.svg', 'social', 'twitter', 'socialBackend');

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

		if(version_compare($installedVer, '1.4.1') < 0) {
			$dmgr = new cfDashboardMgr($db);
			$dmgr->setIconFile('cfSocial.twitter', 'system/modules/social/images/twitter.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) {
		$db->query("CREATE  TABLE IF NOT EXISTS `cf_socialtwitter_{$site}` (
  `feedid` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `screenname` VARCHAR(16) NOT NULL ,
  `tweetcount` TINYINT UNSIGNED NOT NULL ,
  `exreplies` TINYINT UNSIGNED NOT NULL ,
  `feedcache` MEDIUMTEXT NOT NULL ,
  PRIMARY KEY (`feedid`) ,
  UNIQUE INDEX `screenname` (`screenname` ASC) )
ENGINE = MyISAM
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci
");

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

		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('cfSocial', $site);
		$cfg->deleteNamespace();

		$db->query("DROP TABLE IF EXISTS cf_socialtwitter_{$site}");

		return true;
	}
}
