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

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

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

		return array(
			__('Editing', 'testimonialsBackend') => 'editing',
			__('Notification Email', 'testimonialsBackend') => 'notify',
			__('Tiny Image', 'testimonialsBackend') => 'tthumbnail',
			__('Small Image', 'testimonialsBackend') => 'sthumbnail',
			__('Medium Image', 'testimonialsBackend') => 'mthumbnail',
			__('Large Image', 'testimonialsBackend') => 'large',
			__('Notification Service Levels', 'testimonialsBackend') => 'notifications'
		);
	}

	/**
	 * Removes all generated images.
	 */
	protected function clean() {
		if(!clearFusionCMS::getUser()->hasPermission('cfTestimonials.settings'))
			return null;

		$cfg = clearFusionCMS::getSiteConfig('cfSystemFileStore');
		$path = $cfg->privateStorage . '/testimonials/' . clearFusionCMS::getSite() . '/';

		$top = glob($path . '*', GLOB_NOSORT | GLOB_ONLYDIR);
		foreach($top as $td) {
			$second = glob("$td/*", GLOB_NOSORT | GLOB_ONLYDIR);
			foreach($second as $sd) {
				$files = glob("$sd/*_*.*", GLOB_NOSORT);
				foreach($files as $f) {
					@unlink($f);
				}
				@rmdir($sd);
			}
			@rmdir($td);
		}
		@rmdir($path);

		clearFusionCMS::getCache()->flush();
	}

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

		$cfg = clearFusionCMS::getSiteConfig('cfTestimonials');
		$el = cfEditor::getAvailable(true);

		$html = new flFormElementCheckbox($form, 'htmlEditor', $cfg->get('htmlEditor', true));
		$html->setLabel(__('Use HTML Editor', 'testimonialsBackend'));

		$htmlEditor = new flFormElementSelect($form, 'html_editor', $cfg->get('editor', ''), $el);
		$htmlEditor->setLabel(__('Editor for HTML', 'testimonialsBackend'))
			->setDescription(__('Only used if Use HTML Editor is ticked above.', 'testimonialsBackend'));

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

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

		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Testimonials', 'testimonialsBackend'),
				__('Editing', 'testimonialsBackend'),
				[
					'htmlEditor' => $cfg->get('htmlEditor'),
					'editor' => $cfg->get('editor'),
					'filter' => $cfg->get('filter')
				]
			);

			$cfg->htmlEditor = $html->getValue();
			$cfg->editor = $htmlEditor->getValue();
			$cfg->filter = cfEditor::getFilter($htmlEditor->getValue());
			return true;
		}

		return null;
	}

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

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

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

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

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

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

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

		return null;
	}

	/**
	 * Implement tiny thumbnail 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 tthumbnailSetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfTestimonials.settings'))
			return null;

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

		$w = new flFormElementInteger($form, 'w', 10, null, $cfg->tw);
		$w->setLabel(__('Image Width', 'testimonialsBackend'))
			->setRequired()
			->addValidator(new flFormValidatorRequired());

		$h = new flFormElementInteger($form, 'h', 10, null, $cfg->th);
		$h->setLabel(__('Image Height', 'testimonialsBackend'))
			->setRequired()
			->addValidator(new flFormValidatorRequired());

		$crop = new flFormElementCheckbox($form, 'crop', $cfg->tcrop);
		$crop->setLabel(__('Crop Image to Size', 'testimonialsBackend'))
			->setDescription(__('If checked then the image will be scaled and cropped to the exact width and height; if not checked the image will be scaled to fit within the given dimensions.', 'testimonialsBackend'));

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

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

		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Testimonials', 'testimonialsBackend'),
				__('Tiny Image', 'testimonialsBackend'),
				[
					'tw' => $cfg->get('tw'),
					'th' => $cfg->get('th'),
					'tcrop' => $cfg->get('tcrop')
				]
			);
			$cfg->tw = $w->getValue();
			$cfg->th = $h->getValue();
			$cfg->tcrop = $crop->getValue();

			$this->clean();
			return true;
		}

		return null;
	}

	/**
	 * Implement small thumbnail 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 sthumbnailSetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfTestimonials.settings'))
			return null;

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

		$w = new flFormElementInteger($form, 'w', 10, null, $cfg->sw);
		$w->setLabel(__('Image Width', 'testimonialsBackend'))
			->setRequired()
			->addValidator(new flFormValidatorRequired());

		$h = new flFormElementInteger($form, 'h', 10, null, $cfg->sh);
		$h->setLabel(__('Image Height', 'testimonialsBackend'))
			->setRequired()
			->addValidator(new flFormValidatorRequired());

		$crop = new flFormElementCheckbox($form, 'crop', $cfg->scrop);
		$crop->setLabel(__('Crop Image to Size', 'testimonialsBackend'))
			->setDescription(__('If checked then the image will be scaled and cropped to the exact width and height; if not checked the image will be scaled to fit within the given dimensions.', 'testimonialsBackend'));

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

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

		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Testimonials', 'testimonialsBackend'),
				__('Small Image', 'testimonialsBackend'),
				[
					'sw' => $cfg->get('sw'),
					'sh' => $cfg->get('sh'),
					'scrop' => $cfg->get('scrop')
				]
			);
			$cfg->sw = $w->getValue();
			$cfg->sh = $h->getValue();
			$cfg->scrop = $crop->getValue();

			$this->clean();
			return true;
		}

		return null;
	}

	/**
	 * Implement medium thumbnail 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 mthumbnailSetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfTestimonials.settings'))
			return null;

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

		$w = new flFormElementInteger($form, 'w', 10, null, $cfg->mw);
		$w->setLabel(__('Image Width', 'testimonialsBackend'))
			->setRequired()
			->addValidator(new flFormValidatorRequired());

		$h = new flFormElementInteger($form, 'h', 10, null, $cfg->mh);
		$h->setLabel(__('Image Height', 'testimonialsBackend'))
			->setRequired()
			->addValidator(new flFormValidatorRequired());

		$crop = new flFormElementCheckbox($form, 'crop', $cfg->mcrop);
		$crop->setLabel(__('Crop Image to Size', 'testimonialsBackend'))
			->setDescription(__('If checked then the image will be scaled and cropped to the exact width and height; if not checked the image will be scaled to fit within the given dimensions.', 'testimonialsBackend'));

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

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

		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Testimonials', 'testimonialsBackend'),
				__('Medium Image', 'testimonialsBackend'),
				[
					'mw' => $cfg->get('mw'),
					'mh' => $cfg->get('mh'),
					'mcrop' => $cfg->get('mcrop')
				]
			);
			$cfg->mw = $w->getValue();
			$cfg->mh = $h->getValue();
			$cfg->mcrop = $crop->getValue();

			$this->clean();
			return true;
		}

		return null;
	}

	/**
	 * Implement large image 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 largeSetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfTestimonials.settings'))
			return null;

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

		$w = new flFormElementInteger($form, 'w', 10, null, $cfg->lw);
		$w->setLabel(__('Image Width', 'testimonialsBackend'))
			->setRequired()
			->addValidator(new flFormValidatorRequired());

		$h = new flFormElementInteger($form, 'h', 10, null, $cfg->lh);
		$h->setLabel(__('Image Height', 'testimonialsBackend'))
			->setRequired()
			->addValidator(new flFormValidatorRequired());

		$crop = new flFormElementCheckbox($form, 'crop', $cfg->lcrop);
		$crop->setLabel(__('Crop Image to Size', 'testimonialsBackend'))
			->setDescription(__('If checked then the image will be scaled and cropped to the exact width and height; if not checked the image will be scaled to fit within the given dimensions.', 'testimonialsBackend'));

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

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

		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Testimonials', 'testimonialsBackend'),
				__('Large Image', 'testimonialsBackend'),
				[
					'lw' => $cfg->get('lw'),
					'lh' => $cfg->get('lh'),
					'lcrop' => $cfg->get('lcrop')
				]
			);
			$cfg->lw = $w->getValue();
			$cfg->lh = $h->getValue();
			$cfg->lcrop = $crop->getValue();

			$this->clean();
			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('cfTestimonials.settings'))
			return null;

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

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

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

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

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

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

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

			return true;
		}

		return null;
	}
}
