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

/**
 * membersSettings Class
 *
 * Class to implement settings for clearFusionCMS members module.
 *
 * @package clearFusionCMS\module\members
 */
class membersSettings implements cfModuleSettingsInterface {

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

		return array(
			__('Password Email', 'membersBackend') => 'pwdemail',
			__('Activation Email', 'membersBackend') => 'activationemail',
			__('New User Email', 'membersBackend') => 'newemail',
			__('Default Roles', 'membersBackend') => 'defaultroles',
			__('Settings', 'membersBackend') => 'general'
		);
	}

	/**
	 * Implement editing the password reset email.
	 *
	 * @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 pwdemailSetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfMembers.settings'))
			return null;

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

		$subject = new flFormElementText($form, 'subject', $cfg->pwdResetSubject);
		$subject->setLabel(__('The Subject Line', 'membersBackend'))
			->setDescription(__('The subject line for activation emails. Use &#91;&#91;+sitename&#93;&#93; to include the site name.', 'membersBackend'))
			->setRequired()
			->addValidator(new flFormValidatorRequired())
			->addValidator(new flFormValidatorMaxLength(255));

		$emailhtml = new flFormElementWYSIWYG($form, 'emailhtml', $cfg->pwdResetEmailHTML);
		$emailhtml->setLabel(__('Email Body (HTML)', 'membersBackend'))
			->setRows(20)
			->editorSize('100%', '225')
			->setDB($db)
			->editorModule(clearFusionCMS::getController()->getEditorModule())
			->editorInternalLinks(clearFusionCMS::getController()->assemble('', 'browsectnt'))
			->editorFileBrowser(clearFusionCMS::getController()->assemble('', 'browsefiles'))
			->addValidator(new flFormValidatorMaxLength(8192));

		$email = new flFormElementWYSIWYG($form, 'email', $cfg->pwdResetEmail);
		$email->setLabel(__('Email Body (Text)', 'membersBackend'))
			->setDescription(__('The contents of the password reset email. Use &#91;&#91;+sitename&#93;&#93; to include the site name, &#91;&#91;+username&#93;&#93; for the username, &#91;&#91;+reseturi&#93;&#93; for the reset email and &#91;&#91;+hash&#93;&#93; for the authorization code.', 'membersBackend'))
			->setRows(10)
			->editorSize('100%', '225')
			->setRequired()
			->addValidator(new flFormValidatorRequired())
			->addValidator(new flFormValidatorMaxLength(8192))
			->setDB($db)
			->editorModule(clearFusionCMS::getController()->getCodeEditorModule())
			->editorContentType('text');

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

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

		$r = null;
		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Members', 'membersBackend'),
				__('Password Email', 'membersBackend'),
				[
					'pwdResetSubject' => $cfg->get('pwdResetSubject'),
					'pwdResetEmail' => $cfg->get('pwdResetEmail'),
					'pwdResetEmailHTML' => $cfg->get('pwdResetEmailHTML')
				]
			);

			$cfg->pwdResetSubject = $subject->getValue();
			$cfg->pwdResetEmail = $email->getValue();
			$cfg->pwdResetEmailHTML = $emailhtml->getValue();
			$r = true;
		}

		return $r;
	}

	/**
	 * Implement editing the activation email.
	 *
	 * @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 activationemailSetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfMembers.settings'))
			return null;

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

		$subject = new flFormElementText($form, 'subject', $cfg->activationSubject);
		$subject->setLabel(__('The Subject Line', 'membersBackend'))
			->setDescription(__('The subject line for activation emails. Use &#91;&#91;+sitename&#93;&#93; to include the site name.', 'membersBackend'))
			->setRequired()
			->addValidator(new flFormValidatorRequired())
			->addValidator(new flFormValidatorMaxLength(255));

		$emailhtml = new flFormElementWYSIWYG($form, 'emailhtml', $cfg->activationEmailHTML);
		$emailhtml->setLabel(__('Email Body (HTML)', 'membersBackend'))
			->setRows(20)
			->editorSize('100%', '225')
			->setDB($db)
			->editorModule(clearFusionCMS::getController()->getEditorModule())
			->editorInternalLinks(clearFusionCMS::getController()->assemble('', 'browsectnt'))
			->editorFileBrowser(clearFusionCMS::getController()->assemble('', 'browsefiles'))
			->addValidator(new flFormValidatorMaxLength(8192));

		$email = new flFormElementWYSIWYG($form, 'email', $cfg->activationEmail);
		$email->setLabel(__('Email Body (Text)', 'membersBackend'))
			->setDescription(__('The contents of the activation email. Use &#91;&#91;+sitename&#93;&#93; to include the site name, &#91;&#91;+username&#93;&#93; for the username and &#91;&#91;+activateuri&#93;&#93; for the activation page address.', 'membersBackend'))
			->setRows(10)
			->editorSize('100%', '225')
			->setRequired()
			->addValidator(new flFormValidatorRequired())
			->addValidator(new flFormValidatorMaxLength(8192))
			->setDB($db)
			->editorModule(clearFusionCMS::getController()->getCodeEditorModule())
			->editorContentType('text');

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

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

		$r = null;
		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Members', 'membersBackend'),
				__('Activation Email', 'membersBackend'),
				[
					'activationSubject' => $cfg->get('activationSubject'),
					'activationEmail' => $cfg->get('activationEmail'),
					'activationEmailHTML' => $cfg->get('activationEmailHTML')
				]
			);

			$cfg->activationSubject = $subject->getValue();
			$cfg->activationEmail = $email->getValue();
			$cfg->activationEmailHTML = $emailhtml->getValue();
			$r = true;
		}

		return $r;
	}

	/**
	 * Implement editing the new user email.
	 *
	 * @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 newemailSetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfMembers.settings'))
			return null;

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

		$subject = new flFormElementText($form, 'subject', $cfg->newUserSubject);
		$subject->setLabel(__('The Subject Line', 'membersBackend'))
			->setDescription(__('The subject line for new user emails. Use &#91;&#91;+sitename&#93;&#93; to include the site name.', 'membersBackend'))
			->setRequired()
			->addValidator(new flFormValidatorRequired())
			->addValidator(new flFormValidatorMaxLength(255));

		$emailhtml = new flFormElementWYSIWYG($form, 'emailhtml', $cfg->newUserEmailHTML);
		$emailhtml->setLabel(__('Email Body (HTML)', 'membersBackend'))
			->setRows(20)
			->editorSize('100%', '225')
			->setDB($db)
			->editorModule(clearFusionCMS::getController()->getEditorModule())
			->editorInternalLinks(clearFusionCMS::getController()->assemble('', 'browsectnt'))
			->editorFileBrowser(clearFusionCMS::getController()->assemble('', 'browsefiles'))
			->addValidator(new flFormValidatorMaxLength(8192));

		$email = new flFormElementWYSIWYG($form, 'email', $cfg->newUserEmail);
		$email->setLabel(__('Email Body (Text)', 'membersBackend'))
			->setDescription(__('The contents of the activation email. Use &#91;&#91;+sitename&#93;&#93; to include the site name, &#91;&#91;+username&#93;&#93; for the username and &#91;&#91;+email&#93;&#93; for the users email address.', 'membersBackend'))
			->setRows(10)
			->editorSize('100%', '225')
			->setRequired()
			->addValidator(new flFormValidatorRequired())
			->addValidator(new flFormValidatorMaxLength(8192))
			->setDB($db)
			->editorModule(clearFusionCMS::getController()->getCodeEditorModule())
			->editorContentType('text');

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

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

		$r = null;
		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Members', 'membersBackend'),
				__('New User Email', 'membersBackend'),
				[
					'newUserSubject' => $cfg->get('newUserSubject'),
					'newUserEmail' => $cfg->get('newUserEmail'),
					'newUserEmailHTML' => $cfg->get('newUserEmailHTML')
				]
			);

			$cfg->newUserSubject = $subject->getValue();
			$cfg->newUserEmail = $email->getValue();
			$cfg->newUserEmailHTML = $emailhtml->getValue();
			$r = true;
		}

		return $r;
	}

	/**
	 * Implement editing of the users default roles.
	 *
	 * @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 defaultrolesSetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfMembers.settings'))
			return null;

		$cfg = clearFusionCMS::getSiteConfig('cfMembers');
		$acMgr = new flAcMgr($db);

		$roles = new flFormElementChecklist($form, 'roles', unserialize($cfg->defaultRoles), $acMgr->getRoles());
		$roles->setLabel(__('Roles for New Members', 'membersBackend'));

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

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

		$r = null;
		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Members', 'membersBackend'),
				__('Default Roles', 'membersBackend'),
				[
					'defaultRoles' => $cfg->get('defaultRoles')
				]
			);
			$cfg->defaultRoles = serialize($roles->getValue());
			$r = true;
		}

		return $r;
	}

	/**
	 * Implement editing the general settings for the members module.
	 *
	 * @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 generalSetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfMembers.settings'))
			return null;

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

		$age = new flFormElementNumber($form, 'pruneage', null, null, $cfg->purneAfter);
		$age->setLabel(__('Prune Members After (days)', 'membersBackend'))
			->setDescription(__('Any members accounts not activated within this number of days are deleted from the system.', 'membersBackend'))
			->setRequired()
			->addValidator(new flFormValidatorRequired())
			->addValidator(new flFormValidatorInteger());

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

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

		$r = null;
		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Members', 'membersBackend'),
				__('Settings', 'membersBackend'),
				[
					'purneAfter' => $cfg->get('purneAfter')
				]
			);
			$cfg->purneAfter = $age->getValue();
			$r = true;
		}

		return $r;
	}
}
