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

$FieldName = isset($_GET["FieldName"]) ? $_GET["FieldName"] : NULL;
PageTitle($FieldName." Display");

# ----- EXPORTED FUNCTIONS ---------------------------------------------------

/**
* Print field name.
*/
function PrintFieldName()
{
    global $FieldName;

    print $FieldName;
}


/**
* Print resource title.
*/
function PrintResourceTitle()
{
    global $ResourceTitle;

    print $ResourceTitle;
}

/**
* print Resource Url.
*/
function PrintUrl()
{
    global $Url;

    print $Url;
}

/**
* Print link to resource record.
*/
function PrintRecordLink()
{
    global $RecordLink;

    print $RecordLink;
}

/**
* Print ReturnTo page.
*/
function PrintReturnPage()
{
    global $ReturnPage;

    print $ReturnPage;
}

/**
* Print image height.
*/
function PrintHeight()
{
    global $Height;

    print $Height;
}

/**
* print image Width.
*/
function PrintWidth()
{
    global $Width;

    print $Width;
}

/**
* print AltText for image.
*/
function PrintAltText()
{
    global $AltText;

    print $AltText;
}


# ----- LOCAL FUNCTIONS ------------------------------------------------------

# ----- MAIN -----------------------------------------------------------------

# non-standard global variables
global $AltText;
global $FieldName;
global $Height;
global $RecordLink;
global $ResourceTitle;
global $ReturnPage;
global $Url;
global $Width;

if (!isset($_GET["ResourceId"]) || (!isset($_GET["FieldName"]) && !isset($_GET["FI"])))
{
    $AF->SetJumpToPage("Home");
}
else
{
    if (!Resource::ItemExists($_GET["ResourceId"]))
    {
        $AF->SetJumpToPage("Home");
    }
    else
    {
        $Resource = new Resource($_GET["ResourceId"]);
        $Schema = new MetadataSchema($Resource->SchemaId());
        $TitleField = $Schema->GetFieldByMappedName("Title");

        $ResourceTitle = $Resource->UserCanViewField($User, $TitleField)
                ? $Resource->Get($TitleField) : "";
        if (array_key_exists("FI", $_GET))
        {
            $Field = $Schema->GetField($_GET["FI"]);
        }
        else
        {
            $Field = $Schema->GetFieldByName($_GET["FieldName"]);
        }
        if ($Field == NULL)
        {
            $AF->SetJumpToPage("Home");
        }
        else
        {
            # retrieve any supplied image ID
            $ImageId = GetFormValue("ID");

            # if no image ID supplied
            if ($ImageId == NULL)
            {
                # retrieve images for this field
                $ImageIds = $Resource->Get($Field);

                # if images were found
                if (count($ImageIds))
                {
                    # use first image
                    $ImageId = array_shift($ImageIds);
                }
            }

            # if no image ID available
            if ($ImageId == NULL)
            {
                # bail out to home page
                $AF->SetJumpToPage("Home");
            }
            else
            {
                $SPTImage = new SPTImage($ImageId);
                $Width = $SPTImage->Width();
                $Height = $SPTImage->Height();
                $Url = $SPTImage->Url();
                $AltText = $SPTImage->AltText();

                if (!$Resource->UserCanViewField($User, $Field) ||
                    !$Resource->UserCanModifyField($User, $Field) )
                {
                    if (isset($_GET["edit"]) && $_GET["edit"] == 1)
                    {
                        $AF->SetJumpToPage("index.php?P=EditResource&ID="
                                .$Resource->Id());
                    }
                    else
                    {
                        $AF->SetJumpToPage("index.php?P=FullRecord&ID="
                                .$Resource->Id());
                    }
                }

                # return to metadata tool
                if (isset($_GET["edit"]))
                {
                    $RecordLink = "index.php?P=EditResource&ID=".$Resource->Id();
                    $ReturnPage = "Edit Resource for ";
                }
                # return to full record
                else
                {
                    $RecordLink = "index.php?P=FullRecord&ID=".$Resource->Id();
                    $ReturnPage = "Full Record for ";
                }
            }
        }
    }
}
