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

/**
 * contentbuilderSettings Class
 *
 * Class to implement settings for ContentBuilder editor.
 *
 * @package clearFusionCMS\module\contentbuilder
 */
class contentbuilderSettings implements cfModuleSettingsInterface {

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

		return array(
			__('Style Sheets', 'contentbuilderBackend') => 'stylesheets',
			__('Blocks', 'contentbuilderBackend') => 'blocks'
		);
	}

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

		$cfg = new flConfigDb('cfContentBuilder', $db);

		$ionicons = new flFormElementCheckbox($form, 'ionicons', $cfg->get('ionicons', true));
		$ionicons->setLabel(__('Auto include Ionicons', 'contentbuilderBackend'));

		$elemcss = new flFormElementCheckbox($form, 'elemcss', $cfg->get('elemcss', true));
		$elemcss->setLabel(__('Auto include Element CSS', 'contentbuilderBackend'))
			->setDescription(__('Or include in a Less document with @import \'contentbuilder/elements.less\';
', 'contentbuilderBackend'));

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

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

		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('ContentBuilder', 'contentbuilderBackend'),
				__('Style Sheets', 'contentbuilderBackend'),
				[
					'ionicons' => $cfg->get('ionicons'),
					'elemcss' => $cfg->get('elemcss')
				]
			);

			$cfg->ionicons = $ionicons->getValue();
			$cfg->elemcss = $elemcss->getValue();

			// Capture events
			$em = clearFusionCMS::getEventManager();
			$em->removeListener('system.beforePageRender', 'contentbuilderEvents', 'onBeforePageRender');
			
			if($ionicons->getValue() || $elemcss->getValue()) {
				$em->addListener('system.beforePageRender', 'contentbuilderEvents', 'onBeforePageRender');
			}

			return true;
		}

		return null;
	}

	/**
	 * Implement additional blocks.
	 *
	 * @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 blocksSetting($form, $db) {
		if(!clearFusionCMS::getUser()->hasPermission('cfContentBuilder.settings'))
			return null;

		$cfg = new flConfigDb('cfContentBuilder', $db);

		$chunks = new flFormElementText($form, 'chunks', $cfg->get('chunks', ''));
		$chunks->setLabel(__('Additional Blocks', 'contentbuilderBackend'))
			->setDescription(__('Comma separated list of chunks to include.', 'contentbuilderBackend'))
			->addValidator(new flFormValidatorRegex('/^(([a-zA-Z0-9_]+)\s*,?\s*)*$/'));

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

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

		if($form->isSubmitted() && $form->isValid()) {
			cfSettingsAudit::update(
				__('ContentBuilder', 'contentbuilderBackend'),
				__('Blocks', 'contentbuilderBackend'),
				[
					'chunks' => $cfg->get('chunks')
				]
			);
			$cfg->chunks = $chunks->getValue();

			return true;
		}

		return null;
	}
}
