<?php

/*======================================================================*\
|| #################################################################### ||
|| # Drag'nDrop Shop 1.8                                              # ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright 2014 JAKWEB All Rights Reserved.                       # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| #   ----------- Drag'nDrop Shop IS NOT FREE SOFTWARE -----------   # ||
|| #                      http://www.jakweb.ch                        # ||
|| #################################################################### ||
\*======================================================================*/

// Get the data per array for page,newsletter with limit
function jak_get_page_info($lsvar,$lsvar1) 
{
	global $jakdb;
    $sql = 'SELECT * FROM '.$lsvar.' ORDER BY id DESC '.$lsvar1.'';
    $result = $jakdb->query($sql);
    while ($row = $result->fetch_assoc()) {
            // collect each record into $_data
            $jakdata[] = $row;
        }
        
    return $jakdata;
}

// Search for lang files in the admin folder, only choose .ini files.
function jak_get_lang_files($folder) {

if ($folder) {
	$langdir = './lang/';
} else {
	$langdir = '../lang/';
}

if ($handle = opendir($langdir)) {

    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
    $showini = substr($file, strrpos($file, '.'));
    if ($file != '.' && $file != '..' && $showini == '.ini') {
    
    	$getlang[] = substr($file, 0, -4);
    
    }
    }
	return $getlang;
    closedir($handle);
}
}

// Get all user out the database limited with the paginator
function jak_get_user_all($lsvar, $userid) 
{
	
	if ($userid) {
	
		$sqlwhere = ' WHERE id = "'.$userid.'"';
	}
	
	global $jakdb;
	$sql = 'SELECT * FROM '.$lsvar.$sqlwhere;
    $result = $jakdb->query($sql);
    while ($row = $result->fetch_assoc()) {
        $user[] = array('id' => $row['id'], 'username' => $row['username'], 'email' => $row['email'], 'name' => $row['name'], 'access' => $row['access']);
    }
    
    return $user;
}

// Check if user exist and it is possible to delete ## (config.php)
function jak_user_exist_deletable($lsvar)
{
global $jakdb;
$useridarray = explode(',', JAK_SUPERADMIN);
// check if userid is protected in the config.php
if (in_array($lsvar, $useridarray)) {
        return false;
        exit;
} else {
		$sql = 'SELECT id FROM '.DB_PREFIX.'user WHERE id = "'.smartsql($lsvar).'" LIMIT 1';
        $result = $jakdb->query($sql);
        if ($jakdb->affected_rows > 0) {
        return true;
}
}
}

// Check if row exist with id
function jak_field_not_exist_id($lsvar,$lsvar1,$lsvar2,$lsvar3)
{
		global $jakdb;
		$sql = 'SELECT id FROM '.$lsvar2.' WHERE id != "'.smartsql($lsvar1).'" AND '.$lsvar3.' = "'.smartsql($lsvar).'" LIMIT 1';
        $result = $jakdb->query($sql);
        if ($jakdb->affected_rows > 0) {
        return true;
}
}
function getCountry($selected, $where) {

	global $jakdb;
	
	if ($where) {
		$sqlwhere = ' WHERE id = '.$where.'';
	}
	
	$result = $jakdb->query('SELECT id, name FROM '.DB_PREFIX.'shop_country'.$sqlwhere.' LIMIT 240');
	
	while ($row = $result->fetch_assoc()) {
	
		$select = '';
		
		if ($selected == $row['id']) {
			
			$select = ' selected="selected" ';
		
		}
		
		if (!$where) {
	
			$country_list .= '<option value="'.$row['id'].'"'.$select.'>'.$row['name'].'</option>';
		} else {
			$country_list = $row['name'];
		}
	}
	
	return $country_list;

}

// Get the shop table
function jak_get_shop($table, $limit) 
{
	
	global $jakdb;
    $sql = 'SELECT * FROM '.$table.' ORDER BY ecorder ASC '.$limit.'';
    $result = $jakdb->query($sql);
    while ($row = $result->fetch_assoc()) {
            // collect each record into $_data
            $jakdata[] = $row;
        }
        
    return $jakdata;
}

// Get the categories per array with no limit
function jak_get_cat_info($jakvar,$jakvar1) 
{
	global $jakdb;
	
	if (empty($jakvar1)) {
		$sqlwhere = '';
	} else {
		$sqlwhere = ' WHERE count >= 1';
	}
	
    $sql = 'SELECT * FROM '.$jakvar.$sqlwhere.' ORDER BY catorder ASC';
    $result = $jakdb->query($sql);
    while ($row = $result->fetch_assoc()) {
            // collect each record into $_data
            $jakdata[] = $row;
        }        
    return $jakdata;
}

// Search for style files in the site folder, only choose folders.
function jak_get_site_style($styledir) {

if ($handle = opendir($styledir)) {

    /* This is the correct way to loop over the directory. */
    while (false !== ($template = readdir($handle))) {
    if ($template != '.' && $template != '..' && is_dir($styledir.'/'.$template) ) {
	    $getstyle[] = $template;
	    
    }
    }
	return $getstyle;
	clearstatcache();
    closedir($handle);
}
}
?>