<?PHP
#
#   FILE: SavedSearch.php
#
#   Part of the Collection Workflow Integration System (CWIS)
#   Copyright 2011 Internet Scout Project
#   http://scout.wisc.edu/
#

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

function PrintSavedSearches()
{
    global $SysConfig;
    global $User;

    $SSFactory = new SavedSearchFactory();
    $Searches = $SSFactory->GetSearchesForUser($User->Id());
    $NumColumns = $SysConfig->UserAgentsEnabled() ? 5 : 4;

    if ($User->HasPriv(PRIV_RESOURCEADMIN))
    {
        $FreqList = SavedSearch::GetSearchFrequencyList();
    }
    else
    {
        $FreqList = SavedSearch::GetSearchFrequencyList(SavedSearch::SEARCHFREQ_HOURLY);
    }

    # if no saved searches found
    if (count($Searches) < 1)
    {
        # add message indicating no saved searches
        ?>
        <tr>
            <td colspan="<?PHP print $NumColumns; ?>">
                There are no searches currently saved.
                You can enter and save a search via the
                <a href="index.php?P=Advanced">Advanced Search</a> page.
            </td>
        </tr>
        <?PHP
    }
    else
    {
        # add table row for each saved search
        foreach ($Searches as $SearchId => $Search)
        {
            ?>

            <tr>
                <th><a href="index.php?P=AdvancedSearch&amp;Q=Y&amp;<?PHP
                   print $Search->GetSearchGroupsAsUrlParameters();?>"
                ><?PHP  print($Search->SearchName());  ?></a></th>
                <td>
                <a class="cw-button cw-button-elegant cw-button-constrained"
                    href="index.php?P=Advanced&amp;SI=<?PHP
                   print($SearchId);  ?>">Edit</a>
                </td>
                <td>
                <a class="cw-button cw-button-elegant cw-button-constrained"
                    href="index.php?P=SavedSearch&amp;AC=Delete&amp;SI=<?PHP
                         print($SearchId);  ?>">Delete</a>
                </td>

                <?PHP  if ($SysConfig->UserAgentsEnabled()) {  ?>
                <td><?PHP  PrintOptionList("F_Frequency_".$SearchId, $FreqList, $Search->Frequency(), TRUE);  ?></td>
                <?PHP  }  ?>

                <td><?PHP  print($Search->GetSearchGroupsAsTextDescription(NULL, TRUE, FALSE));  ?></td>
            </tr>

        <?PHP
        }
    }
}

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

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

include_once "lib/AxisPHP/Axis--StandardLibrary.php";  # (for PrintOptionList())

if (!CheckAuthorization()) {  return;  }
PageTitle("Manage Saved Searches");

# if user asked to save new search
if (isset($_POST["Submit"]) && $_POST["Submit"] == "Save")
{
    # if reasonable search name was supplied
    if (isset($_POST["F_SearchName"]) && strlen(trim($_POST["F_SearchName"])))
    {
        $FreqList = SavedSearch::GetSearchFrequencyList();
        $F_SearchFreq = isset($_POST["F_SearchFreq"])
            ? intval($_POST["F_SearchFreq"]) : NULL;
        $Frequency = isset($FreqList[$F_SearchFreq])
            ? $F_SearchFreq : SavedSearch::SEARCHFREQ_NEVER;

        # save search
        $NewSavedSearch = new SavedSearch(NULL, $_POST["F_SearchName"],
                $User->Id(), $Frequency,
                SavedSearch::TranslateUrlParametersToSearchGroups($_GET));
    }
    else
    {
        # set error message and return back to new saved search page
        $ErrorMessage = "ERROR: No name was supplied for the saved search.";
        $Session->PassVariable("ErrorMessage", $ErrorMessage);
        $SearchGroups = SavedSearch::TranslateUrlParametersToSearchGroups($_GET);
        $SearchGroupsForUrl = SavedSearch::TranslateSearchGroupsToUrlParameters($SearchGroups);
        $AF->SetJumpToPage("NewSavedSearch&".$SearchGroupsForUrl);
        return;
    }
}

# else if user asked to delete search
elseif (isset($_GET["AC"]) && ($_GET["AC"] == "Delete"))
{
    # delete specified search
    $Search = new SavedSearch($_GET["SI"]);
    $Search->Delete();
}

# else if user asked to change search frequency
elseif (isset($_POST["AC"]) && ($_POST["AC"] == "ChangeFrequency"))
{
    # for each saved search
    $SSFactory = new SavedSearchFactory();
    $Searches = $SSFactory->GetSearchesForUser($User->Id());
    foreach ($Searches as $Search)
    {
        # if frequency has changed
        if (isset($_POST["F_Frequency_".$Search->GetSearchId()])
                && ($_POST["F_Frequency_".$Search->GetSearchId()] != $Search->Frequency()))
        {
            # update frequency
            $Search->Frequency($_POST["F_Frequency_".$Search->GetSearchId()]);
        }
    }
}
