<?

$messages = array();
$error    = FALSE;
 

if (isset($custom_action)) {
    $action = $custom_action;
   
} else {
    $action = $_REQUEST["action"];
}
 
switch ($action) {
      
    case 'delete_sitemapform':
        delete_sitemapform();
        break;
   
     
    case 'save_sitemapform':
        save_sitemapform();
        break;
    case 'edit_sitemapform':
        edit_sitemapform();
        break;
    case 'new_sitemapform':
        require_once("edit_sitemapform_cardform.inc.php");
        break;          
    default:
        require_once("edit_sitemap_listform.inc.php");
        break;
}
function save_sitemapform() {
     
    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
    $input_id    = "";
    $sitemap_name = 'sitemap-';
    $website = array();
     
        $querySite = "SELECT * FROM main_site  WHERE id =  ".$_REQUEST["select_website"];
        $resultSite = @mysqli_query($GLOBALS['mysql_con'], $querySite);
        if(@mysqli_num_rows($resultSite) > 0){
            $website = @mysqli_fetch_array($resultSite);
            $sitemap_name = 'sitemap-'.$website['id'].'-'.$website['code'].'.xml';
        }else{
            $messages[] = "<div class=\"errorbox\">" . $translation->get("site_not_found") . "</div>\n";
            $error = TRUE;
        }
     
    
    if ($_REQUEST["input_id"] <> '') {
        $query    = "UPDATE sitemap SET
				main_site_id = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $_REQUEST["select_website"]) . "',
                main_language_id = '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $GLOBALS["language"]['id']) . "',
                main_site_url = '".mysqli_real_escape_string($GLOBALS['mysql_con'], $website['site_url']) . "',
                sitemap_name = '".mysqli_real_escape_string($GLOBALS['mysql_con'], $sitemap_name) . "',
				modified_date = " . time() . ",
				modified_user = " . (int)$GLOBALS["admin_user"]['id'] . "
               
				 
				WHERE id = '" . $_REQUEST["input_id"] . "'
			LIMIT 1";
        $inserted = FALSE;
        
        $input_id = $_REQUEST["input_id"];
       
    } else {
        
        $query    = "INSERT INTO sitemap
			(main_language_id, main_site_id, modified_date, modified_user, main_site_url, sitemap_name)
			VALUES (
				" . (int)$GLOBALS["language"]['id'] . ",
                " . (int) $_REQUEST["select_website"]. ",
				
				" . time() . ",
				" . (int)$GLOBALS["admin_user"]['id'] . ", 
                '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $website['site_url']) . "',
                '" . mysqli_real_escape_string($GLOBALS['mysql_con'], $sitemap_name) . "'
			)";
            
        $inserted = TRUE;
    }
 
    if (($_REQUEST["select_website"] == '')) {
        $messages[] = "<div class=\"errorbox\">" . $translation->get("error_site") . "</div>\n";
        $error      = TRUE;
    }
     

    if (!$error) {
        @mysqli_query($GLOBALS['mysql_con'], $query);
        $file = isset($sitemap_name) ? $sitemap_name : 'sitemap-updated';
        $file .= ".xml";
        $permissions = 0644;    
        require_once('sitemap.php');
         
        
        
        if ($inserted == TRUE) {
            $input_id   = mysqli_insert_id($GLOBALS['mysql_con']);
            $messages[] = '<div class="successbox">' . $translation->get("site_map_created1") . '</div>';
        } else {
            $messages[] = '<div class="successbox">' . $translation->get("site_map_created2") . '</div>';
        }
        $query    = "UPDATE sitemap SET
				 
                 sitemap_url = '".mysqli_real_escape_string($GLOBALS['mysql_con'], $website['site_url'].'/'.$sitemap_name) . "'
                
               
				 
				WHERE id = '" . $input_id . "'";
                @mysqli_query($GLOBALS['mysql_con'], $query);        
        edit_sitemapform($input_id, $messages);
        

    } else {
        header('EDIT_ERROR: 1');
        $input_sitemapform["main_site_id"] = $website['id'];
        $input_sitemapform["id"]              = $_REQUEST["input_id"];
        require_once("edit_sitemapform_cardform.inc.php");
    }
}
function edit_sitemapform( $_id = "", $messages = array() ) {
    
    $input_id = '';
   
    if ((int)$_id > 0) {
        $input_id = $_id;
    } if(isset($_REQUEST['input_id']) && $_id == ''){
        $input_id = $_REQUEST["input_id"];
    }   
 
 
    if ($input_id <> '') {
        $query  = "SELECT * FROM sitemap WHERE id = '" . $input_id . "' LIMIT 1";
        $result = @mysqli_query($GLOBALS['mysql_con'], $query);
        if (@mysqli_num_rows($result) == 1) {
            $input_sitemapform = @mysqli_fetch_array($result);
            
                require_once("edit_sitemapform_cardform.inc.php");
          
        }
    } else {
        $input_sitemapform = array();
         
        require_once("edit_sitemapform_cardform.inc.php");
    }
}
function delete_sitemapform() {
    if ($_REQUEST["input_id"] <> '') {
        $query = "DELETE FROM sitemap WHERE id = '" . $_REQUEST["input_id"] . "' LIMIT 1";
        @mysqli_query($GLOBALS['mysql_con'], $query);
         

        
    }
 
    require_once("edit_sitemap_listform.inc.php");
}
function create_site_set($id = 0){

    $translation = \DynCom\mysyde\common\classes\Registry::get("translation");
 

    $query = "SELECT * FROM main_site WHERE std_main_language_id = '" . $GLOBALS["language"]['id'] . "' and site_url != ''";
    $result = mysqli_query($GLOBALS['mysql_con'], $query);
    if (@mysqli_num_rows($result) == 0) {
        return;
    }

    $preselect = array();
    // $query2 = "SELECT * FROM sitemap WHERE id = '" . $id . "' LIMIT 1";
     
    // $result2 = mysqli_query($GLOBALS['mysql_con'], $query2);
    
    // while($row2 = @mysqli_fetch_array($result2)){
    //     array_push($preselect,$row2['main_site_id']);
    // }
    
    ?>
     <div class="label"><?php echo $translation->get("top_websites"); ?></div>
            <div class="groups bc-select-ui ui fluid  search selection dropdown" style="clear:both;display:block;width:100% !important;">
                <input type="hidden" name="select_website">
                <i class="dropdown icon"></i>
                <div class="default text"><?php echo $translation->get("top_websites"); ?></div>
                <div class="menu">
                    <?  
                        
                    
                        while($row = @mysqli_fetch_array($result)) {
                            echo "<div class=\"item\" data-value=\"{$row['id']}\">".$row["name"].' - '.$row['site_url']."</div>";
                        }
                      
                    ?>
                </div>
        </div>
        <script type="text/javascript" src="/plugins/jquery/jquery-3.5.1.min.js"></script>
                <script type="text/javascript" src="/plugins/Semantic-UI-master/semantic.min.js"></script>
                <link rel="stylesheet" type="text/css" href="/plugins/Semantic-UI-master/bc-semantic.css?time=<?= filemtime($_SERVER['DOCUMENT_ROOT'] . '/plugins/Semantic-UI-master/bc-semantic.css')?>">
                <script>
                    var $y = jQuery.noConflict();

                    $y( document ).ready(function() {
                        $y('.groups.ui.fluid.dropdown').dropdown('set selected', <?= json_encode($preselect) ?>); 
                    });


        </script>
    <? }
?>