<?php

// Load Elgg engine
include_once dirname(dirname(dirname(__FILE__))) . "/engine/start.php";
include_once dirname(__FILE__) . "/actions/API_connection.php";
include_once dirname(__FILE__) . "/custom/MMDSearchResults.php";
// make sure only logged in users can see this page
gatekeeper();
// get the form input
$keyword = get_input('keyword');
// set the title
$title = "Search for distributed educational resources";
// start building the main column of the page
$area2 = elgg_view_title($title);
//display submenus
$area1 = $address = $CONFIG->DISTRIBUTED_API_URL . "searchservices";
$result = connectToSesame($address);
$searchResults = new MMDSearchResults("services", $result, true);
// Add the list of results
$area2 .= elgg_view("mmdsearch/interface", array("services" => $searchResults->getServices()));
// layout the page
$body = elgg_view_layout('two_column_left_sidebar', '', $area2);
// draw the page
page_draw($title, $body);
    $subject = urlencode(get_input('subject'));
    $address = $CONFIG->DISTRIBUTED_API_URL . "searchservices?lang=" . $lang . "&sub=" . $subject;
} elseif ($searchType == "data") {
    $query = urlencode(get_input('query'));
    $service_uri = urlencode(str_replace("&amp;", "&", get_input('service_uri')));
    $service_lifting = urlencode(str_replace("&amp;", "&", get_input('service_lifting')));
    $lcms_address = get_input('lcms_address');
    $lifting_address = get_input('lifting_address');
    $address = $CONFIG->DISTRIBUTED_API_URL . "serviceresponse?query=" . $query . "&uri=" . $service_uri . "&lifting=" . $service_lifting;
}
//echo $address . "\n";
//link to the API
$result = connectToSesame($address);
//echo "result = ".$result;
//exit();
$searchResults = new MMDSearchResults($searchType, $result, true);
$searchResults->service_uri = get_input('service_uri');
//print_r($searchResults);
//exit();
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
if ($searchType == "services") {
    header('Content-type: text/html');
    echo $searchResults->displayServices();
} else {
    $response = new stdClass();
    $response->searchID = $searchID;
    $response->searchType = $searchType;
    $response->__elgg_ts = time();
    $response->__elgg_token = elgg_view('ajax/securitytoken');
    $response->results = $searchResults->resultsDisplayList();