Your IP : 216.73.216.240


Current Path : /home/juvelize/www/administrator/components/com_ffgate/helpers/
Upload File :
Current File : /home/juvelize/www/administrator/components/com_ffgate/helpers/settings.php

<?php
/**
 * @package    FFGate
 * @author     Yves Hoppe <yves@compojoom.com>
 * @date       26.11.13
 *
 * @copyright  Copyright (C) 2008 - 2013 compojoom.com - Yves Hoppe. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE
 * @since      1.0.0
 */

defined('_JEXEC') or die ('Restricted access');

/**
 * Class FFGateHelperSettings
 *
 * @since  1.0.0
 */
class FFGateHelperSettings
{
	private static $instance;

	/**
	 * @param string $title
	 * @param string $default
	 *
	 * @return mixed
	 */
	public static function _($title = '', $default = '')
	{
		return FFGateHelperSettings::getSettings($title, $default);
	}

	/**
	 * @param  <type> $title - the name of the variable
	 * @param  <type> $eventlist - eventlist value
	 *
	 * @return <type>
	 */
	public static function getSettings($title = '', $default = '')
	{
		if (!isset(self::$instance))
		{
			self::$instance = self::_loadSettings();
		}
		return self::$instance->get($title, $default);
	}

	/**
	 *
	 * @return JObject - loads a singleton object with all settings
	 */
	private static function _loadSettings()
	{
		$db = JFactory::getDBO();
		$settings = new JObject();

		$query = ' SELECT st.title, st.value'
			. ' FROM #__ffgate_settings AS st'
			. ' ORDER BY st.id';

		$db->setQuery($query);
		$data = $db->loadObjectList();
		foreach ($data as $value)
		{
			$settings->set($value->title, $value->value);
		}

		// grab the settings from the menu and merge them in the object
		$app = JFactory::getApplication();
		$menu = $app->getMenu();
		if (is_object($menu))
		{
			if ($item = $menu->getActive())
			{
				$menuParams = $menu->getParams($item->id);
				foreach ($menuParams->toArray() as $key => $value)
				{
					if ($key == 'show_page_heading')
					{
						$key = 'show_page_title';
					}
					$settings->set($key, $value);
				}
			}
		}

		return $settings;
	}


	public static function getSettingField($value)
	{
		switch ($value->type)
		{
			case 'textarea':
				return FFGateHelperSettings::getTextareaSettings($value->id, $value->title, $value->value);
				break;

			case 'select':
				return FFGateHelperSettings::getSelectSettings($value->id, $value->title, $value->value, $value->values);
				break;

			case 'bool':
				return FFGateHelperSettings::getBoolField($value->id, $value->title, $value->value);
				break;

			case 'text':
			default:
				return FFGateHelperSettings::getTextSettings($value->id, $value->title, $value->value);
				break;
		}
	}

	/**
	 *
	 * @param  <type> $id
	 * @param  <type> $title
	 * @param  <type> $value
	 * @param  <type> $class
	 * @param  <type> $rows
	 * @param  <type> $cols
	 * @param  <type> $style
	 *
	 * @return <type>
	 */
	public static function getTextareaSettings($id, $title, $value, $class = 'text_area', $rows = 8, $cols = 50, $style = 'width:300px')
	{
		return '<textarea class="' . $class . '" name="ffgateset[' . $id . ']" id="ffgateset[' . $id
		. ']" rows="' . $rows . '" cols="' . $cols . '" style="' . $style . '" title="' . JText::_('COM_FFGATE_'
			. $title . '_DESC') . '" />' . $value . '</textarea>';
	}

	/**
	 * Generates a nice bootstrap ready (since 3.0) boolean select
	 *
	 * @param   int     $id     - The setting id
	 * @param   string  $title  - The title
	 * @param   string  $value  - The value
	 * @param   string  $class  - The additional class (btn = default)
	 * @param   string  $style  - The style
	 *
	 * @return  string - The html containing the radio boxes
	 */
	public static function getBoolField($id, $title, $value, $class = 'bool', $style = "width: 30px;")
	{
		$nosel = "";
		$ysel = "";

		if ($value == 1)
		{
			$ysel = " checked=\"checked\"";
		}

		if ($value == 0)
		{
			$nosel = " checked=\"checked\"";
		}

		$html = '<fieldset class="radio btn-group">';

		$html .= '<input type="radio" name="ffgateset[' . $id . ']" id="ffgateset_' . $id . '_yes" class="btn ' . $class . '" value="1" ' . $ysel . ' />'
			. '<label for="ffgateset_' . $id . '_yes">' . JText::_('COM_FFGATE_YES') . '</label>';

		$html .= '<input type="radio" name="ffgateset[' . $id . ']" id="ffgateset_' . $id . '_no" class="btn ' . $class . '" value="0" ' . $nosel . ' />'
			. '<label for="ffgateset_' . $id . '_no">' . JText::_('COM_FFGATE_NO') . '</label>';

		$html .= "</fieldset>";

		return $html;
	}

	/**
	 *
	 * @param <type> $id
	 * @param <type> $title
	 * @param <type> $value
	 * @param <type> $values
	 * @param <type> $class
	 * @param <type> $size
	 * @param <type> $maxlength
	 * @param <type> $style
	 *
	 * @return string
	 */
	public static function getSelectSettings($id, $title, $value, $values, $class = 'inputbox', $size = 50, $maxlength = 255, $style = 'width:300px')
	{

		$valuesArray = FFGateHelperSettings::getSettingsValues($values);

		$select = '<select name="ffgateset[' . $id . ']" id="ffgateset[' . $id . ']" class="' . $class . '">' . "\n";
		foreach ($valuesArray as $valueOption)
		{
			if ($value == $valueOption['id'])
			{
				$selected = 'selected="selected"';
			}
			else
			{
				$selected = '';
			}
			$text = strtoupper(str_replace(' ', '_', $valueOption['value']));
			$text = str_replace('(', '', $text);
			$text = str_replace(')', '', $text);
			$text = str_replace(':', '', $text);
			$text = str_replace('.', '', $text);
			$text = str_replace('-', '', $text);
			$text = str_replace('__', '_', $text);
			$select .= '<option value="' . $valueOption['id'] . '" ' . $selected . '>' . JText::_('COM_FFGATE_' . $text) . '</option>' . "\n";
		}
		$select .= '</select>' . "\n";

		return $select;
	}

	/**
	 *
	 * @param  <type> $id
	 * @param  <type> $title
	 * @param  <type> $value
	 * @param  <type> $class
	 * @param  <type> $size
	 * @param  <type> $maxlength
	 * @param  <type> $style
	 *
	 * @return <type>
	 */
	public static function getTextSettings($id, $title, $value, $class = 'text_area', $size = 50, $maxlength = 255, $style = 'width:300px')
	{

		return '<input class="' . $class . '" type="text" name="ffgateset[' . $id . ']"
            id="ffgateset[' . $id . ']" value="' . $value . '" size="' . $size . '"
            maxlength="' . $maxlength . '" style="' . $style . '" title="' .
		JText::_('COM_FFGATE_' . strtoupper($title) . '_DESC') . '" />';
	}

	/**
	 *
	 * @param  <type> $params
	 *
	 * @return <type>
	 */
	public static function getSettingsValues($params)
	{

		$regex_one = '/({\s*)(.*?)(})/si';
		$regex_all = '/{\s*.*?}/si';
		$matches = array();
		$count_matches = preg_match_all($regex_all, $params, $matches, PREG_OFFSET_CAPTURE | PREG_PATTERN_ORDER);

		$values = array();

		for ($i = 0; $i < $count_matches; $i++)
		{

			$ffgate = $matches[0][$i][0];
			preg_match($regex_one, $ffgate, $ffgateParts);
			$values_replace = array("/^'/", "/'$/", "/^&#39;/", "/&#39;$/", "/<br \/>/");
			$values = explode("=", $ffgateParts[2], 2);

			foreach ($values_replace as $key2 => $values2)
			{
				$values = preg_replace($values2, '', $values);
			}

			$returnValues[$i]['id'] = $values[0];
			$returnValues[$i]['value'] = $values[1];
		}

		return $returnValues;
	}


	/**
	 * Returns the generated html for displaying in Matukio settings
	 *
	 * @param   array  $sets  - The settings
	 *
	 * @return  string - The generated html code
	 */
	public static function getSettingsBlock($sets)
	{
		$i = 0;
		$split = (int) (count($sets) / 2) + 1;
		$html = "";

		foreach ($sets as $value)
		{
			$html .= '<div class="control-group">';
			$html .= '<label for="' . $value->title . '" title="' . JText::_('COM_FFGATE_'
					. strtoupper($value->title) . '_DESC') . '" class="hasTooltip control-label"
								data-original-title="' . JText::_('COM_FFGATE_' . strtoupper($value->title) . '_DESC')
				. '" />';
			$html .= JText::_('COM_FFGATE_' . strtoupper($value->title));
			$html .= '</label>';

			$html .= '<div class="controls">';

			switch ($value->type)
			{
				case 'textarea':
					$html .= self::getTextareaSettings($value->id, $value->title, $value->value);
					break;

				case 'select':
					$html .= self::getSelectSettings($value->id, $value->title, $value->value, $value->values);
					break;

				case 'bool':
					$html .= self::getBoolField($value->id, $value->title, $value->value);
					break;

				case 'text':
				default:
					$html .= self::getTextSettings($value->id, $value->title, $value->value);
					break;
			}

			$html .= '</div>';
			$html .= '</div>';

			if ($i == $split)
			{
				$html .= "</div>";
				$html .= "<div class=\"span6\">";
			}

			$i++;
		}

		return $html;
	}
}