<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
require_once( JPATH_ADMINISTRATOR.DS.'components/com_jxplorer'.DS.'xajax'.DS.'xajax_core'.DS.'xajax.inc.php' );

function jxResetRating($id)
{
    $objResponse = new xajaxResponse();

    ob_start();
    jxHtml::jxRating(0);
    $form .= ob_get_clean();

    $objResponse->assign('rating_'.$id, "innerHTML", $form);
    
    $db  =& JFactory::getDBO();
    $sql = "UPDATE #__jx_index SET rate_total = 0, rate_times = 0 WHERE id = $id";
    $db->setQuery($sql);
    $db->query();
    
    $objResponse->script("pBarIn();");
    return $objResponse;
}

/*
 * this displays file properties.
*/
function jcEditFile($ids)
{
    $objResponse = new xajaxResponse();
    if(!$ids)
    {
        $objResponse->alert(jcText::_('Please, make selection from the list to Edit'));
        $objResponse->script("pBarOut();");
        return $objResponse;
    }
    
    foreach($ids AS $id)
    {
        ob_start();
        jxFileProperty($id);
        $form .= ob_get_clean();
    }
    $form .= '
      <div  class="tablecell" style="padding:10px;clear:both">
        <input type="button" value="'.jcText::_('Save').'" onclick="pBarOut(); xajax_jxSaveProperty(xajax.getFormValues(\'jxPropForm\'));" />
        <input type="button" value="'.jcText::_('Close').'" onclick="pBarOut();" />
     </div>'; 
    $objResponse->assign('prop_form', "innerHTML", $form);
   $objResponse->script("pBarIn();");
    return $objResponse;
}
function chmEditFile($ids)
{
    $objResponse = new xajaxResponse();
    if(!$ids)
    {
        $objResponse->alert(jcText::_('Please, make selection from the list to Edit'));
        $objResponse->script("pBarOut();");
        return $objResponse;
    }
    
    foreach($ids AS $id)
    {
        ob_start();
        chmFileProperty($id);
        $form .= ob_get_clean();
    }
    
    $objResponse->assign('prop_form', "innerHTML", $form);
    $objResponse->script("pBarIn();");
    return $objResponse;
}

function jxSaveProperty($post)
{
    $objResponse = new xajaxResponse();
  
    $db  =& JFactory::getDBO();
	foreach($post['id'] AS $id)
    {
        $view = $post['view'][$id];
        $desc = $db->getEscaped($post['description'][$id]);
        $icon = $post['hid_icon_'.$id];
        $sql = "UPDATE #__jx_index SET `view` = '{$view}', `description` = '{$desc}' ".($icon ? ", icon = '{$icon}'" : NULL)." WHERE id = {$id}";
        $db->setQuery($sql);
        if(!$db->query())
        {
            $objResponse->alert($db->stderr());
        }
        if($icon) $objResponse->assign('img_'.$id, "src", 'components/com_jxplorer/images/icons/16/'.$icon);
    }
    $objResponse->script("pBarOut();");
    return $objResponse;
}
function jxSaveChm($post)
{
    $objResponse = new xajaxResponse();
  
    $db  =& JFactory::getDBO();
	foreach($post['id'] AS $id)
    {
    	
       foreach ($post['chmod'] as $group){
       	$summ = 0;
       	foreach ($group as $val){
       		$summ += $val;
       	}
       	$chmod .= $summ;
       }
       
       $sql = "SELECT dir , name FROM #__jx_index WHERE id = {$id}";
        $db->setQuery($sql);
        $file = $db->loadObject();
        
       chmod($file->dir.$file->name,'0'.$chmod);
        $sql = "UPDATE #__jx_index SET `perms` = '".jxPermsToStr($chmod)."' WHERE id = {$id}";
        $db->setQuery($sql);
        if(!$db->query())
        {
            $objResponse->alert($db->stderr());
        }
        $href = '<a d="chm_'.$id.'" onclick="editChm(\'cb'.$id.'\')" href="javascript:void(0)">'.jxPermsToStr($chmod).'</a>';
        $objResponse->assign("chm_$id", "innerHTML", $href);
       
    }
    $objResponse->script("pBarOut();");
    return $objResponse;
}

function jxLoadList($rows,  $directory)
{
    
    $objResponse = new xajaxResponse();
    $_SESSION['jxplorer.rows']  = $rows;
    $_SESSION['jxplorer.direc'] = $directory;
    $objResponse->alert($directory);
    if(!$rows)
    {
        $objResponse->alert(jcText::_('Please make a selection from list to Copy/Move'));
        $objResponse->script("pBarIn();");
        return $objResponse;
    }
    
    $form = jxDestinationList($destination, 30);
    
    $form .= '
      <div  class="tablecell" style="padding:10px;clear:both">
        <input type="button" value="'.jcText::_('Copy').'" onclick="pBarOut(); xajax_jxCopyMoveX(xajax.getFormValues(\'jxPropForm\'), \'copy\');" />
        <input type="button" value="'.jcText::_('Move').'" onclick="pBarOut(); xajax_jxCopyMoveX(xajax.getFormValues(\'jxPropForm\'), \'move\');" />
        <input type="button" value="'.jcText::_('Close').'" onclick="pBarOut();" />
     </div>'; 
    
    $objResponse->assign('prop_form', "innerHTML", $form);
    $objResponse->script("pBarIn();");
    return $objResponse;

}

function jxCopyMoveX($post, $act)
{
    $objResponse = new xajaxResponse();
    $objResponse->script("pBarOut();");
    if(!$post['destination'])
    {
        $objResponse->alert(jcText::_('Please Select Destination!'));
        return $objResponse;
    }
    $dest = jxGetDestinationById($post['destination']);
    jxCopyMove($_SESSION['jxplorer.rows'], $dest, $act, $directory);

    if($act == 'move')
    {
        jxIndex($_SESSION['jxplorer.direc'], false);
        foreach($_SESSION['jxplorer.rows'] AS $id)
        {
            $objResponse->script("new Effect.BlindUp('row_{$id}');");
        }
    }
    unset($_SESSION['jxplorer.direc']);
    unset($_SESSION['jxplorer.rows']);

    $objResponse->script("pBarHiden();");
    return $objResponse;
    
}

function _publish($id, $table, $publish)
{
    $db =& JFactory::getDBO();
    $alt = !$publish ? JText::_( 'Published' ) : JText::_( 'Unpublished' );
    $set = ($publish ? 0 : 1);
   
    
    if(jxParentIsPublished($id)){
        $sql = "UPDATE #__jx_index SET published = '$set' ,  publish_home = 0  WHERE id = '$id'";
    }else {
    	$sql = "UPDATE #__jx_index SET published = '$set' , publish_home = 1 WHERE `id` = '$id'";
    }
    
    $db->setQuery($sql);
    $db->query();
  
    if($set == 0) return;
}

function jcPublish($id, $table, $publish, $some='false')
{
$db =& JFactory::getDBO();
$objResponse = new xajaxResponse();

	if ($some=='true'){
	    $set = ($publish ? 0 : 1);
	    $sql = "SELECT `dir`, `name`  FROM #__jx_index where id='$id'";
	    $db->setQuery($sql);
	    $dirname_array = $db->loadAssocList();
	    $name=$dirname_array[0][name];
	    $dirname=$dirname_array[0][dir];
	    $db->setQuery("SELECT `id` FROM #__jx_index where `dir` LIKE '".$dirname.$name."%'");
	    $array = $db->loadResultArray();
   
    foreach ($array as $element){
    	_publish($element, 'jx_index', $publish);
    }
   
    	_publish($id, $table, $publish);
   
    } else {
    	_publish($id, $table, $publish);
    }
    $publish ? $img  = 'publish_x.png' : $img  = 'tick.png';
    $set = ($publish ? 0 : 1);    
    $href = "<a href=\"javascript:void(0);\"  ".
        "id=\"publish_$id\" onclick=\"document.getElementById('pdiv_$id').innerHTML = '<img src=components/com_jxplorer/images/load.gif>';xajax_jcPublish('$id', '$table', '$set', document.getElementById('sd').checked) \">".
        "<img src=\"images/$img\" border=\"0\" alt=\"$alt\"/></a>";

    
    $objResponse->assign("pdiv_$id", "innerHTML", $href);
    return $objResponse;
}
function jcPublishList($ids, $table, $publish)
{

    $objResponse = new xajaxResponse();
    $objResponse->script("pBarOut();");
    if(!$ids)
    {
        $objResponse->alert(JText::_('Please make a selection from the list to').' '.$alt);
        return $objResponse;
    }
    
    $publish ? $img  = 'publish_x.png' : $img  = 'tick.png';
    $set = ($publish ? 0 : 1);     
    foreach ($ids AS $id)
    {
        _publish($id, $table, $publish);
        
        $href = "<a href=\"javascript:void(0);\"  ".
            "id=\"publish_$id\" onclick=\"document.getElementById('pdiv_$id').innerHTML = '<img src=components/com_jxplorer/images/load.gif>';xajax_jcPublish('$id', '$table', '$set')\">".
            "<img src=\"images/$img\" border=\"0\" alt=\"$alt\"/></a>";
        $objResponse->assign("pdiv_$id", "innerHTML", $href);
    }
    $objResponse->script("pBarIn();");
    return $objResponse;

}
function jcDeleteList($ids, $table)
{
    $objResponse = new xajaxResponse();
    $objResponse->script("pBarOut();");
    if(!$ids)
    {
        $objResponse->alert(JText::_('Please make a selection from the list to Delete'));
        return $objResponse;
    }
    
    $db =& JFactory::getDBO();
    $id = implode(",", $ids);
    
    $sql = "DELETE FROM #__$table  WHERE id IN ($id)";
    
    $db->setQuery($sql);
    $db->query();
    
    SWITCH($table)
    {
        // if deleting property groups, delete group items as well.
        case 'jc_products_properties_groups':
            $sql = "DELETE FROM #__jc_products_properties  WHERE group_id IN ($id)";
            $db->setQuery($sql);
            $db->query();
        break;

        case 'jc_products_categories':
            jcCategoryController::remove($id);
        break;
    }
    
    // Hide delted rows. For that row have to have id="row_ID"
    foreach ($ids AS $id)
    {
        $objResponse->assign("row_$id", "style.display", 'none');
    }
    return $objResponse;

}
function jcAccess($id, $table, $access)
{
    $db =& JFactory::getDBO();
    
    switch ($access)
    {
        case 0:
            $set   = 1;
            $name  = 'Registered';
            $color = 'Red';
            break;
        case 1:
            $set   = 2;
            $name  ='Special';
            $color = 'Black';
            break;
        case 2:
            $set   = 0;
            $name  = 'Public';
            $color = 'Green';
            break;
    }
    $sql = "UPDATE #__$table SET access = '$set' WHERE id = '$id'";
    $db->setQuery($sql);
    $db->query();
    
    $href = "<a href=\"javascript:void(0);\"  id=\"access_$id\" onclick=\"document.getElementById('adiv_$id').innerHTML = '<img src=components/com_jcommerce/images/load.gif>';xajax_jcAccess('$id', '$table', '$set')\">$name</a>";
    $objResponse = new xajaxResponse();
    $objResponse->assign("adiv_$id", "innerHTML", $href);
    $objResponse->assign("access_$id", "style.color", $color);
    return $objResponse;
}

$xajax = new xajax(JURI::root().'/administrator/index3.php?option=com_jxplorer&no_html=1'); 
//$xajax->debugOn(); // Uncomment this line to turn debugging on
$xajax->registerFunction("jcAccess");
$xajax->registerFunction("jcPublish");
$xajax->registerFunction("jcPublishList");
$xajax->registerFunction("jcDeleteList");
$xajax->registerFunction("jxSaveProperty");
$xajax->registerFunction("jxLoadList");
$xajax->registerFunction("jcEditFile");
$xajax->registerFunction("chmEditFile");
$xajax->registerFunction("jxSaveChm");
$xajax->registerFunction("jxResetRating");
$xajax->registerFunction("jxCopyMoveX");
$xajax->registerFunction("_publish");


//$xajax->bDebug = true;
$xajax->processRequest();
$xajax->printJavascript('components/com_jxplorer/js/');
?>