<?php
/**
 *
 * Copyright (C) Die Randgruppe GmbH
 *
 * http://www.randshop.com
 * http://www.dierandgruppe.com
 *
 * Unter der Lizenz von Die Randgruppe GmbH:
 * http://www.randshop.com/Lizenz
 *
 */

include_once('../../conf/config.inc.php');
include_once(DATEIPFAD . 'conf/db.inc.php');
include_once(DATEIPFAD . 'includes/functions.shopeinstellung.inc.php');
include_once(DATEIPFAD . 'includes/functions.artikel.inc.php');
include_once(DATEIPFAD . 'includes/functions.seo.inc.php');
include_once(DATEIPFAD . 'includes/functions.easymarketing.inc.php');

$einstellungen = GetEinstellungen('access_token', 'easymarketing');
if($_GET['shop_token'] !== getShopToken() || !$einstellungen->easymarketing->access_token) {
    header($_SERVER["SERVER_PROTOCOL"] . ' 401 Unauthorized');
    echo '<h1>Error 401 - You are not allowed to access this resource</h1>';
    exit;
}

$waehrung = GetWaehrungDetail();
$einstellungen = GetEinstellungen('root_kategorie', 'easymarketing');

$artikelDataArray = GetArtikelDataArrayPA(array(
    'SortField' => TABLE_ARTIKEL . '.wie_oft_bestellt',
    'SortOrder' => 'DESC',
    'DataOffset' => 0,
    'DataCount' => intval($_GET['limit']),
    'FilterKategorieID' => $einstellungen->easymarketing->root_kategorie,
    'FilterKategorieRekursive' => true,
    'FilterAktiv' => 1
));

$resultObj = new stdClass();
$resultObj->limit = intval($_GET['limit']);
$resultObj->most_sold_since = $_GET['most_sold_since'];
$resultObj->products = getMostSoldSince($_GET['most_sold_since'], $_GET['limit']);

echo json_encode($resultObj);