<?php

class freewheeling_displayPhotos
{
    static public function displayPhotos($attr = "")
    {
        global $eol;
        global $wpdbExtra, $rrw_photos;
        global $photoUrl, $photoPath, $thumbUrl, $thumbPath,
            $highresUrl, $highresPath;
        global $selectDisplay; // a user readable of the selection citeris
        $msg = "";
        $debugProgress = false;
        $msg .= SetConstants("displayphotos");
        if ($debugProgress) $msg .= "photos go here$eol";
        $sqlFind = self::buildSql($attr);
        if (empty($sqlFind))
            return "$eol $msg - No Selection made $eol ";
        $recFiles = $wpdbExtra->get_resultsA($sqlFind);
        if ($wpdbExtra->num_rows > 0)
            $msg .= " <strong>Click photo to enlage.</strong>  -- found $wpdbExtra->num_rows photos,  $selectDisplay. $eol";
        else
            $msg .= " <strong>No photos found. </strong> -- $selectDisplay. $eol";
        if ($debugProgress) $msg .= "$eol $sqlFind $eol";
        $msg .= "<ul class='rrwPhotoGrid' role='list'>";
        $trailermsg = "";
        foreach ($recFiles as $recFile) {
            $photoname = $recFile["photoname"];
            $photographer = $recFile["photographer"];
            $owner = $recFile["owner"];
            $thumbname = "{$photoname}_tmb.jpg";
            $imageTmb = "$thumbUrl/$thumbname";
            //   $msg .= "image file is at imageTmb $eol ";;
            $msg .= "\n<li> ";
            if (!file_exists("$thumbPath/$thumbname")) {
                $msg .= "<a href='display-one-photo?photoname=$photoname' > $photoname $eol not found</a> $eol ";
            } else {
                $meta = getimagesize("$thumbPath/$thumbname");
                $onePhotoUrl = site_url("/display-one-photo");
                $msg .= "<img src='$imageTmb' alt='thumb nail image' $meta[3]
                onclick='openOnePhotopage(\"$photoname\")' />";
                if (freewheeling_fixit::allowedEdit($owner))
                    $msg .= "<br /><span class='rrwCaption'> $photoname</span>";
            }
            $msg .= "</li>";
        }
        //onmouseover='this.width=800'  onmouseout='this.width=150'
        //           onmouseclick='openOnePhotopage(\"$onePhotoUrl\")'
        //         <a href = '$onePhotoUrl?photoname=$filename'>
        $msg .= "\n</ul>$eol";
        if ($wpdbExtra->num_rows > 8) {
            $msg .= "
            <script >
              ifram = document.getElementById('photohere');
              ifram.scrollIntoView();
        </script>
            ";
        }
        return $msg;
    }
    private static function buildSql($attr)
    {
        global $rrw_keywords, $rrw_photos;
        global $rrw_portrait, $rrw_landscape;
        global $eol;
        global $selectDisplay; // a user readable of the selection citeris
        $debug = false;
        $debugOutput = false;
        $msg = "";
        $selectDisplay = "";
        if ($debug) print "query " . $_SERVER['QUERY_STRING'] . $eol;
        if ($debug) print rrwUtil::print_r($_POST, true, "_POST");
        $searchdropdown = rrwParam::String("searchdropdown", $attr);
        $trail = trim(rrwParam::String("trail", $attr));
        if ($debug) print "trail = $trail $eol";
        if ("Any Trail" == $trail)
            $trail = "";
        if ($debug) print "found searchdropdown of $searchdropdown $eol";
        // case of random selection
        switch ($searchdropdown) {
            case "random_21":
                $limit = 21;
                $sql = "SELECT photoname, photographer, owner FROM $rrw_photos
                        where photostatus = 'use' order by (rand() * 10000) limit $limit";
                if ($debugOutput) print "case random - $sql $eol";
                $selectDisplay = "Selection: $limit random photos ";
                return $sql;
            case "landscape":
                $sql = "SELECT photoname, photographer, owner FROM $rrw_landscape";
                $selectDisplay = "Selection: photos in landscape format ";
                if ($trail == "%")
                    return $sql;
                else {
                    $selectDisplay .= " on $trail";
                    $sql .= " where trail_name = '$trail' ";
                    if ($debugOutput)
                        print "case dropdown - $sql $eol";
                    return $sql;
                }
            case "portrait":
                $sql = "SELECT photoname, photographer, owner FROM $rrw_portrait";
                $selectDisplay = "Selection: photos in portrait format ";
                if ($trail == "%")
                    return $sql;
                else {
                    $selectDisplay .= " on $trail";
                    $sql .= " where trail_name = '$trail' ";
                    return $sql;
                }
            case "%":
                // any detail selected
                if ($trail == "%")
                    return ""; // no selection made
                $sql = "select photoname, photographer, owner from $rrw_photos";
                if ($trail == "%")
                    return $sql;
                else {
                    $selectDisplay .= " on $trail";
                    $sql .= " where trail_name = '$trail' ";
                    return $sql;
                }
            default:
                $sql = "select photoname, photographer, owner from $rrw_photos";
                break;
        } // end switch
        // not a request for random, portrait,landscape or all images
        if (!empty($searchdropdown)) {
            // case of dropdown selection
            if ($debug) print "trail= $trail, searchdropdown= $searchdropdown$eol";
            $sqlSearch = "$sql
                where photoname in (
                select keywordfilename from $rrw_keywords
                where keyword = '$searchdropdown' )";
            $selectDisplay = " with keyword $searchdropdown ";
            if ($trail == '%') {
                $selectDisplay .= " and any trail ";
            } else {
                $selectDisplay .= " and trail name = $trail";
                $sqlSearch .= " and trail_name = '$trail' ";
            }
            if ($debugOutput)  print "case dropdown - $sqlSearch $eol";
            return $sqlSearch;
        }
        $directory = rrwUtil::fetchparameterString("directory", $attr);
        $photoname = rrwUtil::fetchparameterString("photoname", $attr);
        $keyword = rrwUtil::fetchparameterString("keyword", $attr);
        $location = rrwUtil::fetchparameterString("location", $attr);
        $people = rrwUtil::fetchparameterString("people", $attr);
        $photographer = rrwUtil::fetchparameterString("photographer", $attr);
        // not a request for from dropdown select
        // must be from "search page"
        $where = ""; // build a complicated or search
        if ($debug) print "trail = $trail $eol";
        foreach (
            array(
                "trail_name" => $trail,
                "location" => $location,
                "people" => $people,
                "photographer" => $photographer,
                "direonp" => $directory,
                "photoname" => $photoname,
            ) as $field => $item
        ) {
            if (!empty($item)) {
                $where .= " $field like '%$item%' or";
                $selectDisplay .= " $field = $item or ";
            }
            if ($debug) print "$field - $item -- $where ";
        }
        $selectDisplay = substr($selectDisplay, 0, -3);
        if ($debug) print "Where = $where $eol";
        if ("or" == substr($where, -2, 2))
            $where = substr($where, 0, strlen($where) - 2);
        $sql = "select photoname, photographer, owner from $rrw_photos
                where $where
                and photostatus = 'use'  ";
        if ($debug) print "$sql $eol ";
        if (empty($where))
            return ""; // no searchitems found
        if ($debugOutput) print "case multi search = $sql $eol ";
        return $sql;
    } // function buildSql()
} // end class
