<?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) 2013 - 2017 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * socialSettings Class
 *
 * Class to implement settings for the social module.
 *
 * @package clearFusionCMS\module\social
 */
class socialSettings implements cfModuleSettingsInterface {

	/**
	 * Get a list of settings groups.
	 *
	 * @return array Array of setting groups, name => func name
	 */
	function getSettingsGroups() {
		if(!clearFusionCMS::getUser()->hasPermission('cfSocial.settings'))
			return array();

		return array(
			__('Facebook Comments', 'socialBackend') => 'fbcomments',
			__('Facebook Admins', 'socialBackend') => 'fbadmins',
		);
	}

	/**
	 * Implement facebook settings.
	 *
	 * @param flForm $form The form object to use.
	 * @param flDb $db The database object.
	 * @return bool true if the settings were updated, false if failed to update or null if no change.
	 */
	function fbcommentsSetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfSocial.settings'))
			return null;

		$cfg = clearFusionCMS::getSiteConfig('cfSocial');

		$email = new flFormElementEmail($form, 'email', $cfg->get('fbCommentNotify', ''));
		$email->setLabel(__('Email Address to Notify on Facebook Comments', 'socialBackend'))
			->setDescription(clearFusionCMS::getController()->alertMessage('Info', __('For this to work the social.fbComments snippet must not be cached i.e. &#91;&#91;!social.fbComments&#93;&#93;.', 'socialBackend')));

		$levels = array(0 => __('Disable Notification', 'socialBackend'), 1 => __('1 (Least Important)', 'socialBackend'), 2 => '2', 3 => '3', 4 => '4', 5 => __('5 (Most Important)', 'socialBackend'));
		$fbCmntSysNotify = new flFormElementSelect($form, 'syslevel', $cfg->get('fbCmntSysNotify', 3), $levels);
		$fbCmntSysNotify->setLabel(__('Notification Service Level', 'socialBackend'));

		$fbCmntSysMention = new flFormElementText($form, 'mentions', $cfg->get('fbCmntMentions', ''));
		$fbCmntSysMention->setLabel(__('Notification Service Mentions', 'socialBackend'))
			->addValidator(new flFormValidatorMaxLength(255))
			->setDescription(__('A list of usernames prefixed with @ to mention in notifications, usernames are defined by the notification service being used.', 'socialBackend'));

		$submit = new flFormElementSubmit($form, 'update');
		$submit->setCaption(__('Update Settings', 'socialBackend'))->setCSS('primary');

		$form->groupElements('submit', array($submit), new flFormDecoratorList(new flFormDecoratorDl()));
		$form->focusElement($email);

		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Social', 'socialBackend'),
				__('Facebook Comments', 'socialBackend'),
				[
					'fbCommentNotify' => $cfg->get('fbCommentNotify'),
					'fbCmntSysNotify' => $cfg->get('fbCmntSysNotify'),
					'fbCmntMentions' => $cfg->get('fbCmntMentions')
				]
			);
			$cfg->fbCommentNotify = $email->getValue();
			$cfg->fbCmntSysNotify = $fbCmntSysNotify->getValue();
			$cfg->fbCmntMentions = $fbCmntSysMention->getValue();
			return true;
		}

		return null;
	}

	/**
	 * Implement facebook admins.
	 *
	 * @param flForm $form The form object to use.
	 * @param flDb $db The database object.
	 * @return bool true if the settings were updated, false if failed to update or null if no change.
	 */
	function fbadminsSetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfSocial.settings'))
			return null;

		$cfg = clearFusionCMS::getSiteConfig('cfSocial');

		$admins = new flFormElementText($form, 'admins', $cfg->get('fbAdmins', ''));
		$admins->setLabel(__('Facebook Comments', 'socialBackend'))
			->setDescription(__('Comma separated list of Facebook Admin IDs.', 'socialBackend'));

		$submit = new flFormElementSubmit($form, 'update');
		$submit->setCaption(__('Update Settings', 'socialBackend'))->setCSS('primary');

		$form->groupElements('submit', array($submit), new flFormDecoratorList(new flFormDecoratorDl()));
		$form->focusElement($admins);

		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Social', 'socialBackend'),
				__('Facebook Admins', 'socialBackend'),
				[
					'fbAdmins' => $cfg->get('fbAdmins')
				]
			);
			$cfg->fbAdmins = $admins->getValue();
			return true;
		}

		return null;
	}
}
