<?php
/**
 * JEvents Component for Joomla! 3.x
 *
 * @version     $Id: edit16.php 2983 2011-11-10 14:02:23Z geraintedwards $
 * @package     JEvents
 * @copyright   Copyright (C)  2008-2020 GWESystems Ltd
 * @license     GNU/GPLv2, see http://www.gnu.org/licenses/gpl-2.0.html
 * @link        http://www.jevents.net
 */
defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Language\Text;
use Joomla\CMS\Filter\InputFilter;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\String\StringHelper;

// We need to get the params first

HTMLHelper::_('formbehavior.chosen', '#adminForm select.chosen');

$version = JEventsVersion::getInstance();

$haslayouts = false;
foreach (JEV_CommonFunctions::getJEventsViewList() as $viewfile)
{
	$config = JPATH_SITE . "/components/" . JEV_COM_COMPONENT . "/views/" . $viewfile . "/config.xml";
	if (file_exists($config))
	{
		$haslayouts = true;
	}
}
$hasPlugins = false;
$db         = Factory::getDbo();
$query      = $db->getQuery(true)
	->select('folder AS type, element AS name, params, enabled, manifest_cache ')
	->from('#__extensions')
	// include unpublished plugins
	//->where('enabled = 1')
	->where('type =' . $db->quote('plugin'))
	->where('state IN (0,1)')
	->where('(folder="jevents" OR element="gwejson" OR element="jevent_embed")')
	->order('enabled desc, ordering asc');

$jevplugins = $db->setQuery($query)->loadObjectList();
//echo $db->getQuery();
//$jevplugins = PluginHelper::getPlugin("jevents");
if (count($jevplugins))
{
	$hasPlugins = true;
}
?>
<!-- Set Difficulty : -->
<div id="jevents">
<form action="index.php" method="post" name="adminForm" autocomplete="off" id="adminForm">
	<fieldset class='jevconfig'>
		<?php
		// difficulty rating is outside the tabs!
		$fieldSets = $this->form->getFieldsets();
		foreach ($fieldSets as $name => $fieldSet)
		{
			foreach ($this->form->getFieldset($name) as $field)
			{
				if ($field->fieldname == "com_difficulty")
				{
					?>
					<table class="settings_level">
						<tr class=" difficulty1 gsl-grid">
							<td class="gsl-width-auto"><span class="editlinktip"><?php echo $field->label;?></span></td>
							<td class="gsl-width-expand"><?php echo $field->input;?></td>
						</tr>
					</table>
					<?php
				}
			}
		}
		?>
		<legend>
			<?php echo Text::_('JEV_EVENTS_CONFIG'); ?>
		</legend>
<div class="gsl-grid  gsl-margin-remove-left">
		<ul class="config gsl-tab gsl-tab-left gsl-margin-right gsl-width-auto gsl-list-divider" id="myParamsTabs" gsl-tab="connect: #jvts-config-tabs">
			<?php
			$fieldSets = $this->form->getFieldsets();
			$first     = true;
			foreach ($fieldSets as $name => $fieldSet)
			{
				if ($name == "permissions")
				{
					continue;
				}
				$label = empty($fieldSet->label) ? $name : $fieldSet->label;

				$class = isset($fieldSet->class) ? $fieldSet->class : "";
				if (!empty($fieldSet->difficulty))
				{
					$difficultySetClass = "difficulty" . $fieldSet->difficulty;
					if ($this->component->params->get("com_difficulty", 1) < $fieldSet->difficulty)
					{
						$difficultySetClass .= " hiddenDifficulty";
					}
				}
				else
				{
					$difficultySetClass = "";
				}
				if ($first)
				{
					$first = false;
					$class = " class= 'active $class $difficultySetClass'";
				}
				else
				{
					$class = " class=' $difficultySetClass'";
				}
				?>
				<li <?php echo $class; ?>><a href="#<?php echo $name; ?>"><?php echo Text::_($label); ?></a></li>
				<?php
			}
			/*
			 * Drop Down tabs - but the drop down doesn't get cleared !
			  if ($haslayouts)
			  {
			  ?>
			  <li class="dropdown">
			  <a data-toggle="dropdown"  class="dropdown-toggle"  href="#club_layouts"><?php echo Text::_("CLUB_LAYOUTS"); ?>  <b class="caret"></b></a>
			  <ul class="dropdown-menu">
			  <?php
			  foreach (JEV_CommonFunctions::getJEventsViewList() as $viewfile)
			  {
			  $config = JPATH_SITE . "/components/" . JEV_COM_COMPONENT . "/views/" . $viewfile . "/config.xml";
			  if (file_exists($config))
			  {
			  ?>
			  <li ><a data-toggle="tab" href="#<?php echo $viewfile; ?>"><?php echo $viewfile; ?></a></li>
			  <?php
			  }
			  }
			  ?>
			  </ul>
			  </li>
			  <?php
			  }
			 */
			if ($haslayouts)
			{
				?>
				<li><a data-toggle="tab" href="#club_layouts"><?php echo Text::_("CLUB_LAYOUTS"); ?></a></li>
				<?php
			}
			if ($hasPlugins)
			{
				?>
				<li><a data-toggle="tab" href="#plugin_options"><?php echo Text::_("JEV_PLUGIN_OPTIONS"); ?></a></li>
				<?php
			}
			?>
		</ul>
        <!-- Tabs themselves //-->
		<div class=" gsl-margin-remove gsl-card-body gsl-card-default gsl-padding gsl-width-expand">
	        <ul class="gsl-switcher" id="jvts-config-tabs">
            <?php

		$fieldSets = $this->form->getFieldsets();

		foreach ($fieldSets as $name => $fieldSet)
		{
			if ($name == "permissions")
			{
				continue;
			}
			$label = empty($fieldSet->label) ? $name : $fieldSet->label;
            ?>
            <li>
                <?php

			$html = array();

			$html[] = '<div class="gsl-width-1-1" >';

			if (isset($fieldSet->description) && !empty($fieldSet->description))
			{
				$desc   = Text::_($fieldSet->description);
				$html[] = '<div  class="gsl-width-1-1 gsl-card gsl-card-default" >' . $desc . '</div>';
			}

			foreach ($this->form->getFieldset($name) as $field)
			{
				if ($field->hidden || $field->fieldname == "com_difficulty")
				{
					continue;
				}

				$maxjoomlaversion = $this->form->getFieldAttribute($field->fieldname, "maxjoomlaversion", false);
				if ($maxjoomlaversion && version_compare(JVERSION, $maxjoomlaversion, ">"))
				{
					continue;
				}
				$minjoomlaversion = $this->form->getFieldAttribute($field->fieldname, "minjoomlaversion", false);
				if ($minjoomlaversion && version_compare(JVERSION, $minjoomlaversion, "<"))
				{
					continue;
				}

				// Hide club update field if no club addons are installed
				//if ($field->fieldname=="clubcode_spacer" || $field->fieldname=="clubcode"){
				//	// disable if no club addons are installed
				//	$plugins = PluginHelper::getPlugin("jevents");
				//	if (count($plugins)==0 && !$haslayouts){
				//		continue;
				//	}
				//}

				$class = isset($field->class) ? $field->class : "";

				$difficultyClass = "difficulty" . $this->form->getFieldAttribute($field->fieldname, "difficulty");
				if ($this->component->params->get("com_difficulty", 1) < $this->form->getFieldAttribute($field->fieldname, "difficulty1"))
				{
					$difficultyClass .= " hiddenDifficulty";
				}

				if (StringHelper::strlen($class) > 0)
				{
					$class = " class='gsl-grid $class $difficultyClass'";
				}
				else
				{
					$class = " class='gsl-grid  $difficultyClass'";
				}

				$html[] = "<div $class>";
				if (strtolower($field->type) == "note" || strtolower($field->type) == "jevinfo")
                {
	                $html[] = '<div class="gsl-width-1-1" >' . $field->label . "<div>" . $field->input . '<br></div></div>';
                }
				else if (!isset($field->label) || $field->label == "")
				{
					$html[] = '<div class="gsl-width-1-2"><span class="editlinktip">' . $field->label . '</span></div>';
					$html[] = '<div class="gsl-width-1-2">' . $field->input . '</div>';
				}
				else
				{
					$html[] = '<div class="gsl-width-1-1" >' . $field->input . '</div>';
				}
				$label = $field->label;

				$html[] = '</div>';
			}

			if ($name == "JEV_PERMISSIONS")
			{
				$name = "permissions";
				foreach ($this->form->getFieldset($name) as $field)
				{
					$class = isset($field->class) ? $field->class : "";

					if (StringHelper::strlen($class) > 0)
					{
						$class = " class='$class'";
					}
					$html[] = "<div $class>";
					$html[] = '<div class="gsl-width-1-1" >' . $field->input . '</div>';

					$html[] = '</div>';
				}
			}

			$html[] = '</div>';

			echo implode("\n", $html);
			?>
            </li>
			<?php
		}

		if ($haslayouts)
		{
			?>
            <li>
			<ul class="gsl-tab" gsl-tab="connect: #jvts-theme-tabs" id="myLayoutTabs">
				<?php
				$first = false;
				foreach (JEV_CommonFunctions::getJEventsViewList() as $viewfile)
				{
					$config = JPATH_SITE . "/components/" . JEV_COM_COMPONENT . "/views/" . $viewfile . "/config.xml";
					if (file_exists($config))
					{

						if (!$first)
						{
							$first = $viewfile;
							$class = ' class="active"';
						}
						else
						{
							$class = '';
						}
						?>
						<li <?php echo $class; ?>><a data-toggle="tab"
						                             href="#<?php echo $viewfile; ?>"><?php echo $viewfile; ?></a></li>
						<?php
					}
				}
				?>
			</ul>
            <!-- Tabs themselves //-->
            <div class=" gsl-margin-remove gsl-card-body gsl-card-default gsl-padding gsl-width-expand">
	            <ul class="gsl-switcher" id="jvts-theme-tabs">
		            <?php

			// Now get layout specific parameters
			//Form::addFormPath(JPATH_COMPONENT ."/views/");
			foreach (JEV_CommonFunctions::getJEventsViewList() as $viewfile)
			{
				$config = JPATH_SITE . "/components/" . JEV_COM_COMPONENT . "/views/" . $viewfile . "/config.xml";
				if (!file_exists($config))
				{
					continue;
				}
					?>
		            <li>
			            <?php
				$config = JPATH_SITE . "/components/" . JEV_COM_COMPONENT . "/views/" . $viewfile . "/config.xml";
				if (file_exists($config))
				{

					$layoutform = Form::getInstance("com_jevent.config.layouts." . $viewfile, $config, array('control' => 'jform', 'load_data' => true), true, "/config");
					$layoutform->bind($this->component->params);

					if (File::exists(JPATH_ADMINISTRATOR . "/manifests/files/$viewfile.xml"))
					{
						$xml        = simplexml_load_file(JPATH_ADMINISTRATOR . "/manifests/files/$viewfile.xml");
						$layoutname = (string) $xml->name;
						$langfile   = 'files_' . str_replace('files_', '', strtolower(InputFilter::getInstance()->clean((string) $layoutname, 'cmd')));
						$lang       = Factory::getLanguage();
						$lang->load($langfile, JPATH_SITE, null, false, true);
					}

					$fieldSets = $layoutform->getFieldsets();
					$html      = array();
					$hasconfig = false;
					foreach ($fieldSets as $name => $fieldSet)
					{
						$html[] = '<div class="gsl-width-1-1" >';

						if (isset($fieldSet->description) && !empty($fieldSet->description))
						{
							$desc   = Text::_($fieldSet->description);
							$html[] = '<div  class="gsl-width-1-1 gsl-card gsl-card-default" >' . $desc . '</div>';
						}

						$html[] = '<div class="paramlist admintable form-horizontal" >';

						foreach ($layoutform->getFieldset($name) as $field)
						{
							if ($field->hidden)
							{
								continue;
							}

							$maxjoomlaversion = $this->form->getFieldAttribute($field->fieldname, "maxjoomlaversion", false);
							if ($maxjoomlaversion && version_compare(JVERSION, $maxjoomlaversion, ">"))
							{
								continue;
							}
							$minjoomlaversion = $this->form->getFieldAttribute($field->fieldname, "minjoomlaversion", false);
							if ($minjoomlaversion && version_compare(JVERSION, $minjoomlaversion, "<"))
							{
								continue;
							}

							$hasconfig = true;
							$fieldhtml = $field->renderField();

							// Short cut replacement pending plugin updates!
							$fieldhtml = str_replace('class="row ', 'class="row  gsl-grid gsl-margin-remove ',$fieldhtml );
							$fieldhtml = str_replace('class="span2', 'class="gsl-width-1-6@m gsl-width-1-1 gsl-margin-small-bottom', $fieldhtml );
							$fieldhtml = str_replace(array('class="span10', 'class=" span10'), 'class="gsl-width-expand gsl-margin-small-bottom  ', $fieldhtml );

							// Needed to deal with early execution of initTemplate in backend
							//$fieldhtml = str_replace('gsl-button-group', 'gsl-button-group-ysts',$fieldhtml );

							$html[] = $fieldhtml;

						}
						$html[] = '</div>';
					}

					if (!$hasconfig)
					{
						$x = 1;
					}
					if ($hasconfig)
					{

						echo implode("\n", $html);

					}
				}
				?>
		            </li>
		            <?php
			}
			?>
	            </ul>
            </div>
            </li>
	            <?php
		}

		if ( $hasPlugins)
		{
            ?>
            <li>
                <ul class="gsl-list-divider" gsl-accordion="targets: > *:not(.no-accordion-icon)">
            <?php
			$i = 0;
			foreach ($jevplugins as $plugin)
			{
				$config = JPATH_SITE . "/plugins/" . $plugin->type . "/" . $plugin->name . "/" . $plugin->name . ".xml";
				if (file_exists($config))
				{
					// Load language file
					$lang     = Factory::getLanguage();
					$langfile = "plg_" . $plugin->type . "_" . $plugin->name . ".sys";
					$lang->load($langfile, JPATH_ADMINISTRATOR, null, false, true);
					$langfile = "plg_" . $plugin->type . "_" . $plugin->name;
					$lang->load($langfile, JPATH_ADMINISTRATOR, null, false, true);

					// Now get plugin specific parameters
					$pluginform = Form::getInstance("com_jevents.config.plugins." . $plugin->name, $config, array('control' => 'jform_plugin[' . $plugin->type . '][' . $plugin->name . ']', 'load_data' => true), true, "/extension/config/fields");
					$pluginparams = new JevRegistry($plugin->params);

					$hasfields = false;
					$fieldSets = $pluginform->getFieldsets();
					foreach ($fieldSets as $name => $fieldSet)
					{
						if ($pluginform->getFieldset($name))
						{
							$hasfields = true;
						}
					}

					?>
					<li class="gsl-card gsl-card-default gsl-card-hover <?php echo !$hasfields ? "no-accordion-icon" : "";?>" style="position:relative">
					<?php

					// Load the whole XML config file to get the plugin name in plain english
					$xml = new SimpleXMLElement($config, 0, true);
					// TODO Consider adding enabled/disabled method here for plugins inclusing unpublished ones!
					// TODO handle unpublished plugins too

					$safedesc = Text::_($xml->description, true);
					$safename = Text::_($xml->name, true);

					if ($safedesc)
					{
						$popclass = " hasYsPopover";
						$labelinfo = '  data-yspoptitle="' . $safename . '" data-yspopcontent="' . $safedesc . '" ';
						$labelinfo .= ' data-yspopoptions=\'{"mode" : "click, hover", "offset" : 20,"delayHide" : 200, "pos" : "top-left"}\' ';
					}
					else
					{
						$popclass = "";
						$labelinfo = '';
					}

					// offer drop down IFF has fields!
					if ($hasfields)
					{
						$label = '<i gsl-icon="icon:chevron-right" ></i> ' . Text::_($xml->name);
					}
					else
					{
						$label = '<span style="margin-left:25px;" >' . Text::_($xml->name) ."</span>";
					}
					if ($safedesc)
					{
						$label .= '<i gsl-icon="icon:info" style="margin-left:10px;font-size:1.2em;" class="' . $popclass. '" ' . $labelinfo. '></i> ';
					}
					else
					{
						$label .= '';
					}

					$checked1 = $plugin->enabled ? 'checked="checked" ' : '';
					$checked0 = !$plugin->enabled ? 'checked="checked" ' : '';
					$labelextra    = '<div class="gsl-button-group " style="position:absolute;right:30px" >'
						. '<input type="radio"  ' . $checked1 . '  value="1" name="jform_plugin[' . $plugin->type . '][' . $plugin->name . '][enabled]"  id="jform_plugin_' . $plugin->type . '_' . $plugin->name . '_params_enabled1" class="gsl-hidden">'
						. '<label for="jform_plugin_' . $plugin->type . '_' . $plugin->name . '_params_enabled1" class="gsl-button gsl-button-small ' . ($plugin->enabled ? 'gsl-button-primary' : ''). ' ">'
						. Text::_('JENABLED')
						. '</label>'
						. '<input type="radio" ' . $checked0 . ' value="0" name="jform_plugin[' . $plugin->type . '][' . $plugin->name . '][enabled]"  id="jform_plugin_' . $plugin->type . '_' . $plugin->name . '_params_enabled0" class="gsl-hidden">'
						. '<label for="jform_plugin_' . $plugin->type . '_' . $plugin->name . '_params_enabled0" class="gsl-button gsl-button-small ' . ($plugin->enabled ? '' : 'gsl-button-danger'). '">'
						. Text::_('JDISABLED')
						. '</label>'
						. '</div>';

					//$label = JText::_($xml->name);
					if ($hasfields)
					{

						$fieldSets = $pluginform->getFieldsets();
						$html      = array();
						$hasconfig = false;
						foreach ($fieldSets as $name => $fieldSet)
						{
							if (!$pluginform->getFieldset($name))
							{
								continue;
							}

							$html[] = '<div class="paramlist admintable form-horizontal" >';

							if (isset($fieldSet->description) && !empty($fieldSet->description))
							{
								$desc   = Text::_($fieldSet->description);
								$html[] = '<div class="paramlist_description" >' . $desc . '</div>';
							}

							foreach ($pluginform->getFieldset($name) as $field)
							{
								if ($field->hidden)
								{
									continue;
								}

								// Set the value for the form
								$paramsval = $pluginparams->get($field->fieldname, $field->default);
								if (is_object($paramsval))
								{
									// Need this for subform to work
									$paramsval = (array) $paramsval;
								}
								$field->setValue($paramsval);

								$maxjoomlaversion = $this->form->getFieldAttribute($field->fieldname, "maxjoomlaversion", false);
								if ($maxjoomlaversion && version_compare(JVERSION, $maxjoomlaversion, ">"))
								{
									continue;
								}
								$minjoomlaversion = $this->form->getFieldAttribute($field->fieldname, "minjoomlaversion", false);
								if ($minjoomlaversion && version_compare(JVERSION, $minjoomlaversion, "<"))
								{
									continue;
								}

								if ($field->fieldname == "whitelist")
								{
									$x = 1;
								}

								$hasconfig = true;
								$html[]    = $field->renderField();
							}
							$html[] = '</div>';
						}

						?>
						<?php echo $labelextra; ?>
                        <a class="gsl-accordion-title " href="#"  >
	                        <?php echo $label; ?>
                        </a>
                        <div class="gsl-accordion-content">
							<?php
							echo implode("\n", $html);
							?>
                        </div>

						<?php
					}
					else
					{

						?>
						<?php echo $labelextra; ?>
						<div class="gsl-accordion-title no-accordion-icon" >
							<?php echo $label; ?>
						</div>
						<?php
					}
					?>
					</li>
					<?php
				}
			}
			?>
                </ul>
            </li>
            <?php
		}
		?>
        </ul>
		</div>
</div>
	</fieldset>

	<input type="hidden" name="id" value="<?php echo $this->component->id; ?>"/>
	<input type="hidden" name="component" value="<?php echo $this->component->option; ?>"/>
	<input type="hidden" name="jform_title" id="jform_title" value="com_jevents"/>
	<input type="hidden" name="controller" value="component"/>
	<input type="hidden" name="option" value="<?php echo JEV_COM_COMPONENT; ?>"/>
	<input type="hidden" name="task" value=""/>
	<?php echo HTMLHelper::_('form.token'); ?>

</form>
</div>


