<?php
$dbuser = "";
$dbpass = "";
$db = "dspace";

$base_url = ""; // Not including trailing slash.
$cutoff = 25;   // The cutoff for non-matching items
$handle = 1765; // The handle used within DSpace

$conn = pg_connect("dbname=$db user=$dbuser password=$dbpass");

if ($conn === FALSE) {
        print("Connection failed!\n");
        exit();
}

// These are types used by DARE, which are based on METIS/Dublin Core type.
$dare_types = array("Annotation",
                "Annual report",
                "Article",
                "Article in monograph - proceedings",
                "Book",
                "Book editorial",
                "Book review",
                "Commission report or memorandum",
                "Conference report",
                "Dataset",
                "Doctoral thesis",
                "Educational material",
                "External research report",
                "Farewell lecture",
                "Inaugural lecture",
                "Internal report",
                "Letter to the editor",
                "Newsletter",
                "Newspaper article",
                "Part of book - chapter",
                "Preprint",
                "Research paper",
                "Sound");

$sql = "SELECT DISTINCT text_value
        FROM                dcvalue
        WHERE                dc_type_id = 66
        ORDER BY        text_value;";

$results = pg_query($conn, $sql);
if ($results === FALSE) {
        print("Something went wrong while executing the SQL query.\n");
}

$rows = pg_fetch_all($results);
if ($rows === FALSE) {
        print("Something went wrong while fetching the results.\n");
}

$type = array();

print("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n  \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
print("<html><head><title>DSpace type overview</title></head><body>");

// Colours, first for matching, second for non-matching.
$colour1 = "#ccffcc";
$colour2 = "#bfddbc";

$fcolour1 = "#ffffcc";
$fcolour2 = "#ffddbc";

print("<table>");
print("<tr><th>Type</th><th>Number of occurences</th><th>DARE compliant?</th><th>Item id</th></tr>");

$i = 0;        // Used to alternately colour the rows.
foreach ($rows as $row) {
        $type_desc = $row['text_value']; // copy the description over for reuse

        $sql = "SELECT COUNT(item_id)
                FROM   dcvalue
                WHERE  text_value = '$type_desc'
                AND    dc_type_id = 66;";

        $results = pg_query($conn, $sql);
        if ($results === FALSE) {
                print("Something went wrong while executing the SQL query.\n");
        }

        $count = pg_fetch_assoc($results);
        if ($count === FALSE) {
                print("Something went wrong while fetching the results.\n");
        }

        $compliant = "No";
        foreach ($dare_types as $dare_type) {
                if ($type_desc === $dare_type) {
                        $compliant = "Yes";
                        break;
                }
        }

        if ($compliant === "Yes") {
                $row_colour = ($i % 2) ? $colour1 : $colour2; 
        } else {
                $row_colour = ($i % 2) ? $fcolour1 : $fcolour2; 
        }

        if ($compliant === "No") {
                if (intval($count['count']) <= $cutoff) {
                        $sql = "SELECT         item_id 
                                FROM         dcvalue
                                WHERE         text_value = '$type_desc'
                                AND         dc_type_id = 66
                                ORDER BY item_id;";

                        $results = pg_query($conn, $sql);
                        if ($results === FALSE) {
                                print("Something went wrong while executing the SQL query.\n");
                        }

                        $items = pg_fetch_all($results);
                        if ($items === FALSE) {
                                print("Something went wrong while fetching the results.\n");
                        }

                        $item_num = "";
                        foreach ($items as $item) {
                                $item_num = $item_num . "<a href=\"$base_url/tools/edit-item?handle=$handle%2F&amp;item_id={$item['item_id']}&amp;submit=Find\">{$item['item_id']}</a> ";
                        }
                } else {
                        $item_num = "Too many to list";
                }
        } else {
                $item_num = "Not needed";
        }

        print("<tr bgcolor=\"$row_colour\"><td>{$row['text_value']}</td>");
        print("<td>{$count['count']}</td>");
        print("<td>$compliant</td>");
        print("<td>$item_num</td></tr>");

        $i++;
}

print("</table>");

$status = pg_close($conn);
if ($status === FALSE) {
        print("Could not successfully close the connection?!\n");
}

print("</body>");
print("</html>");
?>