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

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

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

		return array(
			__('Editors', 'formsBackend') => 'editing',
			__('Notification Service Levels', 'formsBackend') => 'notifications'
		);
	}

	/**
	 * 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('cfForms.settings'))
			return null;

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

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

		$contact = new flFormElementSelect($form, 'contactlevel', $cfg->get('submission', 3), $levels);
		$contact->setLabel(__('Level for Form Submissions', 'formsBackend'));

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

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

		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Forms', 'formsBackend'),
				__('Notification Service Levels', 'formsBackend'),
				[
					'submission' => $cfg->get('submission')
				]
			);

			$cfg->submission = $contact->getValue();

			return true;
		}

		return null;
	}

	/**
	 * Implement content editing 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 editingSetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfForms.settings'))
			return null;

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

		$el = cfEditor::getAvailable(true);

		// Editor for intro and thanks text
		$htmlEditor = new flFormElementSelect($form, 'html_editor', $cfg->get('htmlEditor', ''), $el);
		$htmlEditor->setLabel(__('Editor for HTML', 'formsBackend'))
			->setDescription(__('This editor is used for editing the form intro and thank you copy.', 'formsBackend'));

		// Editor for HTML email
		$emailEditor = new flFormElementSelect($form, 'email_editor', $cfg->get('emailEditor', ''), $el);
		$emailEditor->setLabel(__('Editor for HTML Emails', 'formsBackend'));

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

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

		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Forms', 'formsBackend'),
				__('Editors', 'formsBackend'),
				[
					'htmlEditor' => $cfg->get('htmlEditor'),
					'htmlFilter' => $cfg->get('htmlFilter'),
					'emailEditor' => $cfg->get('emailEditor'),
					'emailFilter' => $cfg->get('emailFilter')
				]
			);

			$cfg->htmlEditor = $htmlEditor->getValue();
			$cfg->htmlFilter = cfEditor::getFilter($htmlEditor->getValue());
			$cfg->emailEditor = $emailEditor->getValue();
			$cfg->emailFilter = cfEditor::getFilter($emailEditor->getValue());

			return true;
		}

		return null;
	}
}
