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

/**
 * commentsSettings Class
 *
 * Class to implement settings for the comments system.
 *
 * @package clearFusionCMS\module\comments
 */
class commentsSettings implements cfModuleSettingsInterface {

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

		return array(
			__('Notification Email', 'commentsBackend') => 'notify',
			__('Notification Service Levels', 'commentsBackend') => 'notifications'
		);
	}

	/**
	 * Implement notification email 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 notifySetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfComments.settings'))
			return null;

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

		$email = new flFormElementEmail($form, 'email', $cfg->get('notifyEmail'));
		$email->setLabel(__('Notification Email Address', 'commentsBackend'))
			->setDescription(__('If given a notification email will be sent to this address for each comment submitted.', 'commentsBackend'));

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

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

		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Comments', 'commentsBackend'),
				__('Notification Email', 'commentsBackend'),
				[
					'notifyEmail' => $cfg->get('notifyEmail')
				]
			);

			$cfg->notifyEmail = $email->getValue();
			return true;
		}

		return null;
	}

	/**
	 * Implement editing of notification levels.
	 *
	 * @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 notificationsSetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfComments.settings'))
			return null;

		$levels = array(0 => __('Disable Notification', 'commentsBackend'), 1 => __('1 (Least Important)', 'commentsBackend'), 2 => '2', 3 => '3', 4 => '4', 5 => __('5 (Most Important)', 'commentsBackend'));

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

		$sub = new flFormElementSelect($form, 'sublevel', $cfg->get('sub', 3), $levels);
		$sub->setLabel(__('Level for Comment Submissions', 'commentsBackend'));

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

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

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

		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Comments', 'commentsBackend'),
				__('Notification Service Levels', 'commentsBackend'),
				[
					'sub' => $cfg->get('sub'),
					'mentions' => $cfg->get('mentions')
				]
			);

			$cfg->sub = $sub->getValue();
			$cfg->mentions = $mention->getValue();

			return true;
		}

		return null;
	}
}
