File: search.inc.php

package info (click to toggle)
zoph 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,632 kB
  • sloc: php: 28,044; javascript: 10,435; sql: 527; sh: 153; makefile: 4
file content (733 lines) | stat: -rw-r--r-- 29,091 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
<?php
/**
 * Photo Search
 *
 * This file converts a set of http request vars into an SQL query
 * in this way, a selection of photos can be made, based on constraints
 * This file is more or less the core of Zoph. Any modification should
 * be made with extreme care!
 *
 * This file is part of Zoph.
 *
 * Zoph is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Zoph is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Zoph; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @package Zoph
 * @author Jason Geiger
 * @author Jeroen Roos
 */

namespace photo;

use album;
use category;
use PDO;
use person;
use photo;
use place;
use set\model as set;
use user;

use db\select;
use db\param;
use db\clause;
use db\selectHelper;

use conf\conf;

/**
 * Photo Search
 *
 * This class converts a set of http request vars into an SQL query
 * in this way, a selection of photos can be made, based on constraints
 *
 * @package Zoph
 * @author Jason Geiger
 * @author Jeroen Roos
 */
class search {
    /** @var Valid comparison operators */
    const OPS       = array("=", "!=", "less than", "more than", ">", ">=",
                        "<", "<=", "like", "not like", "is in photo", "is not in photo");
    /** @var Valid conjunction operators */
    const CONJ      = array("and", "or");
    /** @var Valid sort directions */
    const SORTDIR   = array("asc", "desc");
    /** @var Valid search fields */
    const FIELDS    = array("location_id", "rating", "photographer_id",
                        "date", "time", "timestamp", "name", "path", "title", "view", "description",
                        "width", "height", "size", "aperture", "camera_make", "camera_model",
                        "compression", "exposure", "flash_used", "focal_length", "iso_equiv", "metering_mode");
    /** @var Valid text search fields */
    const TEXT      = array("album", "category", "person", "photographer");

    /** @var \db\query Holds the query */
    private $qry;
    /** Holds the variables that are used to build the constraint */

    /** @var array holds the request vars */
    private $vars;

    /**
     * Create seach object based on http request vars
     * @param array vars http request vars
     */
    public function __construct(array $vars) {

        $this->qry = new select(array("p" => "photos"));
        $this->vars = $vars;
        $this->processVars();
        $this->setOrder();
    }

    /**
     * Get the resulting query
     * @return \db\query SQL query that can be used to get photos from database
     */
    public function getQuery() {
        return $this->qry;
    }

    /**
     * Process the fields needed to determine the ORDER in the SQL query
     */
    private function setOrder() {
        if (isset($this->vars["_order"])) {
            $order = $this->vars["_order"];
        } else {
            $order = conf::get("interface.sort.order");
        }

        if (isset($this->vars["_dir"])) {
            $dir = $this->vars["_dir"];
        } else {
            $dir = conf::get("interface.sort.dir");
        }

        if (!in_array(strtolower($dir), static::SORTDIR)) {
            throw new \illegalValueSecurityException("Illegal sort direction: " . e($dir));
        }

        if (isset($this->vars["_random"])) {
            // get one random result
            $this->qry->addOrder("rand()");
            $this->qry->addLimit(1);

        } else if ($order == "set_order") {
            $this->qry->addOrder("ps.photo_order " . $dir);
        } else {
            $this->qry->addFields(array($order));
            $this->qry->addOrder("p." . $order . " " . $dir);
            if ($order == "date") {
                $this->qry->addFields(array("p.time"));
                $this->qry->addOrder("p.time " . $dir);
            }
            $this->qry->addOrder("p.photo_id " . $dir);
        }
    }

    /**
     * Process variables
     * This function loops over all the variables and adds the various
     * contstraints (clauses) to the SQL query
     */
    private function processVars() {

        foreach ($this->vars as $key => $val) {
            if (empty($key) || empty($val) || $key[0] == "_" || strpos(" $key", "PHP") == 1) {
                continue;
            }
            if (is_array($val)) {
                foreach ($val as $i => $arVal) {
                    $conj = $this->getConj($key, $i);
                    $op = $this->getOp($key, $i);
                    $value = $this->getChildren($key, $i);
                    $this->processFields($key, $value, $i, $op, $conj);
                }
            } else {
                $conj = $this->getConj($key);
                $op = $this->getOp($key);
                $value = $this->getChildren($key);
                $this->processFields($key, $value, "", $op, $conj);
            }

        }
        $this->qry = selectHelper::expandQueryForUser($this->qry, user::getCurrent());

        $distinct=true;
        $this->qry->addFields(array("p.photo_id"), $distinct);
        $this->qry->addFields(array("p.name", "p.path", "p.width", "p.height"));
    }

    private function processFields($key, $val, $suffix, $op, $conj) {
        if ($key == "text") {
            $field = $this->vars["_" . $key][$suffix];

            if (!in_array($field, static::TEXT)) {
                throw new \illegalValueSecurityException("Illegal text search: " . e($field));
            }

            $val = e($this->vars[$key][$suffix]);
            $key = e($field);
        }

        // the regexp matches a list of numbers, separated by commas.
        if (!is_array($val) && preg_match("/^([0-9]+)(,([0-9]+))+$/", $val)) {
            $val=explode(",", $val);
        }

        if ($key == "person" || $key == "photographer") {
            $this->processPerson($key, $val, $suffix, $conj);
            // continue, because processPerson already modifies the query
            return;
        } else if ($key == "album") {
            $key = "album_id";
            $val = $this->processAlbum($val);
            if ($op == "like") {
                $op = "=";
            } else if ($op == "not like") {
                $op = "!=";
            }
        } else if ($key == "category") {
            $key = "category_id";
            $val = $this->processCategory($val);
            if ($op == "like") {
                $op = "=";
            } else if ($op == "not like") {
                $op = "!=";
            }
        }

        switch ($key) {
        case "album_id":
            $this->processAlbumId($val, $suffix, $op, $conj);
            break;
        case "category_id":
            $this->processCategoryId($val, $suffix, $op, $conj);
            break;
        case $key == "location_id":
            $this->processLocationId($val, $suffix, $op, $conj);
            break;
        case $key == "person_id":
            $this->processPersonId($val, $suffix, $op, $conj);
            break;
        case $key == "set_id":
            $this->processSetId($val, $suffix, $op, $conj);
            break;
        case $key == "userrating":
            $this->processUserRating($val, $suffix, $conj);
            break;
        case $key=="rating":
            $this->processRating($val, $suffix, $op, $conj);
            break;
        case $key=="lat":
            $lat=(float) $val;
            $lon=(float) $this->vars["lon"];
            $this->processLatLon($lat, $lon, $suffix, $conj);
            break;
        case $key=="lon":
            // already processed with lat
            break;
        default:
            $this->processOtherFields($key, $val, $suffix, $op, $conj);
        }


    }

    private function getConj(string $key, int $index = null) {
        $conj = $this->vars["_" . $key . "_conj"] ?? "and";
        if (is_array($conj) && $index !== null) {
            $conj = $this->vars["_" . $key . "_conj"][$index] ?? "and";
        }
        if ($conj == "") {
            $conj = "and";
        }
        if (!in_array($conj, static::CONJ)) {
            throw new \illegalValueSecurityException("Illegal conjunction: " . e($conj));
        }
        return $conj;
    }

    private function getOp(string $key, int $index = null) {
        $op = $this->vars["_" . $key . "_op"] ?? "=";
        if (is_array($op) && $index !== null) {
            $op = $this->vars["_" . $key . "_op"][$index] ?? "=";
        }
        if ($op == "") {
            $op = "=";
        }
        if (!in_array($op, static::OPS)) {
            throw new \illegalValueSecurityException("Illegal operator: " . e($op));
        }

        if (($key == "album_id" || $key == "category_id") && $op == "like") {
            $op = "=";
        } else if (($key == "album_id" || $key == "category_id") && $op == "not like") {
            $op = "!=";
        }
        return $op;
    }

    private function getChildren(string $key, int $index = null) {
        $children = $this->vars["_" . $key . "_children"] ?? false;
        if (is_array($children) && $index !== null) {
            $children = $this->vars["_" . $key . "_children"][$index] ?? false;
            if ($children) {
                $object=explode("_", $key);
                if ($object[0]=="location") {
                    $object[0] = "place";
                }
                $obj=new $object[0]($this->vars[$key][$index]);
                return $obj->getBranchIdArray();
            }
        } else if ($index !== null) {
            return $this->vars[$key][$index];
        }
        return $this->vars[$key];
    }

    /**
     * This can be used to reference persons by name directly from the URL
     * it's not actually used in Zoph and it's not well documented.
     * But it could be used to create a URL like http://www.zoph.org/search.php?person=Jeroen Roos
     * With the help of url rewrite, one could even change that into something like
     * http://www.zoph.org/person/Jeroen Roos
     * @param string key name of the field (person|photographer)
     * @param string val value of the field
     * @param string suffix, the suffix can be used to search for the same field multiple times
     * @param string conj, conjugation, whether this is an AND or OR search
     */
    private function processPerson($key, $val, $suffix, $conj) {
        $people = person::getByName($val, true);

        $peopleIds=array();

        if ($people && count($people) > 0) {
            foreach ($people as $person) {
                $peopleIds[]=$person->getId();
            }
        } else {
            // the person did not exist, no photos should be found
            // however, we can't just return 0 here, as there may be an OR clause in the query...
            $peopleIds[]=-1;
        }

        $param=new param(":peopleIds" . $suffix, $peopleIds, PDO::PARAM_INT);
        $this->qry->addParam($param);
        if ($key=="person") {
            $alias = "pp" . $suffix;
            $this->qry->addClause(clause::InClause($alias . ".person_id", $param), $conj);
            $this->qry->join(array($alias => "photo_people"), "p.photo_id=" . $alias . ".photo_id");
        } else if ($key=="photographer") {
            $this->qry->addClause(clause::InClause("photographer_id", $param), $conj);
        }
    }

    /**
     * Search for album by name
     * @param string val value of the field
     * @return int|array album_id or array of album_ids
     */
    private function processAlbum($val) {
        $album=album::getByNameHierarchical($val);
        if ($album instanceof album) {
            $val=$album->getId();
        } else if (is_array($album)) {
            $val=array();
            foreach ($album as $alb) {
                $val[]=$alb->getId();
            }
        } else {
            // the album did not exist, no photos should be found
            // however, we can't just return 0 here, as there may be an OR clause in the query...
            $val=-1;
        }
        return $val;
    }

    /**
     * Search for category by name
     * @param string val value of the field
     * @return int|array category_id or array of category_ids
     */
    private function processCategory($val) {
        $category=category::getByNameHierarchical($val);
        if ($category instanceof category) {
            $val=$category->getId();
        } else if (is_array($category)) {
            $val=array();
            foreach ($category as $cat) {
                $val[]=$cat->getId();
            }
        } else {
            // the category did not exist, no photos should be found
            // however, we can't just return 0 here, as there may be an OR clause in the query...
            $val=-1;
        }
        return $val;
    }

    /**
     * Search for album by id
     * @param string val value of the field
     * @param string suffix, the suffix can be used to search for the same field multiple times
     * @param string operator, how the values should be compared (=, !=, like, etc.)
     * @param string conj, conjugation, whether this is an AND or OR search
     */
    private function processAlbumId($val, $suffix, $op, $conj) {
        if ($op == "=") {
            $alias = "pa" . $suffix;
            /*
             * Because the query builder expects the photo_album table to be aliased to "pa",
             * the first occurence does not have number suffix
             */
            if ($alias=="pa0") {
                $alias="pa";
            }
            $this->qry->join(array($alias => "photo_albums"), "p.photo_id=" . $alias . ".photo_id");
            if (is_numeric($val)) {
                $this->qry->addClause(new clause($alias . ".album_id=:albumId" . $suffix), $conj);
                $this->qry->addParam(new param(":albumId" . $suffix, (int) $val, PDO::PARAM_INT));
            } else if (is_array($val)) {
                $param=new param(":albumIds" . $suffix, $val, PDO::PARAM_INT);
                $this->qry->addParam($param);
                $this->qry->addClause(clause::InClause($alias . ".album_id", $param), $conj);
            } else {
                throw new \keyMustBeNumericSecurityException("album_id must be numeric");
            }
        } else {
            // assume "not in"
            $exclAlbumsQry=new select(array("p" => "photos"));
            $exclAlbumsQry->addFields(array("photo_id"), true);

            $exclAlbumsQry->join(array("pa" => "photo_albums"), "p.photo_id=pa.photo_id");

            $param=new param(":albumIds" . $suffix, (array) $val, PDO::PARAM_INT);
            $exclAlbumsQry->addParam($param);
            $exclAlbumsQry->where(clause::InClause("pa.album_id", $param));

            $exclPhotoIds=$exclAlbumsQry->toArray();

            $param=new param(":photoIds" . $suffix, (array) $exclPhotoIds, PDO::PARAM_INT);
            $this->qry->addParam($param);
            $this->qry->addClause(clause::NotInClause("p.photo_id", $param), $conj);
        }
    }

    /**
     * Search for set by id
     * @param string val value of the field
     * @param string suffix, the suffix can be used to search for the same field multiple times
     * @param string operator, how the values should be compared (=, !=, like, etc.)
     * @param string conj, conjugation, whether this is an AND or OR search
     */
    private function processSetId($val, $suffix, $op, $conj) {
        if ($op == "=") {
            $alias = "ps" . $suffix;
            /*
             * Because the query builder expects the photo_sets table to be aliased to "ps",
             * the first occurence does not have number suffix
             */
            if ($alias=="ps0") {
                $alias="ps";
            }
            $this->qry->join(array($alias => "photo_sets"), "p.photo_id=" . $alias . ".photo_id");
            if (is_numeric($val)) {
                $this->qry->addClause(new clause($alias . ".set_id=:setId" . $suffix), $conj);
                $this->qry->addParam(new param(":setId" . $suffix, (int) $val, PDO::PARAM_INT));
            } else if (is_array($val)) {
                $param=new param(":setIds" . $suffix, $val, PDO::PARAM_INT);
                $this->qry->addParam($param);
                $this->qry->addClause(clause::InClause($alias . ".set_id", $param), $conj);
            } else {
                throw new \keyMustBeNumericSecurityException("set_id must be numeric");
            }
        } else {
            // assume "not in"
            $exclSetsQry=new select(array("p" => "photos"));
            $exclSetsQry->addFields(array("photo_id"), true);

            $exclSetsQry->join(array("ps" => "photo_sets"), "p.photo_id=ps.photo_id");

            $param=new param(":setIds" . $suffix, (array) $val, PDO::PARAM_INT);
            $exclSetsQry->addParam($param);
            $exclSetsQry->where(clause::InClause("ps.set_id", $param));

            $exclPhotoIds=$exclSetsQry->toArray();

            $param=new param(":photoIds" . $suffix, (array) $exclPhotoIds, PDO::PARAM_INT);
            $this->qry->addParam($param);
            $this->qry->addClause(clause::NotInClause("p.photo_id", $param), $conj);
        }
    }

    /**
     * Search for category by id
     * @param string val value of the field
     * @param string suffix, the suffix can be used to search for the same field multiple times
     * @param string operator, how the values should be compared (=, !=, like, etc.)
     * @param string conj, conjugation, whether this is an AND or OR search
     */
    private function processCategoryId($val, $suffix, $op, $conj) {
        if ($op == "=") {
            $alias = "pc" . $suffix;
            $this->qry->join(array($alias => "photo_categories"), "p.photo_id=" . $alias . ".photo_id");
            if (is_numeric($val)) {
                $this->qry->addClause(new clause($alias . ".category_id=:categoryId" . $suffix), $conj);
                $this->qry->addParam(new param(":categoryId" . $suffix, (int) $val, PDO::PARAM_INT));
            } else if (is_array($val)) {
                $param=new param(":categoryIds" . $suffix, $val, PDO::PARAM_INT);
                $this->qry->addParam($param);
                $this->qry->addClause(clause::InClause($alias . ".category_id", $param), $conj);
            } else {
                throw new \keyMustBeNumericSecurityException("category_id must be numeric");
            }
        } else {
            /* assume "not in" */
            $exclCategoryQry=new select(array("p" => "photos"));
            $exclCategoryQry->addFields(array("photo_id"), true);

            $exclCategoryQry->join(array("pc" => "photo_categories"), "p.photo_id=pc.photo_id");

            $param=new param(":categoryIds" . $suffix, (array) $val, PDO::PARAM_INT);
            $exclCategoryQry->addParam($param);
            $exclCategoryQry->where(clause::InClause("pc.category_id", $param));

            $exclPhotoIds=$exclCategoryQry->toArray();
            $param=new param(":photoIds" . $suffix, (array) $exclPhotoIds, PDO::PARAM_INT);
            $this->qry->addParam($param);
            $this->qry->addClause(clause::NotInClause("p.photo_id", $param), $conj);
        }
    }

    /**
     * Search for location by id
     * @param string val value of the field
     * @param string suffix, the suffix can be used to search for the same field multiple times
     * @param string operator, how the values should be compared (=, !=, like, etc.)
     * @param string conj, conjugation, whether this is an AND or OR search
     */
    private function processLocationId($val, $suffix, $op, $conj) {
        if (is_numeric($val)) {
            $this->qry->addParam(new param(":locationId" . $suffix, (int) $val, PDO::PARAM_INT));
            if ($op == "=") {
                $this->qry->addClause(new clause("p.location_id=:locationId" . $suffix), $conj);
            } else {
                $clause=new clause("p.location_id != :locationId" . $suffix);
                $clause->addOr(new clause("p.location_id is null"));
                $this->qry->addClause($clause, $conj);
            }
        } else if (is_array($val)) {
            $param=new param(":locationIds" . $suffix, $val, PDO::PARAM_INT);
            $this->qry->addParam($param);
            if ($op == "=") {
                $this->qry->addClause(clause::InClause("p.location_id", $param), $conj);
            } else {
                $this->qry->addClause(clause::NotInClause("p.location_id", $param), $conj);
            }
        } else {
            throw new \keyMustBeNumericSecurityException("location_id must be numeric");
        }
    }

    /**
     * Search for person by id
     * @param string val value of the field
     * @param string suffix, the suffix can be used to search for the same field multiple times
     * @param string operator, how the values should be compared (=, !=, like, etc.)
     * @param string conj, conjugation, whether this is an AND or OR search
     */
    private function processPersonId($val, $suffix, $op, $conj) {
        if ($op == "=") {
            $alias = "ppl" . $suffix;
            $this->qry->join(array($alias => "photo_people"), "p.photo_id=" . $alias . ".photo_id");
            if (is_numeric($val)) {
                $this->qry->addClause(new clause($alias . ".person_id=:personId" . $suffix), $conj);
                $this->qry->addParam(new param(":personId" . $suffix, (int) $val, PDO::PARAM_INT));
            } else if (is_array($val)) {
                $param=new param(":personIds" . $suffix, $val, PDO::PARAM_INT);
                $this->qry->addParam($param);
                $this->qry->addClause(clause::InClause($alias . ".person_id", $param), $conj);
            } else {
                throw new \keyMustBeNumericSecurityException("person_id must be numeric");
            }

        } else {
            // assume "not in"
            $exclPeopleQry=new select(array("p" => "photos"));
            $exclPeopleQry->addFields(array("photo_id"), true);

            $exclPeopleQry->join(array("ppl" => "photo_people"), "p.photo_id=ppl.photo_id");

            $param=new param(":personIds" . $suffix, (array) $val, PDO::PARAM_INT);
            $exclPeopleQry->addParam($param);
            $exclPeopleQry->where(clause::InClause("ppl.person_id", $param));

            $exclPhotoIds=$exclPeopleQry->toArray();

            if (!empty($exclPhotoIds)) {
                $param=new param(":photoIds" . $suffix, (array) $exclPhotoIds, PDO::PARAM_INT);
                $this->qry->addParam($param);
                $this->qry->addClause(clause::NotInClause("p.photo_id", $param), $conj);
            }
        }
    }

    /**
     * Search for user rating
     * @param string val value of the field
     * @param string suffix, the suffix can be used to search for the same field multiple times
     * @param string conj, conjugation, whether this is an AND or OR search
     */
    private function processUserRating($val, $suffix, $conj) {
        $user=user::getCurrent();
        if ($user->isAdmin() && isset($this->vars["_userrating_user"])) {
            $ratingUserId=$this->vars["_userrating_user"];
        } else {
            $ratingUserId=$user->getId();
        }
        if ($val != "null") {
            $alias = "pr" . $suffix;
            $this->qry->join(array($alias => "photo_ratings"), "p.photo_id=" . $alias . ".photo_id");

            $clause=new clause($alias . ".user_id=:ratingUserId" . $suffix);
            $clause->addAnd(new clause($alias . ".rating=:rating" . $suffix));

            $this->qry->addParam(new param(":ratingUserId", $ratingUserId, PDO::PARAM_INT));
            $this->qry->addParam(new param(":rating", $val, PDO::PARAM_INT));

            $this->qry->addClause($clause, $conj);
        } else {
            $noRateQry=new select(array("pr" => "photo_ratings"));
            $noRateQry->addFields(array("photo_id"), true);
            $noRateQry->where(new clause("pr.user_id=:ratingUserId"));
            $noRateQry->addParam(new param(":ratingUserId", $ratingUserId, PDO::PARAM_INT));

            $photoIds=$noRateQry->toArray();

            if (sizeof($photoIds) > 0) {
                $param=new param(":photoIds" . $suffix, (array) $photoIds, PDO::PARAM_INT);
                $this->qry->addParam($param);
                $this->qry->addClause(clause::NotInClause("p.photo_id", $param), $conj);
            }
        }
    }

    /**
     * Search for rating
     * @param string val value of the field
     * @param string suffix, the suffix can be used to search for the same field multiple times
     * @param string operator, how the values should be compared (=, !=, like, etc.)
     * @param string conj, conjugation, whether this is an AND or OR search
     */
    private function processRating($val, $suffix, $op, $conj) {
        $alias = "vpr" . $suffix;
        $this->qry->join(array($alias => "view_photo_avg_rating"), "p.photo_id=" . $alias . ".photo_id");
        if ($val=="null") {
            if ($op == "!=") {
                $clause=new clause($alias . ".rating is not null");
            } else if ($op == "=") {
                $clause=new clause($alias . ".rating is null");
            }
        } else {
            $this->qry->addParam(new param(":rating" . $suffix, $val, PDO::PARAM_INT));
            $clause=new clause($alias . ".rating " . $op . " :rating" . $suffix);
        }
        $this->qry->addClause($clause, $conj);
    }

    /**
     * Search for Latitude / longitude
     * @param float latitude value
     * @param float longitude value
     * @param string suffix, the suffix can be used to search for the same field multiple times
     * @param string conj, conjugation, whether this is an AND or OR search
     */
    private function processLatLon($lat, $lon, $suffix, $conj) {
        $ids=array();
        $distance=(float) $this->vars["_latlon_distance"];
        if (isset($this->vars["_latlon_entity"]) && $this->vars["_latlon_entity"]=="miles") {
            $distance=$distance * 1.609344;
        }
        if (isset($this->vars["_latlon_photos"])) {
            $photos=photo::getPhotosNear($lat, $lon, $distance, null);
            if ($photos) {
                foreach ($photos as $photo) {
                    $ids[]=$photo->getId();
                }
            }
        }
        if (isset($this->vars["_latlon_places"])) {
            $places=place::getPlacesNear($lat, $lon, $distance, null);
            foreach ($places as $place) {
                $photos=$place->getPhotos(user::getCurrent());
                foreach ($photos as $photo) {
                    $ids[]=$photo->getId();
                }
            }
        }
        if ($ids) {
            $param=new param(":photoIds" . $suffix, $ids, PDO::PARAM_INT);
            $this->qry->addParam($param);
            $this->qry->addClause(clause::InClause("p.photo_id", $param), $conj);
        } else {
            // No photos were found
            $this->qry->addClause(new clause("p.photo_id=-1"), $conj);
        }
    }

    /**
     * Search for other fields
     * @param string key name of the field
     * @param string val value of the field
     * @param string suffix, the suffix can be used to search for the same field multiple times
     * @param string operator, how the values should be compared (=, !=, like, etc.)
     * @param string conj, conjugation, whether this is an AND or OR search
     */
    private function processOtherFields($key, $val, $suffix, $op, $conj) {
        // any other field
        $clause=null;
        if (strncasecmp($key, "field", 5) == 0) {
            $key = $this->vars["_" . $key][$suffix];
        }
        if (!in_array($key, static::FIELDS)) {
            throw new \illegalValueSecurityException("Illegal field: " . e($key));
        }

        $val = e($val);
        $key = e($key);
        if ($val=="null") {
            if ($op == "!=") {
                $clause=new clause("p." . $key . " is not null");
            } else if ($op == "=") {
                $clause=new clause("p." . $key . " is null");
            }
        } else {
            $clause=new clause("p." . $key . " " . $op . " :" . $key . $suffix);

            if ($op == "like" || $op == "not like") {
                $val="%" . $val . "%";
            } else if ($op == "!=") {
                $clause->addOr(new clause("p." . $key . " is null"));
            }

            $this->qry->addParam(new param(":" . $key . $suffix, $val, PDO::PARAM_STR));

        }
        if ($clause instanceof clause) {
            $this->qry->addClause($clause, $conj);
        }
    }
}
?>