<?PHP
#
#   FILE:  ApprovalQueue.php
#
#   Part of the Collection Workflow Integration System (CWIS)
#   Copyright 2002-2013 Edward Almasy and Internet Scout Research Group
#   http://scout.wisc.edu/cwis/
#

if (!CheckAuthorization(PRIV_RESOURCEADMIN, PRIV_MYRESOURCEADMIN, PRIV_RELEASEADMIN)) {  return;  }

$DB = new Database();

if (isset($_GET["Id"]) && isset($_GET["A"]))
{
    $ResourceId = intval($_GET["Id"]);
    $Action = $_GET["A"];

    if ($Action == "Approve" )
    {
        $Resource = new Resource($ResourceId);

        $ImageId = $DB->Query("SELECT ImageId FROM AutoScreenshot_ApprovalQueue "
                              ."WHERE ResourceId=".$ResourceId,"ImageId");

        $Resource->Set("Screenshot", $ImageId);

        global $G_User;
        $Schema = new MetadataSchema();

        $TimestampFields = $Schema->GetFields(
            MetadataSchema::MDFTYPE_TIMESTAMP);
        foreach ($TimestampFields as $Field)
        {
            if ($Field->UpdateMethod() ==
                MetadataField::UPDATEMETHOD_ONRECORDCHANGE)
            {
                $Resource->Set($Field, "now");
            }
        }

        $Resource->Set("Last Modified By Id", $G_User);

        $DB->Query("DELETE FROM AutoScreenshot_ApprovalQueue "
                   ."WHERE ResourceId=".$ResourceId);
    }
    elseif ($Action == "Retry" )
    {
        $ImageId = $DB->Query("SELECT ImageId FROM AutoScreenshot_ApprovalQueue "
                              ."WHERE ResourceId=".$ResourceId,"ImageId");

        if ($ImageId !== NULL)
        {
            $ThisImage = new SPTImage($ImageId);
            $ThisImage->Delete();
        }

        $DB->Query("DELETE FROM AutoScreenshot_ApprovalQueue "
                   ."WHERE ResourceId=".$ResourceId);
        $DB->Query("DELETE FROM AutoScreenshot_Failures "
                   ."WHERE ResourceId=".$ResourceId);
    }
    elseif ($Action == "Blacklist")
    {
        $ImageId = $DB->Query("SELECT ImageId FROM AutoScreenshot_ApprovalQueue "
                              ."WHERE ResourceId=".$ResourceId,"ImageId");

        if ($ImageId !== NULL)
        {
            $ThisImage = new SPTImage($ImageId);
            $ThisImage->Delete();
        }

        $DB->Query("DELETE FROM AutoScreenshot_ApprovalQueue "
                   ."WHERE ResourceId=".$ResourceId);
        $DB->Query("DELETE FROM AutoScreenshot_Failures "
                   ."WHERE ResourceId=".$ResourceId);
        $DB->Query("INSERT INTO AutoScreenshot_Blacklist (ResourceId) "
                   ."VALUES (".$ResourceId.")");
    }
    elseif ($Action == "Deblacklist")
    {
        $DB->Query("DELETE FROM AutoScreenshot_Blacklist "
                   ."WHERE ResourceId=".$ResourceId);
    }
}

if (!isset($_GET["Ajax"]))
{
    global $AF;
    $AF->SetJumpToPage("P_AutoScreenshot_ApprovalQueue");
}

?>
