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

/**
 * liveeditorSettings Class
 *
 * Class to implement settings for the live editor.
 *
 * @package clearFusionCMS\module\liveeditor
 */
class liveeditorSettings implements cfModuleSettingsInterface {

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

		return array(
			__('Features', 'liveeditorBackend') => 'features'
		);
	}

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

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

		$googleFonts = new flFormElementCheckbox($form, 'gfonts', $cfg->googleFonts);
		$googleFonts->setLabel(__('Enable Google Web Fonts', 'liveeditorBackend'))
			->setDescription(__('If ticked Google web fonts will be available in the editor. To enable web fonts in the published content you need to add the snippet &#91;&#91;liveeditor.webfonts&#93;&#93; to your templates.', 'liveeditorBackend'));

		$video = new flFormElementCheckbox($form, 'video', $cfg->html5video);
		$video->setLabel(__('Enable HTML5 Video', 'liveeditorBackend'))
			->setDescription(__('If ticked the ability to insert HTML5 video will be available in the editor. To enable video in the published content you need to add the snippet &#91;&#91;liveeditor.video&#93;&#93; to your templates.', 'liveeditorBackend'));

		$flickr = new flFormElementText($form, 'flikr', $cfg->flickrUser);
		$flickr->setLabel(__('flikr Username', 'liveeditorBackend'))
			->setDescription(__('If given then photos from this account will be made available to insert into content. To enable the "Open Larger" feature on Flickr images add the snippet &#91;&#91;liveeditor.flickr&#93;&#93;', 'liveeditorBackend'));

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

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

		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('Live Editor', 'liveeditorBackend'),
				__('Features', 'liveeditorBackend'),
				[
					'googleFonts' => $cfg->get('googleFonts'),
					'html5video' => $cfg->get('html5video'),
					'flickrUser' => $cfg->get('flickrUser')
				]
			);

			$cfg->googleFonts = $googleFonts->getValue();
			$cfg->html5video = $video->getValue();
			$cfg->flickrUser = $flickr->getValue();

			return true;
		}

		return null;
	}
}
