<?php
/**
* @package JCommerce
* @subpackage config
* @copyright Copyright (C) 2005 - 2006 JoomlaEquipment.
* @license Commercial
* This is file to mange configuration of the JCommerce component


class jcConfigHelper
{
    function 
}
*/
class JEConfig
{
    var $tb = '#__je_config';
    
    
    function get($val)
    {
        $db =& JFactory::getDBO();
        
        if(!$val)
        {
            JError::riseError(0112, jcText::_('Rewuired parameter empty'));
        }
        $val = explode('.', $val);
        $section = $val[0];
        $value   = $val[1];
        $sql = "SELECT `selected` FROM  #__je_config  WHERE `section` = '{$section}' AND name = '{$value}'";
        $db->setQuery( $sql );
        $rows = $db->loadResult();
        return $rows;
    }
    
    function set($val, $val2, $option='com_jxplorer', $ad)
    {
        $database = & JFactory::getDBO();
        
        if(!$option) $option = mosGetParam( $_REQUEST,'option', false);
        if(!$option || !$val) return false;
        $val = explode('.', $val);
        $section = $val[0];
        $value   = $val[1];
        $sql =  "select id from #__je_config where `component` = '{$option}' AND `section` = '{$section}' AND `name` = '{$value}'";
        $database->setQuery( $sql );
        $id = $database->loadResult();
        if($id)
        {
            $sql = "UPDATE `#__je_config` SET `selected` = '{$val2}' WHERE `component` = '{$option}' AND `section` = '{$section}' AND `name` = '{$value}'";
        }
        else
        {
            $sql = "
                INSERT INTO `#__je_config` 
                    ( `id` , `component` , `section` , `lable` , `name` , 
                    `type` , `description` , `values` , `selected` , `hidden` )
                VALUES ( NULL , '{$option}', '{$section}', '{$ad[lable]}', 
                         '{$value}', '{$ad[type]}', '{$ad[descr]}', 
                         '{$val2}', '{$val2}', '{$ad[hidden]}');";
        }
        
        $database->setQuery( $sql );
        if(!$database->query())
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }    
    function display()
    {
        JHTML::_('behavior.tooltip');
        JHTML::_('behavior.switcher');

        $db =& JFactory::getDBO();
        
        $document =& JFactory::getDocument();
        $document->addScript(JURI::root().'/administrator/components/com_jxplorer/js/Switcher.js');

        $sql = "SELECT * FROM #__je_config WHERE hidden = 'N' and component = 'com_jxplorer' ORDER BY `section`, `fieldset`, `type` ASC";
        $db->setQuery( $sql );
        $rows = $db->loadObjectList();
        $com = '';
        $fn = 0;
        foreach($rows AS $k => $row)
        {
            if($set != $row->fieldset)
            {
                if($set != '') 
                {
                    $tabs .= '</table></fieldset>';
                }
            }
            if($com != $row->section)
            {
                if($com != '') 
                {
                    $tabs .= '</div>';
                }
                $tabs .= "<div id=\"page-".str_replace(" ", "-", $row->section)."\">\n";
                $navs .= '<li class="item-smenu"><a id="'.
                          str_replace(" ", "-", $row->section).
                          '">'.ucfirst(jcText::_($row->section)).
                          "</a></li>\n"; 
            }
            if($set != $row->fieldset)
            {
                $fn = 1 - $fn;
                $wd = ($fn > 0 ? '58%' : '38%');
                $tabs .= '<fieldset style="display:block; width:'.$wd.'; float:left"><LEGEND>'.ucfirst($row->fieldset).'</LEGEND><table width="20%" class="admintable">';
            }
            $com = $row->section;
            $set = $row->fieldset;
            $tabs .= JEConfig::_renderRow($row);
        }
        $tabs .= "</table></fieldset></div>\n";
        
//        $out  = '<form action="index.php" method="post" name="adminForm">';
        $out = "<div class=\"submenu-box\">\n";
        $out .= "	<div class=\"submenu-pad\">\n";
        $out .= "		<div id=\"submenu\">\n";
        $out .= "		<div id=\"submenu2\">\n";
        $out .= $navs;
        $out .= "		</div>\n";
        $out .= "	  </div>\n";
        $out .= "		<div class=\"clr\"><BR></div>\n";
        $out .= "	</div>\n";
        $out .= "</div>\n";
        
        $document =& JFactory::getDocument();
        $document->setBuffer($out, 'modules', 'submenu');
        
        $out = "<div id=\"config-document\">\n";
        $out .= $tabs;       
        $out .= "	</div>\n";
        $out .= "<div class=\"clr\"></div>\n";
       // $out .= "<script> loadSwicther()</script>\n";
        $out .= '<input type="hidden" name="option" value="com_jxplorer" />';
        $out .= '<input type="hidden" name="task" value="cpanel" />';
        $out .= '<input type="hidden" name="boxchecked" value="0" />';
        $out .= '<input type="hidden" name="hidemainmenu" value="0" />';
        $out .= '';
        $out .= '</form>';
        echo $out;
        
        //$tabs->endPane();
        
    }
    
    function _renderRow($row)
    {
        $label = $row->lable;
        if($row->description)
        {
            $label = sprintf(
                '<SPAN class="editlinktip hasTip" title="%s::%s">%s</SPAN>',$row->lable, $row->description ,$row->lable);
        }
        $out = sprintf('<tr valign="top"><td width="10%s" nowrap="nowrap" class="key">%s:</td><td width="10%s" nowrap="nowrap">%s</td></tr>', '%', $label, '%',  JEConfig::_renderElement($row));
        return $out;
    }
    
    function _renderElement($row)
    {
        switch ($row->type)
        {
            case "yesno":
                $out =  JHTML::_('select.booleanlist',$row->name.$row->id, 'class="inputbox"', $row->selected);
            break;

            
            case "selectlist":
                $items = explode(",", $row->values);
                
                foreach($items AS $item){
                    $list[] = JHTML::_('select.option',trim($item), trim($item),'id','name');
                }
                $out		= JHTML::_('select.genericlist',   $list,  $row->name.$row->id, 'class="inputbox" size="'.count($items).'"','name', 'name', $row->selected );
            break;
            
            case "dropdown":
                $items = explode(",", $row->values);
                foreach($items AS $item)
                {
                    $list[] =  JHTML::_('select.option',trim($item), trim($item),'id','name');
                }
                $out = JHTML::_('select.genericlist',$list, $row->name.$row->id, 'class="inputbox" size="1" ', 'name', 'name', $row->selected);
            break;
            
            case "textarea":
                $out = sprintf('<textarea class="text_area" name="%s" rows="5" cols="40">%s</textarea>', $row->name.$row->id, htmlspecialchars( stripslashes($row->selected) ) );
            break;
            
            default:
                $out = sprintf('<input type="text" class="inputbox" name="%s" value="%s" size="40">', $row->name.$row->id, htmlspecialchars( stripslashes($row->selected)));
            break;
        }
        
        return $out;
    }

	function save($option = '')
	{
        global $mainframe;
        
        $db =& JFactory::getDBO();
        $option = JRequest::getVar('option');  

        $sql = "SELECT * FROM  #__je_config  WHERE hidden = 'N'";
        $db->setQuery( $sql );
        $rows = $db->loadObjectList();
        foreach($rows AS $row)
        {
            $val = addslashes(JRequest::getVar( $row->name.$row->id ));
            $sql = "UPDATE  #__je_config  SET selected = '$val' WHERE `id` = '{$row->id}'";
            $db->setQuery( $sql );
            $db->query();
        }
       
        switch (JRequest::getVar('task'))
        {
            
            case 'applyconfig':
            $link = 'index.php?option='.$option.'&task=config';
            break;
            
            case 'savecategory':
            default:
            $link = 'index.php?option='.$option;
            break;
        }
        
        $msg = jcText::_( 'Saved Configuration' );
        $mainframe->redirect( $link, $msg );
    }
}
?>